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 MiniDebugMethodInfo *info;
48 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
51 info = g_new0 (MiniDebugMethodInfo, 1);
52 info->breakpoint_id = breakpoint_id;
54 cfg->debug_info = info;
58 mono_debug_open_method (MonoCompile *cfg)
60 MiniDebugMethodInfo *info;
61 MonoDebugMethodJitInfo *jit;
62 MonoMethodHeader *header;
64 info = (MiniDebugMethodInfo *) cfg->debug_info;
68 mono_class_init (cfg->method->klass);
70 header = mono_method_get_header (cfg->method);
73 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
74 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
75 jit->num_locals = header->num_locals;
76 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
80 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
82 if (inst->opcode == OP_REGVAR)
83 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
85 /* the debug interface needs fixing to allow 0(%base) address */
86 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
87 var->offset = inst->inst_offset;
92 * mono_debug_add_vg_method:
94 * Register symbol information for the method with valgrind
97 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
99 #ifdef VALGRIND_ADD_LINE_INFO
100 MonoMethodHeader *header;
102 char *filename = NULL;
103 guint32 address, line_number;
104 const char *full_name;
108 if (!RUNNING_ON_VALGRIND)
111 header = mono_method_get_header (method);
113 full_name = mono_method_full_name (method, TRUE);
115 addresses = g_new0 (guint32, header->code_size + 1);
116 lines = g_new0 (guint32, header->code_size + 1);
119 * Very simple code to convert the addr->offset mappings that mono has
120 * into [addr-addr] ->line number mappings.
123 /* Create offset->line number mapping */
124 for (i = 0; i < header->code_size; ++i) {
127 fname = mono_debug_source_location_from_il_offset (method, i, &lines [i]);
132 /* Create address->offset mapping */
133 for (i = 0; i < jit->num_line_numbers; ++i) {
134 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
136 g_assert (lne->offset <= header->code_size);
138 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
139 addresses [lne->offset] = lne->address;
141 /* Fill out missing addresses */
143 for (i = 0; i < header->code_size; ++i) {
144 if (addresses [i] == 0)
145 addresses [i] = address;
147 address = addresses [i];
153 while (i < header->code_size) {
154 if (lines [i] == line_number)
157 if (line_number > 0) {
158 //g_assert (addresses [i] - 1 >= address);
160 if (addresses [i] - 1 >= address) {
161 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
162 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
165 address = addresses [i];
166 line_number = lines [i];
170 if (line_number > 0) {
171 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
172 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
175 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
179 #endif /* VALGRIND_ADD_LINE_INFO */
183 mono_debug_close_method (MonoCompile *cfg)
185 MiniDebugMethodInfo *info;
186 MonoDebugMethodJitInfo *jit;
187 MonoMethodHeader *header;
188 MonoMethodSignature *sig;
192 info = (MiniDebugMethodInfo *) cfg->debug_info;
193 if (!info || !info->jit) {
199 method = cfg->method;
200 header = mono_method_get_header (method);
201 sig = mono_method_signature (method);
204 jit->code_start = cfg->native_code;
205 jit->epilogue_begin = cfg->epilog_begin;
206 jit->code_size = cfg->code_len;
208 record_line_number (info, jit->epilogue_begin, header->code_size);
210 jit->num_params = sig->param_count;
211 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
213 for (i = 0; i < jit->num_locals; i++)
214 write_variable (cfg->varinfo [cfg->locals_start + i], &jit->locals [i]);
217 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
218 write_variable (cfg->varinfo [0], jit->this_var);
221 for (i = 0; i < jit->num_params; i++)
222 write_variable (cfg->varinfo [i + sig->hasthis], &jit->params [i]);
224 jit->num_line_numbers = info->line_numbers->len;
225 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
227 for (i = 0; i < jit->num_line_numbers; i++)
228 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
230 mono_debug_add_method (method, jit, cfg->domain);
232 mono_debug_add_vg_method (method, jit);
234 if (info->breakpoint_id)
235 mono_debugger_breakpoint_callback (method, info->breakpoint_id);
237 mono_debug_free_method_jit_info (jit);
238 g_array_free (info->line_numbers, TRUE);
243 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
245 MiniDebugMethodInfo *info;
246 MonoMethodHeader *header;
249 info = (MiniDebugMethodInfo *) cfg->debug_info;
250 if (!info || !info->jit || !ins->cil_code)
253 header = mono_method_get_header (cfg->method);
256 if ((ins->cil_code < header->code) ||
257 (ins->cil_code > header->code + header->code_size))
260 offset = ins->cil_code - header->code;
261 if (!info->has_line_numbers) {
262 info->jit->prologue_end = address;
263 info->has_line_numbers = TRUE;
266 record_line_number (info, address, offset);
270 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
274 //printf ("ENCODE: %d 0x%x.\n", value, value);
277 * Same encoding as the one used in the metadata, extended to handle values
278 * greater than 0x1fffffff.
280 if ((value >= 0) && (value <= 127))
282 else if ((value >= 0) && (value <= 16383)) {
283 p [0] = 0x80 | (value >> 8);
284 p [1] = value & 0xff;
286 } else if ((value >= 0) && (value <= 0x1fffffff)) {
287 p [0] = (value >> 24) | 0xc0;
288 p [1] = (value >> 16) & 0xff;
289 p [2] = (value >> 8) & 0xff;
290 p [3] = value & 0xff;
295 p [1] = (value >> 24) & 0xff;
296 p [2] = (value >> 16) & 0xff;
297 p [3] = (value >> 8) & 0xff;
298 p [4] = value & 0xff;
306 decode_value (guint8 *ptr, guint8 **rptr)
311 if ((b & 0x80) == 0){
314 } else if ((b & 0x40) == 0){
315 len = ((b & 0x3f) << 8 | ptr [1]);
317 } else if (b != 0xff) {
318 len = ((b & 0x1f) << 24) |
325 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
331 //printf ("DECODE: %d.\n", len);
336 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
338 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
341 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
342 encode_value (var->index, p, &p);
344 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
345 encode_value (var->index, p, &p);
346 encode_value (var->offset, p, &p);
349 g_assert_not_reached ();
355 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
357 MiniDebugMethodInfo *info;
358 MonoDebugMethodJitInfo *jit;
359 guint32 size, prev_offset, prev_native_offset;
363 info = (MiniDebugMethodInfo *) cfg->debug_info;
364 if (!info || !info->jit) {
370 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
371 p = buf = g_malloc (size);
372 encode_value (jit->epilogue_begin, p, &p);
373 encode_value (jit->prologue_end, p, &p);
374 encode_value (jit->code_size, p, &p);
376 for (i = 0; i < jit->num_params; ++i)
377 serialize_variable (&jit->params [i], p, &p);
379 if (mono_method_signature (cfg->method)->hasthis)
380 serialize_variable (jit->this_var, p, &p);
382 for (i = 0; i < jit->num_locals; i++)
383 serialize_variable (&jit->locals [i], p, &p);
385 encode_value (jit->num_line_numbers, p, &p);
388 prev_native_offset = 0;
389 for (i = 0; i < jit->num_line_numbers; ++i) {
390 /* Sometimes, the offset values are not in increasing order */
391 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
392 encode_value (lne->il_offset - prev_offset, p, &p);
393 encode_value (lne->native_offset - prev_native_offset, p, &p);
394 prev_offset = lne->il_offset;
395 prev_native_offset = lne->native_offset;
398 g_assert (p - buf < size);
405 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
409 var->index = decode_value (p, &p);
411 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
414 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
416 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
417 var->offset = decode_value (p, &p);
420 g_assert_not_reached ();
425 static MonoDebugMethodJitInfo *
426 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
428 MonoMethodHeader *header;
429 gint32 offset, native_offset, prev_offset, prev_native_offset;
430 MonoDebugMethodJitInfo *jit;
434 header = mono_method_get_header (method);
437 jit = g_new0 (MonoDebugMethodJitInfo, 1);
438 jit->code_start = code_start;
439 jit->num_locals = header->num_locals;
440 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
441 jit->num_params = mono_method_signature (method)->param_count;
442 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
445 jit->epilogue_begin = decode_value (p, &p);
446 jit->prologue_end = decode_value (p, &p);
447 jit->code_size = decode_value (p, &p);
449 for (i = 0; i < jit->num_params; ++i)
450 deserialize_variable (&jit->params [i], p, &p);
452 if (mono_method_signature (method)->hasthis) {
453 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
454 deserialize_variable (jit->this_var, p, &p);
457 for (i = 0; i < jit->num_locals; i++)
458 deserialize_variable (&jit->locals [i], p, &p);
460 jit->num_line_numbers = decode_value (p, &p);
461 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
464 prev_native_offset = 0;
465 for (i = 0; i < jit->num_line_numbers; ++i) {
466 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
468 offset = prev_offset + decode_value (p, &p);
469 native_offset = prev_native_offset + decode_value (p, &p);
471 lne->native_offset = native_offset;
472 lne->il_offset = offset;
474 prev_offset = offset;
475 prev_native_offset = native_offset;
482 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
483 guint8 *debug_info, guint32 debug_info_len)
485 MonoDebugMethodJitInfo *jit;
487 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
490 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
491 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
492 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
493 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
494 (method->wrapper_type != MONO_WRAPPER_NONE))
497 if (debug_info_len == 0)
500 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
503 jit = mono_debug_read_method ((MonoDebugMethodAddress *) debug_info);
504 jit->code_start = code_start;
505 jit->wrapper_addr = NULL;
508 mono_debug_add_method (method, jit, domain);
510 mono_debug_add_vg_method (method, jit);
512 mono_debug_free_method_jit_info (jit);
516 mono_init_debugger (const char *file, const char *opt_flags)
522 g_set_prgname (file);
524 opt = mono_parse_default_optimizations (opt_flags);
525 opt |= MONO_OPT_SHARED;
527 mono_set_defaults (0, opt);
529 domain = mono_jit_init (file);
531 mono_config_parse (NULL);
533 error = mono_check_corlib_version ();
535 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
536 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
544 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
546 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
549 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);