2008-04-12 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / mono-debug.c
1 #include <config.h>
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/class-internals.h>
7 #include <mono/metadata/mono-debug.h>
8 #include <mono/metadata/mono-debug-debugger.h>
9 #include <mono/metadata/mono-endian.h>
10 #include <string.h>
11
12 #define DATA_TABLE_CHUNK_SIZE           16384
13
14 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
15
16 #if NO_UNALIGNED_ACCESS
17 #define RETURN_UNALIGNED(type, addr) \
18         { \
19                 type val; \
20                 memcpy(&val, p + offset, sizeof(val)); \
21                 return val; \
22         }
23 #define WRITE_UNALIGNED(type, addr, val) \
24         memcpy(addr, &val, sizeof(type))
25 #define READ_UNALIGNED(type, addr, val) \
26         memcpy(&val, addr, sizeof(type))
27 #else
28 #define RETURN_UNALIGNED(type, addr) \
29         return *(type*)(p + offset);
30 #define WRITE_UNALIGNED(type, addr, val) \
31         (*(type *)(addr) = (val))
32 #define READ_UNALIGNED(type, addr, val) \
33         val = (*(type *)(addr))
34 #endif
35
36 typedef enum {
37         MONO_DEBUG_DATA_ITEM_UNKNOWN            = 0,
38         MONO_DEBUG_DATA_ITEM_CLASS,
39         MONO_DEBUG_DATA_ITEM_METHOD,
40         MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE
41 } MonoDebugDataItemType;
42
43 typedef struct _MonoDebugDataChunk MonoDebugDataChunk;
44
45 struct _MonoDebugDataChunk {
46         guint32 total_size;
47         guint32 allocated_size;
48         guint32 current_offset;
49         guint32 dummy;
50         MonoDebugDataChunk *next;
51         guint8 data [MONO_ZERO_LEN_ARRAY];
52 };
53
54 struct _MonoDebugDataTable {
55         gint32 domain;
56         gint32 _dummy; /* alignment for next field. */
57         MonoDebugDataChunk *first_chunk;
58         MonoDebugDataChunk *current_chunk;
59         GHashTable *method_hash;
60         GHashTable *method_address_hash;
61 };
62
63 typedef struct {
64         const gchar *method_name;
65         const gchar *cil_code;
66         guint32 wrapper_type;
67 } MonoDebugWrapperData;
68
69 typedef struct {
70         guint32 size;
71         guint32 symfile_id;
72         guint32 domain_id;
73         guint32 method_id;
74         MonoDebugWrapperData *wrapper_data;
75         MonoMethod *method;
76         GSList *address_list;
77 } MonoDebugMethodHeader;
78
79 struct _MonoDebugMethodAddress {
80         MonoDebugMethodHeader header;
81         const guint8 *code_start;
82         const guint8 *wrapper_addr;
83         guint32 code_size;
84         guint8 data [MONO_ZERO_LEN_ARRAY];
85 };
86
87 struct _MonoDebugClassEntry {
88         guint32 size;
89         guint8 data [MONO_ZERO_LEN_ARRAY];
90 };
91
92 typedef struct {
93         gpointer code;
94         guint32 size;
95 } MonoDebugDelegateTrampolineEntry;
96
97 MonoSymbolTable *mono_symbol_table = NULL;
98 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
99 gint32 mono_debug_debugger_version = 3;
100
101 static gboolean in_the_mono_debugger = FALSE;
102 static gboolean mono_debug_initialized = FALSE;
103 GHashTable *mono_debug_handles = NULL;
104
105 static GHashTable *data_table_hash = NULL;
106 static int next_symbol_file_id = 0;
107
108 static MonoDebugHandle     *mono_debug_open_image      (MonoImage *image, const guint8 *raw_contents, int size);
109
110 static MonoDebugHandle     *_mono_debug_get_image      (MonoImage *image);
111 static void                 mono_debug_add_assembly    (MonoAssembly *assembly,
112                                                         gpointer user_data);
113 static void                 mono_debug_add_type        (MonoClass *klass);
114
115 void _mono_debug_init_corlib (MonoDomain *domain);
116
117 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
118 extern void (*mono_debugger_class_loaded_methods_func) (MonoClass *klass);
119
120 static MonoDebugDataTable *
121 create_data_table (MonoDomain *domain)
122 {
123         MonoDebugDataTable *table;
124         MonoDebugDataChunk *chunk;
125
126         table = g_new0 (MonoDebugDataTable, 1);
127         table->domain = domain ? mono_domain_get_id (domain) : -1;
128
129         table->method_address_hash = g_hash_table_new (NULL, NULL);
130         table->method_hash = g_hash_table_new (NULL, NULL);
131
132         chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + DATA_TABLE_CHUNK_SIZE);
133         chunk->total_size = DATA_TABLE_CHUNK_SIZE;
134
135         table->first_chunk = table->current_chunk = chunk;
136
137         if (domain) {
138                 mono_debug_list_add (&mono_symbol_table->data_tables, table);
139                 g_hash_table_insert (data_table_hash, domain, table);
140         }
141
142         return table;
143 }
144
145 static void
146 free_header_data (gpointer key, gpointer value, gpointer user_data)
147 {
148         MonoDebugMethodHeader *header = (MonoDebugMethodHeader*)value;
149
150         if (header->wrapper_data) {
151                 g_free ((gpointer)header->wrapper_data->method_name);
152                 g_free ((gpointer)header->wrapper_data->cil_code);
153                 g_slist_free (header->address_list);
154                 g_free (header->wrapper_data);
155         }
156 }
157
158 static void
159 free_data_table (MonoDebugDataTable *table)
160 {
161         MonoDebugDataChunk *chunk, *next_chunk;
162
163         g_hash_table_foreach (table->method_hash, free_header_data, NULL);
164         g_hash_table_destroy (table->method_hash);
165         g_hash_table_destroy (table->method_address_hash);
166
167         table->method_hash = NULL;
168         table->method_address_hash = NULL;
169
170         chunk = table->first_chunk;
171         while (chunk) {
172                 next_chunk = chunk->next;
173                 g_free (chunk);
174                 chunk = next_chunk;
175         }
176
177         table->first_chunk = table->current_chunk = NULL;
178         mono_debug_list_remove (&mono_symbol_table->data_tables, table);
179         g_free (table);
180 }
181
182 static MonoDebugDataTable *
183 lookup_data_table (MonoDomain *domain)
184 {
185         MonoDebugDataTable *table;
186
187         table = g_hash_table_lookup (data_table_hash, domain);
188         g_assert (table);
189         return table;
190 }
191
192 static void
193 free_debug_handle (MonoDebugHandle *handle)
194 {
195         if (handle->symfile)
196                 mono_debug_close_mono_symbol_file (handle->symfile);
197         /* decrease the refcount added with mono_image_addref () */
198         free_data_table (handle->type_table);
199         mono_image_close (handle->image);
200         g_free (handle->image_file);
201         g_free (handle);
202 }
203
204 /*
205  * Initialize debugging support.
206  *
207  * This method must be called after loading corlib,
208  * but before opening the application's main assembly because we need to set some
209  * callbacks here.
210  */
211 void
212 mono_debug_init (MonoDebugFormat format)
213 {
214         g_assert (!mono_debug_initialized);
215
216         mono_debug_initialized = TRUE;
217         mono_debug_format = format;
218         in_the_mono_debugger = format == MONO_DEBUG_FORMAT_DEBUGGER;
219
220         mono_debugger_initialize (in_the_mono_debugger);
221
222         mono_debugger_lock ();
223
224         mono_symbol_table = g_new0 (MonoSymbolTable, 1);
225         mono_symbol_table->magic = MONO_DEBUGGER_MAGIC;
226         mono_symbol_table->version = MONO_DEBUGGER_VERSION;
227         mono_symbol_table->total_size = sizeof (MonoSymbolTable);
228
229         mono_debug_handles = g_hash_table_new_full
230                 (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
231
232         data_table_hash = g_hash_table_new_full (
233                 NULL, NULL, NULL, (GDestroyNotify) free_data_table);
234
235         mono_debugger_class_init_func = mono_debug_add_type;
236         mono_debugger_class_loaded_methods_func = mono_debugger_class_initialized;
237         mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
238
239         mono_symbol_table->global_data_table = create_data_table (NULL);
240
241         mono_debugger_unlock ();
242 }
243
244 /*
245  * INTERNAL USE ONLY !
246  */
247 void
248 _mono_debug_init_corlib (MonoDomain *domain)
249 {
250         if (!mono_debug_initialized)
251                 return;
252
253         mono_symbol_table->corlib = mono_debug_open_image (mono_defaults.corlib, NULL, 0);
254         mono_debugger_event (MONO_DEBUGGER_EVENT_INITIALIZE_CORLIB,
255                              (guint64) (gsize) mono_symbol_table->corlib, 0);
256 }
257
258 void
259 mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
260 {
261         mono_debug_open_image (image, raw_contents, size);
262 }
263
264
265 gboolean
266 mono_debug_using_mono_debugger (void)
267 {
268         return in_the_mono_debugger;
269 }
270
271 void
272 mono_debug_cleanup (void)
273 {
274         if (mono_debug_handles)
275                 g_hash_table_destroy (mono_debug_handles);
276         mono_debug_handles = NULL;
277
278         if (data_table_hash) {
279                 g_hash_table_destroy (data_table_hash);
280                 data_table_hash = NULL;
281         }
282
283         g_free (mono_symbol_table);
284         mono_symbol_table = NULL;
285 }
286
287 void
288 mono_debug_domain_create (MonoDomain *domain)
289 {
290         MonoDebugDataTable *table;
291
292         if (!mono_debug_initialized)
293                 return;
294
295         mono_debugger_lock ();
296
297         table = create_data_table (domain);
298
299         mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_CREATE, (guint64) (gsize) table,
300                              mono_domain_get_id (domain));
301
302         mono_debugger_unlock ();
303 }
304
305 void
306 mono_debug_domain_unload (MonoDomain *domain)
307 {
308         MonoDebugDataTable *table;
309
310         if (!mono_debug_initialized)
311                 return;
312
313         mono_debugger_lock ();
314
315         table = g_hash_table_lookup (data_table_hash, domain);
316         if (!table) {
317                 g_warning (G_STRLOC ": unloading unknown domain %p / %d",
318                            domain, mono_domain_get_id (domain));
319                 mono_debugger_unlock ();
320                 return;
321         }
322
323         mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_UNLOAD, (guint64) (gsize) table,
324                              mono_domain_get_id (domain));
325
326         g_hash_table_remove (data_table_hash, domain);
327
328         mono_debugger_unlock ();
329 }
330
331 static MonoDebugHandle *
332 _mono_debug_get_image (MonoImage *image)
333 {
334         return g_hash_table_lookup (mono_debug_handles, image);
335 }
336
337 void
338 mono_debug_close_image (MonoImage *image)
339 {
340         MonoDebugHandle *handle;
341
342         if (!mono_debug_initialized)
343                 return;
344
345         handle = _mono_debug_get_image (image);
346         if (!handle)
347                 return;
348
349         mono_debugger_lock ();
350
351         mono_debugger_event (MONO_DEBUGGER_EVENT_UNLOAD_MODULE, (guint64) (gsize) handle,
352                              handle->index);
353
354         mono_debug_list_remove (&mono_symbol_table->symbol_files, handle);
355         g_hash_table_remove (mono_debug_handles, image);
356
357         mono_debugger_unlock ();
358 }
359
360 static MonoDebugHandle *
361 mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
362 {
363         MonoDebugHandle *handle;
364
365         if (mono_image_is_dynamic (image))
366                 return NULL;
367
368         handle = _mono_debug_get_image (image);
369         if (handle != NULL)
370                 return handle;
371
372         mono_debugger_lock ();
373
374         handle = g_new0 (MonoDebugHandle, 1);
375         handle->index = ++next_symbol_file_id;
376
377         handle->image = image;
378         mono_image_addref (image);
379         handle->image_file = g_strdup (mono_image_get_filename (image));
380
381         handle->type_table = create_data_table (NULL);
382
383         handle->symfile = mono_debug_open_mono_symbols (handle, raw_contents, size, in_the_mono_debugger);
384
385         mono_debug_list_add (&mono_symbol_table->symbol_files, handle);
386
387         g_hash_table_insert (mono_debug_handles, image, handle);
388
389         if (mono_symbol_table->corlib)
390                 mono_debugger_event (MONO_DEBUGGER_EVENT_LOAD_MODULE,
391                                      (guint64) (gsize) handle, 0);
392
393         mono_debugger_unlock ();
394
395         return handle;
396 }
397
398 static void
399 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
400 {
401         mono_debugger_lock ();
402         mono_debug_open_image (mono_assembly_get_image (assembly), NULL, 0);
403         mono_debugger_unlock ();
404 }
405
406 static guint8 *
407 allocate_data_item (MonoDebugDataTable *table, MonoDebugDataItemType type, guint32 size)
408 {
409         guint32 chunk_size;
410         guint8 *data;
411
412         size = ALIGN_TO (size, sizeof (gpointer));
413
414         if (size + 16 < DATA_TABLE_CHUNK_SIZE)
415                 chunk_size = DATA_TABLE_CHUNK_SIZE;
416         else
417                 chunk_size = size + 16;
418
419         g_assert (table->current_chunk->current_offset == table->current_chunk->allocated_size);
420
421         if (table->current_chunk->allocated_size + size + 8 >= table->current_chunk->total_size) {
422                 MonoDebugDataChunk *new_chunk;
423
424                 new_chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + chunk_size);
425                 new_chunk->total_size = chunk_size;
426
427                 table->current_chunk->next = new_chunk;
428                 table->current_chunk = new_chunk;
429         }
430
431         data = &table->current_chunk->data [table->current_chunk->allocated_size];
432         table->current_chunk->allocated_size += size + 8;
433
434         * ((guint32 *) data) = size;
435         data += 4;
436         * ((guint32 *) data) = type;
437         data += 4;
438         return data;
439 }
440
441 static void
442 write_data_item (MonoDebugDataTable *table, const guint8 *data)
443 {
444         MonoDebugDataChunk *current_chunk = table->current_chunk;
445         guint32 size = * ((guint32 *) (data - 8));
446
447         g_assert (current_chunk->current_offset + size + 8 == current_chunk->allocated_size);
448         current_chunk->current_offset = current_chunk->allocated_size;
449 }
450
451 struct LookupMethodData
452 {
453         MonoDebugMethodInfo *minfo;
454         MonoMethod *method;
455 };
456
457 static void
458 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
459 {
460         MonoDebugHandle *handle = (MonoDebugHandle *) value;
461         struct LookupMethodData *data = (struct LookupMethodData *) user_data;
462
463         if (data->minfo)
464                 return;
465
466         if (handle->symfile)
467                 data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
468 }
469
470 static MonoDebugMethodInfo *
471 _mono_debug_lookup_method (MonoMethod *method)
472 {
473         struct LookupMethodData data;
474
475         data.minfo = NULL;
476         data.method = method;
477
478         if (!mono_debug_handles)
479                 return NULL;
480
481         g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
482         return data.minfo;
483 }
484
485 /**
486  * mono_debug_lookup_method:
487  *
488  * Lookup symbol file information for the method @method.  The returned
489  * `MonoDebugMethodInfo' is a private structure, but it can be passed to
490  * mono_debug_symfile_lookup_location().
491  */
492 MonoDebugMethodInfo *
493 mono_debug_lookup_method (MonoMethod *method)
494 {
495         MonoDebugMethodInfo *minfo;
496
497         mono_debugger_lock ();
498         minfo = _mono_debug_lookup_method (method);
499         mono_debugger_unlock ();
500         return minfo;
501 }
502
503 static inline void
504 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
505 {
506         do {
507                 guint8 byte = value & 0x7f;
508                 value >>= 7;
509                 if (value)
510                         byte |= 0x80;
511                 *ptr++ = byte;
512         } while (value);
513
514         *rptr = ptr;
515 }
516
517 static inline void
518 write_sleb128 (gint32 value, guint8 *ptr, guint8 **rptr)
519 {
520         gboolean more = 1;
521
522         while (more) {
523                 guint8 byte = value & 0x7f;
524                 value >>= 7;
525
526                 if (((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && (byte & 0x40)))
527                         more = 0;
528                 else
529                         byte |= 0x80;
530                 *ptr++ = byte;
531         }
532
533         *rptr = ptr;
534 }
535
536 static void
537 write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
538 {
539         write_leb128 (var->index, ptr, &ptr);
540         write_sleb128 (var->offset, ptr, &ptr);
541         write_leb128 (var->size, ptr, &ptr);
542         write_leb128 (var->begin_scope, ptr, &ptr);
543         write_leb128 (var->end_scope, ptr, &ptr);
544         WRITE_UNALIGNED (gpointer, ptr, var->type);
545         ptr += sizeof (gpointer);
546         *rptr = ptr;
547 }
548
549 MonoDebugMethodAddress *
550 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
551 {
552         MonoMethod *declaring;
553         MonoDebugDataTable *table;
554         MonoDebugMethodHeader *header;
555         MonoDebugMethodAddress *address;
556         MonoDebugMethodInfo *minfo;
557         MonoDebugHandle *handle;
558         guint8 buffer [BUFSIZ];
559         guint8 *ptr, *oldptr;
560         guint32 i, size, total_size, max_size;
561         gboolean is_wrapper = FALSE;
562
563         mono_debugger_lock ();
564
565         table = lookup_data_table (domain);
566
567         handle = _mono_debug_get_image (method->klass->image);
568         minfo = _mono_debug_lookup_method (method);
569
570         if (!minfo || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
571             (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
572             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
573             (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
574             (method->wrapper_type != MONO_WRAPPER_NONE)) {
575                 is_wrapper = TRUE;
576         }
577
578         max_size = 24 + 8 * jit->num_line_numbers +
579                 (20 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
580
581         if (max_size > BUFSIZ)
582                 ptr = oldptr = g_malloc (max_size);
583         else
584                 ptr = oldptr = buffer;
585
586         write_leb128 (jit->prologue_end, ptr, &ptr);
587         write_leb128 (jit->epilogue_begin, ptr, &ptr);
588
589         write_leb128 (jit->num_line_numbers, ptr, &ptr);
590         for (i = 0; i < jit->num_line_numbers; i++) {
591                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
592
593                 write_sleb128 (lne->il_offset, ptr, &ptr);
594                 write_sleb128 (lne->native_offset, ptr, &ptr);
595         }
596
597         *ptr++ = jit->this_var ? 1 : 0;
598         if (jit->this_var)
599                 write_variable (jit->this_var, ptr, &ptr);
600
601         write_leb128 (jit->num_params, ptr, &ptr);
602         for (i = 0; i < jit->num_params; i++)
603                 write_variable (&jit->params [i], ptr, &ptr);
604
605         write_leb128 (jit->num_locals, ptr, &ptr);
606         for (i = 0; i < jit->num_locals; i++)
607                 write_variable (&jit->locals [i], ptr, &ptr);
608
609         size = ptr - oldptr;
610         g_assert (size < max_size);
611         total_size = size + sizeof (MonoDebugMethodAddress);
612
613         address = (MonoDebugMethodAddress *) allocate_data_item (
614                 table, MONO_DEBUG_DATA_ITEM_METHOD, total_size);
615
616         address->header.size = total_size;
617         address->header.symfile_id = handle ? handle->index : 0;
618         address->header.domain_id = mono_domain_get_id (domain);
619         address->header.method_id = is_wrapper ? 0 : minfo->index;
620         address->header.method = method;
621
622         address->code_start = jit->code_start;
623         address->code_size = jit->code_size;
624
625         memcpy (&address->data, oldptr, size);
626         if (max_size > BUFSIZ)
627                 g_free (oldptr);
628
629         declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
630         header = g_hash_table_lookup (table->method_hash, declaring);
631
632         if (!header) {
633                 header = &address->header;
634                 g_hash_table_insert (table->method_hash, declaring, header);
635
636                 if (is_wrapper) {
637                         const unsigned char* il_code;
638                         MonoMethodHeader *mheader;
639                         MonoDebugWrapperData *wrapper;
640                         guint32 il_codesize;
641
642                         mheader = mono_method_get_header (declaring);
643                         il_code = mono_method_header_get_code (mheader, &il_codesize, NULL);
644
645                         header->wrapper_data = wrapper = g_new0 (MonoDebugWrapperData, 1);
646
647                         wrapper->wrapper_type = method->wrapper_type;
648                         wrapper->method_name = mono_method_full_name (declaring, TRUE);
649                         wrapper->cil_code = mono_disasm_code (
650                                 NULL, declaring, il_code, il_code + il_codesize);
651                 }
652         } else {
653                 address->header.wrapper_data = header->wrapper_data;
654                 header->address_list = g_slist_prepend (header->address_list, address);
655         }
656
657         g_hash_table_insert (table->method_address_hash, method, address);
658
659         write_data_item (table, (guint8 *) address);
660
661         mono_debugger_unlock ();
662         return address;
663 }
664
665 void
666 mono_debug_add_delegate_trampoline (gpointer code, int size)
667 {
668         MonoDebugDelegateTrampolineEntry *entry;
669
670         if (!mono_debug_initialized)
671                 return;
672
673         mono_debugger_lock ();
674
675         entry = (MonoDebugDelegateTrampolineEntry *) allocate_data_item (
676                 mono_symbol_table->global_data_table, MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE,
677                 sizeof (MonoDebugDelegateTrampolineEntry));
678         entry->code = code;
679         entry->size = size;
680
681         write_data_item (mono_symbol_table->global_data_table, (guint8 *) entry);
682
683         mono_debugger_unlock ();
684 }
685
686 static inline guint32
687 read_leb128 (guint8 *ptr, guint8 **rptr)
688 {
689         guint32 result = 0, shift = 0;
690
691         while (TRUE) {
692                 guint8 byte = *ptr++;
693
694                 result |= (byte & 0x7f) << shift;
695                 if ((byte & 0x80) == 0)
696                         break;
697                 shift += 7;
698         }
699
700         *rptr = ptr;
701         return result;
702 }
703
704 static inline gint32
705 read_sleb128 (guint8 *ptr, guint8 **rptr)
706 {
707         gint32 result = 0;
708         guint32 shift = 0;
709
710         while (TRUE) {
711                 guint8 byte = *ptr++;
712
713                 result |= (byte & 0x7f) << shift;
714                 shift += 7;
715
716                 if (byte & 0x80)
717                         continue;
718
719                 if ((shift < 32) && (byte & 0x40))
720                         result |= - (1 << shift);
721                 break;
722         }
723
724         *rptr = ptr;
725         return result;
726 }
727
728 static void
729 read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
730 {
731         var->index = read_leb128 (ptr, &ptr);
732         var->offset = read_sleb128 (ptr, &ptr);
733         var->size = read_leb128 (ptr, &ptr);
734         var->begin_scope = read_leb128 (ptr, &ptr);
735         var->end_scope = read_leb128 (ptr, &ptr);
736         READ_UNALIGNED (gpointer, ptr, var->type);
737         ptr += sizeof (gpointer);
738         *rptr = ptr;
739 }
740
741 static MonoDebugMethodJitInfo *
742 mono_debug_read_method (MonoDebugMethodAddress *address)
743 {
744         MonoDebugMethodJitInfo *jit;
745         guint32 i;
746         guint8 *ptr;
747
748         jit = g_new0 (MonoDebugMethodJitInfo, 1);
749         jit->code_start = address->code_start;
750         jit->code_size = address->code_size;
751         jit->wrapper_addr = address->wrapper_addr;
752
753         ptr = (guint8 *) &address->data;
754
755         jit->prologue_end = read_leb128 (ptr, &ptr);
756         jit->epilogue_begin = read_leb128 (ptr, &ptr);
757
758         jit->num_line_numbers = read_leb128 (ptr, &ptr);
759         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
760         for (i = 0; i < jit->num_line_numbers; i++) {
761                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
762
763                 lne->il_offset = read_sleb128 (ptr, &ptr);
764                 lne->native_offset = read_sleb128 (ptr, &ptr);
765         }
766
767         if (*ptr++) {
768                 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
769                 read_variable (jit->this_var, ptr, &ptr);
770         }
771
772         jit->num_params = read_leb128 (ptr, &ptr);
773         jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
774         for (i = 0; i < jit->num_params; i++)
775                 read_variable (&jit->params [i], ptr, &ptr);
776
777         jit->num_locals = read_leb128 (ptr, &ptr);
778         jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
779         for (i = 0; i < jit->num_locals; i++)
780                 read_variable (&jit->locals [i], ptr, &ptr);
781
782         return jit;
783 }
784
785 static void
786 mono_debug_add_type (MonoClass *klass)
787 {
788         MonoDebugHandle *handle;
789         MonoDebugClassEntry *entry;
790         guint8 buffer [BUFSIZ];
791         guint8 *ptr, *oldptr;
792         guint32 size, total_size, max_size;
793         int base_offset = 0;
794
795         handle = _mono_debug_get_image (klass->image);
796         if (!handle)
797                 return;
798
799         if (klass->generic_class || klass->rank ||
800             (klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
801                 return;
802
803         mono_debugger_lock ();
804
805         max_size = 12 + sizeof (gpointer);
806         if (max_size > BUFSIZ)
807                 ptr = oldptr = g_malloc (max_size);
808         else
809                 ptr = oldptr = buffer;
810
811         if (klass->valuetype)
812                 base_offset = - (int)(sizeof (MonoObject));
813
814         write_leb128 (klass->type_token, ptr, &ptr);
815         write_leb128 (klass->instance_size + base_offset, ptr, &ptr);
816         WRITE_UNALIGNED (gpointer, ptr, klass);
817         ptr += sizeof (gpointer);
818
819         size = ptr - oldptr;
820         g_assert (size < max_size);
821         total_size = size + sizeof (MonoDebugClassEntry);
822
823         g_assert (total_size + 9 < DATA_TABLE_CHUNK_SIZE);
824
825         entry = (MonoDebugClassEntry *) allocate_data_item (
826                 handle->type_table, MONO_DEBUG_DATA_ITEM_CLASS, total_size);
827
828         entry->size = total_size;
829
830         memcpy (&entry->data, oldptr, size);
831
832         write_data_item (handle->type_table, (guint8 *) entry);
833
834         if (max_size > BUFSIZ)
835                 g_free (oldptr);
836
837         mono_debugger_unlock ();
838 }
839
840 static MonoDebugMethodJitInfo *
841 find_method (MonoMethod *method, MonoDomain *domain)
842 {
843         MonoDebugDataTable *table;
844         MonoDebugMethodAddress *address;
845
846         table = lookup_data_table (domain);
847         address = g_hash_table_lookup (table->method_address_hash, method);
848
849         if (!address)
850                 return NULL;
851
852         return mono_debug_read_method (address);
853 }
854
855 MonoDebugMethodJitInfo *
856 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
857 {
858         MonoDebugMethodJitInfo *res;
859         mono_debugger_lock ();
860         res = find_method (method, domain);
861         mono_debugger_unlock ();
862         return res;
863 }
864
865 struct LookupMethodAddressData
866 {
867         MonoMethod *method;
868         MonoDebugMethodHeader *result;
869 };
870
871 static void
872 lookup_method_address_func (gpointer key, gpointer value, gpointer user_data)
873 {
874         MonoDebugDataTable *table = (MonoDebugDataTable *) value;
875         struct LookupMethodAddressData *data = (struct LookupMethodAddressData *) user_data;
876         MonoDebugMethodHeader *header;
877
878         header = g_hash_table_lookup (table->method_hash, data->method);
879         if (header)
880                 data->result = header;
881 }
882
883 MonoDebugMethodAddressList *
884 mono_debug_lookup_method_addresses (MonoMethod *method)
885 {
886         MonoDebugMethodAddressList *info;
887         MonoDebugMethodHeader *header = NULL;
888         struct LookupMethodAddressData data;
889         MonoMethod *declaring;
890         int count, size;
891         GSList *list;
892         guint8 *ptr;
893
894         g_assert (mono_debug_debugger_version == 3);
895
896         mono_debugger_lock ();
897
898         declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
899
900         data.method = declaring;
901         data.result = NULL;
902
903         g_hash_table_foreach (data_table_hash, lookup_method_address_func, &data);
904         header = data.result;
905
906         if (!header) {
907                 mono_debugger_unlock ();
908                 return NULL;
909         }
910
911         count = g_slist_length (header->address_list) + 1;
912         size = sizeof (MonoDebugMethodAddressList) + count * sizeof (gpointer);
913
914         info = g_malloc0 (size);
915         info->size = size;
916         info->count = count;
917
918         ptr = info->data;
919
920         WRITE_UNALIGNED (gpointer, ptr, header);
921         ptr += sizeof (gpointer);
922
923         for (list = header->address_list; list; list = list->next) {
924                 WRITE_UNALIGNED (gpointer, ptr, list->data);
925                 ptr += sizeof (gpointer);
926         }
927
928         mono_debugger_unlock ();
929         return info;
930 }
931
932 static gint32
933 il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
934 {
935         MonoDebugMethodJitInfo *jit;
936         int i;
937
938         jit = find_method (method, domain);
939         if (!jit || !jit->line_numbers)
940                 return -1;
941
942         for (i = jit->num_line_numbers - 1; i >= 0; i--) {
943                 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
944
945                 if (lne.native_offset <= native_offset)
946                         return lne.il_offset;
947         }
948
949         return -1;
950 }
951
952 /**
953  * mono_debug_lookup_source_location:
954  * @address: Native offset within the @method's machine code.
955  *
956  * Lookup the source code corresponding to the machine instruction located at
957  * native offset @address within @method.
958  *
959  * The returned `MonoDebugSourceLocation' contains both file / line number
960  * information and the corresponding IL offset.  It must be freed by
961  * mono_debug_free_source_location().
962  */
963 MonoDebugSourceLocation *
964 mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
965 {
966         MonoDebugMethodInfo *minfo;
967         MonoDebugSourceLocation *location;
968         gint32 offset;
969
970         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
971                 return NULL;
972
973         mono_debugger_lock ();
974         minfo = _mono_debug_lookup_method (method);
975         if (!minfo || !minfo->handle || !minfo->handle->symfile || !minfo->handle->symfile->offset_table) {
976                 mono_debugger_unlock ();
977                 return NULL;
978         }
979
980         offset = il_offset_from_address (method, domain, address);
981         if (offset < 0) {
982                 mono_debugger_unlock ();
983                 return NULL;
984         }
985
986         location = mono_debug_symfile_lookup_location (minfo, offset);
987         mono_debugger_unlock ();
988         return location;
989 }
990
991 /**
992  * mono_debug_free_source_location:
993  * @location: A `MonoDebugSourceLocation'.
994  *
995  * Frees the @location.
996  */
997 void
998 mono_debug_free_source_location (MonoDebugSourceLocation *location)
999 {
1000         if (location) {
1001                 g_free (location->source_file);
1002                 g_free (location);
1003         }
1004 }
1005
1006 /**
1007  * mono_debug_print_stack_frame:
1008  * @native_offset: Native offset within the @method's machine code.
1009  *
1010  * Conventient wrapper around mono_debug_lookup_source_location() which can be
1011  * used if you only want to use the location to print a stack frame.
1012  */
1013 gchar *
1014 mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
1015 {
1016         MonoDebugSourceLocation *location;
1017         gchar *fname, *ptr, *res;
1018
1019         fname = mono_method_full_name (method, TRUE);
1020         for (ptr = fname; *ptr; ptr++) {
1021                 if (*ptr == ':') *ptr = '.';
1022         }
1023
1024         location = mono_debug_lookup_source_location (method, native_offset, domain);
1025
1026         if (!location) {
1027                 res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
1028                 g_free (fname);
1029                 return res;
1030         }
1031
1032         res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
1033                                location->source_file, location->row);
1034
1035         g_free (fname);
1036         mono_debug_free_source_location (location);
1037         return res;
1038 }
1039
1040 void
1041 mono_debug_list_add (MonoDebugList **list, gconstpointer data)
1042 {
1043         MonoDebugList *element, **ptr;
1044
1045         element = g_new0 (MonoDebugList, 1);
1046         element->data = data;
1047
1048         for (ptr = list; *ptr; ptr = &(*ptr)->next)
1049                 ;
1050
1051         *ptr = element;
1052 }
1053
1054 void
1055 mono_debug_list_remove (MonoDebugList **list, gconstpointer data)
1056 {
1057         MonoDebugList **ptr;
1058         MonoDebugList *next;
1059
1060         for (ptr = list; *ptr; ptr = &(*ptr)->next) {
1061                 if ((*ptr)->data != data)
1062                         continue;
1063
1064                 next = (*ptr)->next;
1065                 g_free ((*ptr));
1066                 *ptr = next;
1067                 break;
1068         }
1069 }