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.
16 /* needed if we want dladdr() and Dl_Info from glibc's dlfcn.h */
25 #include "interrupt.h"
27 #ifdef LISP_FEATURE_GENCGC
30 #include "gencgc-alloc-region.h"
31 #include "genesis/compiled-debug-fun.h"
32 #include "genesis/compiled-debug-info.h"
33 #include "genesis/package.h"
35 #include "genesis/static-symbols.h"
36 #include "genesis/primitive-objects.h"
39 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
43 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
45 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
46 * better not change. */
49 #ifndef LISP_FEATURE_ALPHA
50 struct call_frame *old_cont;
56 lispobj other_state[5];
60 #ifndef LISP_FEATURE_ALPHA
61 struct call_frame *frame;
66 #ifndef LISP_FEATURE_ALPHA
72 int pc; /* Note: this is the trace file offset, not the actual pc. */
75 #define HEADER_LENGTH(header) ((header)>>8)
77 static int previous_info(struct call_info *info);
80 code_pointer(lispobj object)
82 lispobj *headerp, header;
85 headerp = (lispobj *) native_pointer(object);
87 type = widetag_of(header);
90 case CODE_HEADER_WIDETAG:
92 case RETURN_PC_HEADER_WIDETAG:
93 case SIMPLE_FUN_HEADER_WIDETAG:
94 len = HEADER_LENGTH(header);
104 return (struct code *) headerp;
108 cs_valid_pointer_p(struct call_frame *pointer)
110 struct thread *thread=arch_os_get_current_thread();
111 return (((char *) thread->control_stack_start <= (char *) pointer) &&
112 ((char *) pointer < (char *) access_control_stack_pointer(thread)));
116 call_info_from_lisp_state(struct call_info *info)
118 info->frame = (struct call_frame *)access_control_frame_pointer(arch_os_get_current_thread());
119 info->interrupted = 0;
128 call_info_from_context(struct call_info *info, os_context_t *context)
132 info->interrupted = 1;
133 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
134 == FUN_POINTER_LOWTAG) {
135 /* We tried to call a function, but crapped out before $CODE could
136 * be fixed up. Probably an undefined function. */
138 (struct call_frame *)(uword_t)
139 (*os_context_register_addr(context, reg_OCFP));
140 info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
141 info->code = code_pointer(info->lra);
142 pc = (uword_t)native_pointer(info->lra);
146 (struct call_frame *)(uword_t)
147 (*os_context_register_addr(context, reg_CFP));
149 code_pointer(*os_context_register_addr(context, reg_CODE));
151 pc = *os_context_pc_addr(context);
153 if (info->code != NULL)
154 info->pc = pc - (uword_t) info->code -
155 #ifndef LISP_FEATURE_ALPHA
156 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
158 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
165 previous_info(struct call_info *info)
167 struct call_frame *this_frame;
168 struct thread *thread=arch_os_get_current_thread();
171 if (!cs_valid_pointer_p(info->frame)) {
172 printf("Bogus callee value (0x%08lx).\n", (uword_t)info->frame);
176 this_frame = info->frame;
177 info->lra = this_frame->saved_lra;
178 info->frame = this_frame->old_cont;
179 info->interrupted = 0;
181 if (info->frame == NULL || info->frame == this_frame)
184 if (info->lra == NIL) {
185 /* We were interrupted. Find the correct signal context. */
186 free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
187 while (free_ici-- > 0) {
188 os_context_t *context =
189 thread->interrupt_contexts[free_ici];
190 if ((struct call_frame *)(uword_t)
191 (*os_context_register_addr(context, reg_CFP))
193 call_info_from_context(info, context);
199 info->code = code_pointer(info->lra);
200 if (info->code != NULL)
201 info->pc = (uword_t)native_pointer(info->lra) -
202 (uword_t)info->code -
203 #ifndef LISP_FEATURE_ALPHA
204 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
206 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
216 lisp_backtrace(int nframes)
218 struct call_info info;
220 call_info_from_lisp_state(&info);
223 printf("<Frame 0x%08lx%s, ", (uword_t) info.frame,
224 info.interrupted ? " [interrupted]" : "");
226 if (info.code != (struct code *) 0) {
229 printf("CODE: 0x%08lX, ", (uword_t) info.code | OTHER_POINTER_LOWTAG);
231 #ifndef LISP_FEATURE_ALPHA
232 function = info.code->entry_points;
234 function = ((struct code *)info.code)->entry_points;
236 while (function != NIL) {
237 struct simple_fun *header;
240 header = (struct simple_fun *) native_pointer(function);
243 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
246 object = (lispobj *) native_pointer(name);
248 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
249 struct symbol *symbol;
251 symbol = (struct symbol *) object;
252 object = (lispobj *) native_pointer(symbol->name);
254 if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
255 struct vector *string;
257 string = (struct vector *) object;
258 printf("%s, ", (char *) string->data);
260 /* FIXME: broken from (VECTOR NIL) */
261 printf("(Not simple string??\?), ");
263 printf("(Not other pointer??\?), ");
266 function = header->next;
270 printf("CODE: ???, ");
273 printf("LRA: 0x%08lx, ", (uword_t)info.lra);
275 printf("<no LRA>, ");
278 printf("PC: 0x%x>\n", info.pc);
280 printf("PC: ??\?>\n");
282 } while (--nframes > 0 && previous_info(&info));
288 altstack_pointer_p (void *p) {
289 #ifndef LISP_FEATURE_WIN32
290 void* stack_start = ((void *)arch_os_get_current_thread()) + dynamic_values_bytes;
291 void* stack_end = stack_start + 32*SIGSTKSZ;
293 return (p > stack_start && p <= stack_end);
295 /* Win32 doesn't do altstack */
301 stack_pointer_p (void *p)
303 /* we are using sizeof(long) here, because that is the right value on both
304 * x86 and x86-64. (But note that false positives would not cause much harm
305 * given the heuristical nature of x86_call_context.) */
306 uword_t stack_alignment = sizeof(void*);
308 return (altstack_pointer_p(p)
309 || (p < (void *) arch_os_get_current_thread()->control_stack_end
310 && (p > (void *) &p || altstack_pointer_p(&p))
311 && (((uword_t) p) & (stack_alignment-1)) == 0));
315 ra_pointer_p (void *ra)
317 /* the check against 4096 is still a mystery to everyone interviewed about
318 * it, but recent changes to sb-sprof seem to suggest that such values
319 * do occur sometimes. */
320 return ((uword_t) ra) > 4096 && !stack_pointer_p (ra);
324 x86_call_context (void *fp, void **ra, void **ocfp)
330 if (!stack_pointer_p(fp))
333 c_ocfp = *((void **) fp);
334 c_ra = *((void **) fp + 1);
336 c_valid_p = (c_ocfp > fp
337 && stack_pointer_p(c_ocfp)
338 && ra_pointer_p(c_ra));
341 *ra = c_ra, *ocfp = c_ocfp;
348 struct compiled_debug_fun *
349 debug_function_from_pc (struct code* code, void *pc)
351 uword_t code_header_len = sizeof(lispobj) * HeaderValue(code->header);
353 = (uword_t) pc - (uword_t) code - code_header_len;
354 struct compiled_debug_fun *df;
355 struct compiled_debug_info *di;
359 if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
362 di = (struct compiled_debug_info *) native_pointer(code->debug_info);
363 v = (struct vector *) native_pointer(di->fun_map);
364 len = fixnum_value(v->length);
365 df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
370 for (i = 1;; i += 2) {
374 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
376 if (offset >= (uword_t)fixnum_value(df->elsewhere_pc)) {
377 struct compiled_debug_fun *p
378 = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
379 next_pc = fixnum_value(p->elsewhere_pc);
381 next_pc = fixnum_value(v->data[i]);
383 if (offset < next_pc)
384 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
391 sbcl_putwc(wchar_t c, FILE *file)
393 #ifdef LISP_FEATURE_OS_PROVIDES_PUTWC
405 print_string (lispobj *object)
407 int tag = widetag_of(*object);
408 struct vector *vector = (struct vector *) object;
413 int n = fixnum_value(vector->length); \
414 TYPE *data = (TYPE *) vector->data; \
415 for (i = 0; i < n; i++) { \
416 wchar_t c = (wchar_t) data[i]; \
417 if (c == '\\' || c == '"') \
419 sbcl_putwc(c, stdout); \
424 case SIMPLE_BASE_STRING_WIDETAG:
427 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
428 case SIMPLE_CHARACTER_STRING_WIDETAG:
433 printf("<??? type %d>", tag);
439 print_entry_name (lispobj name)
441 if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
443 while (name != NIL) {
444 struct cons *cons = (struct cons *) native_pointer(name);
445 print_entry_name(cons->car);
451 } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
452 lispobj *object = (lispobj *) native_pointer(name);
453 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
454 struct symbol *symbol = (struct symbol *) object;
455 if (symbol->package != NIL) {
457 = (struct package *) native_pointer(symbol->package);
458 lispobj pkg_name = pkg->_name;
459 print_string(native_pointer(pkg_name));
462 print_string(native_pointer(symbol->name));
463 } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
465 print_string(object);
467 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
468 } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
470 print_string(object);
474 printf("<??? type %d>", (int) widetag_of(*object));
477 printf("<??? lowtag %d>", (int) lowtag_of(name));
482 print_entry_points (struct code *code)
484 lispobj function = code->entry_points;
486 while (function != NIL) {
487 struct simple_fun *header = (struct simple_fun *) native_pointer(function);
488 print_entry_name(header->name);
490 function = header->next;
497 describe_thread_state(void)
500 struct thread *thread = arch_os_get_current_thread();
501 struct interrupt_data *data = thread->interrupt_data;
502 #ifndef LISP_FEATURE_WIN32
503 get_current_sigmask(&mask);
504 printf("Signal mask:\n");
505 printf(" SIGALRM = %d\n", sigismember(&mask, SIGALRM));
506 printf(" SIGINT = %d\n", sigismember(&mask, SIGINT));
507 printf(" SIGPROF = %d\n", sigismember(&mask, SIGPROF));
508 #ifdef SIG_STOP_FOR_GC
509 printf(" SIG_STOP_FOR_GC = %d\n", sigismember(&mask, SIG_STOP_FOR_GC));
512 printf("Specials:\n");
513 printf(" *GC-INHIBIT* = %s\n", (SymbolValue(GC_INHIBIT, thread) == T) ? "T" : "NIL");
514 printf(" *GC-PENDING* = %s\n",
515 (SymbolValue(GC_PENDING, thread) == T) ?
516 "T" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
517 "NIL" : ":IN-PROGRESS"));
518 printf(" *INTERRUPTS-ENABLED* = %s\n", (SymbolValue(INTERRUPTS_ENABLED, thread) == T) ? "T" : "NIL");
519 #ifdef STOP_FOR_GC_PENDING
520 printf(" *STOP-FOR-GC-PENDING* = %s\n", (SymbolValue(STOP_FOR_GC_PENDING, thread) == T) ? "T" : "NIL");
522 printf("Pending handler = %p\n", data->pending_handler);
525 /* This function has been split from lisp_backtrace() to enable Lisp
526 * backtraces from gdb with call backtrace_from_fp(...). Useful for
527 * example when debugging threading deadlocks.
530 backtrace_from_fp(void *fp, int nframes)
534 for (i = 0; i < nframes; ++i) {
539 if (!x86_call_context(fp, &ra, &next_fp))
544 p = (lispobj *) component_ptr_from_pc((lispobj *) ra);
546 struct code *cp = (struct code *) p;
547 struct compiled_debug_fun *df = debug_function_from_pc(cp, ra);
549 print_entry_name(df->name);
551 print_entry_points(cp);
553 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
555 if (dladdr(ra, &info)) {
556 printf("Foreign function %s, fp = 0x%lx, ra = 0x%lx",
562 printf("Foreign fp = 0x%p, ra = 0x%p",
573 lisp_backtrace(int nframes)
577 #if defined(LISP_FEATURE_X86)
578 asm("movl %%ebp,%0" : "=g" (fp));
579 #elif defined (LISP_FEATURE_X86_64)
580 asm("movq %%rbp,%0" : "=g" (fp));
582 #error "How did we get here?"
585 backtrace_from_fp(fp, nframes);