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