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 /* This file can be skipped if we're not supporting LDB. */
29 #if defined(LISP_FEATURE_SB_LDB)
34 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
35 #include "genesis/static-symbols.h"
36 #include "genesis/primitive-objects.h"
38 #include "genesis/static-symbols.h"
42 static int max_lines = 20, cur_lines = 0;
43 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
44 static int max_length = 5;
45 static boolean dont_descend = 0, skip_newline = 0;
46 static int cur_clock = 0;
48 static void print_obj(char *prefix, lispobj obj);
50 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
52 /* FIXME: This should be auto-generated by whatever generates
53 constants.h so we don't have to maintain this twice! */
54 #ifdef LISP_FEATURE_X86_64
55 char *lowtag_Names[] = {
58 "other immediate [0]",
62 "other immediate [1]",
66 "other immediate [2]",
70 "other immediate [3]",
74 char *lowtag_Names[] = {
77 "other immediate [0]",
81 "other immediate [1]",
86 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
87 * but doesn't actually depend on them, so if they change, it gets
88 * all broken. We should either get rid of it or
89 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
90 * in a way which varies correctly with the values in sbcl.h. */
91 char *subtype_Names[] = {
98 #ifdef LONG_FLOAT_WIDETAG
102 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
103 "complex single float",
105 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
106 "complex double float",
108 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
109 "complex long float",
115 "(simple-array (unsigned-byte 2) (*))",
116 "(simple-array (unsigned-byte 4) (*))",
117 "(simple-array (unsigned-byte 8) (*))",
118 "(simple-array (unsigned-byte 16) (*))",
119 "(simple-array (unsigned-byte 32) (*))",
120 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
121 "(simple-array (signed-byte 8) (*))",
123 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
124 "(simple-array (signed-byte 16) (*))",
126 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
127 "(simple-array fixnum (*))",
129 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
130 "(simple-array (signed-byte 32) (*))",
132 "(simple-array single-float (*))",
133 "(simple-array double-float (*))",
134 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
135 "(simple-array long-float (*))",
137 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
138 "(simple-array (complex single-float) (*))",
140 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
141 "(simple-array (complex double-float) (*))",
143 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
144 "(simple-array (complex long-float) (*))",
147 "complex-bit-vector",
153 "funcallable-instance header",
154 "unused function header 1",
155 "unused function header 2",
156 "unused function header 3",
157 "closure function header",
169 static void indent(int in)
171 static char *spaces = " ";
174 fputs(spaces, stdout);
178 fputs(spaces + 64 - in, stdout);
181 static boolean continue_p(boolean newline)
185 if (cur_depth >= max_depth || dont_descend)
194 if (cur_lines >= max_lines) {
195 printf("More? [y] ");
198 if (fgets(buffer, sizeof(buffer), stdin)) {
199 if (buffer[0] == 'n' || buffer[0] == 'N')
204 printf("\nUnable to read response, assuming y.\n");
213 static void newline(char *label)
217 fputs(label, stdout);
219 indent(cur_depth * 2);
223 static void brief_fixnum(lispobj obj)
225 #ifndef LISP_FEATURE_ALPHA
226 printf("%ld", ((long)obj)>>2);
228 printf("%d", ((s32)obj)>>2);
232 static void print_fixnum(lispobj obj)
234 #ifndef LISP_FEATURE_ALPHA
235 printf(": %ld", ((long)obj)>>2);
237 printf(": %d", ((s32)obj)>>2);
241 static void brief_otherimm(lispobj obj)
247 type = widetag_of(obj);
249 case CHARACTER_WIDETAG:
256 printf("#\\Newline");
259 printf("#\\Backspace");
265 strcpy(buffer, "#\\");
267 strcat(buffer, "m-");
271 strcat(buffer, "c-");
274 printf("%s%c", buffer, c);
279 case UNBOUND_MARKER_WIDETAG:
280 printf("<unbound marker>");
285 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
286 printf("%s", lowtag_Names[idx]);
288 printf("unknown type (0x%0x)", type);
293 static void print_otherimm(lispobj obj)
299 type = widetag_of(obj);
302 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
303 printf(", %s", lowtag_Names[idx]);
305 printf(", unknown type (0x%0x)", type);
307 switch (widetag_of(obj)) {
308 case CHARACTER_WIDETAG:
314 case UNBOUND_MARKER_WIDETAG:
318 printf(": data=%ld", (long) (obj>>8)&0xffffff);
323 static void brief_list(lispobj obj)
328 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
329 printf("(invalid Lisp-level address)");
334 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
335 struct cons *cons = (struct cons *)native_pointer(obj);
339 if (++length >= max_length) {
344 print_obj("", cons->car);
358 #ifdef LISP_FEATURE_X86_64
359 static void print_unknown(lispobj obj)
361 printf("unknown object: %p", (void *)obj);
365 static void print_list(lispobj obj)
367 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
368 printf("(invalid address)");
369 } else if (obj == NIL) {
372 struct cons *cons = (struct cons *)native_pointer(obj);
374 print_obj("car: ", cons->car);
375 print_obj("cdr: ", cons->cdr);
379 static void brief_struct(lispobj obj)
381 printf("#<ptr to 0x%08lx instance>",
382 (unsigned long) ((struct instance *)native_pointer(obj))->slots[0]);
385 static void print_struct(lispobj obj)
387 struct instance *instance = (struct instance *)native_pointer(obj);
390 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
391 for (i = 1; i < HeaderValue(instance->header); i++) {
392 sprintf(buffer, "slot %d: ", i);
393 print_obj(buffer, instance->slots[i]);
397 static void brief_otherptr(lispobj obj)
399 lispobj *ptr, header;
401 struct symbol *symbol;
402 struct vector *vector;
405 ptr = (lispobj *) native_pointer(obj);
407 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
408 printf("(invalid address)");
413 type = widetag_of(header);
415 case SYMBOL_HEADER_WIDETAG:
416 symbol = (struct symbol *)ptr;
417 vector = (struct vector *)native_pointer(symbol->name);
418 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
425 case SIMPLE_BASE_STRING_WIDETAG:
426 vector = (struct vector *)ptr;
428 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
438 brief_otherimm(header);
443 static void print_slots(char **slots, int count, lispobj *ptr)
445 while (count-- > 0) {
447 print_obj(*slots++, *ptr++);
449 print_obj("???: ", *ptr++);
454 /* FIXME: Yikes again! This, like subtype_Names[], needs to depend
455 * on the values in sbcl.h (or perhaps be generated automatically
456 * by GENESIS as part of sbcl.h). */
457 static char *symbol_slots[] = {"value: ", "hash: ",
458 "plist: ", "name: ", "package: ",
459 #ifdef LISP_FEATURE_SB_THREAD
463 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
464 static char *complex_slots[] = {"real: ", "imag: ", NULL};
465 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
466 static char *fn_slots[] = {
467 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
468 static char *closure_slots[] = {"fn: ", NULL};
469 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
470 static char *weak_pointer_slots[] = {"value: ", NULL};
471 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
472 static char *value_cell_slots[] = {"value: ", NULL};
474 static void print_otherptr(lispobj obj)
476 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
477 printf("(invalid address)");
479 #ifndef LISP_FEATURE_ALPHA
481 unsigned long header;
482 unsigned long length;
488 int count, type, index;
489 char *cptr, buffer[16];
491 ptr = (lispobj*) native_pointer(obj);
493 printf(" (NULL Pointer)");
498 length = (*ptr) >> 2;
500 type = widetag_of(header);
502 print_obj("header: ", header);
503 if (lowtag_of(header) != OTHER_IMMEDIATE_0_LOWTAG &&
504 lowtag_of(header) != OTHER_IMMEDIATE_1_LOWTAG) {
506 printf("(invalid header object)");
516 printf("%08lx", (unsigned long) *--ptr);
520 print_slots(ratio_slots, count, ptr);
523 case COMPLEX_WIDETAG:
524 print_slots(complex_slots, count, ptr);
527 case SYMBOL_HEADER_WIDETAG:
528 print_slots(symbol_slots, count, ptr);
531 #if N_WORD_BITS == 32
532 case SINGLE_FLOAT_WIDETAG:
534 printf("%g", ((struct single_float *)native_pointer(obj))->value);
537 case DOUBLE_FLOAT_WIDETAG:
539 printf("%g", ((struct double_float *)native_pointer(obj))->value);
542 #ifdef LONG_FLOAT_WIDETAG
543 case LONG_FLOAT_WIDETAG:
545 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
549 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
550 case COMPLEX_SINGLE_FLOAT_WIDETAG:
552 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
554 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
558 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
559 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
561 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
563 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
567 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
568 case COMPLEX_LONG_FLOAT_WIDETAG:
570 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
572 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
576 case SIMPLE_BASE_STRING_WIDETAG:
577 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
578 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
581 cptr = (char *)(ptr+1);
588 case SIMPLE_VECTOR_WIDETAG:
590 printf("length = %ld", length);
593 while (length-- > 0) {
594 sprintf(buffer, "%d: ", index++);
595 print_obj(buffer, *ptr++);
599 case INSTANCE_HEADER_WIDETAG:
601 printf("length = %ld", (long) count);
603 while (count-- > 0) {
604 sprintf(buffer, "%d: ", index++);
605 print_obj(buffer, *ptr++);
609 case SIMPLE_ARRAY_WIDETAG:
610 case SIMPLE_BIT_VECTOR_WIDETAG:
611 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
612 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
613 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
614 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
615 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
616 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
617 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
619 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
620 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
622 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
623 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
625 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
626 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
628 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
629 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
630 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
631 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
633 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
634 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
636 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
637 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
639 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
640 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
642 case COMPLEX_BASE_STRING_WIDETAG:
643 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
644 case COMPLEX_CHARACTER_STRING_WIDETAG:
646 case COMPLEX_VECTOR_NIL_WIDETAG:
647 case COMPLEX_BIT_VECTOR_WIDETAG:
648 case COMPLEX_VECTOR_WIDETAG:
649 case COMPLEX_ARRAY_WIDETAG:
652 case CODE_HEADER_WIDETAG:
653 print_slots(code_slots, count-1, ptr);
656 case SIMPLE_FUN_HEADER_WIDETAG:
657 print_slots(fn_slots, 5, ptr);
660 case RETURN_PC_HEADER_WIDETAG:
661 print_obj("code: ", obj - (count * 4));
664 case CLOSURE_HEADER_WIDETAG:
665 print_slots(closure_slots, count, ptr);
668 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
669 print_slots(funcallable_instance_slots, count, ptr);
672 case VALUE_CELL_HEADER_WIDETAG:
673 print_slots(value_cell_slots, 1, ptr);
678 #ifndef LISP_FEATURE_ALPHA
679 printf("0x%08lx", (unsigned long) *ptr);
681 printf("0x%016lx", *(lispobj*)(ptr+1));
685 case WEAK_POINTER_WIDETAG:
686 print_slots(weak_pointer_slots, 1, ptr);
689 case CHARACTER_WIDETAG:
690 case UNBOUND_MARKER_WIDETAG:
692 printf("pointer to an immediate?");
696 print_slots(fdefn_slots, count, ptr);
701 printf("Unknown header object?");
707 static void print_obj(char *prefix, lispobj obj)
709 #ifdef LISP_FEATURE_X86_64
710 static void (*verbose_fns[])(lispobj obj)
711 = {print_fixnum, print_struct, print_otherimm, print_unknown,
712 print_unknown, print_unknown, print_otherimm, print_list,
713 print_fixnum, print_otherptr, print_otherimm, print_unknown,
714 print_unknown, print_unknown, print_otherimm, print_otherptr};
715 static void (*brief_fns[])(lispobj obj)
716 = {brief_fixnum, brief_struct, brief_otherimm, print_unknown,
717 print_unknown, print_unknown, brief_otherimm, brief_list,
718 brief_fixnum, brief_otherptr, brief_otherimm, print_unknown,
719 print_unknown, print_unknown,brief_otherimm, brief_otherptr};
721 static void (*verbose_fns[])(lispobj obj)
722 = {print_fixnum, print_struct, print_otherimm, print_list,
723 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
724 static void (*brief_fns[])(lispobj obj)
725 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
726 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
728 int type = lowtag_of(obj);
729 struct var *var = lookup_by_obj(obj);
731 boolean verbose = cur_depth < brief_depth;
733 if (!continue_p(verbose))
736 if (var != NULL && var_clock(var) == cur_clock)
740 ((obj & LOWTAG_MASK) == FUN_POINTER_LOWTAG ||
741 (obj & LOWTAG_MASK) == LIST_POINTER_LOWTAG ||
742 (obj & LOWTAG_MASK) == INSTANCE_POINTER_LOWTAG ||
743 (obj & LOWTAG_MASK) == OTHER_POINTER_LOWTAG))
744 var = define_var(NULL, obj, 0);
747 var_setclock(var, cur_clock);
752 sprintf(buffer, "$%s=", var_name(var));
757 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
758 if (cur_depth < brief_depth) {
759 fputs(lowtag_Names[type], stdout);
760 (*verbose_fns[type])(obj);
763 (*brief_fns[type])(obj);
767 printf("$%s", var_name(var));
770 printf("$%s=", var_name(var));
771 (*brief_fns[type])(obj);
785 void print(lispobj obj)
797 void brief_print(lispobj obj)
811 brief_print(lispobj obj)
813 printf("lispobj 0x%lx\n", (unsigned long)obj);
816 #endif /* defined(LISP_FEATURE_SB_LDB) */