fixed broken build
[mono.git] / mono / jit / debug-stabs.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <mono/metadata/class.h>
4 #include <mono/metadata/tabledefs.h>
5 #include <mono/metadata/tokentype.h>
6 #include <mono/jit/codegen.h>
7 #include <mono/jit/debug.h>
8
9 #include "debug-private.h"
10
11 typedef struct {
12         const char *name;
13         const char *spec;
14 } BaseTypes;
15
16 /*
17  * Not 64 bit clean.
18  * Note: same order of MonoTypeEnum.
19  */
20 static BaseTypes
21 base_types[] = {
22         {"", NULL},
23         {"Void", "(0,1)"},
24         {"Boolean", ";0;255;"},
25         {"Char", ";0;65535;"},
26         {"SByte", ";-128;127;"},
27         {"Byte", ";0;255;"},
28         {"Int16", ";-32768;32767;"},
29         {"UInt16", ";0;65535;"},
30         {"Int32", ";0020000000000;0017777777777;"},
31         {"UInt32", ";0000000000000;0037777777777;"},
32         {"Int64", ";01000000000000000000000;0777777777777777777777;"},
33         {"UInt64", ";0000000000000;01777777777777777777777;"},
34         {"Single", "r(0,8);4;0;"},
35         {"Double", "r(0,8);8;0;"},
36         {"String", "(0,41)=*(0,42)=xsMonoString:"}, /*string*/
37         {"", }, /*ptr*/
38         {"", }, /*byref*/
39         {"", }, /*valuetype*/
40         {"Class", "(0,44)=*(0,45)=xsMonoObject:"}, /*class*/
41         {"", }, /*unused*/
42         {"Array", }, /*array*/
43         {"", }, /*typedbyref*/
44         {"", }, /*unused*/
45         {"", }, /*unused*/
46         {"IntPtr", ";0020000000000;0017777777777;"},
47         {"UIntPtr", ";0000000000000;0037777777777;"},
48         {"", }, /*unused*/
49         {"FnPtr", "*(0,1)"}, /*fnptr*/
50         {"Object", "(0,47)=*(0,48)=xsMonoObject:"}, /*object*/
51         {"SzArray", "(0,50)=*(0,51))=xsMonoArray:"}, /*szarray*/
52         {NULL, NULL}
53 };
54
55 void
56 mono_debug_open_assembly_stabs (AssemblyDebugInfo* info)
57 {
58 }
59
60 void
61 mono_debug_close_assembly_stabs (AssemblyDebugInfo* info)
62 {
63 }
64
65 static void
66 write_method_stabs (AssemblyDebugInfo *info, DebugMethodInfo *minfo)
67 {
68         int i;
69         MonoMethod *method = minfo->method_info.method;
70         MonoClass *klass = method->klass;
71         MonoMethodSignature *sig = method->signature;
72         char **names = g_new (char*, sig->param_count);
73
74         fprintf (info->f, ".stabs \"%s:F(0,%d)\",36,0,%d,%p\n", minfo->name, sig->ret->type,
75                  minfo->start_line, minfo->method_info.code_start);
76
77         /* params */
78         mono_method_get_param_names (method, (const char **)names);
79         if (sig->hasthis)
80                 fprintf (info->f, ".stabs \"this:p(0,%d)=(0,%d)\",160,0,%d,%d\n",
81                          info->next_idx++, klass->byval_arg.type, minfo->start_line,
82                          minfo->method_info.this_offset);
83         for (i = 0; i < minfo->method_info.num_params; i++) {
84                 int stack_offset = minfo->method_info.param_offsets [i];
85
86                 fprintf (info->f, ".stabs \"%s:p(0,%d)=(0,%d)\",160,0,%d,%d\n",
87                          names [i], info->next_idx++, sig->params [i]->type,
88                          minfo->start_line, stack_offset);
89         }
90
91         /* local vars */
92         for (i = 0; i < minfo->method_info.num_locals; ++i) {
93                 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
94                 int stack_offset = minfo->method_info.local_offsets [i];
95
96                 fprintf (info->f, ".stabs \"local_%d:(0,%d)=(0,%d)\",128,0,%d,%d\n",
97                          i, info->next_idx++, header->locals [i]->type, minfo->start_line, stack_offset);
98         }
99
100         if (minfo->line_numbers) {
101                 fprintf (info->f, ".stabn 68,0,%d,%d\n", minfo->start_line, 0);
102                 fprintf (info->f, ".stabn 68,0,%d,%d\n", minfo->first_line, minfo->prologue_end_offset);
103
104                 for (i = 1; i < minfo->line_numbers->len; i++) {
105                         DebugLineNumberInfo *lni = g_ptr_array_index (minfo->line_numbers, i);
106
107                         fprintf (info->f, ".stabn 68,0,%d,%d\n", lni->line,
108                                  (char *)lni->address - minfo->method_info.code_start);
109                 }
110
111                 fprintf (info->f, ".stabn 68,0,%d,%d\n", minfo->last_line, minfo->epilogue_begin_offset);
112         }
113
114         /* end of function */
115         fprintf (info->f, ".stabs \"\",36,0,0,%d\n", minfo->method_info.code_size);
116
117         g_free (names);
118         fflush (info->f);
119 }
120
121 static void
122 get_enumvalue (MonoClass *klass, int idx, char *buf)
123 {
124         guint32 const_cols [MONO_CONSTANT_SIZE];
125         const char *ptr;
126         guint32 crow = mono_metadata_get_constant_index (klass->image, MONO_TOKEN_FIELD_DEF | (idx + 1));
127
128         if (!crow) {
129                 buf [0] = '0';
130                 buf [1] = 0;
131                 return;
132         }
133         mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
134         ptr = mono_metadata_blob_heap (klass->image, const_cols [MONO_CONSTANT_VALUE]);
135         switch (const_cols [MONO_CONSTANT_TYPE]) {
136         case MONO_TYPE_U4:
137         case MONO_TYPE_I4:
138                 /* FIXME: add other types... */
139         default:
140                 g_snprintf (buf, 64, "%d", *(gint32*)ptr);
141         }
142 }
143
144 static void
145 write_method_func (gpointer key, gpointer value, gpointer user_data)
146 {
147         write_method_stabs (user_data, value);
148 }
149
150 static void
151 write_class_stabs (AssemblyDebugInfo *info, MonoClass *klass, int idx)
152 {
153         char *name;
154         int i;
155         char buf [64];
156
157         /* output enums ...*/
158         if (klass->enumtype) {
159                 name = g_strdup_printf ("%s%s%s", klass->name_space, klass->name_space [0]? "_": "", klass->name);
160                 fprintf (info->f, ".stabs \"%s:T%d=e", name, ++info->next_idx);
161                 g_free (name);
162                 for (i = 0; i < klass->field.count; ++i) {
163                         if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_LITERAL) {
164                                 get_enumvalue (klass, klass->field.first + i, buf);
165                                 fprintf (info->f, "%s_%s=%s,", klass->name, klass->fields [i].name, buf);
166                         }
167                 }
168                 fprintf (info->f, ";\",128,0,0,0\n");
169         }
170         fflush (info->f);
171 }
172
173 static void
174 write_class (gpointer key, gpointer value, gpointer user_data)
175 {
176         write_class_stabs (user_data, key, GPOINTER_TO_INT (value));
177 }
178
179 void
180 mono_debug_write_assembly_stabs (AssemblyDebugInfo* info)
181 {
182         char *buf;
183         int i;
184
185         if (!(info->f = fopen (info->filename, "w")))
186                 return;
187
188         fprintf (info->f, ".stabs \"%s.il\",100,0,0,0\n", info->name);
189
190         for (i = 0; base_types [i].name; ++i) {
191                 if (! base_types [i].spec)
192                         continue;
193                 fprintf (info->f, ".stabs \"%s:t(0,%d)=", base_types [i].name, i);
194                 if (base_types [i].spec [0] == ';') {
195                         fprintf (info->f, "r(0,%d)%s\"", i, base_types [i].spec);
196                 } else {
197                         fprintf (info->f, "%s\"", base_types [i].spec);
198                 }
199                 fprintf (info->f, ",128,0,0,0\n");
200         }
201
202         g_hash_table_foreach (info->methods, write_method_func, info);
203
204         g_hash_table_foreach (info->type_hash, write_class, info);
205
206         fclose (info->f);
207         info->f = NULL;
208
209         /* yes, it's completely unsafe */
210         buf = g_strdup_printf ("as %s -o /tmp/%s.o", info->filename, info->name);
211         system (buf);
212         g_free (buf);
213 }