1.1.13: will be tagged as "sbcl-1.1.13"
[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 /* needed if we want dladdr() and Dl_Info from glibc's dlfcn.h */
17 #define _GNU_SOURCE
18
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 #ifdef LISP_FEATURE_GENCGC
28 #include <wchar.h>
29 #include "arch.h"
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"
34 #endif
35 #include "genesis/static-symbols.h"
36 #include "genesis/primitive-objects.h"
37 #include "thread.h"
38
39 #ifdef LISP_FEATURE_OS_PROVIDES_DLADDR
40 # include <dlfcn.h>
41 #endif
42
43 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
44
45 /* KLUDGE: Sigh ... I know what the call frame looks like and it had
46  * better not change. */
47
48 struct call_frame {
49 #ifndef LISP_FEATURE_ALPHA
50         struct call_frame *old_cont;
51 #else
52         u32 old_cont;
53 #endif
54         lispobj saved_lra;
55         lispobj code;
56         lispobj other_state[5];
57 };
58
59 struct call_info {
60 #ifndef LISP_FEATURE_ALPHA
61     struct call_frame *frame;
62 #else
63     u32 frame;
64 #endif
65     int interrupted;
66 #ifndef LISP_FEATURE_ALPHA
67     struct code *code;
68 #else
69     u32 code;
70 #endif
71     lispobj lra;
72     int pc; /* Note: this is the trace file offset, not the actual pc. */
73 };
74
75 #define HEADER_LENGTH(header) ((header)>>8)
76
77 static int previous_info(struct call_info *info);
78
79 static struct code *
80 code_pointer(lispobj object)
81 {
82     lispobj *headerp, header;
83     int type, len;
84
85     headerp = (lispobj *) native_pointer(object);
86     header = *headerp;
87     type = widetag_of(header);
88
89     switch (type) {
90         case CODE_HEADER_WIDETAG:
91             break;
92         case RETURN_PC_HEADER_WIDETAG:
93         case SIMPLE_FUN_HEADER_WIDETAG:
94             len = HEADER_LENGTH(header);
95             if (len == 0)
96                 headerp = NULL;
97             else
98                 headerp -= len;
99             break;
100         default:
101             headerp = NULL;
102     }
103
104     return (struct code *) headerp;
105 }
106
107 static boolean
108 cs_valid_pointer_p(struct call_frame *pointer)
109 {
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)));
113 }
114
115 static void
116 call_info_from_lisp_state(struct call_info *info)
117 {
118     info->frame = (struct call_frame *)access_control_frame_pointer(arch_os_get_current_thread());
119     info->interrupted = 0;
120     info->code = NULL;
121     info->lra = 0;
122     info->pc = 0;
123
124     previous_info(info);
125 }
126
127 static void
128 call_info_from_context(struct call_info *info, os_context_t *context)
129 {
130     uword_t pc;
131
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. */
137         info->frame =
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);
143     }
144     else {
145         info->frame =
146             (struct call_frame *)(uword_t)
147                 (*os_context_register_addr(context, reg_CFP));
148         info->code =
149             code_pointer(*os_context_register_addr(context, reg_CODE));
150         info->lra = NIL;
151         pc = *os_context_pc_addr(context);
152     }
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));
157 #else
158             (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
159 #endif
160     else
161         info->pc = 0;
162 }
163
164 static int
165 previous_info(struct call_info *info)
166 {
167     struct call_frame *this_frame;
168     struct thread *thread=arch_os_get_current_thread();
169     int free_ici;
170
171     if (!cs_valid_pointer_p(info->frame)) {
172         printf("Bogus callee value (0x%08lx).\n", (uword_t)info->frame);
173         return 0;
174     }
175
176     this_frame = info->frame;
177     info->lra = this_frame->saved_lra;
178     info->frame = this_frame->old_cont;
179     info->interrupted = 0;
180
181     if (info->frame == NULL || info->frame == this_frame)
182         return 0;
183
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))
192                 == info->frame) {
193                 call_info_from_context(info, context);
194                 break;
195             }
196         }
197     }
198     else {
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));
205 #else
206                 (HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
207 #endif
208         else
209             info->pc = 0;
210     }
211
212     return 1;
213 }
214
215 void
216 lisp_backtrace(int nframes)
217 {
218     struct call_info info;
219
220     call_info_from_lisp_state(&info);
221
222     do {
223         printf("<Frame 0x%08lx%s, ", (uword_t) info.frame,
224                 info.interrupted ? " [interrupted]" : "");
225
226         if (info.code != (struct code *) 0) {
227             lispobj function;
228
229             printf("CODE: 0x%08lX, ", (uword_t) info.code | OTHER_POINTER_LOWTAG);
230
231 #ifndef LISP_FEATURE_ALPHA
232             function = info.code->entry_points;
233 #else
234             function = ((struct code *)info.code)->entry_points;
235 #endif
236             while (function != NIL) {
237                 struct simple_fun *header;
238                 lispobj name;
239
240                 header = (struct simple_fun *) native_pointer(function);
241                 name = header->name;
242
243                 if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
244                     lispobj *object;
245
246                     object = (lispobj *) native_pointer(name);
247
248                     if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
249                         struct symbol *symbol;
250
251                         symbol = (struct symbol *) object;
252                         object = (lispobj *) native_pointer(symbol->name);
253                     }
254                     if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
255                         struct vector *string;
256
257                         string = (struct vector *) object;
258                         printf("%s, ", (char *) string->data);
259                     } else
260                         /* FIXME: broken from (VECTOR NIL) */
261                         printf("(Not simple string??\?), ");
262                 } else
263                     printf("(Not other pointer??\?), ");
264
265
266                 function = header->next;
267             }
268         }
269         else
270             printf("CODE: ???, ");
271
272         if (info.lra != NIL)
273             printf("LRA: 0x%08lx, ", (uword_t)info.lra);
274         else
275             printf("<no LRA>, ");
276
277         if (info.pc)
278             printf("PC: 0x%x>\n", info.pc);
279         else
280             printf("PC: ??\?>\n");
281
282     } while (--nframes > 0 && previous_info(&info));
283 }
284
285 #else
286
287 static int
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;
292
293     return (p > stack_start && p <= stack_end);
294 #else
295     /* Win32 doesn't do altstack */
296     return 0;
297 #endif
298 }
299
300 static int
301 stack_pointer_p (void *p)
302 {
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*);
307
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));
312 }
313
314 static int
315 ra_pointer_p (void *ra)
316 {
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);
321 }
322
323 static int
324 x86_call_context (void *fp, void **ra, void **ocfp)
325 {
326   void *c_ocfp;
327   void *c_ra;
328   int c_valid_p;
329
330   if (!stack_pointer_p(fp))
331     return 0;
332
333   c_ocfp    = *((void **) fp);
334   c_ra      = *((void **) fp + 1);
335
336   c_valid_p = (c_ocfp > fp
337                && stack_pointer_p(c_ocfp)
338                && ra_pointer_p(c_ra));
339
340   if (c_valid_p)
341     *ra = c_ra, *ocfp = c_ocfp;
342   else
343     return 0;
344
345   return 1;
346 }
347
348 struct compiled_debug_fun *
349 debug_function_from_pc (struct code* code, void *pc)
350 {
351   uword_t code_header_len = sizeof(lispobj) * HeaderValue(code->header);
352   uword_t offset
353     = (uword_t) pc - (uword_t) code - code_header_len;
354   struct compiled_debug_fun *df;
355   struct compiled_debug_info *di;
356   struct vector *v;
357   int i, len;
358
359   if (lowtag_of(code->debug_info) != INSTANCE_POINTER_LOWTAG)
360     return 0;
361
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]);
366
367   if (len == 1)
368     return df;
369
370   for (i = 1;; i += 2) {
371     unsigned next_pc;
372
373     if (i == len)
374       return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
375
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);
380     } else
381       next_pc = fixnum_value(v->data[i]);
382
383     if (offset < next_pc)
384       return ((struct compiled_debug_fun *) native_pointer(v->data[i - 1]));
385   }
386
387   return NULL;
388 }
389
390 static void
391 sbcl_putwc(wchar_t c, FILE *file)
392 {
393 #ifdef LISP_FEATURE_OS_PROVIDES_PUTWC
394     putwc(c, file);
395 #else
396     if (c < 256) {
397         fputc(c, file);
398     } else {
399         fputc('?', file);
400     }
401 #endif
402 }
403
404 static void
405 print_string (lispobj *object)
406 {
407   int tag = widetag_of(*object);
408   struct vector *vector = (struct vector *) object;
409
410 #define doit(TYPE)                              \
411   do {                                          \
412     int i;                                      \
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 == '"')                \
418         putchar('\\');                          \
419       sbcl_putwc(c, stdout);                    \
420     }                                           \
421   } while (0)
422
423   switch (tag) {
424   case SIMPLE_BASE_STRING_WIDETAG:
425     doit(unsigned char);
426     break;
427 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
428   case SIMPLE_CHARACTER_STRING_WIDETAG:
429     doit(unsigned int);
430     break;
431 #endif
432   default:
433     printf("<??? type %d>", tag);
434   }
435 #undef doit
436 }
437
438 static void
439 print_entry_name (lispobj name)
440 {
441   if (lowtag_of (name) == LIST_POINTER_LOWTAG) {
442     putchar('(');
443     while (name != NIL) {
444       struct cons *cons = (struct cons *) native_pointer(name);
445       print_entry_name(cons->car);
446       name = cons->cdr;
447       if (name != NIL)
448         putchar(' ');
449     }
450     putchar(')');
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) {
456         struct package *pkg
457           = (struct package *) native_pointer(symbol->package);
458         lispobj pkg_name = pkg->_name;
459         print_string(native_pointer(pkg_name));
460         fputs("::", stdout);
461       }
462       print_string(native_pointer(symbol->name));
463     } else if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
464          putchar('"');
465          print_string(object);
466          putchar('"');
467 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
468       } else if (widetag_of(*object) == SIMPLE_CHARACTER_STRING_WIDETAG) {
469          putchar('"');
470          print_string(object);
471          putchar('"');
472 #endif
473     } else {
474       printf("<??? type %d>", (int) widetag_of(*object));
475     }
476   } else {
477     printf("<??? lowtag %d>", (int) lowtag_of(name));
478   }
479 }
480
481 static void
482 print_entry_points (struct code *code)
483 {
484   lispobj function = code->entry_points;
485
486   while (function != NIL) {
487     struct simple_fun *header = (struct simple_fun *) native_pointer(function);
488     print_entry_name(header->name);
489
490     function = header->next;
491     if (function != NIL)
492       printf (", ");
493   }
494 }
495
496 void
497 describe_thread_state(void)
498 {
499     sigset_t mask;
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));
510 #endif
511 #endif
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");
521 #endif
522     printf("Pending handler = %p\n", data->pending_handler);
523 }
524
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.
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                    (uword_t) next_fp,
559                    (uword_t) ra);
560         } else
561 #endif
562         printf("Foreign fp = 0x%p, ra = 0x%p",
563                (void*) next_fp,
564                (void*) ra);
565     }
566
567     putchar('\n');
568     fp = next_fp;
569   }
570 }
571
572 void
573 lisp_backtrace(int nframes)
574 {
575   void *fp;
576
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));
581 #else
582 #error "How did we get here?"
583 #endif
584
585   backtrace_from_fp(fp, nframes);
586 }
587
588 #endif