+ --debug is now --imcc-debug; make this more consistent with -D.
[parrot.git] / src / jit_debug.c
blob43cb37822c9b819fd3e3fdc45a4a1607577e1ed2
1 /*
2 Copyright (C) 2001-2006, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/jit_debug.c - Write stabs file for JIT code
9 =head1 SYNOPSIS
11 When debugging JIT code with C<gdb>, do:
13 add-symbol-file <file.o> 0
15 =head1 DESCRIPTION
17 Stabs is a file format for information that describes a program to a
18 debugger.
20 For more information see the stabs documentation at
21 http://sources.redhat.com/gdb/current/onlinedocs/stabs_toc.html.
23 =head2 Functions
25 =over 4
27 =cut
31 /* HEADERIZER HFILE: none */
32 /* HEADERIZER STOP */
34 #include <parrot/parrot.h>
35 #include "parrot/exec.h"
36 #include "jit.h"
38 #define N_GSYM "32" /* global variable */
39 #define N_FNAME "34"
40 #define N_FUN "36"
41 #define N_STSYM "38" /* variable in data section */
42 #define N_LCSYM "40" /* bss section */
43 #define N_MAIN "42"
44 #define N_ROSYM "44"
45 #define N_PC "48"
46 #define N_NSYMS "50"
47 #define N_NOMAP "52"
48 #define N_OBJ "56"
49 #define N_OPT "60"
50 #define N_RSYM "64" /* register variable */
51 #define N_M2C "66"
52 #define N_SLINE "68"
53 #define N_DSLINE "70"
54 #define N_BSLINE "72"
55 #define N_BROWS "72"
56 #define N_DEFD "74"
57 #define N_FLINE "76"
58 #define N_EHDECL "80"
59 #define N_MOD2 "80"
60 #define N_CATCH "84"
61 #define N_SSYM "96"
62 #define N_ENDM "98"
63 #define N_SO "100" /* filename */
64 #define N_LSYM "128" /* stack variable */
65 #define N_BINCL "130"
66 #define N_SOL "132"
67 #define N_PSYM "160" /* parameter */
68 #define N_EINCL "162"
69 #define N_ENTRY "164"
70 #define N_LBRAC "192"
71 #define N_EXCL "194"
72 #define N_SCOPE "196"
73 #define N_RBRAC "224"
74 #define N_BCOMM "226"
75 #define N_ECOMM "228"
76 #define N_ECOML "232"
77 #define N_WITH "234"
78 #define N_NBTEXT "240"
79 #define N_NBDATA "242"
80 #define N_NBBSS "244"
81 #define N_NBSTS "246"
82 #define N_NBLCS "248"
84 #ifdef __GNUC__
85 void Parrot_jit_debug(PARROT_INTERP);
87 # define BIT_SIZE(t) ((int)(sizeof (t)*8))
88 # define BYTE_SIZE(t) ((int)sizeof (t))
89 # define BIT_OFFSET(str, field) ((int)(offsetof(str, field) * 8))
91 typedef struct BaseTypes {
92 const char *name;
93 const char *spec;
94 } BaseTypes;
98 =item C<static void write_types(FILE *stabs, PARROT_INTERP)>
100 Writes the types to C<stabs>.
102 =cut
106 static void
107 write_types(FILE *stabs, PARROT_INTERP)
109 int i, j;
110 /* borrowed from mono */
111 static BaseTypes base_types[] = {
112 {"Void", "(0,1)"},
113 {"Char", ";-128;127;"},
114 {"Byte", ";0;255;"},
115 {"Int16", ";-32768;32767;"},
116 {"UInt16", ";0;65535;"},
117 {"Int32", ";0020000000000;0017777777777;"}, /* 5 */
118 {"UInt32", ";0000000000000;0037777777777;"},
119 {"Int64", ";01000000000000000000000;0777777777777777777777;"},
120 {"UInt64", ";0000000000000;01777777777777777777777;"},
121 {"Single", "r(0,8);4;0;"},
122 {"Double", "r(0,8);8;0;"}, /* 10 */
123 {"LongDouble", "r(0,8);12;0;"},
124 # if INTVAL_SIZE == 4
125 {"INTVAL", "(0,5);"}, /* 12 */
126 # else
127 {"INTVAL", "(0,7);"},
128 # endif
129 # if NUMVAL_SIZE == 8
130 {"FLOATVAL", "(0,10);"}, /* 13 */
131 # else
132 {"FLOATVAL", "(0,11);"},
133 # endif
134 {"Ptr", "*(0,0);"},
135 {"CharPtr", "*(0,1);"}, /* 15 */
136 {0, 0}
138 for (i = 0; base_types[i].name; ++i) {
139 if (! base_types[i].spec)
140 continue;
141 fprintf(stabs, ".stabs \"%s:t(0,%d)=", base_types[i].name, i);
142 if (base_types[i].spec [0] == ';') {
143 fprintf(stabs, "r(0,%d)%s\"", i, base_types[i].spec);
145 else {
146 fprintf(stabs, "%s\"", base_types[i].spec);
148 fprintf(stabs, "," N_LSYM ",0,0,0\n");
150 fprintf(stabs, ".stabs \"STRING:t(0,%d)=*(0,%d)\""
151 "," N_LSYM ",0,0,0\n", i, i+1);
152 ++i;
153 fprintf(stabs, ".stabs \"Parrot_String:T(0,%d)=s%d"
154 "bufstart:(0,14),%d,%d;"
155 "buflen:(0,6),%d,%d;" /* XXX type */
156 "flags:(0,12),%d,%d;"
157 "bufused:(0,12),%d,%d;"
158 "strstart:(0,15),%d,%d;" /* fake a char* */
159 ";\""
160 "," N_LSYM ",0,0,0\n", i++, BYTE_SIZE(STRING),
161 BIT_OFFSET(STRING, obj.u._b._bufstart), BIT_SIZE(void*),
162 BIT_OFFSET(STRING, obj.u._b._buflen), BIT_SIZE(size_t),
163 BIT_OFFSET(STRING, obj.flags), BIT_SIZE(UINTVAL),
164 BIT_OFFSET(STRING, bufused), BIT_SIZE(UINTVAL),
165 BIT_OFFSET(STRING, strstart), BIT_SIZE(void*));
167 fprintf(stabs, ".stabs \"PMCType:T(0,%d)=e", i++);
168 for (j = 0; j < interp->n_vtable_max; ++j) {
169 if (interp->vtables[j] && interp->vtables[j]->whoami) {
170 STRING* name = interp->vtables[j]->whoami;
171 fwrite(name->strstart, name->strlen, 1, stabs);
172 fprintf(stabs, ":%d,", j);
175 fprintf(stabs, ";\"," N_LSYM ",0,0,0\n");
177 /* PMC type */
178 fprintf(stabs, ".stabs \"PMC:T(0,%d)=s%d", i, BYTE_SIZE(PMC));
179 fprintf(stabs, "obj:(0,%d),%d,%d;",
180 i + 1, BIT_OFFSET(PMC, obj), BIT_SIZE(pobj_t));
181 fprintf(stabs, "vtable:*(0,%d),%d,%d;",
182 i + 3, BIT_OFFSET(PMC, vtable), BIT_SIZE(void*));
183 # if ! PMC_DATA_IN_EXT
184 fprintf(stabs, "data:(0,14),%d,%d;",
185 BIT_OFFSET(PMC, data), BIT_SIZE(void*));
186 # endif
187 fprintf(stabs, "pmc_ext:*(0,%d),%d,%d;",
188 i, BIT_OFFSET(PMC, pmc_ext), BIT_SIZE(void*));
189 fprintf(stabs, ";\"");
190 fprintf(stabs, "," N_LSYM ",0,0,0\n");
192 fprintf(stabs, ".stabs \"pobj_t:T(0,%d)=s%d"
193 "u:(0,%d),%d,%d;"
194 "flags:(0,12),%d,%d;"
195 ";\""
196 "," N_LSYM ",0,0,0\n", i + 1, (int)(sizeof (pobj_t)),
197 i + 2, BIT_OFFSET(pobj_t, u), BIT_SIZE(UnionVal),
198 BIT_OFFSET(pobj_t, flags), BIT_SIZE(Parrot_UInt));
199 fprintf(stabs, ".stabs \"UnionVal:T(0,%d)=u%d"
200 "int_val:(0,12),%d,%d;"
201 "pmc_val:*(0,%d),%d,%d;"
202 ";\""
203 "," N_LSYM ",0,0,0\n", i + 2, BYTE_SIZE(UnionVal),
204 BIT_OFFSET(UnionVal, _i._int_val), BIT_SIZE(INTVAL),
205 i, BIT_OFFSET(UnionVal, _ptrs._pmc_val), BIT_SIZE(void*));
206 fprintf(stabs, ".stabs \"VTABLE:T(0,%d)=s%d"
207 "base_type:(0,%d),%d,%d;"
208 ";\""
209 "," N_LSYM ",0,0,0\n", i + 3, BYTE_SIZE(UnionVal),
210 i - 1, BIT_OFFSET(VTABLE, base_type), BIT_SIZE(INTVAL));
211 i += 4;
217 =item C<static void
218 write_vars(FILE *stabs, PARROT_INTERP)>
220 Writes the contents of the registers to C<stabs>.
222 =cut
226 static void
227 write_vars(FILE *stabs, PARROT_INTERP)
229 int i;
230 /* fake static var stabs */
231 for (i = 0; i < NUM_REGISTERS; i++) {
232 fprintf(stabs, ".stabs \"I%d:S(0,12)\"," N_STSYM ",0,0,%p\n", i,
233 (char*)&REG_INT(interp, i));
234 fprintf(stabs, ".stabs \"N%d:S(0,13)\"," N_STSYM ",0,0,%p\n", i,
235 (char*)&REG_NUM(interp, i));
236 fprintf(stabs, ".stabs \"S%d:S(0,16)\"," N_STSYM ",0,0,%p\n", i,
237 (char*)&REG_STR(interp, i));
238 fprintf(stabs, ".stabs \"P%d:S*(0,19)\"," N_STSYM ",0,0,%p\n", i,
239 (char*)&REG_PMC(interp, i));
245 =item C<static STRING *
246 debug_file(PARROT_INTERP, STRING *file, const char *ext)>
248 Returns C<file> with C<ext> appended.
250 =cut
254 static STRING *
255 debug_file(PARROT_INTERP, STRING *file, const char *ext)
257 STRING *ret;
258 ret = string_copy(interp, file);
259 ret = string_append(interp, ret,
260 string_make(interp, ext, strlen(ext), NULL,
261 PObj_external_FLAG));
262 return ret;
267 =item C<static void
268 Parrot_jit_debug_stabs(PARROT_INTERP)>
270 Writes the JIT debugging stabs.
272 =cut
276 static void
277 Parrot_jit_debug_stabs(PARROT_INTERP)
279 Parrot_jit_info_t *jit_info = interp->code->jit_info;
280 STRING *file = NULL;
281 STRING *pasmfile, *stabsfile, *ofile, *cmd;
282 FILE *stabs;
283 size_t i;
284 int line;
285 opcode_t lc;
286 PackFile_Debug *debug;
288 if (interp->code->debugs) {
289 char *ext;
290 char *src = string_to_cstring(interp,
291 Parrot_debug_pc_to_filename(interp,
292 interp->code->debugs, 0));
293 pasmfile = string_make(interp, src, strlen(src), NULL,
294 PObj_external_FLAG);
295 file = string_copy(interp, pasmfile);
296 /* chop pasm/pir */
298 ext = strrchr(src, '.');
299 if (ext && strcmp(ext, ".pasm") == 0)
300 string_chopn_inplace(interp, file, 4);
301 else if (ext && strcmp(ext, ".pir") == 0)
302 string_chopn_inplace(interp, file, 3);
303 else if (!ext) /* EVAL_n */
304 file = string_append(interp, file,
305 string_make(interp, ".", 1, NULL, PObj_external_FLAG));
307 string_cstring_free(src);
309 else {
310 /* chop pbc */
311 string_chopn_inplace(interp, file, 3);
312 pasmfile = debug_file(interp, file, "pasm");
314 stabsfile = debug_file(interp, file, "stabs.s");
315 ofile = debug_file(interp, file, "o");
317 char *temp = string_to_cstring(interp, stabsfile);
318 stabs = fopen(temp, "w");
319 string_cstring_free(temp);
321 if (stabs == NULL)
322 return;
325 char *temp = string_to_cstring(interp, pasmfile);
326 /* filename info */
327 fprintf(stabs, ".data\n.text\n"); /* darwin wants it */
328 fprintf(stabs, ".stabs \"%s\"," N_SO ",0,0,0\n", temp);
329 string_cstring_free(temp);
331 /* jit_func start addr */
332 fprintf(stabs, ".stabs \"jit_func:F(0,1)\"," N_FUN ",0,1,%p\n",
333 jit_info->arena.start);
335 write_types(stabs, interp);
336 write_vars(stabs, interp);
337 /* if we don't have line numbers, emit dummys, assuming there are
338 * no comments and spaces in source for testing
341 /* jit_begin */
342 fprintf(stabs, ".stabn " N_SLINE ",0,1,0\n");
343 line = 1;
344 lc = 0;
345 debug = interp->code->debugs;
346 for (i = 0; i < interp->code->base.size; i++) {
347 if (jit_info->arena.op_map[i].ptr) {
348 op_info_t* op = &interp->op_info_table[
349 interp->code->base.data[i]];
350 if (interp->code->debugs) {
351 if (lc >= (int)(debug->base.size))
352 break;
353 line = (int)debug->base.data[lc++];
355 fprintf(stabs, ".stabn " N_SLINE ",0,%d,%d /* %s */\n", line,
356 (int)((char *)jit_info->arena.op_map[i].ptr -
357 (char *)jit_info->arena.start),
358 op->full_name);
359 line++;
362 /* eof */
363 fprintf(stabs, ".stabs \"\"," N_FUN ",0,1,%p\n",
364 (char *) jit_info->arena.size);
365 fclose(stabs);
366 /* run the stabs file through C<as> generating file.o */
367 cmd = Parrot_sprintf_c(interp, "as %Ss -o %Ss", stabsfile, ofile);
370 char *temp = string_to_cstring(interp, cmd);
371 system(temp);
372 string_cstring_free(temp);
378 =item C<void
379 Parrot_jit_debug(PARROT_INTERP)>
381 Writes the JIT debugging stabs. Just calls C<Parrot_jit_debug_stabs()>.
383 =cut
387 void
388 Parrot_jit_debug(PARROT_INTERP)
390 Parrot_jit_debug_stabs(interp);
394 #endif
398 =back
400 =head1 SEE ALSO
402 F<src/jit.c>, F<include/parrot/jit.h>, F<docs/jit.pod>.
404 =cut
410 * Local variables:
411 * c-file-style: "parrot"
412 * End:
413 * vim: expandtab shiftwidth=4: