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 *)(unsigned long)
137 (*os_context_register_addr(context, reg_OCFP));
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 *)(unsigned long)
145 (*os_context_register_addr(context, reg_CFP));
147 code_pointer(*os_context_register_addr(context, reg_CODE));
149 pc = *os_context_pc_addr(context);
151 if (info->code != NULL)
152 info->pc = pc - (unsigned long) info->code -
153 #ifndef LISP_FEATURE_ALPHA
154 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
156 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
163 previous_info(struct call_info *info)
165 struct call_frame *this_frame;
166 struct thread *thread=arch_os_get_current_thread();
169 if (!cs_valid_pointer_p(info->frame)) {
170 printf("Bogus callee value (0x%08lx).\n", (unsigned long)info->frame);
174 this_frame = info->frame;
175 info->lra = this_frame->saved_lra;
176 info->frame = this_frame->old_cont;
177 info->interrupted = 0;
179 if (info->frame == NULL || info->frame == this_frame)
182 if (info->lra == NIL) {
183 /* We were interrupted. Find the correct signal context. */
184 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)>>2;
186 os_context_t *context =
187 thread->interrupt_contexts[free];
188 if ((struct call_frame *)(unsigned long)
189 (*os_context_register_addr(context, reg_CFP))
191 call_info_from_context(info, context);
197 info->code = code_pointer(info->lra);
198 if (info->code != NULL)
199 info->pc = (unsigned long)native_pointer(info->lra) -
200 (unsigned long)info->code -
201 #ifndef LISP_FEATURE_ALPHA
202 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
204 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
214 backtrace(int nframes)
216 struct call_info info;
218 call_info_from_lisp_state(&info);
221 printf("<Frame 0x%08lx%s, ", (unsigned long) info.frame,
222 info.interrupted ? " [interrupted]" : "");
224 if (info.code != (struct code *) 0) {
227 printf("CODE: 0x%08lX, ", (unsigned long) info.code | OTHER_POINTER_LOWTAG);
229 #ifndef LISP_FEATURE_ALPHA
230 function = info.code->entry_points;
232 function = ((struct code *)info.code)->entry_points;
234 while (function != NIL) {
235 struct simple_fun *header;
238 header = (struct simple_fun *) native_pointer(function);
241 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
244 object = (lispobj *) native_pointer(name);
246 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
247 struct symbol *symbol;
249 symbol = (struct symbol *) object;
250 object = (lispobj *) native_pointer(symbol->name);
252 if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
253 struct vector *string;
255 string = (struct vector *) object;
256 printf("%s, ", (char *) string->data);
258 /* FIXME: broken from (VECTOR NIL) */
259 printf("(Not simple string??\?), ");
261 printf("(Not other pointer??\?), ");
264 function = header->next;
268 printf("CODE: ???, ");
271 printf("LRA: 0x%08lx, ", (unsigned long)info.lra);
273 printf("<no LRA>, ");
276 printf("PC: 0x%x>\n", info.pc);
278 printf("PC: ??\?>\n");
280 } while (--nframes > 0 && previous_info(&info));
286 stack_pointer_p (void *p)
288 return (p < (void *) arch_os_get_current_thread()->control_stack_end
290 && (((unsigned long) p) & 3) == 0);
294 ra_pointer_p (void *ra)
296 return ((unsigned long) ra) > 4096 && !stack_pointer_p (ra);
300 x86_call_context (void *fp, void **ra, void **ocfp)
306 int lisp_valid_p, c_valid_p;
308 if (!stack_pointer_p(fp))
311 c_ocfp = *((void **) fp);
312 c_ra = *((void **) fp + 1);
313 lisp_ocfp = *((void **) fp - 1);
314 lisp_ra = *((void **) fp - 2);
316 lisp_valid_p = (lisp_ocfp > fp
317 && stack_pointer_p(lisp_ocfp)
318 && ra_pointer_p(lisp_ra));
319 c_valid_p = (c_ocfp > fp
320 && stack_pointer_p(c_ocfp)
321 && ra_pointer_p(c_ra));
323 if (lisp_valid_p && c_valid_p) {
328 int lisp_path_p = x86_call_context(lisp_ocfp, &lisp_path_fp, &dummy);
329 int c_path_p = x86_call_context(c_ocfp, &c_path_fp, &dummy);
331 if (lisp_path_p && c_path_p) {
332 #if defined __FreeBSD__ && __FreeBSD_version > 400000
333 if (lisp_ocfp > c_ocfp)
334 *ra = lisp_ra, *ocfp = lisp_ocfp;
336 *ra = c_ra, *ocfp = c_ocfp;
338 *ra = lisp_ra, *ocfp = lisp_ocfp;
341 else if (lisp_path_p)
342 *ra = lisp_ra, *ocfp = lisp_ocfp;
344 *ra = c_ra, *ocfp = c_ocfp;
348 else if (lisp_valid_p)
349 *ra = lisp_ra, *ocfp = lisp_ocfp;
351 *ra = c_ra, *ocfp = c_ocfp;
358 struct compiled_debug_fun *
359 debug_function_from_pc (struct code* code, void *pc)
361 unsigned long code_header_len = sizeof(lispobj) * HeaderValue(code->header);
363 = (unsigned long) pc - (unsigned long) code - code_header_len;
364 struct compiled_debug_fun *df;
365 struct compiled_debug_info *di;
369 if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
372 di = (struct compiled_debug_info *) native_pointer(code->debug_info);
373 v = (struct vector *) native_pointer(di->fun_map);
374 len = fixnum_value(v->length);
375 df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
380 for (i = 1;; i += 2) {
384 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
386 if (offset >= fixnum_value(df->elsewhere_pc)) {
387 struct compiled_debug_fun *p
388 = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
389 next_pc = fixnum_value(p->elsewhere_pc);
391 next_pc = fixnum_value(v->data[i]);
393 if (offset < next_pc)
394 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
401 print_entry_name (lispobj name)
403 if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
405 while (name != NIL) {
406 struct cons *cons = (struct cons *) native_pointer(name);
407 print_entry_name(cons->car);
413 } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
414 lispobj *object = (lispobj *) native_pointer(name);
416 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
417 struct symbol *symbol = (struct symbol *) object;
418 struct vector *string;
420 if (symbol->package != NIL) {
422 = (struct package *) native_pointer(symbol->package);
423 lispobj pkg_name = pkg->_name;
424 string = (struct vector *) native_pointer(pkg_name);
425 printf("%s::", (char *) string->data);
428 object = (lispobj *) native_pointer(symbol->name);
429 string = (struct vector *) object;
430 printf("%s", (char *) string->data);
431 } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
432 struct vector *string = (struct vector *) object;
433 printf("\"%s\"", (char *) string->data);
434 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
435 } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
436 printf("<oops, a unicode string>"); /* FIXME */
439 printf("<??? type %d>", (int) widetag_of(*object));
441 printf("<??? lowtag %d>", (int) lowtag_of(name));
445 print_entry_points (struct code *code)
447 lispobj function = code->entry_points;
449 while (function != NIL) {
450 struct simple_fun *header = (struct simple_fun *) native_pointer(function);
451 print_entry_name(header->name);
453 function = header->next;
460 backtrace(int nframes)
465 #if defined(LISP_FEATURE_X86)
466 asm("movl %%ebp,%0" : "=g" (fp));
467 #elif defined (LISP_FEATURE_X86_64)
468 asm("movq %%rbp,%0" : "=g" (fp));
470 #error "How did we get here?"
473 for (i = 0; i < nframes; ++i) {
478 if (!x86_call_context(fp, &ra, &next_fp))
483 p = (lispobj *) component_ptr_from_pc((lispobj *) ra);
485 struct code *cp = (struct code *) p;
486 struct compiled_debug_fun *df = debug_function_from_pc(cp, ra);
488 print_entry_name(df->name);
490 print_entry_points(cp);
492 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
494 if (dladdr(ra, &info)) {
495 printf("Foreign function %s, fp = 0x%lx, ra = 0x%lx",
497 (unsigned long) next_fp,
501 printf("Foreign fp = 0x%lx, ra = 0x%lx",
502 (unsigned long) next_fp,