1 /* code for low-level debugging 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 * 1. Ordinary users won't get much out of this code, so it shouldn't
17 * be compiled into the ordinary build of the system. Probably it
18 * should be made conditional on the SB-SHOW target feature.
19 * 2. Some of the code in here (subtype_Names[] and the various
20 * foo_slots[], at least) is deeply broken, depending on fixed
21 * (and already out-of-date) values in sbcl.h.
33 static int max_lines = 20, cur_lines = 0;
34 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
35 static int max_length = 5;
36 static boolean dont_descend = 0, skip_newline = 0;
37 static int cur_clock = 0;
39 static void print_obj(char *prefix, lispobj obj);
41 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
43 char *lowtag_Names[] = {
46 "other immediate [0]",
50 "other immediate [1]",
54 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
55 * but doesn't actually depend on them, so if they change, it gets
56 * all broken. We should either get rid of it or
57 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
58 * in a way which varies correctly with the values in sbcl.h. */
59 char *subtype_Names[] = {
70 #ifdef type_ComplexSingleFloat
71 "complex single float",
73 #ifdef type_ComplexDoubleFloat
74 "complex double float",
76 #ifdef type_ComplexLongFloat
83 "(simple-array (unsigned-byte 2) (*))",
84 "(simple-array (unsigned-byte 4) (*))",
85 "(simple-array (unsigned-byte 8) (*))",
86 "(simple-array (unsigned-byte 16) (*))",
87 "(simple-array (unsigned-byte 32) (*))",
88 #ifdef type_SimpleArraySignedByte8
89 "(simple-array (signed-byte 8) (*))",
91 #ifdef type_SimpleArraySignedByte16
92 "(simple-array (signed-byte 16) (*))",
94 #ifdef type_SimpleArraySignedByte30
95 "(simple-array fixnum (*))",
97 #ifdef type_SimpleArraySignedByte32
98 "(simple-array (signed-byte 32) (*))",
100 "(simple-array single-float (*))",
101 "(simple-array double-float (*))",
102 #ifdef type_SimpleArrayLongFloat
103 "(simple-array long-float (*))",
105 #ifdef type_SimpleArrayComplexSingleFloat
106 "(simple-array (complex single-float) (*))",
108 #ifdef type_SimpleArrayComplexDoubleFloat
109 "(simple-array (complex double-float) (*))",
111 #ifdef type_SimpleArrayComplexLongFloat
112 "(simple-array (complex long-float) (*))",
115 "complex-bit-vector",
121 "funcallable-instance header",
122 "unused function header 1",
123 "unused function header 2",
124 "unused function header 3",
125 "closure function header",
137 static void indent(int in)
139 static char *spaces = " ";
142 fputs(spaces, stdout);
146 fputs(spaces + 64 - in, stdout);
149 static boolean continue_p(boolean newline)
153 if (cur_depth >= max_depth || dont_descend)
162 if (cur_lines >= max_lines) {
163 printf("More? [y] ");
166 fgets(buffer, sizeof(buffer), stdin);
168 if (buffer[0] == 'n' || buffer[0] == 'N')
178 static void newline(char *label)
182 fputs(label, stdout);
184 indent(cur_depth * 2);
188 static void brief_fixnum(lispobj obj)
191 printf("%ld", ((long)obj)>>2);
193 printf("%d", ((s32)obj)>>2);
197 static void print_fixnum(lispobj obj)
200 printf(": %ld", ((long)obj)>>2);
202 printf(": %d", ((s32)obj)>>2);
206 static void brief_otherimm(lispobj obj)
220 printf("#\\Newline");
223 printf("#\\Backspace");
229 strcpy(buffer, "#\\");
231 strcat(buffer, "m-");
235 strcat(buffer, "c-");
238 printf("%s%c", buffer, c);
243 case type_UnboundMarker:
244 printf("<unbound marker>");
249 if (idx < (sizeof(subtype_Names) / sizeof(char *)))
250 printf("%s", subtype_Names[idx]);
252 printf("unknown type (0x%0x)", type);
257 static void print_otherimm(lispobj obj)
264 if (idx < (sizeof(subtype_Names) / sizeof(char *)))
265 printf(", %s", subtype_Names[idx]);
267 printf(", unknown type (0x%0x)", type);
269 switch (TypeOf(obj)) {
276 case type_UnboundMarker:
280 printf(": data=%ld", (long) (obj>>8)&0xffffff);
285 static void brief_list(lispobj obj)
290 if (!is_valid_lisp_addr((os_vm_address_t)obj))
291 printf("(invalid Lisp-level address)");
296 while (LowtagOf(obj) == type_ListPointer) {
297 struct cons *cons = (struct cons *)PTR(obj);
301 if (++length >= max_length) {
306 print_obj(NULL, cons->car);
314 print_obj(NULL, obj);
320 static void print_list(lispobj obj)
322 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
323 printf("(invalid address)");
324 } else if (obj == NIL) {
327 struct cons *cons = (struct cons *)PTR(obj);
329 print_obj("car: ", cons->car);
330 print_obj("cdr: ", cons->cdr);
334 static void brief_struct(lispobj obj)
336 printf("#<ptr to 0x%08lx instance>",
337 (unsigned long) ((struct instance *)PTR(obj))->slots[0]);
340 static void print_struct(lispobj obj)
342 struct instance *instance = (struct instance *)PTR(obj);
345 print_obj("type: ", ((struct instance *)PTR(obj))->slots[0]);
346 for (i = 1; i < HeaderValue(instance->header); i++) {
347 sprintf(buffer, "slot %d: ", i);
348 print_obj(buffer, instance->slots[i]);
352 static void brief_otherptr(lispobj obj)
354 lispobj *ptr, header;
356 struct symbol *symbol;
357 struct vector *vector;
360 ptr = (lispobj *) PTR(obj);
362 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
363 printf("(invalid address)");
368 type = TypeOf(header);
370 case type_SymbolHeader:
371 symbol = (struct symbol *)ptr;
372 vector = (struct vector *)PTR(symbol->name);
373 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
380 case type_SimpleString:
381 vector = (struct vector *)ptr;
383 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
393 brief_otherimm(header);
398 static void print_slots(char **slots, int count, lispobj *ptr)
400 while (count-- > 0) {
402 print_obj(*slots++, *ptr++);
404 print_obj("???: ", *ptr++);
409 /* FIXME: Yikes again! This, like subtype_Names[], needs to depend
410 * on the values in sbcl.h. */
411 static char *symbol_slots[] = {"value: ", "unused: ",
412 "plist: ", "name: ", "package: ", NULL};
413 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
414 static char *complex_slots[] = {"real: ", "imag: ", NULL};
415 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
416 static char *fn_slots[] = {"self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
417 static char *closure_slots[] = {"fn: ", NULL};
418 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
419 static char *weak_pointer_slots[] = {"value: ", NULL};
420 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
421 static char *value_cell_slots[] = {"value: ", NULL};
423 static void print_otherptr(lispobj obj)
425 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
426 printf("(invalid address)");
430 unsigned long header;
431 unsigned long length;
437 int count, type, index;
438 char *cptr, buffer[16];
440 ptr = (lispobj*) PTR(obj);
442 printf(" (NULL Pointer)");
447 length = (*ptr) >> 2;
449 type = TypeOf(header);
451 print_obj("header: ", header);
452 if (LowtagOf(header) != type_OtherImmediate0 && LowtagOf(header) != type_OtherImmediate1) {
454 printf("(invalid header object)");
464 printf("%08lx", (unsigned long) *--ptr);
468 print_slots(ratio_slots, count, ptr);
472 print_slots(complex_slots, count, ptr);
475 case type_SymbolHeader:
476 print_slots(symbol_slots, count, ptr);
479 case type_SingleFloat:
481 printf("%g", ((struct single_float *)PTR(obj))->value);
484 case type_DoubleFloat:
486 printf("%g", ((struct double_float *)PTR(obj))->value);
489 #ifdef type_LongFloat
492 printf("%Lg", ((struct long_float *)PTR(obj))->value);
496 #ifdef type_ComplexSingleFloat
497 case type_ComplexSingleFloat:
499 printf("%g", ((struct complex_single_float *)PTR(obj))->real);
501 printf("%g", ((struct complex_single_float *)PTR(obj))->imag);
505 #ifdef type_ComplexDoubleFloat
506 case type_ComplexDoubleFloat:
508 printf("%g", ((struct complex_double_float *)PTR(obj))->real);
510 printf("%g", ((struct complex_double_float *)PTR(obj))->imag);
514 #ifdef type_ComplexLongFloat
515 case type_ComplexLongFloat:
517 printf("%Lg", ((struct complex_long_float *)PTR(obj))->real);
519 printf("%Lg", ((struct complex_long_float *)PTR(obj))->imag);
523 case type_SimpleString:
525 cptr = (char *)(ptr+1);
532 case type_SimpleVector:
534 printf("length = %ld", length);
537 while (length-- > 0) {
538 sprintf(buffer, "%d: ", index++);
539 print_obj(buffer, *ptr++);
543 case type_InstanceHeader:
545 printf("length = %ld", (long) count);
547 while (count-- > 0) {
548 sprintf(buffer, "%d: ", index++);
549 print_obj(buffer, *ptr++);
553 case type_SimpleArray:
554 case type_SimpleBitVector:
555 case type_SimpleArrayUnsignedByte2:
556 case type_SimpleArrayUnsignedByte4:
557 case type_SimpleArrayUnsignedByte8:
558 case type_SimpleArrayUnsignedByte16:
559 case type_SimpleArrayUnsignedByte32:
560 #ifdef type_SimpleArraySignedByte8
561 case type_SimpleArraySignedByte8:
563 #ifdef type_SimpleArraySignedByte16
564 case type_SimpleArraySignedByte16:
566 #ifdef type_SimpleArraySignedByte30
567 case type_SimpleArraySignedByte30:
569 #ifdef type_SimpleArraySignedByte32
570 case type_SimpleArraySignedByte32:
572 case type_SimpleArraySingleFloat:
573 case type_SimpleArrayDoubleFloat:
574 #ifdef type_SimpleArrayLongFloat
575 case type_SimpleArrayLongFloat:
577 #ifdef type_SimpleArrayComplexSingleFloat
578 case type_SimpleArrayComplexSingleFloat:
580 #ifdef type_SimpleArrayComplexDoubleFloat
581 case type_SimpleArrayComplexDoubleFloat:
583 #ifdef type_SimpleArrayComplexLongFloat
584 case type_SimpleArrayComplexLongFloat:
586 case type_ComplexString:
587 case type_ComplexBitVector:
588 case type_ComplexVector:
589 case type_ComplexArray:
592 case type_CodeHeader:
593 print_slots(code_slots, count-1, ptr);
596 case type_FunctionHeader:
597 case type_ClosureFunctionHeader:
598 print_slots(fn_slots, 5, ptr);
601 case type_ReturnPcHeader:
602 print_obj("code: ", obj - (count * 4));
605 case type_ClosureHeader:
606 print_slots(closure_slots, count, ptr);
609 case type_FuncallableInstanceHeader:
610 print_slots(funcallable_instance_slots, count, ptr);
613 case type_ValueCellHeader:
614 print_slots(value_cell_slots, 1, ptr);
620 printf("0x%08lx", (unsigned long) *ptr);
622 printf("0x%016lx", *(lispobj*)(ptr+1));
626 case type_WeakPointer:
627 print_slots(weak_pointer_slots, 1, ptr);
631 case type_UnboundMarker:
633 printf("pointer to an immediate?");
637 print_slots(fdefn_slots, count, ptr);
642 printf("Unknown header object?");
648 static void print_obj(char *prefix, lispobj obj)
650 static void (*verbose_fns[])(lispobj obj)
651 = {print_fixnum, print_otherptr, print_otherimm, print_list,
652 print_fixnum, print_struct, print_otherimm, print_otherptr};
653 static void (*brief_fns[])(lispobj obj)
654 = {brief_fixnum, brief_otherptr, brief_otherimm, brief_list,
655 brief_fixnum, brief_struct, brief_otherimm, brief_otherptr};
656 int type = LowtagOf(obj);
657 struct var *var = lookup_by_obj(obj);
659 boolean verbose = cur_depth < brief_depth;
662 if (!continue_p(verbose))
665 if (var != NULL && var_clock(var) == cur_clock)
668 if (var == NULL && (obj & type_FunctionPointer & type_ListPointer & type_InstancePointer & type_OtherPointer) != 0)
669 var = define_var(NULL, obj, 0);
672 var_setclock(var, cur_clock);
677 sprintf(buffer, "$%s=", var_name(var));
682 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
683 if (cur_depth < brief_depth) {
684 fputs(lowtag_Names[type], stdout);
685 (*verbose_fns[type])(obj);
688 (*brief_fns[type])(obj);
692 printf("$%s", var_name(var));
695 printf("$%s=", var_name(var));
696 (*brief_fns[type])(obj);
710 void print(lispobj obj)
722 void brief_print(lispobj obj)