Change immobile space free pointers to alien vars
[sbcl.git] / src / runtime / backtrace.c
bloba5ad66406762d9d88cb43786a21e347a1010b7ef
1 /*
2 * simple backtrace facility
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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 */
17 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <signal.h>
21 #include "sbcl.h"
22 #include "runtime.h"
23 #include "globals.h"
24 #include "os.h"
25 #include "interrupt.h"
26 #include "lispregs.h"
27 #include <wchar.h>
28 #include "arch.h"
29 #include "genesis/compiled-debug-fun.h"
30 #include "genesis/compiled-debug-info.h"
31 #include "genesis/package.h"
32 #include "genesis/static-symbols.h"
33 #include "genesis/primitive-objects.h"
34 #include "thread.h"
35 #include "gc-internal.h"
36 #include "var-io.h"
38 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
39 # include <dlfcn.h>
40 #endif
42 static void
43 sbcl_putwc(wchar_t c, FILE *file)
45 #ifdef LISP_FEATURE_OS_PROVIDES_PUTWC
46 putwc(c, file);
47 #else
48 if (c < 256) {
49 fputc(c, file);
50 } else {
51 fputc('?', file);
53 #endif
56 unsigned int decode_elsewhere_pc(lispobj packed_integer)
58 struct varint_unpacker unpacker;
59 int value;
60 varint_unpacker_init(&unpacker, packed_integer);
61 varint_unpack(&unpacker, &value);
62 return value;
65 struct compiled_debug_fun *
66 debug_function_from_pc (struct code* code, void *pc)
68 uword_t code_header_len = sizeof(lispobj) * code_header_words(code->header);
69 uword_t offset
70 = (uword_t) pc - (uword_t) code - code_header_len;
71 struct compiled_debug_fun *df;
72 struct compiled_debug_info *di;
73 struct vector *v;
74 int i, len;
76 if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
77 return NULL;
79 di = (struct compiled_debug_info *) native_pointer(code->debug_info);
81 if (lowtag_of(di->fun_map) != INSTANCE_POINTER_LOWTAG)
82 return NULL;
84 v = VECTOR(di->fun_map);
86 len = fixnum_value(v->length);
88 if (lowtag_of(v->data[0]) != INSTANCE_POINTER_LOWTAG)
89 return NULL;
91 df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
93 if (len == 1)
94 return df;
96 for (i = 1;; i += 2) {
97 unsigned next_pc;
99 if (i == len)
100 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
102 if (offset >= (uword_t)decode_elsewhere_pc(df->encoded_locs)) {
103 struct compiled_debug_fun *p
104 = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
105 next_pc = decode_elsewhere_pc(p->encoded_locs);
106 } else
107 next_pc = fixnum_value(v->data[i]);
109 if (offset < next_pc)
110 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
113 return NULL;
116 static void
117 print_string (struct vector *vector)
119 int tag = widetag_of(vector->header);
121 #define doit(TYPE) \
122 do { \
123 int i; \
124 int n = fixnum_value(vector->length); \
125 TYPE *data = (TYPE *) vector->data; \
126 for (i = 0; i < n; i++) { \
127 wchar_t c = (wchar_t) data[i]; \
128 if (c == '\\' || c == '"') \
129 putchar('\\'); \
130 sbcl_putwc(c, stdout); \
132 } while (0)
134 switch (tag) {
135 case SIMPLE_BASE_STRING_WIDETAG:
136 doit(unsigned char);
137 break;
138 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
139 case SIMPLE_CHARACTER_STRING_WIDETAG:
140 doit(unsigned int);
141 break;
142 #endif
143 default:
144 printf("<??? type %d>", tag);
146 #undef doit
149 static int string_equal (struct vector *vector, char *string)
151 if (widetag_of(vector->header) != SIMPLE_BASE_STRING_WIDETAG)
152 return 0;
153 return !strcmp((char *) vector->data, string);
156 static void
157 print_entry_name (lispobj name)
159 if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
160 putchar('(');
161 while (name != NIL) {
162 if (lowtag_of (name) != LIST_POINTER_LOWTAG) {
163 printf("%p: unexpected lowtag while printing a cons\n",
164 (void*)name);
165 return;
167 print_entry_name(CONS(name)->car);
168 name = CONS(name)->cdr;
169 if (name != NIL)
170 putchar(' ');
172 putchar(')');
173 } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
174 lispobj *object = native_pointer(name);
175 if (widetag_of(*object) == SYMBOL_WIDETAG) {
176 struct symbol *symbol = (struct symbol *) object;
177 if (symbol->package != NIL) {
178 struct package *pkg
179 = (struct package *) native_pointer(symbol->package);
180 struct vector *pkg_name = VECTOR(pkg->_name);
181 if (string_equal(pkg_name, "COMMON-LISP"))
183 else if (string_equal(pkg_name, "COMMON-LISP-USER")) {
184 fputs("CL-USER::", stdout);
186 else if (string_equal(pkg_name, "KEYWORD")) {
187 putchar(':');
188 } else {
189 print_string(pkg_name);
190 fputs("::", stdout);
193 print_string(VECTOR(symbol->name));
194 } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG
195 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
196 || widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG
197 #endif
199 putchar('"');
200 print_string((struct vector*)object);
201 putchar('"');
202 } else {
203 printf("<??? type %d>", (int) widetag_of(*object));
205 } else {
206 printf("<??? lowtag %d>", (int) lowtag_of(name));
210 static void
211 print_entry_points (struct code *code)
213 int n_funs = code_n_funs(code);
214 for_each_simple_fun(index, fun, code, 0, {
215 if (widetag_of(fun->header) != SIMPLE_FUN_WIDETAG) {
216 printf("%p: bogus function entry", fun);
217 return;
219 print_entry_name(fun->name);
220 if ((index + 1) < n_funs) printf (", ");
225 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
227 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
228 * better not change. */
230 struct call_frame {
231 #ifndef LISP_FEATURE_ALPHA
232 struct call_frame *old_cont;
233 #else
234 u32 old_cont;
235 #endif
236 lispobj saved_lra;
237 lispobj code;
238 lispobj other_state[5];
241 struct call_info {
242 #ifndef LISP_FEATURE_ALPHA
243 struct call_frame *frame;
244 #else
245 u32 frame;
246 #endif
247 int interrupted;
248 #ifndef LISP_FEATURE_ALPHA
249 struct code *code;
250 #else
251 u32 code;
252 #endif
253 lispobj lra;
254 int pc; /* Note: this is the trace file offset, not the actual pc. */
257 #define HEADER_LENGTH(header) ((header)>>8)
259 static int previous_info(struct call_info *info);
261 static struct code *
262 code_pointer(lispobj object)
264 lispobj *headerp, header;
265 int type, len;
267 headerp = native_pointer(object);
268 header = *headerp;
269 type = widetag_of(header);
271 switch (type) {
272 case CODE_HEADER_WIDETAG:
273 break;
274 case RETURN_PC_WIDETAG:
275 case SIMPLE_FUN_WIDETAG:
276 len = HEADER_LENGTH(header);
277 if (len == 0)
278 headerp = NULL;
279 else
280 headerp -= len;
281 break;
282 default:
283 headerp = NULL;
286 return (struct code *) headerp;
289 static boolean
290 cs_valid_pointer_p(struct call_frame *pointer)
292 struct thread *thread=arch_os_get_current_thread();
293 return (((char *) thread->control_stack_start <= (char *) pointer) &&
294 ((char *) pointer < (char *) access_control_stack_pointer(thread)));
297 static void
298 call_info_from_lisp_state(struct call_info *info)
300 info->frame = (struct call_frame *)access_control_frame_pointer(arch_os_get_current_thread());
301 info->interrupted = 0;
302 info->code = NULL;
303 info->lra = 0;
304 info->pc = 0;
306 previous_info(info);
309 static void
310 call_info_from_context(struct call_info *info, os_context_t *context)
312 uword_t pc;
314 info->interrupted = 1;
315 #if !defined(LISP_FEATURE_ARM) && !defined(LISP_FEATURE_ARM64)
316 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
317 == FUN_POINTER_LOWTAG) {
318 /* We tried to call a function, but crapped out before $CODE could
319 * be fixed up. Probably an undefined function. */
320 info->frame =
321 (struct call_frame *)(uword_t)
322 (*os_context_register_addr(context, reg_OCFP));
323 info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
324 info->code = code_pointer(info->lra);
325 pc = (uword_t)native_pointer(info->lra);
326 } else
327 #endif
329 info->frame =
330 (struct call_frame *)(uword_t)
331 (*os_context_register_addr(context, reg_CFP));
332 info->code =
333 code_pointer(*os_context_register_addr(context, reg_CODE));
334 info->lra = NIL;
335 pc = *os_context_pc_addr(context);
337 if (info->code != NULL)
338 info->pc = pc - (uword_t) info->code -
339 #ifndef LISP_FEATURE_ALPHA
340 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
341 #else
342 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
343 #endif
344 else
345 info->pc = 0;
348 static int
349 previous_info(struct call_info *info)
351 struct call_frame *this_frame;
352 struct thread *thread=arch_os_get_current_thread();
353 int free_ici;
354 lispobj lra;
356 if (!cs_valid_pointer_p(info->frame)) {
357 printf("Bogus callee value (0x%lx).\n", (long)info->frame);
358 return 0;
361 this_frame = info->frame;
362 info->lra = this_frame->saved_lra;
363 info->frame = this_frame->old_cont;
364 info->interrupted = 0;
366 if (info->frame == NULL || info->frame == this_frame)
367 return 0;
368 lra = info->lra;
369 if (lra == NIL) {
370 /* We were interrupted. Find the correct signal context. */
371 free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
372 while (free_ici-- > 0) {
373 os_context_t *context =
374 thread->interrupt_contexts[free_ici];
375 if ((struct call_frame *)(uword_t)
376 (*os_context_register_addr(context, reg_CFP))
377 == info->frame) {
378 call_info_from_context(info, context);
379 break;
382 } else if (fixnump(lra)) {
383 info->code = (struct code*)native_pointer(this_frame->code);
384 info->pc = (uword_t)(info->code + lra);
385 info->lra = NIL;
386 } else {
387 info->code = code_pointer(lra);
389 if (info->code != NULL)
390 info->pc = (uword_t)native_pointer(info->lra) -
391 (uword_t)info->code -
392 #ifndef LISP_FEATURE_ALPHA
393 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
394 #else
395 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
396 #endif
397 else
398 info->pc = 0;
401 return 1;
404 void
405 lisp_backtrace(int nframes)
407 struct call_info info;
408 int i = 0;
409 call_info_from_lisp_state(&info);
411 do {
412 printf("%4d: ", i);
414 if (info.code != (struct code *) 0) {
415 struct compiled_debug_fun *df ;
416 if (info.lra != NIL &&
417 (df = debug_function_from_pc((struct code *)info.code, (void *)info.lra)))
418 print_entry_name(df->name);
419 else
420 print_entry_points((struct code *)info.code);
422 printf(" %p", (void*)((uword_t) info.code | OTHER_POINTER_LOWTAG));
424 else
425 printf("CODE = ???");
426 printf("%s fp = %p", info.interrupted ? " [interrupted]" : "",
427 info.frame);
429 if (info.lra != NIL)
430 printf(" LRA = %p", (void*)info.lra);
431 else
432 printf(" <no LRA>");
434 if (info.pc)
435 printf(" pc = %p", (void*)(long)info.pc);
436 putchar('\n');
438 } while (i++ < nframes && previous_info(&info));
441 #else
443 static int
444 altstack_pointer_p (void *p) {
445 #ifndef LISP_FEATURE_WIN32
446 void* stack_start = ((char*)arch_os_get_current_thread()) + dynamic_values_bytes;
447 void* stack_end = (char*)stack_start + 32*SIGSTKSZ;
449 return (p > stack_start && p <= stack_end);
450 #else
451 /* Win32 doesn't do altstack */
452 return 0;
453 #endif
456 static int
457 stack_pointer_p (void *p)
459 /* we are using sizeof(long) here, because that is the right value on both
460 * x86 and x86-64. (But note that false positives would not cause much harm
461 * given the heuristical nature of x86_call_context.) */
462 uword_t stack_alignment = sizeof(void*);
463 void *stack_start;
464 struct thread *thread = arch_os_get_current_thread();
466 if (altstack_pointer_p(p))
467 return 1;
469 if (altstack_pointer_p(&p)) {
470 stack_start = (void *) thread->control_stack_start;
471 } else {
472 /* Use the current frame address, since there should be no
473 * relevant frames below. */
474 stack_start = &p;
476 return p >= stack_start
477 && p < (void *) thread->control_stack_end
478 && (((uword_t) p) & (stack_alignment-1)) == 0;
481 static int
482 ra_pointer_p (void *ra)
484 /* the check against 4096 is still a mystery to everyone interviewed about
485 * it, but recent changes to sb-sprof seem to suggest that such values
486 * do occur sometimes. */
487 return ((uword_t) ra) > 4096 && !stack_pointer_p (ra);
490 static int
491 x86_call_context (void *fp, void **ra, void **ocfp)
493 void *c_ocfp;
494 void *c_ra;
495 int c_valid_p;
497 if (!stack_pointer_p(fp))
498 return 0;
500 c_ocfp = *((void **) fp);
501 c_ra = *((void **) fp + 1);
503 c_valid_p = (c_ocfp > fp
504 && stack_pointer_p(c_ocfp)
505 && ra_pointer_p(c_ra));
507 if (c_valid_p)
508 *ra = c_ra, *ocfp = c_ocfp;
509 else
510 return 0;
512 return 1;
515 void
516 describe_thread_state(void)
518 sigset_t mask;
519 struct thread *thread = arch_os_get_current_thread();
520 struct interrupt_data *data = thread->interrupt_data;
521 #ifndef LISP_FEATURE_WIN32
522 get_current_sigmask(&mask);
523 printf("Signal mask:\n");
524 printf(" SIGALRM = %d\n", sigismember(&mask, SIGALRM));
525 printf(" SIGINT = %d\n", sigismember(&mask, SIGINT));
526 printf(" SIGPROF = %d\n", sigismember(&mask, SIGPROF));
527 #ifdef SIG_STOP_FOR_GC
528 printf(" SIG_STOP_FOR_GC = %d\n", sigismember(&mask, SIG_STOP_FOR_GC));
529 #endif
530 #endif
531 printf("Specials:\n");
532 printf(" *GC-INHIBIT* = %s\n", (SymbolValue(GC_INHIBIT, thread) == T) ? "T" : "NIL");
533 printf(" *GC-PENDING* = %s\n",
534 (SymbolValue(GC_PENDING, thread) == T) ?
535 "T" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
536 "NIL" : ":IN-PROGRESS"));
537 printf(" *INTERRUPTS-ENABLED* = %s\n", (SymbolValue(INTERRUPTS_ENABLED, thread) == T) ? "T" : "NIL");
538 #ifdef STOP_FOR_GC_PENDING
539 printf(" *STOP-FOR-GC-PENDING* = %s\n", (SymbolValue(STOP_FOR_GC_PENDING, thread) == T) ? "T" : "NIL");
540 #endif
541 printf("Pending handler = %p\n", data->pending_handler);
544 void print_backtrace_frame(void *pc, void *fp, int i) {
545 lispobj *p;
546 printf("%4d: ", i);
548 p = (lispobj *) component_ptr_from_pc((lispobj *) pc);
550 if (p) {
551 struct code *cp = (struct code *) p;
552 struct compiled_debug_fun *df = debug_function_from_pc(cp, pc);
553 if (df)
554 print_entry_name(df->name);
555 else
556 print_entry_points(cp);
557 printf(", pc = %p, fp = %p", pc, fp);
558 } else {
559 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
560 Dl_info info;
561 if (dladdr(pc, &info)) {
562 printf("Foreign function %s, pc = %p, fp = %p", info.dli_sname, pc, fp);
563 } else
564 #endif
565 printf("Foreign function, pc = %p, fp = %p", pc, fp);
568 putchar('\n');
571 /* This function has been split from lisp_backtrace() to enable Lisp
572 * backtraces from gdb with call backtrace_from_fp(...). Useful for
573 * example when debugging threading deadlocks.
575 void
576 backtrace_from_fp(void *fp, int nframes, int start)
578 int i = start;
580 for (; i < nframes; ++i) {
581 void *ra;
582 void *next_fp;
584 if (!x86_call_context(fp, &ra, &next_fp))
585 break;
586 print_backtrace_frame(ra, next_fp, i);
587 fp = next_fp;
591 void backtrace_from_context(os_context_t *context, int nframes) {
592 #ifdef LISP_FEATURE_X86
593 void *fp = (void *)*os_context_register_addr(context,reg_EBP);
594 #elif defined (LISP_FEATURE_X86_64)
595 void *fp = (void *)*os_context_register_addr(context,reg_RBP);
596 #endif
597 print_backtrace_frame((void *)*os_context_pc_addr(context), fp, 0);
598 backtrace_from_fp(fp, nframes - 1, 1);
601 void
602 lisp_backtrace(int nframes)
604 struct thread *thread=arch_os_get_current_thread();
605 int free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
607 if (free_ici) {
608 os_context_t *context = thread->interrupt_contexts[free_ici - 1];
609 backtrace_from_context(context, nframes);
610 } else {
611 void *fp;
613 #ifdef LISP_FEATURE_X86
614 asm("movl %%ebp,%0" : "=g" (fp));
615 #elif defined (LISP_FEATURE_X86_64)
616 asm("movq %%rbp,%0" : "=g" (fp));
617 #endif
618 backtrace_from_fp(fp, nframes, 0);
621 #endif