2 * simple backtrace facility
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
22 #include "interrupt.h"
24 #ifdef LISP_FEATURE_GENCGC
26 #include "gencgc-alloc-region.h"
27 #include "genesis/compiled-debug-fun.h"
28 #include "genesis/compiled-debug-info.h"
29 #include "genesis/package.h"
31 #include "genesis/static-symbols.h"
32 #include "genesis/primitive-objects.h"
35 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
36 /* __USE_GNU needed if we want dladdr() and Dl_Info from glibc. */
41 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
43 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
44 * better not change. */
47 #ifndef LISP_FEATURE_ALPHA
48 struct call_frame *old_cont;
54 lispobj other_state[5];
58 #ifndef LISP_FEATURE_ALPHA
59 struct call_frame *frame;
64 #ifndef LISP_FEATURE_ALPHA
70 int pc; /* Note: this is the trace file offset, not the actual pc. */
73 #define HEADER_LENGTH(header) ((header)>>8)
75 static int previous_info(struct call_info *info);
78 code_pointer(lispobj object)
80 lispobj *headerp, header;
83 headerp = (lispobj *) native_pointer(object);
85 type = widetag_of(header);
88 case CODE_HEADER_WIDETAG:
90 case RETURN_PC_HEADER_WIDETAG:
91 case SIMPLE_FUN_HEADER_WIDETAG:
92 len = HEADER_LENGTH(header);
102 return (struct code *) headerp;
106 cs_valid_pointer_p(struct call_frame *pointer)
108 struct thread *thread=arch_os_get_current_thread();
109 return (((char *) thread->control_stack_start <= (char *) pointer) &&
110 ((char *) pointer < (char *) current_control_stack_pointer));
114 call_info_from_lisp_state(struct call_info *info)
116 info->frame = (struct call_frame *)current_control_frame_pointer;
117 info->interrupted = 0;
126 call_info_from_context(struct call_info *info, os_context_t *context)
130 info->interrupted = 1;
131 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
132 == FUN_POINTER_LOWTAG) {
133 /* We tried to call a function, but crapped out before $CODE could
134 * be fixed up. Probably an undefined function. */
136 (struct call_frame *)(*os_context_register_addr(context,
138 info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
139 info->code = code_pointer(info->lra);
140 pc = (unsigned long)native_pointer(info->lra);
144 (struct call_frame *)(*os_context_register_addr(context, reg_CFP));
146 code_pointer(*os_context_register_addr(context, reg_CODE));
148 pc = *os_context_pc_addr(context);
150 if (info->code != NULL)
151 info->pc = pc - (unsigned long) info->code -
152 #ifndef LISP_FEATURE_ALPHA
153 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
155 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
162 previous_info(struct call_info *info)
164 struct call_frame *this_frame;
165 struct thread *thread=arch_os_get_current_thread();
168 if (!cs_valid_pointer_p(info->frame)) {
169 printf("Bogus callee value (0x%08lx).\n", (unsigned long)info->frame);
173 this_frame = info->frame;
174 info->lra = this_frame->saved_lra;
175 info->frame = this_frame->old_cont;
176 info->interrupted = 0;
178 if (info->frame == NULL || info->frame == this_frame)
181 if (info->lra == NIL) {
182 /* We were interrupted. Find the correct signal context. */
183 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)>>2;
185 os_context_t *context =
186 thread->interrupt_contexts[free];
187 if ((struct call_frame *)(*os_context_register_addr(context,
190 call_info_from_context(info, context);
196 info->code = code_pointer(info->lra);
197 if (info->code != NULL)
198 info->pc = (unsigned long)native_pointer(info->lra) -
199 (unsigned long)info->code -
200 #ifndef LISP_FEATURE_ALPHA
201 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
203 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
213 backtrace(int nframes)
215 struct call_info info;
217 call_info_from_lisp_state(&info);
220 printf("<Frame 0x%08lx%s, ", (unsigned long) info.frame,
221 info.interrupted ? " [interrupted]" : "");
223 if (info.code != (struct code *) 0) {
226 printf("CODE: 0x%08lX, ", (unsigned long) info.code | OTHER_POINTER_LOWTAG);
228 #ifndef LISP_FEATURE_ALPHA
229 function = info.code->entry_points;
231 function = ((struct code *)info.code)->entry_points;
233 while (function != NIL) {
234 struct simple_fun *header;
237 header = (struct simple_fun *) native_pointer(function);
240 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
243 object = (lispobj *) native_pointer(name);
245 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
246 struct symbol *symbol;
248 symbol = (struct symbol *) object;
249 object = (lispobj *) native_pointer(symbol->name);
251 if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
252 struct vector *string;
254 string = (struct vector *) object;
255 printf("%s, ", (char *) string->data);
257 /* FIXME: broken from (VECTOR NIL) */
258 printf("(Not simple string??\?), ");
260 printf("(Not other pointer??\?), ");
263 function = header->next;
267 printf("CODE: ???, ");
270 printf("LRA: 0x%08lx, ", (unsigned long)info.lra);
272 printf("<no LRA>, ");
275 printf("PC: 0x%x>\n", info.pc);
277 printf("PC: ??\?>\n");
279 } while (--nframes > 0 && previous_info(&info));
285 stack_pointer_p (void *p)
287 return (p < (void *) arch_os_get_current_thread()->control_stack_end
289 && (((unsigned long) p) & 3) == 0);
293 ra_pointer_p (void *ra)
295 return ((unsigned long) ra) > 4096 && !stack_pointer_p (ra);
299 x86_call_context (void *fp, void **ra, void **ocfp)
305 int lisp_valid_p, c_valid_p;
307 if (!stack_pointer_p(fp))
310 c_ocfp = *((void **) fp);
311 c_ra = *((void **) fp + 1);
312 lisp_ocfp = *((void **) fp - 1);
313 lisp_ra = *((void **) fp - 2);
315 lisp_valid_p = (lisp_ocfp > fp
316 && stack_pointer_p(lisp_ocfp)
317 && ra_pointer_p(lisp_ra));
318 c_valid_p = (c_ocfp > fp
319 && stack_pointer_p(c_ocfp)
320 && ra_pointer_p(c_ra));
322 if (lisp_valid_p && c_valid_p) {
327 int lisp_path_p = x86_call_context(lisp_ocfp, &lisp_path_fp, &dummy);
328 int c_path_p = x86_call_context(c_ocfp, &c_path_fp, &dummy);
330 if (lisp_path_p && c_path_p) {
331 #if defined __FreeBSD__ && __FreeBSD_version > 400000
332 if (lisp_ocfp > c_ocfp)
333 *ra = lisp_ra, *ocfp = lisp_ocfp;
335 *ra = c_ra, *ocfp = c_ocfp;
337 *ra = lisp_ra, *ocfp = lisp_ocfp;
340 else if (lisp_path_p)
341 *ra = lisp_ra, *ocfp = lisp_ocfp;
343 *ra = c_ra, *ocfp = c_ocfp;
347 else if (lisp_valid_p)
348 *ra = lisp_ra, *ocfp = lisp_ocfp;
350 *ra = c_ra, *ocfp = c_ocfp;
357 struct compiled_debug_fun *
358 debug_function_from_pc (struct code* code, void *pc)
360 unsigned long code_header_len = sizeof(lispobj) * HeaderValue(code->header);
362 = (unsigned long) pc - (unsigned long) code - code_header_len;
363 struct compiled_debug_fun *df;
364 struct compiled_debug_info *di;
368 if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
371 di = (struct compiled_debug_info *) native_pointer(code->debug_info);
372 v = (struct vector *) native_pointer(di->fun_map);
373 len = fixnum_value(v->length);
374 df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
379 for (i = 1;; i += 2) {
383 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
385 if (offset >= fixnum_value(df->elsewhere_pc)) {
386 struct compiled_debug_fun *p
387 = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
388 next_pc = fixnum_value(p->elsewhere_pc);
390 next_pc = fixnum_value(v->data[i]);
392 if (offset < next_pc)
393 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
400 print_entry_name (lispobj name)
402 if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
404 while (name != NIL) {
405 struct cons *cons = (struct cons *) native_pointer(name);
406 print_entry_name(cons->car);
412 } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
413 lispobj *object = (lispobj *) native_pointer(name);
415 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
416 struct symbol *symbol = (struct symbol *) object;
417 struct vector *string;
419 if (symbol->package != NIL) {
421 = (struct package *) native_pointer(symbol->package);
422 lispobj pkg_name = pkg->_name;
423 string = (struct vector *) native_pointer(pkg_name);
424 printf("%s::", (char *) string->data);
427 object = (lispobj *) native_pointer(symbol->name);
428 string = (struct vector *) object;
429 printf("%s", (char *) string->data);
430 } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
431 struct vector *string = (struct vector *) object;
432 printf("\"%s\"", (char *) string->data);
433 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
434 } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
435 printf("<oops, a unicode string>"); /* FIXME */
438 printf("<??? type %d>", (int) widetag_of(*object));
440 printf("<??? lowtag %d>", (int) lowtag_of(name));
444 print_entry_points (struct code *code)
446 lispobj function = code->entry_points;
448 while (function != NIL) {
449 struct simple_fun *header = (struct simple_fun *) native_pointer(function);
450 print_entry_name(header->name);
452 function = header->next;
459 backtrace(int nframes)
464 #if defined(LISP_FEATURE_X86)
465 asm("movl %%ebp,%0" : "=g" (fp));
466 #elif defined (LISP_FEATURE_X86_64)
467 asm("movq %%rbp,%0" : "=g" (fp));
469 #error "How did we get here?"
472 for (i = 0; i < nframes; ++i) {
477 if (!x86_call_context(fp, &ra, &next_fp))
482 p = (lispobj *) component_ptr_from_pc((lispobj *) ra);
484 struct code *cp = (struct code *) p;
485 struct compiled_debug_fun *df = debug_function_from_pc(cp, ra);
487 print_entry_name(df->name);
489 print_entry_points(cp);
491 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
493 if (dladdr(ra, &info)) {
494 printf("Foreign function %s, fp = 0x%lx, ra = 0x%lx",
496 (unsigned long) next_fp,
500 printf("Foreign fp = 0x%lx, ra = 0x%lx",
501 (unsigned long) next_fp,