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;
156 #ifdef LISP_FEATURE_SB_THREAD
157 struct thread *arch_os_get_current_thread(void);
158 struct thread *self = arch_os_get_current_thread();
159 void *pth = self ? (void *) self->os_thread : 0;
160 snprintf(buf, sizeof(buf), "[%p/%p] ", self, pth);
164 vsnprintf(buf + n, sizeof(buf) - n - 1, fmt, args);
165 /* buf is now zero-terminated (even in case of overflow).
166 * Our caller took care of the newline (if any) through `fmt'. */
168 /* A sufficiently POSIXy implementation of stdio will provide
169 * per-FILE locking, as defined in the spec for flockfile. At least
170 * glibc complies with this. Hence we do not need to perform
171 * locking ourselves here. (Should it turn out, of course, that
172 * other libraries opt for speed rather than safety, we need to
173 * revisit this decision.) */
176 #ifdef LISP_FEATURE_WIN32
177 /* stdio's stderr is line-bufferred, i.e. \n ought to flush it.
178 * Unfortunately, MinGW does not behave the way I would expect it
179 * to. Let's be safe: */
185 #ifdef LISP_FEATURE_WIN32
186 SetLastError(lastError);
188 errno = original_errno;
192 /* Translate the rather awkward syntax
193 * FSHOW((stderr, "xyz"))
194 * into the new and cleaner
196 * If we were willing to clean up all existing call sites, we could remove
197 * this wrapper function. (This is a function, because I don't know how to
198 * strip the extra parens in a macro.) */
200 fshow_fun(void __attribute__((__unused__)) *ignored,
206 vodxprint_fun(fmt, args);
210 /* This file can be skipped if we're not supporting LDB. */
211 #if defined(LISP_FEATURE_SB_LDB)
216 #ifdef LISP_FEATURE_GENCGC
217 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
219 #if defined(LISP_FEATURE_WIN32)
220 # include "win32-thread-private-events.h" /* genesis/thread.h needs this */
222 #include "genesis/static-symbols.h"
223 #include "genesis/primitive-objects.h"
224 #include "genesis/static-symbols.h"
225 #include "genesis/tagnames.h"
227 static int max_lines = 20, cur_lines = 0;
228 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
229 static int max_length = 5;
230 static boolean dont_descend = 0, skip_newline = 0;
231 static int cur_clock = 0;
233 static void print_obj(char *prefix, lispobj obj);
235 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
237 static void indent(int in)
239 static char *spaces = " ";
242 fputs(spaces, stdout);
246 fputs(spaces + 64 - in, stdout);
249 static boolean continue_p(boolean newline)
253 if (cur_depth >= max_depth || dont_descend)
262 if (cur_lines >= max_lines) {
263 printf("More? [y] ");
266 if (fgets(buffer, sizeof(buffer), stdin)) {
267 if (buffer[0] == 'n' || buffer[0] == 'N')
272 printf("\nUnable to read response, assuming y.\n");
281 static void newline(char *label)
285 fputs(label, stdout);
287 indent(cur_depth * 2);
291 static void print_unknown(lispobj obj)
293 printf("unknown object: %p", (void *)obj);
296 static void brief_fixnum(lispobj obj)
298 /* KLUDGE: Rather than update the tables in print_obj(), we
299 declare all fixnum-or-unknown tags to be fixnums and sort it
300 out here with a guard clause. */
301 if (!fixnump(obj)) return print_unknown(obj);
303 #ifndef LISP_FEATURE_ALPHA
304 printf("%ld", ((long)obj)>>2);
306 printf("%d", ((s32)obj)>>2);
310 static void print_fixnum(lispobj obj)
312 /* KLUDGE: Rather than update the tables in print_obj(), we
313 declare all fixnum-or-unknown tags to be fixnums and sort it
314 out here with a guard clause. */
315 if (!fixnump(obj)) return print_unknown(obj);
317 #ifndef LISP_FEATURE_ALPHA
318 printf(": %ld", ((long)obj)>>2);
320 printf(": %d", ((s32)obj)>>2);
324 static void brief_otherimm(lispobj obj)
329 type = widetag_of(obj);
331 case CHARACTER_WIDETAG:
338 printf("#\\Newline");
341 printf("#\\Backspace");
347 strcpy(buffer, "#\\");
349 strcat(buffer, "m-");
353 strcat(buffer, "c-");
356 printf("%s%c", buffer, c);
361 case UNBOUND_MARKER_WIDETAG:
362 printf("<unbound marker>");
366 printf("%s", widetag_names[type >> 2]);
371 static void print_otherimm(lispobj obj)
373 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
375 switch (widetag_of(obj)) {
376 case CHARACTER_WIDETAG:
382 case UNBOUND_MARKER_WIDETAG:
386 printf(": data=%ld", (long) (obj>>8)&0xffffff);
391 static void brief_list(lispobj obj)
396 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
397 printf("(invalid Lisp-level address)");
402 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
403 struct cons *cons = (struct cons *)native_pointer(obj);
407 if (++length >= max_length) {
412 print_obj("", cons->car);
426 static void print_list(lispobj obj)
428 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
429 printf("(invalid address)");
430 } else if (obj == NIL) {
433 struct cons *cons = (struct cons *)native_pointer(obj);
435 print_obj("car: ", cons->car);
436 print_obj("cdr: ", cons->cdr);
440 static void brief_struct(lispobj obj)
442 struct instance *instance = (struct instance *)native_pointer(obj);
443 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
444 printf("(invalid address)");
446 printf("#<ptr to 0x%08lx instance>",
447 (unsigned long) instance->slots[0]);
451 static void print_struct(lispobj obj)
453 struct instance *instance = (struct instance *)native_pointer(obj);
456 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
457 printf("(invalid address)");
459 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
460 for (i = 1; i < HeaderValue(instance->header); i++) {
461 sprintf(buffer, "slot %d: ", i);
462 print_obj(buffer, instance->slots[i]);
467 static void brief_otherptr(lispobj obj)
469 lispobj *ptr, header;
471 struct symbol *symbol;
472 struct vector *vector;
475 ptr = (lispobj *) native_pointer(obj);
477 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
478 printf("(invalid address)");
483 type = widetag_of(header);
485 case SYMBOL_HEADER_WIDETAG:
486 symbol = (struct symbol *)ptr;
487 vector = (struct vector *)native_pointer(symbol->name);
488 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
495 case SIMPLE_BASE_STRING_WIDETAG:
496 vector = (struct vector *)ptr;
498 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
508 brief_otherimm(header);
513 static void print_slots(char **slots, int count, lispobj *ptr)
515 while (count-- > 0) {
517 print_obj(*slots++, *ptr++);
519 print_obj("???: ", *ptr++);
524 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
525 * perhaps be generated automatically by GENESIS as part of
527 static char *symbol_slots[] = {"value: ", "hash: ",
528 "plist: ", "name: ", "package: ",
529 #ifdef LISP_FEATURE_SB_THREAD
533 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
534 static char *complex_slots[] = {"real: ", "imag: ", NULL};
535 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
536 static char *fn_slots[] = {
537 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
538 static char *closure_slots[] = {"fn: ", NULL};
539 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
540 static char *weak_pointer_slots[] = {"value: ", NULL};
541 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
542 static char *value_cell_slots[] = {"value: ", NULL};
544 static void print_otherptr(lispobj obj)
546 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
547 printf("(invalid address)");
549 #ifndef LISP_FEATURE_ALPHA
551 unsigned long header;
552 unsigned long length;
558 int count, type, index;
559 char *cptr, buffer[16];
561 ptr = (lispobj*) native_pointer(obj);
563 printf(" (NULL Pointer)");
568 length = fixnum_value(*ptr);
569 count = HeaderValue(header);
570 type = widetag_of(header);
572 print_obj("header: ", header);
573 if (!other_immediate_lowtag_p(header)) {
575 printf("(invalid header object)");
585 printf("%08lx", (unsigned long) *--ptr);
589 print_slots(ratio_slots, count, ptr);
592 case COMPLEX_WIDETAG:
593 print_slots(complex_slots, count, ptr);
596 case SYMBOL_HEADER_WIDETAG:
597 print_slots(symbol_slots, count, ptr);
600 #if N_WORD_BITS == 32
601 case SINGLE_FLOAT_WIDETAG:
603 printf("%g", ((struct single_float *)native_pointer(obj))->value);
606 case DOUBLE_FLOAT_WIDETAG:
608 printf("%g", ((struct double_float *)native_pointer(obj))->value);
611 #ifdef LONG_FLOAT_WIDETAG
612 case LONG_FLOAT_WIDETAG:
614 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
618 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
619 case COMPLEX_SINGLE_FLOAT_WIDETAG:
621 #ifdef LISP_FEATURE_X86_64
622 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
624 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
627 #ifdef LISP_FEATURE_X86_64
628 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
630 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
635 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
636 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
638 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
640 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
644 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
645 case COMPLEX_LONG_FLOAT_WIDETAG:
647 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
649 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
653 case SIMPLE_BASE_STRING_WIDETAG:
654 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
655 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
658 cptr = (char *)(ptr+1);
665 case SIMPLE_VECTOR_WIDETAG:
667 printf("length = %ld", length);
670 while (length-- > 0) {
671 sprintf(buffer, "%d: ", index++);
672 print_obj(buffer, *ptr++);
676 case INSTANCE_HEADER_WIDETAG:
678 printf("length = %ld", (long) count);
680 while (count-- > 0) {
681 sprintf(buffer, "%d: ", index++);
682 print_obj(buffer, *ptr++);
686 case SIMPLE_ARRAY_WIDETAG:
687 case SIMPLE_BIT_VECTOR_WIDETAG:
688 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
689 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
690 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
691 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
692 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
693 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
695 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
697 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
698 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
699 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
700 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
702 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
703 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
705 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
706 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
708 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
709 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
712 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
714 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
715 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
717 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
718 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
720 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
721 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
722 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
723 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
725 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
726 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
728 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
729 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
731 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
732 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
734 case COMPLEX_BASE_STRING_WIDETAG:
735 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
736 case COMPLEX_CHARACTER_STRING_WIDETAG:
738 case COMPLEX_VECTOR_NIL_WIDETAG:
739 case COMPLEX_BIT_VECTOR_WIDETAG:
740 case COMPLEX_VECTOR_WIDETAG:
741 case COMPLEX_ARRAY_WIDETAG:
744 case CODE_HEADER_WIDETAG:
745 print_slots(code_slots, count-1, ptr);
748 case SIMPLE_FUN_HEADER_WIDETAG:
749 print_slots(fn_slots, 5, ptr);
752 case RETURN_PC_HEADER_WIDETAG:
753 print_obj("code: ", obj - (count * 4));
756 case CLOSURE_HEADER_WIDETAG:
757 print_slots(closure_slots, count, ptr);
760 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
761 print_slots(funcallable_instance_slots, count, ptr);
764 case VALUE_CELL_HEADER_WIDETAG:
765 print_slots(value_cell_slots, 1, ptr);
770 #ifndef LISP_FEATURE_ALPHA
771 printf("0x%08lx", (unsigned long) *ptr);
773 printf("0x%016lx", *(lispobj*)(ptr+1));
777 case WEAK_POINTER_WIDETAG:
778 print_slots(weak_pointer_slots, 1, ptr);
781 case CHARACTER_WIDETAG:
782 case UNBOUND_MARKER_WIDETAG:
784 printf("pointer to an immediate?");
788 print_slots(fdefn_slots, count, ptr);
793 printf("Unknown header object?");
799 static void print_obj(char *prefix, lispobj obj)
801 #ifdef LISP_FEATURE_X86_64
802 static void (*verbose_fns[])(lispobj obj)
803 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
804 print_fixnum, print_otherimm, print_fixnum, print_list,
805 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
806 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
807 static void (*brief_fns[])(lispobj obj)
808 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
809 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
810 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
811 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
813 static void (*verbose_fns[])(lispobj obj)
814 = {print_fixnum, print_struct, print_otherimm, print_list,
815 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
816 static void (*brief_fns[])(lispobj obj)
817 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
818 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
820 int type = lowtag_of(obj);
821 struct var *var = lookup_by_obj(obj);
823 boolean verbose = cur_depth < brief_depth;
825 if (!continue_p(verbose))
828 if (var != NULL && var_clock(var) == cur_clock)
831 if (var == NULL && is_lisp_pointer(obj))
832 var = define_var(NULL, obj, 0);
835 var_setclock(var, cur_clock);
840 sprintf(buffer, "$%s=", var_name(var));
845 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
846 if (cur_depth < brief_depth) {
847 fputs(lowtag_names[type], stdout);
848 (*verbose_fns[type])(obj);
851 (*brief_fns[type])(obj);
855 printf("$%s", var_name(var));
858 printf("$%s=", var_name(var));
859 (*brief_fns[type])(obj);
873 void print(lispobj obj)
885 void brief_print(lispobj obj)
900 brief_print(lispobj obj)
902 printf("lispobj 0x%lx\n", (unsigned long)obj);
905 #endif /* defined(LISP_FEATURE_SB_LDB) */