cleanup: types in gc_alloc_large
[sbcl.git] / src / runtime / print.c
1 /* code for low-level debugging/diagnostic output */
2
3 /*
4  * This software is part of the SBCL system. See the README file for
5  * more information.
6  *
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.
12  */
13
14 /*
15  * FIXME:
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.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "sbcl.h"
25 #include "print.h"
26 #include "runtime.h"
27
28 /* This file can be skipped if we're not supporting LDB. */
29 #if defined(LISP_FEATURE_SB_LDB)
30
31 #include "monitor.h"
32 #include "vars.h"
33 #include "os.h"
34 #include "gencgc-alloc-region.h" /* genesis/thread.h needs this */
35 #include "genesis/static-symbols.h"
36 #include "genesis/primitive-objects.h"
37 #include "genesis/static-symbols.h"
38 #include "genesis/tagnames.h"
39
40 static int max_lines = 20, cur_lines = 0;
41 static int max_depth = 5, brief_depth = 2, cur_depth = 0;
42 static int max_length = 5;
43 static boolean dont_descend = 0, skip_newline = 0;
44 static int cur_clock = 0;
45
46 static void print_obj(char *prefix, lispobj obj);
47
48 #define NEWLINE_OR_RETURN if (continue_p(1)) newline(NULL); else return;
49
50 static void indent(int in)
51 {
52     static char *spaces = "                                                                ";
53
54     while (in > 64) {
55         fputs(spaces, stdout);
56         in -= 64;
57     }
58     if (in != 0)
59         fputs(spaces + 64 - in, stdout);
60 }
61
62 static boolean continue_p(boolean newline)
63 {
64     char buffer[256];
65
66     if (cur_depth >= max_depth || dont_descend)
67         return 0;
68
69     if (newline) {
70         if (skip_newline)
71             skip_newline = 0;
72         else
73             putchar('\n');
74
75         if (cur_lines >= max_lines) {
76             printf("More? [y] ");
77             fflush(stdout);
78
79             if (fgets(buffer, sizeof(buffer), stdin)) {
80                 if (buffer[0] == 'n' || buffer[0] == 'N')
81                     throw_to_monitor();
82                 else
83                     cur_lines = 0;
84             } else {
85                 printf("\nUnable to read response, assuming y.\n");
86                 cur_lines = 0;
87             }
88         }
89     }
90
91     return 1;
92 }
93
94 static void newline(char *label)
95 {
96     cur_lines++;
97     if (label != NULL)
98         fputs(label, stdout);
99     putchar('\t');
100     indent(cur_depth * 2);
101 }
102
103
104 static void print_unknown(lispobj obj)
105 {
106   printf("unknown object: %p", (void *)obj);
107 }
108
109 static void brief_fixnum(lispobj obj)
110 {
111     /* KLUDGE: Rather than update the tables in print_obj(), we
112        declare all fixnum-or-unknown tags to be fixnums and sort it
113        out here with a guard clause. */
114     if (!fixnump(obj)) return print_unknown(obj);
115
116 #ifndef LISP_FEATURE_ALPHA
117     printf("%ld", ((long)obj)>>2);
118 #else
119     printf("%d", ((s32)obj)>>2);
120 #endif
121 }
122
123 static void print_fixnum(lispobj obj)
124 {
125     /* KLUDGE: Rather than update the tables in print_obj(), we
126        declare all fixnum-or-unknown tags to be fixnums and sort it
127        out here with a guard clause. */
128     if (!fixnump(obj)) return print_unknown(obj);
129
130 #ifndef LISP_FEATURE_ALPHA
131     printf(": %ld", ((long)obj)>>2);
132 #else
133     printf(": %d", ((s32)obj)>>2);
134 #endif
135 }
136
137 static void brief_otherimm(lispobj obj)
138 {
139     int type, c;
140     char buffer[10];
141
142     type = widetag_of(obj);
143     switch (type) {
144         case CHARACTER_WIDETAG:
145             c = (obj>>8)&0xff;
146             switch (c) {
147                 case '\0':
148                     printf("#\\Null");
149                     break;
150                 case '\n':
151                     printf("#\\Newline");
152                     break;
153                 case '\b':
154                     printf("#\\Backspace");
155                     break;
156                 case '\177':
157                     printf("#\\Delete");
158                     break;
159                 default:
160                     strcpy(buffer, "#\\");
161                     if (c >= 128) {
162                         strcat(buffer, "m-");
163                         c -= 128;
164                     }
165                     if (c < 32) {
166                         strcat(buffer, "c-");
167                         c += '@';
168                     }
169                     printf("%s%c", buffer, c);
170                     break;
171             }
172             break;
173
174         case UNBOUND_MARKER_WIDETAG:
175             printf("<unbound marker>");
176             break;
177
178         default:
179             printf("%s", widetag_names[type >> 2]);
180             break;
181     }
182 }
183
184 static void print_otherimm(lispobj obj)
185 {
186     printf(", %s", widetag_names[widetag_of(obj) >> 2]);
187
188     switch (widetag_of(obj)) {
189         case CHARACTER_WIDETAG:
190             printf(": ");
191             brief_otherimm(obj);
192             break;
193
194         case SAP_WIDETAG:
195         case UNBOUND_MARKER_WIDETAG:
196             break;
197
198         default:
199             printf(": data=%ld", (long) (obj>>8)&0xffffff);
200             break;
201     }
202 }
203
204 static void brief_list(lispobj obj)
205 {
206     int space = 0;
207     int length = 0;
208
209     if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj)))
210         printf("(invalid Lisp-level address)");
211     else if (obj == NIL)
212         printf("NIL");
213     else {
214         putchar('(');
215         while (lowtag_of(obj) == LIST_POINTER_LOWTAG) {
216             struct cons *cons = (struct cons *)native_pointer(obj);
217
218             if (space)
219                 putchar(' ');
220             if (++length >= max_length) {
221                 printf("...");
222                 obj = NIL;
223                 break;
224             }
225             print_obj("", cons->car);
226             obj = cons->cdr;
227             space = 1;
228             if (obj == NIL)
229                 break;
230         }
231         if (obj != NIL) {
232             printf(" . ");
233             print_obj("", obj);
234         }
235         putchar(')');
236     }
237 }
238
239 static void print_list(lispobj obj)
240 {
241     if (!is_valid_lisp_addr((os_vm_address_t)native_pointer(obj))) {
242         printf("(invalid address)");
243     } else if (obj == NIL) {
244         printf(" (NIL)");
245     } else {
246         struct cons *cons = (struct cons *)native_pointer(obj);
247
248         print_obj("car: ", cons->car);
249         print_obj("cdr: ", cons->cdr);
250     }
251 }
252
253 static void brief_struct(lispobj obj)
254 {
255     struct instance *instance = (struct instance *)native_pointer(obj);
256     if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
257         printf("(invalid address)");
258     } else {
259         printf("#<ptr to 0x%08lx instance>",
260                (unsigned long) instance->slots[0]);
261     }
262 }
263
264 static void print_struct(lispobj obj)
265 {
266     struct instance *instance = (struct instance *)native_pointer(obj);
267     unsigned int i;
268     char buffer[16];
269     if (!is_valid_lisp_addr((os_vm_address_t)instance)) {
270         printf("(invalid address)");
271     } else {
272         print_obj("type: ", ((struct instance *)native_pointer(obj))->slots[0]);
273         for (i = 1; i < HeaderValue(instance->header); i++) {
274             sprintf(buffer, "slot %d: ", i);
275             print_obj(buffer, instance->slots[i]);
276         }
277     }
278 }
279
280 static void brief_otherptr(lispobj obj)
281 {
282     lispobj *ptr, header;
283     int type;
284     struct symbol *symbol;
285     struct vector *vector;
286     char *charptr;
287
288     ptr = (lispobj *) native_pointer(obj);
289
290     if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
291             printf("(invalid address)");
292             return;
293     }
294
295     header = *ptr;
296     type = widetag_of(header);
297     switch (type) {
298         case SYMBOL_HEADER_WIDETAG:
299             symbol = (struct symbol *)ptr;
300             vector = (struct vector *)native_pointer(symbol->name);
301             for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
302                 if (*charptr == '"')
303                     putchar('\\');
304                 putchar(*charptr);
305             }
306             break;
307
308         case SIMPLE_BASE_STRING_WIDETAG:
309             vector = (struct vector *)ptr;
310             putchar('"');
311             for (charptr = (char *)vector->data; *charptr != '\0'; charptr++) {
312                 if (*charptr == '"')
313                     putchar('\\');
314                 putchar(*charptr);
315             }
316             putchar('"');
317             break;
318
319         default:
320             printf("#<ptr to ");
321             brief_otherimm(header);
322             putchar('>');
323     }
324 }
325
326 static void print_slots(char **slots, int count, lispobj *ptr)
327 {
328     while (count-- > 0) {
329         if (*slots) {
330             print_obj(*slots++, *ptr++);
331         } else {
332             print_obj("???: ", *ptr++);
333         }
334     }
335 }
336
337 /* FIXME: Yikes! This needs to depend on the values in sbcl.h (or
338  * perhaps be generated automatically by GENESIS as part of
339  * sbcl.h). */
340 static char *symbol_slots[] = {"value: ", "hash: ",
341     "plist: ", "name: ", "package: ",
342 #ifdef LISP_FEATURE_SB_THREAD
343     "tls-index: " ,
344 #endif
345     NULL};
346 static char *ratio_slots[] = {"numer: ", "denom: ", NULL};
347 static char *complex_slots[] = {"real: ", "imag: ", NULL};
348 static char *code_slots[] = {"words: ", "entry: ", "debug: ", NULL};
349 static char *fn_slots[] = {
350     "self: ", "next: ", "name: ", "arglist: ", "type: ", NULL};
351 static char *closure_slots[] = {"fn: ", NULL};
352 static char *funcallable_instance_slots[] = {"fn: ", "lexenv: ", "layout: ", NULL};
353 static char *weak_pointer_slots[] = {"value: ", NULL};
354 static char *fdefn_slots[] = {"name: ", "function: ", "raw_addr: ", NULL};
355 static char *value_cell_slots[] = {"value: ", NULL};
356
357 static void print_otherptr(lispobj obj)
358 {
359     if (!is_valid_lisp_addr((os_vm_address_t)obj)) {
360         printf("(invalid address)");
361     } else {
362 #ifndef LISP_FEATURE_ALPHA
363         lispobj *ptr;
364         unsigned long header;
365         unsigned long length;
366 #else
367         u32 *ptr;
368         u32 header;
369         u32 length;
370 #endif
371         int count, type, index;
372         char *cptr, buffer[16];
373
374         ptr = (lispobj*) native_pointer(obj);
375         if (ptr == NULL) {
376                 printf(" (NULL Pointer)");
377                 return;
378         }
379
380         header = *ptr++;
381         length = fixnum_value(*ptr);
382         count = HeaderValue(header);
383         type = widetag_of(header);
384
385         print_obj("header: ", header);
386         if (!other_immediate_lowtag_p(header)) {
387             NEWLINE_OR_RETURN;
388             printf("(invalid header object)");
389             return;
390         }
391
392         switch (type) {
393             case BIGNUM_WIDETAG:
394                 ptr += count;
395                 NEWLINE_OR_RETURN;
396                 printf("0x");
397                 while (count-- > 0)
398                     printf("%08lx", (unsigned long) *--ptr);
399                 break;
400
401             case RATIO_WIDETAG:
402                 print_slots(ratio_slots, count, ptr);
403                 break;
404
405             case COMPLEX_WIDETAG:
406                 print_slots(complex_slots, count, ptr);
407                 break;
408
409             case SYMBOL_HEADER_WIDETAG:
410                 print_slots(symbol_slots, count, ptr);
411                 break;
412
413 #if N_WORD_BITS == 32
414             case SINGLE_FLOAT_WIDETAG:
415                 NEWLINE_OR_RETURN;
416                 printf("%g", ((struct single_float *)native_pointer(obj))->value);
417                 break;
418 #endif
419             case DOUBLE_FLOAT_WIDETAG:
420                 NEWLINE_OR_RETURN;
421                 printf("%g", ((struct double_float *)native_pointer(obj))->value);
422                 break;
423
424 #ifdef LONG_FLOAT_WIDETAG
425             case LONG_FLOAT_WIDETAG:
426                 NEWLINE_OR_RETURN;
427                 printf("%Lg", ((struct long_float *)native_pointer(obj))->value);
428                 break;
429 #endif
430
431 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
432             case COMPLEX_SINGLE_FLOAT_WIDETAG:
433                 NEWLINE_OR_RETURN;
434 #ifdef LISP_FEATURE_X86_64
435                 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[0]);
436 #else
437                 printf("%g", ((struct complex_single_float *)native_pointer(obj))->real);
438 #endif
439                 NEWLINE_OR_RETURN;
440 #ifdef LISP_FEATURE_X86_64
441                 printf("%g", ((struct complex_single_float *)native_pointer(obj))->data.data[1]);
442 #else
443                 printf("%g", ((struct complex_single_float *)native_pointer(obj))->imag);
444 #endif
445                 break;
446 #endif
447
448 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
449             case COMPLEX_DOUBLE_FLOAT_WIDETAG:
450                 NEWLINE_OR_RETURN;
451                 printf("%g", ((struct complex_double_float *)native_pointer(obj))->real);
452                 NEWLINE_OR_RETURN;
453                 printf("%g", ((struct complex_double_float *)native_pointer(obj))->imag);
454                 break;
455 #endif
456
457 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
458             case COMPLEX_LONG_FLOAT_WIDETAG:
459                 NEWLINE_OR_RETURN;
460                 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->real);
461                 NEWLINE_OR_RETURN;
462                 printf("%Lg", ((struct complex_long_float *)native_pointer(obj))->imag);
463                 break;
464 #endif
465
466             case SIMPLE_BASE_STRING_WIDETAG:
467 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
468         case SIMPLE_CHARACTER_STRING_WIDETAG: /* FIXME */
469 #endif
470                 NEWLINE_OR_RETURN;
471                 cptr = (char *)(ptr+1);
472                 putchar('"');
473                 while (length-- > 0)
474                     putchar(*cptr++);
475                 putchar('"');
476                 break;
477
478             case SIMPLE_VECTOR_WIDETAG:
479                 NEWLINE_OR_RETURN;
480                 printf("length = %ld", length);
481                 ptr++;
482                 index = 0;
483                 while (length-- > 0) {
484                     sprintf(buffer, "%d: ", index++);
485                     print_obj(buffer, *ptr++);
486                 }
487                 break;
488
489             case INSTANCE_HEADER_WIDETAG:
490                 NEWLINE_OR_RETURN;
491                 printf("length = %ld", (long) count);
492                 index = 0;
493                 while (count-- > 0) {
494                     sprintf(buffer, "%d: ", index++);
495                     print_obj(buffer, *ptr++);
496                 }
497                 break;
498
499             case SIMPLE_ARRAY_WIDETAG:
500             case SIMPLE_BIT_VECTOR_WIDETAG:
501             case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
502             case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
503             case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
504             case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
505             case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
506             case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
507
508             case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
509
510             case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
511             case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
512 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
513             case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
514 #endif
515 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
516             case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
517 #endif
518 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
519             case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
520 #endif
521 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
522             case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
523 #endif
524
525             case SIMPLE_ARRAY_FIXNUM_WIDETAG:
526
527 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
528             case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
529 #endif
530 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
531             case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
532 #endif
533             case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
534             case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
535 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
536             case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
537 #endif
538 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
539             case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
540 #endif
541 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
542             case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
543 #endif
544 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
545             case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
546 #endif
547             case COMPLEX_BASE_STRING_WIDETAG:
548 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
549         case COMPLEX_CHARACTER_STRING_WIDETAG:
550 #endif
551             case COMPLEX_VECTOR_NIL_WIDETAG:
552             case COMPLEX_BIT_VECTOR_WIDETAG:
553             case COMPLEX_VECTOR_WIDETAG:
554             case COMPLEX_ARRAY_WIDETAG:
555                 break;
556
557             case CODE_HEADER_WIDETAG:
558                 print_slots(code_slots, count-1, ptr);
559                 break;
560
561             case SIMPLE_FUN_HEADER_WIDETAG:
562                 print_slots(fn_slots, 5, ptr);
563                 break;
564
565             case RETURN_PC_HEADER_WIDETAG:
566                 print_obj("code: ", obj - (count * 4));
567                 break;
568
569             case CLOSURE_HEADER_WIDETAG:
570                 print_slots(closure_slots, count, ptr);
571                 break;
572
573             case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
574                 print_slots(funcallable_instance_slots, count, ptr);
575                 break;
576
577             case VALUE_CELL_HEADER_WIDETAG:
578                 print_slots(value_cell_slots, 1, ptr);
579                 break;
580
581             case SAP_WIDETAG:
582                 NEWLINE_OR_RETURN;
583 #ifndef LISP_FEATURE_ALPHA
584                 printf("0x%08lx", (unsigned long) *ptr);
585 #else
586                 printf("0x%016lx", *(lispobj*)(ptr+1));
587 #endif
588                 break;
589
590             case WEAK_POINTER_WIDETAG:
591                 print_slots(weak_pointer_slots, 1, ptr);
592                 break;
593
594             case CHARACTER_WIDETAG:
595             case UNBOUND_MARKER_WIDETAG:
596                 NEWLINE_OR_RETURN;
597                 printf("pointer to an immediate?");
598                 break;
599
600             case FDEFN_WIDETAG:
601                 print_slots(fdefn_slots, count, ptr);
602                 break;
603
604             default:
605                 NEWLINE_OR_RETURN;
606                 printf("Unknown header object?");
607                 break;
608         }
609     }
610 }
611
612 static void print_obj(char *prefix, lispobj obj)
613 {
614 #ifdef LISP_FEATURE_X86_64
615     static void (*verbose_fns[])(lispobj obj)
616         = {print_fixnum, print_otherimm, print_fixnum, print_struct,
617            print_fixnum, print_otherimm, print_fixnum, print_list,
618            print_fixnum, print_otherimm, print_fixnum, print_otherptr,
619            print_fixnum, print_otherimm, print_fixnum, print_otherptr};
620     static void (*brief_fns[])(lispobj obj)
621         = {brief_fixnum, brief_otherimm, brief_fixnum, brief_struct,
622            brief_fixnum, brief_otherimm, brief_fixnum, brief_list,
623            brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr,
624            brief_fixnum, brief_otherimm, brief_fixnum, brief_otherptr};
625 #else
626     static void (*verbose_fns[])(lispobj obj)
627         = {print_fixnum, print_struct, print_otherimm, print_list,
628            print_fixnum, print_otherptr, print_otherimm, print_otherptr};
629     static void (*brief_fns[])(lispobj obj)
630         = {brief_fixnum, brief_struct, brief_otherimm, brief_list,
631            brief_fixnum, brief_otherptr, brief_otherimm, brief_otherptr};
632 #endif
633     int type = lowtag_of(obj);
634     struct var *var = lookup_by_obj(obj);
635     char buffer[256];
636     boolean verbose = cur_depth < brief_depth;
637
638     if (!continue_p(verbose))
639         return;
640
641     if (var != NULL && var_clock(var) == cur_clock)
642         dont_descend = 1;
643
644     if (var == NULL && is_lisp_pointer(obj))
645         var = define_var(NULL, obj, 0);
646
647     if (var != NULL)
648         var_setclock(var, cur_clock);
649
650     cur_depth++;
651     if (verbose) {
652         if (var != NULL) {
653             sprintf(buffer, "$%s=", var_name(var));
654             newline(buffer);
655         }
656         else
657             newline(NULL);
658         printf("%s0x%08lx: ", prefix, (unsigned long) obj);
659         if (cur_depth < brief_depth) {
660             fputs(lowtag_names[type], stdout);
661             (*verbose_fns[type])(obj);
662         }
663         else
664             (*brief_fns[type])(obj);
665     }
666     else {
667         if (dont_descend)
668             printf("$%s", var_name(var));
669         else {
670             if (var != NULL)
671                 printf("$%s=", var_name(var));
672             (*brief_fns[type])(obj);
673         }
674     }
675     cur_depth--;
676     dont_descend = 0;
677 }
678
679 void reset_printer()
680 {
681     cur_clock++;
682     cur_lines = 0;
683     dont_descend = 0;
684 }
685
686 void print(lispobj obj)
687 {
688     skip_newline = 1;
689     cur_depth = 0;
690     max_depth = 5;
691     max_lines = 20;
692
693     print_obj("", obj);
694
695     putchar('\n');
696 }
697
698 void brief_print(lispobj obj)
699 {
700     skip_newline = 1;
701     cur_depth = 0;
702     max_depth = 1;
703     max_lines = 5000;
704
705     print_obj("", obj);
706     putchar('\n');
707 }
708
709 #else
710
711 void
712 brief_print(lispobj obj)
713 {
714     printf("lispobj 0x%lx\n", (unsigned long)obj);
715 }
716
717 #endif /* defined(LISP_FEATURE_SB_LDB) */