2 * debug-mini.c: Mini-specific debugging stuff.
5 * Martin Baulig (martin@ximian.com)
7 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/verify.h>
13 #include <mono/metadata/mono-config.h>
14 #include <mono/metadata/mono-debug.h>
15 #include <mono/metadata/appdomain.h>
16 /* mono-debug-debugger.h needs config.h to work... */
18 #include <mono/metadata/mono-debug-debugger.h>
20 #ifdef HAVE_VALGRIND_H
21 #include <valgrind/valgrind.h>
26 MonoDebugMethodJitInfo *jit;
28 guint32 has_line_numbers;
29 guint32 breakpoint_id;
30 } MiniDebugMethodInfo;
33 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
35 MonoDebugLineNumberEntry lne;
37 lne.native_offset = address;
38 lne.il_offset = offset;
40 g_array_append_val (info->line_numbers, lne);
44 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
46 MonoMethod *method = cfg->method;
47 MiniDebugMethodInfo *info;
49 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
52 info = g_new0 (MiniDebugMethodInfo, 1);
53 info->breakpoint_id = breakpoint_id;
55 cfg->debug_info = info;
59 mono_debug_open_method (MonoCompile *cfg)
61 MiniDebugMethodInfo *info;
62 MonoDebugMethodJitInfo *jit;
63 MonoMethodHeader *header;
65 info = (MiniDebugMethodInfo *) cfg->debug_info;
69 mono_class_init (cfg->method->klass);
71 header = mono_method_get_header (cfg->method);
74 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
75 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
76 jit->num_locals = header->num_locals;
77 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
81 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
83 if (inst->opcode == OP_REGVAR)
84 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
86 /* the debug interface needs fixing to allow 0(%base) address */
87 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
88 var->offset = inst->inst_offset;
93 * mono_debug_add_vg_method:
95 * Register symbol information for the method with valgrind
98 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
100 #ifdef VALGRIND_ADD_LINE_INFO
101 MonoMethodHeader *header;
103 char *filename = NULL;
104 guint32 address, line_number;
105 const char *full_name;
109 if (!RUNNING_ON_VALGRIND)
112 header = mono_method_get_header (method);
114 full_name = mono_method_full_name (method, TRUE);
116 addresses = g_new0 (guint32, header->code_size + 1);
117 lines = g_new0 (guint32, header->code_size + 1);
120 * Very simple code to convert the addr->offset mappings that mono has
121 * into [addr-addr] ->line number mappings.
124 /* Create offset->line number mapping */
125 for (i = 0; i < header->code_size; ++i) {
128 fname = mono_debug_source_location_from_il_offset (method, i, &lines [i]);
133 /* Create address->offset mapping */
134 for (i = 0; i < jit->num_line_numbers; ++i) {
135 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
137 g_assert (lne->offset <= header->code_size);
139 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
140 addresses [lne->offset] = lne->address;
142 /* Fill out missing addresses */
144 for (i = 0; i < header->code_size; ++i) {
145 if (addresses [i] == 0)
146 addresses [i] = address;
148 address = addresses [i];
154 while (i < header->code_size) {
155 if (lines [i] == line_number)
158 if (line_number > 0) {
159 //g_assert (addresses [i] - 1 >= address);
161 if (addresses [i] - 1 >= address) {
162 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
163 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
166 address = addresses [i];
167 line_number = lines [i];
171 if (line_number > 0) {
172 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
173 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
176 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
180 #endif /* VALGRIND_ADD_LINE_INFO */
184 mono_debug_close_method (MonoCompile *cfg)
186 MiniDebugMethodInfo *info;
187 MonoDebugMethodJitInfo *jit;
188 MonoMethodHeader *header;
189 MonoMethodSignature *sig;
193 info = (MiniDebugMethodInfo *) cfg->debug_info;
194 if (!info || !info->jit) {
200 method = cfg->method;
201 header = mono_method_get_header (method);
202 sig = mono_method_signature (method);
205 jit->code_start = cfg->native_code;
206 jit->epilogue_begin = cfg->epilog_begin;
207 jit->code_size = cfg->code_len;
209 record_line_number (info, jit->epilogue_begin, header->code_size);
211 jit->num_params = sig->param_count;
212 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
214 for (i = 0; i < jit->num_locals; i++)
215 write_variable (cfg->varinfo [cfg->locals_start + i], &jit->locals [i]);
218 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
219 write_variable (cfg->varinfo [0], jit->this_var);
222 for (i = 0; i < jit->num_params; i++)
223 write_variable (cfg->varinfo [i + sig->hasthis], &jit->params [i]);
225 jit->num_line_numbers = info->line_numbers->len;
226 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
228 for (i = 0; i < jit->num_line_numbers; i++)
229 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
231 mono_debug_add_method (method, jit, cfg->domain);
233 mono_debug_add_vg_method (method, jit);
235 if (info->breakpoint_id)
236 mono_debugger_breakpoint_callback (method, info->breakpoint_id);
238 mono_debug_free_method_jit_info (jit);
239 g_array_free (info->line_numbers, TRUE);
244 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
246 MiniDebugMethodInfo *info;
247 MonoMethodHeader *header;
250 info = (MiniDebugMethodInfo *) cfg->debug_info;
251 if (!info || !info->jit || !ins->cil_code)
254 header = mono_method_get_header (cfg->method);
257 if ((ins->cil_code < header->code) ||
258 (ins->cil_code > header->code + header->code_size))
261 offset = ins->cil_code - header->code;
262 if (!info->has_line_numbers) {
263 info->jit->prologue_end = address;
264 info->has_line_numbers = TRUE;
267 record_line_number (info, address, offset);
271 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
275 //printf ("ENCODE: %d 0x%x.\n", value, value);
278 * Same encoding as the one used in the metadata, extended to handle values
279 * greater than 0x1fffffff.
281 if ((value >= 0) && (value <= 127))
283 else if ((value >= 0) && (value <= 16383)) {
284 p [0] = 0x80 | (value >> 8);
285 p [1] = value & 0xff;
287 } else if ((value >= 0) && (value <= 0x1fffffff)) {
288 p [0] = (value >> 24) | 0xc0;
289 p [1] = (value >> 16) & 0xff;
290 p [2] = (value >> 8) & 0xff;
291 p [3] = value & 0xff;
296 p [1] = (value >> 24) & 0xff;
297 p [2] = (value >> 16) & 0xff;
298 p [3] = (value >> 8) & 0xff;
299 p [4] = value & 0xff;
307 decode_value (guint8 *ptr, guint8 **rptr)
312 if ((b & 0x80) == 0){
315 } else if ((b & 0x40) == 0){
316 len = ((b & 0x3f) << 8 | ptr [1]);
318 } else if (b != 0xff) {
319 len = ((b & 0x1f) << 24) |
326 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
332 //printf ("DECODE: %d.\n", len);
337 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
339 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
342 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
343 encode_value (var->index, p, &p);
345 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
346 encode_value (var->index, p, &p);
347 encode_value (var->offset, p, &p);
350 g_assert_not_reached ();
356 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
358 MiniDebugMethodInfo *info;
359 MonoDebugMethodJitInfo *jit;
360 guint32 size, prev_offset, prev_native_offset;
364 info = (MiniDebugMethodInfo *) cfg->debug_info;
365 if (!info || !info->jit) {
371 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
372 p = buf = g_malloc (size);
373 encode_value (jit->epilogue_begin, p, &p);
374 encode_value (jit->prologue_end, p, &p);
375 encode_value (jit->code_size, p, &p);
377 for (i = 0; i < jit->num_params; ++i)
378 serialize_variable (&jit->params [i], p, &p);
380 if (mono_method_signature (cfg->method)->hasthis)
381 serialize_variable (jit->this_var, p, &p);
383 for (i = 0; i < jit->num_locals; i++)
384 serialize_variable (&jit->locals [i], p, &p);
386 encode_value (jit->num_line_numbers, p, &p);
389 prev_native_offset = 0;
390 for (i = 0; i < jit->num_line_numbers; ++i) {
391 /* Sometimes, the offset values are not in increasing order */
392 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
393 encode_value (lne->il_offset - prev_offset, p, &p);
394 encode_value (lne->native_offset - prev_native_offset, p, &p);
395 prev_offset = lne->il_offset;
396 prev_native_offset = lne->native_offset;
399 g_assert (p - buf < size);
406 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
410 var->index = decode_value (p, &p);
412 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
415 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
417 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
418 var->offset = decode_value (p, &p);
421 g_assert_not_reached ();
426 static MonoDebugMethodJitInfo *
427 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
429 MonoMethodHeader *header;
430 gint32 offset, native_offset, prev_offset, prev_native_offset;
431 MonoDebugMethodJitInfo *jit;
435 header = mono_method_get_header (method);
438 jit = g_new0 (MonoDebugMethodJitInfo, 1);
439 jit->code_start = code_start;
440 jit->num_locals = header->num_locals;
441 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
442 jit->num_params = mono_method_signature (method)->param_count;
443 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
446 jit->epilogue_begin = decode_value (p, &p);
447 jit->prologue_end = decode_value (p, &p);
448 jit->code_size = decode_value (p, &p);
450 for (i = 0; i < jit->num_params; ++i)
451 deserialize_variable (&jit->params [i], p, &p);
453 if (mono_method_signature (method)->hasthis) {
454 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
455 deserialize_variable (jit->this_var, p, &p);
458 for (i = 0; i < jit->num_locals; i++)
459 deserialize_variable (&jit->locals [i], p, &p);
461 jit->num_line_numbers = decode_value (p, &p);
462 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
465 prev_native_offset = 0;
466 for (i = 0; i < jit->num_line_numbers; ++i) {
467 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
469 offset = prev_offset + decode_value (p, &p);
470 native_offset = prev_native_offset + decode_value (p, &p);
472 lne->native_offset = native_offset;
473 lne->il_offset = offset;
475 prev_offset = offset;
476 prev_native_offset = native_offset;
483 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
484 guint8 *debug_info, guint32 debug_info_len)
486 MonoDebugMethodJitInfo *jit;
488 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
491 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
492 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
493 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
494 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
495 (method->wrapper_type != MONO_WRAPPER_NONE))
498 if (debug_info_len == 0)
501 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
504 jit = mono_debug_read_method ((MonoDebugMethodAddress *) debug_info);
505 jit->code_start = code_start;
506 jit->wrapper_addr = NULL;
509 mono_debug_add_method (method, jit, domain);
511 mono_debug_add_vg_method (method, jit);
513 mono_debug_free_method_jit_info (jit);
517 mono_init_debugger (const char *file, const char *opt_flags)
523 g_set_prgname (file);
525 opt = mono_parse_default_optimizations (opt_flags);
526 opt |= MONO_OPT_SHARED;
528 mono_set_defaults (0, opt);
530 domain = mono_jit_init (file);
532 mono_config_parse (NULL);
534 error = mono_check_corlib_version ();
536 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
537 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
545 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
547 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
550 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);