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.
27 /* This file can be skipped if we're not supporting LDB. */
28 #if defined(LISP_FEATURE_SB_LDB)
33 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
34 #include "genesis/static-symbols.h"
35 #include "genesis/primitive-objects.h"
37 #include "genesis/static-symbols.h"
41 static int max_lines = 20, cur_lines = 0;
42 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
43 static int max_length = 5;
44 static boolean dont_descend = 0, skip_newline = 0;
45 static int cur_clock = 0;
47 static void print_obj(char *prefix, lispobj obj);
49 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
51 char *lowtag_Names[] = {
54 "other immediate [0]",
58 "other immediate [1]",
62 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
63 * but doesn't actually depend on them, so if they change, it gets
64 * all broken. We should either get rid of it or
65 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
66 * in a way which varies correctly with the values in sbcl.h. */
67 char *subtype_Names[] = {
74 #ifdef LONG_FLOAT_WIDETAG
78 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
79 "complex single float",
81 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
82 "complex double float",
84 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
91 "(simple-array (unsigned-byte 2) (*))",
92 "(simple-array (unsigned-byte 4) (*))",
93 "(simple-array (unsigned-byte 8) (*))",
94 "(simple-array (unsigned-byte 16) (*))",
95 "(simple-array (unsigned-byte 32) (*))",
96 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
97 "(simple-array (signed-byte 8) (*))",
99 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
100 "(simple-array (signed-byte 16) (*))",
102 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
103 "(simple-array fixnum (*))",
105 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
106 "(simple-array (signed-byte 32) (*))",
108 "(simple-array single-float (*))",
109 "(simple-array double-float (*))",
110 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
111 "(simple-array long-float (*))",
113 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
114 "(simple-array (complex single-float) (*))",
116 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
117 "(simple-array (complex double-float) (*))",
119 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
120 "(simple-array (complex long-float) (*))",
123 "complex-bit-vector",
129 "funcallable-instance header",
130 "unused function header 1",
131 "unused function header 2",
132 "unused function header 3",
133 "closure function header",
145 static void indent(int in)
147 static char *spaces = " ";
150 fputs(spaces, stdout);
154 fputs(spaces + 64 - in, stdout);
157 static boolean continue_p(boolean newline)
161 if (cur_depth >= max_depth || dont_descend)
170 if (cur_lines >= max_lines) {
171 printf("More? [y] ");
174 fgets(buffer, sizeof(buffer), stdin);
176 if (buffer[0] == 'n' || buffer[0] == 'N')
186 static void newline(char *label)
190 fputs(label, stdout);
192 indent(cur_depth * 2);
196 static void brief_fixnum(lispobj obj)
199 printf("%ld", ((long)obj)>>2);
201 printf("%d", ((s32)obj)>>2);
205 static void print_fixnum(lispobj obj)
208 printf(": %ld", ((long)obj)>>2);
210 printf(": %d", ((s32)obj)>>2);
214 static void brief_otherimm(lispobj obj)
219 type = widetag_of(obj);
221 case CHARACTER_WIDETAG:
228 printf("#\\Newline");
231 printf("#\\Backspace");
237 strcpy(buffer, "#\\");
239 strcat(buffer, "m-");
243 strcat(buffer, "c-");
246 printf("%s%c", buffer, c);
251 case UNBOUND_MARKER_WIDETAG:
252 printf("<unbound marker>");
257 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
258 printf("%s", lowtag_Names[idx]);
260 printf("unknown type (0x%0x)", type);
265 static void print_otherimm(lispobj obj)
269 type = widetag_of(obj);
272 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
273 printf(", %s", lowtag_Names[idx]);
275 printf(", unknown type (0x%0x)", type);
277 switch (widetag_of(obj)) {
278 case CHARACTER_WIDETAG:
284 case UNBOUND_MARKER_WIDETAG:
288 printf(": data=%ld", (long) (obj>>8)&0xffffff);
293 static void brief_list(lispobj obj)
298 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
299 printf("(invalid Lisp-level address)");
304 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
305 struct cons *cons = (struct cons *)native_pointer(obj);
309 if (++length >= max_length) {
314 print_obj(NULL, cons->car);
322 print_obj(NULL, obj);
328 static void print_list(lispobj obj)
330 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
331 printf("(invalid address)");
332 } else if (obj == NIL) {
335 struct cons *cons = (struct cons *)native_pointer(obj);
337 print_obj("car: ", cons->car);
338 print_obj("cdr: ", cons->cdr);
342 static void brief_struct(lispobj obj)
344 printf("#<ptr to 0x%08lx instance>",
345 (unsigned long) ((struct instance *)native_pointer(obj))->slots[0]);
348 static void print_struct(lispobj obj)
350 struct instance *instance = (struct instance *)native_pointer(obj);
353 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
354 for (i = 1; i < HeaderValue(instance->header); i++) {
355 sprintf(buffer, "slot %d: ", i);
356 print_obj(buffer, instance->slots[i]);
360 static void brief_otherptr(lispobj obj)
362 lispobj *ptr, header;
364 struct symbol *symbol;
365 struct vector *vector;
368 ptr = (lispobj *) native_pointer(obj);
370 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
371 printf("(invalid address)");
376 type = widetag_of(header);
378 case SYMBOL_HEADER_WIDETAG:
379 symbol = (struct symbol *)ptr;
380 vector = (struct vector *)native_pointer(symbol->name);
381 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
388 case SIMPLE_BASE_STRING_WIDETAG:
389 vector = (struct vector *)ptr;
391 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
401 brief_otherimm(header);
406 static void print_slots(char **slots, int count, lispobj *ptr)
408 while (count-- > 0) {
410 print_obj(*slots++, *ptr++);
412 print_obj("???: ", *ptr++);
417 /* FIXME: Yikes again! This, like subtype_Names[], needs to depend
418 * on the values in sbcl.h (or perhaps be generated automatically
419 * by GENESIS as part of sbcl.h). */
420 static char *symbol_slots[] = {"value: ", "unused: ",
421 "plist: ", "name: ", "package: ",
422 #ifdef LISP_FEATURE_SB_THREAD
426 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
427 static char *complex_slots[] = {"real: ", "imag: ", NULL};
428 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
429 static char *fn_slots[] = {
430 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
431 static char *closure_slots[] = {"fn: ", NULL};
432 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
433 static char *weak_pointer_slots[] = {"value: ", NULL};
434 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
435 static char *value_cell_slots[] = {"value: ", NULL};
437 static void print_otherptr(lispobj obj)
439 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
440 printf("(invalid address)");
444 unsigned long header;
445 unsigned long length;
451 int count, type, index;
452 char *cptr, buffer[16];
454 ptr = (lispobj*) native_pointer(obj);
456 printf(" (NULL Pointer)");
461 length = (*ptr) >> 2;
463 type = widetag_of(header);
465 print_obj("header: ", header);
466 if (lowtag_of(header) != OTHER_IMMEDIATE_0_LOWTAG &&
467 lowtag_of(header) != OTHER_IMMEDIATE_1_LOWTAG) {
469 printf("(invalid header object)");
479 printf("%08lx", (unsigned long) *--ptr);
483 print_slots(ratio_slots, count, ptr);
486 case COMPLEX_WIDETAG:
487 print_slots(complex_slots, count, ptr);
490 case SYMBOL_HEADER_WIDETAG:
491 print_slots(symbol_slots, count, ptr);
494 case SINGLE_FLOAT_WIDETAG:
496 printf("%g", ((struct single_float *)native_pointer(obj))->value);
499 case DOUBLE_FLOAT_WIDETAG:
501 printf("%g", ((struct double_float *)native_pointer(obj))->value);
504 #ifdef LONG_FLOAT_WIDETAG
505 case LONG_FLOAT_WIDETAG:
507 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
511 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
512 case COMPLEX_SINGLE_FLOAT_WIDETAG:
514 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
516 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
520 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
521 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
523 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
525 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
529 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
530 case COMPLEX_LONG_FLOAT_WIDETAG:
532 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
534 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
538 case SIMPLE_BASE_STRING_WIDETAG:
539 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
540 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
543 cptr = (char *)(ptr+1);
550 case SIMPLE_VECTOR_WIDETAG:
552 printf("length = %ld", length);
555 while (length-- > 0) {
556 sprintf(buffer, "%d: ", index++);
557 print_obj(buffer, *ptr++);
561 case INSTANCE_HEADER_WIDETAG:
563 printf("length = %ld", (long) count);
565 while (count-- > 0) {
566 sprintf(buffer, "%d: ", index++);
567 print_obj(buffer, *ptr++);
571 case SIMPLE_ARRAY_WIDETAG:
572 case SIMPLE_BIT_VECTOR_WIDETAG:
573 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
574 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
575 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
576 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
577 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
578 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
579 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
581 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
582 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
584 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
585 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
587 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
588 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
590 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
591 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
592 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
593 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
595 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
596 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
598 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
599 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
601 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
602 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
604 case COMPLEX_BASE_STRING_WIDETAG:
605 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
606 case COMPLEX_CHARACTER_STRING_WIDETAG:
608 case COMPLEX_VECTOR_NIL_WIDETAG:
609 case COMPLEX_BIT_VECTOR_WIDETAG:
610 case COMPLEX_VECTOR_WIDETAG:
611 case COMPLEX_ARRAY_WIDETAG:
614 case CODE_HEADER_WIDETAG:
615 print_slots(code_slots, count-1, ptr);
618 case SIMPLE_FUN_HEADER_WIDETAG:
619 print_slots(fn_slots, 5, ptr);
622 case RETURN_PC_HEADER_WIDETAG:
623 print_obj("code: ", obj - (count * 4));
626 case CLOSURE_HEADER_WIDETAG:
627 print_slots(closure_slots, count, ptr);
630 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
631 print_slots(funcallable_instance_slots, count, ptr);
634 case VALUE_CELL_HEADER_WIDETAG:
635 print_slots(value_cell_slots, 1, ptr);
641 printf("0x%08lx", (unsigned long) *ptr);
643 printf("0x%016lx", *(lispobj*)(ptr+1));
647 case WEAK_POINTER_WIDETAG:
648 print_slots(weak_pointer_slots, 1, ptr);
651 case CHARACTER_WIDETAG:
652 case UNBOUND_MARKER_WIDETAG:
654 printf("pointer to an immediate?");
658 print_slots(fdefn_slots, count, ptr);
663 printf("Unknown header object?");
669 static void print_obj(char *prefix, lispobj obj)
671 static void (*verbose_fns[])(lispobj obj)
672 = {print_fixnum, print_struct, print_otherimm, print_list,
673 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
674 static void (*brief_fns[])(lispobj obj)
675 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
676 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
677 int type = lowtag_of(obj);
678 struct var *var = lookup_by_obj(obj);
680 boolean verbose = cur_depth < brief_depth;
682 if (!continue_p(verbose))
685 if (var != NULL && var_clock(var) == cur_clock)
689 /* FIXME: What does this "x & y & z & .." expression mean? */
690 (obj & FUN_POINTER_LOWTAG & LIST_POINTER_LOWTAG & INSTANCE_POINTER_LOWTAG & OTHER_POINTER_LOWTAG) != 0)
691 var = define_var(NULL, obj, 0);
694 var_setclock(var, cur_clock);
699 sprintf(buffer, "$%s=", var_name(var));
704 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
705 if (cur_depth < brief_depth) {
706 fputs(lowtag_Names[type], stdout);
707 (*verbose_fns[type])(obj);
710 (*brief_fns[type])(obj);
714 printf("$%s", var_name(var));
717 printf("$%s=", var_name(var));
718 (*brief_fns[type])(obj);
732 void print(lispobj obj)
744 void brief_print(lispobj obj)
758 brief_print(lispobj obj)
760 printf("lispobj 0x%lx\n", (unsigned long)obj);
763 #endif /* defined(LISP_FEATURE_SB_LDB) */