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 "thread.h" /* genesis/primitive-objects.h needs this */
37 #include "genesis/primitive-objects.h"
38 #include "genesis/static-symbols.h"
39 #include "genesis/tagnames.h"
41 static int max_lines = 20, cur_lines = 0;
42 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
43 static int max_length = 5;
44 static boolean dont_descend = 0, skip_newline = 0;
45 static int cur_clock = 0;
47 static void print_obj(char *prefix, lispobj obj);
49 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
51 static void indent(int in)
53 static char *spaces = " ";
56 fputs(spaces, stdout);
60 fputs(spaces + 64 - in, stdout);
63 static boolean continue_p(boolean newline)
67 if (cur_depth >= max_depth || dont_descend)
76 if (cur_lines >= max_lines) {
80 if (fgets(buffer, sizeof(buffer), stdin)) {
81 if (buffer[0] == 'n' || buffer[0] == 'N')
86 printf("\nUnable to read response, assuming y.\n");
95 static void newline(char *label)
101 indent(cur_depth * 2);
105 static void print_unknown(lispobj obj)
107 printf("unknown object: %p", (void *)obj);
110 static void brief_fixnum(lispobj obj)
112 /* KLUDGE: Rather than update the tables in print_obj(), we
113 declare all fixnum-or-unknown tags to be fixnums and sort it
114 out here with a guard clause. */
115 if (!fixnump(obj)) return print_unknown(obj);
117 #ifndef LISP_FEATURE_ALPHA
118 printf("%ld", ((long)obj)>>2);
120 printf("%d", ((s32)obj)>>2);
124 static void print_fixnum(lispobj obj)
126 /* KLUDGE: Rather than update the tables in print_obj(), we
127 declare all fixnum-or-unknown tags to be fixnums and sort it
128 out here with a guard clause. */
129 if (!fixnump(obj)) return print_unknown(obj);
131 #ifndef LISP_FEATURE_ALPHA
132 printf(": %ld", ((long)obj)>>2);
134 printf(": %d", ((s32)obj)>>2);
138 static void brief_otherimm(lispobj obj)
143 type = widetag_of(obj);
145 case CHARACTER_WIDETAG:
152 printf("#\\Newline");
155 printf("#\\Backspace");
161 strcpy(buffer, "#\\");
163 strcat(buffer, "m-");
167 strcat(buffer, "c-");
170 printf("%s%c", buffer, c);
175 case UNBOUND_MARKER_WIDETAG:
176 printf("<unbound marker>");
180 printf("%s", widetag_names[type >> 2]);
185 static void print_otherimm(lispobj obj)
187 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
189 switch (widetag_of(obj)) {
190 case CHARACTER_WIDETAG:
196 case UNBOUND_MARKER_WIDETAG:
200 printf(": data=%ld", (long) (obj>>8)&0xffffff);
205 static void brief_list(lispobj obj)
210 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
211 printf("(invalid Lisp-level address)");
216 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
217 struct cons *cons = (struct cons *)native_pointer(obj);
221 if (++length >= max_length) {
226 print_obj("", cons->car);
240 static void print_list(lispobj obj)
242 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
243 printf("(invalid address)");
244 } else if (obj == NIL) {
247 struct cons *cons = (struct cons *)native_pointer(obj);
249 print_obj("car: ", cons->car);
250 print_obj("cdr: ", cons->cdr);
254 static void brief_struct(lispobj obj)
256 struct instance *instance = (struct instance *)native_pointer(obj);
257 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
258 printf("(invalid address)");
260 printf("#<ptr to 0x%08lx instance>",
261 (unsigned long) instance->slots[0]);
265 static void print_struct(lispobj obj)
267 struct instance *instance = (struct instance *)native_pointer(obj);
270 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
271 printf("(invalid address)");
273 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
274 for (i = 1; i < HeaderValue(instance->header); i++) {
275 sprintf(buffer, "slot %d: ", i);
276 print_obj(buffer, instance->slots[i]);
281 static void brief_otherptr(lispobj obj)
283 lispobj *ptr, header;
285 struct symbol *symbol;
286 struct vector *vector;
289 ptr = (lispobj *) native_pointer(obj);
291 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
292 printf("(invalid address)");
297 type = widetag_of(header);
299 case SYMBOL_HEADER_WIDETAG:
300 symbol = (struct symbol *)ptr;
301 vector = (struct vector *)native_pointer(symbol->name);
302 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
309 case SIMPLE_BASE_STRING_WIDETAG:
310 vector = (struct vector *)ptr;
312 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
322 brief_otherimm(header);
327 static void print_slots(char **slots, int count, lispobj *ptr)
329 while (count-- > 0) {
331 print_obj(*slots++, *ptr++);
333 print_obj("???: ", *ptr++);
338 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
339 * perhaps be generated automatically by GENESIS as part of
341 static char *symbol_slots[] = {"value: ", "hash: ",
342 "plist: ", "name: ", "package: ",
343 #ifdef LISP_FEATURE_SB_THREAD
347 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
348 static char *complex_slots[] = {"real: ", "imag: ", NULL};
349 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
350 static char *fn_slots[] = {
351 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
352 static char *closure_slots[] = {"fn: ", NULL};
353 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
354 static char *weak_pointer_slots[] = {"value: ", NULL};
355 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
356 static char *value_cell_slots[] = {"value: ", NULL};
358 static void print_otherptr(lispobj obj)
360 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
361 printf("(invalid address)");
363 #ifndef LISP_FEATURE_ALPHA
365 unsigned long header;
366 unsigned long length;
372 int count, type, index;
373 char *cptr, buffer[16];
375 ptr = (lispobj*) native_pointer(obj);
377 printf(" (NULL Pointer)");
382 length = fixnum_value(*ptr);
383 count = HeaderValue(header);
384 type = widetag_of(header);
386 print_obj("header: ", header);
387 if (!other_immediate_lowtag_p(header)) {
389 printf("(invalid header object)");
399 printf("%08lx", (unsigned long) *--ptr);
403 print_slots(ratio_slots, count, ptr);
406 case COMPLEX_WIDETAG:
407 print_slots(complex_slots, count, ptr);
410 case SYMBOL_HEADER_WIDETAG:
411 print_slots(symbol_slots, count, ptr);
414 #if N_WORD_BITS == 32
415 case SINGLE_FLOAT_WIDETAG:
417 printf("%g", ((struct single_float *)native_pointer(obj))->value);
420 case DOUBLE_FLOAT_WIDETAG:
422 printf("%g", ((struct double_float *)native_pointer(obj))->value);
425 #ifdef LONG_FLOAT_WIDETAG
426 case LONG_FLOAT_WIDETAG:
428 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
432 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
433 case COMPLEX_SINGLE_FLOAT_WIDETAG:
435 #ifdef LISP_FEATURE_X86_64
436 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
438 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
441 #ifdef LISP_FEATURE_X86_64
442 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
444 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
449 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
450 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
452 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
454 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
458 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
459 case COMPLEX_LONG_FLOAT_WIDETAG:
461 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
463 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
467 case SIMPLE_BASE_STRING_WIDETAG:
468 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
469 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
472 cptr = (char *)(ptr+1);
479 case SIMPLE_VECTOR_WIDETAG:
481 printf("length = %ld", length);
484 while (length-- > 0) {
485 sprintf(buffer, "%d: ", index++);
486 print_obj(buffer, *ptr++);
490 case INSTANCE_HEADER_WIDETAG:
492 printf("length = %ld", (long) count);
494 while (count-- > 0) {
495 sprintf(buffer, "%d: ", index++);
496 print_obj(buffer, *ptr++);
500 case SIMPLE_ARRAY_WIDETAG:
501 case SIMPLE_BIT_VECTOR_WIDETAG:
502 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
503 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
504 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
505 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
506 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
507 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
509 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
511 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
512 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
513 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
514 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
516 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
517 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
519 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
520 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
522 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
523 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
526 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
528 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
529 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
531 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
532 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
534 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
535 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
536 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
537 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
539 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
540 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
542 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
543 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
545 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
546 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
548 case COMPLEX_BASE_STRING_WIDETAG:
549 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
550 case COMPLEX_CHARACTER_STRING_WIDETAG:
552 case COMPLEX_VECTOR_NIL_WIDETAG:
553 case COMPLEX_BIT_VECTOR_WIDETAG:
554 case COMPLEX_VECTOR_WIDETAG:
555 case COMPLEX_ARRAY_WIDETAG:
558 case CODE_HEADER_WIDETAG:
559 print_slots(code_slots, count-1, ptr);
562 case SIMPLE_FUN_HEADER_WIDETAG:
563 print_slots(fn_slots, 5, ptr);
566 case RETURN_PC_HEADER_WIDETAG:
567 print_obj("code: ", obj - (count * 4));
570 case CLOSURE_HEADER_WIDETAG:
571 print_slots(closure_slots, count, ptr);
574 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
575 print_slots(funcallable_instance_slots, count, ptr);
578 case VALUE_CELL_HEADER_WIDETAG:
579 print_slots(value_cell_slots, 1, ptr);
584 #ifndef LISP_FEATURE_ALPHA
585 printf("0x%08lx", (unsigned long) *ptr);
587 printf("0x%016lx", *(lispobj*)(ptr+1));
591 case WEAK_POINTER_WIDETAG:
592 print_slots(weak_pointer_slots, 1, ptr);
595 case CHARACTER_WIDETAG:
596 case UNBOUND_MARKER_WIDETAG:
598 printf("pointer to an immediate?");
602 print_slots(fdefn_slots, count, ptr);
607 printf("Unknown header object?");
613 static void print_obj(char *prefix, lispobj obj)
615 #ifdef LISP_FEATURE_X86_64
616 static void (*verbose_fns[])(lispobj obj)
617 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
618 print_fixnum, print_otherimm, print_fixnum, print_list,
619 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
620 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
621 static void (*brief_fns[])(lispobj obj)
622 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
623 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
624 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
625 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
627 static void (*verbose_fns[])(lispobj obj)
628 = {print_fixnum, print_struct, print_otherimm, print_list,
629 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
630 static void (*brief_fns[])(lispobj obj)
631 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
632 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
634 int type = lowtag_of(obj);
635 struct var *var = lookup_by_obj(obj);
637 boolean verbose = cur_depth < brief_depth;
639 if (!continue_p(verbose))
642 if (var != NULL && var_clock(var) == cur_clock)
645 if (var == NULL && is_lisp_pointer(obj))
646 var = define_var(NULL, obj, 0);
649 var_setclock(var, cur_clock);
654 sprintf(buffer, "$%s=", var_name(var));
659 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
660 if (cur_depth < brief_depth) {
661 fputs(lowtag_names[type], stdout);
662 (*verbose_fns[type])(obj);
665 (*brief_fns[type])(obj);
669 printf("$%s", var_name(var));
672 printf("$%s=", var_name(var));
673 (*brief_fns[type])(obj);
687 void print(lispobj obj)
699 void brief_print(lispobj obj)
713 brief_print(lispobj obj)
715 printf("lispobj 0x%lx\n", (unsigned long)obj);
718 #endif /* defined(LISP_FEATURE_SB_LDB) */