2009-03-12 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / dwarfwriter.c
1 /*
2  * dwarfwriter.c: Creation of DWARF debug information
3  *
4  * Author:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2008-2009 Novell, Inc.
8  */
9
10 #include "config.h"
11 #include "dwarfwriter.h"
12
13 #include <sys/types.h>
14 #include <ctype.h>
15 #include <string.h>
16 #ifdef HAVE_STDINT_H
17 #include <stdint.h>
18 #endif
19
20 #include <mono/metadata/mono-endian.h>
21 #include <mono/metadata/debug-mono-symfile.h>
22 #include <mono/utils/mono-compiler.h>
23
24 #ifndef PLATFORM_WIN32
25 #include <mono/utils/freebsd-elf32.h>
26 #include <mono/utils/freebsd-elf64.h>
27 #endif
28
29 #include <mono/utils/freebsd-dwarf.h>
30
31 struct _MonoDwarfWriter
32 {
33         MonoImageWriter *w;
34         GHashTable *class_to_die;
35         int fde_index, tdie_index, line_number_file_index, line_number_dir_index;
36         GHashTable *file_to_index, *dir_to_index;
37         FILE *il_file;
38         int il_file_line_index, loclist_index;
39         GSList *cie_program;
40 };
41
42 /*
43  * mono_dwarf_writer_create:
44  *
45  *   Create a DWARF writer object. WRITER is the underlying image writer this 
46  * writer will emit to. IL_FILE is the file where IL code will be dumped to for
47  * methods which have no line number info. It can be NULL.
48  */
49 MonoDwarfWriter*
50 mono_dwarf_writer_create (MonoImageWriter *writer, FILE *il_file)
51 {
52         MonoDwarfWriter *w = g_new0 (MonoDwarfWriter, 1);
53         
54         w->w = writer;
55         w->il_file = il_file;
56
57         return w;
58 }
59
60 void
61 mono_dwarf_writer_destroy (MonoDwarfWriter *w)
62 {
63         g_free (w);
64 }
65
66 /* Wrappers around the image writer functions */
67
68 static inline void
69 emit_section_change (MonoDwarfWriter *w, const char *section_name, int subsection_index)
70 {
71         img_writer_emit_section_change (w->w, section_name, subsection_index);
72 }
73
74 static inline void
75 emit_push_section (MonoDwarfWriter *w, const char *section_name, int subsection)
76 {
77         img_writer_emit_push_section (w->w, section_name, subsection);
78 }
79
80 static inline void
81 emit_pop_section (MonoDwarfWriter *w)
82 {
83         img_writer_emit_pop_section (w->w);
84 }
85
86 static inline void
87 emit_local_symbol (MonoDwarfWriter *w, const char *name, const char *end_label, gboolean func) 
88
89         img_writer_emit_local_symbol (w->w, name, end_label, func); 
90 }
91
92 static inline void
93 emit_label (MonoDwarfWriter *w, const char *name) 
94
95         img_writer_emit_label (w->w, name); 
96 }
97
98 static inline void
99 emit_bytes (MonoDwarfWriter *w, const guint8* buf, int size) 
100
101         img_writer_emit_bytes (w->w, buf, size); 
102 }
103
104 static inline void
105 emit_string (MonoDwarfWriter *w, const char *value) 
106
107         img_writer_emit_string (w->w, value); 
108 }
109
110 static inline void
111 emit_line (MonoDwarfWriter *w) 
112
113         img_writer_emit_line (w->w); 
114 }
115
116 static inline void
117 emit_alignment (MonoDwarfWriter *w, int size) 
118
119         img_writer_emit_alignment (w->w, size); 
120 }
121
122 static inline void
123 emit_pointer_unaligned (MonoDwarfWriter *w, const char *target) 
124
125         img_writer_emit_pointer_unaligned (w->w, target); 
126 }
127
128 static inline void
129 emit_pointer (MonoDwarfWriter *w, const char *target) 
130
131         img_writer_emit_pointer (w->w, target); 
132 }
133
134 static inline void
135 emit_int16 (MonoDwarfWriter *w, int value) 
136
137         img_writer_emit_int16 (w->w, value); 
138 }
139
140 static inline void
141 emit_int32 (MonoDwarfWriter *w, int value) 
142
143         img_writer_emit_int32 (w->w, value); 
144 }
145
146 static inline void
147 emit_symbol_diff (MonoDwarfWriter *w, const char *end, const char* start, int offset) 
148
149         img_writer_emit_symbol_diff (w->w, end, start, offset); 
150 }
151
152 static inline void
153 emit_zero_bytes (MonoDwarfWriter *w, int num) 
154
155         img_writer_emit_zero_bytes (w->w, num); 
156 }
157
158 static inline void
159 emit_byte (MonoDwarfWriter *w, guint8 val) 
160
161         img_writer_emit_byte (w->w, val); 
162 }
163
164 static G_GNUC_UNUSED void
165 emit_uleb128 (MonoDwarfWriter *w, guint32 value)
166 {
167         do {
168                 guint8 b = value & 0x7f;
169                 value >>= 7;
170                 if (value != 0) /* more bytes to come */
171                         b |= 0x80;
172                 emit_byte (w, b);
173         } while (value);
174 }
175
176 static G_GNUC_UNUSED void
177 emit_sleb128 (MonoDwarfWriter *w, gint64 value)
178 {
179         gboolean more = 1;
180         gboolean negative = (value < 0);
181         guint32 size = 64;
182         guint8 byte;
183
184         while (more) {
185                 byte = value & 0x7f;
186                 value >>= 7;
187                 /* the following is unnecessary if the
188                  * implementation of >>= uses an arithmetic rather
189                  * than logical shift for a signed left operand
190                  */
191                 if (negative)
192                         /* sign extend */
193                         value |= - ((gint64)1 <<(size - 7));
194                 /* sign bit of byte is second high order bit (0x40) */
195                 if ((value == 0 && !(byte & 0x40)) ||
196                         (value == -1 && (byte & 0x40)))
197                         more = 0;
198                 else
199                         byte |= 0x80;
200                 emit_byte (w, byte);
201         }
202 }
203
204 static G_GNUC_UNUSED void
205 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
206 {
207         guint8 *p = buf;
208
209         do {
210                 guint8 b = value & 0x7f;
211                 value >>= 7;
212                 if (value != 0) /* more bytes to come */
213                         b |= 0x80;
214                 *p ++ = b;
215         } while (value);
216
217         *endbuf = p;
218 }
219
220 static G_GNUC_UNUSED void
221 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
222 {
223         gboolean more = 1;
224         gboolean negative = (value < 0);
225         guint32 size = 32;
226         guint8 byte;
227         guint8 *p = buf;
228
229         while (more) {
230                 byte = value & 0x7f;
231                 value >>= 7;
232                 /* the following is unnecessary if the
233                  * implementation of >>= uses an arithmetic rather
234                  * than logical shift for a signed left operand
235                  */
236                 if (negative)
237                         /* sign extend */
238                         value |= - (1 <<(size - 7));
239                 /* sign bit of byte is second high order bit (0x40) */
240                 if ((value == 0 && !(byte & 0x40)) ||
241                         (value == -1 && (byte & 0x40)))
242                         more = 0;
243                 else
244                         byte |= 0x80;
245                 *p ++= byte;
246         }
247
248         *endbuf = p;
249 }
250
251 static void
252 emit_dwarf_abbrev (MonoDwarfWriter *w, int code, int tag, gboolean has_child,
253                                    int *attrs, int attrs_len)
254 {
255         int i;
256
257         emit_uleb128 (w, code);
258         emit_uleb128 (w, tag);
259         emit_byte (w, has_child);
260
261         for (i = 0; i < attrs_len; i++)
262                 emit_uleb128 (w, attrs [i]);
263         emit_uleb128 (w, 0);
264         emit_uleb128 (w, 0);
265 }
266
267 static void
268 emit_cie (MonoDwarfWriter *w)
269 {
270         emit_section_change (w, ".debug_frame", 0);
271
272         emit_alignment (w, 8);
273
274         /* Emit a CIE */
275         emit_symbol_diff (w, ".Lcie0_end", ".", -4); /* length */
276         emit_int32 (w, 0xffffffff); /* CIE id */
277         emit_byte (w, 3); /* version */
278         emit_string (w, ""); /* augmention */
279         emit_sleb128 (w, 1); /* code alignment factor */
280         emit_sleb128 (w, mono_unwind_get_dwarf_data_align ()); /* data alignment factor */
281         emit_uleb128 (w, mono_unwind_get_dwarf_pc_reg ());
282
283         w->cie_program = w->cie_program;
284         if (w->cie_program) {
285                 guint32 uw_info_len;
286                 guint8 *uw_info = mono_unwind_ops_encode (w->cie_program, &uw_info_len);
287                 emit_bytes (w, uw_info, uw_info_len);
288                 g_free (uw_info);
289         }
290
291         emit_alignment (w, sizeof (gpointer));
292         emit_label (w, ".Lcie0_end");
293 }
294
295 static void
296 emit_pointer_value (MonoDwarfWriter *w, gpointer ptr)
297 {
298         gssize val = (gssize)ptr;
299         emit_bytes (w, (guint8*)&val, sizeof (gpointer));
300 }
301
302 static void
303 emit_fde (MonoDwarfWriter *w, int fde_index, char *start_symbol, char *end_symbol,
304                   guint8 *code, guint32 code_size, GSList *unwind_ops, gboolean use_cie)
305 {
306         char symbol [128];
307         GSList *l;
308         guint8 *uw_info;
309         guint32 uw_info_len;
310
311         emit_section_change (w, ".debug_frame", 0);
312
313         sprintf (symbol, ".Lfde%d_end", fde_index);
314         emit_symbol_diff (w, symbol, ".", -4); /* length */
315         emit_int32 (w, 0); /* CIE_pointer */
316         if (start_symbol) {
317                 emit_pointer (w, start_symbol); /* initial_location */
318                 if (end_symbol)
319                         emit_symbol_diff (w, end_symbol, start_symbol, 0); /* address_range */
320                 else {
321                         g_assert (code_size);
322                         emit_int32 (w, code_size);
323                 }
324         } else {
325                 emit_pointer_value (w, code);
326                 emit_int32 (w, code_size);
327         }
328 #if SIZEOF_VOID_P == 8
329         /* Upper 32 bits of code size */
330         emit_int32 (w, 0);
331 #endif
332
333         l = unwind_ops;
334         if (w->cie_program) {
335                 // FIXME: Check that the ops really begin with the CIE program */
336                 int i;
337
338                 for (i = 0; i < g_slist_length (w->cie_program); ++i)
339                         l = l->next;
340         }
341
342         /* Convert the list of MonoUnwindOps to the format used by DWARF */     
343         uw_info = mono_unwind_ops_encode (l, &uw_info_len);
344         emit_bytes (w, uw_info, uw_info_len);
345         g_free (uw_info);
346
347         emit_alignment (w, sizeof (gpointer));
348         sprintf (symbol, ".Lfde%d_end", fde_index);
349         emit_label (w, symbol);
350 }
351
352 /* Abbrevations */
353 #define ABBREV_COMPILE_UNIT 1
354 #define ABBREV_SUBPROGRAM 2
355 #define ABBREV_PARAM 3
356 #define ABBREV_BASE_TYPE 4
357 #define ABBREV_STRUCT_TYPE 5
358 #define ABBREV_DATA_MEMBER 6
359 #define ABBREV_TYPEDEF 7
360 #define ABBREV_ENUM_TYPE 8
361 #define ABBREV_ENUMERATOR 9
362 #define ABBREV_NAMESPACE 10
363 #define ABBREV_VARIABLE 11
364 #define ABBREV_VARIABLE_LOCLIST 12
365
366 static int compile_unit_attr [] = {
367         DW_AT_producer     ,DW_FORM_string,
368     DW_AT_name         ,DW_FORM_string,
369     DW_AT_comp_dir     ,DW_FORM_string,
370         DW_AT_language     ,DW_FORM_data1,
371     DW_AT_low_pc       ,DW_FORM_addr,
372     DW_AT_high_pc      ,DW_FORM_addr,
373         DW_AT_stmt_list    ,DW_FORM_data4
374 };
375
376 static int subprogram_attr [] = {
377         DW_AT_name         , DW_FORM_string,
378     DW_AT_low_pc       , DW_FORM_addr,
379     DW_AT_high_pc      , DW_FORM_addr,
380         DW_AT_frame_base   , DW_FORM_block1
381 };
382
383 static int param_attr [] = {
384         DW_AT_name,     DW_FORM_string,
385         DW_AT_type,     DW_FORM_ref4,
386         DW_AT_location, DW_FORM_block1
387 };
388
389 static int base_type_attr [] = {
390         DW_AT_byte_size,   DW_FORM_data1,
391         DW_AT_encoding,    DW_FORM_data1,
392         DW_AT_name,        DW_FORM_string
393 };
394
395 static int struct_type_attr [] = {
396         DW_AT_name,        DW_FORM_string,
397         DW_AT_byte_size,   DW_FORM_udata,
398 };
399
400 static int data_member_attr [] = {
401         DW_AT_name,        DW_FORM_string,
402         DW_AT_type,        DW_FORM_ref4,
403         DW_AT_data_member_location, DW_FORM_block1
404 };
405
406 static int typedef_attr [] = {
407         DW_AT_name,        DW_FORM_string,
408         DW_AT_type,        DW_FORM_ref4
409 };
410
411 static int enum_type_attr [] = {
412         DW_AT_name,        DW_FORM_string,
413         DW_AT_byte_size,   DW_FORM_udata,
414         DW_AT_type,        DW_FORM_ref4,
415 };
416
417 static int enumerator_attr [] = {
418         DW_AT_name,        DW_FORM_string,
419         DW_AT_const_value, DW_FORM_sdata,
420 };
421
422 static int namespace_attr [] = {
423         DW_AT_name,        DW_FORM_string,
424 };
425
426 static int variable_attr [] = {
427         DW_AT_name,     DW_FORM_string,
428         DW_AT_type,     DW_FORM_ref4,
429         DW_AT_location, DW_FORM_block1
430 };
431
432 static int variable_loclist_attr [] = {
433         DW_AT_name,     DW_FORM_string,
434         DW_AT_type,     DW_FORM_ref4,
435         DW_AT_location, DW_FORM_data4
436 };
437
438 typedef struct DwarfBasicType {
439         const char *die_name, *name;
440         int type;
441         int size;
442         int encoding;
443 } DwarfBasicType;
444
445 static DwarfBasicType basic_types [] = {
446         { ".LDIE_I1", "sbyte", MONO_TYPE_I1, 1, DW_ATE_signed },
447         { ".LDIE_U1", "byte", MONO_TYPE_U1, 1, DW_ATE_unsigned },
448         { ".LDIE_I2", "short", MONO_TYPE_I2, 2, DW_ATE_signed },
449         { ".LDIE_U2", "ushort", MONO_TYPE_U2, 2, DW_ATE_unsigned },
450         { ".LDIE_I4", "int", MONO_TYPE_I4, 4, DW_ATE_signed },
451         { ".LDIE_U4", "uint", MONO_TYPE_U4, 4, DW_ATE_unsigned },
452         { ".LDIE_I8", "long", MONO_TYPE_I8, 8, DW_ATE_signed },
453         { ".LDIE_U8", "ulong", MONO_TYPE_U8, 8, DW_ATE_unsigned },
454         { ".LDIE_I", "intptr", MONO_TYPE_I, SIZEOF_VOID_P, DW_ATE_signed },
455         { ".LDIE_U", "uintptr", MONO_TYPE_U, SIZEOF_VOID_P, DW_ATE_unsigned },
456         { ".LDIE_R4", "float", MONO_TYPE_R4, 4, DW_ATE_float },
457         { ".LDIE_R8", "double", MONO_TYPE_R8, 8, DW_ATE_float },
458         { ".LDIE_BOOLEAN", "boolean", MONO_TYPE_BOOLEAN, 1, DW_ATE_boolean },
459         { ".LDIE_CHAR", "char", MONO_TYPE_CHAR, 2, DW_ATE_unsigned_char },
460         { ".LDIE_STRING", "string", MONO_TYPE_STRING, sizeof (gpointer), DW_ATE_address },
461         { ".LDIE_OBJECT", "object", MONO_TYPE_OBJECT, sizeof (gpointer), DW_ATE_address },
462         { ".LDIE_SZARRAY", "object", MONO_TYPE_SZARRAY, sizeof (gpointer), DW_ATE_address },
463 };
464
465 /* Constants for encoding line number special opcodes */
466 #define OPCODE_BASE 13
467 #define LINE_BASE -5
468 #define LINE_RANGE 14
469
470 /* Subsections of the .debug_line section */
471 #define LINE_SUBSECTION_HEADER 1
472 #define LINE_SUBSECTION_INCLUDES 2
473 #define LINE_SUBSECTION_FILES 3
474 #define LINE_SUBSECTION_DATA 4
475 #define LINE_SUBSECTION_END 5
476
477 static int
478 emit_line_number_file_name (MonoDwarfWriter *w, const char *name,
479                                                         gint64 last_mod_time, gint64 file_size)
480 {
481         int index;
482         int dir_index;
483         char *basename = NULL;
484
485         if (!w->file_to_index)
486                 w->file_to_index = g_hash_table_new (g_str_hash, g_str_equal);
487
488         index = GPOINTER_TO_UINT (g_hash_table_lookup (w->file_to_index, name));
489         if (index > 0)
490                 return index;
491
492         if (g_path_is_absolute (name)) {
493                 char *dir = g_path_get_dirname (name);
494
495                 if (!w->dir_to_index)
496                         w->dir_to_index = g_hash_table_new (g_str_hash, g_str_equal);
497
498                 dir_index = GPOINTER_TO_UINT (g_hash_table_lookup (w->dir_to_index, dir));
499                 if (dir_index == 0) {
500                         emit_section_change (w, ".debug_line", LINE_SUBSECTION_INCLUDES);
501                         emit_string (w, dir);
502
503                         dir_index = ++ w->line_number_dir_index;
504                         g_hash_table_insert (w->dir_to_index, g_strdup (dir), GUINT_TO_POINTER (dir_index));
505                 }
506
507                 g_free (dir);
508
509                 basename = g_path_get_basename (name);
510         } else {
511                 dir_index = 0;
512         }
513
514         emit_section_change (w, ".debug_line", LINE_SUBSECTION_FILES);
515
516         if (basename)
517                 emit_string (w, basename);
518         else
519                 emit_string (w, name);
520         emit_uleb128 (w, dir_index);
521         emit_byte (w, 0);
522         emit_byte (w, 0);
523
524         emit_section_change (w, ".debug_line", LINE_SUBSECTION_DATA);
525
526         if (basename)
527                 g_free (basename);
528
529         index = ++ w->line_number_file_index;
530         g_hash_table_insert (w->file_to_index, g_strdup (name), GUINT_TO_POINTER (index));
531
532         return index;
533 }
534
535 static void
536 emit_line_number_info_begin (MonoDwarfWriter *w)
537 {
538         if (!w->il_file) {
539                 /* FIXME: This doesn't seem to work with !xdebug */
540                 emit_section_change (w, ".debug_line", 0);
541                 emit_label (w, ".Ldebug_line_start");
542                 emit_label (w, ".Ldebug_line_section_start");
543                 return;
544         }
545
546         /* Line number info header */
547         /* 
548          * GAS seems to emit its own data to the end of the first subsection, so we use
549          * subsections 1, 2 etc:
550          * 1 - contains the header
551          * 2 - contains the file names
552          * 3 - contains the end of the header + the data
553          * 4 - the end symbol
554          */
555         emit_section_change (w, ".debug_line", 0);
556         emit_label (w, ".Ldebug_line_section_start");
557         emit_section_change (w, ".debug_line", LINE_SUBSECTION_HEADER);
558         emit_label (w, ".Ldebug_line_start");
559         emit_symbol_diff (w, ".Ldebug_line_end", ".", -4); /* length */
560         emit_int16 (w, 0x2); /* version */
561         emit_symbol_diff (w, ".Ldebug_line_header_end", ".", -4); /* header_length */
562         emit_byte (w, 1); /* minimum_instruction_length */
563         emit_byte (w, 1); /* default_is_stmt */
564         emit_byte (w, LINE_BASE); /* line_base */
565         emit_byte (w, LINE_RANGE); /* line_range */
566         emit_byte (w, OPCODE_BASE); /* opcode_base */
567         emit_byte (w, 0); /* standard_opcode_lengths */
568         emit_byte (w, 1);
569         emit_byte (w, 1);
570         emit_byte (w, 1);
571         emit_byte (w, 1);
572         emit_byte (w, 0);
573         emit_byte (w, 0);
574         emit_byte (w, 0);
575         emit_byte (w, 1);
576         emit_byte (w, 0);
577         emit_byte (w, 0);
578         emit_byte (w, 1);
579
580         /* Includes */
581         emit_section_change (w, ".debug_line", LINE_SUBSECTION_INCLUDES);
582
583         /* End of Includes */
584         emit_section_change (w, ".debug_line", LINE_SUBSECTION_FILES);
585         emit_byte (w, 0);
586
587         /* Files */
588         emit_line_number_file_name (w, "xdb.il", 0, 0);
589
590         /* End of Files */
591         emit_section_change (w, ".debug_line", LINE_SUBSECTION_DATA);
592         emit_byte (w, 0);
593
594         emit_label (w, ".Ldebug_line_header_end");
595
596         /* Emit this into a separate subsection so it gets placed at the end */ 
597         emit_section_change (w, ".debug_line", LINE_SUBSECTION_END);
598
599         emit_byte (w, 0);
600         emit_byte (w, 1);
601         emit_byte (w, DW_LNE_end_sequence);
602
603         emit_label (w, ".Ldebug_line_end");
604 }
605
606 void
607 mono_dwarf_writer_emit_base_info (MonoDwarfWriter *w, GSList *base_unwind_program)
608 {
609         char *s, *build_info;
610         int i;
611
612         w->cie_program = base_unwind_program;
613
614         emit_section_change (w, ".debug_abbrev", 0);
615         emit_dwarf_abbrev (w, ABBREV_COMPILE_UNIT, DW_TAG_compile_unit, TRUE, 
616                                            compile_unit_attr, G_N_ELEMENTS (compile_unit_attr));
617         emit_dwarf_abbrev (w, ABBREV_SUBPROGRAM, DW_TAG_subprogram, TRUE, 
618                                            subprogram_attr, G_N_ELEMENTS (subprogram_attr));
619         emit_dwarf_abbrev (w, ABBREV_PARAM, DW_TAG_formal_parameter, FALSE, 
620                                            param_attr, G_N_ELEMENTS (param_attr));
621         emit_dwarf_abbrev (w, ABBREV_BASE_TYPE, DW_TAG_base_type, FALSE, 
622                                            base_type_attr, G_N_ELEMENTS (base_type_attr));
623         emit_dwarf_abbrev (w, ABBREV_STRUCT_TYPE, DW_TAG_class_type, TRUE, 
624                                            struct_type_attr, G_N_ELEMENTS (struct_type_attr));
625         emit_dwarf_abbrev (w, ABBREV_DATA_MEMBER, DW_TAG_member, FALSE, 
626                                            data_member_attr, G_N_ELEMENTS (data_member_attr));
627         emit_dwarf_abbrev (w, ABBREV_TYPEDEF, DW_TAG_typedef, FALSE, 
628                                            typedef_attr, G_N_ELEMENTS (typedef_attr));
629         emit_dwarf_abbrev (w, ABBREV_ENUM_TYPE, DW_TAG_enumeration_type, TRUE,
630                                            enum_type_attr, G_N_ELEMENTS (enum_type_attr));
631         emit_dwarf_abbrev (w, ABBREV_ENUMERATOR, DW_TAG_enumerator, FALSE,
632                                            enumerator_attr, G_N_ELEMENTS (enumerator_attr));
633         emit_dwarf_abbrev (w, ABBREV_NAMESPACE, DW_TAG_namespace, TRUE,
634                                            namespace_attr, G_N_ELEMENTS (namespace_attr));
635         emit_dwarf_abbrev (w, ABBREV_VARIABLE, DW_TAG_variable, FALSE,
636                                            variable_attr, G_N_ELEMENTS (variable_attr));
637         emit_dwarf_abbrev (w, ABBREV_VARIABLE_LOCLIST, DW_TAG_variable, FALSE,
638                                            variable_loclist_attr, G_N_ELEMENTS (variable_loclist_attr));
639         emit_byte (w, 0);
640
641         emit_section_change (w, ".debug_info", 0);
642         emit_label (w, ".Ldebug_info_start");
643         emit_symbol_diff (w, ".Ldebug_info_end", ".", -4); /* length */
644         emit_int16 (w, 0x2); /* DWARF version 2 */
645         emit_int32 (w, 0); /* .debug_abbrev offset */
646         emit_byte (w, sizeof (gpointer)); /* address size */
647
648         /* Emit this into a separate section so it gets placed at the end */
649         emit_section_change (w, ".debug_info", 1);
650         emit_int32 (w, 0); /* close everything */
651         emit_label (w, ".Ldebug_info_end");
652         emit_section_change (w, ".debug_info", 0);
653
654         /* Compilation unit */
655         emit_uleb128 (w, ABBREV_COMPILE_UNIT);
656         build_info = mono_get_runtime_build_info ();
657         s = g_strdup_printf ("Mono AOT Compiler %s", build_info);
658         emit_string (w, s);
659         g_free (build_info);
660         g_free (s);
661         emit_string (w, "JITted code");
662         emit_string (w, "");
663         emit_byte (w, DW_LANG_C);
664         emit_pointer_value (w, 0);
665         emit_pointer_value (w, 0);
666         /* offset into .debug_line section */
667         emit_symbol_diff (w, ".Ldebug_line_start", ".Ldebug_line_section_start", 0);
668
669         /* Base types */
670         for (i = 0; i < G_N_ELEMENTS (basic_types); ++i) {
671                 emit_label (w, basic_types [i].die_name);
672                 emit_uleb128 (w, ABBREV_BASE_TYPE);
673                 emit_byte (w, basic_types [i].size);
674                 emit_byte (w, basic_types [i].encoding);
675                 emit_string (w, basic_types [i].name);
676         }
677
678         /* debug_loc section */
679         emit_section_change (w, ".debug_loc", 0);
680         emit_label (w, ".Ldebug_loc_start");
681
682         /* debug_line section */
683         emit_line_number_info_begin (w);
684
685         emit_cie (w);
686 }
687
688 /* Returns the local symbol pointing to the emitted debug info */
689 static char*
690 emit_class_dwarf_info (MonoDwarfWriter *w, MonoClass *klass)
691 {
692         char *die;
693         char *full_name;
694         gpointer iter;
695         MonoClassField *field;
696         const char *fdie;
697         int k;
698         gboolean emit_namespace = FALSE;
699
700         // FIXME: Appdomains
701         if (!w->class_to_die)
702                 w->class_to_die = g_hash_table_new (NULL, NULL);
703
704         die = g_hash_table_lookup (w->class_to_die, klass);
705         if (die)
706                 return die;
707
708         if (!((klass->byval_arg.type == MONO_TYPE_CLASS) || klass->enumtype))
709                 return NULL;
710
711         /*
712          * FIXME: gdb can't handle namespaces in languages it doesn't know about.
713          */
714         /*
715         if (klass->name_space && klass->name_space [0] != '\0')
716                 emit_namespace = TRUE;
717         */
718         if (emit_namespace) {
719                 emit_uleb128 (w, ABBREV_NAMESPACE);
720                 emit_string (w, klass->name_space);
721         }
722
723         full_name = g_strdup_printf ("%s%s%s", klass->name_space, klass->name_space ? "." : "", klass->name);
724
725         die = g_strdup_printf (".LTDIE_%d", w->tdie_index);
726         emit_label (w, die);
727
728         if (klass->enumtype) {
729                 int size = mono_class_value_size (mono_class_from_mono_type (mono_class_enum_basetype (klass)), NULL);
730
731                 emit_uleb128 (w, ABBREV_ENUM_TYPE);
732                 emit_string (w, full_name);
733                 emit_uleb128 (w, size);
734                 for (k = 0; k < G_N_ELEMENTS (basic_types); ++k)
735                         if (basic_types [k].type == mono_class_enum_basetype (klass)->type)
736                                 break;
737                 g_assert (k < G_N_ELEMENTS (basic_types));
738                 emit_symbol_diff (w, basic_types [k].die_name, ".Ldebug_info_start", 0);
739
740                 /* Emit enum values */
741                 iter = NULL;
742                 while ((field = mono_class_get_fields (klass, &iter))) {
743                         const char *p;
744                         int len;
745                         MonoTypeEnum def_type;
746
747                         if (strcmp ("value__", mono_field_get_name (field)) == 0)
748                                 continue;
749                         if (mono_field_is_deleted (field))
750                                 continue;
751
752                         emit_uleb128 (w, ABBREV_ENUMERATOR);
753                         emit_string (w, mono_field_get_name (field));
754
755                         p = mono_class_get_field_default_value (field, &def_type);
756                         len = mono_metadata_decode_blob_size (p, &p);
757                         switch (mono_class_enum_basetype (klass)->type) {
758                         case MONO_TYPE_U1:
759                         case MONO_TYPE_I1:
760                         case MONO_TYPE_BOOLEAN:
761                                 emit_sleb128 (w, *p);
762                                 break;
763                         case MONO_TYPE_U2:
764                         case MONO_TYPE_I2:
765                         case MONO_TYPE_CHAR:
766                                 emit_sleb128 (w, read16 (p));
767                                 break;
768                         case MONO_TYPE_U4:
769                         case MONO_TYPE_I4:
770                                 emit_sleb128 (w, read32 (p));
771                                 break;
772                         case MONO_TYPE_U8:
773                         case MONO_TYPE_I8:
774                                 emit_sleb128 (w, read64 (p));
775                                 break;
776                         case MONO_TYPE_I:
777                         case MONO_TYPE_U:
778 #if SIZEOF_VOID_P == 8
779                                 emit_sleb128 (w, read64 (p));
780 #else
781                                 emit_sleb128 (w, read32 (p));
782 #endif
783                                 break;
784                         default:
785                                 g_assert_not_reached ();
786                         }
787                 }
788         } else {
789                 emit_uleb128 (w, ABBREV_STRUCT_TYPE);
790                 emit_string (w, full_name);
791                 emit_uleb128 (w, klass->instance_size);
792
793                 /* Emit fields */
794                 iter = NULL;
795                 while ((field = mono_class_get_fields (klass, &iter))) {
796                         guint8 buf [128];
797                         guint8 *p;
798
799                         if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
800                                 continue;
801
802                         for (k = 0; k < G_N_ELEMENTS (basic_types); ++k)
803                                 if (basic_types [k].type == field->type->type)
804                                         break;
805                         if (k < G_N_ELEMENTS (basic_types) && field->type->type != MONO_TYPE_SZARRAY && field->type->type != MONO_TYPE_CLASS) {
806                                 fdie = basic_types [k].die_name;
807
808                                 emit_uleb128 (w, ABBREV_DATA_MEMBER);
809                                 emit_string (w, field->name);
810                                 emit_symbol_diff (w, fdie, ".Ldebug_info_start", 0);
811                                 /* location */
812                                 p = buf;
813                                 *p ++= DW_OP_plus_uconst;
814                                 encode_uleb128 (field->offset, p, &p);
815
816                                 emit_byte (w, p - buf);
817                                 emit_bytes (w, buf, p - buf);
818                         }
819                 }
820         }
821
822         /* Type end */
823         emit_uleb128 (w, 0x0);
824
825         /* Add a typedef, so we can reference the type without a 'struct' in gdb */
826         emit_uleb128 (w, ABBREV_TYPEDEF);
827         emit_string (w, full_name);
828         emit_symbol_diff (w, die, ".Ldebug_info_start", 0);
829
830         g_free (full_name);
831         w->tdie_index ++;
832
833
834         if (emit_namespace) {
835                 /* Namespace end */
836                 emit_uleb128 (w, 0x0);
837         }
838
839         g_hash_table_insert (w->class_to_die, klass, die);
840         return die;
841 }
842
843 static void
844 emit_var_type (MonoDwarfWriter *w, MonoType *t)
845 {
846         MonoClass *klass = mono_class_from_mono_type (t);
847         int j;
848         const char *tdie;
849
850         for (j = 0; j < G_N_ELEMENTS (basic_types); ++j)
851                 if (basic_types [j].type == t->type)
852                         break;
853         if (j < G_N_ELEMENTS (basic_types))
854                 tdie = basic_types [j].die_name;
855         else {
856                 switch (t->type) {
857                 case MONO_TYPE_CLASS:
858                 case MONO_TYPE_ARRAY:
859                         tdie = ".LDIE_OBJECT";
860                         break;
861                 case MONO_TYPE_VALUETYPE:
862                         if (klass->enumtype)
863                                 tdie = emit_class_dwarf_info (w, klass);
864                         else
865                                 tdie = ".LDIE_I4";
866                         break;
867                 default:
868                         tdie = ".LDIE_I4";
869                         break;
870                 }
871         }
872         if (t->byref)
873                 // FIXME:
874                 tdie = ".LDIE_I4";
875         emit_symbol_diff (w, tdie, ".Ldebug_info_start", 0);
876 }
877
878 static void
879 encode_var_location (MonoDwarfWriter *w, MonoInst *ins, guint8 *p, guint8 **endp)
880 {
881         /* location */
882         /* FIXME: This needs a location list, since the args can go from reg->stack */
883         if (!ins || ins->flags & MONO_INST_IS_DEAD) {
884                 /* gdb treats this as optimized out */
885         } else if (ins->opcode == OP_REGVAR) {
886                 *p = DW_OP_reg0 + mono_hw_reg_to_dwarf_reg (ins->dreg);
887                 p ++;
888         } else if (ins->opcode == OP_REGOFFSET) {
889                 *p ++= DW_OP_breg0 + mono_hw_reg_to_dwarf_reg (ins->inst_basereg);
890                 encode_sleb128 (ins->inst_offset, p, &p);
891         } else {
892                 // FIXME:
893                 *p ++ = DW_OP_reg0;
894         }
895
896         *endp = p;
897 }
898
899 static void
900 emit_loclist (MonoDwarfWriter *w, MonoInst *ins,
901                           guint8 *loclist_begin_addr, guint8 *loclist_end_addr,
902                           guint8 *expr, guint32 expr_len)
903 {
904         char label [128];
905
906         emit_push_section (w, ".debug_loc", 0);
907         sprintf (label, ".Lloclist_%d", w->loclist_index ++ );
908         emit_label (w, label);
909
910         emit_pointer_value (w, loclist_begin_addr);
911         emit_pointer_value (w, loclist_end_addr);
912         emit_byte (w, expr_len % 256);
913         emit_byte (w, expr_len / 256);
914         emit_bytes (w, expr, expr_len);
915
916         emit_pointer_value (w, NULL);
917         emit_pointer_value (w, NULL);
918
919         emit_pop_section (w);
920         emit_symbol_diff (w, label, ".Ldebug_loc_start", 0);
921 }
922
923 /* 
924  * MonoDisHelper->tokener doesn't take an IP argument, and we can't add one since 
925  * it is a public header.
926  */
927 static const guint8 *token_handler_ip;
928
929 static char*
930 token_handler (MonoDisHelper *dh, MonoMethod *method, guint32 token)
931 {
932         char *res, *desc;
933         MonoMethod *cmethod;
934         MonoClass *klass;
935         gpointer data = NULL;
936
937         if (method->wrapper_type)
938                 data = mono_method_get_wrapper_data (method, token);
939
940         switch (*token_handler_ip) {
941         case CEE_ISINST:
942         case CEE_LDELEMA:
943                 if (method->wrapper_type)
944                         klass = data;
945                 else
946                         klass = mono_class_get_full (method->klass->image, token, NULL);
947                 res = g_strdup_printf ("<%s>", klass->name);
948                 break;
949         case CEE_NEWOBJ:
950         case CEE_CALL:
951         case CEE_CALLVIRT:
952                 if (method->wrapper_type)
953                         cmethod = data;
954                 else
955                         cmethod = mono_get_method_full (method->klass->image, token, NULL, NULL);
956                 desc = mono_method_full_name (cmethod, TRUE);
957                 res = g_strdup_printf ("<%s>", desc);
958                 g_free (desc);
959                 break;
960         case CEE_CALLI:
961                 if (method->wrapper_type) {
962                         desc = mono_signature_get_desc (data, FALSE);
963                         res = g_strdup_printf ("<%s>", desc);
964                         g_free (desc);
965                 } else {
966                         res = g_strdup_printf ("<0x%08x>", token);
967                 }
968                 break;
969         default:
970                 res = g_strdup_printf ("<0x%08x>", token);
971                 break;
972         }
973
974         return res;
975 }
976
977 /*
978  * disasm_ins:
979  *
980  *   Produce a disassembled form of the IL instruction at IP. This is an extension
981  * of mono_disasm_code_one () which can disasm tokens, handle wrapper methods, and
982  * CEE_MONO_ opcodes.
983  */
984 static char*
985 disasm_ins (MonoMethod *method, const guchar *ip, const guint8 **endip)
986 {
987         char *dis;
988         MonoDisHelper dh;
989         MonoMethodHeader *header = mono_method_get_header (method);
990
991         memset (&dh, 0, sizeof (dh));
992         dh.newline = "";
993         dh.label_format = "IL_%04x: ";
994         dh.label_target = "IL_%04x";
995         dh.tokener = token_handler;
996
997         token_handler_ip = ip;
998         if (*ip == MONO_CUSTOM_PREFIX) {
999                 guint32 token;
1000                 gpointer data;
1001
1002                 switch (ip [1]) {
1003                 case CEE_MONO_ICALL: {
1004                         MonoJitICallInfo *info;
1005
1006                         token = read32 (ip + 2);
1007                         data = mono_method_get_wrapper_data (method, token);
1008                         info = mono_find_jit_icall_by_addr (data);
1009                         g_assert (info);
1010
1011                         dis = g_strdup_printf ("IL_%04x: mono_icall <%s>", (int)(ip - header->code), info->name);
1012                         ip += 6;
1013                         break;
1014                 }
1015                 case CEE_MONO_CLASSCONST: {
1016                         token = read32 (ip + 2);
1017                         data = mono_method_get_wrapper_data (method, token);
1018
1019                         dis = g_strdup_printf ("IL_%04x: mono_classconst <%s>", (int)(ip - header->code), ((MonoClass*)data)->name);
1020                         ip += 6;
1021                         break;
1022                 }
1023                 default:
1024                         dis = mono_disasm_code_one (&dh, method, ip, &ip);
1025                 }
1026         } else {
1027                 dis = mono_disasm_code_one (&dh, method, ip, &ip);
1028         }
1029         token_handler_ip = NULL;
1030
1031         *endip = ip;
1032         return dis;
1033 }
1034
1035 /*
1036 static gint32
1037 il_offset_from_address (MonoMethod *method, MonoDebugMethodJitInfo *jit, 
1038                                                 guint32 native_offset)
1039 {
1040         int i;
1041
1042         if (!jit->line_numbers)
1043                 return -1;
1044
1045         for (i = jit->num_line_numbers - 1; i >= 0; i--) {
1046                 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
1047
1048                 if (lne.native_offset <= native_offset)
1049                         return lne.il_offset;
1050         }
1051
1052         return -1;
1053 }
1054 */
1055
1056 static int max_special_addr_diff = 0;
1057
1058 static inline void
1059 emit_advance_op (MonoDwarfWriter *w, int line_diff, int addr_diff)
1060 {
1061         gint64 opcode = 0;
1062
1063         /* Use a special opcode if possible */
1064         if (line_diff - LINE_BASE >= 0 && line_diff - LINE_BASE < LINE_RANGE) {
1065                 if (max_special_addr_diff == 0)
1066                         max_special_addr_diff = (255 - OPCODE_BASE) / LINE_RANGE;
1067
1068                 if (addr_diff > max_special_addr_diff && (addr_diff < 2 * max_special_addr_diff)) {
1069                         emit_byte (w, DW_LNS_const_add_pc);
1070                         addr_diff -= max_special_addr_diff;
1071                 }
1072
1073                 opcode = (line_diff - LINE_BASE) + (LINE_RANGE * addr_diff) + OPCODE_BASE;
1074                 if (opcode > 255)
1075                         opcode = 0;
1076         }
1077
1078         if (opcode != 0) {
1079                 emit_byte (w, opcode);
1080         } else {
1081                 emit_byte (w, DW_LNS_advance_line);
1082                 emit_sleb128 (w, line_diff);
1083                 emit_byte (w, DW_LNS_advance_pc);
1084                 emit_sleb128 (w, addr_diff);
1085                 emit_byte (w, DW_LNS_copy);
1086         }
1087 }
1088
1089 static gint
1090 compare_lne (MonoDebugLineNumberEntry *a, MonoDebugLineNumberEntry *b)
1091 {
1092         if (a->native_offset == b->native_offset)
1093                 return a->il_offset - b->il_offset;
1094         else
1095                 return a->native_offset - b->native_offset;
1096 }
1097
1098 static void
1099 emit_line_number_info (MonoDwarfWriter *w, MonoMethod *method, guint8 *code,
1100                                            guint32 code_size, MonoDebugMethodJitInfo *debug_info)
1101 {
1102         guint32 prev_line = 0;
1103         guint32 prev_native_offset = 0;
1104         int i, file_index, il_offset, prev_il_offset;
1105         gboolean first = TRUE;
1106         MonoDebugSourceLocation *loc;
1107         char *prev_file_name = NULL;
1108         MonoMethodHeader *header = mono_method_get_header (method);
1109         MonoDebugMethodInfo *minfo;
1110         GArray *ln_array;
1111         int *native_to_il_offset;
1112
1113         if (!code)
1114                 // FIXME: The set_address op below only works with xdebug
1115                 return;
1116
1117         minfo = mono_debug_lookup_method (method);
1118
1119         /* Compute the native->IL offset mapping */
1120
1121         ln_array = g_array_sized_new (FALSE, FALSE, sizeof (MonoDebugLineNumberEntry), 
1122                                                                   debug_info->num_line_numbers);
1123         g_array_append_vals (ln_array, debug_info->line_numbers, debug_info->num_line_numbers);
1124         g_array_sort (ln_array, (GCompareFunc)compare_lne);
1125         native_to_il_offset = g_new0 (int, code_size + 1);
1126
1127         for (i = 0; i < debug_info->num_line_numbers; ++i) {
1128                 int j;
1129                 MonoDebugLineNumberEntry lne = g_array_index (ln_array, MonoDebugLineNumberEntry, i);
1130
1131                 if (i == 0) {
1132                         for (j = 0; j < lne.native_offset; ++j)
1133                                 native_to_il_offset [j] = -1;
1134                 }
1135
1136                 if (i < debug_info->num_line_numbers - 1) {
1137                         MonoDebugLineNumberEntry lne_next = g_array_index (ln_array, MonoDebugLineNumberEntry, i + 1);
1138
1139                         for (j = lne.native_offset; j < lne_next.native_offset; ++j)
1140                                 native_to_il_offset [j] = lne.il_offset;
1141                 } else {
1142                         for (j = lne.native_offset; j < code_size; ++j)
1143                                 native_to_il_offset [j] = lne.il_offset;
1144                 }
1145         }
1146         g_array_free (ln_array, TRUE);
1147
1148         prev_line = 1;
1149         prev_il_offset = -1;
1150
1151         for (i = 0; i < code_size; ++i) {
1152                 if (!minfo)
1153                         continue;
1154
1155                 if (!debug_info->line_numbers)
1156                         continue;
1157
1158                 /*
1159                 il_offset = il_offset_from_address (method, debug_info, i);
1160
1161                 g_assert (il_offset == native_to_il_offset [i]);
1162                 */
1163                 il_offset = native_to_il_offset [i];
1164                 if (il_offset < 0)
1165                         continue;
1166
1167                 if (il_offset == prev_il_offset)
1168                         continue;
1169
1170                 prev_il_offset = il_offset;
1171
1172                 loc = mono_debug_symfile_lookup_location (minfo, il_offset);
1173
1174                 if (loc) {
1175                         int line_diff = (gint32)loc->row - (gint32)prev_line;
1176                         int addr_diff = i - prev_native_offset;
1177
1178                         if (first) {    
1179                                 emit_section_change (w, ".debug_line", LINE_SUBSECTION_DATA);
1180
1181                                 emit_byte (w, 0);
1182                                 emit_byte (w, sizeof (gpointer) + 1);
1183                                 emit_byte (w, DW_LNE_set_address);
1184                                 emit_pointer_value (w, code);
1185
1186                                 /* 
1187                                  * The prolog+initlocals region does not have a line number, this
1188                                  * makes them belong to the first line of the method.
1189                                  */
1190                                 emit_byte (w, DW_LNS_advance_line);
1191                                 emit_sleb128 (w, (gint32)loc->row - (gint32)prev_line);
1192                                 prev_line = loc->row;
1193                         }
1194
1195                         if (loc->row != prev_line) {
1196                                 if (!prev_file_name || strcmp (loc->source_file, prev_file_name) != 0) {
1197                                         /* Add an entry to the file table */
1198                                         /* FIXME: Avoid duplicates */
1199                                         file_index = emit_line_number_file_name (w, loc->source_file, 0, 0);
1200                                         g_free (prev_file_name);
1201                                         prev_file_name = g_strdup (loc->source_file);
1202
1203                                         emit_byte (w, DW_LNS_set_file);
1204                                         emit_uleb128 (w, file_index);
1205                                         emit_byte (w, DW_LNS_copy);
1206                                 }
1207
1208                                 //printf ("X: %p(+0x%x) %d %s:%d(+%d)\n", code + i, addr_diff, loc->il_offset, loc->source_file, loc->row, line_diff);
1209
1210                                 emit_advance_op (w, line_diff, addr_diff);
1211
1212                                 prev_line = loc->row;
1213                                 prev_native_offset = i;
1214                         }
1215
1216                         first = FALSE;
1217                         g_free (loc);
1218                 }
1219         }
1220
1221         g_free (prev_file_name);
1222
1223         if (!first) {
1224                 emit_byte (w, DW_LNS_advance_pc);
1225                 emit_sleb128 (w, code_size - prev_native_offset);
1226                 emit_byte (w, DW_LNS_copy);
1227
1228                 emit_byte (w, 0);
1229                 emit_byte (w, 1);
1230                 emit_byte (w, DW_LNE_end_sequence);
1231         } else if (code) {
1232                 /* No debug info, XDEBUG mode */
1233                 char *name, *dis;
1234                 const guint8 *ip = header->code;
1235                 int prev_line, prev_native_offset;
1236                 int *il_to_line;
1237
1238                 /*
1239                  * Emit the IL code into a temporary file and emit line number info
1240                  * referencing that file.
1241                  */
1242
1243                 name = mono_method_full_name (method, TRUE);
1244                 fprintf (w->il_file, "// %s\n", name);
1245                 w->il_file_line_index ++;
1246                 g_free (name);
1247
1248                 il_to_line = g_new0 (int, header->code_size);
1249
1250                 emit_section_change (w, ".debug_line", LINE_SUBSECTION_DATA);
1251                 emit_byte (w, 0);
1252                 emit_byte (w, sizeof (gpointer) + 1);
1253                 emit_byte (w, DW_LNE_set_address);
1254                 emit_pointer_value (w, code);
1255
1256                 // FIXME: Optimize this
1257                 while (ip < header->code + header->code_size) {
1258                         int il_offset = ip - header->code;
1259
1260                         /* Emit IL */
1261                         w->il_file_line_index ++;
1262
1263                         dis = disasm_ins (method, ip, &ip);
1264                         fprintf (w->il_file, "%s\n", dis);
1265                         g_free (dis);
1266
1267                         il_to_line [il_offset] = w->il_file_line_index;
1268                 }
1269
1270                 /* Emit line number info */
1271                 prev_line = 1;
1272                 prev_native_offset = 0;
1273                 for (i = 0; i < debug_info->num_line_numbers; ++i) {
1274                         MonoDebugLineNumberEntry *lne = &debug_info->line_numbers [i];
1275                         int line;
1276
1277                         if (lne->il_offset >= header->code_size)
1278                                 continue;
1279                         line = il_to_line [lne->il_offset];
1280                         if (!line) {
1281                                 /* To help debugging */
1282                                 printf ("%s\n", mono_method_full_name (method, TRUE));
1283                                 printf ("%d %d\n", lne->il_offset, header->code_size);
1284                                 g_assert (line);
1285                         }
1286
1287                         if (line - prev_line != 0) {
1288                                 emit_advance_op (w, line - prev_line, (gint32)lne->native_offset - prev_native_offset);
1289
1290                                 prev_line = line;
1291                                 prev_native_offset = lne->native_offset;
1292                         }
1293                 }
1294
1295                 emit_byte (w, DW_LNS_advance_pc);
1296                 emit_sleb128 (w, code_size - prev_native_offset);
1297                 emit_byte (w, DW_LNS_copy);
1298
1299                 emit_byte (w, 0);
1300                 emit_byte (w, 1);
1301                 emit_byte (w, DW_LNE_end_sequence);
1302
1303                 fflush (w->il_file);
1304                 g_free (il_to_line);
1305         }
1306 }
1307
1308 void
1309 mono_dwarf_writer_emit_method (MonoDwarfWriter *w, MonoCompile *cfg, MonoMethod *method, char *start_symbol, char *end_symbol, guint8 *code, guint32 code_size, MonoInst **args, MonoInst **locals, GSList *unwind_info, MonoDebugMethodJitInfo *debug_info)
1310 {
1311         char *name;
1312         MonoMethodSignature *sig;
1313         MonoMethodHeader *header;
1314         char **names, **tdies, **local_tdies;
1315         char **local_names;
1316         int *local_indexes;
1317         int i, num_locals;
1318         guint8 buf [128];
1319         guint8 *p;
1320
1321         emit_section_change (w, ".debug_info", 0);
1322
1323         sig = mono_method_signature (method);
1324         header = mono_method_get_header (method);
1325
1326         /* Parameter types */
1327         tdies = g_new0 (char *, sig->param_count + sig->hasthis);
1328         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1329                 MonoType *t;
1330
1331                 if (i == 0 && sig->hasthis) {
1332                         t = &method->klass->this_arg;
1333                 } else {
1334                         t = sig->params [i - sig->hasthis];
1335                 }
1336
1337                 emit_class_dwarf_info (w, mono_class_from_mono_type (t));
1338         }
1339
1340         /* Local types */
1341         local_tdies = g_new0 (char *, header->num_locals);
1342         for (i = 0; i < header->num_locals; ++i) {
1343                 emit_class_dwarf_info (w, mono_class_from_mono_type (header->locals [i]));
1344         }
1345
1346         /* Subprogram */
1347         names = g_new0 (char *, sig->param_count);
1348         mono_method_get_param_names (method, (const char **) names);
1349
1350         emit_uleb128 (w, ABBREV_SUBPROGRAM);
1351         name = mono_method_full_name (method, FALSE);
1352         emit_string (w, name);
1353         g_free (name);
1354         if (start_symbol) {
1355                 emit_pointer_unaligned (w, start_symbol);
1356                 emit_pointer_unaligned (w, end_symbol);
1357         } else {
1358                 emit_pointer_value (w, code);
1359                 emit_pointer_value (w, code + code_size);
1360         }
1361         /* frame_base */
1362         emit_byte (w, 2);
1363         emit_byte (w, DW_OP_breg6);
1364         emit_byte (w, 16);
1365
1366         /* Parameters */
1367         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1368                 MonoInst *arg = args ? args [i] : NULL;
1369                 MonoType *t;
1370                 const char *pname;
1371                 char pname_buf [128];
1372
1373                 if (i == 0 && sig->hasthis) {
1374                         t = &mono_defaults.object_class->byval_arg;
1375                         pname = "this";
1376                 } else {
1377                         t = sig->params [i - sig->hasthis];
1378                         pname = names [i - sig->hasthis];
1379                 }
1380                 
1381                 emit_uleb128 (w, ABBREV_PARAM);
1382                 /* name */
1383                 if (pname[0] == '\0') {
1384                         sprintf (pname_buf, "param%d", i - sig->hasthis);
1385                         pname = pname_buf;
1386                 }
1387                 emit_string (w, pname);
1388                 /* type */
1389                 if (!arg || arg->flags & MONO_INST_IS_DEAD)
1390                         emit_var_type (w, &mono_defaults.int32_class->byval_arg);
1391                 else
1392                         emit_var_type (w, t);
1393
1394                 p = buf;
1395                 encode_var_location (w, arg, p, &p);
1396                 emit_byte (w, p - buf);
1397                 emit_bytes (w, buf, p - buf);
1398         }               
1399         g_free (names);
1400
1401         /* Locals */
1402         num_locals = mono_debug_lookup_locals (method, &local_names, &local_indexes);
1403
1404         for (i = 0; i < header->num_locals; ++i) {
1405                 MonoInst *ins = locals [i];
1406                 char name_buf [128];
1407                 int j;
1408                 MonoMethodVar *vmv = NULL;
1409                 gboolean need_loclist = FALSE;
1410
1411                 /* ins->dreg no longer contains the original vreg */
1412                 if (cfg->varinfo) {
1413                         for (j = 0; j < cfg->num_varinfo; ++j) {
1414                                 if (cfg->varinfo [j] == ins)
1415                                         break;
1416                         }
1417
1418                         if (code && j < cfg->num_varinfo) {
1419                                 vmv = MONO_VARINFO (cfg, j);
1420                                 if (vmv->live_range_start) {
1421                                         /* This variable has a precise live range */
1422                                         need_loclist = TRUE;
1423                                 }
1424                         }
1425                 }
1426
1427                 if (need_loclist)
1428                         emit_uleb128 (w, ABBREV_VARIABLE_LOCLIST);
1429                 else
1430                         emit_uleb128 (w, ABBREV_VARIABLE);
1431                 /* name */
1432                 for (j = 0; j < num_locals; ++j)
1433                         if (local_indexes [j] == i)
1434                                 break;
1435                 if (j < num_locals) {
1436                         emit_string (w, local_names [j]);
1437                 } else {
1438                         sprintf (name_buf, "V_%d", i);
1439                         emit_string (w, name_buf);
1440                 }
1441                 /* type */
1442                 if (!ins || ins->flags & MONO_INST_IS_DEAD)
1443                         emit_var_type (w, &mono_defaults.int32_class->byval_arg);
1444                 else
1445                         emit_var_type (w, header->locals [i]);
1446
1447                 p = buf;
1448                 encode_var_location (w, ins, p, &p);
1449
1450                 if (need_loclist) {
1451                         if (vmv->live_range_end == 0)
1452                                 /* FIXME: Uses made in calls are not recorded */
1453                                 vmv->live_range_end = code_size;
1454                         emit_loclist (w, ins, code + vmv->live_range_start, code + vmv->live_range_end, buf, p - buf);
1455                 } else {
1456                         emit_byte (w, p - buf);
1457                         emit_bytes (w, buf, p - buf);
1458                 }
1459         }
1460
1461         g_free (local_names);
1462         g_free (local_indexes);
1463
1464         /* Subprogram end */
1465         emit_uleb128 (w, 0x0);
1466
1467         emit_line (w);
1468
1469         /* Emit unwind info */
1470         emit_fde (w, w->fde_index, start_symbol, end_symbol, code, code_size, unwind_info, TRUE);
1471         w->fde_index ++;
1472
1473         /* Emit line number info */
1474         if (code && debug_info)
1475                 emit_line_number_info (w, method, code, code_size, debug_info);
1476
1477         emit_line (w);
1478 }
1479
1480 void
1481 mono_dwarf_writer_emit_trampoline (MonoDwarfWriter *w, const char *tramp_name, char *start_symbol, char *end_symbol, guint8 *code, guint32 code_size, GSList *unwind_info)
1482 {
1483         emit_section_change (w, ".debug_info", 0);
1484
1485         /* Subprogram */
1486         emit_uleb128 (w, ABBREV_SUBPROGRAM);
1487         emit_string (w, tramp_name);
1488         emit_pointer_value (w, code);
1489         emit_pointer_value (w, code + code_size);
1490         /* frame_base */
1491         emit_byte (w, 2);
1492         emit_byte (w, DW_OP_breg6);
1493         emit_byte (w, 16);
1494
1495         /* Subprogram end */
1496         emit_uleb128 (w, 0x0);
1497
1498         /* Emit unwind info */
1499         emit_fde (w, w->fde_index, start_symbol, end_symbol, code, code_size, unwind_info, FALSE);
1500         w->fde_index ++;
1501 }