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 */
31 /* FSHOW and odxprint provide debugging output for low-level information
32 * (signal handling, exceptions, safepoints) which is hard to debug by
35 * If enabled at all, environment variables control whether calls of the
36 * form odxprint(name, ...) are enabled at run-time, e.g. using
37 * SBCL_DYNDEBUG="fshow fshow_signal safepoints".
39 * In the case of FSHOW and FSHOW_SIGNAL, old-style code from runtime.h
40 * can also be used to enable or disable these more aggressively.
43 struct dyndebug_config dyndebug_config = {
44 QSHOW == 2, QSHOW_SIGNALS == 2
50 #define DYNDEBUG_NFLAGS (sizeof(struct dyndebug_config) / sizeof(int))
51 #define dyndebug_init1(lowercase, uppercase) \
53 int *ptr = &dyndebug_config.dyndebug_##lowercase; \
55 names[n] = #lowercase; \
56 char *val = getenv("SBCL_DYNDEBUG__" uppercase); \
57 *ptr = val && strlen(val); \
61 char *names[DYNDEBUG_NFLAGS];
62 int *ptrs[DYNDEBUG_NFLAGS];
64 dyndebug_init1(fshow, "FSHOW");
65 dyndebug_init1(fshow_signal, "FSHOW_SIGNAL");
66 dyndebug_init1(gencgc_verbose, "GENCGC_VERBOSE");
67 dyndebug_init1(safepoints, "SAFEPOINTS");
68 dyndebug_init1(seh, "SEH");
69 dyndebug_init1(misc, "MISC");
70 dyndebug_init1(pagefaults, "PAGEFAULTS");
72 if (n != DYNDEBUG_NFLAGS)
73 fprintf(stderr, "Bug in dyndebug_init\n");
75 gencgc_verbose = dyndebug_config.dyndebug_gencgc_verbose;
77 char *featurelist = getenv("SBCL_DYNDEBUG");
80 featurelist = strdup(featurelist);
81 char *ptr = featurelist;
83 char *token = strtok(ptr, " ");
86 if (!strcmp(token, "all"))
87 for (i = 0; i < DYNDEBUG_NFLAGS; i++)
90 for (i = 0; i < DYNDEBUG_NFLAGS; i++)
91 if (!strcmp(token, names[i])) {
95 if (i == DYNDEBUG_NFLAGS) {
96 fprintf(stderr, "No such dyndebug flag: `%s'\n", token);
104 fprintf(stderr, "Valid flags are:\n");
105 fprintf(stderr, " all ;enables all of the following:\n");
107 for (i = 0; i < DYNDEBUG_NFLAGS; i++)
108 fprintf(stderr, " %s\n", names[i]);
112 #undef dyndebug_init1
113 #undef DYNDEBUG_NFLAGS
116 /* Temporarily, odxprint merely performs the equivalent of a traditional
117 * FSHOW call, i.e. it merely formats to stderr. Ultimately, it should
118 * be restored to its full win32 branch functionality, where output to a
119 * file or to the debugger can be selected at runtime. */
122 odxprint_fun(const char *fmt, ...)
126 vodxprint_fun(fmt, args);
131 vodxprint_fun(const char *fmt, va_list args)
133 #ifdef LISP_FEATURE_WIN32
134 DWORD lastError = GetLastError();
136 int original_errno = errno;
143 #ifdef LISP_FEATURE_SB_THREAD
144 struct thread *arch_os_get_current_thread(void);
145 struct thread *self = arch_os_get_current_thread();
146 void *pth = self ? (void *) self->os_thread : 0;
147 snprintf(buf, sizeof(buf), "[%p/%p] ", self, pth);
151 vsnprintf(buf + n, sizeof(buf) - n - 1, fmt, args);
152 /* buf is now zero-terminated (even in case of overflow).
153 * Our caller took care of the newline (if any) through `fmt'. */
155 /* A sufficiently POSIXy implementation of stdio will provide
156 * per-FILE locking, as defined in the spec for flockfile. At least
157 * glibc complies with this. Hence we do not need to perform
158 * locking ourselves here. (Should it turn out, of course, that
159 * other libraries opt for speed rather than safety, we need to
160 * revisit this decision.) */
163 #ifdef LISP_FEATURE_WIN32
164 /* stdio's stderr is line-bufferred, i.e. \n ought to flush it.
165 * Unfortunately, MinGW does not behave the way I would expect it
166 * to. Let's be safe: */
172 #ifdef LISP_FEATURE_WIN32
173 SetLastError(lastError);
175 errno = original_errno;
179 /* Translate the rather awkward syntax
180 * FSHOW((stderr, "xyz"))
181 * into the new and cleaner
183 * If we were willing to clean up all existing call sites, we could remove
184 * this wrapper function. (This is a function, because I don't know how to
185 * strip the extra parens in a macro.) */
187 fshow_fun(void __attribute__((__unused__)) *ignored,
193 vodxprint_fun(fmt, args);
197 /* This file can be skipped if we're not supporting LDB. */
198 #if defined(LISP_FEATURE_SB_LDB)
203 #ifdef LISP_FEATURE_GENCGC
204 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
206 #include "genesis/static-symbols.h"
207 #include "genesis/primitive-objects.h"
208 #include "genesis/static-symbols.h"
209 #include "genesis/tagnames.h"
211 static int max_lines = 20, cur_lines = 0;
212 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
213 static int max_length = 5;
214 static boolean dont_descend = 0, skip_newline = 0;
215 static int cur_clock = 0;
217 static void print_obj(char *prefix, lispobj obj);
219 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
221 static void indent(int in)
223 static char *spaces = " ";
226 fputs(spaces, stdout);
230 fputs(spaces + 64 - in, stdout);
233 static boolean continue_p(boolean newline)
237 if (cur_depth >= max_depth || dont_descend)
246 if (cur_lines >= max_lines) {
247 printf("More? [y] ");
250 if (fgets(buffer, sizeof(buffer), stdin)) {
251 if (buffer[0] == 'n' || buffer[0] == 'N')
256 printf("\nUnable to read response, assuming y.\n");
265 static void newline(char *label)
269 fputs(label, stdout);
271 indent(cur_depth * 2);
275 static void print_unknown(lispobj obj)
277 printf("unknown object: %p", (void *)obj);
280 static void brief_fixnum(lispobj obj)
282 /* KLUDGE: Rather than update the tables in print_obj(), we
283 declare all fixnum-or-unknown tags to be fixnums and sort it
284 out here with a guard clause. */
285 if (!fixnump(obj)) return print_unknown(obj);
287 #ifndef LISP_FEATURE_ALPHA
288 printf("%ld", ((long)obj)>>2);
290 printf("%d", ((s32)obj)>>2);
294 static void print_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 brief_otherimm(lispobj obj)
313 type = widetag_of(obj);
315 case CHARACTER_WIDETAG:
322 printf("#\\Newline");
325 printf("#\\Backspace");
331 strcpy(buffer, "#\\");
333 strcat(buffer, "m-");
337 strcat(buffer, "c-");
340 printf("%s%c", buffer, c);
345 case UNBOUND_MARKER_WIDETAG:
346 printf("<unbound marker>");
350 printf("%s", widetag_names[type >> 2]);
355 static void print_otherimm(lispobj obj)
357 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
359 switch (widetag_of(obj)) {
360 case CHARACTER_WIDETAG:
366 case UNBOUND_MARKER_WIDETAG:
370 printf(": data=%ld", (long) (obj>>8)&0xffffff);
375 static void brief_list(lispobj obj)
380 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
381 printf("(invalid Lisp-level address)");
386 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
387 struct cons *cons = (struct cons *)native_pointer(obj);
391 if (++length >= max_length) {
396 print_obj("", cons->car);
410 static void print_list(lispobj obj)
412 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
413 printf("(invalid address)");
414 } else if (obj == NIL) {
417 struct cons *cons = (struct cons *)native_pointer(obj);
419 print_obj("car: ", cons->car);
420 print_obj("cdr: ", cons->cdr);
424 static void brief_struct(lispobj obj)
426 struct instance *instance = (struct instance *)native_pointer(obj);
427 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
428 printf("(invalid address)");
430 printf("#<ptr to 0x%08lx instance>",
431 (unsigned long) instance->slots[0]);
435 static void print_struct(lispobj obj)
437 struct instance *instance = (struct instance *)native_pointer(obj);
440 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
441 printf("(invalid address)");
443 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
444 for (i = 1; i < HeaderValue(instance->header); i++) {
445 sprintf(buffer, "slot %d: ", i);
446 print_obj(buffer, instance->slots[i]);
451 static void brief_otherptr(lispobj obj)
453 lispobj *ptr, header;
455 struct symbol *symbol;
456 struct vector *vector;
459 ptr = (lispobj *) native_pointer(obj);
461 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
462 printf("(invalid address)");
467 type = widetag_of(header);
469 case SYMBOL_HEADER_WIDETAG:
470 symbol = (struct symbol *)ptr;
471 vector = (struct vector *)native_pointer(symbol->name);
472 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
479 case SIMPLE_BASE_STRING_WIDETAG:
480 vector = (struct vector *)ptr;
482 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
492 brief_otherimm(header);
497 static void print_slots(char **slots, int count, lispobj *ptr)
499 while (count-- > 0) {
501 print_obj(*slots++, *ptr++);
503 print_obj("???: ", *ptr++);
508 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
509 * perhaps be generated automatically by GENESIS as part of
511 static char *symbol_slots[] = {"value: ", "hash: ",
512 "plist: ", "name: ", "package: ",
513 #ifdef LISP_FEATURE_SB_THREAD
517 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
518 static char *complex_slots[] = {"real: ", "imag: ", NULL};
519 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
520 static char *fn_slots[] = {
521 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
522 static char *closure_slots[] = {"fn: ", NULL};
523 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
524 static char *weak_pointer_slots[] = {"value: ", NULL};
525 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
526 static char *value_cell_slots[] = {"value: ", NULL};
528 static void print_otherptr(lispobj obj)
530 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
531 printf("(invalid address)");
533 #ifndef LISP_FEATURE_ALPHA
535 unsigned long header;
536 unsigned long length;
542 int count, type, index;
543 char *cptr, buffer[16];
545 ptr = (lispobj*) native_pointer(obj);
547 printf(" (NULL Pointer)");
552 length = fixnum_value(*ptr);
553 count = HeaderValue(header);
554 type = widetag_of(header);
556 print_obj("header: ", header);
557 if (!other_immediate_lowtag_p(header)) {
559 printf("(invalid header object)");
569 printf("%08lx", (unsigned long) *--ptr);
573 print_slots(ratio_slots, count, ptr);
576 case COMPLEX_WIDETAG:
577 print_slots(complex_slots, count, ptr);
580 case SYMBOL_HEADER_WIDETAG:
581 print_slots(symbol_slots, count, ptr);
584 #if N_WORD_BITS == 32
585 case SINGLE_FLOAT_WIDETAG:
587 printf("%g", ((struct single_float *)native_pointer(obj))->value);
590 case DOUBLE_FLOAT_WIDETAG:
592 printf("%g", ((struct double_float *)native_pointer(obj))->value);
595 #ifdef LONG_FLOAT_WIDETAG
596 case LONG_FLOAT_WIDETAG:
598 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
602 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
603 case COMPLEX_SINGLE_FLOAT_WIDETAG:
605 #ifdef LISP_FEATURE_X86_64
606 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
608 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
611 #ifdef LISP_FEATURE_X86_64
612 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
614 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
619 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
620 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
622 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
624 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
628 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
629 case COMPLEX_LONG_FLOAT_WIDETAG:
631 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
633 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
637 case SIMPLE_BASE_STRING_WIDETAG:
638 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
639 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
642 cptr = (char *)(ptr+1);
649 case SIMPLE_VECTOR_WIDETAG:
651 printf("length = %ld", length);
654 while (length-- > 0) {
655 sprintf(buffer, "%d: ", index++);
656 print_obj(buffer, *ptr++);
660 case INSTANCE_HEADER_WIDETAG:
662 printf("length = %ld", (long) count);
664 while (count-- > 0) {
665 sprintf(buffer, "%d: ", index++);
666 print_obj(buffer, *ptr++);
670 case SIMPLE_ARRAY_WIDETAG:
671 case SIMPLE_BIT_VECTOR_WIDETAG:
672 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
673 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
674 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
675 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
676 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
677 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
679 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
681 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
682 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
683 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
684 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
686 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
687 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
689 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
690 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
692 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
693 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
696 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
698 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
699 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
701 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
702 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
704 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
705 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
706 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
707 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
709 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
710 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
712 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
713 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
715 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
716 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
718 case COMPLEX_BASE_STRING_WIDETAG:
719 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
720 case COMPLEX_CHARACTER_STRING_WIDETAG:
722 case COMPLEX_VECTOR_NIL_WIDETAG:
723 case COMPLEX_BIT_VECTOR_WIDETAG:
724 case COMPLEX_VECTOR_WIDETAG:
725 case COMPLEX_ARRAY_WIDETAG:
728 case CODE_HEADER_WIDETAG:
729 print_slots(code_slots, count-1, ptr);
732 case SIMPLE_FUN_HEADER_WIDETAG:
733 print_slots(fn_slots, 5, ptr);
736 case RETURN_PC_HEADER_WIDETAG:
737 print_obj("code: ", obj - (count * 4));
740 case CLOSURE_HEADER_WIDETAG:
741 print_slots(closure_slots, count, ptr);
744 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
745 print_slots(funcallable_instance_slots, count, ptr);
748 case VALUE_CELL_HEADER_WIDETAG:
749 print_slots(value_cell_slots, 1, ptr);
754 #ifndef LISP_FEATURE_ALPHA
755 printf("0x%08lx", (unsigned long) *ptr);
757 printf("0x%016lx", *(lispobj*)(ptr+1));
761 case WEAK_POINTER_WIDETAG:
762 print_slots(weak_pointer_slots, 1, ptr);
765 case CHARACTER_WIDETAG:
766 case UNBOUND_MARKER_WIDETAG:
768 printf("pointer to an immediate?");
772 print_slots(fdefn_slots, count, ptr);
777 printf("Unknown header object?");
783 static void print_obj(char *prefix, lispobj obj)
785 #ifdef LISP_FEATURE_X86_64
786 static void (*verbose_fns[])(lispobj obj)
787 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
788 print_fixnum, print_otherimm, print_fixnum, print_list,
789 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
790 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
791 static void (*brief_fns[])(lispobj obj)
792 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
793 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
794 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
795 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
797 static void (*verbose_fns[])(lispobj obj)
798 = {print_fixnum, print_struct, print_otherimm, print_list,
799 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
800 static void (*brief_fns[])(lispobj obj)
801 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
802 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
804 int type = lowtag_of(obj);
805 struct var *var = lookup_by_obj(obj);
807 boolean verbose = cur_depth < brief_depth;
809 if (!continue_p(verbose))
812 if (var != NULL && var_clock(var) == cur_clock)
815 if (var == NULL && is_lisp_pointer(obj))
816 var = define_var(NULL, obj, 0);
819 var_setclock(var, cur_clock);
824 sprintf(buffer, "$%s=", var_name(var));
829 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
830 if (cur_depth < brief_depth) {
831 fputs(lowtag_names[type], stdout);
832 (*verbose_fns[type])(obj);
835 (*brief_fns[type])(obj);
839 printf("$%s", var_name(var));
842 printf("$%s=", var_name(var));
843 (*brief_fns[type])(obj);
857 void print(lispobj obj)
869 void brief_print(lispobj obj)
884 brief_print(lispobj obj)
886 printf("lispobj 0x%lx\n", (unsigned long)obj);
889 #endif /* defined(LISP_FEATURE_SB_LDB) */