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 "genesis/static-symbols.h"
34 #include "genesis/primitive-objects.h"
36 static int max_lines = 20, cur_lines = 0;
37 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
38 static int max_length = 5;
39 static boolean dont_descend = 0, skip_newline = 0;
40 static int cur_clock = 0;
42 static void print_obj(char *prefix, lispobj obj);
44 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
46 char *lowtag_Names[] = {
49 "other immediate [0]",
53 "other immediate [1]",
57 /* FIXME: Yikes! This table implicitly depends on the values in sbcl.h,
58 * but doesn't actually depend on them, so if they change, it gets
59 * all broken. We should either get rid of it or
60 * rewrite the code so that it's cleanly initialized by gc_init_tables[]
61 * in a way which varies correctly with the values in sbcl.h. */
62 char *subtype_Names[] = {
69 #ifdef LONG_FLOAT_WIDETAG
73 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
74 "complex single float",
76 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
77 "complex double float",
79 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
86 "(simple-array (unsigned-byte 2) (*))",
87 "(simple-array (unsigned-byte 4) (*))",
88 "(simple-array (unsigned-byte 8) (*))",
89 "(simple-array (unsigned-byte 16) (*))",
90 "(simple-array (unsigned-byte 32) (*))",
91 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
92 "(simple-array (signed-byte 8) (*))",
94 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
95 "(simple-array (signed-byte 16) (*))",
97 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
98 "(simple-array fixnum (*))",
100 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
101 "(simple-array (signed-byte 32) (*))",
103 "(simple-array single-float (*))",
104 "(simple-array double-float (*))",
105 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
106 "(simple-array long-float (*))",
108 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
109 "(simple-array (complex single-float) (*))",
111 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
112 "(simple-array (complex double-float) (*))",
114 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
115 "(simple-array (complex long-float) (*))",
118 "complex-bit-vector",
124 "funcallable-instance header",
125 "unused function header 1",
126 "unused function header 2",
127 "unused function header 3",
128 "closure function header",
140 static void indent(int in)
142 static char *spaces = " ";
145 fputs(spaces, stdout);
149 fputs(spaces + 64 - in, stdout);
152 static boolean continue_p(boolean newline)
156 if (cur_depth >= max_depth || dont_descend)
165 if (cur_lines >= max_lines) {
166 printf("More? [y] ");
169 fgets(buffer, sizeof(buffer), stdin);
171 if (buffer[0] == 'n' || buffer[0] == 'N')
181 static void newline(char *label)
185 fputs(label, stdout);
187 indent(cur_depth * 2);
191 static void brief_fixnum(lispobj obj)
194 printf("%ld", ((long)obj)>>2);
196 printf("%d", ((s32)obj)>>2);
200 static void print_fixnum(lispobj obj)
203 printf(": %ld", ((long)obj)>>2);
205 printf(": %d", ((s32)obj)>>2);
209 static void brief_otherimm(lispobj obj)
214 type = widetag_of(obj);
216 case BASE_CHAR_WIDETAG:
223 printf("#\\Newline");
226 printf("#\\Backspace");
232 strcpy(buffer, "#\\");
234 strcat(buffer, "m-");
238 strcat(buffer, "c-");
241 printf("%s%c", buffer, c);
246 case UNBOUND_MARKER_WIDETAG:
247 printf("<unbound marker>");
252 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
253 printf("%s", lowtag_Names[idx]);
255 printf("unknown type (0x%0x)", type);
260 static void print_otherimm(lispobj obj)
264 type = widetag_of(obj);
267 if (idx < (sizeof(lowtag_Names) / sizeof(char *)))
268 printf(", %s", lowtag_Names[idx]);
270 printf(", unknown type (0x%0x)", type);
272 switch (widetag_of(obj)) {
273 case BASE_CHAR_WIDETAG:
279 case UNBOUND_MARKER_WIDETAG:
283 printf(": data=%ld", (long) (obj>>8)&0xffffff);
288 static void brief_list(lispobj obj)
293 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
294 printf("(invalid Lisp-level address)");
299 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
300 struct cons *cons = (struct cons *)native_pointer(obj);
304 if (++length >= max_length) {
309 print_obj(NULL, cons->car);
317 print_obj(NULL, obj);
323 static void print_list(lispobj obj)
325 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
326 printf("(invalid address)");
327 } else if (obj == NIL) {
330 struct cons *cons = (struct cons *)native_pointer(obj);
332 print_obj("car: ", cons->car);
333 print_obj("cdr: ", cons->cdr);
337 static void brief_struct(lispobj obj)
339 printf("#<ptr to 0x%08lx instance>",
340 (unsigned long) ((struct instance *)native_pointer(obj))->slots[0]);
343 static void print_struct(lispobj obj)
345 struct instance *instance = (struct instance *)native_pointer(obj);
348 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
349 for (i = 1; i < HeaderValue(instance->header); i++) {
350 sprintf(buffer, "slot %d: ", i);
351 print_obj(buffer, instance->slots[i]);
355 static void brief_otherptr(lispobj obj)
357 lispobj *ptr, header;
359 struct symbol *symbol;
360 struct vector *vector;
363 ptr = (lispobj *) native_pointer(obj);
365 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
366 printf("(invalid address)");
371 type = widetag_of(header);
373 case SYMBOL_HEADER_WIDETAG:
374 symbol = (struct symbol *)ptr;
375 vector = (struct vector *)native_pointer(symbol->name);
376 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
383 case SIMPLE_STRING_WIDETAG:
384 vector = (struct vector *)ptr;
386 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
396 brief_otherimm(header);
401 static void print_slots(char **slots, int count, lispobj *ptr)
403 while (count-- > 0) {
405 print_obj(*slots++, *ptr++);
407 print_obj("???: ", *ptr++);
412 /* FIXME: Yikes again! This, like subtype_Names[], needs to depend
413 * on the values in sbcl.h (or perhaps be generated automatically
414 * by GENESIS as part of 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[] = {
421 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
422 static char *closure_slots[] = {"fn: ", NULL};
423 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
424 static char *weak_pointer_slots[] = {"value: ", NULL};
425 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
426 static char *value_cell_slots[] = {"value: ", NULL};
428 static void print_otherptr(lispobj obj)
430 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
431 printf("(invalid address)");
435 unsigned long header;
436 unsigned long length;
442 int count, type, index;
443 char *cptr, buffer[16];
445 ptr = (lispobj*) native_pointer(obj);
447 printf(" (NULL Pointer)");
452 length = (*ptr) >> 2;
454 type = widetag_of(header);
456 print_obj("header: ", header);
457 if (lowtag_of(header) != OTHER_IMMEDIATE_0_LOWTAG &&
458 lowtag_of(header) != OTHER_IMMEDIATE_1_LOWTAG) {
460 printf("(invalid header object)");
470 printf("%08lx", (unsigned long) *--ptr);
474 print_slots(ratio_slots, count, ptr);
477 case COMPLEX_WIDETAG:
478 print_slots(complex_slots, count, ptr);
481 case SYMBOL_HEADER_WIDETAG:
482 print_slots(symbol_slots, count, ptr);
485 case SINGLE_FLOAT_WIDETAG:
487 printf("%g", ((struct single_float *)native_pointer(obj))->value);
490 case DOUBLE_FLOAT_WIDETAG:
492 printf("%g", ((struct double_float *)native_pointer(obj))->value);
495 #ifdef LONG_FLOAT_WIDETAG
496 case LONG_FLOAT_WIDETAG:
498 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
502 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
503 case COMPLEX_SINGLE_FLOAT_WIDETAG:
505 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
507 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
511 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
512 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
514 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
516 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
520 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
521 case COMPLEX_LONG_FLOAT_WIDETAG:
523 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
525 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
529 case SIMPLE_STRING_WIDETAG:
531 cptr = (char *)(ptr+1);
538 case SIMPLE_VECTOR_WIDETAG:
540 printf("length = %ld", length);
543 while (length-- > 0) {
544 sprintf(buffer, "%d: ", index++);
545 print_obj(buffer, *ptr++);
549 case INSTANCE_HEADER_WIDETAG:
551 printf("length = %ld", (long) count);
553 while (count-- > 0) {
554 sprintf(buffer, "%d: ", index++);
555 print_obj(buffer, *ptr++);
559 case SIMPLE_ARRAY_WIDETAG:
560 case SIMPLE_BIT_VECTOR_WIDETAG:
561 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
562 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
563 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
564 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
565 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
566 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
567 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
569 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
570 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
572 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
573 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
575 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
576 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
578 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
579 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
580 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
581 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
583 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
584 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
586 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
587 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
589 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
590 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
592 case COMPLEX_STRING_WIDETAG:
593 case COMPLEX_BIT_VECTOR_WIDETAG:
594 case COMPLEX_VECTOR_WIDETAG:
595 case COMPLEX_ARRAY_WIDETAG:
598 case CODE_HEADER_WIDETAG:
599 print_slots(code_slots, count-1, ptr);
602 case SIMPLE_FUN_HEADER_WIDETAG:
603 case CLOSURE_FUN_HEADER_WIDETAG:
604 print_slots(fn_slots, 5, ptr);
607 case RETURN_PC_HEADER_WIDETAG:
608 print_obj("code: ", obj - (count * 4));
611 case CLOSURE_HEADER_WIDETAG:
612 print_slots(closure_slots, count, ptr);
615 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
616 print_slots(funcallable_instance_slots, count, ptr);
619 case VALUE_CELL_HEADER_WIDETAG:
620 print_slots(value_cell_slots, 1, ptr);
626 printf("0x%08lx", (unsigned long) *ptr);
628 printf("0x%016lx", *(lispobj*)(ptr+1));
632 case WEAK_POINTER_WIDETAG:
633 print_slots(weak_pointer_slots, 1, ptr);
636 case BASE_CHAR_WIDETAG:
637 case UNBOUND_MARKER_WIDETAG:
639 printf("pointer to an immediate?");
643 print_slots(fdefn_slots, count, ptr);
648 printf("Unknown header object?");
654 static void print_obj(char *prefix, lispobj obj)
656 static void (*verbose_fns[])(lispobj obj)
657 = {print_fixnum, print_struct, print_otherimm, print_list,
658 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
659 static void (*brief_fns[])(lispobj obj)
660 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
661 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
662 int type = lowtag_of(obj);
663 struct var *var = lookup_by_obj(obj);
665 boolean verbose = cur_depth < brief_depth;
667 if (!continue_p(verbose))
670 if (var != NULL && var_clock(var) == cur_clock)
674 /* FIXME: What does this "x & y & z & .." expression mean? */
675 (obj & FUN_POINTER_LOWTAG & LIST_POINTER_LOWTAG & INSTANCE_POINTER_LOWTAG & OTHER_POINTER_LOWTAG) != 0)
676 var = define_var(NULL, obj, 0);
679 var_setclock(var, cur_clock);
684 sprintf(buffer, "$%s=", var_name(var));
689 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
690 if (cur_depth < brief_depth) {
691 fputs(lowtag_Names[type], stdout);
692 (*verbose_fns[type])(obj);
695 (*brief_fns[type])(obj);
699 printf("$%s", var_name(var));
702 printf("$%s=", var_name(var));
703 (*brief_fns[type])(obj);
717 void print(lispobj obj)
729 void brief_print(lispobj obj)
743 brief_print(lispobj obj)
745 printf("lispobj 0x%lx\n", (unsigned long)obj);
748 #endif /* defined(LISP_FEATURE_SB_LDB) */