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.
20 * 1. Ordinary users won't get much out of this code, so it shouldn't
21 * be compiled into the ordinary build of the system. Probably it
22 * should be made conditional on the SB-SHOW target feature.
23 * 2. Some of the code in here (subtype_Names[] and the various
24 * foo_slots[], at least) is deeply broken, depending on fixed
25 * (and already out-of-date) values in sbcl.h.
37 static int max_lines = 20, cur_lines = 0;
38 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
39 static int max_length = 5;
40 static boolean dont_descend = 0, skip_newline = 0;
43 static void print_obj(char *prefix, lispobj obj);
45 #define NEWLINE if (continue_p(1)) newline(NULL); else return;
47 char *lowtag_Names[] = {
50 "other immediate [0]",
54 "other immediate [1]",
58 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
59 * but doesn't actually depend on them, so if they change, it gets
60 * all broken. We should either get rid of it or
61 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
62 * in a way which varies correctly with the values in sbcl.h. */
63 char *subtype_Names[] = {
74 #ifdef type_ComplexSingleFloat
75 "complex single float",
77 #ifdef type_ComplexDoubleFloat
78 "complex double float",
80 #ifdef type_ComplexLongFloat
87 "(simple-array (unsigned-byte 2) (*))",
88 "(simple-array (unsigned-byte 4) (*))",
89 "(simple-array (unsigned-byte 8) (*))",
90 "(simple-array (unsigned-byte 16) (*))",
91 "(simple-array (unsigned-byte 32) (*))",
92 #ifdef type_SimpleArraySignedByte8
93 "(simple-array (signed-byte 8) (*))",
95 #ifdef type_SimpleArraySignedByte16
96 "(simple-array (signed-byte 16) (*))",
98 #ifdef type_SimpleArraySignedByte30
99 "(simple-array fixnum (*))",
101 #ifdef type_SimpleArraySignedByte32
102 "(simple-array (signed-byte 32) (*))",
104 "(simple-array single-float (*))",
105 "(simple-array double-float (*))",
106 #ifdef type_SimpleArrayLongFloat
107 "(simple-array long-float (*))",
109 #ifdef type_SimpleArrayComplexSingleFloat
110 "(simple-array (complex single-float) (*))",
112 #ifdef type_SimpleArrayComplexDoubleFloat
113 "(simple-array (complex double-float) (*))",
115 #ifdef type_SimpleArrayComplexLongFloat
116 "(simple-array (complex long-float) (*))",
119 "complex-bit-vector",
125 "funcallable-instance header",
126 "unused function header 1",
127 "unused function header 2",
128 "unused function header 3",
129 "closure function header",
141 static void indent(int in)
143 static char *spaces = " ";
146 fputs(spaces, stdout);
150 fputs(spaces + 64 - in, stdout);
153 static boolean continue_p(boolean newline)
157 if (cur_depth >= max_depth || dont_descend)
166 if (cur_lines >= max_lines) {
167 printf("More? [y] ");
170 fgets(buffer, sizeof(buffer), stdin);
172 if (buffer[0] == 'n' || buffer[0] == 'N')
182 static void newline(char *label)
186 fputs(label, stdout);
188 indent(cur_depth * 2);
192 static void brief_fixnum(lispobj obj)
195 printf("%ld", ((long)obj)>>2);
197 printf("%d", ((s32)obj)>>2);
201 static void print_fixnum(lispobj obj)
204 printf(": %ld", ((long)obj)>>2);
206 printf(": %d", ((s32)obj)>>2);
210 static void brief_otherimm(lispobj obj)
224 printf("#\\Newline");
227 printf("#\\Backspace");
233 strcpy(buffer, "#\\");
235 strcat(buffer, "m-");
239 strcat(buffer, "c-");
242 printf("%s%c", buffer, c);
247 case type_UnboundMarker:
248 printf("<unbound marker>");
253 if (idx < (sizeof(subtype_Names) / sizeof(char *)))
254 printf("%s", subtype_Names[idx]);
256 printf("unknown type (0x%0x)", type);
261 static void print_otherimm(lispobj obj)
268 if (idx < (sizeof(subtype_Names) / sizeof(char *)))
269 printf(", %s", subtype_Names[idx]);
271 printf(", unknown type (0x%0x)", type);
273 switch (TypeOf(obj)) {
280 case type_UnboundMarker:
284 printf(": data=%ld", (obj>>8)&0xffffff);
289 static void brief_list(lispobj obj)
294 if (!is_valid_lisp_addr((os_vm_address_t)obj))
295 printf("(invalid Lisp-level address)");
300 while (LowtagOf(obj) == type_ListPointer) {
301 struct cons *cons = (struct cons *)PTR(obj);
305 if (++length >= max_length) {
310 print_obj(NULL, cons->car);
318 print_obj(NULL, obj);
324 static void print_list(lispobj obj)
326 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
327 printf("(invalid address)");
328 } else if (obj == NIL) {
331 struct cons *cons = (struct cons *)PTR(obj);
333 print_obj("car: ", cons->car);
334 print_obj("cdr: ", cons->cdr);
338 static void brief_struct(lispobj obj)
340 printf("#<ptr to 0x%08lx instance>",
341 ((struct instance *)PTR(obj))->slots[0]);
344 static void print_struct(lispobj obj)
346 struct instance *instance = (struct instance *)PTR(obj);
349 print_obj("type: ", ((struct instance *)PTR(obj))->slots[0]);
350 for (i = 1; i < HeaderValue(instance->header); i++) {
351 sprintf(buffer, "slot %d: ", i);
352 print_obj(buffer, instance->slots[i]);
356 static void brief_otherptr(lispobj obj)
358 lispobj *ptr, header;
360 struct symbol *symbol;
361 struct vector *vector;
364 ptr = (lispobj *) PTR(obj);
366 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
367 printf("(invalid address)");
372 type = TypeOf(header);
374 case type_SymbolHeader:
375 symbol = (struct symbol *)ptr;
376 vector = (struct vector *)PTR(symbol->name);
377 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
384 case type_SimpleString:
385 vector = (struct vector *)ptr;
387 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
397 brief_otherimm(header);
402 static void print_slots(char **slots, int count, lispobj *ptr)
404 while (count-- > 0) {
406 print_obj(*slots++, *ptr++);
408 print_obj("???: ", *ptr++);
413 /* FIXME: Yikes again! This, like subtype_Names[], needs to depend
414 * on the values in sbcl.h. */
415 static char *symbol_slots[] = {"value: ", "unused: ",
416 "plist: ", "name: ", "package: ", NULL};
417 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
418 static char *complex_slots[] = {"real: ", "imag: ", NULL};
419 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
420 static char *fn_slots[] = {"self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
421 static char *closure_slots[] = {"fn: ", NULL};
422 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
423 static char *weak_pointer_slots[] = {"value: ", NULL};
424 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
425 static char *value_cell_slots[] = {"value: ", NULL};
427 static void print_otherptr(lispobj obj)
429 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
430 printf("(invalid address)");
434 unsigned long header;
435 unsigned long length;
441 int count, type, index;
442 char *cptr, buffer[16];
444 ptr = (lispobj*) PTR(obj);
446 printf(" (NULL Pointer)");
451 length = (*ptr) >> 2;
453 type = TypeOf(header);
455 print_obj("header: ", header);
456 if (LowtagOf(header) != type_OtherImmediate0 && LowtagOf(header) != type_OtherImmediate1) {
458 printf("(invalid header object)");
468 printf("%08lx", *--ptr);
472 print_slots(ratio_slots, count, ptr);
476 print_slots(complex_slots, count, ptr);
479 case type_SymbolHeader:
480 print_slots(symbol_slots, count, ptr);
483 case type_SingleFloat:
485 printf("%g", ((struct single_float *)PTR(obj))->value);
488 case type_DoubleFloat:
490 printf("%g", ((struct double_float *)PTR(obj))->value);
493 #ifdef type_LongFloat
496 printf("%Lg", ((struct long_float *)PTR(obj))->value);
500 #ifdef type_ComplexSingleFloat
501 case type_ComplexSingleFloat:
503 printf("%g", ((struct complex_single_float *)PTR(obj))->real);
505 printf("%g", ((struct complex_single_float *)PTR(obj))->imag);
509 #ifdef type_ComplexDoubleFloat
510 case type_ComplexDoubleFloat:
512 printf("%g", ((struct complex_double_float *)PTR(obj))->real);
514 printf("%g", ((struct complex_double_float *)PTR(obj))->imag);
518 #ifdef type_ComplexLongFloat
519 case type_ComplexLongFloat:
521 printf("%Lg", ((struct complex_long_float *)PTR(obj))->real);
523 printf("%Lg", ((struct complex_long_float *)PTR(obj))->imag);
527 case type_SimpleString:
529 cptr = (char *)(ptr+1);
536 case type_SimpleVector:
537 case type_InstanceHeader:
539 printf("length = %ld", length);
542 while (length-- > 0) {
543 sprintf(buffer, "%d: ", index++);
544 print_obj(buffer, *ptr++);
548 case type_SimpleArray:
549 case type_SimpleBitVector:
550 case type_SimpleArrayUnsignedByte2:
551 case type_SimpleArrayUnsignedByte4:
552 case type_SimpleArrayUnsignedByte8:
553 case type_SimpleArrayUnsignedByte16:
554 case type_SimpleArrayUnsignedByte32:
555 #ifdef type_SimpleArraySignedByte8
556 case type_SimpleArraySignedByte8:
558 #ifdef type_SimpleArraySignedByte16
559 case type_SimpleArraySignedByte16:
561 #ifdef type_SimpleArraySignedByte30
562 case type_SimpleArraySignedByte30:
564 #ifdef type_SimpleArraySignedByte32
565 case type_SimpleArraySignedByte32:
567 case type_SimpleArraySingleFloat:
568 case type_SimpleArrayDoubleFloat:
569 #ifdef type_SimpleArrayLongFloat
570 case type_SimpleArrayLongFloat:
572 #ifdef type_SimpleArrayComplexSingleFloat
573 case type_SimpleArrayComplexSingleFloat:
575 #ifdef type_SimpleArrayComplexDoubleFloat
576 case type_SimpleArrayComplexDoubleFloat:
578 #ifdef type_SimpleArrayComplexLongFloat
579 case type_SimpleArrayComplexLongFloat:
581 case type_ComplexString:
582 case type_ComplexBitVector:
583 case type_ComplexVector:
584 case type_ComplexArray:
587 case type_CodeHeader:
588 print_slots(code_slots, count-1, ptr);
591 case type_FunctionHeader:
592 case type_ClosureFunctionHeader:
593 print_slots(fn_slots, 5, ptr);
596 case type_ReturnPcHeader:
597 print_obj("code: ", obj - (count * 4));
600 case type_ClosureHeader:
601 print_slots(closure_slots, count, ptr);
604 case type_FuncallableInstanceHeader:
605 print_slots(funcallable_instance_slots, count, ptr);
608 case type_ValueCellHeader:
609 print_slots(value_cell_slots, 1, ptr);
615 printf("0x%08lx", *ptr);
617 printf("0x%016lx", *(long*)(ptr+1));
621 case type_WeakPointer:
622 print_slots(weak_pointer_slots, 1, ptr);
626 case type_UnboundMarker:
628 printf("pointer to an immediate?");
632 print_slots(fdefn_slots, count, ptr);
637 printf("Unknown header object?");
643 static void print_obj(char *prefix, lispobj obj)
645 static void (*verbose_fns[])(lispobj obj)
646 = {print_fixnum, print_otherptr, print_otherimm, print_list,
647 print_fixnum, print_struct, print_otherimm, print_otherptr};
648 static void (*brief_fns[])(lispobj obj)
649 = {brief_fixnum, brief_otherptr, brief_otherimm, brief_list,
650 brief_fixnum, brief_struct, brief_otherimm, brief_otherptr};
651 int type = LowtagOf(obj);
652 struct var *var = lookup_by_obj(obj);
654 boolean verbose = cur_depth < brief_depth;
657 if (!continue_p(verbose))
660 if (var != NULL && var_clock(var) == cur_clock)
663 if (var == NULL && (obj & type_FunctionPointer & type_ListPointer & type_InstancePointer & type_OtherPointer) != 0)
664 var = define_var(NULL, obj, 0);
667 var_setclock(var, cur_clock);
672 sprintf(buffer, "$%s=", var_name(var));
677 printf("%s0x%08lx: ", prefix, obj);
678 if (cur_depth < brief_depth) {
679 fputs(lowtag_Names[type], stdout);
680 (*verbose_fns[type])(obj);
683 (*brief_fns[type])(obj);
687 printf("$%s", var_name(var));
690 printf("$%s=", var_name(var));
691 (*brief_fns[type])(obj);
705 void print(lispobj obj)
717 void brief_print(lispobj obj)