1.0.4.93: backtrace_from_fp
[sbcl.git] / src / runtime / backtrace.c
1 /*
2  * simple backtrace facility
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
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.
14  */
15
16 #include <stdio.h>
17 #include <signal.h>
18 #include "sbcl.h"
19 #include "runtime.h"
20 #include "globals.h"
21 #include "os.h"
22 #include "interrupt.h"
23 #include "lispregs.h"
24 #ifdef LISP_FEATURE_GENCGC
25 #include <wchar.h>
26 #include "arch.h"
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"
31 #endif
32 #include "genesis/static-symbols.h"
33 #include "genesis/primitive-objects.h"
34 #include "thread.h"
35
36 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
37 /* __USE_GNU needed if we want dladdr() and Dl_Info from glibc. */
38 #define __USE_GNU
39 #include "dlfcn.h"
40 #endif
41
42 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
43
44 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
45  * better not change. */
46
47 struct call_frame {
48 #ifndef LISP_FEATURE_ALPHA
49         struct call_frame *old_cont;
50 #else
51         u32 old_cont;
52 #endif
53         lispobj saved_lra;
54         lispobj code;
55         lispobj other_state[5];
56 };
57
58 struct call_info {
59 #ifndef LISP_FEATURE_ALPHA
60     struct call_frame *frame;
61 #else
62     u32 frame;
63 #endif
64     int interrupted;
65 #ifndef LISP_FEATURE_ALPHA
66     struct code *code;
67 #else
68     u32 code;
69 #endif
70     lispobj lra;
71     int pc; /* Note: this is the trace file offset, not the actual pc. */
72 };
73
74 #define HEADER_LENGTH(header) ((header)>>8)
75
76 static int previous_info(struct call_info *info);
77
78 static struct code *
79 code_pointer(lispobj object)
80 {
81     lispobj *headerp, header;
82     int type, len;
83
84     headerp = (lispobj *) native_pointer(object);
85     header = *headerp;
86     type = widetag_of(header);
87
88     switch (type) {
89         case CODE_HEADER_WIDETAG:
90             break;
91         case RETURN_PC_HEADER_WIDETAG:
92         case SIMPLE_FUN_HEADER_WIDETAG:
93             len = HEADER_LENGTH(header);
94             if (len == 0)
95                 headerp = NULL;
96             else
97                 headerp -= len;
98             break;
99         default:
100             headerp = NULL;
101     }
102
103     return (struct code *) headerp;
104 }
105
106 static boolean
107 cs_valid_pointer_p(struct call_frame *pointer)
108 {
109     struct thread *thread=arch_os_get_current_thread();
110     return (((char *) thread->control_stack_start <= (char *) pointer) &&
111             ((char *) pointer < (char *) current_control_stack_pointer));
112 }
113
114 static void
115 call_info_from_lisp_state(struct call_info *info)
116 {
117     info->frame = (struct call_frame *)current_control_frame_pointer;
118     info->interrupted = 0;
119     info->code = NULL;
120     info->lra = 0;
121     info->pc = 0;
122
123     previous_info(info);
124 }
125
126 static void
127 call_info_from_context(struct call_info *info, os_context_t *context)
128 {
129     unsigned long pc;
130
131     info->interrupted = 1;
132     if (lowtag_of(*os_context_register_addr(context, reg_CODE))
133         == FUN_POINTER_LOWTAG) {
134         /* We tried to call a function, but crapped out before $CODE could
135          * be fixed up. Probably an undefined function. */
136         info->frame =
137             (struct call_frame *)(unsigned long)
138                 (*os_context_register_addr(context, reg_OCFP));
139         info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
140         info->code = code_pointer(info->lra);
141         pc = (unsigned long)native_pointer(info->lra);
142     }
143     else {
144         info->frame =
145             (struct call_frame *)(unsigned long)
146                 (*os_context_register_addr(context, reg_CFP));
147         info->code =
148             code_pointer(*os_context_register_addr(context, reg_CODE));
149         info->lra = NIL;
150         pc = *os_context_pc_addr(context);
151     }
152     if (info->code != NULL)
153         info->pc = pc - (unsigned long) info->code -
154 #ifndef LISP_FEATURE_ALPHA
155             (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
156 #else
157             (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
158 #endif
159     else
160         info->pc = 0;
161 }
162
163 static int
164 previous_info(struct call_info *info)
165 {
166     struct call_frame *this_frame;
167     struct thread *thread=arch_os_get_current_thread();
168     int free_ici;
169
170     if (!cs_valid_pointer_p(info->frame)) {
171         printf("Bogus callee value (0x%08lx).\n", (unsigned long)info->frame);
172         return 0;
173     }
174
175     this_frame = info->frame;
176     info->lra = this_frame->saved_lra;
177     info->frame = this_frame->old_cont;
178     info->interrupted = 0;
179
180     if (info->frame == NULL || info->frame == this_frame)
181         return 0;
182
183     if (info->lra == NIL) {
184         /* We were interrupted. Find the correct signal context. */
185         free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
186         while (free_ici-- > 0) {
187             os_context_t *context =
188                 thread->interrupt_contexts[free_ici];
189             if ((struct call_frame *)(unsigned long)
190                     (*os_context_register_addr(context, reg_CFP))
191                 == info->frame) {
192                 call_info_from_context(info, context);
193                 break;
194             }
195         }
196     }
197     else {
198         info->code = code_pointer(info->lra);
199         if (info->code != NULL)
200             info->pc = (unsigned long)native_pointer(info->lra) -
201                 (unsigned long)info->code -
202 #ifndef LISP_FEATURE_ALPHA
203                 (HEADER_LENGTH(info->code->header) * sizeof(lispobj));
204 #else
205                 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
206 #endif
207         else
208             info->pc = 0;
209     }
210
211     return 1;
212 }
213
214 void
215 backtrace(int nframes)
216 {
217     struct call_info info;
218
219     call_info_from_lisp_state(&info);
220
221     do {
222         printf("<Frame 0x%08lx%s, ", (unsigned long) info.frame,
223                 info.interrupted ? " [interrupted]" : "");
224
225         if (info.code != (struct code *) 0) {
226             lispobj function;
227
228             printf("CODE: 0x%08lX, ", (unsigned long) info.code | OTHER_POINTER_LOWTAG);
229
230 #ifndef LISP_FEATURE_ALPHA
231             function = info.code->entry_points;
232 #else
233             function = ((struct code *)info.code)->entry_points;
234 #endif
235             while (function != NIL) {
236                 struct simple_fun *header;
237                 lispobj name;
238
239                 header = (struct simple_fun *) native_pointer(function);
240                 name = header->name;
241
242                 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
243                     lispobj *object;
244
245                     object = (lispobj *) native_pointer(name);
246
247                     if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
248                         struct symbol *symbol;
249
250                         symbol = (struct symbol *) object;
251                         object = (lispobj *) native_pointer(symbol->name);
252                     }
253                     if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
254                         struct vector *string;
255
256                         string = (struct vector *) object;
257                         printf("%s, ", (char *) string->data);
258                     } else
259                         /* FIXME: broken from (VECTOR NIL) */
260                         printf("(Not simple string??\?), ");
261                 } else
262                     printf("(Not other pointer??\?), ");
263
264
265                 function = header->next;
266             }
267         }
268         else
269             printf("CODE: ???, ");
270
271         if (info.lra != NIL)
272             printf("LRA: 0x%08lx, ", (unsigned long)info.lra);
273         else
274             printf("<no LRA>, ");
275
276         if (info.pc)
277             printf("PC: 0x%x>\n", info.pc);
278         else
279             printf("PC: ??\?>\n");
280
281     } while (--nframes > 0 && previous_info(&info));
282 }
283
284 #else
285
286 static int
287 altstack_pointer_p (void *p) {
288     char* stack_start = ((char *) arch_os_get_current_thread())
289         + dynamic_values_bytes;
290     char* stack_end = stack_start + 32*SIGSTKSZ;
291
292     return (p > stack_start && p <= stack_end);
293 }
294
295 static int
296 stack_pointer_p (void *p)
297 {
298   /* we are using sizeof(long) here, because that is the right value on both
299    * x86 and x86-64.  (But note that false positives would not cause much harm
300    * given the heuristical nature of x86_call_context.) */
301   unsigned long stack_alignment = sizeof(long);
302
303   return (altstack_pointer_p(p)
304           || (p < (void *) arch_os_get_current_thread()->control_stack_end
305               && (p > (void *) &p || altstack_pointer_p(&p))
306               && (((unsigned long) p) & (stack_alignment-1)) == 0));
307 }
308
309 static int
310 ra_pointer_p (void *ra)
311 {
312   /* the check against 4096 is still a mystery to everyone interviewed about
313    * it, but recent changes to sb-sprof seem to suggest that such values
314    * do occur sometimes. */
315   return ((unsigned long) ra) > 4096 && !stack_pointer_p (ra);
316 }
317
318 static int
319 x86_call_context (void *fp, void **ra, void **ocfp)
320 {
321   void *lisp_ocfp;
322   void *lisp_ra;
323   void *c_ocfp;
324   void *c_ra;
325   int lisp_valid_p, c_valid_p;
326
327   if (!stack_pointer_p(fp))
328     return 0;
329
330   c_ocfp    = *((void **) fp);
331   c_ra      = *((void **) fp + 1);
332   lisp_ocfp = *((void **) fp - 1);
333   lisp_ra   = *((void **) fp - 2);
334
335   lisp_valid_p = (lisp_ocfp > fp
336                   && stack_pointer_p(lisp_ocfp)
337                   && ra_pointer_p(lisp_ra));
338   c_valid_p = (c_ocfp > fp
339                && stack_pointer_p(c_ocfp)
340                && ra_pointer_p(c_ra));
341
342   if (lisp_valid_p && c_valid_p) {
343     void *lisp_path_fp;
344     void *c_path_fp;
345     void *dummy;
346
347     int lisp_path_p = x86_call_context(lisp_ocfp, &lisp_path_fp, &dummy);
348     int c_path_p = x86_call_context(c_ocfp, &c_path_fp, &dummy);
349
350     if (lisp_path_p && c_path_p) {
351 #if defined __FreeBSD__ && __FreeBSD_version > 400000
352       if (lisp_ocfp > c_ocfp)
353         *ra = lisp_ra, *ocfp = lisp_ocfp;
354       else
355         *ra = c_ra, *ocfp = c_ocfp;
356 #else
357       *ra = lisp_ra, *ocfp = lisp_ocfp;
358 #endif
359     }
360     else if (lisp_path_p)
361       *ra = lisp_ra, *ocfp = lisp_ocfp;
362     else if (c_path_p)
363       *ra = c_ra, *ocfp = c_ocfp;
364     else
365       return 0;
366   }
367   else if (lisp_valid_p)
368     *ra = lisp_ra, *ocfp = lisp_ocfp;
369   else if (c_valid_p)
370     *ra = c_ra, *ocfp = c_ocfp;
371   else
372     return 0;
373
374   return 1;
375 }
376
377 struct compiled_debug_fun *
378 debug_function_from_pc (struct code* code, void *pc)
379 {
380   unsigned long code_header_len = sizeof(lispobj) * HeaderValue(code->header);
381   unsigned long offset
382     = (unsigned long) pc - (unsigned long) code - code_header_len;
383   struct compiled_debug_fun *df;
384   struct compiled_debug_info *di;
385   struct vector *v;
386   int i, len;
387
388   if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
389     return 0;
390
391   di = (struct compiled_debug_info *) native_pointer(code->debug_info);
392   v = (struct vector *) native_pointer(di->fun_map);
393   len = fixnum_value(v->length);
394   df = (struct compiled_debug_fun *) native_pointer(v->data[0]);
395
396   if (len == 1)
397     return df;
398
399   for (i = 1;; i += 2) {
400     unsigned next_pc;
401
402     if (i == len)
403       return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
404
405     if (offset >= fixnum_value(df->elsewhere_pc)) {
406       struct compiled_debug_fun *p
407         = ((struct compiled_debug_fun *) native_pointer(v->data[i + 1]));
408       next_pc = fixnum_value(p->elsewhere_pc);
409     } else
410       next_pc = fixnum_value(v->data[i]);
411
412     if (offset < next_pc)
413       return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
414   }
415
416   return NULL;
417 }
418
419 static void
420 sbcl_putwc(wchar_t c, FILE *file)
421 {
422 #ifdef LISP_FEATURE_OS_PROVIDES_PUTWC
423     putwc(c, file);
424 #else
425     if (c < 256) {
426         fputc(c, file);
427     } else {
428         fputc('?', file);
429     }
430 #endif
431 }
432
433 static void
434 print_string (lispobj *object)
435 {
436   int tag = widetag_of(*object);
437   struct vector *vector = (struct vector *) object;
438
439 #define doit(TYPE)                              \
440   do {                                          \
441     int i;                                      \
442     int n = fixnum_value(vector->length);       \
443     TYPE *data = (TYPE *) vector->data;         \
444     for (i = 0; i < n; i++) {                   \
445       wchar_t c = (wchar_t) data[i];            \
446       if (c == '\\' || c == '"')                \
447         putchar('\\');                          \
448       sbcl_putwc(c, stdout);                    \
449     }                                           \
450   } while (0)
451
452   switch (tag) {
453   case SIMPLE_BASE_STRING_WIDETAG:
454     doit(unsigned char);
455     break;
456 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
457   case SIMPLE_CHARACTER_STRING_WIDETAG:
458     doit(unsigned int);
459     break;
460 #endif
461   default:
462     printf("<??? type %d>", tag);
463   }
464 #undef doit
465 }
466
467 static void
468 print_entry_name (lispobj name)
469 {
470   if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
471     putchar('(');
472     while (name != NIL) {
473       struct cons *cons = (struct cons *) native_pointer(name);
474       print_entry_name(cons->car);
475       name = cons->cdr;
476       if (name != NIL)
477         putchar(' ');
478     }
479     putchar(')');
480   } else if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
481     lispobj *object = (lispobj *) native_pointer(name);
482     if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
483       struct symbol *symbol = (struct symbol *) object;
484       if (symbol->package != NIL) {
485         struct package *pkg
486           = (struct package *) native_pointer(symbol->package);
487         lispobj pkg_name = pkg->_name;
488         print_string(native_pointer(pkg_name));
489         fputs("::", stdout);
490       }
491       print_string(native_pointer(symbol->name));
492     } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
493          putchar('"');
494          print_string(object);
495          putchar('"');
496 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
497       } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
498          putchar('"');
499          print_string(object);
500          putchar('"');
501 #endif
502     } else {
503       printf("<??? type %d>", (int) widetag_of(*object));
504     }
505   } else {
506     printf("<??? lowtag %d>", (int) lowtag_of(name));
507   }
508 }
509
510 static void
511 print_entry_points (struct code *code)
512 {
513   lispobj function = code->entry_points;
514
515   while (function != NIL) {
516     struct simple_fun *header = (struct simple_fun *) native_pointer(function);
517     print_entry_name(header->name);
518
519     function = header->next;
520     if (function != NIL)
521       printf (", ");
522   }
523 }
524
525 /* This function has been split from backtrace() to enable Lisp
526  * backtraces from gdb with call backtrace_from_fp(...). Useful for
527  * example when debugging threading deadlocks.
528  */
529 void
530 backtrace_from_fp(void *fp, int nframes)
531 {
532   int i;
533
534   for (i = 0; i < nframes; ++i) {
535     lispobj *p;
536     void *ra;
537     void *next_fp;
538
539     if (!x86_call_context(fp, &ra, &next_fp))
540       break;
541
542     printf("%4d: ", i);
543
544     p = (lispobj *) component_ptr_from_pc((lispobj *) ra);
545     if (p) {
546       struct code *cp = (struct code *) p;
547       struct compiled_debug_fun *df = debug_function_from_pc(cp, ra);
548       if (df)
549         print_entry_name(df->name);
550       else
551         print_entry_points(cp);
552     } else {
553 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
554         Dl_info info;
555         if (dladdr(ra, &info)) {
556             printf("Foreign function %s, fp = 0x%lx, ra = 0x%lx",
557                    info.dli_sname,
558                    (unsigned long) next_fp,
559                    (unsigned long) ra);
560         } else
561 #endif
562         printf("Foreign fp = 0x%lx, ra = 0x%lx",
563                (unsigned long) next_fp,
564                (unsigned long) ra);
565     }
566
567     putchar('\n');
568     fp = next_fp;
569   }
570 }
571
572 void
573 backtrace(int nframes)
574 {
575   void *fp;
576   int i;
577
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));
582 #else
583 #error "How did we get here?"
584 #endif
585
586   backtrace_from_fp(fp, nframes);
587 }
588
589 #endif