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 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
37 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
38 * better not change. */
41 #ifndef LISP_FEATURE_ALPHA
42 struct call_frame *old_cont;
48 lispobj other_state[5];
52 #ifndef LISP_FEATURE_ALPHA
53 struct call_frame *frame;
58 #ifndef LISP_FEATURE_ALPHA
64 int pc; /* Note: this is the trace file offset, not the actual pc. */
67 #define HEADER_LENGTH(header) ((header)>>8)
69 static int previous_info(struct call_info *info);
72 code_pointer(lispobj object)
74 lispobj *headerp, header;
77 headerp = (lispobj *) native_pointer(object);
79 type = widetag_of(header);
82 case CODE_HEADER_WIDETAG:
84 case RETURN_PC_HEADER_WIDETAG:
85 case SIMPLE_FUN_HEADER_WIDETAG:
86 len = HEADER_LENGTH(header);
96 return (struct code *) headerp;
100 cs_valid_pointer_p(struct call_frame *pointer)
102 struct thread *thread=arch_os_get_current_thread();
103 return (((char *) thread->control_stack_start <= (char *) pointer) &&
104 ((char *) pointer < (char *) current_control_stack_pointer));
108 call_info_from_lisp_state(struct call_info *info)
110 info->frame = (struct call_frame *)current_control_frame_pointer;
111 info->interrupted = 0;
120 call_info_from_context(struct call_info *info, os_context_t *context)
124 info->interrupted = 1;
125 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
126 == FUN_POINTER_LOWTAG) {
127 /* We tried to call a function, but crapped out before $CODE could
128 * be fixed up. Probably an undefined function. */
130 (struct call_frame *)(*os_context_register_addr(context,
132 info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
133 info->code = code_pointer(info->lra);
134 pc = (unsigned long)native_pointer(info->lra);
138 (struct call_frame *)(*os_context_register_addr(context, reg_CFP));
140 code_pointer(*os_context_register_addr(context, reg_CODE));
142 pc = *os_context_pc_addr(context);
144 if (info->code != NULL)
145 info->pc = pc - (unsigned long) info->code -
146 #ifndef LISP_FEATURE_ALPHA
147 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
149 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
156 previous_info(struct call_info *info)
158 struct call_frame *this_frame;
159 struct thread *thread=arch_os_get_current_thread();
162 if (!cs_valid_pointer_p(info->frame)) {
163 printf("Bogus callee value (0x%08lx).\n", (unsigned long)info->frame);
167 this_frame = info->frame;
168 info->lra = this_frame->saved_lra;
169 info->frame = this_frame->old_cont;
170 info->interrupted = 0;
172 if (info->frame == NULL || info->frame == this_frame)
175 if (info->lra == NIL) {
176 /* We were interrupted. Find the correct signal context. */
177 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)>>2;
179 os_context_t *context =
180 thread->interrupt_contexts[free];
181 if ((struct call_frame *)(*os_context_register_addr(context,
184 call_info_from_context(info, context);
190 info->code = code_pointer(info->lra);
191 if (info->code != NULL)
192 info->pc = (unsigned long)native_pointer(info->lra) -
193 (unsigned long)info->code -
194 #ifndef LISP_FEATURE_ALPHA
195 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
197 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
207 backtrace(int nframes)
209 struct call_info info;
211 call_info_from_lisp_state(&info);
214 printf("<Frame 0x%08lx%s, ", (unsigned long) info.frame,
215 info.interrupted ? " [interrupted]" : "");
217 if (info.code != (struct code *) 0) {
220 printf("CODE: 0x%08lX, ", (unsigned long) info.code | OTHER_POINTER_LOWTAG);
222 #ifndef LISP_FEATURE_ALPHA
223 function = info.code->entry_points;
225 function = ((struct code *)info.code)->entry_points;
227 while (function != NIL) {
228 struct simple_fun *header;
231 header = (struct simple_fun *) native_pointer(function);
234 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
237 object = (lispobj *) native_pointer(name);
239 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
240 struct symbol *symbol;
242 symbol = (struct symbol *) object;
243 object = (lispobj *) native_pointer(symbol->name);
245 if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
246 struct vector *string;
248 string = (struct vector *) object;
249 printf("%s, ", (char *) string->data);
251 /* FIXME: broken from (VECTOR NIL) */
252 printf("(Not simple string??\?), ");
254 printf("(Not other pointer??\?), ");
257 function = header->next;
261 printf("CODE: ???, ");
264 printf("LRA: 0x%08lx, ", (unsigned long)info.lra);
266 printf("<no LRA>, ");
269 printf("PC: 0x%x>\n", info.pc);
271 printf("PC: ??\?>\n");
273 } while (--nframes > 0 && previous_info(&info));
279 stack_pointer_p (void *p)
281 return (p < (void *) arch_os_get_current_thread()->control_stack_end
283 && (((unsigned long) p) & 3) == 0);
287 ra_pointer_p (void *ra)
289 return ((unsigned long) ra) > 4096 && !stack_pointer_p (ra);
293 x86_call_context (void *fp, void **ra, void **ocfp)
299 int lisp_valid_p, c_valid_p;
301 if (!stack_pointer_p(fp))
304 c_ocfp = *((void **) fp);
305 c_ra = *((void **) fp + 1);
306 lisp_ocfp = *((void **) fp - 1);
307 lisp_ra = *((void **) fp - 2);
309 lisp_valid_p = (lisp_ocfp > fp
310 && stack_pointer_p(lisp_ocfp)
311 && ra_pointer_p(lisp_ra));
312 c_valid_p = (c_ocfp > fp
313 && stack_pointer_p(c_ocfp)
314 && ra_pointer_p(c_ra));
316 if (lisp_valid_p && c_valid_p) {
321 int lisp_path_p = x86_call_context(lisp_ocfp, &lisp_path_fp, &dummy);
322 int c_path_p = x86_call_context(c_ocfp, &c_path_fp, &dummy);
324 if (lisp_path_p && c_path_p) {
325 #if defined __FreeBSD__ && __FreeBSD_version > 400000
326 if (lisp_ocfp > c_ocfp)
327 *ra = lisp_ra, *ocfp = lisp_ocfp;
329 *ra = c_ra, *ocfp = c_ocfp;
331 *ra = lisp_ra, *ocfp = lisp_ocfp;
334 else if (lisp_path_p)
335 *ra = lisp_ra, *ocfp = lisp_ocfp;
337 *ra = c_ra, *ocfp = c_ocfp;
341 else if (lisp_valid_p)
342 *ra = lisp_ra, *ocfp = lisp_ocfp;
344 *ra = c_ra, *ocfp = c_ocfp;
351 struct compiled_debug_fun *
352 debug_function_from_pc (struct code* code, void *pc)
354 unsigned long code_header_len = sizeof(lispobj) * HeaderValue(code->header);
356 = (unsigned long) pc - (unsigned long) code - code_header_len;
357 struct compiled_debug_fun *df;
358 struct compiled_debug_info *di;
362 if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
365 di = (struct compiled_debug_info *) native_pointer(code->debug_info);
366 v = (struct vector *) native_pointer(di->fun_map);
367 len = fixnum_value(v->length);
368 df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
373 for (i = 1;; i += 2) {
377 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
379 if (offset >= fixnum_value(df->elsewhere_pc)) {
380 struct compiled_debug_fun *p
381 = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
382 next_pc = fixnum_value(p->elsewhere_pc);
384 next_pc = fixnum_value(v->data[i]);
386 if (offset < next_pc)
387 return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
394 print_entry_name (lispobj name)
396 if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
398 while (name != NIL) {
399 struct cons *cons = (struct cons *) native_pointer(name);
400 print_entry_name(cons->car);
406 } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
407 lispobj *object = (lispobj *) native_pointer(name);
409 if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
410 struct symbol *symbol = (struct symbol *) object;
411 struct vector *string;
413 if (symbol->package != NIL) {
415 = (struct package *) native_pointer(symbol->package);
416 lispobj pkg_name = pkg->_name;
417 string = (struct vector *) native_pointer(pkg_name);
418 printf("%s::", (char *) string->data);
421 object = (lispobj *) native_pointer(symbol->name);
422 string = (struct vector *) object;
423 printf("%s", (char *) string->data);
424 } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
425 struct vector *string = (struct vector *) object;
426 printf("\"%s\"", (char *) string->data);
427 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
428 } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
429 struct vector *string = (struct vector *) object;
430 printf("<oops, a unicode string>"); /* FIXME */
433 printf("<??? type %d>", (int) widetag_of(*object));
435 printf("<??? lowtag %d>", (int) lowtag_of(name));
439 print_entry_points (struct code *code)
441 lispobj function = code->entry_points;
443 while (function != NIL) {
444 struct simple_fun *header = (struct simple_fun *) native_pointer(function);
445 print_entry_name(header->name);
447 function = header->next;
454 backtrace(int nframes)
459 #if defined(LISP_FEATURE_X86)
460 asm("movl %%ebp,%0" : "=g" (fp));
461 #elif defined (LISP_FEATURE_X86_64)
462 asm("movq %%rbp,%0" : "=g" (fp));
464 #error "How did we get here?"
467 for (i = 0; i < nframes; ++i) {
472 if (!x86_call_context(fp, &ra, &next_fp))
477 p = (lispobj *) component_ptr_from_pc((lispobj *) ra);
479 struct code *cp = (struct code *) p;
480 struct compiled_debug_fun *df = debug_function_from_pc(cp, ra);
482 print_entry_name(df->name);
484 print_entry_points(cp);
486 printf("Foreign fp = 0x%lx, ra = 0x%lx",
487 (unsigned long) next_fp,