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.
26 /* This file can be skipped if we're not supporting LDB. */
27 #if defined(LISP_FEATURE_SB_LDB)
34 static int max_lines = 20, cur_lines = 0;
35 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
36 static int max_length = 5;
37 static boolean dont_descend = 0, skip_newline = 0;
38 static int cur_clock = 0;
40 static void print_obj(char *prefix, lispobj obj);
42 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
44 char *lowtag_Names[] = {
47 "other immediate [0]",
51 "other immediate [1]",
55 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
56 * but doesn't actually depend on them, so if they change, it gets
57 * all broken. We should either get rid of it or
58 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
59 * in a way which varies correctly with the values in sbcl.h. */
60 char *subtype_Names[] = {
67 #ifdef LONG_FLOAT_WIDETAG
71 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
72 "complex single float",
74 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
75 "complex double float",
77 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
84 "(simple-array (unsigned-byte 2) (*))",
85 "(simple-array (unsigned-byte 4) (*))",
86 "(simple-array (unsigned-byte 8) (*))",
87 "(simple-array (unsigned-byte 16) (*))",
88 "(simple-array (unsigned-byte 32) (*))",
89 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
90 "(simple-array (signed-byte 8) (*))",
92 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
93 "(simple-array (signed-byte 16) (*))",
95 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
96 "(simple-array fixnum (*))",
98 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
99 "(simple-array (signed-byte 32) (*))",
101 "(simple-array single-float (*))",
102 "(simple-array double-float (*))",
103 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
104 "(simple-array long-float (*))",
106 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
107 "(simple-array (complex single-float) (*))",
109 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
110 "(simple-array (complex double-float) (*))",
112 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
113 "(simple-array (complex long-float) (*))",
116 "complex-bit-vector",
122 "funcallable-instance header",
123 "unused function header 1",
124 "unused function header 2",
125 "unused function header 3",
126 "closure function header",
138 static void indent(int in)
140 static char *spaces = " ";
143 fputs(spaces, stdout);
147 fputs(spaces + 64 - in, stdout);
150 static boolean continue_p(boolean newline)
154 if (cur_depth >= max_depth || dont_descend)
163 if (cur_lines >= max_lines) {
164 printf("More? [y] ");
167 fgets(buffer, sizeof(buffer), stdin);
169 if (buffer[0] == 'n' || buffer[0] == 'N')
179 static void newline(char *label)
183 fputs(label, stdout);
185 indent(cur_depth * 2);
189 static void brief_fixnum(lispobj obj)
192 printf("%ld", ((long)obj)>>2);
194 printf("%d", ((s32)obj)>>2);
198 static void print_fixnum(lispobj obj)
201 printf(": %ld", ((long)obj)>>2);
203 printf(": %d", ((s32)obj)>>2);
207 static void brief_otherimm(lispobj obj)
212 type = widetag_of(obj);
214 case BASE_CHAR_WIDETAG:
221 printf("#\\Newline");
224 printf("#\\Backspace");
230 strcpy(buffer, "#\\");
232 strcat(buffer, "m-");
236 strcat(buffer, "c-");
239 printf("%s%c", buffer, c);
244 case UNBOUND_MARKER_WIDETAG:
245 printf("<unbound marker>");
250 if (idx < (sizeof(SUBNAMES_WIDETAG) / sizeof(char *)))
251 printf("%s", SUBNAMES_WIDETAG[idx]);
253 printf("unknown type (0x%0x)", type);
258 static void print_otherimm(lispobj obj)
262 type = widetag_of(obj);
265 if (idx < (sizeof(SUBNAMES_WIDETAG) / sizeof(char *)))
266 printf(", %s", SUBNAMES_WIDETAG[idx]);
268 printf(", unknown type (0x%0x)", type);
270 switch (widetag_of(obj)) {
271 case BASE_CHAR_WIDETAG:
277 case UNBOUND_MARKER_WIDETAG:
281 printf(": data=%ld", (long) (obj>>8)&0xffffff);
286 static void brief_list(lispobj obj)
291 if (!is_valid_lisp_addr((os_vm_address_t)obj))
292 printf("(invalid Lisp-level address)");
297 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
298 struct cons *cons = (struct cons *)native_pointer(obj);
302 if (++length >= max_length) {
307 print_obj(NULL, cons->car);
315 print_obj(NULL, obj);
321 static void print_list(lispobj obj)
323 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
324 printf("(invalid address)");
325 } else if (obj == NIL) {
328 struct cons *cons = (struct cons *)native_pointer(obj);
330 print_obj("car: ", cons->car);
331 print_obj("cdr: ", cons->cdr);
335 static void brief_struct(lispobj obj)
337 printf("#<ptr to 0x%08lx instance>",
338 (unsigned long) ((struct instance *)native_pointer(obj))->slots[0]);
341 static void print_struct(lispobj obj)
343 struct instance *instance = (struct instance *)native_pointer(obj);
346 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
347 for (i = 1; i < HeaderValue(instance->header); i++) {
348 sprintf(buffer, "slot %d: ", i);
349 print_obj(buffer, instance->slots[i]);
353 static void brief_otherptr(lispobj obj)
355 lispobj *ptr, header;
357 struct symbol *symbol;
358 struct vector *vector;
361 ptr = (lispobj *) native_pointer(obj);
363 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
364 printf("(invalid address)");
369 type = widetag_of(header);
371 case SYMBOL_HEADER_WIDETAG:
372 symbol = (struct symbol *)ptr;
373 vector = (struct vector *)native_pointer(symbol->name);
374 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
381 case SIMPLE_STRING_WIDETAG:
382 vector = (struct vector *)ptr;
384 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
394 brief_otherimm(header);
399 static void print_slots(char **slots, int count, lispobj *ptr)
401 while (count-- > 0) {
403 print_obj(*slots++, *ptr++);
405 print_obj("???: ", *ptr++);
410 /* FIXME: Yikes again! This, like SUBNAMES_WIDETAG[], needs to depend
411 * on the values in sbcl.h. */
412 static char *symbol_slots[] = {"value: ", "unused: ",
413 "plist: ", "name: ", "package: ", NULL};
414 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
415 static char *complex_slots[] = {"real: ", "imag: ", NULL};
416 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
417 static char *fn_slots[] = {"self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
418 static char *closure_slots[] = {"fn: ", NULL};
419 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
420 static char *weak_pointer_slots[] = {"value: ", NULL};
421 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
422 static char *value_cell_slots[] = {"value: ", NULL};
424 static void print_otherptr(lispobj obj)
426 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
427 printf("(invalid address)");
431 unsigned long header;
432 unsigned long length;
438 int count, type, index;
439 char *cptr, buffer[16];
441 ptr = (lispobj*) native_pointer(obj);
443 printf(" (NULL Pointer)");
448 length = (*ptr) >> 2;
450 type = widetag_of(header);
452 print_obj("header: ", header);
453 if (lowtag_of(header) != OTHER_IMMEDIATE_0_LOWTAG &&
454 lowtag_of(header) != OTHER_IMMEDIATE_1_LOWTAG) {
456 printf("(invalid header object)");
466 printf("%08lx", (unsigned long) *--ptr);
470 print_slots(ratio_slots, count, ptr);
473 case COMPLEX_WIDETAG:
474 print_slots(complex_slots, count, ptr);
477 case SYMBOL_HEADER_WIDETAG:
478 print_slots(symbol_slots, count, ptr);
481 case SINGLE_FLOAT_WIDETAG:
483 printf("%g", ((struct single_float *)native_pointer(obj))->value);
486 case DOUBLE_FLOAT_WIDETAG:
488 printf("%g", ((struct double_float *)native_pointer(obj))->value);
491 #ifdef LONG_FLOAT_WIDETAG
492 case LONG_FLOAT_WIDETAG:
494 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
498 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
499 case COMPLEX_SINGLE_FLOAT_WIDETAG:
501 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
503 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
507 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
508 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
510 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
512 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
516 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
517 case COMPLEX_LONG_FLOAT_WIDETAG:
519 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
521 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
525 case SIMPLE_STRING_WIDETAG:
527 cptr = (char *)(ptr+1);
534 case SIMPLE_VECTOR_WIDETAG:
536 printf("length = %ld", length);
539 while (length-- > 0) {
540 sprintf(buffer, "%d: ", index++);
541 print_obj(buffer, *ptr++);
545 case INSTANCE_HEADER_WIDETAG:
547 printf("length = %ld", (long) count);
549 while (count-- > 0) {
550 sprintf(buffer, "%d: ", index++);
551 print_obj(buffer, *ptr++);
555 case SIMPLE_ARRAY_WIDETAG:
556 case SIMPLE_BIT_VECTOR_WIDETAG:
557 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
558 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
559 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
560 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
561 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
562 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
563 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
565 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
566 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
568 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
569 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
571 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
572 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
574 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
575 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
576 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
577 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
579 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
580 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
582 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
583 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
585 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
586 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
588 case COMPLEX_STRING_WIDETAG:
589 case COMPLEX_BIT_VECTOR_WIDETAG:
590 case COMPLEX_VECTOR_WIDETAG:
591 case COMPLEX_ARRAY_WIDETAG:
594 case CODE_HEADER_WIDETAG:
595 print_slots(code_slots, count-1, ptr);
598 case SIMPLE_FUN_HEADER_WIDETAG:
599 case CLOSURE_FUN_HEADER_WIDETAG:
600 print_slots(fn_slots, 5, ptr);
603 case RETURN_PC_HEADER_WIDETAG:
604 print_obj("code: ", obj - (count * 4));
607 case CLOSURE_HEADER_WIDETAG:
608 print_slots(closure_slots, count, ptr);
611 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
612 print_slots(funcallable_instance_slots, count, ptr);
615 case VALUE_CELL_HEADER_WIDETAG:
616 print_slots(value_cell_slots, 1, ptr);
622 printf("0x%08lx", (unsigned long) *ptr);
624 printf("0x%016lx", *(lispobj*)(ptr+1));
628 case WEAK_POINTER_WIDETAG:
629 print_slots(weak_pointer_slots, 1, ptr);
632 case BASE_CHAR_WIDETAG:
633 case UNBOUND_MARKER_WIDETAG:
635 printf("pointer to an immediate?");
639 print_slots(fdefn_slots, count, ptr);
644 printf("Unknown header object?");
650 static void print_obj(char *prefix, lispobj obj)
652 static void (*verbose_fns[])(lispobj obj)
653 = {print_fixnum, print_otherptr, print_otherimm, print_list,
654 print_fixnum, print_struct, print_otherimm, print_otherptr};
655 static void (*brief_fns[])(lispobj obj)
656 = {brief_fixnum, brief_otherptr, brief_otherimm, brief_list,
657 brief_fixnum, brief_struct, brief_otherimm, brief_otherptr};
658 int type = lowtag_of(obj);
659 struct var *var = lookup_by_obj(obj);
661 boolean verbose = cur_depth < brief_depth;
663 if (!continue_p(verbose))
666 if (var != NULL && var_clock(var) == cur_clock)
670 /* FIXME: What does this "x & y & z & .." expression mean? */
671 (obj & FUN_POINTER_LOWTAG & LIST_POINTER_LOWTAG & INSTANCE_POINTER_LOWTAG & OTHER_POINTER_LOWTAG) != 0)
672 var = define_var(NULL, obj, 0);
675 var_setclock(var, cur_clock);
680 sprintf(buffer, "$%s=", var_name(var));
685 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
686 if (cur_depth < brief_depth) {
687 fputs(lowtag_Names[type], stdout);
688 (*verbose_fns[type])(obj);
691 (*brief_fns[type])(obj);
695 printf("$%s", var_name(var));
698 printf("$%s=", var_name(var));
699 (*brief_fns[type])(obj);
713 void print(lispobj obj)
725 void brief_print(lispobj obj)
739 brief_print(lispobj obj)
741 printf("lispobj 0x%lx\n", (unsigned long)obj);
744 #endif /* defined(LISP_FEATURE_SB_LDB) */