Reorganisation of the source tree
[ghc-hetmet.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 "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_CLEAN:
355         {
356           StgMutVar* mv = (StgMutVar*)obj;
357           debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
358           break;
359         }
360
361     case MUT_VAR_DIRTY:
362         {
363           StgMutVar* mv = (StgMutVar*)obj;
364           debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
365           break;
366         }
367
368     case WEAK:
369             debugBelch("WEAK("); 
370             debugBelch(" key=%p value=%p finalizer=%p", 
371                     (StgPtr)(((StgWeak*)obj)->key),
372                     (StgPtr)(((StgWeak*)obj)->value),
373                     (StgPtr)(((StgWeak*)obj)->finalizer));
374             debugBelch(")\n"); 
375             /* ToDo: chase 'link' ? */
376             break;
377
378     case STABLE_NAME:
379             debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn); 
380             break;
381
382     case TSO:
383       debugBelch("TSO("); 
384       debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
385       debugBelch(")\n"); 
386       break;
387
388 #if defined(PAR)
389     case BLOCKED_FETCH:
390       debugBelch("BLOCKED_FETCH("); 
391       printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
392       printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
393       debugBelch(")\n"); 
394       break;
395
396     case FETCH_ME:
397       debugBelch("FETCH_ME("); 
398       printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
399       debugBelch(")\n"); 
400       break;
401
402     case FETCH_ME_BQ:
403       debugBelch("FETCH_ME_BQ("); 
404       // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
405       printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
406       debugBelch(")\n"); 
407       break;
408 #endif
409
410 #if defined(GRAN) || defined(PAR)
411     case RBH:
412       debugBelch("RBH("); 
413       printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
414       debugBelch(")\n"); 
415       break;
416
417 #endif
418
419 #if 0
420       /* Symptomatic of a problem elsewhere, have it fall-through & fail */
421     case EVACUATED:
422       debugBelch("EVACUATED("); 
423       printClosure((StgEvacuated*)obj->evacuee);
424       debugBelch(")\n"); 
425       break;
426 #endif
427
428 #if defined(PAR) && defined(DIST)
429     case REMOTE_REF:
430       debugBelch("REMOTE_REF("); 
431       printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
432       debugBelch(")\n"); 
433       break;
434 #endif
435
436     default:
437             //barf("printClosure %d",get_itbl(obj)->type);
438             debugBelch("*** printClosure: unknown type %d ****\n",
439                     get_itbl(obj)->type );
440             barf("printClosure %d",get_itbl(obj)->type);
441             return;
442     }
443 }
444
445 /*
446 void printGraph( StgClosure *obj )
447 {
448  printClosure(obj);
449 }
450 */
451
452 StgPtr
453 printStackObj( StgPtr sp )
454 {
455     /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
456
457         StgClosure* c = (StgClosure*)(*sp);
458         printPtr((StgPtr)*sp);
459         if (c == (StgClosure*)&stg_ctoi_R1p_info) {
460            debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
461         } else
462         if (c == (StgClosure*)&stg_ctoi_R1n_info) {
463            debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
464         } else
465         if (c == (StgClosure*)&stg_ctoi_F1_info) {
466            debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
467         } else
468         if (c == (StgClosure*)&stg_ctoi_D1_info) {
469            debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
470         } else
471         if (c == (StgClosure*)&stg_ctoi_V_info) {
472            debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
473         } else
474         if (get_itbl(c)->type == BCO) {
475            debugBelch("\t\t\t");
476            debugBelch("BCO(...)\n"); 
477         }
478         else {
479            debugBelch("\t\t\t");
480            printClosure ( (StgClosure*)(*sp));
481         }
482         sp += 1;
483
484     return sp;
485     
486 }
487
488 static void
489 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
490 {
491     StgPtr p;
492     nat i;
493
494     p = payload;
495     for(i = 0; i < size; i++, bitmap >>= 1 ) {
496         debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
497         if ((bitmap & 1) == 0) {
498             printPtr((P_)payload[i]);
499             debugBelch("\n");
500         } else {
501             debugBelch("Word# %lu\n", (lnat)payload[i]);
502         }
503     }
504 }
505
506 static void
507 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
508 {
509     StgWord bmp;
510     nat i, j;
511
512     i = 0;
513     for (bmp=0; i < size; bmp++) {
514         StgWord bitmap = large_bitmap->bitmap[bmp];
515         j = 0;
516         for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
517             debugBelch("   stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
518             if ((bitmap & 1) == 0) {
519                 printPtr((P_)payload[i]);
520                 debugBelch("\n");
521             } else {
522                 debugBelch("Word# %lu\n", (lnat)payload[i]);
523             }
524         }
525     }
526 }
527
528 void
529 printStackChunk( StgPtr sp, StgPtr spBottom )
530 {
531     StgWord bitmap;
532     const StgInfoTable *info;
533
534     ASSERT(sp <= spBottom);
535     for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
536
537         info = get_itbl((StgClosure *)sp);
538
539         switch (info->type) {
540             
541         case UPDATE_FRAME:
542         case CATCH_FRAME:
543             printObj((StgClosure*)sp);
544             continue;
545
546         case STOP_FRAME:
547             printObj((StgClosure*)sp);
548             return;
549
550         case RET_DYN:
551         { 
552             StgRetDyn* r;
553             StgPtr p;
554             StgWord dyn;
555             nat size;
556
557             r = (StgRetDyn *)sp;
558             dyn = r->liveness;
559             debugBelch("RET_DYN (%p)\n", r);
560
561             p = (P_)(r->payload);
562             printSmallBitmap(spBottom, sp,
563                              RET_DYN_LIVENESS(r->liveness), 
564                              RET_DYN_BITMAP_SIZE);
565             p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
566
567             for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
568                 debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-p), p);
569                 debugBelch("Word# %ld\n", (long)*p);
570                 p++;
571             }
572         
573             for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
574                 debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-p), p);
575                 printPtr(p);
576                 p++;
577             }
578             continue;
579         }
580
581         case RET_SMALL:
582         case RET_VEC_SMALL:
583             debugBelch("RET_SMALL (%p)\n", info);
584             bitmap = info->layout.bitmap;
585             printSmallBitmap(spBottom, sp+1, 
586                              BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
587             continue;
588
589         case RET_BCO: {
590             StgBCO *bco;
591             
592             bco = ((StgBCO *)sp[1]);
593
594             debugBelch("RET_BCO (%p)\n", sp);
595             printLargeBitmap(spBottom, sp+2,
596                              BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
597             continue;
598         }
599
600         case RET_BIG:
601         case RET_VEC_BIG:
602             barf("todo");
603
604         case RET_FUN:
605         {
606             StgFunInfoTable *fun_info;
607             StgRetFun *ret_fun;
608             nat size;
609
610             ret_fun = (StgRetFun *)sp;
611             fun_info = get_fun_itbl(ret_fun->fun);
612             size = ret_fun->size;
613             debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
614             switch (fun_info->f.fun_type) {
615             case ARG_GEN:
616                 printSmallBitmap(spBottom, sp+2,
617                                  BITMAP_BITS(fun_info->f.b.bitmap),
618                                  BITMAP_SIZE(fun_info->f.b.bitmap));
619                 break;
620             case ARG_GEN_BIG:
621                 printLargeBitmap(spBottom, sp+2,
622                                  GET_FUN_LARGE_BITMAP(fun_info),
623                                  GET_FUN_LARGE_BITMAP(fun_info)->size);
624                 break;
625             default:
626                 printSmallBitmap(spBottom, sp+2,
627                                  BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
628                                  BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
629                 break;
630             }
631             continue;
632         }
633            
634         default:
635             debugBelch("unknown object %d\n", info->type);
636             barf("printStackChunk");
637         }
638     }
639 }
640
641 void printTSO( StgTSO *tso )
642 {
643     printStackChunk( tso->sp, tso->stack+tso->stack_size);
644 }
645
646 /* -----------------------------------------------------------------------------
647    Closure types
648    
649    NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
650    -------------------------------------------------------------------------- */
651
652 static char *closure_type_names[] = {
653     "INVALID_OBJECT",
654     "CONSTR",
655     "CONSTR_1",
656     "CONSTR_0",
657     "CONSTR_2",
658     "CONSTR_1",
659     "CONSTR_0",
660     "CONSTR_INTLIKE",
661     "CONSTR_CHARLIKE",
662     "CONSTR_STATIC",
663     "CONSTR_NOCAF_STATIC",
664     "FUN",
665     "FUN_1_0",
666     "FUN_0_1",
667     "FUN_2_0",
668     "FUN_1_1",
669     "FUN_0",
670     "FUN_STATIC",
671     "THUNK",
672     "THUNK_1_0",
673     "THUNK_0_1",
674     "THUNK_2_0",
675     "THUNK_1_1",
676     "THUNK_0",
677     "THUNK_STATIC",
678     "THUNK_SELECTOR",
679     "BCO",
680     "AP_UPD",
681     "PAP",
682     "AP_STACK",
683     "IND",
684     "IND_OLDGEN",
685     "IND_PERM",
686     "IND_OLDGEN_PERM",
687     "IND_STATIC",
688     "RET_BCO",
689     "RET_SMALL",
690     "RET_VEC_SMALL",
691     "RET_BIG",
692     "RET_VEC_BIG",
693     "RET_DYN",
694     "RET_FUN",
695     "UPDATE_FRAME",
696     "CATCH_FRAME",
697     "STOP_FRAME",
698     "CAF_BLACKHOLE",
699     "BLACKHOLE",
700     "BLACKHOLE_BQ",
701     "SE_BLACKHOLE",
702     "SE_CAF_BLACKHOLE",
703     "MVAR",
704     "ARR_WORDS",
705     "MUT_ARR_PTRS_CLEAN",
706     "MUT_ARR_PTRS_DIRTY",
707     "MUT_ARR_PTRS_FROZEN",
708     "MUT_VAR_CLEAN",
709     "MUT_VAR_DIRTY",
710     "MUT_CONS",
711     "WEAK",
712     "FOREIGN",
713     "STABLE_NAME",
714     "TSO",
715     "BLOCKED_FETCH",
716     "FETCH_ME",
717     "FETCH_ME_BQ",
718     "RBH",
719     "EVACUATED",
720     "REMOTE_REF",
721     "TVAR_WAIT_QUEUE",
722     "TVAR",
723     "TREC_CHUNK",
724     "TREC_HEADER",
725     "ATOMICALLY_FRAME",
726     "CATCH_RETRY_FRAME"
727 };
728
729
730 char *
731 info_type(StgClosure *closure){ 
732   return closure_type_names[get_itbl(closure)->type];
733 }
734
735 char *
736 info_type_by_ip(StgInfoTable *ip){ 
737   return closure_type_names[ip->type];
738 }
739
740 void
741 info_hdr_type(StgClosure *closure, char *res){ 
742   strcpy(res,closure_type_names[get_itbl(closure)->type]);
743 }
744
745 /* --------------------------------------------------------------------------
746  * Address printing code
747  *
748  * Uses symbol table in (unstripped executable)
749  * ------------------------------------------------------------------------*/
750
751 /* --------------------------------------------------------------------------
752  * Simple lookup table
753  *
754  * Current implementation is pretty dumb!
755  * ------------------------------------------------------------------------*/
756
757 struct entry {
758     nat value;
759     const char *name;
760 };
761
762 static nat table_size;
763 static struct entry* table;
764
765 #ifdef USING_LIBBFD
766 static nat max_table_size;
767
768 static void reset_table( int size )
769 {
770     max_table_size = size;
771     table_size = 0;
772     table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
773 }
774
775 static void prepare_table( void )
776 {
777     /* Could sort it...  */
778 }
779
780 static void insert( unsigned value, const char *name )
781 {
782     if ( table_size >= max_table_size ) {
783         barf( "Symbol table overflow\n" );
784     }
785     table[table_size].value = value;
786     table[table_size].name = name;
787     table_size = table_size + 1;
788 }
789 #endif
790
791 #if 0
792 static rtsBool lookup_name( char *name, unsigned *result )
793 {
794     int i;
795     for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
796     }
797     if (i < table_size) {
798         *result = table[i].value;
799         return rtsTrue;
800     } else {
801         return rtsFalse;
802     }
803 }
804 #endif
805
806 /* Code from somewhere inside GHC (circa 1994)
807  * * Z-escapes:
808  *     "std"++xs -> "Zstd"++xs
809  *     char_to_c 'Z'  = "ZZ"
810  *     char_to_c '&'  = "Za"
811  *     char_to_c '|'  = "Zb"
812  *     char_to_c ':'  = "Zc"
813  *     char_to_c '/'  = "Zd"
814  *     char_to_c '='  = "Ze"
815  *     char_to_c '>'  = "Zg"
816  *     char_to_c '#'  = "Zh"
817  *     char_to_c '<'  = "Zl"
818  *     char_to_c '-'  = "Zm"
819  *     char_to_c '!'  = "Zn"
820  *     char_to_c '.'  = "Zo"
821  *     char_to_c '+'  = "Zp"
822  *     char_to_c '\'' = "Zq"
823  *     char_to_c '*'  = "Zt"
824  *     char_to_c '_'  = "Zu"
825  *     char_to_c c    = "Z" ++ show (ord c)
826  */
827 static char unZcode( char ch )
828 {
829     switch (ch) {
830     case 'a'  : return ('&');
831     case 'b'  : return ('|');
832     case 'c'  : return (':');
833     case 'd'  : return ('/');
834     case 'e'  : return ('=');
835     case 'g'  : return ('>');
836     case 'h'  : return ('#');
837     case 'l'  : return ('<');
838     case 'm'  : return ('-');
839     case 'n'  : return ('!');
840     case 'o'  : return ('.');
841     case 'p'  : return ('+');
842     case 'q'  : return ('\'');
843     case 't'  : return ('*');
844     case 'u'  : return ('_');
845     case 'Z'  :
846     case '\0' : return ('Z');
847     default   : return (ch);
848     }
849 }
850
851 #if 0
852 /* Precondition: out big enough to handle output (about twice length of in) */
853 static void enZcode( char *in, char *out )
854 {
855     int i, j;
856
857     j = 0;
858     out[ j++ ] = '_';
859     for( i = 0; in[i] != '\0'; ++i ) {
860         switch (in[i]) {
861         case 'Z'  : 
862                 out[j++] = 'Z';
863                 out[j++] = 'Z';
864                 break;
865         case '&'  : 
866                 out[j++] = 'Z';
867                 out[j++] = 'a';
868                 break;
869         case '|'  : 
870                 out[j++] = 'Z';
871                 out[j++] = 'b';
872                 break;
873         case ':'  : 
874                 out[j++] = 'Z';
875                 out[j++] = 'c';
876                 break;
877         case '/'  : 
878                 out[j++] = 'Z';
879                 out[j++] = 'd';
880                 break;
881         case '='  : 
882                 out[j++] = 'Z';
883                 out[j++] = 'e';
884                 break;
885         case '>'  : 
886                 out[j++] = 'Z';
887                 out[j++] = 'g';
888                 break;
889         case '#'  : 
890                 out[j++] = 'Z';
891                 out[j++] = 'h';
892                 break;
893         case '<'  : 
894                 out[j++] = 'Z';
895                 out[j++] = 'l';
896                 break;
897         case '-'  : 
898                 out[j++] = 'Z';
899                 out[j++] = 'm';
900                 break;
901         case '!'  : 
902                 out[j++] = 'Z';
903                 out[j++] = 'n';
904                 break;
905         case '.'  : 
906                 out[j++] = 'Z';
907                 out[j++] = 'o';
908                 break;
909         case '+'  : 
910                 out[j++] = 'Z';
911                 out[j++] = 'p';
912                 break;
913         case '\'' : 
914                 out[j++] = 'Z';
915                 out[j++] = 'q';
916                 break;
917         case '*'  : 
918                 out[j++] = 'Z';
919                 out[j++] = 't';
920                 break;
921         case '_'  : 
922                 out[j++] = 'Z';
923                 out[j++] = 'u';
924                 break;
925         default :
926                 out[j++] = in[i];
927                 break;
928         }
929     }
930     out[j] = '\0';
931 }
932 #endif
933
934 const char *lookupGHCName( void *addr )
935 {
936     nat i;
937     for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
938     }
939     if (i < table_size) {
940         return table[i].name;
941     } else {
942         return NULL;
943     }
944 }
945
946 static void printZcoded( const char *raw )
947 {
948     nat j = 0;
949     
950     while ( raw[j] != '\0' ) {
951         if (raw[j] == 'Z') {
952             debugBelch("%c", unZcode(raw[j+1]));
953             j = j + 2;
954         } else {
955             debugBelch("%c", unZcode(raw[j+1]));
956             j = j + 1;
957         }
958     }
959 }
960
961 /* --------------------------------------------------------------------------
962  * Symbol table loading
963  * ------------------------------------------------------------------------*/
964
965 /* Causing linking trouble on Win32 plats, so I'm
966    disabling this for now. 
967 */
968 #ifdef USING_LIBBFD
969
970 #include <bfd.h>
971
972 /* Fairly ad-hoc piece of code that seems to filter out a lot of
973  * rubbish like the obj-splitting symbols
974  */
975
976 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
977 {
978 #if 0
979     /* ToDo: make this work on BFD */
980     int tp = type & N_TYPE;    
981     if (tp == N_TEXT || tp == N_DATA) {
982         return (name[0] == '_' && name[1] != '_');
983     } else {
984         return rtsFalse;
985     }
986 #else
987     if (*name == '\0'  || 
988         (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
989         (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
990         return rtsFalse;
991     }
992     return rtsTrue;
993 #endif
994 }
995
996 extern void DEBUG_LoadSymbols( char *name )
997 {
998     bfd* abfd;
999     char **matching;
1000
1001     bfd_init();
1002     abfd = bfd_openr(name, "default");
1003     if (abfd == NULL) {
1004         barf("can't open executable %s to get symbol table", name);
1005     }
1006     if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1007         barf("mismatch");
1008     }
1009
1010     {
1011         long storage_needed;
1012         asymbol **symbol_table;
1013         long number_of_symbols;
1014         long num_real_syms = 0;
1015         long i;
1016      
1017         storage_needed = bfd_get_symtab_upper_bound (abfd);
1018      
1019         if (storage_needed < 0) {
1020             barf("can't read symbol table");
1021         }     
1022 #if 0
1023         if (storage_needed == 0) {
1024             debugBelch("no storage needed");
1025         }
1026 #endif
1027         symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1028
1029         number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1030      
1031         if (number_of_symbols < 0) {
1032             barf("can't canonicalise symbol table");
1033         }
1034
1035         for( i = 0; i != number_of_symbols; ++i ) {
1036             symbol_info info;
1037             bfd_get_symbol_info(abfd,symbol_table[i],&info);
1038             /*debugBelch("\t%c\t0x%x      \t%s\n",info.type,(nat)info.value,info.name); */
1039             if (isReal(info.type, info.name)) {
1040                 num_real_syms += 1;
1041             }
1042         }
1043     
1044         IF_DEBUG(interpreter,
1045                  debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n", 
1046                          number_of_symbols, num_real_syms)
1047                  );
1048
1049         reset_table( num_real_syms );
1050     
1051         for( i = 0; i != number_of_symbols; ++i ) {
1052             symbol_info info;
1053             bfd_get_symbol_info(abfd,symbol_table[i],&info);
1054             if (isReal(info.type, info.name)) {
1055                 insert( info.value, info.name );
1056             }
1057         }
1058
1059         stgFree(symbol_table);
1060     }
1061     prepare_table();
1062 }
1063
1064 #else /* HAVE_BFD_H */
1065
1066 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1067 {
1068   /* nothing, yet */
1069 }
1070
1071 #endif /* HAVE_BFD_H */
1072
1073 void findPtr(P_ p, int);                /* keep gcc -Wall happy */
1074
1075 void
1076 findPtr(P_ p, int follow)
1077 {
1078   nat s, g;
1079   P_ q, r;
1080   bdescr *bd;
1081 #if defined(__GNUC__)
1082   const int arr_size = 1024;
1083 #else
1084 #define arr_size 1024
1085 #endif
1086   StgPtr arr[arr_size];
1087   int i = 0;
1088
1089   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1090       for (s = 0; s < generations[g].n_steps; s++) {
1091           bd = generations[g].steps[s].blocks;
1092           for (; bd; bd = bd->link) {
1093               for (q = bd->start; q < bd->free; q++) {
1094                   if (*q == (W_)p) {
1095                       if (i < arr_size) {
1096                           r = q;
1097                           while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1098                               r--;
1099                           }
1100                           debugBelch("%p = ", r);
1101                           printClosure((StgClosure *)r);
1102                           arr[i++] = r;
1103                       } else {
1104                           return;
1105                       }
1106                   }
1107               }
1108           }
1109       }
1110   }
1111   if (follow && i == 1) {
1112       debugBelch("-->\n");
1113       findPtr(arr[0], 1);
1114   }
1115 }
1116
1117 #else /* DEBUG */
1118 void printPtr( StgPtr p )
1119 {
1120     debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1121 }
1122   
1123 void printObj( StgClosure *obj )
1124 {
1125     debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
1126 }
1127 #endif /* DEBUG */