2 Copyright (C) 2001-2006, The Perl Foundation.
7 src/jit_debug.c - Write stabs file for JIT code
11 When debugging JIT code with C<gdb>, do:
13 add-symbol-file <file.o> 0
17 Stabs is a file format for information that describes a program to a
20 For more information see the stabs documentation at
21 http://sources.redhat.com/gdb/current/onlinedocs/stabs_toc.html.
31 /* HEADERIZER HFILE: none */
34 #include <parrot/parrot.h>
35 #include "parrot/exec.h"
38 #define N_GSYM "32" /* global variable */
41 #define N_STSYM "38" /* variable in data section */
42 #define N_LCSYM "40" /* bss section */
50 #define N_RSYM "64" /* register variable */
63 #define N_SO "100" /* filename */
64 #define N_LSYM "128" /* stack variable */
67 #define N_PSYM "160" /* parameter */
78 #define N_NBTEXT "240"
79 #define N_NBDATA "242"
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
{
98 =item C<static void write_types(FILE *stabs, PARROT_INTERP)>
100 Writes the types to C<stabs>.
107 write_types(FILE *stabs
, PARROT_INTERP
)
110 /* borrowed from mono */
111 static BaseTypes base_types
[] = {
113 {"Char", ";-128;127;"},
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 */
127 {"INTVAL", "(0,7);"},
129 # if NUMVAL_SIZE == 8
130 {"FLOATVAL", "(0,10);"}, /* 13 */
132 {"FLOATVAL", "(0,11);"},
135 {"CharPtr", "*(0,1);"}, /* 15 */
138 for (i
= 0; base_types
[i
].name
; ++i
) {
139 if (! base_types
[i
].spec
)
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
);
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);
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* */
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");
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*));
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"
194 "flags:(0,12),%d,%d;"
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;"
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;"
209 "," N_LSYM
",0,0,0\n", i
+ 3, BYTE_SIZE(UnionVal
),
210 i
- 1, BIT_OFFSET(VTABLE
, base_type
), BIT_SIZE(INTVAL
));
218 write_vars(FILE *stabs, PARROT_INTERP)>
220 Writes the contents of the registers to C<stabs>.
227 write_vars(FILE *stabs
, PARROT_INTERP
)
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*)®_INT(interp
, i
));
234 fprintf(stabs
, ".stabs \"N%d:S(0,13)\"," N_STSYM
",0,0,%p\n", i
,
235 (char*)®_NUM(interp
, i
));
236 fprintf(stabs
, ".stabs \"S%d:S(0,16)\"," N_STSYM
",0,0,%p\n", i
,
237 (char*)®_STR(interp
, i
));
238 fprintf(stabs
, ".stabs \"P%d:S*(0,19)\"," N_STSYM
",0,0,%p\n", i
,
239 (char*)®_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.
255 debug_file(PARROT_INTERP
, STRING
*file
, const char *ext
)
258 ret
= string_copy(interp
, file
);
259 ret
= string_append(interp
, ret
,
260 string_make(interp
, ext
, strlen(ext
), NULL
,
261 PObj_external_FLAG
));
268 Parrot_jit_debug_stabs(PARROT_INTERP)>
270 Writes the JIT debugging stabs.
277 Parrot_jit_debug_stabs(PARROT_INTERP
)
279 Parrot_jit_info_t
*jit_info
= interp
->code
->jit_info
;
281 STRING
*pasmfile
, *stabsfile
, *ofile
, *cmd
;
286 PackFile_Debug
*debug
;
288 if (interp
->code
->debugs
) {
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
,
295 file
= string_copy(interp
, pasmfile
);
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
);
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
);
325 char *temp
= string_to_cstring(interp
, pasmfile
);
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
342 fprintf(stabs
, ".stabn " N_SLINE
",0,1,0\n");
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
))
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
),
363 fprintf(stabs
, ".stabs \"\"," N_FUN
",0,1,%p\n",
364 (char *) jit_info
->arena
.size
);
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
);
372 string_cstring_free(temp
);
379 Parrot_jit_debug(PARROT_INTERP)>
381 Writes the JIT debugging stabs. Just calls C<Parrot_jit_debug_stabs()>.
388 Parrot_jit_debug(PARROT_INTERP
)
390 Parrot_jit_debug_stabs(interp
);
402 F<src/jit.c>, F<include/parrot/jit.h>, F<docs/jit.pod>.
411 * c-file-style: "parrot"
413 * vim: expandtab shiftwidth=4: