4 #include <mono/metadata/class.h>
5 #include <mono/metadata/tabledefs.h>
6 #include <mono/metadata/tokentype.h>
7 #include <mono/jit/codegen.h>
8 #include <mono/jit/debug.h>
10 #include "debug-private.h"
19 * Note: same order of MonoTypeEnum.
25 {"Boolean", ";0;255;"},
26 {"Char", ";0;65535;"},
27 {"SByte", ";-128;127;"},
29 {"Int16", ";-32768;32767;"},
30 {"UInt16", ";0;65535;"},
31 {"Int32", ";0020000000000;0017777777777;"},
32 {"UInt32", ";0000000000000;0037777777777;"},
33 {"Int64", ";01000000000000000000000;0777777777777777777777;"},
34 {"UInt64", ";0000000000000;01777777777777777777777;"},
35 {"Single", "r(0,8);4;0;"},
36 {"Double", "r(0,8);8;0;"},
37 {"String", "(0,41)=*(0,42)=xsMonoString:"}, /*string*/
41 {"Class", "(0,44)=*(0,45)=xsMonoObject:"}, /*class*/
43 {"Array", }, /*array*/
44 {"", }, /*typedbyref*/
47 {"IntPtr", ";0020000000000;0017777777777;"},
48 {"UIntPtr", ";0000000000000;0037777777777;"},
50 {"FnPtr", "*(0,1)"}, /*fnptr*/
51 {"Object", "(0,47)=*(0,48)=xsMonoObject:"}, /*object*/
52 {"SzArray", "(0,50)=*(0,51))=xsMonoArray:"}, /*szarray*/
57 write_method_stabs (MonoDebugHandle *debug, DebugMethodInfo *minfo)
60 MonoMethod *method = minfo->method_info.method;
61 MonoClass *klass = method->klass;
62 MonoMethodSignature *sig = method->signature;
63 char **names = g_new (char*, sig->param_count);
66 source_file = g_ptr_array_index (debug->source_files, minfo->source_file);
68 fprintf (debug->f, ".stabs \"%s\",100,0,0,0\n", source_file);
70 fprintf (debug->f, ".stabs \"%s:F(0,%d)\",36,0,%d,%p\n", minfo->name, sig->ret->type,
71 minfo->start_line, minfo->method_info.code_start);
74 mono_method_get_param_names (method, (const char **)names);
76 fprintf (debug->f, ".stabs \"this:p(0,%d)=(0,%d)\",160,0,%d,%d\n",
77 debug->next_idx++, klass->byval_arg.type, minfo->start_line,
78 minfo->method_info.this_var->offset);
79 for (i = 0; i < minfo->method_info.num_params; i++) {
80 int stack_offset = minfo->method_info.params [i].offset;
82 fprintf (debug->f, ".stabs \"%s:p(0,%d)=(0,%d)\",160,0,%d,%d\n",
83 names [i], debug->next_idx++, sig->params [i]->type,
84 minfo->start_line, stack_offset);
88 for (i = 0; i < minfo->method_info.num_locals; ++i) {
89 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
90 int stack_offset = minfo->method_info.locals [i].offset;
92 fprintf (debug->f, ".stabs \"local_%d:(0,%d)=(0,%d)\",128,0,%d,%d\n",
93 i, debug->next_idx++, header->locals [i]->type, minfo->start_line, stack_offset);
96 if (minfo->line_numbers) {
97 fprintf (debug->f, ".stabn 68,0,%d,%d\n", minfo->start_line, 0);
98 fprintf (debug->f, ".stabn 68,0,%d,%d\n", minfo->first_line, minfo->method_info.prologue_end);
100 for (i = 1; i < minfo->line_numbers->len; i++) {
101 DebugLineNumberInfo *lni = g_ptr_array_index (minfo->line_numbers, i);
103 fprintf (debug->f, ".stabn 68,0,%d,%d\n", lni->line,
104 (char *)lni->address - minfo->method_info.code_start);
107 fprintf (debug->f, ".stabn 68,0,%d,%d\n", minfo->last_line, minfo->method_info.epilogue_begin);
110 /* end of function */
111 fprintf (debug->f, ".stabs \"\",36,0,0,%d\n", minfo->method_info.code_size);
118 get_enumvalue (MonoClass *klass, int idx, char *buf)
120 guint32 const_cols [MONO_CONSTANT_SIZE];
122 guint32 crow = mono_metadata_get_constant_index (klass->image, MONO_TOKEN_FIELD_DEF | (idx + 1));
129 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
130 ptr = mono_metadata_blob_heap (klass->image, const_cols [MONO_CONSTANT_VALUE]);
131 switch (const_cols [MONO_CONSTANT_TYPE]) {
134 /* FIXME: add other types... */
136 g_snprintf (buf, 64, "%d", *(gint32*)ptr);
141 write_method_func (gpointer key, gpointer value, gpointer user_data)
143 write_method_stabs (user_data, value);
147 write_class_stabs (MonoDebugHandle *debug, MonoClass *klass, int idx)
153 /* output enums ...*/
154 if (klass->enumtype) {
155 name = g_strdup_printf ("%s%s%s", klass->name_space, klass->name_space [0]? "_": "", klass->name);
156 fprintf (debug->f, ".stabs \"%s:T%d=e", name, ++debug->next_idx);
158 for (i = 0; i < klass->field.count; ++i) {
159 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_LITERAL) {
160 get_enumvalue (klass, klass->field.first + i, buf);
161 fprintf (debug->f, "%s_%s=%s,", klass->name, klass->fields [i].name, buf);
164 fprintf (debug->f, ";\",128,0,0,0\n");
170 write_class (gpointer key, gpointer value, gpointer user_data)
172 write_class_stabs (user_data, key, GPOINTER_TO_INT (value));
176 mono_debug_write_stabs (MonoDebugHandle *debug)
181 if (!(debug->f = fopen (debug->filename, "w"))) {
182 g_warning ("Can't create stabs file `%s': %s", debug->filename, g_strerror (errno));
186 source_file = g_ptr_array_index (debug->source_files, 0);
188 fprintf (debug->f, ".stabs \"%s\",100,0,0,0\n", source_file);
190 for (i = 0; base_types [i].name; ++i) {
191 if (! base_types [i].spec)
193 fprintf (debug->f, ".stabs \"%s:t(0,%d)=", base_types [i].name, i);
194 if (base_types [i].spec [0] == ';') {
195 fprintf (debug->f, "r(0,%d)%s\"", i, base_types [i].spec);
197 fprintf (debug->f, "%s\"", base_types [i].spec);
199 fprintf (debug->f, ",128,0,0,0\n");
202 g_hash_table_foreach (debug->methods, write_method_func, debug);
204 g_hash_table_foreach (debug->type_hash, write_class, debug);
209 if (!(debug->flags & MONO_DEBUG_FLAGS_DONT_ASSEMBLE)) {
212 /* yes, it's completely unsafe */
213 buf = g_strdup_printf ("as %s -o %s", debug->filename, debug->objfile);