[project @ 2006-01-17 16:13:18 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_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         case STOP_FRAME:
544             printObj((StgClosure*)sp);
545             continue;
546
547         case RET_DYN:
548         { 
549             StgRetDyn* r;
550             StgPtr p;
551             StgWord dyn;
552             nat size;
553
554             r = (StgRetDyn *)sp;
555             dyn = r->liveness;
556             debugBelch("RET_DYN (%p)\n", r);
557
558             p = (P_)(r->payload);
559             printSmallBitmap(spBottom, sp,
560                              RET_DYN_LIVENESS(r->liveness), 
561                              RET_DYN_BITMAP_SIZE);
562             p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
563
564             for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
565                 debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-p), p);
566                 debugBelch("Word# %ld\n", (long)*p);
567                 p++;
568             }
569         
570             for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
571                 debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-p), p);
572                 printPtr(p);
573                 p++;
574             }
575             continue;
576         }
577
578         case RET_SMALL:
579         case RET_VEC_SMALL:
580             debugBelch("RET_SMALL (%p)\n", sp);
581             bitmap = info->layout.bitmap;
582             printSmallBitmap(spBottom, sp+1, 
583                              BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
584             continue;
585
586         case RET_BCO: {
587             StgBCO *bco;
588             
589             bco = ((StgBCO *)sp[1]);
590
591             debugBelch("RET_BCO (%p)\n", sp);
592             printLargeBitmap(spBottom, sp+2,
593                              BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
594             continue;
595         }
596
597         case RET_BIG:
598         case RET_VEC_BIG:
599             barf("todo");
600
601         case RET_FUN:
602         {
603             StgFunInfoTable *fun_info;
604             StgRetFun *ret_fun;
605             nat size;
606
607             ret_fun = (StgRetFun *)sp;
608             fun_info = get_fun_itbl(ret_fun->fun);
609             size = ret_fun->size;
610             debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
611             switch (fun_info->f.fun_type) {
612             case ARG_GEN:
613                 printSmallBitmap(spBottom, sp+1,
614                                  BITMAP_BITS(fun_info->f.b.bitmap),
615                                  BITMAP_SIZE(fun_info->f.b.bitmap));
616                 break;
617             case ARG_GEN_BIG:
618                 printLargeBitmap(spBottom, sp+2,
619                                  GET_FUN_LARGE_BITMAP(fun_info),
620                                  GET_FUN_LARGE_BITMAP(fun_info)->size);
621                 break;
622             default:
623                 printSmallBitmap(spBottom, sp+1,
624                                  BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
625                                  BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
626                 break;
627             }
628             continue;
629         }
630            
631         default:
632             debugBelch("unknown object %d\n", info->type);
633             barf("printStackChunk");
634         }
635     }
636 }
637
638 void printTSO( StgTSO *tso )
639 {
640     printStackChunk( tso->sp, tso->stack+tso->stack_size);
641 }
642
643 /* -----------------------------------------------------------------------------
644    Closure types
645    
646    NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
647    -------------------------------------------------------------------------- */
648
649 static char *closure_type_names[] = {
650     "INVALID_OBJECT",
651     "CONSTR",
652     "CONSTR_1",
653     "CONSTR_0",
654     "CONSTR_2",
655     "CONSTR_1",
656     "CONSTR_0",
657     "CONSTR_INTLIKE",
658     "CONSTR_CHARLIKE",
659     "CONSTR_STATIC",
660     "CONSTR_NOCAF_STATIC",
661     "FUN",
662     "FUN_1_0",
663     "FUN_0_1",
664     "FUN_2_0",
665     "FUN_1_1",
666     "FUN_0",
667     "FUN_STATIC",
668     "THUNK",
669     "THUNK_1_0",
670     "THUNK_0_1",
671     "THUNK_2_0",
672     "THUNK_1_1",
673     "THUNK_0",
674     "THUNK_STATIC",
675     "THUNK_SELECTOR",
676     "BCO",
677     "AP_UPD",
678     "PAP",
679     "AP_STACK",
680     "IND",
681     "IND_OLDGEN",
682     "IND_PERM",
683     "IND_OLDGEN_PERM",
684     "IND_STATIC",
685     "RET_BCO",
686     "RET_SMALL",
687     "RET_VEC_SMALL",
688     "RET_BIG",
689     "RET_VEC_BIG",
690     "RET_DYN",
691     "RET_FUN",
692     "UPDATE_FRAME",
693     "CATCH_FRAME",
694     "STOP_FRAME",
695     "CAF_BLACKHOLE",
696     "BLACKHOLE",
697     "BLACKHOLE_BQ",
698     "SE_BLACKHOLE",
699     "SE_CAF_BLACKHOLE",
700     "MVAR",
701     "ARR_WORDS",
702     "MUT_ARR_PTRS_CLEAN",
703     "MUT_ARR_PTRS_DIRTY",
704     "MUT_ARR_PTRS_FROZEN",
705     "MUT_VAR_CLEAN",
706     "MUT_VAR_DIRTY",
707     "MUT_CONS",
708     "WEAK",
709     "FOREIGN",
710     "STABLE_NAME",
711     "TSO",
712     "BLOCKED_FETCH",
713     "FETCH_ME",
714     "FETCH_ME_BQ",
715     "RBH",
716     "EVACUATED",
717     "REMOTE_REF",
718     "TVAR_WAIT_QUEUE",
719     "TVAR",
720     "TREC_CHUNK",
721     "TREC_HEADER",
722     "ATOMICALLY_FRAME",
723     "CATCH_RETRY_FRAME"
724 };
725
726
727 char *
728 info_type(StgClosure *closure){ 
729   return closure_type_names[get_itbl(closure)->type];
730 }
731
732 char *
733 info_type_by_ip(StgInfoTable *ip){ 
734   return closure_type_names[ip->type];
735 }
736
737 void
738 info_hdr_type(StgClosure *closure, char *res){ 
739   strcpy(res,closure_type_names[get_itbl(closure)->type]);
740 }
741
742 /* --------------------------------------------------------------------------
743  * Address printing code
744  *
745  * Uses symbol table in (unstripped executable)
746  * ------------------------------------------------------------------------*/
747
748 /* --------------------------------------------------------------------------
749  * Simple lookup table
750  *
751  * Current implementation is pretty dumb!
752  * ------------------------------------------------------------------------*/
753
754 struct entry {
755     nat value;
756     const char *name;
757 };
758
759 static nat table_size;
760 static struct entry* table;
761
762 #ifdef USING_LIBBFD
763 static nat max_table_size;
764
765 static void reset_table( int size )
766 {
767     max_table_size = size;
768     table_size = 0;
769     table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
770 }
771
772 static void prepare_table( void )
773 {
774     /* Could sort it...  */
775 }
776
777 static void insert( unsigned value, const char *name )
778 {
779     if ( table_size >= max_table_size ) {
780         barf( "Symbol table overflow\n" );
781     }
782     table[table_size].value = value;
783     table[table_size].name = name;
784     table_size = table_size + 1;
785 }
786 #endif
787
788 #if 0
789 static rtsBool lookup_name( char *name, unsigned *result )
790 {
791     int i;
792     for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
793     }
794     if (i < table_size) {
795         *result = table[i].value;
796         return rtsTrue;
797     } else {
798         return rtsFalse;
799     }
800 }
801 #endif
802
803 /* Code from somewhere inside GHC (circa 1994)
804  * * Z-escapes:
805  *     "std"++xs -> "Zstd"++xs
806  *     char_to_c 'Z'  = "ZZ"
807  *     char_to_c '&'  = "Za"
808  *     char_to_c '|'  = "Zb"
809  *     char_to_c ':'  = "Zc"
810  *     char_to_c '/'  = "Zd"
811  *     char_to_c '='  = "Ze"
812  *     char_to_c '>'  = "Zg"
813  *     char_to_c '#'  = "Zh"
814  *     char_to_c '<'  = "Zl"
815  *     char_to_c '-'  = "Zm"
816  *     char_to_c '!'  = "Zn"
817  *     char_to_c '.'  = "Zo"
818  *     char_to_c '+'  = "Zp"
819  *     char_to_c '\'' = "Zq"
820  *     char_to_c '*'  = "Zt"
821  *     char_to_c '_'  = "Zu"
822  *     char_to_c c    = "Z" ++ show (ord c)
823  */
824 static char unZcode( char ch )
825 {
826     switch (ch) {
827     case 'a'  : return ('&');
828     case 'b'  : return ('|');
829     case 'c'  : return (':');
830     case 'd'  : return ('/');
831     case 'e'  : return ('=');
832     case 'g'  : return ('>');
833     case 'h'  : return ('#');
834     case 'l'  : return ('<');
835     case 'm'  : return ('-');
836     case 'n'  : return ('!');
837     case 'o'  : return ('.');
838     case 'p'  : return ('+');
839     case 'q'  : return ('\'');
840     case 't'  : return ('*');
841     case 'u'  : return ('_');
842     case 'Z'  :
843     case '\0' : return ('Z');
844     default   : return (ch);
845     }
846 }
847
848 #if 0
849 /* Precondition: out big enough to handle output (about twice length of in) */
850 static void enZcode( char *in, char *out )
851 {
852     int i, j;
853
854     j = 0;
855     out[ j++ ] = '_';
856     for( i = 0; in[i] != '\0'; ++i ) {
857         switch (in[i]) {
858         case 'Z'  : 
859                 out[j++] = 'Z';
860                 out[j++] = 'Z';
861                 break;
862         case '&'  : 
863                 out[j++] = 'Z';
864                 out[j++] = 'a';
865                 break;
866         case '|'  : 
867                 out[j++] = 'Z';
868                 out[j++] = 'b';
869                 break;
870         case ':'  : 
871                 out[j++] = 'Z';
872                 out[j++] = 'c';
873                 break;
874         case '/'  : 
875                 out[j++] = 'Z';
876                 out[j++] = 'd';
877                 break;
878         case '='  : 
879                 out[j++] = 'Z';
880                 out[j++] = 'e';
881                 break;
882         case '>'  : 
883                 out[j++] = 'Z';
884                 out[j++] = 'g';
885                 break;
886         case '#'  : 
887                 out[j++] = 'Z';
888                 out[j++] = 'h';
889                 break;
890         case '<'  : 
891                 out[j++] = 'Z';
892                 out[j++] = 'l';
893                 break;
894         case '-'  : 
895                 out[j++] = 'Z';
896                 out[j++] = 'm';
897                 break;
898         case '!'  : 
899                 out[j++] = 'Z';
900                 out[j++] = 'n';
901                 break;
902         case '.'  : 
903                 out[j++] = 'Z';
904                 out[j++] = 'o';
905                 break;
906         case '+'  : 
907                 out[j++] = 'Z';
908                 out[j++] = 'p';
909                 break;
910         case '\'' : 
911                 out[j++] = 'Z';
912                 out[j++] = 'q';
913                 break;
914         case '*'  : 
915                 out[j++] = 'Z';
916                 out[j++] = 't';
917                 break;
918         case '_'  : 
919                 out[j++] = 'Z';
920                 out[j++] = 'u';
921                 break;
922         default :
923                 out[j++] = in[i];
924                 break;
925         }
926     }
927     out[j] = '\0';
928 }
929 #endif
930
931 const char *lookupGHCName( void *addr )
932 {
933     nat i;
934     for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
935     }
936     if (i < table_size) {
937         return table[i].name;
938     } else {
939         return NULL;
940     }
941 }
942
943 static void printZcoded( const char *raw )
944 {
945     nat j = 0;
946     
947     while ( raw[j] != '\0' ) {
948         if (raw[j] == 'Z') {
949             debugBelch("%c", unZcode(raw[j+1]));
950             j = j + 2;
951         } else {
952             debugBelch("%c", unZcode(raw[j+1]));
953             j = j + 1;
954         }
955     }
956 }
957
958 /* --------------------------------------------------------------------------
959  * Symbol table loading
960  * ------------------------------------------------------------------------*/
961
962 /* Causing linking trouble on Win32 plats, so I'm
963    disabling this for now. 
964 */
965 #ifdef USING_LIBBFD
966
967 #include <bfd.h>
968
969 /* Fairly ad-hoc piece of code that seems to filter out a lot of
970  * rubbish like the obj-splitting symbols
971  */
972
973 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
974 {
975 #if 0
976     /* ToDo: make this work on BFD */
977     int tp = type & N_TYPE;    
978     if (tp == N_TEXT || tp == N_DATA) {
979         return (name[0] == '_' && name[1] != '_');
980     } else {
981         return rtsFalse;
982     }
983 #else
984     if (*name == '\0'  || 
985         (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
986         (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
987         return rtsFalse;
988     }
989     return rtsTrue;
990 #endif
991 }
992
993 extern void DEBUG_LoadSymbols( char *name )
994 {
995     bfd* abfd;
996     char **matching;
997
998     bfd_init();
999     abfd = bfd_openr(name, "default");
1000     if (abfd == NULL) {
1001         barf("can't open executable %s to get symbol table", name);
1002     }
1003     if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1004         barf("mismatch");
1005     }
1006
1007     {
1008         long storage_needed;
1009         asymbol **symbol_table;
1010         long number_of_symbols;
1011         long num_real_syms = 0;
1012         long i;
1013      
1014         storage_needed = bfd_get_symtab_upper_bound (abfd);
1015      
1016         if (storage_needed < 0) {
1017             barf("can't read symbol table");
1018         }     
1019 #if 0
1020         if (storage_needed == 0) {
1021             debugBelch("no storage needed");
1022         }
1023 #endif
1024         symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1025
1026         number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1027      
1028         if (number_of_symbols < 0) {
1029             barf("can't canonicalise symbol table");
1030         }
1031
1032         for( i = 0; i != number_of_symbols; ++i ) {
1033             symbol_info info;
1034             bfd_get_symbol_info(abfd,symbol_table[i],&info);
1035             /*debugBelch("\t%c\t0x%x      \t%s\n",info.type,(nat)info.value,info.name); */
1036             if (isReal(info.type, info.name)) {
1037                 num_real_syms += 1;
1038             }
1039         }
1040     
1041         IF_DEBUG(interpreter,
1042                  debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n", 
1043                          number_of_symbols, num_real_syms)
1044                  );
1045
1046         reset_table( num_real_syms );
1047     
1048         for( i = 0; i != number_of_symbols; ++i ) {
1049             symbol_info info;
1050             bfd_get_symbol_info(abfd,symbol_table[i],&info);
1051             if (isReal(info.type, info.name)) {
1052                 insert( info.value, info.name );
1053             }
1054         }
1055
1056         stgFree(symbol_table);
1057     }
1058     prepare_table();
1059 }
1060
1061 #else /* HAVE_BFD_H */
1062
1063 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1064 {
1065   /* nothing, yet */
1066 }
1067
1068 #endif /* HAVE_BFD_H */
1069
1070 void findPtr(P_ p, int);                /* keep gcc -Wall happy */
1071
1072 void
1073 findPtr(P_ p, int follow)
1074 {
1075   nat s, g;
1076   P_ q, r;
1077   bdescr *bd;
1078 #if defined(__GNUC__)
1079   const int arr_size = 1024;
1080 #else
1081 #define arr_size 1024
1082 #endif
1083   StgPtr arr[arr_size];
1084   int i = 0;
1085
1086   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1087       for (s = 0; s < generations[g].n_steps; s++) {
1088           bd = generations[g].steps[s].blocks;
1089           for (; bd; bd = bd->link) {
1090               for (q = bd->start; q < bd->free; q++) {
1091                   if (*q == (W_)p) {
1092                       if (i < arr_size) {
1093                           r = q;
1094                           while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1095                               r--;
1096                           }
1097                           debugBelch("%p = ", r);
1098                           printClosure((StgClosure *)r);
1099                           arr[i++] = r;
1100                       } else {
1101                           return;
1102                       }
1103                   }
1104               }
1105           }
1106       }
1107   }
1108   if (follow && i == 1) {
1109       debugBelch("-->\n");
1110       findPtr(arr[0], 1);
1111   }
1112 }
1113
1114 #else /* DEBUG */
1115 void printPtr( StgPtr p )
1116 {
1117     debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1118 }
1119   
1120 void printObj( StgClosure *obj )
1121 {
1122     debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
1123 }
1124 #endif /* DEBUG */