Produce new-style Cmm from the Cmm parser
[ghc.git] / rts / Printer.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1994-2000.
4 *
5 * Heap printer
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "rts/Bytecodes.h" /* for InstrPtr */
12
13 #include "sm/Storage.h"
14 #include "Printer.h"
15 #include "RtsUtils.h"
16
17 #include <string.h>
18
19 #ifdef DEBUG
20
21 #include "Disassembler.h"
22 #include "Apply.h"
23
24 /* --------------------------------------------------------------------------
25 * local function decls
26 * ------------------------------------------------------------------------*/
27
28 static void printStdObjPayload( StgClosure *obj );
29 #ifdef USING_LIBBFD
30 static void reset_table ( int size );
31 static void prepare_table ( void );
32 static void insert ( StgWord value, const char *name );
33 #endif
34 #if 0 /* unused but might be useful sometime */
35 static rtsBool lookup_name ( char *name, StgWord *result );
36 static void enZcode ( char *in, char *out );
37 #endif
38 static char unZcode ( char ch );
39 static void printZcoded ( const char *raw );
40
41 /* --------------------------------------------------------------------------
42 * Printer
43 * ------------------------------------------------------------------------*/
44
45 void printPtr( StgPtr p )
46 {
47 const char *raw;
48 raw = lookupGHCName(p);
49 if (raw != NULL) {
50 printZcoded(raw);
51 } else {
52 debugBelch("%p", p);
53 }
54 }
55
56 void printObj( StgClosure *obj )
57 {
58 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
59 printClosure(obj);
60 }
61
62 STATIC_INLINE void
63 printStdObjHdr( StgClosure *obj, char* tag )
64 {
65 debugBelch("%s(",tag);
66 printPtr((StgPtr)obj->header.info);
67 #ifdef PROFILING
68 debugBelch(", %s", obj->header.prof.ccs->cc->label);
69 #endif
70 }
71
72 static void
73 printStdObjPayload( StgClosure *obj )
74 {
75 StgWord i, j;
76 const StgInfoTable* info;
77
78 info = get_itbl(obj);
79 for (i = 0; i < info->layout.payload.ptrs; ++i) {
80 debugBelch(", ");
81 printPtr((StgPtr)obj->payload[i]);
82 }
83 for (j = 0; j < info->layout.payload.nptrs; ++j) {
84 debugBelch(", %pd#",obj->payload[i+j]);
85 }
86 debugBelch(")\n");
87 }
88
89 static void
90 printThunkPayload( StgThunk *obj )
91 {
92 StgWord i, j;
93 const StgInfoTable* info;
94
95 info = get_itbl((StgClosure *)obj);
96 for (i = 0; i < info->layout.payload.ptrs; ++i) {
97 debugBelch(", ");
98 printPtr((StgPtr)obj->payload[i]);
99 }
100 for (j = 0; j < info->layout.payload.nptrs; ++j) {
101 debugBelch(", %pd#",obj->payload[i+j]);
102 }
103 debugBelch(")\n");
104 }
105
106 static void
107 printThunkObject( StgThunk *obj, char* tag )
108 {
109 printStdObjHdr( (StgClosure *)obj, tag );
110 printThunkPayload( obj );
111 }
112
113 void
114 printClosure( StgClosure *obj )
115 {
116 obj = UNTAG_CLOSURE(obj);
117
118 StgInfoTable *info;
119 info = get_itbl(obj);
120
121 switch ( info->type ) {
122 case INVALID_OBJECT:
123 barf("Invalid object");
124
125 case CONSTR:
126 case CONSTR_1_0: case CONSTR_0_1:
127 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
128 case CONSTR_STATIC:
129 case CONSTR_NOCAF_STATIC:
130 {
131 StgWord i, j;
132 StgConInfoTable *con_info = get_con_itbl (obj);
133
134 debugBelch("%s(", GET_CON_DESC(con_info));
135 for (i = 0; i < info->layout.payload.ptrs; ++i) {
136 if (i != 0) debugBelch(", ");
137 printPtr((StgPtr)obj->payload[i]);
138 }
139 for (j = 0; j < info->layout.payload.nptrs; ++j) {
140 if (i != 0 || j != 0) debugBelch(", ");
141 debugBelch("%p#", obj->payload[i+j]);
142 }
143 debugBelch(")\n");
144 break;
145 }
146
147 case FUN:
148 case FUN_1_0: case FUN_0_1:
149 case FUN_1_1: case FUN_0_2: case FUN_2_0:
150 case FUN_STATIC:
151 debugBelch("FUN/%d(",(int)itbl_to_fun_itbl(info)->f.arity);
152 printPtr((StgPtr)obj->header.info);
153 #ifdef PROFILING
154 debugBelch(", %s", obj->header.prof.ccs->cc->label);
155 #endif
156 printStdObjPayload(obj);
157 break;
158
159 case PRIM:
160 debugBelch("PRIM(");
161 printPtr((StgPtr)obj->header.info);
162 printStdObjPayload(obj);
163 break;
164
165 case THUNK:
166 case THUNK_1_0: case THUNK_0_1:
167 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
168 case THUNK_STATIC:
169 /* ToDo: will this work for THUNK_STATIC too? */
170 #ifdef PROFILING
171 printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
172 #else
173 printThunkObject((StgThunk *)obj,"THUNK");
174 #endif
175 break;
176
177 case THUNK_SELECTOR:
178 printStdObjHdr(obj, "THUNK_SELECTOR");
179 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
180 break;
181
182 case BCO:
183 disassemble( (StgBCO*)obj );
184 break;
185
186 case AP:
187 {
188 StgAP* ap = (StgAP*)obj;
189 StgWord i;
190 debugBelch("AP("); printPtr((StgPtr)ap->fun);
191 for (i = 0; i < ap->n_args; ++i) {
192 debugBelch(", ");
193 printPtr((P_)ap->payload[i]);
194 }
195 debugBelch(")\n");
196 break;
197 }
198
199 case PAP:
200 {
201 StgPAP* pap = (StgPAP*)obj;
202 StgWord i;
203 debugBelch("PAP/%d(",(int)pap->arity);
204 printPtr((StgPtr)pap->fun);
205 for (i = 0; i < pap->n_args; ++i) {
206 debugBelch(", ");
207 printPtr((StgPtr)pap->payload[i]);
208 }
209 debugBelch(")\n");
210 break;
211 }
212
213 case AP_STACK:
214 {
215 StgAP_STACK* ap = (StgAP_STACK*)obj;
216 StgWord i;
217 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
218 for (i = 0; i < ap->size; ++i) {
219 debugBelch(", ");
220 printPtr((P_)ap->payload[i]);
221 }
222 debugBelch(")\n");
223 break;
224 }
225
226 case IND:
227 debugBelch("IND(");
228 printPtr((StgPtr)((StgInd*)obj)->indirectee);
229 debugBelch(")\n");
230 break;
231
232 case IND_PERM:
233 debugBelch("IND(");
234 printPtr((StgPtr)((StgInd*)obj)->indirectee);
235 debugBelch(")\n");
236 break;
237
238 case IND_STATIC:
239 debugBelch("IND_STATIC(");
240 printPtr((StgPtr)((StgInd*)obj)->indirectee);
241 debugBelch(")\n");
242 break;
243
244 case BLACKHOLE:
245 debugBelch("BLACKHOLE(");
246 printPtr((StgPtr)((StgInd*)obj)->indirectee);
247 debugBelch(")\n");
248 break;
249
250 /* Cannot happen -- use default case.
251 case RET_BCO:
252 case RET_SMALL:
253 case RET_BIG:
254 case RET_FUN:
255 */
256
257 case UPDATE_FRAME:
258 {
259 StgUpdateFrame* u = (StgUpdateFrame*)obj;
260 debugBelch("UPDATE_FRAME(");
261 printPtr((StgPtr)GET_INFO((StgClosure *)u));
262 debugBelch(",");
263 printPtr((StgPtr)u->updatee);
264 debugBelch(")\n");
265 break;
266 }
267
268 case CATCH_FRAME:
269 {
270 StgCatchFrame* u = (StgCatchFrame*)obj;
271 debugBelch("CATCH_FRAME(");
272 printPtr((StgPtr)GET_INFO((StgClosure *)u));
273 debugBelch(",");
274 printPtr((StgPtr)u->handler);
275 debugBelch(")\n");
276 break;
277 }
278
279 case UNDERFLOW_FRAME:
280 {
281 StgUnderflowFrame* u = (StgUnderflowFrame*)obj;
282 debugBelch("UNDERFLOW_FRAME(");
283 printPtr((StgPtr)u->next_chunk);
284 debugBelch(")\n");
285 break;
286 }
287
288 case STOP_FRAME:
289 {
290 StgStopFrame* u = (StgStopFrame*)obj;
291 debugBelch("STOP_FRAME(");
292 printPtr((StgPtr)GET_INFO((StgClosure *)u));
293 debugBelch(")\n");
294 break;
295 }
296
297 case ARR_WORDS:
298 {
299 StgWord i;
300 debugBelch("ARR_WORDS(\"");
301 for (i=0; i<arr_words_words((StgArrWords *)obj); i++)
302 debugBelch("%" FMT_Word, (W_)((StgArrWords *)obj)->payload[i]);
303 debugBelch("\")\n");
304 break;
305 }
306
307 case MUT_ARR_PTRS_CLEAN:
308 debugBelch("MUT_ARR_PTRS_CLEAN(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
309 break;
310
311 case MUT_ARR_PTRS_DIRTY:
312 debugBelch("MUT_ARR_PTRS_DIRTY(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
313 break;
314
315 case MUT_ARR_PTRS_FROZEN:
316 debugBelch("MUT_ARR_PTRS_FROZEN(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
317 break;
318
319 case MVAR_CLEAN:
320 case MVAR_DIRTY:
321 {
322 StgMVar* mv = (StgMVar*)obj;
323 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
324 break;
325 }
326
327 case MUT_VAR_CLEAN:
328 {
329 StgMutVar* mv = (StgMutVar*)obj;
330 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
331 break;
332 }
333
334 case MUT_VAR_DIRTY:
335 {
336 StgMutVar* mv = (StgMutVar*)obj;
337 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
338 break;
339 }
340
341 case WEAK:
342 debugBelch("WEAK(");
343 debugBelch(" key=%p value=%p finalizer=%p",
344 (StgPtr)(((StgWeak*)obj)->key),
345 (StgPtr)(((StgWeak*)obj)->value),
346 (StgPtr)(((StgWeak*)obj)->finalizer));
347 debugBelch(")\n");
348 /* ToDo: chase 'link' ? */
349 break;
350
351 case TSO:
352 debugBelch("TSO(");
353 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
354 debugBelch(")\n");
355 break;
356
357 case STACK:
358 debugBelch("STACK");
359 break;
360
361 #if 0
362 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
363 case EVACUATED:
364 debugBelch("EVACUATED(");
365 printClosure((StgEvacuated*)obj->evacuee);
366 debugBelch(")\n");
367 break;
368 #endif
369
370 default:
371 //barf("printClosure %d",get_itbl(obj)->type);
372 debugBelch("*** printClosure: unknown type %d ****\n",
373 (int)get_itbl(obj)->type );
374 barf("printClosure %d",get_itbl(obj)->type);
375 return;
376 }
377 }
378
379 /*
380 void printGraph( StgClosure *obj )
381 {
382 printClosure(obj);
383 }
384 */
385
386 StgPtr
387 printStackObj( StgPtr sp )
388 {
389 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
390
391 StgClosure* c = (StgClosure*)(*sp);
392 printPtr((StgPtr)*sp);
393 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
394 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
395 } else
396 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
397 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
398 } else
399 if (c == (StgClosure*)&stg_ctoi_F1_info) {
400 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
401 } else
402 if (c == (StgClosure*)&stg_ctoi_D1_info) {
403 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
404 } else
405 if (c == (StgClosure*)&stg_ctoi_V_info) {
406 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
407 } else
408 if (get_itbl(c)->type == BCO) {
409 debugBelch("\t\t\t");
410 debugBelch("BCO(...)\n");
411 }
412 else {
413 debugBelch("\t\t\t");
414 printClosure ( (StgClosure*)(*sp));
415 }
416 sp += 1;
417
418 return sp;
419
420 }
421
422 static void
423 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
424 {
425 nat i;
426
427 for(i = 0; i < size; i++, bitmap >>= 1 ) {
428 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
429 if ((bitmap & 1) == 0) {
430 printPtr((P_)payload[i]);
431 debugBelch("\n");
432 } else {
433 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
434 }
435 }
436 }
437
438 static void
439 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
440 {
441 StgWord bmp;
442 nat i, j;
443
444 i = 0;
445 for (bmp=0; i < size; bmp++) {
446 StgWord bitmap = large_bitmap->bitmap[bmp];
447 j = 0;
448 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
449 debugBelch(" stk[%" FMT_Word "] (%p) = ", (W_)(spBottom-(payload+i)), payload+i);
450 if ((bitmap & 1) == 0) {
451 printPtr((P_)payload[i]);
452 debugBelch("\n");
453 } else {
454 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
455 }
456 }
457 }
458 }
459
460 void
461 printStackChunk( StgPtr sp, StgPtr spBottom )
462 {
463 StgWord bitmap;
464 const StgInfoTable *info;
465
466 ASSERT(sp <= spBottom);
467 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
468
469 info = get_itbl((StgClosure *)sp);
470
471 switch (info->type) {
472
473 case UPDATE_FRAME:
474 case CATCH_FRAME:
475 case UNDERFLOW_FRAME:
476 case STOP_FRAME:
477 printObj((StgClosure*)sp);
478 continue;
479
480 case RET_SMALL:
481 debugBelch("RET_SMALL (%p)\n", info);
482 bitmap = info->layout.bitmap;
483 printSmallBitmap(spBottom, sp+1,
484 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
485 continue;
486
487 case RET_BCO: {
488 StgBCO *bco;
489
490 bco = ((StgBCO *)sp[1]);
491
492 debugBelch("RET_BCO (%p)\n", sp);
493 printLargeBitmap(spBottom, sp+2,
494 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
495 continue;
496 }
497
498 case RET_BIG:
499 barf("todo");
500
501 case RET_FUN:
502 {
503 StgFunInfoTable *fun_info;
504 StgRetFun *ret_fun;
505
506 ret_fun = (StgRetFun *)sp;
507 fun_info = get_fun_itbl(ret_fun->fun);
508 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, (int)fun_info->f.fun_type);
509 switch (fun_info->f.fun_type) {
510 case ARG_GEN:
511 printSmallBitmap(spBottom, sp+2,
512 BITMAP_BITS(fun_info->f.b.bitmap),
513 BITMAP_SIZE(fun_info->f.b.bitmap));
514 break;
515 case ARG_GEN_BIG:
516 printLargeBitmap(spBottom, sp+2,
517 GET_FUN_LARGE_BITMAP(fun_info),
518 GET_FUN_LARGE_BITMAP(fun_info)->size);
519 break;
520 default:
521 printSmallBitmap(spBottom, sp+2,
522 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
523 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
524 break;
525 }
526 continue;
527 }
528
529 default:
530 debugBelch("unknown object %d\n", (int)info->type);
531 barf("printStackChunk");
532 }
533 }
534 }
535
536 void printTSO( StgTSO *tso )
537 {
538 printStackChunk( tso->stackobj->sp,
539 tso->stackobj->stack+tso->stackobj->stack_size);
540 }
541
542 /* --------------------------------------------------------------------------
543 * Address printing code
544 *
545 * Uses symbol table in (unstripped executable)
546 * ------------------------------------------------------------------------*/
547
548 /* --------------------------------------------------------------------------
549 * Simple lookup table
550 *
551 * Current implementation is pretty dumb!
552 * ------------------------------------------------------------------------*/
553
554 struct entry {
555 StgWord value;
556 const char *name;
557 };
558
559 static nat table_size;
560 static struct entry* table;
561
562 #ifdef USING_LIBBFD
563 static nat max_table_size;
564
565 static void reset_table( int size )
566 {
567 max_table_size = size;
568 table_size = 0;
569 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
570 }
571
572 static void prepare_table( void )
573 {
574 /* Could sort it... */
575 }
576
577 static void insert( StgWord value, const char *name )
578 {
579 if ( table_size >= max_table_size ) {
580 barf( "Symbol table overflow\n" );
581 }
582 table[table_size].value = value;
583 table[table_size].name = name;
584 table_size = table_size + 1;
585 }
586 #endif
587
588 #if 0
589 static rtsBool lookup_name( char *name, StgWord *result )
590 {
591 nat i;
592 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
593 }
594 if (i < table_size) {
595 *result = table[i].value;
596 return rtsTrue;
597 } else {
598 return rtsFalse;
599 }
600 }
601 #endif
602
603 /* Code from somewhere inside GHC (circa 1994)
604 * * Z-escapes:
605 * "std"++xs -> "Zstd"++xs
606 * char_to_c 'Z' = "ZZ"
607 * char_to_c '&' = "Za"
608 * char_to_c '|' = "Zb"
609 * char_to_c ':' = "Zc"
610 * char_to_c '/' = "Zd"
611 * char_to_c '=' = "Ze"
612 * char_to_c '>' = "Zg"
613 * char_to_c '#' = "Zh"
614 * char_to_c '<' = "Zl"
615 * char_to_c '-' = "Zm"
616 * char_to_c '!' = "Zn"
617 * char_to_c '.' = "Zo"
618 * char_to_c '+' = "Zp"
619 * char_to_c '\'' = "Zq"
620 * char_to_c '*' = "Zt"
621 * char_to_c '_' = "Zu"
622 * char_to_c c = "Z" ++ show (ord c)
623 */
624 static char unZcode( char ch )
625 {
626 switch (ch) {
627 case 'a' : return ('&');
628 case 'b' : return ('|');
629 case 'c' : return (':');
630 case 'd' : return ('/');
631 case 'e' : return ('=');
632 case 'g' : return ('>');
633 case 'h' : return ('#');
634 case 'l' : return ('<');
635 case 'm' : return ('-');
636 case 'n' : return ('!');
637 case 'o' : return ('.');
638 case 'p' : return ('+');
639 case 'q' : return ('\'');
640 case 't' : return ('*');
641 case 'u' : return ('_');
642 case 'Z' :
643 case '\0' : return ('Z');
644 default : return (ch);
645 }
646 }
647
648 #if 0
649 /* Precondition: out big enough to handle output (about twice length of in) */
650 static void enZcode( char *in, char *out )
651 {
652 int i, j;
653
654 j = 0;
655 out[ j++ ] = '_';
656 for( i = 0; in[i] != '\0'; ++i ) {
657 switch (in[i]) {
658 case 'Z' :
659 out[j++] = 'Z';
660 out[j++] = 'Z';
661 break;
662 case '&' :
663 out[j++] = 'Z';
664 out[j++] = 'a';
665 break;
666 case '|' :
667 out[j++] = 'Z';
668 out[j++] = 'b';
669 break;
670 case ':' :
671 out[j++] = 'Z';
672 out[j++] = 'c';
673 break;
674 case '/' :
675 out[j++] = 'Z';
676 out[j++] = 'd';
677 break;
678 case '=' :
679 out[j++] = 'Z';
680 out[j++] = 'e';
681 break;
682 case '>' :
683 out[j++] = 'Z';
684 out[j++] = 'g';
685 break;
686 case '#' :
687 out[j++] = 'Z';
688 out[j++] = 'h';
689 break;
690 case '<' :
691 out[j++] = 'Z';
692 out[j++] = 'l';
693 break;
694 case '-' :
695 out[j++] = 'Z';
696 out[j++] = 'm';
697 break;
698 case '!' :
699 out[j++] = 'Z';
700 out[j++] = 'n';
701 break;
702 case '.' :
703 out[j++] = 'Z';
704 out[j++] = 'o';
705 break;
706 case '+' :
707 out[j++] = 'Z';
708 out[j++] = 'p';
709 break;
710 case '\'' :
711 out[j++] = 'Z';
712 out[j++] = 'q';
713 break;
714 case '*' :
715 out[j++] = 'Z';
716 out[j++] = 't';
717 break;
718 case '_' :
719 out[j++] = 'Z';
720 out[j++] = 'u';
721 break;
722 default :
723 out[j++] = in[i];
724 break;
725 }
726 }
727 out[j] = '\0';
728 }
729 #endif
730
731 const char *lookupGHCName( void *addr )
732 {
733 nat i;
734 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
735 }
736 if (i < table_size) {
737 return table[i].name;
738 } else {
739 return NULL;
740 }
741 }
742
743 static void printZcoded( const char *raw )
744 {
745 nat j = 0;
746
747 while ( raw[j] != '\0' ) {
748 if (raw[j] == 'Z') {
749 debugBelch("%c", unZcode(raw[j+1]));
750 j = j + 2;
751 } else {
752 debugBelch("%c", unZcode(raw[j+1]));
753 j = j + 1;
754 }
755 }
756 }
757
758 /* --------------------------------------------------------------------------
759 * Symbol table loading
760 * ------------------------------------------------------------------------*/
761
762 /* Causing linking trouble on Win32 plats, so I'm
763 disabling this for now.
764 */
765 #ifdef USING_LIBBFD
766
767 #include <bfd.h>
768
769 /* Fairly ad-hoc piece of code that seems to filter out a lot of
770 * rubbish like the obj-splitting symbols
771 */
772
773 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
774 {
775 #if 0
776 /* ToDo: make this work on BFD */
777 int tp = type & N_TYPE;
778 if (tp == N_TEXT || tp == N_DATA) {
779 return (name[0] == '_' && name[1] != '_');
780 } else {
781 return rtsFalse;
782 }
783 #else
784 if (*name == '\0' ||
785 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
786 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
787 return rtsFalse;
788 }
789 return rtsTrue;
790 #endif
791 }
792
793 extern void DEBUG_LoadSymbols( char *name )
794 {
795 bfd* abfd;
796 char **matching;
797
798 bfd_init();
799 abfd = bfd_openr(name, "default");
800 if (abfd == NULL) {
801 barf("can't open executable %s to get symbol table", name);
802 }
803 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
804 barf("mismatch");
805 }
806
807 {
808 long storage_needed;
809 asymbol **symbol_table;
810 long number_of_symbols;
811 long num_real_syms = 0;
812 long i;
813
814 storage_needed = bfd_get_symtab_upper_bound (abfd);
815
816 if (storage_needed < 0) {
817 barf("can't read symbol table");
818 }
819 #if 0
820 if (storage_needed == 0) {
821 debugBelch("no storage needed");
822 }
823 #endif
824 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
825
826 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
827
828 if (number_of_symbols < 0) {
829 barf("can't canonicalise symbol table");
830 }
831
832 for( i = 0; i != number_of_symbols; ++i ) {
833 symbol_info info;
834 bfd_get_symbol_info(abfd,symbol_table[i],&info);
835 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
836 if (isReal(info.type, info.name)) {
837 num_real_syms += 1;
838 }
839 }
840
841 IF_DEBUG(interpreter,
842 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
843 number_of_symbols, num_real_syms)
844 );
845
846 reset_table( num_real_syms );
847
848 for( i = 0; i != number_of_symbols; ++i ) {
849 symbol_info info;
850 bfd_get_symbol_info(abfd,symbol_table[i],&info);
851 if (isReal(info.type, info.name)) {
852 insert( info.value, info.name );
853 }
854 }
855
856 stgFree(symbol_table);
857 }
858 prepare_table();
859 }
860
861 #else /* HAVE_BFD_H */
862
863 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
864 {
865 /* nothing, yet */
866 }
867
868 #endif /* HAVE_BFD_H */
869
870 void findPtr(P_ p, int); /* keep gcc -Wall happy */
871
872 int searched = 0;
873
874 static int
875 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
876 {
877 StgPtr q, r, end;
878 for (; bd; bd = bd->link) {
879 searched++;
880 for (q = bd->start; q < bd->free; q++) {
881 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
882 if (i < arr_size) {
883 for (r = bd->start; r < bd->free; r = end) {
884 // skip over zeroed-out slop
885 while (*r == 0) r++;
886 if (!LOOKS_LIKE_CLOSURE_PTR(r)) {
887 debugBelch("%p found at %p, no closure at %p\n",
888 p, q, r);
889 break;
890 }
891 end = r + closure_sizeW((StgClosure*)r);
892 if (q < end) {
893 debugBelch("%p = ", r);
894 printClosure((StgClosure *)r);
895 arr[i++] = r;
896 break;
897 }
898 }
899 if (r >= bd->free) {
900 debugBelch("%p found at %p, closure?", p, q);
901 }
902 } else {
903 return i;
904 }
905 }
906 }
907 }
908 return i;
909 }
910
911 void
912 findPtr(P_ p, int follow)
913 {
914 nat g, n;
915 bdescr *bd;
916 const int arr_size = 1024;
917 StgPtr arr[arr_size];
918 int i = 0;
919 searched = 0;
920
921 for (n = 0; n < n_capabilities; n++) {
922 bd = nurseries[i].blocks;
923 i = findPtrBlocks(p,bd,arr,arr_size,i);
924 if (i >= arr_size) return;
925 }
926
927 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
928 bd = generations[g].blocks;
929 i = findPtrBlocks(p,bd,arr,arr_size,i);
930 bd = generations[g].large_objects;
931 i = findPtrBlocks(p,bd,arr,arr_size,i);
932 if (i >= arr_size) return;
933 }
934 if (follow && i == 1) {
935 debugBelch("-->\n");
936 findPtr(arr[0], 1);
937 }
938 }
939
940 /* prettyPrintClosure() is for printing out a closure using the data constructor
941 names found in the info tables. Closures are printed in a fashion that resembles
942 their Haskell representation. Useful during debugging.
943
944 Todo: support for more closure types, and support for non pointer fields in the
945 payload.
946 */
947
948 void prettyPrintClosure_ (StgClosure *);
949
950 void prettyPrintClosure (StgClosure *obj)
951 {
952 prettyPrintClosure_ (obj);
953 debugBelch ("\n");
954 }
955
956 void prettyPrintClosure_ (StgClosure *obj)
957 {
958 StgInfoTable *info;
959 StgConInfoTable *con_info;
960
961 /* collapse any indirections */
962 unsigned int type;
963 type = get_itbl(obj)->type;
964
965 while (type == IND ||
966 type == IND_STATIC ||
967 type == IND_PERM)
968 {
969 obj = ((StgInd *)obj)->indirectee;
970 type = get_itbl(obj)->type;
971 }
972
973 /* find the info table for this object */
974 info = get_itbl(obj);
975
976 /* determine what kind of object we have */
977 switch (info->type)
978 {
979 /* full applications of data constructors */
980 case CONSTR:
981 case CONSTR_1_0:
982 case CONSTR_0_1:
983 case CONSTR_1_1:
984 case CONSTR_0_2:
985 case CONSTR_2_0:
986 case CONSTR_STATIC:
987 case CONSTR_NOCAF_STATIC:
988 {
989 nat i;
990 char *descriptor;
991
992 /* find the con_info for the constructor */
993 con_info = get_con_itbl (obj);
994
995 /* obtain the name of the constructor */
996 descriptor = GET_CON_DESC(con_info);
997
998 debugBelch ("(%s", descriptor);
999
1000 /* process the payload of the closure */
1001 /* we don't handle non pointers at the moment */
1002 for (i = 0; i < info->layout.payload.ptrs; i++)
1003 {
1004 debugBelch (" ");
1005 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1006 }
1007 debugBelch (")");
1008 break;
1009 }
1010
1011 /* if it isn't a constructor then just print the closure type */
1012 default:
1013 {
1014 debugBelch ("<%s>", info_type(obj));
1015 break;
1016 }
1017 }
1018 }
1019
1020 char *what_next_strs[] = {
1021 [0] = "(unknown)",
1022 [ThreadRunGHC] = "ThreadRunGHC",
1023 [ThreadInterpret] = "ThreadInterpret",
1024 [ThreadKilled] = "ThreadKilled",
1025 [ThreadComplete] = "ThreadComplete"
1026 };
1027
1028 #else /* DEBUG */
1029 void printPtr( StgPtr p )
1030 {
1031 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1032 }
1033
1034 void printObj( StgClosure *obj )
1035 {
1036 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
1037 }
1038
1039
1040 #endif /* DEBUG */
1041
1042 /* -----------------------------------------------------------------------------
1043 Closure types
1044
1045 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
1046 -------------------------------------------------------------------------- */
1047
1048 char *closure_type_names[] = {
1049 [INVALID_OBJECT] = "INVALID_OBJECT",
1050 [CONSTR] = "CONSTR",
1051 [CONSTR_1_0] = "CONSTR_1_0",
1052 [CONSTR_0_1] = "CONSTR_0_1",
1053 [CONSTR_2_0] = "CONSTR_2_0",
1054 [CONSTR_1_1] = "CONSTR_1_1",
1055 [CONSTR_0_2] = "CONSTR_0_2",
1056 [CONSTR_STATIC] = "CONSTR_STATIC",
1057 [CONSTR_NOCAF_STATIC] = "CONSTR_NOCAF_STATIC",
1058 [FUN] = "FUN",
1059 [FUN_1_0] = "FUN_1_0",
1060 [FUN_0_1] = "FUN_0_1",
1061 [FUN_2_0] = "FUN_2_0",
1062 [FUN_1_1] = "FUN_1_1",
1063 [FUN_0_2] = "FUN_0_2",
1064 [FUN_STATIC] = "FUN_STATIC",
1065 [THUNK] = "THUNK",
1066 [THUNK_1_0] = "THUNK_1_0",
1067 [THUNK_0_1] = "THUNK_0_1",
1068 [THUNK_2_0] = "THUNK_2_0",
1069 [THUNK_1_1] = "THUNK_1_1",
1070 [THUNK_0_2] = "THUNK_0_2",
1071 [THUNK_STATIC] = "THUNK_STATIC",
1072 [THUNK_SELECTOR] = "THUNK_SELECTOR",
1073 [BCO] = "BCO",
1074 [AP] = "AP",
1075 [PAP] = "PAP",
1076 [AP_STACK] = "AP_STACK",
1077 [IND] = "IND",
1078 [IND_PERM] = "IND_PERM",
1079 [IND_STATIC] = "IND_STATIC",
1080 [RET_BCO] = "RET_BCO",
1081 [RET_SMALL] = "RET_SMALL",
1082 [RET_BIG] = "RET_BIG",
1083 [RET_FUN] = "RET_FUN",
1084 [UPDATE_FRAME] = "UPDATE_FRAME",
1085 [CATCH_FRAME] = "CATCH_FRAME",
1086 [UNDERFLOW_FRAME] = "UNDERFLOW_FRAME",
1087 [STOP_FRAME] = "STOP_FRAME",
1088 [BLOCKING_QUEUE] = "BLOCKING_QUEUE",
1089 [BLACKHOLE] = "BLACKHOLE",
1090 [MVAR_CLEAN] = "MVAR_CLEAN",
1091 [MVAR_DIRTY] = "MVAR_DIRTY",
1092 [ARR_WORDS] = "ARR_WORDS",
1093 [MUT_ARR_PTRS_CLEAN] = "MUT_ARR_PTRS_CLEAN",
1094 [MUT_ARR_PTRS_DIRTY] = "MUT_ARR_PTRS_DIRTY",
1095 [MUT_ARR_PTRS_FROZEN0] = "MUT_ARR_PTRS_FROZEN0",
1096 [MUT_ARR_PTRS_FROZEN] = "MUT_ARR_PTRS_FROZEN",
1097 [MUT_VAR_CLEAN] = "MUT_VAR_CLEAN",
1098 [MUT_VAR_DIRTY] = "MUT_VAR_DIRTY",
1099 [WEAK] = "WEAK",
1100 [PRIM] = "PRIM",
1101 [MUT_PRIM] = "MUT_PRIM",
1102 [TSO] = "TSO",
1103 [STACK] = "STACK",
1104 [TREC_CHUNK] = "TREC_CHUNK",
1105 [ATOMICALLY_FRAME] = "ATOMICALLY_FRAME",
1106 [CATCH_RETRY_FRAME] = "CATCH_RETRY_FRAME",
1107 [CATCH_STM_FRAME] = "CATCH_STM_FRAME",
1108 [WHITEHOLE] = "WHITEHOLE"
1109 };
1110
1111 char *
1112 info_type(StgClosure *closure){
1113 return closure_type_names[get_itbl(closure)->type];
1114 }
1115
1116 char *
1117 info_type_by_ip(StgInfoTable *ip){
1118 return closure_type_names[ip->type];
1119 }
1120
1121 void
1122 info_hdr_type(StgClosure *closure, char *res){
1123 strcpy(res,closure_type_names[get_itbl(closure)->type]);
1124 }
1125