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
27 #include "gencgc-alloc-region.h"
28 #include "genesis/compiled-debug-fun.h"
29 #include "genesis/compiled-debug-info.h"
30 #include "genesis/package.h"
32 #include "genesis/static-symbols.h"
33 #include "genesis/primitive-objects.h"
36 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
38 /* __USE_GNU needed if we want dladdr() and Dl_Info from glibc. */
44 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
46 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
47 * better not change. */
50 #ifndef LISP_FEATURE_ALPHA
51 struct call_frame *old_cont;
57 lispobj other_state[5];
61 #ifndef LISP_FEATURE_ALPHA
62 struct call_frame *frame;
67 #ifndef LISP_FEATURE_ALPHA
73 int pc; /* Note: this is the trace file offset, not the actual pc. */
76 #define HEADER_LENGTH(header) ((header)>>8)
78 static int previous_info(struct call_info *info);
81 code_pointer(lispobj object)
83 lispobj *headerp, header;
86 headerp = (lispobj *) native_pointer(object);
88 type = widetag_of(header);
91 case CODE_HEADER_WIDETAG:
93 case RETURN_PC_HEADER_WIDETAG:
94 case SIMPLE_FUN_HEADER_WIDETAG:
95 len = HEADER_LENGTH(header);
105 return (struct code *) headerp;
109 cs_valid_pointer_p(struct call_frame *pointer)
111 struct thread *thread=arch_os_get_current_thread();
112 return (((char *) thread->control_stack_start <= (char *) pointer) &&
113 ((char *) pointer < (char *) access_control_stack_pointer(thread)));
117 call_info_from_lisp_state(struct call_info *info)
119 info->frame = (struct call_frame *)access_control_frame_pointer(arch_os_get_current_thread());
120 info->interrupted = 0;
129 call_info_from_context(struct call_info *info, os_context_t *context)
133 info->interrupted = 1;
134 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
135 == FUN_POINTER_LOWTAG) {
136 /* We tried to call a function, but crapped out before $CODE could
137 * be fixed up. Probably an undefined function. */
139 (struct call_frame *)(unsigned long)
140 (*os_context_register_addr(context, reg_OCFP));
141 info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
142 info->code = code_pointer(info->lra);
143 pc = (unsigned long)native_pointer(info->lra);
147 (struct call_frame *)(unsigned long)
148 (*os_context_register_addr(context, reg_CFP));
150 code_pointer(*os_context_register_addr(context, reg_CODE));
152 pc = *os_context_pc_addr(context);
154 if (info->code != NULL)
155 info->pc = pc - (unsigned long) info->code -
156 #ifndef LISP_FEATURE_ALPHA
157 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
159 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
166 previous_info(struct call_info *info)
168 struct call_frame *this_frame;
169 struct thread *thread=arch_os_get_current_thread();
172 if (!cs_valid_pointer_p(info->frame)) {
173 printf("Bogus callee value (0x%08lx).\n", (unsigned long)info->frame);
177 this_frame = info->frame;
178 info->lra = this_frame->saved_lra;
179 info->frame = this_frame->old_cont;
180 info->interrupted = 0;
182 if (info->frame == NULL || info->frame == this_frame)
185 if (info->lra == NIL) {
186 /* We were interrupted. Find the correct signal context. */
187 free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
188 while (free_ici-- > 0) {
189 os_context_t *context =
190 thread->interrupt_contexts[free_ici];
191 if ((struct call_frame *)(unsigned long)
192 (*os_context_register_addr(context, reg_CFP))
194 call_info_from_context(info, context);
200 info->code = code_pointer(info->lra);
201 if (info->code != NULL)
202 info->pc = (unsigned long)native_pointer(info->lra) -
203 (unsigned long)info->code -
204 #ifndef LISP_FEATURE_ALPHA
205 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
207 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
217 lisp_backtrace(int nframes)
219 struct call_info info;
221 call_info_from_lisp_state(&info);
224 printf("<Frame 0x%08lx%s, ", (unsigned long) info.frame,
225 info.interrupted ? " [interrupted]" : "");
227 if (info.code != (struct code *) 0) {
230 printf("CODE: 0x%08lX, ", (unsigned long) info.code | OTHER_POINTER_LOWTAG);
232 #ifndef LISP_FEATURE_ALPHA
233 function = info.code->entry_points;
235 function = ((struct code *)info.code)->entry_points;
237 while (function != NIL) {
238 struct simple_fun *header;
241 header = (struct simple_fun *) native_pointer(function);
244 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
247 object = (lispobj *) native_pointer(name);
249 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
250 struct symbol *symbol;
252 symbol = (struct symbol *) object;
253 object = (lispobj *) native_pointer(symbol->name);
255 if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
256 struct vector *string;
258 string = (struct vector *) object;
259 printf("%s, ", (char *) string->data);
261 /* FIXME: broken from (VECTOR NIL) */
262 printf("(Not simple string??\?), ");
264 printf("(Not other pointer??\?), ");
267 function = header->next;
271 printf("CODE: ???, ");
274 printf("LRA: 0x%08lx, ", (unsigned long)info.lra);
276 printf("<no LRA>, ");
279 printf("PC: 0x%x>\n", info.pc);
281 printf("PC: ??\?>\n");
283 } while (--nframes > 0 && previous_info(&info));
289 altstack_pointer_p (void *p) {
290 #ifndef LISP_FEATURE_WIN32
291 void* stack_start = ((void *)arch_os_get_current_thread()) + dynamic_values_bytes;
292 void* stack_end = stack_start + 32*SIGSTKSZ;
294 return (p > stack_start && p <= stack_end);
296 /* Win32 doesn't do altstack */
302 stack_pointer_p (void *p)
304 /* we are using sizeof(long) here, because that is the right value on both
305 * x86 and x86-64. (But note that false positives would not cause much harm
306 * given the heuristical nature of x86_call_context.) */
307 unsigned long stack_alignment = sizeof(void*);
309 return (altstack_pointer_p(p)
310 || (p < (void *) arch_os_get_current_thread()->control_stack_end
311 && (p > (void *) &p || altstack_pointer_p(&p))
312 && (((unsigned long) p) & (stack_alignment-1)) == 0));
316 ra_pointer_p (void *ra)
318 /* the check against 4096 is still a mystery to everyone interviewed about
319 * it, but recent changes to sb-sprof seem to suggest that such values
320 * do occur sometimes. */
321 return ((unsigned long) ra) > 4096 && !stack_pointer_p (ra);
325 x86_call_context (void *fp, void **ra, void **ocfp)
331 if (!stack_pointer_p(fp))
334 c_ocfp = *((void **) fp);
335 c_ra = *((void **) fp + 1);
337 c_valid_p = (c_ocfp > fp
338 && stack_pointer_p(c_ocfp)
339 && ra_pointer_p(c_ra));
342 *ra = c_ra, *ocfp = c_ocfp;
349 struct compiled_debug_fun *
350 debug_function_from_pc (struct code* code, void *pc)
352 unsigned long code_header_len = sizeof(lispobj) * HeaderValue(code->header);
354 = (unsigned long) pc - (unsigned long) code - code_header_len;
355 struct compiled_debug_fun *df;
356 struct compiled_debug_info *di;
360 if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
363 di = (struct compiled_debug_info *) native_pointer(code->debug_info);
364 v = (struct vector *) native_pointer(di->fun_map);
365 len = fixnum_value(v->length);
366 df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
371 for (i = 1;; i += 2) {
375 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
377 if (offset >= (unsigned long)fixnum_value(df->elsewhere_pc)) {
378 struct compiled_debug_fun *p
379 = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
380 next_pc = fixnum_value(p->elsewhere_pc);
382 next_pc = fixnum_value(v->data[i]);
384 if (offset < next_pc)
385 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
392 sbcl_putwc(wchar_t c, FILE *file)
394 #ifdef LISP_FEATURE_OS_PROVIDES_PUTWC
406 print_string (lispobj *object)
408 int tag = widetag_of(*object);
409 struct vector *vector = (struct vector *) object;
414 int n = fixnum_value(vector->length); \
415 TYPE *data = (TYPE *) vector->data; \
416 for (i = 0; i < n; i++) { \
417 wchar_t c = (wchar_t) data[i]; \
418 if (c == '\\' || c == '"') \
420 sbcl_putwc(c, stdout); \
425 case SIMPLE_BASE_STRING_WIDETAG:
428 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
429 case SIMPLE_CHARACTER_STRING_WIDETAG:
434 printf("<??? type %d>", tag);
440 print_entry_name (lispobj name)
442 if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
444 while (name != NIL) {
445 struct cons *cons = (struct cons *) native_pointer(name);
446 print_entry_name(cons->car);
452 } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
453 lispobj *object = (lispobj *) native_pointer(name);
454 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
455 struct symbol *symbol = (struct symbol *) object;
456 if (symbol->package != NIL) {
458 = (struct package *) native_pointer(symbol->package);
459 lispobj pkg_name = pkg->_name;
460 print_string(native_pointer(pkg_name));
463 print_string(native_pointer(symbol->name));
464 } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
466 print_string(object);
468 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
469 } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
471 print_string(object);
475 printf("<??? type %d>", (int) widetag_of(*object));
478 printf("<??? lowtag %d>", (int) lowtag_of(name));
483 print_entry_points (struct code *code)
485 lispobj function = code->entry_points;
487 while (function != NIL) {
488 struct simple_fun *header = (struct simple_fun *) native_pointer(function);
489 print_entry_name(header->name);
491 function = header->next;
498 describe_thread_state(void)
501 struct thread *thread = arch_os_get_current_thread();
502 struct interrupt_data *data = thread->interrupt_data;
503 #ifndef LISP_FEATURE_WIN32
504 get_current_sigmask(&mask);
505 printf("Signal mask:\n");
506 printf(" SIGALRM = %d\n", sigismember(&mask, SIGALRM));
507 printf(" SIGINT = %d\n", sigismember(&mask, SIGINT));
508 printf(" SIGPROF = %d\n", sigismember(&mask, SIGPROF));
509 #ifdef SIG_STOP_FOR_GC
510 printf(" SIG_STOP_FOR_GC = %d\n", sigismember(&mask, SIG_STOP_FOR_GC));
513 printf("Specials:\n");
514 printf(" *GC-INHIBIT* = %s\n", (SymbolValue(GC_INHIBIT, thread) == T) ? "T" : "NIL");
515 printf(" *GC-PENDING* = %s\n",
516 (SymbolValue(GC_PENDING, thread) == T) ?
517 "T" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
518 "NIL" : ":IN-PROGRESS"));
519 printf(" *INTERRUPTS-ENABLED* = %s\n", (SymbolValue(INTERRUPTS_ENABLED, thread) == T) ? "T" : "NIL");
520 #ifdef STOP_FOR_GC_PENDING
521 printf(" *STOP-FOR-GC-PENDING* = %s\n", (SymbolValue(STOP_FOR_GC_PENDING, thread) == T) ? "T" : "NIL");
523 printf("Pending handler = %p\n", data->pending_handler);
526 /* This function has been split from lisp_backtrace() to enable Lisp
527 * backtraces from gdb with call backtrace_from_fp(...). Useful for
528 * example when debugging threading deadlocks.
531 backtrace_from_fp(void *fp, int nframes)
535 for (i = 0; i < nframes; ++i) {
540 if (!x86_call_context(fp, &ra, &next_fp))
545 p = (lispobj *) component_ptr_from_pc((lispobj *) ra);
547 struct code *cp = (struct code *) p;
548 struct compiled_debug_fun *df = debug_function_from_pc(cp, ra);
550 print_entry_name(df->name);
552 print_entry_points(cp);
554 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
556 if (dladdr(ra, &info)) {
557 printf("Foreign function %s, fp = 0x%lx, ra = 0x%lx",
559 (unsigned long) next_fp,
563 printf("Foreign fp = 0x%p, ra = 0x%p",
574 lisp_backtrace(int nframes)
578 #if defined(LISP_FEATURE_X86)
579 asm("movl %%ebp,%0" : "=g" (fp));
580 #elif defined (LISP_FEATURE_X86_64)
581 asm("movq %%rbp,%0" : "=g" (fp));
583 #error "How did we get here?"
586 backtrace_from_fp(fp, nframes);