1 /* code for low-level debugging/diagnostic output */
4 * This software is part of the SBCL system. See the README file for
7 * This software is derived from the CMU CL system, which was
8 * written at Carnegie Mellon University and released into the
9 * public domain. The software is in the public domain and is
10 * provided with absolutely no warranty. See the COPYING and CREDITS
11 * files for more information.
16 * Some of the code in here (the various
17 * foo_slots[], at least) is deeply broken, depending on guessing
18 * already out-of-date values instead of getting them from sbcl.h.
28 #include "thread.h" /* genesis/primitive-objects.h needs this */
32 /* FSHOW and odxprint provide debugging output for low-level information
33 * (signal handling, exceptions, safepoints) which is hard to debug by
36 * If enabled at all, environment variables control whether calls of the
37 * form odxprint(name, ...) are enabled at run-time, e.g. using
38 * SBCL_DYNDEBUG="fshow fshow_signal safepoints".
40 * In the case of FSHOW and FSHOW_SIGNAL, old-style code from runtime.h
41 * can also be used to enable or disable these more aggressively.
44 struct dyndebug_config dyndebug_config = {
45 QSHOW == 2, QSHOW_SIGNALS == 2
51 #define DYNDEBUG_NFLAGS (sizeof(struct dyndebug_config) / sizeof(int))
52 #define dyndebug_init1(lowercase, uppercase) \
54 int *ptr = &dyndebug_config.dyndebug_##lowercase; \
56 names[n] = #lowercase; \
57 char *val = getenv("SBCL_DYNDEBUG__" uppercase); \
58 *ptr = val && strlen(val); \
62 char *names[DYNDEBUG_NFLAGS];
63 int *ptrs[DYNDEBUG_NFLAGS];
65 dyndebug_init1(fshow, "FSHOW");
66 dyndebug_init1(fshow_signal, "FSHOW_SIGNAL");
67 dyndebug_init1(gencgc_verbose, "GENCGC_VERBOSE");
68 dyndebug_init1(safepoints, "SAFEPOINTS");
69 dyndebug_init1(seh, "SEH");
70 dyndebug_init1(misc, "MISC");
71 dyndebug_init1(pagefaults, "PAGEFAULTS");
73 int n_output_flags = n;
74 dyndebug_init1(backtrace_when_lost, "BACKTRACE_WHEN_LOST");
75 dyndebug_init1(sleep_when_lost, "SLEEP_WHEN_LOST");
77 if (n != DYNDEBUG_NFLAGS)
78 fprintf(stderr, "Bug in dyndebug_init\n");
80 #if defined(LISP_FEATURE_GENCGC)
81 gencgc_verbose = dyndebug_config.dyndebug_gencgc_verbose;
84 char *featurelist = getenv("SBCL_DYNDEBUG");
87 featurelist = strdup(featurelist);
88 char *ptr = featurelist;
90 char *token = strtok(ptr, " ");
93 if (!strcmp(token, "all"))
94 for (i = 0; i < n_output_flags; i++)
97 for (i = 0; i < DYNDEBUG_NFLAGS; i++)
98 if (!strcmp(token, names[i])) {
102 if (i == DYNDEBUG_NFLAGS) {
103 fprintf(stderr, "No such dyndebug flag: `%s'\n", token);
111 fprintf(stderr, "Valid flags are:\n");
112 fprintf(stderr, " all ;enables all of the following:\n");
114 for (i = 0; i < DYNDEBUG_NFLAGS; i++) {
115 if (i == n_output_flags)
116 fprintf(stderr, "Additional options:\n");
117 fprintf(stderr, " %s\n", names[i]);
122 #undef dyndebug_init1
123 #undef DYNDEBUG_NFLAGS
126 /* Temporarily, odxprint merely performs the equivalent of a traditional
127 * FSHOW call, i.e. it merely formats to stderr. Ultimately, it should
128 * be restored to its full win32 branch functionality, where output to a
129 * file or to the debugger can be selected at runtime. */
131 void vodxprint_fun(const char *, va_list);
134 odxprint_fun(const char *fmt, ...)
138 vodxprint_fun(fmt, args);
143 vodxprint_fun(const char *fmt, va_list args)
145 #ifdef LISP_FEATURE_WIN32
146 DWORD lastError = GetLastError();
148 int original_errno = errno;
155 #ifdef LISP_FEATURE_SB_THREAD
156 struct thread *arch_os_get_current_thread(void);
157 struct thread *self = arch_os_get_current_thread();
158 void *pth = self ? (void *) self->os_thread : 0;
159 snprintf(buf, sizeof(buf), "[%p/%p] ", self, pth);
163 vsnprintf(buf + n, sizeof(buf) - n - 1, fmt, args);
164 /* buf is now zero-terminated (even in case of overflow).
165 * Our caller took care of the newline (if any) through `fmt'. */
167 /* A sufficiently POSIXy implementation of stdio will provide
168 * per-FILE locking, as defined in the spec for flockfile. At least
169 * glibc complies with this. Hence we do not need to perform
170 * locking ourselves here. (Should it turn out, of course, that
171 * other libraries opt for speed rather than safety, we need to
172 * revisit this decision.) */
175 #ifdef LISP_FEATURE_WIN32
176 /* stdio's stderr is line-bufferred, i.e. \n ought to flush it.
177 * Unfortunately, MinGW does not behave the way I would expect it
178 * to. Let's be safe: */
184 #ifdef LISP_FEATURE_WIN32
185 SetLastError(lastError);
187 errno = original_errno;
191 /* Translate the rather awkward syntax
192 * FSHOW((stderr, "xyz"))
193 * into the new and cleaner
195 * If we were willing to clean up all existing call sites, we could remove
196 * this wrapper function. (This is a function, because I don't know how to
197 * strip the extra parens in a macro.) */
199 fshow_fun(void __attribute__((__unused__)) *ignored,
205 vodxprint_fun(fmt, args);
209 /* This file can be skipped if we're not supporting LDB. */
210 #if defined(LISP_FEATURE_SB_LDB)
215 #ifdef LISP_FEATURE_GENCGC
216 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
218 #if defined(LISP_FEATURE_WIN32)
219 # include "win32-thread-private-events.h" /* genesis/thread.h needs this */
221 #include "genesis/static-symbols.h"
222 #include "genesis/primitive-objects.h"
223 #include "genesis/static-symbols.h"
224 #include "genesis/tagnames.h"
226 static int max_lines = 20, cur_lines = 0;
227 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
228 static int max_length = 5;
229 static boolean dont_descend = 0, skip_newline = 0;
230 static int cur_clock = 0;
232 static void print_obj(char *prefix, lispobj obj);
234 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
236 static void indent(int in)
238 static char *spaces = " ";
241 fputs(spaces, stdout);
245 fputs(spaces + 64 - in, stdout);
248 static boolean continue_p(boolean newline)
252 if (cur_depth >= max_depth || dont_descend)
261 if (cur_lines >= max_lines) {
262 printf("More? [y] ");
265 if (fgets(buffer, sizeof(buffer), stdin)) {
266 if (buffer[0] == 'n' || buffer[0] == 'N')
271 printf("\nUnable to read response, assuming y.\n");
280 static void newline(char *label)
284 fputs(label, stdout);
286 indent(cur_depth * 2);
290 static void print_unknown(lispobj obj)
292 printf("unknown object: %p", (void *)obj);
295 static void brief_fixnum(lispobj obj)
297 /* KLUDGE: Rather than update the tables in print_obj(), we
298 declare all fixnum-or-unknown tags to be fixnums and sort it
299 out here with a guard clause. */
300 if (!fixnump(obj)) return print_unknown(obj);
302 #ifndef LISP_FEATURE_ALPHA
303 printf("%ld", ((long)obj)>>2);
305 printf("%d", ((s32)obj)>>2);
309 static void print_fixnum(lispobj obj)
311 /* KLUDGE: Rather than update the tables in print_obj(), we
312 declare all fixnum-or-unknown tags to be fixnums and sort it
313 out here with a guard clause. */
314 if (!fixnump(obj)) return print_unknown(obj);
316 #ifndef LISP_FEATURE_ALPHA
317 printf(": %ld", ((long)obj)>>2);
319 printf(": %d", ((s32)obj)>>2);
323 static void brief_otherimm(lispobj obj)
328 type = widetag_of(obj);
330 case CHARACTER_WIDETAG:
337 printf("#\\Newline");
340 printf("#\\Backspace");
346 strcpy(buffer, "#\\");
348 strcat(buffer, "m-");
352 strcat(buffer, "c-");
355 printf("%s%c", buffer, c);
360 case UNBOUND_MARKER_WIDETAG:
361 printf("<unbound marker>");
365 printf("%s", widetag_names[type >> 2]);
370 static void print_otherimm(lispobj obj)
372 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
374 switch (widetag_of(obj)) {
375 case CHARACTER_WIDETAG:
381 case UNBOUND_MARKER_WIDETAG:
385 printf(": data=%ld", (long) (obj>>8)&0xffffff);
390 static void brief_list(lispobj obj)
395 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
396 printf("(invalid Lisp-level address)");
401 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
402 struct cons *cons = (struct cons *)native_pointer(obj);
406 if (++length >= max_length) {
411 print_obj("", cons->car);
425 static void print_list(lispobj obj)
427 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
428 printf("(invalid address)");
429 } else if (obj == NIL) {
432 struct cons *cons = (struct cons *)native_pointer(obj);
434 print_obj("car: ", cons->car);
435 print_obj("cdr: ", cons->cdr);
439 static void brief_struct(lispobj obj)
441 struct instance *instance = (struct instance *)native_pointer(obj);
442 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
443 printf("(invalid address)");
445 printf("#<ptr to 0x%08lx instance>",
446 (unsigned long) instance->slots[0]);
450 static void print_struct(lispobj obj)
452 struct instance *instance = (struct instance *)native_pointer(obj);
455 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
456 printf("(invalid address)");
458 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
459 for (i = 1; i < HeaderValue(instance->header); i++) {
460 sprintf(buffer, "slot %d: ", i);
461 print_obj(buffer, instance->slots[i]);
466 static void brief_otherptr(lispobj obj)
468 lispobj *ptr, header;
470 struct symbol *symbol;
471 struct vector *vector;
474 ptr = (lispobj *) native_pointer(obj);
476 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
477 printf("(invalid address)");
482 type = widetag_of(header);
484 case SYMBOL_HEADER_WIDETAG:
485 symbol = (struct symbol *)ptr;
486 vector = (struct vector *)native_pointer(symbol->name);
487 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
494 case SIMPLE_BASE_STRING_WIDETAG:
495 vector = (struct vector *)ptr;
497 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
507 brief_otherimm(header);
512 static void print_slots(char **slots, int count, lispobj *ptr)
514 while (count-- > 0) {
516 print_obj(*slots++, *ptr++);
518 print_obj("???: ", *ptr++);
523 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
524 * perhaps be generated automatically by GENESIS as part of
526 static char *symbol_slots[] = {"value: ", "hash: ",
527 "plist: ", "name: ", "package: ",
528 #ifdef LISP_FEATURE_SB_THREAD
532 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
533 static char *complex_slots[] = {"real: ", "imag: ", NULL};
534 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
535 static char *fn_slots[] = {
536 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
537 static char *closure_slots[] = {"fn: ", NULL};
538 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
539 static char *weak_pointer_slots[] = {"value: ", NULL};
540 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
541 static char *value_cell_slots[] = {"value: ", NULL};
543 static void print_otherptr(lispobj obj)
545 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
546 printf("(invalid address)");
548 #ifndef LISP_FEATURE_ALPHA
550 unsigned long header;
551 unsigned long length;
557 int count, type, index;
558 char *cptr, buffer[16];
560 ptr = (lispobj*) native_pointer(obj);
562 printf(" (NULL Pointer)");
567 length = fixnum_value(*ptr);
568 count = HeaderValue(header);
569 type = widetag_of(header);
571 print_obj("header: ", header);
572 if (!other_immediate_lowtag_p(header)) {
574 printf("(invalid header object)");
584 printf("%08lx", (unsigned long) *--ptr);
588 print_slots(ratio_slots, count, ptr);
591 case COMPLEX_WIDETAG:
592 print_slots(complex_slots, count, ptr);
595 case SYMBOL_HEADER_WIDETAG:
596 print_slots(symbol_slots, count, ptr);
599 #if N_WORD_BITS == 32
600 case SINGLE_FLOAT_WIDETAG:
602 printf("%g", ((struct single_float *)native_pointer(obj))->value);
605 case DOUBLE_FLOAT_WIDETAG:
607 printf("%g", ((struct double_float *)native_pointer(obj))->value);
610 #ifdef LONG_FLOAT_WIDETAG
611 case LONG_FLOAT_WIDETAG:
613 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
617 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
618 case COMPLEX_SINGLE_FLOAT_WIDETAG:
620 #ifdef LISP_FEATURE_X86_64
621 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
623 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
626 #ifdef LISP_FEATURE_X86_64
627 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
629 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
634 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
635 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
637 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
639 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
643 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
644 case COMPLEX_LONG_FLOAT_WIDETAG:
646 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
648 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
652 case SIMPLE_BASE_STRING_WIDETAG:
653 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
654 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
657 cptr = (char *)(ptr+1);
664 case SIMPLE_VECTOR_WIDETAG:
666 printf("length = %ld", length);
669 while (length-- > 0) {
670 sprintf(buffer, "%d: ", index++);
671 print_obj(buffer, *ptr++);
675 case INSTANCE_HEADER_WIDETAG:
677 printf("length = %ld", (long) count);
679 while (count-- > 0) {
680 sprintf(buffer, "%d: ", index++);
681 print_obj(buffer, *ptr++);
685 case SIMPLE_ARRAY_WIDETAG:
686 case SIMPLE_BIT_VECTOR_WIDETAG:
687 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
688 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
689 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
690 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
691 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
692 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
694 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
696 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
697 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
698 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
699 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
701 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
702 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
704 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
705 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
707 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
708 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
711 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
713 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
714 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
716 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
717 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
719 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
720 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
721 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
722 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
724 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
725 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
727 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
728 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
730 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
731 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
733 case COMPLEX_BASE_STRING_WIDETAG:
734 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
735 case COMPLEX_CHARACTER_STRING_WIDETAG:
737 case COMPLEX_VECTOR_NIL_WIDETAG:
738 case COMPLEX_BIT_VECTOR_WIDETAG:
739 case COMPLEX_VECTOR_WIDETAG:
740 case COMPLEX_ARRAY_WIDETAG:
743 case CODE_HEADER_WIDETAG:
744 print_slots(code_slots, count-1, ptr);
747 case SIMPLE_FUN_HEADER_WIDETAG:
748 print_slots(fn_slots, 5, ptr);
751 case RETURN_PC_HEADER_WIDETAG:
752 print_obj("code: ", obj - (count * 4));
755 case CLOSURE_HEADER_WIDETAG:
756 print_slots(closure_slots, count, ptr);
759 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
760 print_slots(funcallable_instance_slots, count, ptr);
763 case VALUE_CELL_HEADER_WIDETAG:
764 print_slots(value_cell_slots, 1, ptr);
769 #ifndef LISP_FEATURE_ALPHA
770 printf("0x%08lx", (unsigned long) *ptr);
772 printf("0x%016lx", *(lispobj*)(ptr+1));
776 case WEAK_POINTER_WIDETAG:
777 print_slots(weak_pointer_slots, 1, ptr);
780 case CHARACTER_WIDETAG:
781 case UNBOUND_MARKER_WIDETAG:
783 printf("pointer to an immediate?");
787 print_slots(fdefn_slots, count, ptr);
792 printf("Unknown header object?");
798 static void print_obj(char *prefix, lispobj obj)
800 #ifdef LISP_FEATURE_X86_64
801 static void (*verbose_fns[])(lispobj obj)
802 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
803 print_fixnum, print_otherimm, print_fixnum, print_list,
804 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
805 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
806 static void (*brief_fns[])(lispobj obj)
807 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
808 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
809 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
810 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
812 static void (*verbose_fns[])(lispobj obj)
813 = {print_fixnum, print_struct, print_otherimm, print_list,
814 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
815 static void (*brief_fns[])(lispobj obj)
816 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
817 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
819 int type = lowtag_of(obj);
820 struct var *var = lookup_by_obj(obj);
822 boolean verbose = cur_depth < brief_depth;
824 if (!continue_p(verbose))
827 if (var != NULL && var_clock(var) == cur_clock)
830 if (var == NULL && is_lisp_pointer(obj))
831 var = define_var(NULL, obj, 0);
834 var_setclock(var, cur_clock);
839 sprintf(buffer, "$%s=", var_name(var));
844 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
845 if (cur_depth < brief_depth) {
846 fputs(lowtag_names[type], stdout);
847 (*verbose_fns[type])(obj);
850 (*brief_fns[type])(obj);
854 printf("$%s", var_name(var));
857 printf("$%s=", var_name(var));
858 (*brief_fns[type])(obj);
872 void print(lispobj obj)
884 void brief_print(lispobj obj)
899 brief_print(lispobj obj)
901 printf("lispobj 0x%lx\n", (unsigned long)obj);
904 #endif /* defined(LISP_FEATURE_SB_LDB) */