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