2002-03-26 Martin Baulig <martin@gnome.org>
[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         char *name;
13         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         /*
75          * We need to output all the basic info, if we change filename...
76          * fprintf (info->f, ".stabs \"%s.il\",100,0,0,0\n", klass->image->assembly_name);
77          */
78         fprintf (info->f, ".stabs \"%s:F(0,%d)\",36,0,%d,%p\n", minfo->name, sig->ret->type,
79                  minfo->start_line, minfo->method_info.code_start);
80
81         /* params */
82         mono_method_get_param_names (method, (const char **)names);
83         if (sig->hasthis)
84                 fprintf (info->f, ".stabs \"this:p(0,%d)=(0,%d)\",160,0,%d,%d\n",
85                          info->next_idx++, klass->byval_arg.type, minfo->start_line,
86                          minfo->method_info.this_offset);
87         for (i = 0; i < minfo->method_info.num_params; i++) {
88                 int stack_offset = minfo->method_info.param_offsets [i];
89
90                 fprintf (info->f, ".stabs \"%s:p(0,%d)=(0,%d)\",160,0,%d,%d\n",
91                          names [i], info->next_idx++, sig->params [i]->type,
92                          minfo->start_line, stack_offset);
93         }
94
95         /* local vars */
96         for (i = 0; i < minfo->method_info.num_locals; ++i) {
97                 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
98                 int stack_offset = minfo->method_info.local_offsets [i];
99
100                 fprintf (info->f, ".stabs \"local_%d:(0,%d)=(0,%d)\",128,0,%d,%d\n",
101                          i, info->next_idx++, header->locals [i]->type, minfo->start_line, stack_offset);
102         }
103
104         if (minfo->line_numbers) {
105                 fprintf (info->f, ".stabn 68,0,%d,%d\n", minfo->start_line, 0);
106
107                 for (i = 1; i < minfo->line_numbers->len; i++) {
108                         DebugLineNumberInfo *lni = g_ptr_array_index (minfo->line_numbers, i);
109
110                         fprintf (info->f, ".stabn 68,0,%d,%d\n", lni->line,
111                                  lni->address - minfo->method_info.code_start);
112                 }
113         }
114
115         /* end of function */
116         fprintf (info->f, ".stabs \"\",36,0,0,%d\n", minfo->method_info.code_size);
117
118         g_free (names);
119         fflush (info->f);
120 }
121
122 static void
123 get_enumvalue (MonoClass *klass, int index, char *buf)
124 {
125         guint32 const_cols [MONO_CONSTANT_SIZE];
126         const char *ptr;
127         guint32 crow = mono_metadata_get_constant_index (klass->image, MONO_TOKEN_FIELD_DEF | (index + 1));
128
129         if (!crow) {
130                 buf [0] = '0';
131                 buf [1] = 0;
132                 return;
133         }
134         mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
135         ptr = mono_metadata_blob_heap (klass->image, const_cols [MONO_CONSTANT_VALUE]);
136         switch (const_cols [MONO_CONSTANT_TYPE]) {
137         case MONO_TYPE_U4:
138         case MONO_TYPE_I4:
139                 /* FIXME: add other types... */
140         default:
141                 g_snprintf (buf, 64, "%d", *(gint32*)ptr);
142         }
143 }
144
145 static void
146 write_method_func (gpointer key, gpointer value, gpointer user_data)
147 {
148         write_method_stabs (user_data, value);
149 }
150
151 static void
152 write_class_stabs (AssemblyDebugInfo *info, MonoClass *klass, int index)
153 {
154         char *name;
155         int i;
156         char buf [64];
157
158         /* output enums ...*/
159         if (klass->enumtype) {
160                 name = g_strdup_printf ("%s%s%s", klass->name_space, klass->name_space [0]? "_": "", klass->name);
161                 fprintf (info->f, ".stabs \"%s:T%d=e", name, ++info->next_idx);
162                 g_free (name);
163                 for (i = 0; i < klass->field.count; ++i) {
164                         if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_LITERAL) {
165                                 get_enumvalue (klass, klass->field.first + i, buf);
166                                 fprintf (info->f, "%s_%s=%s,", klass->name, klass->fields [i].name, buf);
167                         }
168                 }
169                 fprintf (info->f, ";\",128,0,0,0\n");
170         }
171         fflush (info->f);
172 }
173
174 static void
175 write_class (gpointer key, gpointer value, gpointer user_data)
176 {
177         write_class_stabs (user_data, key, GPOINTER_TO_INT (value));
178 }
179
180 void
181 mono_debug_write_assembly_stabs (AssemblyDebugInfo* info)
182 {
183         char *buf;
184         int i;
185
186         if (!(info->f = fopen (info->filename, "w")))
187                 return;
188
189         for (i = 0; base_types [i].name; ++i) {
190                 if (! base_types [i].spec)
191                         continue;
192                 fprintf (info->f, ".stabs \"%s:t(0,%d)=", base_types [i].name, i);
193                 if (base_types [i].spec [0] == ';') {
194                         fprintf (info->f, "r(0,%d)%s\"", i, base_types [i].spec);
195                 } else {
196                         fprintf (info->f, "%s\"", base_types [i].spec);
197                 }
198                 fprintf (info->f, ",128,0,0,0\n");
199         }
200
201         g_hash_table_foreach (info->methods, write_method_func, info);
202
203         g_hash_table_foreach (info->type_hash, write_class, info);
204
205         fclose (info->f);
206         info->f = NULL;
207
208         /* yes, it's completely unsafe */
209         buf = g_strdup_printf ("as %s -o /tmp/%s.o", info->filename, info->name);
210         system (buf);
211         g_free (buf);
212 }