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 gencgc_verbose = dyndebug_config.dyndebug_gencgc_verbose;
82 char *featurelist = getenv("SBCL_DYNDEBUG");
85 featurelist = strdup(featurelist);
86 char *ptr = featurelist;
88 char *token = strtok(ptr, " ");
91 if (!strcmp(token, "all"))
92 for (i = 0; i < n_output_flags; i++)
95 for (i = 0; i < DYNDEBUG_NFLAGS; i++)
96 if (!strcmp(token, names[i])) {
100 if (i == DYNDEBUG_NFLAGS) {
101 fprintf(stderr, "No such dyndebug flag: `%s'\n", token);
109 fprintf(stderr, "Valid flags are:\n");
110 fprintf(stderr, " all ;enables all of the following:\n");
112 for (i = 0; i < DYNDEBUG_NFLAGS; i++) {
113 if (i == n_output_flags)
114 fprintf(stderr, "Additional options:\n");
115 fprintf(stderr, " %s\n", names[i]);
120 #undef dyndebug_init1
121 #undef DYNDEBUG_NFLAGS
124 /* Temporarily, odxprint merely performs the equivalent of a traditional
125 * FSHOW call, i.e. it merely formats to stderr. Ultimately, it should
126 * be restored to its full win32 branch functionality, where output to a
127 * file or to the debugger can be selected at runtime. */
129 void vodxprint_fun(const char *, va_list);
132 odxprint_fun(const char *fmt, ...)
136 vodxprint_fun(fmt, args);
141 vodxprint_fun(const char *fmt, va_list args)
143 #ifdef LISP_FEATURE_WIN32
144 DWORD lastError = GetLastError();
146 int original_errno = errno;
153 #ifdef LISP_FEATURE_SB_THREAD
154 struct thread *arch_os_get_current_thread(void);
155 struct thread *self = arch_os_get_current_thread();
156 void *pth = self ? (void *) self->os_thread : 0;
157 snprintf(buf, sizeof(buf), "[%p/%p] ", self, pth);
161 vsnprintf(buf + n, sizeof(buf) - n - 1, fmt, args);
162 /* buf is now zero-terminated (even in case of overflow).
163 * Our caller took care of the newline (if any) through `fmt'. */
165 /* A sufficiently POSIXy implementation of stdio will provide
166 * per-FILE locking, as defined in the spec for flockfile. At least
167 * glibc complies with this. Hence we do not need to perform
168 * locking ourselves here. (Should it turn out, of course, that
169 * other libraries opt for speed rather than safety, we need to
170 * revisit this decision.) */
173 #ifdef LISP_FEATURE_WIN32
174 /* stdio's stderr is line-bufferred, i.e. \n ought to flush it.
175 * Unfortunately, MinGW does not behave the way I would expect it
176 * to. Let's be safe: */
182 #ifdef LISP_FEATURE_WIN32
183 SetLastError(lastError);
185 errno = original_errno;
189 /* Translate the rather awkward syntax
190 * FSHOW((stderr, "xyz"))
191 * into the new and cleaner
193 * If we were willing to clean up all existing call sites, we could remove
194 * this wrapper function. (This is a function, because I don't know how to
195 * strip the extra parens in a macro.) */
197 fshow_fun(void __attribute__((__unused__)) *ignored,
203 vodxprint_fun(fmt, args);
207 /* This file can be skipped if we're not supporting LDB. */
208 #if defined(LISP_FEATURE_SB_LDB)
213 #ifdef LISP_FEATURE_GENCGC
214 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
216 #include "genesis/static-symbols.h"
217 #include "genesis/primitive-objects.h"
218 #include "genesis/static-symbols.h"
219 #include "genesis/tagnames.h"
221 static int max_lines = 20, cur_lines = 0;
222 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
223 static int max_length = 5;
224 static boolean dont_descend = 0, skip_newline = 0;
225 static int cur_clock = 0;
227 static void print_obj(char *prefix, lispobj obj);
229 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
231 static void indent(int in)
233 static char *spaces = " ";
236 fputs(spaces, stdout);
240 fputs(spaces + 64 - in, stdout);
243 static boolean continue_p(boolean newline)
247 if (cur_depth >= max_depth || dont_descend)
256 if (cur_lines >= max_lines) {
257 printf("More? [y] ");
260 if (fgets(buffer, sizeof(buffer), stdin)) {
261 if (buffer[0] == 'n' || buffer[0] == 'N')
266 printf("\nUnable to read response, assuming y.\n");
275 static void newline(char *label)
279 fputs(label, stdout);
281 indent(cur_depth * 2);
285 static void print_unknown(lispobj obj)
287 printf("unknown object: %p", (void *)obj);
290 static void brief_fixnum(lispobj obj)
292 /* KLUDGE: Rather than update the tables in print_obj(), we
293 declare all fixnum-or-unknown tags to be fixnums and sort it
294 out here with a guard clause. */
295 if (!fixnump(obj)) return print_unknown(obj);
297 #ifndef LISP_FEATURE_ALPHA
298 printf("%ld", ((long)obj)>>2);
300 printf("%d", ((s32)obj)>>2);
304 static void print_fixnum(lispobj obj)
306 /* KLUDGE: Rather than update the tables in print_obj(), we
307 declare all fixnum-or-unknown tags to be fixnums and sort it
308 out here with a guard clause. */
309 if (!fixnump(obj)) return print_unknown(obj);
311 #ifndef LISP_FEATURE_ALPHA
312 printf(": %ld", ((long)obj)>>2);
314 printf(": %d", ((s32)obj)>>2);
318 static void brief_otherimm(lispobj obj)
323 type = widetag_of(obj);
325 case CHARACTER_WIDETAG:
332 printf("#\\Newline");
335 printf("#\\Backspace");
341 strcpy(buffer, "#\\");
343 strcat(buffer, "m-");
347 strcat(buffer, "c-");
350 printf("%s%c", buffer, c);
355 case UNBOUND_MARKER_WIDETAG:
356 printf("<unbound marker>");
360 printf("%s", widetag_names[type >> 2]);
365 static void print_otherimm(lispobj obj)
367 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
369 switch (widetag_of(obj)) {
370 case CHARACTER_WIDETAG:
376 case UNBOUND_MARKER_WIDETAG:
380 printf(": data=%ld", (long) (obj>>8)&0xffffff);
385 static void brief_list(lispobj obj)
390 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
391 printf("(invalid Lisp-level address)");
396 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
397 struct cons *cons = (struct cons *)native_pointer(obj);
401 if (++length >= max_length) {
406 print_obj("", cons->car);
420 static void print_list(lispobj obj)
422 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
423 printf("(invalid address)");
424 } else if (obj == NIL) {
427 struct cons *cons = (struct cons *)native_pointer(obj);
429 print_obj("car: ", cons->car);
430 print_obj("cdr: ", cons->cdr);
434 static void brief_struct(lispobj obj)
436 struct instance *instance = (struct instance *)native_pointer(obj);
437 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
438 printf("(invalid address)");
440 printf("#<ptr to 0x%08lx instance>",
441 (unsigned long) instance->slots[0]);
445 static void print_struct(lispobj obj)
447 struct instance *instance = (struct instance *)native_pointer(obj);
450 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
451 printf("(invalid address)");
453 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
454 for (i = 1; i < HeaderValue(instance->header); i++) {
455 sprintf(buffer, "slot %d: ", i);
456 print_obj(buffer, instance->slots[i]);
461 static void brief_otherptr(lispobj obj)
463 lispobj *ptr, header;
465 struct symbol *symbol;
466 struct vector *vector;
469 ptr = (lispobj *) native_pointer(obj);
471 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
472 printf("(invalid address)");
477 type = widetag_of(header);
479 case SYMBOL_HEADER_WIDETAG:
480 symbol = (struct symbol *)ptr;
481 vector = (struct vector *)native_pointer(symbol->name);
482 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
489 case SIMPLE_BASE_STRING_WIDETAG:
490 vector = (struct vector *)ptr;
492 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
502 brief_otherimm(header);
507 static void print_slots(char **slots, int count, lispobj *ptr)
509 while (count-- > 0) {
511 print_obj(*slots++, *ptr++);
513 print_obj("???: ", *ptr++);
518 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
519 * perhaps be generated automatically by GENESIS as part of
521 static char *symbol_slots[] = {"value: ", "hash: ",
522 "plist: ", "name: ", "package: ",
523 #ifdef LISP_FEATURE_SB_THREAD
527 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
528 static char *complex_slots[] = {"real: ", "imag: ", NULL};
529 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
530 static char *fn_slots[] = {
531 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
532 static char *closure_slots[] = {"fn: ", NULL};
533 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
534 static char *weak_pointer_slots[] = {"value: ", NULL};
535 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
536 static char *value_cell_slots[] = {"value: ", NULL};
538 static void print_otherptr(lispobj obj)
540 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
541 printf("(invalid address)");
543 #ifndef LISP_FEATURE_ALPHA
545 unsigned long header;
546 unsigned long length;
552 int count, type, index;
553 char *cptr, buffer[16];
555 ptr = (lispobj*) native_pointer(obj);
557 printf(" (NULL Pointer)");
562 length = fixnum_value(*ptr);
563 count = HeaderValue(header);
564 type = widetag_of(header);
566 print_obj("header: ", header);
567 if (!other_immediate_lowtag_p(header)) {
569 printf("(invalid header object)");
579 printf("%08lx", (unsigned long) *--ptr);
583 print_slots(ratio_slots, count, ptr);
586 case COMPLEX_WIDETAG:
587 print_slots(complex_slots, count, ptr);
590 case SYMBOL_HEADER_WIDETAG:
591 print_slots(symbol_slots, count, ptr);
594 #if N_WORD_BITS == 32
595 case SINGLE_FLOAT_WIDETAG:
597 printf("%g", ((struct single_float *)native_pointer(obj))->value);
600 case DOUBLE_FLOAT_WIDETAG:
602 printf("%g", ((struct double_float *)native_pointer(obj))->value);
605 #ifdef LONG_FLOAT_WIDETAG
606 case LONG_FLOAT_WIDETAG:
608 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
612 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
613 case COMPLEX_SINGLE_FLOAT_WIDETAG:
615 #ifdef LISP_FEATURE_X86_64
616 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
618 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
621 #ifdef LISP_FEATURE_X86_64
622 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
624 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
629 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
630 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
632 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
634 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
638 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
639 case COMPLEX_LONG_FLOAT_WIDETAG:
641 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
643 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
647 case SIMPLE_BASE_STRING_WIDETAG:
648 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
649 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
652 cptr = (char *)(ptr+1);
659 case SIMPLE_VECTOR_WIDETAG:
661 printf("length = %ld", length);
664 while (length-- > 0) {
665 sprintf(buffer, "%d: ", index++);
666 print_obj(buffer, *ptr++);
670 case INSTANCE_HEADER_WIDETAG:
672 printf("length = %ld", (long) count);
674 while (count-- > 0) {
675 sprintf(buffer, "%d: ", index++);
676 print_obj(buffer, *ptr++);
680 case SIMPLE_ARRAY_WIDETAG:
681 case SIMPLE_BIT_VECTOR_WIDETAG:
682 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
683 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
684 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
685 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
686 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
687 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
689 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
691 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
692 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
693 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
694 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
696 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
697 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
699 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
700 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
702 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
703 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
706 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
708 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
709 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
711 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
712 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
714 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
715 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
716 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
717 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
719 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
720 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
722 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
723 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
725 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
726 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
728 case COMPLEX_BASE_STRING_WIDETAG:
729 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
730 case COMPLEX_CHARACTER_STRING_WIDETAG:
732 case COMPLEX_VECTOR_NIL_WIDETAG:
733 case COMPLEX_BIT_VECTOR_WIDETAG:
734 case COMPLEX_VECTOR_WIDETAG:
735 case COMPLEX_ARRAY_WIDETAG:
738 case CODE_HEADER_WIDETAG:
739 print_slots(code_slots, count-1, ptr);
742 case SIMPLE_FUN_HEADER_WIDETAG:
743 print_slots(fn_slots, 5, ptr);
746 case RETURN_PC_HEADER_WIDETAG:
747 print_obj("code: ", obj - (count * 4));
750 case CLOSURE_HEADER_WIDETAG:
751 print_slots(closure_slots, count, ptr);
754 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
755 print_slots(funcallable_instance_slots, count, ptr);
758 case VALUE_CELL_HEADER_WIDETAG:
759 print_slots(value_cell_slots, 1, ptr);
764 #ifndef LISP_FEATURE_ALPHA
765 printf("0x%08lx", (unsigned long) *ptr);
767 printf("0x%016lx", *(lispobj*)(ptr+1));
771 case WEAK_POINTER_WIDETAG:
772 print_slots(weak_pointer_slots, 1, ptr);
775 case CHARACTER_WIDETAG:
776 case UNBOUND_MARKER_WIDETAG:
778 printf("pointer to an immediate?");
782 print_slots(fdefn_slots, count, ptr);
787 printf("Unknown header object?");
793 static void print_obj(char *prefix, lispobj obj)
795 #ifdef LISP_FEATURE_X86_64
796 static void (*verbose_fns[])(lispobj obj)
797 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
798 print_fixnum, print_otherimm, print_fixnum, print_list,
799 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
800 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
801 static void (*brief_fns[])(lispobj obj)
802 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
803 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
804 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
805 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
807 static void (*verbose_fns[])(lispobj obj)
808 = {print_fixnum, print_struct, print_otherimm, print_list,
809 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
810 static void (*brief_fns[])(lispobj obj)
811 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
812 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
814 int type = lowtag_of(obj);
815 struct var *var = lookup_by_obj(obj);
817 boolean verbose = cur_depth < brief_depth;
819 if (!continue_p(verbose))
822 if (var != NULL && var_clock(var) == cur_clock)
825 if (var == NULL && is_lisp_pointer(obj))
826 var = define_var(NULL, obj, 0);
829 var_setclock(var, cur_clock);
834 sprintf(buffer, "$%s=", var_name(var));
839 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
840 if (cur_depth < brief_depth) {
841 fputs(lowtag_names[type], stdout);
842 (*verbose_fns[type])(obj);
845 (*brief_fns[type])(obj);
849 printf("$%s", var_name(var));
852 printf("$%s=", var_name(var));
853 (*brief_fns[type])(obj);
867 void print(lispobj obj)
879 void brief_print(lispobj obj)
894 brief_print(lispobj obj)
896 printf("lispobj 0x%lx\n", (unsigned long)obj);
899 #endif /* defined(LISP_FEATURE_SB_LDB) */