First set of licensing changes
[mono.git] / mono / metadata / mono-debug.c
1 /*
2  * mono-debug.c: 
3  *
4  * Author:
5  *      Mono Project (http://www.mono-project.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #include <config.h>
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/tokentype.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/class-internals.h>
19 #include <mono/metadata/mono-debug.h>
20 #include <mono/metadata/mono-debug-debugger.h>
21 #include <mono/metadata/mono-endian.h>
22 #include <mono/metadata/gc-internals.h>
23 #include <mono/metadata/mempool.h>
24 #include <mono/metadata/debug-mono-ppdb.h>
25 #include <string.h>
26
27 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
28
29 #if NO_UNALIGNED_ACCESS
30 #define WRITE_UNALIGNED(type, addr, val) \
31         memcpy(addr, &val, sizeof(type))
32 #define READ_UNALIGNED(type, addr, val) \
33         memcpy(&val, addr, sizeof(type))
34 #else
35 #define WRITE_UNALIGNED(type, addr, val) \
36         (*(type *)(addr) = (val))
37 #define READ_UNALIGNED(type, addr, val) \
38         val = (*(type *)(addr))
39 #endif
40
41 /* This contains per-domain info */
42 struct _MonoDebugDataTable {
43         MonoMemPool *mp;
44         GHashTable *method_address_hash;
45 };
46
47 /* This contains JIT debugging information about a method in serialized format */
48 struct _MonoDebugMethodAddress {
49         const guint8 *code_start;
50         guint32 code_size;
51         guint8 data [MONO_ZERO_LEN_ARRAY];
52 };
53
54 static MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
55
56 static gboolean mono_debug_initialized = FALSE;
57 /* Maps MonoImage -> MonoMonoDebugHandle */
58 static GHashTable *mono_debug_handles;
59 /* Maps MonoDomain -> MonoDataTable */
60 static GHashTable *data_table_hash;
61
62 static mono_mutex_t debugger_lock_mutex;
63
64 static gboolean is_attached = FALSE;
65
66 static MonoDebugHandle     *mono_debug_open_image      (MonoImage *image, const guint8 *raw_contents, int size);
67
68 static MonoDebugHandle     *mono_debug_get_image      (MonoImage *image);
69 static void                 mono_debug_add_assembly    (MonoAssembly *assembly,
70                                                         gpointer user_data);
71
72 static MonoDebugHandle     *open_symfile_from_bundle   (MonoImage *image);
73
74 static MonoDebugDataTable *
75 create_data_table (MonoDomain *domain)
76 {
77         MonoDebugDataTable *table;
78
79         table = g_new0 (MonoDebugDataTable, 1);
80
81         table->mp = mono_mempool_new ();
82         table->method_address_hash = g_hash_table_new (NULL, NULL);
83
84         if (domain)
85                 g_hash_table_insert (data_table_hash, domain, table);
86
87         return table;
88 }
89
90 static void
91 free_data_table (MonoDebugDataTable *table)
92 {
93         mono_mempool_destroy (table->mp);
94         g_hash_table_destroy (table->method_address_hash);
95
96         g_free (table);
97 }
98
99 static MonoDebugDataTable *
100 lookup_data_table (MonoDomain *domain)
101 {
102         MonoDebugDataTable *table;
103
104         table = (MonoDebugDataTable *)g_hash_table_lookup (data_table_hash, domain);
105         if (!table) {
106                 g_error ("lookup_data_table () failed for %p\n", domain);
107                 g_assert (table);
108         }
109         return table;
110 }
111
112 static void
113 free_debug_handle (MonoDebugHandle *handle)
114 {
115         if (handle->ppdb)
116                 mono_ppdb_close (handle);
117         if (handle->symfile)
118                 mono_debug_close_mono_symbol_file (handle->symfile);
119         /* decrease the refcount added with mono_image_addref () */
120         mono_image_close (handle->image);
121         g_free (handle);
122 }
123
124 /*
125  * Initialize debugging support.
126  *
127  * This method must be called after loading corlib,
128  * but before opening the application's main assembly because we need to set some
129  * callbacks here.
130  */
131 void
132 mono_debug_init (MonoDebugFormat format)
133 {
134         g_assert (!mono_debug_initialized);
135         if (format == MONO_DEBUG_FORMAT_DEBUGGER)
136                 g_error ("The mdb debugger is no longer supported.");
137
138         mono_debug_initialized = TRUE;
139         mono_debug_format = format;
140
141         mono_os_mutex_init_recursive (&debugger_lock_mutex);
142
143         mono_debugger_lock ();
144
145         mono_debug_handles = g_hash_table_new_full
146                 (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
147
148         data_table_hash = g_hash_table_new_full (
149                 NULL, NULL, NULL, (GDestroyNotify) free_data_table);
150
151         mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
152
153         mono_debugger_unlock ();
154 }
155
156 void
157 mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
158 {
159         if (!mono_debug_initialized)
160                 return;
161
162         mono_debug_open_image (image, raw_contents, size);
163 }
164
165 void
166 mono_debug_cleanup (void)
167 {
168         if (mono_debug_handles)
169                 g_hash_table_destroy (mono_debug_handles);
170         mono_debug_handles = NULL;
171
172         if (data_table_hash) {
173                 g_hash_table_destroy (data_table_hash);
174                 data_table_hash = NULL;
175         }
176 }
177
178 void
179 mono_debug_domain_create (MonoDomain *domain)
180 {
181         if (!mono_debug_initialized)
182                 return;
183
184         mono_debugger_lock ();
185
186         create_data_table (domain);
187
188         mono_debugger_unlock ();
189 }
190
191 void
192 mono_debug_domain_unload (MonoDomain *domain)
193 {
194         MonoDebugDataTable *table;
195
196         if (!mono_debug_initialized)
197                 return;
198
199         mono_debugger_lock ();
200
201         table = (MonoDebugDataTable *)g_hash_table_lookup (data_table_hash, domain);
202         if (!table) {
203                 g_warning (G_STRLOC ": unloading unknown domain %p / %d",
204                            domain, mono_domain_get_id (domain));
205                 mono_debugger_unlock ();
206                 return;
207         }
208
209         g_hash_table_remove (data_table_hash, domain);
210
211         mono_debugger_unlock ();
212 }
213
214 /*
215  * LOCKING: Assumes the debug lock is held.
216  */
217 static MonoDebugHandle *
218 mono_debug_get_image (MonoImage *image)
219 {
220         return (MonoDebugHandle *)g_hash_table_lookup (mono_debug_handles, image);
221 }
222
223 void
224 mono_debug_close_image (MonoImage *image)
225 {
226         MonoDebugHandle *handle;
227
228         if (!mono_debug_initialized)
229                 return;
230
231         mono_debugger_lock ();
232
233         handle = mono_debug_get_image (image);
234         if (!handle) {
235                 mono_debugger_unlock ();
236                 return;
237         }
238
239         g_hash_table_remove (mono_debug_handles, image);
240
241         mono_debugger_unlock ();
242 }
243
244 static MonoDebugHandle *
245 mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
246 {
247         MonoDebugHandle *handle;
248
249         if (mono_image_is_dynamic (image))
250                 return NULL;
251
252         mono_debugger_lock ();
253
254         handle = mono_debug_get_image (image);
255         if (handle != NULL) {
256                 mono_debugger_unlock ();
257                 return handle;
258         }
259
260         handle = g_new0 (MonoDebugHandle, 1);
261
262         handle->image = image;
263         mono_image_addref (image);
264
265         /* Try a ppdb file first */
266         handle->ppdb = mono_ppdb_load_file (handle->image, raw_contents, size);
267
268         if (!handle->ppdb)
269                 handle->symfile = mono_debug_open_mono_symbols (handle, raw_contents, size, FALSE);
270
271         g_hash_table_insert (mono_debug_handles, image, handle);
272
273         mono_debugger_unlock ();
274
275         return handle;
276 }
277
278 static void
279 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
280 {
281         MonoDebugHandle *handle;
282         MonoImage *image;
283
284         mono_debugger_lock ();
285         image = mono_assembly_get_image (assembly);
286         handle = open_symfile_from_bundle (image);
287         if (!handle)
288                 mono_debug_open_image (image, NULL, 0);
289         mono_debugger_unlock ();
290 }
291
292 struct LookupMethodData
293 {
294         MonoDebugMethodInfo *minfo;
295         MonoMethod *method;
296 };
297
298 static void
299 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
300 {
301         MonoDebugHandle *handle = (MonoDebugHandle *) value;
302         struct LookupMethodData *data = (struct LookupMethodData *) user_data;
303
304         if (data->minfo)
305                 return;
306
307         if (handle->ppdb)
308                 data->minfo = mono_ppdb_lookup_method (handle, data->method);
309         else if (handle->symfile)
310                 data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
311 }
312
313 static MonoDebugMethodInfo *
314 mono_debug_lookup_method_internal (MonoMethod *method)
315 {
316         struct LookupMethodData data;
317
318         data.minfo = NULL;
319         data.method = method;
320
321         if (!mono_debug_handles)
322                 return NULL;
323
324         g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
325         return data.minfo;
326 }
327
328 /**
329  * mono_debug_lookup_method:
330  *
331  * Lookup symbol file information for the method @method.  The returned
332  * `MonoDebugMethodInfo' is a private structure, but it can be passed to
333  * mono_debug_symfile_lookup_location().
334  */
335 MonoDebugMethodInfo *
336 mono_debug_lookup_method (MonoMethod *method)
337 {
338         MonoDebugMethodInfo *minfo;
339
340         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
341                 return NULL;
342
343         mono_debugger_lock ();
344         minfo = mono_debug_lookup_method_internal (method);
345         mono_debugger_unlock ();
346         return minfo;
347 }
348
349 typedef struct
350 {
351         gboolean found;
352         MonoImage *image;
353 } LookupImageData;
354
355 static void
356 lookup_image_func (gpointer key, gpointer value, gpointer user_data)
357 {
358         MonoDebugHandle *handle = (MonoDebugHandle *) value;
359         LookupImageData *data = (LookupImageData *) user_data;
360
361         if (data->found)
362                 return;
363
364         if (handle->image == data->image && handle->symfile)
365                 data->found = TRUE;
366 }
367
368 gboolean
369 mono_debug_image_has_debug_info (MonoImage *image)
370 {
371         LookupImageData data;
372
373         if (!mono_debug_handles)
374                 return FALSE;
375
376         memset (&data, 0, sizeof (data));
377         data.image = image;
378
379         mono_debugger_lock ();
380         g_hash_table_foreach (mono_debug_handles, lookup_image_func, &data);
381         mono_debugger_unlock ();
382         return data.found;
383 }
384
385 static inline void
386 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
387 {
388         do {
389                 guint8 byte = value & 0x7f;
390                 value >>= 7;
391                 if (value)
392                         byte |= 0x80;
393                 *ptr++ = byte;
394         } while (value);
395
396         *rptr = ptr;
397 }
398
399 static inline void
400 write_sleb128 (gint32 value, guint8 *ptr, guint8 **rptr)
401 {
402         gboolean more = 1;
403
404         while (more) {
405                 guint8 byte = value & 0x7f;
406                 value >>= 7;
407
408                 if (((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && (byte & 0x40)))
409                         more = 0;
410                 else
411                         byte |= 0x80;
412                 *ptr++ = byte;
413         }
414
415         *rptr = ptr;
416 }
417
418 static void
419 write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
420 {
421         write_leb128 (var->index, ptr, &ptr);
422         write_sleb128 (var->offset, ptr, &ptr);
423         write_leb128 (var->size, ptr, &ptr);
424         write_leb128 (var->begin_scope, ptr, &ptr);
425         write_leb128 (var->end_scope, ptr, &ptr);
426         WRITE_UNALIGNED (gpointer, ptr, var->type);
427         ptr += sizeof (gpointer);
428         *rptr = ptr;
429 }
430
431 MonoDebugMethodAddress *
432 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
433 {
434         MonoDebugDataTable *table;
435         MonoDebugMethodAddress *address;
436         guint8 buffer [BUFSIZ];
437         guint8 *ptr, *oldptr;
438         guint32 i, size, total_size, max_size;
439
440         mono_debugger_lock ();
441
442         table = lookup_data_table (domain);
443
444         max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) +
445                 (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
446
447         if (max_size > BUFSIZ)
448                 ptr = oldptr = (guint8 *)g_malloc (max_size);
449         else
450                 ptr = oldptr = buffer;
451
452         write_leb128 (jit->prologue_end, ptr, &ptr);
453         write_leb128 (jit->epilogue_begin, ptr, &ptr);
454
455         write_leb128 (jit->num_line_numbers, ptr, &ptr);
456         for (i = 0; i < jit->num_line_numbers; i++) {
457                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
458
459                 write_sleb128 (lne->il_offset, ptr, &ptr);
460                 write_sleb128 (lne->native_offset, ptr, &ptr);
461         }
462
463         *ptr++ = jit->this_var ? 1 : 0;
464         if (jit->this_var)
465                 write_variable (jit->this_var, ptr, &ptr);
466
467         write_leb128 (jit->num_params, ptr, &ptr);
468         for (i = 0; i < jit->num_params; i++)
469                 write_variable (&jit->params [i], ptr, &ptr);
470
471         write_leb128 (jit->num_locals, ptr, &ptr);
472         for (i = 0; i < jit->num_locals; i++)
473                 write_variable (&jit->locals [i], ptr, &ptr);
474
475         *ptr++ = jit->gsharedvt_info_var ? 1 : 0;
476         if (jit->gsharedvt_info_var) {
477                 write_variable (jit->gsharedvt_info_var, ptr, &ptr);
478                 write_variable (jit->gsharedvt_locals_var, ptr, &ptr);
479         }
480
481         size = ptr - oldptr;
482         g_assert (size < max_size);
483         total_size = size + sizeof (MonoDebugMethodAddress);
484
485         if (method_is_dynamic (method)) {
486                 address = (MonoDebugMethodAddress *)g_malloc0 (total_size);
487         } else {
488                 address = (MonoDebugMethodAddress *)mono_mempool_alloc (table->mp, total_size);
489         }
490
491         address->code_start = jit->code_start;
492         address->code_size = jit->code_size;
493
494         memcpy (&address->data, oldptr, size);
495         if (max_size > BUFSIZ)
496                 g_free (oldptr);
497
498         g_hash_table_insert (table->method_address_hash, method, address);
499
500         mono_debugger_unlock ();
501         return address;
502 }
503
504 void
505 mono_debug_remove_method (MonoMethod *method, MonoDomain *domain)
506 {
507         MonoDebugDataTable *table;
508         MonoDebugMethodAddress *address;
509
510         if (!mono_debug_initialized)
511                 return;
512
513         g_assert (method_is_dynamic (method));
514
515         mono_debugger_lock ();
516
517         table = lookup_data_table (domain);
518
519         address = (MonoDebugMethodAddress *)g_hash_table_lookup (table->method_address_hash, method);
520         if (address)
521                 g_free (address);
522
523         g_hash_table_remove (table->method_address_hash, method);
524
525         mono_debugger_unlock ();
526 }
527
528 void
529 mono_debug_add_delegate_trampoline (gpointer code, int size)
530 {
531 }
532
533 static inline guint32
534 read_leb128 (guint8 *ptr, guint8 **rptr)
535 {
536         guint32 result = 0, shift = 0;
537
538         while (TRUE) {
539                 guint8 byte = *ptr++;
540
541                 result |= (byte & 0x7f) << shift;
542                 if ((byte & 0x80) == 0)
543                         break;
544                 shift += 7;
545         }
546
547         *rptr = ptr;
548         return result;
549 }
550
551 static inline gint32
552 read_sleb128 (guint8 *ptr, guint8 **rptr)
553 {
554         gint32 result = 0;
555         guint32 shift = 0;
556
557         while (TRUE) {
558                 guint8 byte = *ptr++;
559
560                 result |= (byte & 0x7f) << shift;
561                 shift += 7;
562
563                 if (byte & 0x80)
564                         continue;
565
566                 if ((shift < 32) && (byte & 0x40))
567                         result |= - (1 << shift);
568                 break;
569         }
570
571         *rptr = ptr;
572         return result;
573 }
574
575 static void
576 read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
577 {
578         var->index = read_leb128 (ptr, &ptr);
579         var->offset = read_sleb128 (ptr, &ptr);
580         var->size = read_leb128 (ptr, &ptr);
581         var->begin_scope = read_leb128 (ptr, &ptr);
582         var->end_scope = read_leb128 (ptr, &ptr);
583         READ_UNALIGNED (MonoType *, ptr, var->type);
584         ptr += sizeof (gpointer);
585         *rptr = ptr;
586 }
587
588 void
589 mono_debug_free_method_jit_info (MonoDebugMethodJitInfo *jit)
590 {
591         if (!jit)
592                 return;
593         g_free (jit->line_numbers);
594         g_free (jit->this_var);
595         g_free (jit->params);
596         g_free (jit->locals);
597         g_free (jit->gsharedvt_info_var);
598         g_free (jit->gsharedvt_locals_var);
599         g_free (jit);
600 }
601
602 static MonoDebugMethodJitInfo *
603 mono_debug_read_method (MonoDebugMethodAddress *address)
604 {
605         MonoDebugMethodJitInfo *jit;
606         guint32 i;
607         guint8 *ptr;
608
609         jit = g_new0 (MonoDebugMethodJitInfo, 1);
610         jit->code_start = address->code_start;
611         jit->code_size = address->code_size;
612
613         ptr = (guint8 *) &address->data;
614
615         jit->prologue_end = read_leb128 (ptr, &ptr);
616         jit->epilogue_begin = read_leb128 (ptr, &ptr);
617
618         jit->num_line_numbers = read_leb128 (ptr, &ptr);
619         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
620         for (i = 0; i < jit->num_line_numbers; i++) {
621                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
622
623                 lne->il_offset = read_sleb128 (ptr, &ptr);
624                 lne->native_offset = read_sleb128 (ptr, &ptr);
625         }
626
627         if (*ptr++) {
628                 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
629                 read_variable (jit->this_var, ptr, &ptr);
630         }
631
632         jit->num_params = read_leb128 (ptr, &ptr);
633         jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
634         for (i = 0; i < jit->num_params; i++)
635                 read_variable (&jit->params [i], ptr, &ptr);
636
637         jit->num_locals = read_leb128 (ptr, &ptr);
638         jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
639         for (i = 0; i < jit->num_locals; i++)
640                 read_variable (&jit->locals [i], ptr, &ptr);
641
642         if (*ptr++) {
643                 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
644                 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
645                 read_variable (jit->gsharedvt_info_var, ptr, &ptr);
646                 read_variable (jit->gsharedvt_locals_var, ptr, &ptr);
647         }
648
649         return jit;
650 }
651
652 static MonoDebugMethodJitInfo *
653 find_method (MonoMethod *method, MonoDomain *domain)
654 {
655         MonoDebugDataTable *table;
656         MonoDebugMethodAddress *address;
657
658         table = lookup_data_table (domain);
659         address = (MonoDebugMethodAddress *)g_hash_table_lookup (table->method_address_hash, method);
660
661         if (!address)
662                 return NULL;
663
664         return mono_debug_read_method (address);
665 }
666
667 MonoDebugMethodJitInfo *
668 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
669 {
670         MonoDebugMethodJitInfo *res;
671
672         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
673                 return NULL;
674
675         mono_debugger_lock ();
676         res = find_method (method, domain);
677         mono_debugger_unlock ();
678         return res;
679 }
680
681 MonoDebugMethodAddressList *
682 mono_debug_lookup_method_addresses (MonoMethod *method)
683 {
684         g_assert_not_reached ();
685         return NULL;
686 }
687
688 static gint32
689 il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
690 {
691         MonoDebugMethodJitInfo *jit;
692         int i;
693
694         jit = find_method (method, domain);
695         if (!jit || !jit->line_numbers)
696                 goto cleanup_and_fail;
697
698         for (i = jit->num_line_numbers - 1; i >= 0; i--) {
699                 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
700
701                 if (lne.native_offset <= native_offset) {
702                         mono_debug_free_method_jit_info (jit);
703                         return lne.il_offset;
704                 }
705         }
706
707 cleanup_and_fail:
708         mono_debug_free_method_jit_info (jit);
709         return -1;
710 }
711
712 /**
713  * mono_debug_il_offset_from_address:
714  *
715  *   Compute the IL offset corresponding to NATIVE_OFFSET inside the native
716  * code of METHOD in DOMAIN.
717  */
718 gint32
719 mono_debug_il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
720 {
721         gint32 res;
722
723         mono_debugger_lock ();
724
725         res = il_offset_from_address (method, domain, native_offset);
726
727         mono_debugger_unlock ();
728
729         return res;
730 }
731
732 /**
733  * mono_debug_lookup_source_location:
734  * @address: Native offset within the @method's machine code.
735  *
736  * Lookup the source code corresponding to the machine instruction located at
737  * native offset @address within @method.
738  *
739  * The returned `MonoDebugSourceLocation' contains both file / line number
740  * information and the corresponding IL offset.  It must be freed by
741  * mono_debug_free_source_location().
742  */
743 MonoDebugSourceLocation *
744 mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
745 {
746         MonoDebugMethodInfo *minfo;
747         MonoDebugSourceLocation *location;
748         gint32 offset;
749
750         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
751                 return NULL;
752
753         mono_debugger_lock ();
754         minfo = mono_debug_lookup_method_internal (method);
755         if (!minfo || !minfo->handle) {
756                 mono_debugger_unlock ();
757                 return NULL;
758         }
759
760         if (!minfo->handle->ppdb && (!minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile))) {
761                 mono_debugger_unlock ();
762                 return NULL;
763         }
764
765         offset = il_offset_from_address (method, domain, address);
766         if (offset < 0) {
767                 mono_debugger_unlock ();
768                 return NULL;
769         }
770
771         if (minfo->handle->ppdb)
772                 location = mono_ppdb_lookup_location (minfo, offset);
773         else
774                 location = mono_debug_symfile_lookup_location (minfo, offset);
775         mono_debugger_unlock ();
776         return location;
777 }
778
779 MonoDebugSourceLocation *
780 mono_debug_method_lookup_location (MonoDebugMethodInfo *minfo, int il_offset)
781 {
782         MonoDebugSourceLocation *location;
783
784         mono_debugger_lock ();
785         if (minfo->handle->ppdb)
786                 location = mono_ppdb_lookup_location (minfo, il_offset);
787         else
788                 location = mono_debug_symfile_lookup_location (minfo, il_offset);
789         mono_debugger_unlock ();
790         return location;
791 }
792
793 /*
794  * mono_debug_lookup_locals:
795  *
796  *   Return information about the local variables of MINFO.
797  * The result should be freed using mono_debug_free_locals ().
798  */
799 MonoDebugLocalsInfo*
800 mono_debug_lookup_locals (MonoMethod *method)
801 {
802         MonoDebugMethodInfo *minfo;
803         MonoDebugLocalsInfo *res;
804
805         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
806                 return NULL;
807
808         mono_debugger_lock ();
809         minfo = mono_debug_lookup_method_internal (method);
810         if (!minfo || !minfo->handle) {
811                 mono_debugger_unlock ();
812                 return NULL;
813         }
814
815         if (minfo->handle->ppdb) {
816                 res = mono_ppdb_lookup_locals (minfo);
817         } else {
818                 if (!minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile))
819                         res = NULL;
820                 else
821                         res = mono_debug_symfile_lookup_locals (minfo);
822         }
823         mono_debugger_unlock ();
824
825         return res;
826 }
827
828 /*
829  * mono_debug_free_locals:
830  *
831  *   Free all the data allocated by mono_debug_lookup_locals ().
832  */
833 void
834 mono_debug_free_locals (MonoDebugLocalsInfo *info)
835 {
836         int i;
837
838         for (i = 0; i < info->num_locals; ++i)
839                 g_free (info->locals [i].name);
840         g_free (info->locals);
841         g_free (info->code_blocks);
842         g_free (info);
843 }
844
845 /**
846  * mono_debug_free_source_location:
847  * @location: A `MonoDebugSourceLocation'.
848  *
849  * Frees the @location.
850  */
851 void
852 mono_debug_free_source_location (MonoDebugSourceLocation *location)
853 {
854         if (location) {
855                 g_free (location->source_file);
856                 g_free (location);
857         }
858 }
859
860 /**
861  * mono_debug_print_stack_frame:
862  * @native_offset: Native offset within the @method's machine code.
863  *
864  * Conventient wrapper around mono_debug_lookup_source_location() which can be
865  * used if you only want to use the location to print a stack frame.
866  */
867 gchar *
868 mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
869 {
870         MonoDebugSourceLocation *location;
871         gchar *fname, *ptr, *res;
872         int offset;
873
874         fname = mono_method_full_name (method, TRUE);
875         for (ptr = fname; *ptr; ptr++) {
876                 if (*ptr == ':') *ptr = '.';
877         }
878
879         location = mono_debug_lookup_source_location (method, native_offset, domain);
880
881         if (!location) {
882                 if (mono_debug_initialized) {
883                         mono_debugger_lock ();
884                         offset = il_offset_from_address (method, domain, native_offset);
885                         mono_debugger_unlock ();
886                 } else {
887                         offset = -1;
888                 }
889
890                 if (offset < 0)
891                         res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
892                 else
893                         res = g_strdup_printf ("at %s <IL 0x%05x, 0x%05x>", fname, offset, native_offset);
894                 g_free (fname);
895                 return res;
896         }
897
898         res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
899                                location->source_file, location->row);
900
901         g_free (fname);
902         mono_debug_free_source_location (location);
903         return res;
904 }
905
906 void
907 mono_set_is_debugger_attached (gboolean attached)
908 {
909         is_attached = attached;
910 }
911
912 gboolean
913 mono_is_debugger_attached (void)
914 {
915         return is_attached;
916 }
917
918 /*
919  * Bundles
920  */
921
922 typedef struct _BundledSymfile BundledSymfile;
923
924 struct _BundledSymfile {
925         BundledSymfile *next;
926         const char *aname;
927         const mono_byte *raw_contents;
928         int size;
929 };
930
931 static BundledSymfile *bundled_symfiles = NULL;
932
933 void
934 mono_register_symfile_for_assembly (const char *assembly_name, const mono_byte *raw_contents, int size)
935 {
936         BundledSymfile *bsymfile;
937
938         bsymfile = g_new0 (BundledSymfile, 1);
939         bsymfile->aname = assembly_name;
940         bsymfile->raw_contents = raw_contents;
941         bsymfile->size = size;
942         bsymfile->next = bundled_symfiles;
943         bundled_symfiles = bsymfile;
944 }
945
946 static MonoDebugHandle *
947 open_symfile_from_bundle (MonoImage *image)
948 {
949         BundledSymfile *bsymfile;
950
951         for (bsymfile = bundled_symfiles; bsymfile; bsymfile = bsymfile->next) {
952                 if (strcmp (bsymfile->aname, image->module_name))
953                         continue;
954
955                 return mono_debug_open_image (image, bsymfile->raw_contents, bsymfile->size);
956         }
957
958         return NULL;
959 }
960
961 void
962 mono_debugger_lock (void)
963 {
964         g_assert (mono_debug_initialized);
965         mono_os_mutex_lock (&debugger_lock_mutex);
966 }
967
968 void
969 mono_debugger_unlock (void)
970 {
971         g_assert (mono_debug_initialized);
972         mono_os_mutex_unlock (&debugger_lock_mutex);
973 }
974
975 /**
976  * mono_debug_enabled:
977  *
978  * Returns true is debug information is enabled. This doesn't relate if a debugger is present or not.
979  */
980 mono_bool
981 mono_debug_enabled (void)
982 {
983         return mono_debug_format != MONO_DEBUG_FORMAT_NONE;
984 }
985
986 void
987 mono_debug_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
988 {
989         if (minfo->handle->ppdb)
990                 mono_ppdb_get_seq_points (minfo, source_file, source_file_list, source_files, seq_points, n_seq_points);
991         else
992                 mono_debug_symfile_get_seq_points (minfo, source_file, source_file_list, source_files, seq_points, n_seq_points);
993 }