2 #include <mono/metadata/assembly.h>
3 #include <mono/metadata/tabledefs.h>
4 #include <mono/metadata/tokentype.h>
5 #include <mono/metadata/appdomain.h>
6 #include <mono/metadata/mono-debug.h>
7 #include <mono/metadata/mono-debug-debugger.h>
9 struct _MonoDebugHandlePriv
11 MonoDebuggerSymbolFile *debugger_info;
12 MonoDebugDomainData *domain_table;
15 struct _MonoDebugDomainDataPriv
17 GHashTable *wrapper_info;
18 MonoDebugDomainData *next;
21 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
23 static gboolean in_the_mono_debugger = FALSE;
24 static gboolean mono_debug_initialized = FALSE;
25 GHashTable *mono_debug_handles = NULL;
27 static MonoDebugHandle *mono_debug_open_image (MonoImage *image);
28 static void mono_debug_close_image (MonoDebugHandle *debug);
30 static MonoDebugHandle *_mono_debug_get_image (MonoImage *image);
31 static void mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data);
32 static void mono_debug_add_type (MonoClass *klass);
34 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
37 * Initialize debugging support.
39 * This method must be called after loading corlib,
40 * but before opening the application's main assembly because we need to set some
44 mono_debug_init (MonoDebugFormat format)
48 g_assert (!mono_debug_initialized);
50 mono_debug_initialized = TRUE;
51 mono_debug_format = format;
52 in_the_mono_debugger = format == MONO_DEBUG_FORMAT_DEBUGGER;
54 if (in_the_mono_debugger)
55 mono_debugger_initialize ();
57 mono_debugger_lock ();
59 mono_debug_handles = g_hash_table_new_full
60 (NULL, NULL, NULL, (GDestroyNotify) mono_debug_close_image);
62 mono_debugger_class_init_func = mono_debug_add_type;
63 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
65 mono_debug_open_image (mono_defaults.corlib);
66 for (ass = mono_defaults.corlib->references; ass && *ass; ass++)
67 mono_debug_open_image ((*ass)->image);
71 * Initialize debugging support - part 2.
73 * This method must be called after loading the application's main assembly.
76 mono_debug_init_2 (MonoAssembly *assembly)
78 mono_debug_open_image (assembly->image);
80 mono_debug_add_type (mono_defaults.object_class);
81 mono_debug_add_type (mono_defaults.object_class);
82 mono_debug_add_type (mono_defaults.byte_class);
83 mono_debug_add_type (mono_defaults.void_class);
84 mono_debug_add_type (mono_defaults.boolean_class);
85 mono_debug_add_type (mono_defaults.sbyte_class);
86 mono_debug_add_type (mono_defaults.int16_class);
87 mono_debug_add_type (mono_defaults.uint16_class);
88 mono_debug_add_type (mono_defaults.int32_class);
89 mono_debug_add_type (mono_defaults.uint32_class);
90 mono_debug_add_type (mono_defaults.int_class);
91 mono_debug_add_type (mono_defaults.uint_class);
92 mono_debug_add_type (mono_defaults.int64_class);
93 mono_debug_add_type (mono_defaults.uint64_class);
94 mono_debug_add_type (mono_defaults.single_class);
95 mono_debug_add_type (mono_defaults.double_class);
96 mono_debug_add_type (mono_defaults.char_class);
97 mono_debug_add_type (mono_defaults.string_class);
98 mono_debug_add_type (mono_defaults.enum_class);
99 mono_debug_add_type (mono_defaults.array_class);
100 mono_debug_add_type (mono_defaults.multicastdelegate_class);
101 mono_debug_add_type (mono_defaults.asyncresult_class);
102 mono_debug_add_type (mono_defaults.waithandle_class);
103 mono_debug_add_type (mono_defaults.typehandle_class);
104 mono_debug_add_type (mono_defaults.fieldhandle_class);
105 mono_debug_add_type (mono_defaults.methodhandle_class);
106 mono_debug_add_type (mono_defaults.monotype_class);
107 mono_debug_add_type (mono_defaults.exception_class);
108 mono_debug_add_type (mono_defaults.threadabortexception_class);
109 mono_debug_add_type (mono_defaults.thread_class);
110 mono_debug_add_type (mono_defaults.transparent_proxy_class);
111 mono_debug_add_type (mono_defaults.real_proxy_class);
112 mono_debug_add_type (mono_defaults.mono_method_message_class);
113 mono_debug_add_type (mono_defaults.appdomain_class);
114 mono_debug_add_type (mono_defaults.field_info_class);
115 mono_debug_add_type (mono_defaults.stringbuilder_class);
116 mono_debug_add_type (mono_defaults.math_class);
117 mono_debug_add_type (mono_defaults.stack_frame_class);
118 mono_debug_add_type (mono_defaults.stack_trace_class);
119 mono_debug_add_type (mono_defaults.marshal_class);
120 mono_debug_add_type (mono_defaults.iserializeable_class);
121 mono_debug_add_type (mono_defaults.serializationinfo_class);
122 mono_debug_add_type (mono_defaults.streamingcontext_class);
124 mono_debugger_unlock ();
128 mono_debug_cleanup (void)
130 mono_debugger_cleanup ();
132 if (mono_debug_handles)
133 g_hash_table_destroy (mono_debug_handles);
134 mono_debug_handles = NULL;
137 static MonoDebugHandle *
138 _mono_debug_get_image (MonoImage *image)
140 return g_hash_table_lookup (mono_debug_handles, image);
143 static MonoDebugHandle *
144 mono_debug_open_image (MonoImage *image)
146 MonoDebugHandle *handle;
148 handle = _mono_debug_get_image (image);
152 handle = g_new0 (MonoDebugHandle, 1);
153 handle->image = image;
154 handle->image->ref_count++;
155 handle->image_file = g_strdup (image->name);
156 handle->_priv = g_new0 (MonoDebugHandlePriv, 1);
158 g_hash_table_insert (mono_debug_handles, image, handle);
160 if (image->assembly->dynamic)
163 handle->symfile = mono_debug_open_mono_symbol_file (handle, in_the_mono_debugger);
164 if (in_the_mono_debugger)
165 handle->_priv->debugger_info = mono_debugger_add_symbol_file (handle);
171 mono_debug_close_image (MonoDebugHandle *handle)
174 mono_debug_close_mono_symbol_file (handle->symfile);
175 handle->image->ref_count--;
176 g_free (handle->_priv);
181 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
183 mono_debugger_lock ();
184 mono_debug_open_image (assembly->image);
185 mono_debugger_unlock ();
189 * This is called via the `mono_debugger_class_init_func' from mono_class_init() each time
190 * a new class is initialized.
193 mono_debug_add_type (MonoClass *klass)
195 MonoDebugHandle *handle;
197 handle = _mono_debug_get_image (klass->image);
200 if (handle->_priv->debugger_info)
201 mono_debugger_add_type (handle->_priv->debugger_info, klass);
204 struct LookupMethodData
206 MonoDebugMethodInfo *minfo;
211 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
213 MonoDebugHandle *handle = (MonoDebugHandle *) value;
214 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
220 data->minfo = mono_debug_find_method (handle->symfile, data->method);
223 static MonoDebugMethodInfo *
224 _mono_debug_lookup_method (MonoMethod *method)
226 struct LookupMethodData data = { NULL, method };
228 if (!mono_debug_handles)
231 g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
236 * This is called by the JIT to tell the debugging code about a newly compiled
240 mono_debug_add_wrapper (MonoMethod *method, MonoMethod *wrapper_method, MonoDomain *domain)
242 MonoClass *klass = method->klass;
243 MonoDebugHandle *handle;
244 MonoDebugMethodInfo *minfo;
245 MonoDebugMethodJitInfo *jit;
246 MonoDebugDomainData *domain_data;
248 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
251 mono_class_init (klass);
253 handle = _mono_debug_get_image (klass->image);
256 minfo = _mono_debug_lookup_method (method);
260 domain_data = mono_debug_get_domain_data (handle, domain);
261 g_assert (!domain_data->jit [minfo->index]);
263 jit = g_hash_table_lookup (domain_data->_priv->wrapper_info, wrapper_method);
266 mono_debugger_lock ();
268 domain_data->jit [minfo->index] = jit;
269 jit->wrapper_addr = method->addr;
271 if (handle->_priv->debugger_info && (domain == mono_root_domain))
272 mono_debugger_add_method (handle->_priv->debugger_info, minfo, jit);
274 mono_debugger_unlock ();
278 * This is called by the JIT to tell the debugging code about a newly
282 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
284 MonoClass *klass = method->klass;
285 MonoDebugDomainData *domain_data;
286 MonoDebugHandle *handle;
287 MonoDebugMethodInfo *minfo;
289 mono_class_init (klass);
291 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
292 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
293 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
294 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
297 handle = _mono_debug_get_image (klass->image);
300 minfo = _mono_debug_lookup_method (method);
304 mono_debugger_lock ();
306 domain_data = mono_debug_get_domain_data (handle, domain);
307 g_assert (!domain_data->jit [minfo->index]);
309 if (method->wrapper_type != MONO_WRAPPER_NONE) {
310 g_hash_table_insert (domain_data->_priv->wrapper_info, method, jit);
311 mono_debugger_unlock ();
315 domain_data->jit [minfo->index] = jit;
317 if (handle->_priv->debugger_info && (domain == mono_root_domain))
318 mono_debugger_add_method (handle->_priv->debugger_info, minfo, jit);
320 mono_debugger_unlock ();
324 il_offset_from_address (MonoDebugMethodJitInfo *jit, guint32 address)
328 if (!jit || !jit->line_numbers)
331 for (i = jit->line_numbers->len - 1; i >= 0; i--) {
332 MonoDebugLineNumberEntry lne = g_array_index (
333 jit->line_numbers, MonoDebugLineNumberEntry, i);
335 if (lne.address <= address)
343 * Used by the exception code to get a source location from a machine address.
345 * Returns a textual representation of the specified address which is suitable to be displayed to
346 * the user (for instance "/home/martin/monocvs/debugger/test/Y.cs:8").
348 * If the optional @line_number argument is not NULL, the line number is stored there and just the
349 * source file is returned (ie. it'd return "/home/martin/monocvs/debugger/test/Y.cs" and store the
350 * line number 8 in the variable pointed to by @line_number).
353 mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number,
356 MonoDebugMethodInfo *minfo = _mono_debug_lookup_method (method);
357 MonoDebugDomainData *domain_data;
362 domain_data = mono_debug_get_domain_data (minfo->handle, domain);
363 if (!domain_data->jit [minfo->index])
367 gint32 offset = il_offset_from_address (domain_data->jit [minfo->index], address);
372 return mono_debug_find_source_location (minfo->handle->symfile, method, offset, line_number);
379 * Used by the exception code to get a source location from an IL offset.
381 * Returns a textual representation of the specified address which is suitable to be displayed to
382 * the user (for instance "/home/martin/monocvs/debugger/test/Y.cs:8").
384 * If the optional @line_number argument is not NULL, the line number is stored there and just the
385 * source file is returned (ie. it'd return "/home/martin/monocvs/debugger/test/Y.cs" and store the
386 * line number 8 in the variable pointed to by @line_number).
389 mono_debug_source_location_from_il_offset (MonoMethod *method, guint32 offset, guint32 *line_number)
391 MonoDebugMethodInfo *minfo = _mono_debug_lookup_method (method);
393 if (!minfo || !minfo->handle)
396 return mono_debug_find_source_location (minfo->handle->symfile, method, offset, line_number);
400 * Returns the IL offset corresponding to machine address @address which is an offset
401 * relative to the beginning of the method @method.
404 mono_debug_il_offset_from_address (MonoMethod *method, gint32 address, MonoDomain *domain)
406 MonoDebugMethodInfo *minfo;
407 MonoDebugDomainData *domain_data;
412 minfo = _mono_debug_lookup_method (method);
413 if (!minfo || !minfo->il_offsets)
416 domain_data = mono_debug_get_domain_data (minfo->handle, domain);
418 return il_offset_from_address (domain_data->jit [minfo->index], address);
422 * Returns the machine address corresponding to IL offset @il_offset.
423 * The returned value is an offset relative to the beginning of the method @method.
426 mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset, MonoDomain *domain)
428 MonoDebugMethodInfo *minfo;
429 MonoDebugDomainData *domain_data;
434 minfo = _mono_debug_lookup_method (method);
435 if (!minfo || !minfo->il_offsets)
438 domain_data = mono_debug_get_domain_data (minfo->handle, domain);
440 return _mono_debug_address_from_il_offset (domain_data->jit [minfo->index], il_offset);
443 MonoDebugDomainData *
444 mono_debug_get_domain_data (MonoDebugHandle *handle, MonoDomain *domain)
446 MonoDebugDomainData *data;
448 for (data = handle->_priv->domain_table; data; data = data->_priv->next)
449 if (data->domain_id == domain->domain_id)
452 data = g_new0 (MonoDebugDomainData, 1);
453 data->domain_id = domain->domain_id;
454 data->jit = g_new0 (MonoDebugMethodJitInfo *, handle->symfile->offset_table->method_count + 1);
456 data->_priv = g_new0 (MonoDebugDomainDataPriv, 1);
457 data->_priv->next = handle->_priv->domain_table;
458 data->_priv->wrapper_info = g_hash_table_new (g_direct_hash, g_direct_equal);
459 handle->_priv->domain_table = data;