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