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");
73 dyndebug_init1(runtime_link
, "RUNTIME_LINK");
75 int n_output_flags
= n
;
76 dyndebug_init1(backtrace_when_lost
, "BACKTRACE_WHEN_LOST");
77 dyndebug_init1(sleep_when_lost
, "SLEEP_WHEN_LOST");
79 if (n
!= DYNDEBUG_NFLAGS
)
80 fprintf(stderr
, "Bug in dyndebug_init\n");
82 #if defined(LISP_FEATURE_GENCGC)
83 gencgc_verbose
= dyndebug_config
.dyndebug_gencgc_verbose
;
86 char *featurelist
= getenv("SBCL_DYNDEBUG");
89 featurelist
= strdup(featurelist
);
90 char *ptr
= featurelist
;
92 char *token
= strtok(ptr
, " ");
95 if (!strcmp(token
, "all"))
96 for (i
= 0; i
< n_output_flags
; i
++)
99 for (i
= 0; i
< (int)DYNDEBUG_NFLAGS
; i
++)
100 if (!strcmp(token
, names
[i
])) {
104 if (i
== DYNDEBUG_NFLAGS
) {
105 fprintf(stderr
, "No such dyndebug flag: `%s'\n", token
);
113 fprintf(stderr
, "Valid flags are:\n");
114 fprintf(stderr
, " all ;enables all of the following:\n");
116 for (i
= 0; i
< (int)DYNDEBUG_NFLAGS
; i
++) {
117 if (i
== n_output_flags
)
118 fprintf(stderr
, "Additional options:\n");
119 fprintf(stderr
, " %s\n", names
[i
]);
124 #undef dyndebug_init1
125 #undef DYNDEBUG_NFLAGS
128 /* Temporarily, odxprint merely performs the equivalent of a traditional
129 * FSHOW call, i.e. it merely formats to stderr. Ultimately, it should
130 * be restored to its full win32 branch functionality, where output to a
131 * file or to the debugger can be selected at runtime. */
133 void vodxprint_fun(const char *, va_list);
136 odxprint_fun(const char *fmt
, ...)
140 vodxprint_fun(fmt
, args
);
145 vodxprint_fun(const char *fmt
, va_list args
)
147 #ifdef LISP_FEATURE_WIN32
148 DWORD lastError
= GetLastError();
150 int original_errno
= errno
;
157 #ifdef LISP_FEATURE_SB_THREAD
158 struct thread
*arch_os_get_current_thread(void);
159 struct thread
*self
= arch_os_get_current_thread();
160 void *pth
= self
? (void *) self
->os_thread
: 0;
161 snprintf(buf
, sizeof(buf
), "[%p/%p] ", self
, pth
);
165 vsnprintf(buf
+ n
, sizeof(buf
) - n
- 1, fmt
, args
);
166 /* buf is now zero-terminated (even in case of overflow).
167 * Our caller took care of the newline (if any) through `fmt'. */
169 /* A sufficiently POSIXy implementation of stdio will provide
170 * per-FILE locking, as defined in the spec for flockfile. At least
171 * glibc complies with this. Hence we do not need to perform
172 * locking ourselves here. (Should it turn out, of course, that
173 * other libraries opt for speed rather than safety, we need to
174 * revisit this decision.) */
177 #ifdef LISP_FEATURE_WIN32
178 /* stdio's stderr is line-bufferred, i.e. \n ought to flush it.
179 * Unfortunately, MinGW does not behave the way I would expect it
180 * to. Let's be safe: */
186 #ifdef LISP_FEATURE_WIN32
187 SetLastError(lastError
);
189 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
)>>N_FIXNUM_TAG_BITS
);
306 printf("%d", ((s32
)obj
)>>N_FIXNUM_TAG_BITS
);
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
)>>N_FIXNUM_TAG_BITS
);
320 printf(": %d", ((s32
)obj
)>>N_FIXNUM_TAG_BITS
);
324 static void brief_otherimm(lispobj obj
)
329 type
= widetag_of(obj
);
331 case CHARACTER_WIDETAG
:
332 c
= obj
>>8; // no mask. show whatever's there
335 case '\0': charname
= "Nul"; break;
336 case '\n': charname
= "Newline"; break;
337 case '\b': charname
= "Backspace"; break;
338 case '\177': charname
= "Delete"; break;
340 if (c
< 32) printf("^%c", c
+64);
341 else if (c
< 128) printf("%c", c
);
342 else printf("U+%X", c
);
345 fputs(charname
, stdout
);
348 case UNBOUND_MARKER_WIDETAG
:
349 printf("<unbound marker>");
353 printf("%s", widetag_names
[type
>> 2]);
358 static void print_otherimm(lispobj obj
)
360 printf(", %s", widetag_names
[widetag_of(obj
) >> 2]);
362 switch (widetag_of(obj
)) {
363 case CHARACTER_WIDETAG
:
369 case UNBOUND_MARKER_WIDETAG
:
373 printf(": data=%ld", (long) (obj
>>8)&0xffffff);
378 static void brief_list(lispobj obj
)
383 if (!is_valid_lisp_addr((os_vm_address_t
)native_pointer(obj
)))
384 printf("(invalid Lisp-level address)");
389 while (lowtag_of(obj
) == LIST_POINTER_LOWTAG
) {
390 struct cons
*cons
= (struct cons
*)native_pointer(obj
);
394 if (++length
>= max_length
) {
399 print_obj("", cons
->car
);
413 static void print_list(lispobj obj
)
415 if (!is_valid_lisp_addr((os_vm_address_t
)native_pointer(obj
))) {
416 printf("(invalid address)");
417 } else if (obj
== NIL
) {
420 struct cons
*cons
= (struct cons
*)native_pointer(obj
);
422 print_obj("car: ", cons
->car
);
423 print_obj("cdr: ", cons
->cdr
);
427 // takes native pointer as input
428 char * simple_base_stringize(struct vector
* string
)
430 if (widetag_of(string
->header
) == SIMPLE_BASE_STRING_WIDETAG
)
431 return (char*)string
->data
;
432 int length
= string
->length
;
433 char * newstring
= malloc(length
+1);
434 uint32_t * data
= (uint32_t*)string
->data
;
436 for(i
=0;i
<length
;++i
)
437 newstring
[i
] = data
[i
] < 128 ? data
[i
] : '?';
438 newstring
[length
] = 0;
442 static void brief_struct(lispobj obj
)
444 struct instance
*instance
= (struct instance
*)native_pointer(obj
);
445 if (!is_valid_lisp_addr((os_vm_address_t
)instance
)) {
446 printf("(invalid address)");
448 extern struct vector
* instance_classoid_name(lispobj
*);
449 struct vector
* classoid_name
;
450 classoid_name
= instance_classoid_name((lispobj
*)instance
);
451 if ( classoid_name
) {
452 char * namestring
= simple_base_stringize(classoid_name
);
453 printf("#<ptr to 0x%08lx %s instance>",
454 (unsigned long) instance
->slots
[0], namestring
);
455 if ( namestring
!= (char*)classoid_name
->data
)
458 printf("#<ptr to 0x%08lx instance>",
459 (unsigned long) instance
->slots
[0]);
464 #include "genesis/layout.h"
465 static boolean
untagged_slot_p(struct layout
* layout
,
468 #ifdef LISP_FEATURE_INTERLEAVED_RAW_SLOTS
469 extern boolean
positive_bignum_logbitp(int,struct bignum
*);
470 lispobj bitmap
= layout
->untagged_bitmap
;
471 return fixnump(bitmap
)
472 ? (fixnum_value(bitmap
) >> slot_index
) & 1
473 : positive_bignum_logbitp(slot_index
,
474 (struct bignum
*)native_pointer(bitmap
));
476 return slot_index
>= (fixnum_value(layout
->length
)|1)
477 - fixnum_value(layout
->n_untagged_slots
);
481 static void print_struct(lispobj obj
)
483 struct instance
*instance
= (struct instance
*)native_pointer(obj
);
486 if (!is_valid_lisp_addr((os_vm_address_t
)instance
)) {
487 printf("(invalid address)");
489 lispobj layout_obj
= ((struct instance
*)native_pointer(obj
))->slots
[0];
490 print_obj("type: ", layout_obj
);
491 struct layout
* layout
= (struct layout
*)native_pointer(layout_obj
);
492 for (i
= 1; i
< HeaderValue(instance
->header
); i
++) {
493 sprintf(buffer
, "slot %d: ", i
);
494 if (layout
==NULL
|| untagged_slot_p(layout
, i
)) {
496 printf("\n\t %s0x%"OBJ_FMTX
" [raw]", buffer
, instance
->slots
[i
]);
498 print_obj(buffer
, instance
->slots
[i
]);
503 void show_lstring(struct vector
* string
, int quotes
, FILE *s
)
506 int i
, len
= fixnum_value(string
->length
);
508 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
509 if (widetag_of(string
->header
) == SIMPLE_CHARACTER_STRING_WIDETAG
) {
512 putc('u', s
); /* an arbitrary notational convention */
515 if (quotes
) putc('"', s
);
516 for (i
=0 ; i
<len
; i
++) {
517 // hopefully the compiler will optimize out the ucs4_p test
518 // when the runtime is built without Unicode support
521 ch
= i
[(uint32_t*)string
->data
];
523 ch
= i
[(char*)string
->data
];
524 if (ch
>= 32 && ch
< 127) {
525 if (quotes
&& (ch
== '"' || ch
== '\\'))
529 fprintf(s
, ch
> 0xffff ? "\\U%08X" :
530 ch
> 0xff ? "\\u%04X" : "\\x%02X", ch
);
533 if (quotes
) putc('"', s
);
536 static void brief_otherptr(lispobj obj
)
538 lispobj
*ptr
, header
;
540 struct symbol
*symbol
;
542 ptr
= (lispobj
*) native_pointer(obj
);
544 if (!is_valid_lisp_addr((os_vm_address_t
)obj
)) {
545 printf("(invalid address)");
550 type
= widetag_of(header
);
552 case SYMBOL_HEADER_WIDETAG
:
553 symbol
= (struct symbol
*)ptr
;
554 if (symbol
->package
== NIL
)
556 show_lstring((struct vector
*)native_pointer(symbol
->name
),
560 case SIMPLE_BASE_STRING_WIDETAG
:
561 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
562 case SIMPLE_CHARACTER_STRING_WIDETAG
:
564 show_lstring((struct vector
*)ptr
, 1, stdout
);
569 brief_otherimm(header
);
574 static void print_slots(char **slots
, int count
, lispobj
*ptr
)
576 while (count
-- > 0) {
578 print_obj(*slots
++, *ptr
++);
580 print_obj("???: ", *ptr
++);
585 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
586 * perhaps be generated automatically by GENESIS as part of
588 static char *symbol_slots
[] = {"value: ", "hash: ",
589 "info: ", "name: ", "package: ",
590 #if defined (LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_X86_64)
594 static char *ratio_slots
[] = {"numer: ", "denom: ", NULL
};
595 static char *complex_slots
[] = {"real: ", "imag: ", NULL
};
596 static char *code_slots
[] = {"words: ", "entry: ", "debug: ", NULL
};
597 static char *fn_slots
[] = {
598 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL
};
599 static char *closure_slots
[] = {"fn: ", NULL
};
600 static char *funcallable_instance_slots
[] = {"fn: ", "lexenv: ", "layout: ", NULL
};
601 static char *weak_pointer_slots
[] = {"value: ", NULL
};
602 static char *fdefn_slots
[] = {"name: ", "function: ", "raw_addr: ", NULL
};
603 static char *value_cell_slots
[] = {"value: ", NULL
};
605 static void print_otherptr(lispobj obj
)
607 if (!is_valid_lisp_addr((os_vm_address_t
)obj
)) {
608 printf("(invalid address)");
610 #ifndef LISP_FEATURE_ALPHA
612 unsigned long header
;
613 unsigned long length
;
619 int count
, type
, index
;
622 ptr
= (lispobj
*) native_pointer(obj
);
624 printf(" (NULL Pointer)");
629 length
= fixnum_value(*ptr
);
630 count
= HeaderValue(header
);
631 type
= widetag_of(header
);
633 print_obj("header: ", header
);
634 if (!other_immediate_lowtag_p(header
)) {
636 printf("(invalid header object)");
640 if (unprintable_array_types
[type
/8] & (1<<(type
% 8)))
649 #if N_WORD_BITS == 32
654 (unsigned long) *--ptr
, (count
?"_":""));
658 print_slots(ratio_slots
, count
, ptr
);
661 case COMPLEX_WIDETAG
:
662 print_slots(complex_slots
, count
, ptr
);
665 case SYMBOL_HEADER_WIDETAG
:
666 // Only 1 byte of a symbol header conveys its size.
667 // The other bytes may be freely used by the backend.
668 print_slots(symbol_slots
, count
& 0xFF, ptr
);
671 #if N_WORD_BITS == 32
672 case SINGLE_FLOAT_WIDETAG
:
674 printf("%g", ((struct single_float
*)native_pointer(obj
))->value
);
677 case DOUBLE_FLOAT_WIDETAG
:
679 printf("%g", ((struct double_float
*)native_pointer(obj
))->value
);
682 #ifdef LONG_FLOAT_WIDETAG
683 case LONG_FLOAT_WIDETAG
:
685 printf("%Lg", ((struct long_float
*)native_pointer(obj
))->value
);
689 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
690 case COMPLEX_SINGLE_FLOAT_WIDETAG
:
692 #ifdef LISP_FEATURE_X86_64
693 printf("%g", ((struct complex_single_float
*)native_pointer(obj
))->data
.data
[0]);
695 printf("%g", ((struct complex_single_float
*)native_pointer(obj
))->real
);
698 #ifdef LISP_FEATURE_X86_64
699 printf("%g", ((struct complex_single_float
*)native_pointer(obj
))->data
.data
[1]);
701 printf("%g", ((struct complex_single_float
*)native_pointer(obj
))->imag
);
706 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
707 case COMPLEX_DOUBLE_FLOAT_WIDETAG
:
709 printf("%g", ((struct complex_double_float
*)native_pointer(obj
))->real
);
711 printf("%g", ((struct complex_double_float
*)native_pointer(obj
))->imag
);
715 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
716 case COMPLEX_LONG_FLOAT_WIDETAG
:
718 printf("%Lg", ((struct complex_long_float
*)native_pointer(obj
))->real
);
720 printf("%Lg", ((struct complex_long_float
*)native_pointer(obj
))->imag
);
724 case SIMPLE_BASE_STRING_WIDETAG
:
725 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
726 case SIMPLE_CHARACTER_STRING_WIDETAG
:
729 show_lstring((struct vector
*)native_pointer(obj
), 1, stdout
);
732 case SIMPLE_VECTOR_WIDETAG
:
734 printf("length = %ld", length
);
737 while (length
-- > 0) {
738 sprintf(buffer
, "%d: ", index
++);
739 print_obj(buffer
, *ptr
++);
743 // FIXME: This case looks unreachable. print_struct() does it
744 case INSTANCE_HEADER_WIDETAG
:
746 printf("length = %ld", (long) count
);
748 while (count
-- > 0) {
749 sprintf(buffer
, "%d: ", index
++);
750 print_obj(buffer
, *ptr
++);
754 case CODE_HEADER_WIDETAG
:
755 print_slots(code_slots
, count
-1, ptr
);
758 case SIMPLE_FUN_HEADER_WIDETAG
:
759 print_slots(fn_slots
, 5, ptr
);
762 case RETURN_PC_HEADER_WIDETAG
:
763 print_obj("code: ", obj
- (count
* 4));
766 case CLOSURE_HEADER_WIDETAG
:
767 print_slots(closure_slots
, count
, ptr
);
770 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG
:
771 print_slots(funcallable_instance_slots
, count
, ptr
);
774 case VALUE_CELL_HEADER_WIDETAG
:
775 print_slots(value_cell_slots
, 1, ptr
);
780 #ifndef LISP_FEATURE_ALPHA
781 printf("0x%08lx", (unsigned long) *ptr
);
783 printf("0x%016lx", *(lispobj
*)(ptr
+1));
787 case WEAK_POINTER_WIDETAG
:
788 print_slots(weak_pointer_slots
, 1, ptr
);
791 case CHARACTER_WIDETAG
:
792 case UNBOUND_MARKER_WIDETAG
:
794 printf("pointer to an immediate?");
798 print_slots(fdefn_slots
, count
, ptr
);
803 printf("Unknown header object?");
809 static void print_obj(char *prefix
, lispobj obj
)
811 #ifdef LISP_FEATURE_X86_64
812 static void (*verbose_fns
[])(lispobj obj
)
813 = {print_fixnum
, print_otherimm
, print_fixnum
, print_struct
,
814 print_fixnum
, print_otherimm
, print_fixnum
, print_list
,
815 print_fixnum
, print_otherimm
, print_fixnum
, print_otherptr
,
816 print_fixnum
, print_otherimm
, print_fixnum
, print_otherptr
};
817 static void (*brief_fns
[])(lispobj obj
)
818 = {brief_fixnum
, brief_otherimm
, brief_fixnum
, brief_struct
,
819 brief_fixnum
, brief_otherimm
, brief_fixnum
, brief_list
,
820 brief_fixnum
, brief_otherimm
, brief_fixnum
, brief_otherptr
,
821 brief_fixnum
, brief_otherimm
, brief_fixnum
, brief_otherptr
};
823 static void (*verbose_fns
[])(lispobj obj
)
824 = {print_fixnum
, print_struct
, print_otherimm
, print_list
,
825 print_fixnum
, print_otherptr
, print_otherimm
, print_otherptr
};
826 static void (*brief_fns
[])(lispobj obj
)
827 = {brief_fixnum
, brief_struct
, brief_otherimm
, brief_list
,
828 brief_fixnum
, brief_otherptr
, brief_otherimm
, brief_otherptr
};
830 int type
= lowtag_of(obj
);
831 struct var
*var
= lookup_by_obj(obj
);
833 boolean verbose
= cur_depth
< brief_depth
;
835 if (!continue_p(verbose
))
838 if (var
!= NULL
&& var_clock(var
) == cur_clock
)
841 if (var
== NULL
&& is_lisp_pointer(obj
))
842 var
= define_var(NULL
, obj
, 0);
845 var_setclock(var
, cur_clock
);
850 sprintf(buffer
, "$%s=", var_name(var
));
855 printf("%s0x%08lx: ", prefix
, (unsigned long) obj
);
856 if (cur_depth
< brief_depth
) {
857 fputs(lowtag_names
[type
], stdout
);
858 (*verbose_fns
[type
])(obj
);
861 (*brief_fns
[type
])(obj
);
865 printf("$%s", var_name(var
));
868 printf("$%s=", var_name(var
));
869 (*brief_fns
[type
])(obj
);
883 void print(lispobj obj
)
895 void brief_print(lispobj obj
)
910 brief_print(lispobj obj
)
912 printf("lispobj 0x%lx\n", (unsigned long)obj
);
915 #endif /* defined(LISP_FEATURE_SB_LDB) */