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