2 * debug-mini.c: Mini-specific debugging stuff.
5 * Martin Baulig (martin@ximian.com)
7 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/mono-config.h>
15 #include <mono/metadata/mono-debug.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/mono-debug-debugger.h>
19 #ifdef HAVE_VALGRIND_H
20 #include <valgrind/valgrind.h>
25 MonoDebugMethodJitInfo *jit;
27 guint32 has_line_numbers;
28 guint32 breakpoint_id;
29 } MiniDebugMethodInfo;
32 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
34 MonoDebugLineNumberEntry lne;
36 lne.native_offset = address;
37 lne.il_offset = offset;
39 g_array_append_val (info->line_numbers, lne);
43 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
45 MiniDebugMethodInfo *info;
47 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
50 info = g_new0 (MiniDebugMethodInfo, 1);
51 info->breakpoint_id = breakpoint_id;
53 cfg->debug_info = info;
57 mono_debug_open_method (MonoCompile *cfg)
59 MiniDebugMethodInfo *info;
60 MonoDebugMethodJitInfo *jit;
61 MonoMethodHeader *header;
63 info = (MiniDebugMethodInfo *) cfg->debug_info;
67 mono_class_init (cfg->method->klass);
69 header = mono_method_get_header (cfg->method);
72 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
73 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
74 jit->num_locals = header->num_locals;
75 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
79 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
81 if (inst->opcode == OP_REGVAR)
82 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
84 /* the debug interface needs fixing to allow 0(%base) address */
85 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
86 var->offset = inst->inst_offset;
91 * mono_debug_add_vg_method:
93 * Register symbol information for the method with valgrind
96 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
98 #ifdef VALGRIND_ADD_LINE_INFO
99 MonoMethodHeader *header;
100 MonoDebugMethodInfo *minfo;
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 minfo = mono_debug_lookup_method (method);
125 /* Create offset->line number mapping */
126 for (i = 0; i < header->code_size; ++i) {
127 MonoDebugSourceLocation *location;
129 location = mono_debug_symfile_lookup_location (minfo, i);
133 lines [i] = location.row;
135 filename = location.source_file;
137 mono_debug_free_source_location (location);
141 /* Create address->offset mapping */
142 for (i = 0; i < jit->num_line_numbers; ++i) {
143 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
145 g_assert (lne->offset <= header->code_size);
147 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
148 addresses [lne->offset] = lne->address;
150 /* Fill out missing addresses */
152 for (i = 0; i < header->code_size; ++i) {
153 if (addresses [i] == 0)
154 addresses [i] = address;
156 address = addresses [i];
162 while (i < header->code_size) {
163 if (lines [i] == line_number)
166 if (line_number > 0) {
167 //g_assert (addresses [i] - 1 >= address);
169 if (addresses [i] - 1 >= address) {
170 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
171 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
174 address = addresses [i];
175 line_number = lines [i];
179 if (line_number > 0) {
180 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
181 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
184 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
188 #endif /* VALGRIND_ADD_LINE_INFO */
192 mono_debug_close_method (MonoCompile *cfg)
194 MiniDebugMethodInfo *info;
195 MonoDebugMethodJitInfo *jit;
196 MonoMethodHeader *header;
197 MonoMethodSignature *sig;
201 info = (MiniDebugMethodInfo *) cfg->debug_info;
202 if (!info || !info->jit) {
208 method = cfg->method;
209 header = mono_method_get_header (method);
210 sig = mono_method_signature (method);
213 jit->code_start = cfg->native_code;
214 jit->epilogue_begin = cfg->epilog_begin;
215 jit->code_size = cfg->code_len;
217 record_line_number (info, jit->epilogue_begin, header->code_size);
219 jit->num_params = sig->param_count;
220 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
222 for (i = 0; i < jit->num_locals; i++)
223 write_variable (cfg->varinfo [cfg->locals_start + i], &jit->locals [i]);
226 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
227 write_variable (cfg->varinfo [0], jit->this_var);
230 for (i = 0; i < jit->num_params; i++)
231 write_variable (cfg->varinfo [i + sig->hasthis], &jit->params [i]);
233 jit->num_line_numbers = info->line_numbers->len;
234 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
236 for (i = 0; i < jit->num_line_numbers; i++)
237 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
239 mono_debug_add_method (method, jit, cfg->domain);
241 mono_debug_add_vg_method (method, jit);
243 if (info->breakpoint_id)
244 mono_debugger_breakpoint_callback (method, info->breakpoint_id);
246 mono_debug_free_method_jit_info (jit);
247 g_array_free (info->line_numbers, TRUE);
252 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
254 MiniDebugMethodInfo *info;
255 MonoMethodHeader *header;
258 info = (MiniDebugMethodInfo *) cfg->debug_info;
259 if (!info || !info->jit || !ins->cil_code)
262 header = mono_method_get_header (cfg->method);
265 if ((ins->cil_code < header->code) ||
266 (ins->cil_code > header->code + header->code_size))
269 offset = ins->cil_code - header->code;
270 if (!info->has_line_numbers) {
271 info->jit->prologue_end = address;
272 info->has_line_numbers = TRUE;
275 record_line_number (info, address, offset);
279 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
281 MiniDebugMethodInfo *info;
282 MonoMethodHeader *header;
285 info = (MiniDebugMethodInfo *) cfg->debug_info;
286 if (!info || !info->jit || !bb->cil_code)
289 header = mono_method_get_header (cfg->method);
292 if ((bb->cil_code < header->code) ||
293 (bb->cil_code > header->code + header->code_size))
296 offset = bb->cil_code - header->code;
297 if (!info->has_line_numbers) {
298 info->jit->prologue_end = address;
299 info->has_line_numbers = TRUE;
302 record_line_number (info, address, offset);
306 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
310 //printf ("ENCODE: %d 0x%x.\n", value, value);
313 * Same encoding as the one used in the metadata, extended to handle values
314 * greater than 0x1fffffff.
316 if ((value >= 0) && (value <= 127))
318 else if ((value >= 0) && (value <= 16383)) {
319 p [0] = 0x80 | (value >> 8);
320 p [1] = value & 0xff;
322 } else if ((value >= 0) && (value <= 0x1fffffff)) {
323 p [0] = (value >> 24) | 0xc0;
324 p [1] = (value >> 16) & 0xff;
325 p [2] = (value >> 8) & 0xff;
326 p [3] = value & 0xff;
331 p [1] = (value >> 24) & 0xff;
332 p [2] = (value >> 16) & 0xff;
333 p [3] = (value >> 8) & 0xff;
334 p [4] = value & 0xff;
342 decode_value (guint8 *ptr, guint8 **rptr)
347 if ((b & 0x80) == 0){
350 } else if ((b & 0x40) == 0){
351 len = ((b & 0x3f) << 8 | ptr [1]);
353 } else if (b != 0xff) {
354 len = ((b & 0x1f) << 24) |
361 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
367 //printf ("DECODE: %d.\n", len);
372 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
374 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
377 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
378 encode_value (var->index, p, &p);
380 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
381 encode_value (var->index, p, &p);
382 encode_value (var->offset, p, &p);
385 g_assert_not_reached ();
391 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
393 MiniDebugMethodInfo *info;
394 MonoDebugMethodJitInfo *jit;
395 guint32 size, prev_offset, prev_native_offset;
399 info = (MiniDebugMethodInfo *) cfg->debug_info;
400 if (!info || !info->jit) {
406 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
407 p = buf = g_malloc (size);
408 encode_value (jit->epilogue_begin, p, &p);
409 encode_value (jit->prologue_end, p, &p);
410 encode_value (jit->code_size, p, &p);
412 for (i = 0; i < jit->num_params; ++i)
413 serialize_variable (&jit->params [i], p, &p);
415 if (mono_method_signature (cfg->method)->hasthis)
416 serialize_variable (jit->this_var, p, &p);
418 for (i = 0; i < jit->num_locals; i++)
419 serialize_variable (&jit->locals [i], p, &p);
421 encode_value (jit->num_line_numbers, p, &p);
424 prev_native_offset = 0;
425 for (i = 0; i < jit->num_line_numbers; ++i) {
426 /* Sometimes, the offset values are not in increasing order */
427 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
428 encode_value (lne->il_offset - prev_offset, p, &p);
429 encode_value (lne->native_offset - prev_native_offset, p, &p);
430 prev_offset = lne->il_offset;
431 prev_native_offset = lne->native_offset;
434 g_assert (p - buf < size);
441 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
445 var->index = decode_value (p, &p);
447 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
450 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
452 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
453 var->offset = decode_value (p, &p);
456 g_assert_not_reached ();
461 static MonoDebugMethodJitInfo *
462 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
464 MonoMethodHeader *header;
465 gint32 offset, native_offset, prev_offset, prev_native_offset;
466 MonoDebugMethodJitInfo *jit;
470 header = mono_method_get_header (method);
473 jit = g_new0 (MonoDebugMethodJitInfo, 1);
474 jit->code_start = code_start;
475 jit->num_locals = header->num_locals;
476 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
477 jit->num_params = mono_method_signature (method)->param_count;
478 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
481 jit->epilogue_begin = decode_value (p, &p);
482 jit->prologue_end = decode_value (p, &p);
483 jit->code_size = decode_value (p, &p);
485 for (i = 0; i < jit->num_params; ++i)
486 deserialize_variable (&jit->params [i], p, &p);
488 if (mono_method_signature (method)->hasthis) {
489 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
490 deserialize_variable (jit->this_var, p, &p);
493 for (i = 0; i < jit->num_locals; i++)
494 deserialize_variable (&jit->locals [i], p, &p);
496 jit->num_line_numbers = decode_value (p, &p);
497 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
500 prev_native_offset = 0;
501 for (i = 0; i < jit->num_line_numbers; ++i) {
502 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
504 offset = prev_offset + decode_value (p, &p);
505 native_offset = prev_native_offset + decode_value (p, &p);
507 lne->native_offset = native_offset;
508 lne->il_offset = offset;
510 prev_offset = offset;
511 prev_native_offset = native_offset;
518 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
519 guint8 *debug_info, guint32 debug_info_len)
521 MonoDebugMethodJitInfo *jit;
523 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
526 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
527 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
528 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
529 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
530 (method->wrapper_type != MONO_WRAPPER_NONE))
533 if (debug_info_len == 0)
536 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
539 jit = mono_debug_read_method ((MonoDebugMethodAddress *) debug_info);
540 jit->code_start = code_start;
541 jit->wrapper_addr = NULL;
544 mono_debug_add_method (method, jit, domain);
546 mono_debug_add_vg_method (method, jit);
548 mono_debug_free_method_jit_info (jit);
552 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
554 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
557 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
561 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
563 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
564 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
565 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
567 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
568 g_print ("%s %s (%d) in memory: base register %s + %d\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)), info->offset);
570 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
572 g_assert_not_reached ();
577 * mono_debug_print_locals:
579 * Prints to stdout the information about the local variables in
580 * a method (if @only_arguments is false) or about the arguments.
581 * The information includes the storage info (where the variable
582 * lives, in a register or in memory).
583 * The method is found by looking up what method has been emitted at
584 * the instruction address @ip.
585 * This is for use inside a debugger.
588 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
590 MonoDomain *domain = mono_domain_get ();
591 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
592 MonoDebugMethodInfo *minfo;
593 MonoDebugMethodJitInfo *jit;
599 minfo = mono_debug_lookup_method (mono_jit_info_get_method (ji));
603 jit = mono_debug_find_method (minfo, domain);
606 if (only_arguments) {
608 names = g_new (char *, jit->num_params);
609 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
611 print_var_info (jit->this_var, 0, "this", "Arg");
612 for (i = 0; i < jit->num_params; ++i) {
613 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
617 for (i = 0; i < jit->num_locals; ++i) {
618 print_var_info (&jit->locals [i], i, "", "Local");