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 char *lowtag_Names[] = {
55 "other immediate [0]",
59 "other immediate [1]",
63 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
64 * but doesn't actually depend on them, so if they change, it gets
65 * all broken. We should either get rid of it or
66 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
67 * in a way which varies correctly with the values in sbcl.h. */
68 char *subtype_Names[] = {
75 #ifdef LONG_FLOAT_WIDETAG
79 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
80 "complex single float",
82 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
83 "complex double float",
85 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
92 "(simple-array (unsigned-byte 2) (*))",
93 "(simple-array (unsigned-byte 4) (*))",
94 "(simple-array (unsigned-byte 8) (*))",
95 "(simple-array (unsigned-byte 16) (*))",
96 "(simple-array (unsigned-byte 32) (*))",
97 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
98 "(simple-array (signed-byte 8) (*))",
100 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
101 "(simple-array (signed-byte 16) (*))",
103 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
104 "(simple-array fixnum (*))",
106 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
107 "(simple-array (signed-byte 32) (*))",
109 "(simple-array single-float (*))",
110 "(simple-array double-float (*))",
111 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
112 "(simple-array long-float (*))",
114 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
115 "(simple-array (complex single-float) (*))",
117 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
118 "(simple-array (complex double-float) (*))",
120 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
121 "(simple-array (complex long-float) (*))",
124 "complex-bit-vector",
130 "funcallable-instance header",
131 "unused function header 1",
132 "unused function header 2",
133 "unused function header 3",
134 "closure function header",
146 static void indent(int in)
148 static char *spaces = " ";
151 fputs(spaces, stdout);
155 fputs(spaces + 64 - in, stdout);
158 static boolean continue_p(boolean newline)
162 if (cur_depth >= max_depth || dont_descend)
171 if (cur_lines >= max_lines) {
172 printf("More? [y] ");
175 fgets(buffer, sizeof(buffer), stdin);
177 if (buffer[0] == 'n' || buffer[0] == 'N')
187 static void newline(char *label)
191 fputs(label, stdout);
193 indent(cur_depth * 2);
197 static void brief_fixnum(lispobj obj)
199 #ifndef LISP_FEATURE_ALPHA
200 printf("%ld", ((long)obj)>>2);
202 printf("%d", ((s32)obj)>>2);
206 static void print_fixnum(lispobj obj)
208 #ifndef LISP_FEATURE_ALPHA
209 printf(": %ld", ((long)obj)>>2);
211 printf(": %d", ((s32)obj)>>2);
215 static void brief_otherimm(lispobj obj)
220 type = widetag_of(obj);
222 case CHARACTER_WIDETAG:
229 printf("#\\Newline");
232 printf("#\\Backspace");
238 strcpy(buffer, "#\\");
240 strcat(buffer, "m-");
244 strcat(buffer, "c-");
247 printf("%s%c", buffer, c);
252 case UNBOUND_MARKER_WIDETAG:
253 printf("<unbound marker>");
258 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
259 printf("%s", lowtag_Names[idx]);
261 printf("unknown type (0x%0x)", type);
266 static void print_otherimm(lispobj obj)
270 type = widetag_of(obj);
273 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
274 printf(", %s", lowtag_Names[idx]);
276 printf(", unknown type (0x%0x)", type);
278 switch (widetag_of(obj)) {
279 case CHARACTER_WIDETAG:
285 case UNBOUND_MARKER_WIDETAG:
289 printf(": data=%ld", (long) (obj>>8)&0xffffff);
294 static void brief_list(lispobj obj)
299 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
300 printf("(invalid Lisp-level address)");
305 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
306 struct cons *cons = (struct cons *)native_pointer(obj);
310 if (++length >= max_length) {
315 print_obj(NULL, cons->car);
323 print_obj(NULL, obj);
329 static void print_list(lispobj obj)
331 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
332 printf("(invalid address)");
333 } else if (obj == NIL) {
336 struct cons *cons = (struct cons *)native_pointer(obj);
338 print_obj("car: ", cons->car);
339 print_obj("cdr: ", cons->cdr);
343 static void brief_struct(lispobj obj)
345 printf("#<ptr to 0x%08lx instance>",
346 (unsigned long) ((struct instance *)native_pointer(obj))->slots[0]);
349 static void print_struct(lispobj obj)
351 struct instance *instance = (struct instance *)native_pointer(obj);
354 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
355 for (i = 1; i < HeaderValue(instance->header); i++) {
356 sprintf(buffer, "slot %d: ", i);
357 print_obj(buffer, instance->slots[i]);
361 static void brief_otherptr(lispobj obj)
363 lispobj *ptr, header;
365 struct symbol *symbol;
366 struct vector *vector;
369 ptr = (lispobj *) native_pointer(obj);
371 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
372 printf("(invalid address)");
377 type = widetag_of(header);
379 case SYMBOL_HEADER_WIDETAG:
380 symbol = (struct symbol *)ptr;
381 vector = (struct vector *)native_pointer(symbol->name);
382 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
389 case SIMPLE_BASE_STRING_WIDETAG:
390 vector = (struct vector *)ptr;
392 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
402 brief_otherimm(header);
407 static void print_slots(char **slots, int count, lispobj *ptr)
409 while (count-- > 0) {
411 print_obj(*slots++, *ptr++);
413 print_obj("???: ", *ptr++);
418 /* FIXME: Yikes again! This, like subtype_Names[], needs to depend
419 * on the values in sbcl.h (or perhaps be generated automatically
420 * by GENESIS as part of sbcl.h). */
421 static char *symbol_slots[] = {"value: ", "unused: ",
422 "plist: ", "name: ", "package: ",
423 #ifdef LISP_FEATURE_SB_THREAD
427 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
428 static char *complex_slots[] = {"real: ", "imag: ", NULL};
429 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
430 static char *fn_slots[] = {
431 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
432 static char *closure_slots[] = {"fn: ", NULL};
433 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
434 static char *weak_pointer_slots[] = {"value: ", NULL};
435 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
436 static char *value_cell_slots[] = {"value: ", NULL};
438 static void print_otherptr(lispobj obj)
440 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
441 printf("(invalid address)");
443 #ifndef LISP_FEATURE_ALPHA
445 unsigned long header;
446 unsigned long length;
452 int count, type, index;
453 char *cptr, buffer[16];
455 ptr = (lispobj*) native_pointer(obj);
457 printf(" (NULL Pointer)");
462 length = (*ptr) >> 2;
464 type = widetag_of(header);
466 print_obj("header: ", header);
467 if (lowtag_of(header) != OTHER_IMMEDIATE_0_LOWTAG &&
468 lowtag_of(header) != OTHER_IMMEDIATE_1_LOWTAG) {
470 printf("(invalid header object)");
480 printf("%08lx", (unsigned long) *--ptr);
484 print_slots(ratio_slots, count, ptr);
487 case COMPLEX_WIDETAG:
488 print_slots(complex_slots, count, ptr);
491 case SYMBOL_HEADER_WIDETAG:
492 print_slots(symbol_slots, count, ptr);
495 #if N_WORD_BITS == 32
496 case SINGLE_FLOAT_WIDETAG:
498 printf("%g", ((struct single_float *)native_pointer(obj))->value);
501 case DOUBLE_FLOAT_WIDETAG:
503 printf("%g", ((struct double_float *)native_pointer(obj))->value);
506 #ifdef LONG_FLOAT_WIDETAG
507 case LONG_FLOAT_WIDETAG:
509 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
513 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
514 case COMPLEX_SINGLE_FLOAT_WIDETAG:
516 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
518 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
522 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
523 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
525 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
527 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
531 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
532 case COMPLEX_LONG_FLOAT_WIDETAG:
534 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
536 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
540 case SIMPLE_BASE_STRING_WIDETAG:
541 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
542 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
545 cptr = (char *)(ptr+1);
552 case SIMPLE_VECTOR_WIDETAG:
554 printf("length = %ld", length);
557 while (length-- > 0) {
558 sprintf(buffer, "%d: ", index++);
559 print_obj(buffer, *ptr++);
563 case INSTANCE_HEADER_WIDETAG:
565 printf("length = %ld", (long) count);
567 while (count-- > 0) {
568 sprintf(buffer, "%d: ", index++);
569 print_obj(buffer, *ptr++);
573 case SIMPLE_ARRAY_WIDETAG:
574 case SIMPLE_BIT_VECTOR_WIDETAG:
575 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
576 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
577 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
578 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
579 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
580 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
581 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
583 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
584 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
586 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
587 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
589 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
590 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
592 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
593 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
594 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
595 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
597 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
598 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
600 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
601 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
603 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
604 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
606 case COMPLEX_BASE_STRING_WIDETAG:
607 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
608 case COMPLEX_CHARACTER_STRING_WIDETAG:
610 case COMPLEX_VECTOR_NIL_WIDETAG:
611 case COMPLEX_BIT_VECTOR_WIDETAG:
612 case COMPLEX_VECTOR_WIDETAG:
613 case COMPLEX_ARRAY_WIDETAG:
616 case CODE_HEADER_WIDETAG:
617 print_slots(code_slots, count-1, ptr);
620 case SIMPLE_FUN_HEADER_WIDETAG:
621 print_slots(fn_slots, 5, ptr);
624 case RETURN_PC_HEADER_WIDETAG:
625 print_obj("code: ", obj - (count * 4));
628 case CLOSURE_HEADER_WIDETAG:
629 print_slots(closure_slots, count, ptr);
632 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
633 print_slots(funcallable_instance_slots, count, ptr);
636 case VALUE_CELL_HEADER_WIDETAG:
637 print_slots(value_cell_slots, 1, ptr);
642 #ifndef LISP_FEATURE_ALPHA
643 printf("0x%08lx", (unsigned long) *ptr);
645 printf("0x%016lx", *(lispobj*)(ptr+1));
649 case WEAK_POINTER_WIDETAG:
650 print_slots(weak_pointer_slots, 1, ptr);
653 case CHARACTER_WIDETAG:
654 case UNBOUND_MARKER_WIDETAG:
656 printf("pointer to an immediate?");
660 print_slots(fdefn_slots, count, ptr);
665 printf("Unknown header object?");
671 static void print_obj(char *prefix, lispobj obj)
673 static void (*verbose_fns[])(lispobj obj)
674 = {print_fixnum, print_struct, print_otherimm, print_list,
675 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
676 static void (*brief_fns[])(lispobj obj)
677 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
678 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
679 int type = lowtag_of(obj);
680 struct var *var = lookup_by_obj(obj);
682 boolean verbose = cur_depth < brief_depth;
684 if (!continue_p(verbose))
687 if (var != NULL && var_clock(var) == cur_clock)
691 /* FIXME: What does this "x & y & z & .." expression mean? */
692 (obj & FUN_POINTER_LOWTAG & LIST_POINTER_LOWTAG & INSTANCE_POINTER_LOWTAG & OTHER_POINTER_LOWTAG) != 0)
693 var = define_var(NULL, obj, 0);
696 var_setclock(var, cur_clock);
701 sprintf(buffer, "$%s=", var_name(var));
706 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
707 if (cur_depth < brief_depth) {
708 fputs(lowtag_Names[type], stdout);
709 (*verbose_fns[type])(obj);
712 (*brief_fns[type])(obj);
716 printf("$%s", var_name(var));
719 printf("$%s=", var_name(var));
720 (*brief_fns[type])(obj);
734 void print(lispobj obj)
746 void brief_print(lispobj obj)
760 brief_print(lispobj obj)
762 printf("lispobj 0x%lx\n", (unsigned long)obj);
765 #endif /* defined(LISP_FEATURE_SB_LDB) */