Autogenerate <obj>_slots[] for print.c
[sbcl.git] / src / runtime / print.c
blob07195239fc30310afe1fced2488c301e5e18718e
1 /* code for low-level debugging/diagnostic output */
3 /*
4 * This software is part of the SBCL system. See the README file for
5 * more information.
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.
15 * FIXME:
16 * Some of the code in here is deeply broken, depending on guessing
17 * already out-of-date values instead of getting them from sbcl.h.
20 #include <stdio.h>
21 #include <string.h>
23 #include "sbcl.h"
24 #include "print.h"
25 #include "runtime.h"
26 #include "gc-internal.h"
27 #include <stdarg.h>
28 #include "thread.h" /* genesis/primitive-objects.h needs this */
29 #include <errno.h>
30 #include <stdlib.h>
32 /* FSHOW and odxprint provide debugging output for low-level information
33 * (signal handling, exceptions, safepoints) which is hard to debug by
34 * other means.
36 * If enabled at all, environment variables control whether calls of the
37 * form odxprint(name, ...) are enabled at run-time, e.g. using
38 * SBCL_DYNDEBUG="fshow fshow_signal safepoints".
40 * In the case of FSHOW and FSHOW_SIGNAL, old-style code from runtime.h
41 * can also be used to enable or disable these more aggressively.
44 struct dyndebug_config dyndebug_config = {
45 QSHOW == 2, QSHOW_SIGNALS == 2
48 void
49 dyndebug_init()
51 #define DYNDEBUG_NFLAGS (sizeof(struct dyndebug_config) / sizeof(int))
52 #define dyndebug_init1(lowercase, uppercase) \
53 do { \
54 int *ptr = &dyndebug_config.dyndebug_##lowercase; \
55 ptrs[n] = ptr; \
56 names[n] = #lowercase; \
57 char *val = getenv("SBCL_DYNDEBUG__" uppercase); \
58 *ptr = val && strlen(val); \
59 n++; \
60 } while (0)
61 int n = 0;
62 char *names[DYNDEBUG_NFLAGS];
63 int *ptrs[DYNDEBUG_NFLAGS];
65 dyndebug_init1(fshow, "FSHOW");
66 dyndebug_init1(fshow_signal, "FSHOW_SIGNAL");
67 dyndebug_init1(gencgc_verbose, "GENCGC_VERBOSE");
68 dyndebug_init1(safepoints, "SAFEPOINTS");
69 dyndebug_init1(seh, "SEH");
70 dyndebug_init1(misc, "MISC");
71 dyndebug_init1(pagefaults, "PAGEFAULTS");
72 dyndebug_init1(io, "IO");
73 dyndebug_init1(runtime_link, "RUNTIME_LINK");
75 int n_output_flags = n;
76 dyndebug_init1(backtrace_when_lost, "BACKTRACE_WHEN_LOST");
77 dyndebug_init1(sleep_when_lost, "SLEEP_WHEN_LOST");
79 if (n != DYNDEBUG_NFLAGS)
80 fprintf(stderr, "Bug in dyndebug_init\n");
82 #if defined(LISP_FEATURE_GENCGC)
83 gencgc_verbose = dyndebug_config.dyndebug_gencgc_verbose;
84 #endif
86 char *featurelist = getenv("SBCL_DYNDEBUG");
87 if (featurelist) {
88 int err = 0;
89 featurelist = strdup(featurelist);
90 char *ptr = featurelist;
91 for (;;) {
92 char *token = strtok(ptr, " ");
93 if (!token) break;
94 int i;
95 if (!strcmp(token, "all"))
96 for (i = 0; i < n_output_flags; i++)
97 *ptrs[i] = 1;
98 else {
99 for (i = 0; i < (int)DYNDEBUG_NFLAGS; i++)
100 if (!strcmp(token, names[i])) {
101 *ptrs[i] = 1;
102 break;
104 if (i == DYNDEBUG_NFLAGS) {
105 fprintf(stderr, "No such dyndebug flag: `%s'\n", token);
106 err = 1;
109 ptr = 0;
111 free(featurelist);
112 if (err) {
113 fprintf(stderr, "Valid flags are:\n");
114 fprintf(stderr, " all ;enables all of the following:\n");
115 int i;
116 for (i = 0; i < (int)DYNDEBUG_NFLAGS; i++) {
117 if (i == n_output_flags)
118 fprintf(stderr, "Additional options:\n");
119 fprintf(stderr, " %s\n", names[i]);
124 #undef dyndebug_init1
125 #undef DYNDEBUG_NFLAGS
128 /* Temporarily, odxprint merely performs the equivalent of a traditional
129 * FSHOW call, i.e. it merely formats to stderr. Ultimately, it should
130 * be restored to its full win32 branch functionality, where output to a
131 * file or to the debugger can be selected at runtime. */
133 void vodxprint_fun(const char *, va_list);
135 void
136 odxprint_fun(const char *fmt, ...)
138 va_list args;
139 va_start(args, fmt);
140 vodxprint_fun(fmt, args);
141 va_end(args);
144 void
145 vodxprint_fun(const char *fmt, va_list args)
147 #ifdef LISP_FEATURE_WIN32
148 DWORD lastError = GetLastError();
149 #endif
150 int original_errno = errno;
152 QSHOW_BLOCK;
154 char buf[1024];
155 int n = 0;
157 #ifdef LISP_FEATURE_SB_THREAD
158 struct thread *arch_os_get_current_thread(void);
159 struct thread *self = arch_os_get_current_thread();
160 void *pth = self ? (void *) self->os_thread : 0;
161 snprintf(buf, sizeof(buf), "[%p/%p] ", self, pth);
162 n = strlen(buf);
163 #endif
165 vsnprintf(buf + n, sizeof(buf) - n - 1, fmt, args);
166 /* buf is now zero-terminated (even in case of overflow).
167 * Our caller took care of the newline (if any) through `fmt'. */
169 /* A sufficiently POSIXy implementation of stdio will provide
170 * per-FILE locking, as defined in the spec for flockfile. At least
171 * glibc complies with this. Hence we do not need to perform
172 * locking ourselves here. (Should it turn out, of course, that
173 * other libraries opt for speed rather than safety, we need to
174 * revisit this decision.) */
175 fputs(buf, stderr);
177 #ifdef LISP_FEATURE_WIN32
178 /* stdio's stderr is line-bufferred, i.e. \n ought to flush it.
179 * Unfortunately, MinGW does not behave the way I would expect it
180 * to. Let's be safe: */
181 fflush(stderr);
182 #endif
184 QSHOW_UNBLOCK;
186 #ifdef LISP_FEATURE_WIN32
187 SetLastError(lastError);
188 #endif
189 errno = original_errno;
192 /* Translate the rather awkward syntax
193 * FSHOW((stderr, "xyz"))
194 * into the new and cleaner
195 * odxprint("xyz").
196 * If we were willing to clean up all existing call sites, we could remove
197 * this wrapper function. (This is a function, because I don't know how to
198 * strip the extra parens in a macro.) */
199 void
200 fshow_fun(void __attribute__((__unused__)) *ignored,
201 const char *fmt,
202 ...)
204 va_list args;
205 va_start(args, fmt);
206 vodxprint_fun(fmt, args);
207 va_end(args);
210 /* This file can be skipped if we're not supporting LDB. */
211 #if defined(LISP_FEATURE_SB_LDB)
213 #include "monitor.h"
214 #include "vars.h"
215 #include "os.h"
216 #ifdef LISP_FEATURE_GENCGC
217 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
218 #endif
219 #if defined(LISP_FEATURE_WIN32)
220 # include "win32-thread-private-events.h" /* genesis/thread.h needs this */
221 #endif
222 #include "genesis/static-symbols.h"
223 #include "genesis/primitive-objects.h"
224 #include "genesis/static-symbols.h"
225 #include "genesis/tagnames.h"
227 static int max_lines = 20, cur_lines = 0;
228 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
229 static int max_length = 5;
230 static boolean dont_descend = 0, skip_newline = 0;
231 static int cur_clock = 0;
233 static void print_obj(char *prefix, lispobj obj);
235 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
237 static void indent(int in)
239 static char *spaces = " ";
241 while (in > 64) {
242 fputs(spaces, stdout);
243 in -= 64;
245 if (in != 0)
246 fputs(spaces + 64 - in, stdout);
249 static boolean continue_p(boolean newline)
251 char buffer[256];
253 if (cur_depth >= max_depth || dont_descend)
254 return 0;
256 if (newline) {
257 if (skip_newline)
258 skip_newline = 0;
259 else
260 putchar('\n');
262 if (cur_lines >= max_lines) {
263 printf("More? [y] ");
264 fflush(stdout);
266 if (fgets(buffer, sizeof(buffer), stdin)) {
267 if (buffer[0] == 'n' || buffer[0] == 'N')
268 throw_to_monitor();
269 else
270 cur_lines = 0;
271 } else {
272 printf("\nUnable to read response, assuming y.\n");
273 cur_lines = 0;
278 return 1;
281 static void newline(char *label)
283 cur_lines++;
284 if (label != NULL)
285 fputs(label, stdout);
286 putchar('\t');
287 indent(cur_depth * 2);
291 static void print_unknown(lispobj obj)
293 printf("unknown object: %p", (void *)obj);
296 static void brief_fixnum(lispobj obj)
298 /* KLUDGE: Rather than update the tables in print_obj(), we
299 declare all fixnum-or-unknown tags to be fixnums and sort it
300 out here with a guard clause. */
301 if (!fixnump(obj)) return print_unknown(obj);
303 #ifndef LISP_FEATURE_ALPHA
304 printf("%ld", ((long)obj)>>N_FIXNUM_TAG_BITS);
305 #else
306 printf("%d", ((s32)obj)>>N_FIXNUM_TAG_BITS);
307 #endif
310 static void print_fixnum(lispobj obj)
312 /* KLUDGE: Rather than update the tables in print_obj(), we
313 declare all fixnum-or-unknown tags to be fixnums and sort it
314 out here with a guard clause. */
315 if (!fixnump(obj)) return print_unknown(obj);
317 #ifndef LISP_FEATURE_ALPHA
318 printf(": %ld", ((long)obj)>>N_FIXNUM_TAG_BITS);
319 #else
320 printf(": %d", ((s32)obj)>>N_FIXNUM_TAG_BITS);
321 #endif
324 static void brief_otherimm(lispobj obj)
326 int type, c;
327 char * charname = 0;
329 type = widetag_of(obj);
330 switch (type) {
331 case CHARACTER_WIDETAG:
332 c = obj>>8; // no mask. show whatever's there
333 printf("#\\");
334 switch (c) {
335 case '\0': charname = "Nul"; break;
336 case '\n': charname = "Newline"; break;
337 case '\b': charname = "Backspace"; break;
338 case '\177': charname = "Delete"; break;
339 default:
340 if (c < 32) printf("^%c", c+64);
341 else printf(c < 128 ? "%c" : "U+%X", c);
343 if (charname)
344 fputs(charname, stdout);
345 break;
347 case UNBOUND_MARKER_WIDETAG:
348 printf("<unbound marker>");
349 break;
351 default:
352 printf("%s", widetag_names[type >> 2]);
353 break;
357 static void print_otherimm(lispobj obj)
359 printf(", %s", widetag_names[widetag_of(obj) >> 2]);
361 switch (widetag_of(obj)) {
362 case CHARACTER_WIDETAG:
363 printf(": ");
364 brief_otherimm(obj);
365 break;
367 case SAP_WIDETAG:
368 case UNBOUND_MARKER_WIDETAG:
369 break;
371 default:
372 printf(": data=%"OBJ_FMTX, (obj>>8));
373 break;
377 static void brief_list(lispobj obj)
379 int space = 0;
380 int length = 0;
382 if (obj == NIL)
383 printf("NIL");
384 else {
385 putchar('(');
386 while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
387 if (space)
388 putchar(' ');
389 if (++length >= max_length) {
390 printf("...");
391 obj = NIL;
392 break;
394 print_obj("", CONS(obj)->car);
395 obj = CONS(obj)->cdr;
396 space = 1;
397 if (obj == NIL)
398 break;
400 if (obj != NIL) {
401 printf(" . ");
402 print_obj("", obj);
404 putchar(')');
408 static void print_list(lispobj obj)
410 if (obj == NIL) {
411 printf(" (NIL)");
412 } else {
413 print_obj("car: ", CONS(obj)->car);
414 print_obj("cdr: ", CONS(obj)->cdr);
418 // takes native pointer as input
419 char * simple_base_stringize(struct vector * string)
421 if (widetag_of(string->header) == SIMPLE_BASE_STRING_WIDETAG)
422 return (char*)string->data;
423 int length = string->length;
424 char * newstring = malloc(length+1);
425 uint32_t * data = (uint32_t*)string->data;
426 int i;
427 for(i=0;i<length;++i)
428 newstring[i] = data[i] < 128 ? data[i] : '?';
429 newstring[length] = 0;
430 return newstring;
433 static void brief_struct(lispobj obj)
435 struct instance *instance = (struct instance *)native_pointer(obj);
436 extern struct vector * instance_classoid_name(lispobj*);
437 struct vector * classoid_name;
438 classoid_name = instance_classoid_name((lispobj*)instance);
439 if ( classoid_name ) {
440 char * namestring = simple_base_stringize(classoid_name);
441 printf("#<ptr to %p %s instance>",
442 (void*)instance_layout((lispobj*)instance), namestring);
443 if ( namestring != (char*)classoid_name->data )
444 free(namestring);
445 } else {
446 printf("#<ptr to %p instance>",
447 (void*)instance_layout((lispobj*)instance));
451 #include "genesis/layout.h"
452 static boolean tagged_slot_p(struct layout * layout,
453 int slot_index)
455 lispobj bitmap = layout->bitmap;
456 sword_t fixnum = (sword_t)bitmap >> N_FIXNUM_TAG_BITS; // optimistically
457 return fixnump(bitmap)
458 ? bitmap == make_fixnum(-1) ||
459 (slot_index < N_WORD_BITS && ((fixnum >> slot_index) & 1) != 0)
460 : positive_bignum_logbitp(slot_index,
461 (struct bignum*)native_pointer(bitmap));
464 static void print_struct(lispobj obj)
466 struct instance *instance = (struct instance *)native_pointer(obj);
467 unsigned int i;
468 char buffer[16];
469 lispobj layout_obj = instance_layout(native_pointer(obj));
470 print_obj("type: ", layout_obj);
471 struct layout * layout = (struct layout*)native_pointer(layout_obj);
472 for (i=INSTANCE_DATA_START; i<instance_length(instance->header); i++) {
473 sprintf(buffer, "slot %d: ", i);
474 if (layout != NULL && tagged_slot_p(layout, i)) {
475 print_obj(buffer, instance->slots[i]);
476 } else {
477 newline(NULL);
478 printf("\n\t %s0x%"OBJ_FMTX" [raw]", buffer, instance->slots[i]);
483 void show_lstring(struct vector * string, int quotes, FILE *s)
485 int ucs4_p = 0;
486 int i, len = fixnum_value(string->length);
488 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
489 if (widetag_of(string->header) == SIMPLE_CHARACTER_STRING_WIDETAG) {
490 ucs4_p = 1;
491 if (quotes)
492 putc('u', s); /* an arbitrary notational convention */
494 #endif
495 if (quotes) putc('"', s);
496 for (i=0 ; i<len ; i++) {
497 // hopefully the compiler will optimize out the ucs4_p test
498 // when the runtime is built without Unicode support
499 int ch;
500 if (ucs4_p)
501 ch = i[(uint32_t*)string->data];
502 else
503 ch = i[(char*)string->data];
504 if (ch >= 32 && ch < 127) {
505 if (quotes && (ch == '"' || ch == '\\'))
506 putc('\\', s);
507 putc(ch, s);
508 } else {
509 fprintf(s, ch > 0xffff ? "\\U%08X" :
510 ch > 0xff ? "\\u%04X" : "\\x%02X", ch);
513 if (quotes) putc('"', s);
516 static void brief_otherptr(lispobj obj)
518 extern void safely_show_lstring(struct vector*, int, FILE*);
519 lispobj *ptr, header;
520 int type;
521 struct symbol *symbol;
523 ptr = native_pointer(obj);
524 header = *ptr;
525 type = widetag_of(header);
526 switch (type) {
527 case SYMBOL_WIDETAG:
528 symbol = (struct symbol *)ptr;
529 if (symbol->package == NIL)
530 printf("#:");
531 show_lstring(VECTOR(symbol->name), 0, stdout);
532 break;
534 case SIMPLE_BASE_STRING_WIDETAG:
535 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
536 case SIMPLE_CHARACTER_STRING_WIDETAG:
537 #endif
538 show_lstring((struct vector*)ptr, 1, stdout);
539 break;
541 default:
542 printf("#<ptr to ");
543 brief_otherimm(header);
544 if (type == FDEFN_WIDETAG) { // Try to print name, if a symbol
545 // FIXME: more address validity checks perhaps?
546 lispobj name = ((struct fdefn*)ptr)->name;
547 if (lowtag_of(name) == OTHER_POINTER_LOWTAG
548 && widetag_of(*native_pointer(name)) == SYMBOL_WIDETAG) {
549 printf(" for ");
550 struct vector* str = symbol_name(native_pointer(name));
551 safely_show_lstring(str, 0, stdout);
554 putchar('>');
558 static void print_slots(char **slots, int count, lispobj *ptr)
560 while (count-- > 0) {
561 if (*slots) {
562 print_obj(*slots++, *ptr++);
563 } else {
564 print_obj("???: ", *ptr++);
569 static lispobj symbol_function(lispobj* symbol)
571 lispobj info = ((struct symbol*)symbol)->info;
572 if (lowtag_of(info) == LIST_POINTER_LOWTAG)
573 info = CONS(info)->cdr;
574 if (lowtag_of(info) == OTHER_POINTER_LOWTAG) {
575 struct vector* v = VECTOR(info);
576 int len = fixnum_value(v->length);
577 if (len != 0) {
578 lispobj elt = v->data[0]; // Just like INFO-VECTOR-FDEFN
579 if (fixnump(elt) && (fixnum_value(elt) & 07777) >= 07701) {
580 lispobj fdefn = v->data[len-1];
581 if (lowtag_of(fdefn) == OTHER_POINTER_LOWTAG)
582 return FDEFN(fdefn)->fun;
586 return NIL;
589 static void print_otherptr(lispobj obj)
591 #ifndef LISP_FEATURE_ALPHA
592 lispobj *ptr;
593 unsigned long header;
594 unsigned long length;
595 #else
596 u32 *ptr;
597 u32 header;
598 u32 length;
599 #endif
600 int count, type, index;
601 char buffer[16];
603 ptr = native_pointer(obj);
604 if (ptr == NULL) {
605 printf(" (NULL Pointer)");
606 return;
609 header = *ptr++;
610 length = fixnum_value(*ptr);
611 count = HeaderValue(header);
612 type = widetag_of(header);
614 print_obj("header: ", header);
615 if (!other_immediate_lowtag_p(header)) {
616 NEWLINE_OR_RETURN;
617 printf("(invalid header object)");
618 return;
621 if (unprintable_array_types[type/8] & (1<<(type % 8)))
622 return;
623 switch (type) {
624 case BIGNUM_WIDETAG:
625 ptr += count;
626 NEWLINE_OR_RETURN;
627 printf("0x");
628 while (count-- > 0)
629 printf(
630 #if N_WORD_BITS == 32
631 "%08lx%s",
632 #else
633 "%016lx%s",
634 #endif
635 (unsigned long) *--ptr, (count?"_":""));
636 break;
638 case RATIO_WIDETAG:
639 print_slots(ratio_slots, count, ptr);
640 break;
642 case COMPLEX_WIDETAG:
643 print_slots(complex_slots, count, ptr);
644 break;
646 case SYMBOL_WIDETAG:
647 // Only 1 byte of a symbol header conveys its size.
648 // The other bytes may be freely used by the backend.
649 print_slots(symbol_slots, count & 0xFF, ptr);
650 if (symbol_function(ptr-1) != NIL)
651 print_obj("fun: ", symbol_function(ptr-1));
652 break;
654 #if N_WORD_BITS == 32
655 case SINGLE_FLOAT_WIDETAG:
656 NEWLINE_OR_RETURN;
657 printf("%g", ((struct single_float *)native_pointer(obj))->value);
658 break;
659 #endif
660 case DOUBLE_FLOAT_WIDETAG:
661 NEWLINE_OR_RETURN;
662 printf("%g", ((struct double_float *)native_pointer(obj))->value);
663 break;
665 #ifdef LONG_FLOAT_WIDETAG
666 case LONG_FLOAT_WIDETAG:
667 NEWLINE_OR_RETURN;
668 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
669 break;
670 #endif
672 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
673 case COMPLEX_SINGLE_FLOAT_WIDETAG:
674 NEWLINE_OR_RETURN;
675 #ifdef LISP_FEATURE_64_BIT
676 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
677 #else
678 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
679 #endif
680 NEWLINE_OR_RETURN;
681 #ifdef LISP_FEATURE_64_BIT
682 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
683 #else
684 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
685 #endif
686 break;
687 #endif
689 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
690 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
691 NEWLINE_OR_RETURN;
692 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
693 NEWLINE_OR_RETURN;
694 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
695 break;
696 #endif
698 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
699 case COMPLEX_LONG_FLOAT_WIDETAG:
700 NEWLINE_OR_RETURN;
701 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
702 NEWLINE_OR_RETURN;
703 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
704 break;
705 #endif
707 case SIMPLE_BASE_STRING_WIDETAG:
708 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
709 case SIMPLE_CHARACTER_STRING_WIDETAG:
710 #endif
711 NEWLINE_OR_RETURN;
712 show_lstring((struct vector*)native_pointer(obj), 1, stdout);
713 break;
715 case SIMPLE_VECTOR_WIDETAG:
716 NEWLINE_OR_RETURN;
717 printf("length = %ld", length);
718 ptr++;
719 index = 0;
720 while (length-- > 0) {
721 sprintf(buffer, "%d: ", index++);
722 print_obj(buffer, *ptr++);
724 break;
726 // FIXME: This case looks unreachable. print_struct() does it
727 case INSTANCE_WIDETAG:
728 NEWLINE_OR_RETURN;
729 count &= SHORT_HEADER_MAX_WORDS;
730 printf("length = %ld", (long) count);
731 index = 0;
732 while (count-- > 0) {
733 sprintf(buffer, "%d: ", index++);
734 print_obj(buffer, *ptr++);
736 break;
738 case CODE_HEADER_WIDETAG:
739 count &= SHORT_HEADER_MAX_WORDS;
740 // ptr was already bumped up
741 for_each_simple_fun(fun_index, fun, (struct code*)(ptr-1), 0, {
742 sprintf(buffer, "f[%d]: ", fun_index);
743 print_obj(buffer, make_lispobj(fun,FUN_POINTER_LOWTAG));
745 print_slots(code_slots, count-1, ptr);
746 break;
748 case SIMPLE_FUN_WIDETAG:
749 print_obj("code: ",
750 make_lispobj(native_pointer((lispobj)(ptr-1))
751 -(HeaderValue(header)&0xFFFF),
752 OTHER_POINTER_LOWTAG));
753 print_slots(simple_fun_slots,
754 sizeof simple_fun_slots/sizeof(char*)-1, ptr);
755 break;
757 #ifdef RETURN_PC_WIDETAG
758 case RETURN_PC_WIDETAG:
759 print_obj("code: ", obj - (count * 4));
760 break;
761 #endif
763 case CLOSURE_WIDETAG:
764 print_slots(closure_slots,
765 count & SHORT_HEADER_MAX_WORDS, ptr);
766 break;
768 case FUNCALLABLE_INSTANCE_WIDETAG:
769 print_slots(funcallable_instance_slots,
770 count & SHORT_HEADER_MAX_WORDS, ptr);
771 break;
773 case VALUE_CELL_WIDETAG:
774 print_slots(value_cell_slots, 1, ptr);
775 break;
777 case SAP_WIDETAG:
778 NEWLINE_OR_RETURN;
779 #ifndef LISP_FEATURE_ALPHA
780 printf("0x%08lx", (unsigned long) *ptr);
781 #else
782 printf("0x%016lx", *(lispobj*)(ptr+1));
783 #endif
784 break;
786 case WEAK_POINTER_WIDETAG:
787 print_slots(weak_pointer_slots, 1, ptr);
788 break;
790 case CHARACTER_WIDETAG:
791 case UNBOUND_MARKER_WIDETAG:
792 NEWLINE_OR_RETURN;
793 printf("pointer to an immediate?");
794 break;
796 case FDEFN_WIDETAG:
797 #ifdef LISP_FEATURE_IMMOBILE_CODE
798 print_slots(fdefn_slots, 2, ptr);
799 print_obj("entry: ", fdefn_raw_referent((struct fdefn*)(ptr-1)));
800 #else
801 print_slots(fdefn_slots, count & SHORT_HEADER_MAX_WORDS, ptr);
802 #endif
803 break;
805 default:
806 NEWLINE_OR_RETURN;
807 printf("Unknown header object?");
808 break;
812 static void print_obj(char *prefix, lispobj obj)
814 #ifdef LISP_FEATURE_64_BIT
815 static void (*verbose_fns[])(lispobj obj)
816 = {print_fixnum, print_otherimm, print_fixnum, print_struct,
817 print_fixnum, print_otherimm, print_fixnum, print_list,
818 print_fixnum, print_otherimm, print_fixnum, print_otherptr,
819 print_fixnum, print_otherimm, print_fixnum, print_otherptr};
820 static void (*brief_fns[])(lispobj obj)
821 = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
822 brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
823 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
824 brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
825 #else
826 static void (*verbose_fns[])(lispobj obj)
827 = {print_fixnum, print_struct, print_otherimm, print_list,
828 print_fixnum, print_otherptr, print_otherimm, print_otherptr};
829 static void (*brief_fns[])(lispobj obj)
830 = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
831 brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
832 #endif
833 int type = lowtag_of(obj);
834 struct var *var = lookup_by_obj(obj);
835 char buffer[256];
836 boolean verbose = cur_depth < brief_depth;
838 if (!continue_p(verbose))
839 return;
841 if (var != NULL && var_clock(var) == cur_clock)
842 dont_descend = 1;
844 if (var == NULL && is_lisp_pointer(obj))
845 var = define_var(NULL, obj, 0);
847 if (var != NULL)
848 var_setclock(var, cur_clock);
850 void (**fns)(lispobj) = NULL;
851 cur_depth++;
852 if (verbose) {
853 if (var != NULL) {
854 sprintf(buffer, "$%s=", var_name(var));
855 newline(buffer);
857 else
858 newline(NULL);
859 printf("%s0x%08lx: ", prefix, (unsigned long) obj);
860 if (cur_depth < brief_depth) {
861 fputs(lowtag_names[type], stdout);
862 fns = verbose_fns;
864 else
865 fns = brief_fns;
867 else {
868 if (dont_descend)
869 printf("$%s", var_name(var));
870 else {
871 if (var != NULL)
872 printf("$%s=", var_name(var));
873 fns = brief_fns;
876 if (!fns)
878 else if (is_lisp_pointer(obj)
879 && !is_valid_lisp_addr((os_vm_address_t)obj))
880 printf("(bad-address)");
881 else
882 (*fns[type])(obj);
883 cur_depth--;
884 dont_descend = 0;
887 void reset_printer()
889 cur_clock++;
890 cur_lines = 0;
891 dont_descend = 0;
894 void print(lispobj obj)
896 skip_newline = 1;
897 cur_depth = 0;
898 max_depth = 5;
899 max_lines = 20;
901 print_obj("", obj);
903 putchar('\n');
906 void brief_print(lispobj obj)
908 skip_newline = 1;
909 cur_depth = 0;
910 max_depth = 1;
911 max_lines = 5000;
912 cur_lines = 0;
914 print_obj("", obj);
915 putchar('\n');
918 // The following accessors, which take a valid native pointer as input
919 // and return a Lisp string, are designed to be foolproof during GC,
920 // hence all the forwarding checks.
922 #include "forwarding-ptr.h"
923 #include "genesis/classoid.h"
924 struct vector * symbol_name(lispobj * sym)
926 if (forwarding_pointer_p(sym))
927 sym = native_pointer(forwarding_pointer_value(sym));
928 if (lowtag_of(((struct symbol*)sym)->name) != OTHER_POINTER_LOWTAG)
929 return NULL;
930 lispobj * name = native_pointer(((struct symbol*)sym)->name);
931 if (forwarding_pointer_p(name))
932 name = native_pointer(forwarding_pointer_value(name));
933 return (struct vector*)name;
935 struct vector * classoid_name(lispobj * classoid)
937 if (forwarding_pointer_p(classoid))
938 classoid = native_pointer(forwarding_pointer_value(classoid));
939 lispobj sym = ((struct classoid*)classoid)->name;
940 return lowtag_of(sym) != OTHER_POINTER_LOWTAG ? NULL
941 : symbol_name(native_pointer(sym));
943 struct vector * layout_classoid_name(lispobj * layout)
945 if (forwarding_pointer_p(layout))
946 layout = native_pointer(forwarding_pointer_value(layout));
947 lispobj classoid = ((struct layout*)layout)->classoid;
948 return lowtag_of(classoid) != INSTANCE_POINTER_LOWTAG ? NULL
949 : classoid_name(native_pointer(classoid));
951 struct vector * instance_classoid_name(lispobj * instance)
953 if (forwarding_pointer_p(instance))
954 instance = native_pointer(forwarding_pointer_value(instance));
955 lispobj layout = instance_layout(instance);
956 return lowtag_of(layout) != INSTANCE_POINTER_LOWTAG ? NULL
957 : layout_classoid_name(native_pointer(layout));
959 void safely_show_lstring(struct vector * string, int quotes, FILE *s)
961 extern void show_lstring(struct vector*, int, FILE*);
962 if (forwarding_pointer_p((lispobj*)string))
963 string = (struct vector*)forwarding_pointer_value((lispobj*)string);
964 if (
965 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
966 widetag_of(string->header) == SIMPLE_CHARACTER_STRING_WIDETAG ||
967 #endif
968 widetag_of(string->header) == SIMPLE_BASE_STRING_WIDETAG)
969 show_lstring(string, quotes, s);
970 else {
971 fprintf(s, "#<[widetag=%02X]>", widetag_of(string->header));
975 #else
977 void
978 brief_print(lispobj obj)
980 printf("lispobj 0x%lx\n", (unsigned long)obj);
983 #endif /* defined(LISP_FEATURE_SB_LDB) */