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");
72 dyndebug_init1(io, "IO");
74 int n_output_flags = n;
75 dyndebug_init1(backtrace_when_lost, "BACKTRACE_WHEN_LOST");
76 dyndebug_init1(sleep_when_lost, "SLEEP_WHEN_LOST");
78 if (n != DYNDEBUG_NFLAGS)
79 fprintf(stderr, "Bug in dyndebug_init\n");
81 #if defined(LISP_FEATURE_GENCGC)
82 gencgc_verbose = dyndebug_config.dyndebug_gencgc_verbose;
85 char *featurelist = getenv("SBCL_DYNDEBUG");
88 featurelist = strdup(featurelist);
89 char *ptr = featurelist;
91 char *token = strtok(ptr, " ");
94 if (!strcmp(token, "all"))
95 for (i = 0; i < n_output_flags; i++)
98 for (i = 0; i < DYNDEBUG_NFLAGS; i++)
99 if (!strcmp(token, names[i])) {
103 if (i == DYNDEBUG_NFLAGS) {
104 fprintf(stderr, "No such dyndebug flag: `%s'\n", token);
112 fprintf(stderr, "Valid flags are:\n");
113 fprintf(stderr, " all ;enables all of the following:\n");
115 for (i = 0; i < DYNDEBUG_NFLAGS; i++) {
116 if (i == n_output_flags)
117 fprintf(stderr, "Additional options:\n");
118 fprintf(stderr, " %s\n", names[i]);
123 #undef dyndebug_init1
124 #undef DYNDEBUG_NFLAGS
127 /* Temporarily, odxprint merely performs the equivalent of a traditional
128 * FSHOW call, i.e. it merely formats to stderr. Ultimately, it should
129 * be restored to its full win32 branch functionality, where output to a
130 * file or to the debugger can be selected at runtime. */
132 void vodxprint_fun(const char *, va_list);
135 odxprint_fun(const char *fmt, ...)
139 vodxprint_fun(fmt, args);
144 vodxprint_fun(const char *fmt, va_list args)
146 #ifdef LISP_FEATURE_WIN32
147 DWORD lastError = GetLastError();
149 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;
190 /* Translate the rather awkward syntax
191 * FSHOW((stderr, "xyz"))
192 * into the new and cleaner
194 * If we were willing to clean up all existing call sites, we could remove
195 * this wrapper function. (This is a function, because I don't know how to
196 * strip the extra parens in a macro.) */
198 fshow_fun(void __attribute__((__unused__)) *ignored,
204 vodxprint_fun(fmt, args);
208 /* This file can be skipped if we're not supporting LDB. */
209 #if defined(LISP_FEATURE_SB_LDB)
214 #ifdef LISP_FEATURE_GENCGC
215 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
217 #if defined(LISP_FEATURE_WIN32)
218 # include "win32-thread-private-events.h" /* genesis/thread.h needs this */
220 #include "genesis/static-symbols.h"
221 #include "genesis/primitive-objects.h"
222 #include "genesis/static-symbols.h"
223 #include "genesis/tagnames.h"
225 static int max_lines = 20, cur_lines = 0;
226 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
227 static int max_length = 5;
228 static boolean dont_descend = 0, skip_newline = 0;
229 static int cur_clock = 0;
231 static void print_obj(char *prefix, lispobj obj);
233 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
235 static void indent(int in)
237 static char *spaces = " ";
240 fputs(spaces, stdout);
244 fputs(spaces + 64 - in, stdout);
247 static boolean continue_p(boolean newline)
251 if (cur_depth >= max_depth || dont_descend)
260 if (cur_lines >= max_lines) {
261 printf("More? [y] ");
264 if (fgets(buffer, sizeof(buffer), stdin)) {
265 if (buffer[0] == 'n' || buffer[0] == 'N')
270 printf("\nUnable to read response, assuming y.\n");
279 static void newline(char *label)
283 fputs(label, stdout);
285 indent(cur_depth * 2);
289 static void print_unknown(lispobj obj)
291 printf("unknown object: %p", (void *)obj);
294 static void brief_fixnum(lispobj obj)
296 /* KLUDGE: Rather than update the tables in print_obj(), we
297 declare all fixnum-or-unknown tags to be fixnums and sort it
298 out here with a guard clause. */
299 if (!fixnump(obj)) return print_unknown(obj);
301 #ifndef LISP_FEATURE_ALPHA
302 printf("%ld", ((long)obj)>>2);
304 printf("%d", ((s32)obj)>>2);
308 static void print_fixnum(lispobj obj)
310 /* KLUDGE: Rather than update the tables in print_obj(), we
311 declare all fixnum-or-unknown tags to be fixnums and sort it
312 out here with a guard clause. */
313 if (!fixnump(obj)) return print_unknown(obj);
315 #ifndef LISP_FEATURE_ALPHA
316 printf(": %ld", ((long)obj)>>2);
318 printf(": %d", ((s32)obj)>>2);
322 static void brief_otherimm(lispobj obj)
327 type = widetag_of(obj);
329 case CHARACTER_WIDETAG:
336 printf("#\\Newline");
339 printf("#\\Backspace");
345 strcpy(buffer, "#\\");
347 strcat(buffer, "m-");
351 strcat(buffer, "c-");
354 printf("%s%c", buffer, c);
359 case UNBOUND_MARKER_WIDETAG:
360 printf("<unbound marker>");
364 printf("%s", widetag_names[type >> 2]);
369 static void print_otherimm(lispobj obj)
371 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
373 switch (widetag_of(obj)) {
374 case CHARACTER_WIDETAG:
380 case UNBOUND_MARKER_WIDETAG:
384 printf(": data=%ld", (long) (obj>>8)&0xffffff);
389 static void brief_list(lispobj obj)
394 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
395 printf("(invalid Lisp-level address)");
400 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
401 struct cons *cons = (struct cons *)native_pointer(obj);
405 if (++length >= max_length) {
410 print_obj("", cons->car);
424 static void print_list(lispobj obj)
426 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
427 printf("(invalid address)");
428 } else if (obj == NIL) {
431 struct cons *cons = (struct cons *)native_pointer(obj);
433 print_obj("car: ", cons->car);
434 print_obj("cdr: ", cons->cdr);
438 static void brief_struct(lispobj obj)
440 struct instance *instance = (struct instance *)native_pointer(obj);
441 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
442 printf("(invalid address)");
444 printf("#<ptr to 0x%08lx instance>",
445 (unsigned long) instance->slots[0]);
449 static void print_struct(lispobj obj)
451 struct instance *instance = (struct instance *)native_pointer(obj);
454 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
455 printf("(invalid address)");
457 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
458 for (i = 1; i < HeaderValue(instance->header); i++) {
459 sprintf(buffer, "slot %d: ", i);
460 print_obj(buffer, instance->slots[i]);
465 static void brief_otherptr(lispobj obj)
467 lispobj *ptr, header;
469 struct symbol *symbol;
470 struct vector *vector;
473 ptr = (lispobj *) native_pointer(obj);
475 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
476 printf("(invalid address)");
481 type = widetag_of(header);
483 case SYMBOL_HEADER_WIDETAG:
484 symbol = (struct symbol *)ptr;
485 vector = (struct vector *)native_pointer(symbol->name);
486 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
493 case SIMPLE_BASE_STRING_WIDETAG:
494 vector = (struct vector *)ptr;
496 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
506 brief_otherimm(header);
511 static void print_slots(char **slots, int count, lispobj *ptr)
513 while (count-- > 0) {
515 print_obj(*slots++, *ptr++);
517 print_obj("???: ", *ptr++);
522 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
523 * perhaps be generated automatically by GENESIS as part of
525 static char *symbol_slots[] = {"value: ", "hash: ",
526 "plist: ", "name: ", "package: ",
527 #ifdef LISP_FEATURE_SB_THREAD
531 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
532 static char *complex_slots[] = {"real: ", "imag: ", NULL};
533 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
534 static char *fn_slots[] = {
535 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
536 static char *closure_slots[] = {"fn: ", NULL};
537 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
538 static char *weak_pointer_slots[] = {"value: ", NULL};
539 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
540 static char *value_cell_slots[] = {"value: ", NULL};
542 static void print_otherptr(lispobj obj)
544 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
545 printf("(invalid address)");
547 #ifndef LISP_FEATURE_ALPHA
549 unsigned long header;
550 unsigned long length;
556 int count, type, index;
557 char *cptr, buffer[16];
559 ptr = (lispobj*) native_pointer(obj);
561 printf(" (NULL Pointer)");
566 length = fixnum_value(*ptr);
567 count = HeaderValue(header);
568 type = widetag_of(header);
570 print_obj("header: ", header);
571 if (!other_immediate_lowtag_p(header)) {
573 printf("(invalid header object)");
583 printf("%08lx", (unsigned long) *--ptr);
587 print_slots(ratio_slots, count, ptr);
590 case COMPLEX_WIDETAG:
591 print_slots(complex_slots, count, ptr);
594 case SYMBOL_HEADER_WIDETAG:
595 print_slots(symbol_slots, count, ptr);
598 #if N_WORD_BITS == 32
599 case SINGLE_FLOAT_WIDETAG:
601 printf("%g", ((struct single_float *)native_pointer(obj))->value);
604 case DOUBLE_FLOAT_WIDETAG:
606 printf("%g", ((struct double_float *)native_pointer(obj))->value);
609 #ifdef LONG_FLOAT_WIDETAG
610 case LONG_FLOAT_WIDETAG:
612 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
616 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
617 case COMPLEX_SINGLE_FLOAT_WIDETAG:
619 #ifdef LISP_FEATURE_X86_64
620 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
622 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
625 #ifdef LISP_FEATURE_X86_64
626 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
628 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
633 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
634 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
636 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
638 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
642 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
643 case COMPLEX_LONG_FLOAT_WIDETAG:
645 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
647 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
651 case SIMPLE_BASE_STRING_WIDETAG:
652 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
653 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
656 cptr = (char *)(ptr+1);
663 case SIMPLE_VECTOR_WIDETAG:
665 printf("length = %ld", length);
668 while (length-- > 0) {
669 sprintf(buffer, "%d: ", index++);
670 print_obj(buffer, *ptr++);
674 case INSTANCE_HEADER_WIDETAG:
676 printf("length = %ld", (long) count);
678 while (count-- > 0) {
679 sprintf(buffer, "%d: ", index++);
680 print_obj(buffer, *ptr++);
684 case SIMPLE_ARRAY_WIDETAG:
685 case SIMPLE_BIT_VECTOR_WIDETAG:
686 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
687 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
688 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
689 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
690 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
691 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
693 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
695 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
696 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
697 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
698 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
700 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
701 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
703 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
704 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
706 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
707 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
710 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
712 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
713 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
715 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
716 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
718 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
719 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
720 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
721 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
723 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
724 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
726 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
727 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
729 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
730 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
732 case COMPLEX_BASE_STRING_WIDETAG:
733 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
734 case COMPLEX_CHARACTER_STRING_WIDETAG:
736 case COMPLEX_VECTOR_NIL_WIDETAG:
737 case COMPLEX_BIT_VECTOR_WIDETAG:
738 case COMPLEX_VECTOR_WIDETAG:
739 case COMPLEX_ARRAY_WIDETAG:
742 case CODE_HEADER_WIDETAG:
743 print_slots(code_slots, count-1, ptr);
746 case SIMPLE_FUN_HEADER_WIDETAG:
747 print_slots(fn_slots, 5, ptr);
750 case RETURN_PC_HEADER_WIDETAG:
751 print_obj("code: ", obj - (count * 4));
754 case CLOSURE_HEADER_WIDETAG:
755 print_slots(closure_slots, count, ptr);
758 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
759 print_slots(funcallable_instance_slots, count, ptr);
762 case VALUE_CELL_HEADER_WIDETAG:
763 print_slots(value_cell_slots, 1, ptr);
768 #ifndef LISP_FEATURE_ALPHA
769 printf("0x%08lx", (unsigned long) *ptr);
771 printf("0x%016lx", *(lispobj*)(ptr+1));
775 case WEAK_POINTER_WIDETAG:
776 print_slots(weak_pointer_slots, 1, ptr);
779 case CHARACTER_WIDETAG:
780 case UNBOUND_MARKER_WIDETAG:
782 printf("pointer to an immediate?");
786 print_slots(fdefn_slots, count, ptr);
791 printf("Unknown header object?");
797 static void print_obj(char *prefix, lispobj obj)
799 #ifdef LISP_FEATURE_X86_64
800 static void (*verbose_fns[])(lispobj obj)
801 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
802 print_fixnum, print_otherimm, print_fixnum, print_list,
803 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
804 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
805 static void (*brief_fns[])(lispobj obj)
806 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
807 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
808 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
809 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
811 static void (*verbose_fns[])(lispobj obj)
812 = {print_fixnum, print_struct, print_otherimm, print_list,
813 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
814 static void (*brief_fns[])(lispobj obj)
815 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
816 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
818 int type = lowtag_of(obj);
819 struct var *var = lookup_by_obj(obj);
821 boolean verbose = cur_depth < brief_depth;
823 if (!continue_p(verbose))
826 if (var != NULL && var_clock(var) == cur_clock)
829 if (var == NULL && is_lisp_pointer(obj))
830 var = define_var(NULL, obj, 0);
833 var_setclock(var, cur_clock);
838 sprintf(buffer, "$%s=", var_name(var));
843 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
844 if (cur_depth < brief_depth) {
845 fputs(lowtag_names[type], stdout);
846 (*verbose_fns[type])(obj);
849 (*brief_fns[type])(obj);
853 printf("$%s", var_name(var));
856 printf("$%s=", var_name(var));
857 (*brief_fns[type])(obj);
871 void print(lispobj obj)
883 void brief_print(lispobj obj)
898 brief_print(lispobj obj)
900 printf("lispobj 0x%lx\n", (unsigned long)obj);
903 #endif /* defined(LISP_FEATURE_SB_LDB) */