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 #ifdef LISP_FEATURE_GENCGC
35 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
37 #include "genesis/static-symbols.h"
38 #include "thread.h" /* genesis/primitive-objects.h needs this */
39 #include "genesis/primitive-objects.h"
40 #include "genesis/static-symbols.h"
41 #include "genesis/tagnames.h"
43 static int max_lines = 20, cur_lines = 0;
44 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
45 static int max_length = 5;
46 static boolean dont_descend = 0, skip_newline = 0;
47 static int cur_clock = 0;
49 static void print_obj(char *prefix, lispobj obj);
51 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
53 static void indent(int in)
55 static char *spaces = " ";
58 fputs(spaces, stdout);
62 fputs(spaces + 64 - in, stdout);
65 static boolean continue_p(boolean newline)
69 if (cur_depth >= max_depth || dont_descend)
78 if (cur_lines >= max_lines) {
82 if (fgets(buffer, sizeof(buffer), stdin)) {
83 if (buffer[0] == 'n' || buffer[0] == 'N')
88 printf("\nUnable to read response, assuming y.\n");
97 static void newline(char *label)
101 fputs(label, stdout);
103 indent(cur_depth * 2);
107 static void print_unknown(lispobj obj)
109 printf("unknown object: %p", (void *)obj);
112 static void brief_fixnum(lispobj obj)
114 /* KLUDGE: Rather than update the tables in print_obj(), we
115 declare all fixnum-or-unknown tags to be fixnums and sort it
116 out here with a guard clause. */
117 if (!fixnump(obj)) return print_unknown(obj);
119 #ifndef LISP_FEATURE_ALPHA
120 printf("%ld", ((long)obj)>>2);
122 printf("%d", ((s32)obj)>>2);
126 static void print_fixnum(lispobj obj)
128 /* KLUDGE: Rather than update the tables in print_obj(), we
129 declare all fixnum-or-unknown tags to be fixnums and sort it
130 out here with a guard clause. */
131 if (!fixnump(obj)) return print_unknown(obj);
133 #ifndef LISP_FEATURE_ALPHA
134 printf(": %ld", ((long)obj)>>2);
136 printf(": %d", ((s32)obj)>>2);
140 static void brief_otherimm(lispobj obj)
145 type = widetag_of(obj);
147 case CHARACTER_WIDETAG:
154 printf("#\\Newline");
157 printf("#\\Backspace");
163 strcpy(buffer, "#\\");
165 strcat(buffer, "m-");
169 strcat(buffer, "c-");
172 printf("%s%c", buffer, c);
177 case UNBOUND_MARKER_WIDETAG:
178 printf("<unbound marker>");
182 printf("%s", widetag_names[type >> 2]);
187 static void print_otherimm(lispobj obj)
189 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
191 switch (widetag_of(obj)) {
192 case CHARACTER_WIDETAG:
198 case UNBOUND_MARKER_WIDETAG:
202 printf(": data=%ld", (long) (obj>>8)&0xffffff);
207 static void brief_list(lispobj obj)
212 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
213 printf("(invalid Lisp-level address)");
218 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
219 struct cons *cons = (struct cons *)native_pointer(obj);
223 if (++length >= max_length) {
228 print_obj("", cons->car);
242 static void print_list(lispobj obj)
244 if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
245 printf("(invalid address)");
246 } else if (obj == NIL) {
249 struct cons *cons = (struct cons *)native_pointer(obj);
251 print_obj("car: ", cons->car);
252 print_obj("cdr: ", cons->cdr);
256 static void brief_struct(lispobj obj)
258 struct instance *instance = (struct instance *)native_pointer(obj);
259 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
260 printf("(invalid address)");
262 printf("#<ptr to 0x%08lx instance>",
263 (unsigned long) instance->slots[0]);
267 static void print_struct(lispobj obj)
269 struct instance *instance = (struct instance *)native_pointer(obj);
272 if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
273 printf("(invalid address)");
275 print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
276 for (i = 1; i < HeaderValue(instance->header); i++) {
277 sprintf(buffer, "slot %d: ", i);
278 print_obj(buffer, instance->slots[i]);
283 static void brief_otherptr(lispobj obj)
285 lispobj *ptr, header;
287 struct symbol *symbol;
288 struct vector *vector;
291 ptr = (lispobj *) native_pointer(obj);
293 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
294 printf("(invalid address)");
299 type = widetag_of(header);
301 case SYMBOL_HEADER_WIDETAG:
302 symbol = (struct symbol *)ptr;
303 vector = (struct vector *)native_pointer(symbol->name);
304 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
311 case SIMPLE_BASE_STRING_WIDETAG:
312 vector = (struct vector *)ptr;
314 for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
324 brief_otherimm(header);
329 static void print_slots(char **slots, int count, lispobj *ptr)
331 while (count-- > 0) {
333 print_obj(*slots++, *ptr++);
335 print_obj("???: ", *ptr++);
340 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
341 * perhaps be generated automatically by GENESIS as part of
343 static char *symbol_slots[] = {"value: ", "hash: ",
344 "plist: ", "name: ", "package: ",
345 #ifdef LISP_FEATURE_SB_THREAD
349 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
350 static char *complex_slots[] = {"real: ", "imag: ", NULL};
351 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
352 static char *fn_slots[] = {
353 "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
354 static char *closure_slots[] = {"fn: ", NULL};
355 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
356 static char *weak_pointer_slots[] = {"value: ", NULL};
357 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
358 static char *value_cell_slots[] = {"value: ", NULL};
360 static void print_otherptr(lispobj obj)
362 if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
363 printf("(invalid address)");
365 #ifndef LISP_FEATURE_ALPHA
367 unsigned long header;
368 unsigned long length;
374 int count, type, index;
375 char *cptr, buffer[16];
377 ptr = (lispobj*) native_pointer(obj);
379 printf(" (NULL Pointer)");
384 length = fixnum_value(*ptr);
385 count = HeaderValue(header);
386 type = widetag_of(header);
388 print_obj("header: ", header);
389 if (!other_immediate_lowtag_p(header)) {
391 printf("(invalid header object)");
401 printf("%08lx", (unsigned long) *--ptr);
405 print_slots(ratio_slots, count, ptr);
408 case COMPLEX_WIDETAG:
409 print_slots(complex_slots, count, ptr);
412 case SYMBOL_HEADER_WIDETAG:
413 print_slots(symbol_slots, count, ptr);
416 #if N_WORD_BITS == 32
417 case SINGLE_FLOAT_WIDETAG:
419 printf("%g", ((struct single_float *)native_pointer(obj))->value);
422 case DOUBLE_FLOAT_WIDETAG:
424 printf("%g", ((struct double_float *)native_pointer(obj))->value);
427 #ifdef LONG_FLOAT_WIDETAG
428 case LONG_FLOAT_WIDETAG:
430 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
434 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
435 case COMPLEX_SINGLE_FLOAT_WIDETAG:
437 #ifdef LISP_FEATURE_X86_64
438 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
440 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
443 #ifdef LISP_FEATURE_X86_64
444 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
446 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
451 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
452 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
454 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
456 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
460 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
461 case COMPLEX_LONG_FLOAT_WIDETAG:
463 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
465 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
469 case SIMPLE_BASE_STRING_WIDETAG:
470 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
471 case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
474 cptr = (char *)(ptr+1);
481 case SIMPLE_VECTOR_WIDETAG:
483 printf("length = %ld", length);
486 while (length-- > 0) {
487 sprintf(buffer, "%d: ", index++);
488 print_obj(buffer, *ptr++);
492 case INSTANCE_HEADER_WIDETAG:
494 printf("length = %ld", (long) count);
496 while (count-- > 0) {
497 sprintf(buffer, "%d: ", index++);
498 print_obj(buffer, *ptr++);
502 case SIMPLE_ARRAY_WIDETAG:
503 case SIMPLE_BIT_VECTOR_WIDETAG:
504 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
505 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
506 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
507 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
508 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
509 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
511 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
513 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
514 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
515 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
516 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
518 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
519 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
521 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
522 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
524 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
525 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
528 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
530 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
531 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
533 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
534 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
536 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
537 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
538 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
539 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
541 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
542 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
544 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
545 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
547 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
548 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
550 case COMPLEX_BASE_STRING_WIDETAG:
551 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
552 case COMPLEX_CHARACTER_STRING_WIDETAG:
554 case COMPLEX_VECTOR_NIL_WIDETAG:
555 case COMPLEX_BIT_VECTOR_WIDETAG:
556 case COMPLEX_VECTOR_WIDETAG:
557 case COMPLEX_ARRAY_WIDETAG:
560 case CODE_HEADER_WIDETAG:
561 print_slots(code_slots, count-1, ptr);
564 case SIMPLE_FUN_HEADER_WIDETAG:
565 print_slots(fn_slots, 5, ptr);
568 case RETURN_PC_HEADER_WIDETAG:
569 print_obj("code: ", obj - (count * 4));
572 case CLOSURE_HEADER_WIDETAG:
573 print_slots(closure_slots, count, ptr);
576 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
577 print_slots(funcallable_instance_slots, count, ptr);
580 case VALUE_CELL_HEADER_WIDETAG:
581 print_slots(value_cell_slots, 1, ptr);
586 #ifndef LISP_FEATURE_ALPHA
587 printf("0x%08lx", (unsigned long) *ptr);
589 printf("0x%016lx", *(lispobj*)(ptr+1));
593 case WEAK_POINTER_WIDETAG:
594 print_slots(weak_pointer_slots, 1, ptr);
597 case CHARACTER_WIDETAG:
598 case UNBOUND_MARKER_WIDETAG:
600 printf("pointer to an immediate?");
604 print_slots(fdefn_slots, count, ptr);
609 printf("Unknown header object?");
615 static void print_obj(char *prefix, lispobj obj)
617 #ifdef LISP_FEATURE_X86_64
618 static void (*verbose_fns[])(lispobj obj)
619 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
620 print_fixnum, print_otherimm, print_fixnum, print_list,
621 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
622 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
623 static void (*brief_fns[])(lispobj obj)
624 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
625 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
626 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
627 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
629 static void (*verbose_fns[])(lispobj obj)
630 = {print_fixnum, print_struct, print_otherimm, print_list,
631 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
632 static void (*brief_fns[])(lispobj obj)
633 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
634 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
636 int type = lowtag_of(obj);
637 struct var *var = lookup_by_obj(obj);
639 boolean verbose = cur_depth < brief_depth;
641 if (!continue_p(verbose))
644 if (var != NULL && var_clock(var) == cur_clock)
647 if (var == NULL && is_lisp_pointer(obj))
648 var = define_var(NULL, obj, 0);
651 var_setclock(var, cur_clock);
656 sprintf(buffer, "$%s=", var_name(var));
661 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
662 if (cur_depth < brief_depth) {
663 fputs(lowtag_names[type], stdout);
664 (*verbose_fns[type])(obj);
667 (*brief_fns[type])(obj);
671 printf("$%s", var_name(var));
674 printf("$%s=", var_name(var));
675 (*brief_fns[type])(obj);
689 void print(lispobj obj)
701 void brief_print(lispobj obj)
715 brief_print(lispobj obj)
717 printf("lispobj 0x%lx\n", (unsigned long)obj);
720 #endif /* defined(LISP_FEATURE_SB_LDB) */