Detab TcUnify
[ghc-hetmet.git] / rts / Printer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1994-2000.
4  *
5  * Heap printer
6  * 
7  * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "Printer.h"
12 #include "RtsUtils.h"
13
14 #ifdef DEBUG
15
16 #include "RtsFlags.h"
17 #include "MBlock.h"
18 #include "Bytecodes.h"  /* for InstrPtr */
19 #include "Disassembler.h"
20 #include "Apply.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if defined(GRAN) || defined(PAR)
26 // HWL: explicit fixed header size to make debugging easier
27 int fixed_hs = sizeof(StgHeader), itbl_sz = sizeofW(StgInfoTable), 
28     uf_sz=sizeofW(StgUpdateFrame); 
29 #endif
30
31 /* --------------------------------------------------------------------------
32  * local function decls
33  * ------------------------------------------------------------------------*/
34
35 static void    printStdObjPayload( StgClosure *obj );
36 #ifdef USING_LIBBFD
37 static void    reset_table   ( int size );
38 static void    prepare_table ( void );
39 static void    insert        ( StgWord value, const char *name );
40 #endif
41 #if 0 /* unused but might be useful sometime */
42 static rtsBool lookup_name   ( char *name, StgWord *result );
43 static void    enZcode       ( char *in, char *out );
44 #endif
45 static char    unZcode       ( char ch );
46 const char *   lookupGHCName ( void *addr );
47 static void    printZcoded   ( const char *raw );
48
49 /* --------------------------------------------------------------------------
50  * Printer
51  * ------------------------------------------------------------------------*/
52
53 void printPtr( StgPtr p )
54 {
55     const char *raw;
56     raw = lookupGHCName(p);
57     if (raw != NULL) {
58         printZcoded(raw);
59     } else {
60         debugBelch("%p", p);
61     }
62 }
63   
64 void printObj( StgClosure *obj )
65 {
66     debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
67     printClosure(obj);
68 }
69
70 STATIC_INLINE void
71 printStdObjHdr( StgClosure *obj, char* tag )
72 {
73     debugBelch("%s(",tag);
74     printPtr((StgPtr)obj->header.info);
75 #ifdef PROFILING
76     debugBelch(", %s", obj->header.prof.ccs->cc->label);
77 #endif
78 }
79
80 static void
81 printStdObjPayload( StgClosure *obj )
82 {
83     StgWord i, j;
84     const StgInfoTable* info;
85
86     info = get_itbl(obj);
87     for (i = 0; i < info->layout.payload.ptrs; ++i) {
88         debugBelch(", ");
89         printPtr((StgPtr)obj->payload[i]);
90     }
91     for (j = 0; j < info->layout.payload.nptrs; ++j) {
92         debugBelch(", %pd#",obj->payload[i+j]);
93     }
94     debugBelch(")\n");
95 }
96
97 static void
98 printThunkPayload( StgThunk *obj )
99 {
100     StgWord i, j;
101     const StgInfoTable* info;
102
103     info = get_itbl(obj);
104     for (i = 0; i < info->layout.payload.ptrs; ++i) {
105         debugBelch(", ");
106         printPtr((StgPtr)obj->payload[i]);
107     }
108     for (j = 0; j < info->layout.payload.nptrs; ++j) {
109         debugBelch(", %pd#",obj->payload[i+j]);
110     }
111     debugBelch(")\n");
112 }
113
114 static void
115 printThunkObject( StgThunk *obj, char* tag )
116 {
117     printStdObjHdr( (StgClosure *)obj, tag );
118     printThunkPayload( obj );
119 }
120
121 void
122 printClosure( StgClosure *obj )
123 {
124     StgInfoTable *info;
125     
126     info = get_itbl(obj);
127
128     switch ( info->type ) {
129     case INVALID_OBJECT:
130             barf("Invalid object");
131
132     case CONSTR:
133     case CONSTR_1_0: case CONSTR_0_1:
134     case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
135     case CONSTR_STATIC:
136     case CONSTR_NOCAF_STATIC:
137         {
138             StgWord i, j;
139
140 #ifdef PROFILING
141             debugBelch("%s(", GET_PROF_DESC(info));
142             debugBelch("%s", obj->header.prof.ccs->cc->label);
143 #else
144             debugBelch("CONSTR(");
145             printPtr((StgPtr)obj->header.info);
146             debugBelch("(tag=%d)",info->srt_bitmap);
147 #endif
148             for (i = 0; i < info->layout.payload.ptrs; ++i) {
149                 debugBelch(", ");
150                 printPtr((StgPtr)obj->payload[i]);
151             }
152             for (j = 0; j < info->layout.payload.nptrs; ++j) {
153                 debugBelch(", %p#", obj->payload[i+j]);
154             }
155             debugBelch(")\n");
156             break;
157         }
158
159     case FUN:
160     case FUN_1_0: case FUN_0_1: 
161     case FUN_1_1: case FUN_0_2: case FUN_2_0:
162     case FUN_STATIC:
163         debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
164         printPtr((StgPtr)obj->header.info);
165 #ifdef PROFILING
166         debugBelch(", %s", obj->header.prof.ccs->cc->label);
167 #endif
168         printStdObjPayload(obj);
169         break;
170
171     case THUNK:
172     case THUNK_1_0: case THUNK_0_1:
173     case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
174     case THUNK_STATIC:
175             /* ToDo: will this work for THUNK_STATIC too? */
176 #ifdef PROFILING
177             printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
178 #else
179             printThunkObject((StgThunk *)obj,"THUNK");
180 #endif
181             break;
182
183     case THUNK_SELECTOR:
184         printStdObjHdr(obj, "THUNK_SELECTOR");
185         debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
186         break;
187
188     case BCO:
189             disassemble( (StgBCO*)obj );
190             break;
191
192     case AP:
193         {
194             StgAP* ap = stgCast(StgAP*,obj);
195             StgWord i;
196             debugBelch("AP("); printPtr((StgPtr)ap->fun);
197             for (i = 0; i < ap->n_args; ++i) {
198                 debugBelch(", ");
199                 printPtr((P_)ap->payload[i]);
200             }
201             debugBelch(")\n");
202             break;
203         }
204
205     case PAP:
206         {
207             StgPAP* pap = stgCast(StgPAP*,obj);
208             StgWord i;
209             debugBelch("PAP/%d(",pap->arity); 
210             printPtr((StgPtr)pap->fun);
211             for (i = 0; i < pap->n_args; ++i) {
212                 debugBelch(", ");
213                 printPtr((StgPtr)pap->payload[i]);
214             }
215             debugBelch(")\n");
216             break;
217         }
218
219     case AP_STACK:
220         {
221             StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
222             StgWord i;
223             debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
224             for (i = 0; i < ap->size; ++i) {
225                 debugBelch(", ");
226                 printPtr((P_)ap->payload[i]);
227             }
228             debugBelch(")\n");
229             break;
230         }
231
232     case IND:
233             debugBelch("IND("); 
234             printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
235             debugBelch(")\n"); 
236             break;
237
238     case IND_OLDGEN:
239             debugBelch("IND_OLDGEN("); 
240             printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
241             debugBelch(")\n"); 
242             break;
243
244     case IND_PERM:
245             debugBelch("IND("); 
246             printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
247             debugBelch(")\n"); 
248             break;
249
250     case IND_OLDGEN_PERM:
251             debugBelch("IND_OLDGEN_PERM("); 
252             printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
253             debugBelch(")\n"); 
254             break;
255
256     case IND_STATIC:
257             debugBelch("IND_STATIC("); 
258             printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
259             debugBelch(")\n"); 
260             break;
261
262     /* Cannot happen -- use default case.
263     case RET_BCO:
264     case RET_SMALL:
265     case RET_BIG:
266     case RET_DYN:
267     case RET_FUN:
268     */
269
270     case UPDATE_FRAME:
271         {
272             StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
273             debugBelch("UPDATE_FRAME(");
274             printPtr((StgPtr)GET_INFO(u));
275             debugBelch(",");
276             printPtr((StgPtr)u->updatee);
277             debugBelch(")\n"); 
278             break;
279         }
280
281     case CATCH_FRAME:
282         {
283             StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
284             debugBelch("CATCH_FRAME(");
285             printPtr((StgPtr)GET_INFO(u));
286             debugBelch(",");
287             printPtr((StgPtr)u->handler);
288             debugBelch(")\n"); 
289             break;
290         }
291
292     case STOP_FRAME:
293         {
294             StgStopFrame* u = stgCast(StgStopFrame*,obj);
295             debugBelch("STOP_FRAME(");
296             printPtr((StgPtr)GET_INFO(u));
297             debugBelch(")\n"); 
298             break;
299         }
300
301     case CAF_BLACKHOLE:
302             debugBelch("CAF_BH"); 
303             break;
304
305     case BLACKHOLE:
306             debugBelch("BH\n"); 
307             break;
308
309     case SE_BLACKHOLE:
310             debugBelch("SE_BH\n"); 
311             break;
312
313     case SE_CAF_BLACKHOLE:
314             debugBelch("SE_CAF_BH\n"); 
315             break;
316
317     case ARR_WORDS:
318         {
319             StgWord i;
320             debugBelch("ARR_WORDS(\"");
321             /* ToDo: we can't safely assume that this is a string! 
322             for (i = 0; arrWordsGetChar(obj,i); ++i) {
323                 putchar(arrWordsGetChar(obj,i));
324                 } */
325             for (i=0; i<((StgArrWords *)obj)->words; i++)
326               debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
327             debugBelch("\")\n");
328             break;
329         }
330
331     case MUT_ARR_PTRS_CLEAN:
332         debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
333         break;
334
335     case MUT_ARR_PTRS_DIRTY:
336         debugBelch("MUT_ARR_PTRS_DIRTY(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_CLEAN:
344     case MVAR_DIRTY:
345         {
346           StgMVar* mv = (StgMVar*)obj;
347           debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
348           break;
349         }
350
351     case MUT_VAR_CLEAN:
352         {
353           StgMutVar* mv = (StgMutVar*)obj;
354           debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
355           break;
356         }
357
358     case MUT_VAR_DIRTY:
359         {
360           StgMutVar* mv = (StgMutVar*)obj;
361           debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
362           break;
363         }
364
365     case WEAK:
366             debugBelch("WEAK("); 
367             debugBelch(" key=%p value=%p finalizer=%p", 
368                     (StgPtr)(((StgWeak*)obj)->key),
369                     (StgPtr)(((StgWeak*)obj)->value),
370                     (StgPtr)(((StgWeak*)obj)->finalizer));
371             debugBelch(")\n"); 
372             /* ToDo: chase 'link' ? */
373             break;
374
375     case STABLE_NAME:
376             debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn); 
377             break;
378
379     case TSO:
380       debugBelch("TSO("); 
381       debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
382       debugBelch(")\n"); 
383       break;
384
385 #if defined(PAR)
386     case BLOCKED_FETCH:
387       debugBelch("BLOCKED_FETCH("); 
388       printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
389       printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
390       debugBelch(")\n"); 
391       break;
392
393     case FETCH_ME:
394       debugBelch("FETCH_ME("); 
395       printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
396       debugBelch(")\n"); 
397       break;
398
399     case FETCH_ME_BQ:
400       debugBelch("FETCH_ME_BQ("); 
401       // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
402       printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
403       debugBelch(")\n"); 
404       break;
405 #endif
406
407 #if defined(GRAN) || defined(PAR)
408     case RBH:
409       debugBelch("RBH("); 
410       printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
411       debugBelch(")\n"); 
412       break;
413
414 #endif
415
416 #if 0
417       /* Symptomatic of a problem elsewhere, have it fall-through & fail */
418     case EVACUATED:
419       debugBelch("EVACUATED("); 
420       printClosure((StgEvacuated*)obj->evacuee);
421       debugBelch(")\n"); 
422       break;
423 #endif
424
425 #if defined(PAR) && defined(DIST)
426     case REMOTE_REF:
427       debugBelch("REMOTE_REF("); 
428       printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
429       debugBelch(")\n"); 
430       break;
431 #endif
432
433     default:
434             //barf("printClosure %d",get_itbl(obj)->type);
435             debugBelch("*** printClosure: unknown type %d ****\n",
436                     get_itbl(obj)->type );
437             barf("printClosure %d",get_itbl(obj)->type);
438             return;
439     }
440 }
441
442 /*
443 void printGraph( StgClosure *obj )
444 {
445  printClosure(obj);
446 }
447 */
448
449 StgPtr
450 printStackObj( StgPtr sp )
451 {
452     /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
453
454         StgClosure* c = (StgClosure*)(*sp);
455         printPtr((StgPtr)*sp);
456         if (c == (StgClosure*)&stg_ctoi_R1p_info) {
457            debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
458         } else
459         if (c == (StgClosure*)&stg_ctoi_R1n_info) {
460            debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
461         } else
462         if (c == (StgClosure*)&stg_ctoi_F1_info) {
463            debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
464         } else
465         if (c == (StgClosure*)&stg_ctoi_D1_info) {
466            debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
467         } else
468         if (c == (StgClosure*)&stg_ctoi_V_info) {
469            debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
470         } else
471         if (get_itbl(c)->type == BCO) {
472            debugBelch("\t\t\t");
473            debugBelch("BCO(...)\n"); 
474         }
475         else {
476            debugBelch("\t\t\t");
477            printClosure ( (StgClosure*)(*sp));
478         }
479         sp += 1;
480
481     return sp;
482     
483 }
484
485 static void
486 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
487 {
488     StgPtr p;
489     nat i;
490
491     p = payload;
492     for(i = 0; i < size; i++, bitmap >>= 1 ) {
493         debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
494         if ((bitmap & 1) == 0) {
495             printPtr((P_)payload[i]);
496             debugBelch("\n");
497         } else {
498             debugBelch("Word# %lu\n", (lnat)payload[i]);
499         }
500     }
501 }
502
503 static void
504 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
505 {
506     StgWord bmp;
507     nat i, j;
508
509     i = 0;
510     for (bmp=0; i < size; bmp++) {
511         StgWord bitmap = large_bitmap->bitmap[bmp];
512         j = 0;
513         for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
514             debugBelch("   stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
515             if ((bitmap & 1) == 0) {
516                 printPtr((P_)payload[i]);
517                 debugBelch("\n");
518             } else {
519                 debugBelch("Word# %lu\n", (lnat)payload[i]);
520             }
521         }
522     }
523 }
524
525 void
526 printStackChunk( StgPtr sp, StgPtr spBottom )
527 {
528     StgWord bitmap;
529     const StgInfoTable *info;
530
531     ASSERT(sp <= spBottom);
532     for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
533
534         info = get_itbl((StgClosure *)sp);
535
536         switch (info->type) {
537             
538         case UPDATE_FRAME:
539         case CATCH_FRAME:
540             printObj((StgClosure*)sp);
541             continue;
542
543         case STOP_FRAME:
544             printObj((StgClosure*)sp);
545             return;
546
547         case RET_DYN:
548         { 
549             StgRetDyn* r;
550             StgPtr p;
551             StgWord dyn;
552             nat size;
553
554             r = (StgRetDyn *)sp;
555             dyn = r->liveness;
556             debugBelch("RET_DYN (%p)\n", r);
557
558             p = (P_)(r->payload);
559             printSmallBitmap(spBottom, sp,
560                              RET_DYN_LIVENESS(r->liveness), 
561                              RET_DYN_BITMAP_SIZE);
562             p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
563
564             for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
565                 debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-p), p);
566                 debugBelch("Word# %ld\n", (long)*p);
567                 p++;
568             }
569         
570             for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
571                 debugBelch("   stk[%ld] (%p) = ", (long)(spBottom-p), p);
572                 printPtr(p);
573                 p++;
574             }
575             continue;
576         }
577
578         case RET_SMALL:
579             debugBelch("RET_SMALL (%p)\n", info);
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             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, fun_info->f.fun_type);
609             switch (fun_info->f.fun_type) {
610             case ARG_GEN:
611                 printSmallBitmap(spBottom, sp+2,
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+2,
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_STATIC",
656     "CONSTR_NOCAF_STATIC",
657     "FUN",
658     "FUN_1_0",
659     "FUN_0_1",
660     "FUN_2_0",
661     "FUN_1_1",
662     "FUN_0",
663     "FUN_STATIC",
664     "THUNK",
665     "THUNK_1_0",
666     "THUNK_0_1",
667     "THUNK_2_0",
668     "THUNK_1_1",
669     "THUNK_0",
670     "THUNK_STATIC",
671     "THUNK_SELECTOR",
672     "BCO",
673     "AP_UPD",
674     "PAP",
675     "AP_STACK",
676     "IND",
677     "IND_OLDGEN",
678     "IND_PERM",
679     "IND_OLDGEN_PERM",
680     "IND_STATIC",
681     "RET_BCO",
682     "RET_SMALL",
683     "RET_BIG",
684     "RET_DYN",
685     "RET_FUN",
686     "UPDATE_FRAME",
687     "CATCH_FRAME",
688     "STOP_FRAME",
689     "CAF_BLACKHOLE",
690     "BLACKHOLE",
691     "BLACKHOLE_BQ",
692     "SE_BLACKHOLE",
693     "SE_CAF_BLACKHOLE",
694     "MVAR",
695     "ARR_WORDS",
696     "MUT_ARR_PTRS_CLEAN",
697     "MUT_ARR_PTRS_DIRTY",
698     "MUT_ARR_PTRS_FROZEN",
699     "MUT_VAR_CLEAN",
700     "MUT_VAR_DIRTY",
701     "MUT_CONS",
702     "WEAK",
703     "FOREIGN",
704     "STABLE_NAME",
705     "TSO",
706     "BLOCKED_FETCH",
707     "FETCH_ME",
708     "FETCH_ME_BQ",
709     "RBH",
710     "EVACUATED",
711     "REMOTE_REF",
712     "TVAR_WATCH_QUEUE",
713     "INVARIANT_CHECK_QUEUE",
714     "ATOMIC_INVARIANT",
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     StgWord 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( StgWord 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, StgWord *result )
786 {
787     nat 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 != (StgWord) 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 int searched = 0;
1069
1070 static int
1071 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
1072 {
1073     StgPtr q, r;
1074     for (; bd; bd = bd->link) {
1075         searched++;
1076         for (q = bd->start; q < bd->free; q++) {
1077             if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
1078                 if (i < arr_size) {
1079                     r = q;
1080                     while (HEAP_ALLOCED((StgPtr)*r) || !LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1081                         r--;
1082                     }
1083                     debugBelch("%p = ", r);
1084                     printClosure((StgClosure *)r);
1085                     arr[i++] = r;
1086                 } else {
1087                     return i;
1088                 }
1089             }
1090         }
1091     }
1092     return i;
1093 }
1094
1095 void
1096 findPtr(P_ p, int follow)
1097 {
1098   nat s, g;
1099   bdescr *bd;
1100 #if defined(__GNUC__)
1101   const int arr_size = 1024;
1102 #else
1103 #define arr_size 1024
1104 #endif
1105   StgPtr arr[arr_size];
1106   int i = 0;
1107   searched = 0;
1108
1109   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1110       for (s = 0; s < generations[g].n_steps; s++) {
1111           bd = generations[g].steps[s].blocks;
1112           i = findPtrBlocks(p,bd,arr,arr_size,i);
1113           bd = generations[g].steps[s].large_objects;
1114           i = findPtrBlocks(p,bd,arr,arr_size,i);
1115           if (i >= arr_size) return;
1116       }
1117   }
1118   if (follow && i == 1) {
1119       debugBelch("-->\n");
1120       findPtr(arr[0], 1);
1121   }
1122 }
1123
1124 /* prettyPrintClosure() is for printing out a closure using the data constructor
1125    names found in the info tables. Closures are printed in a fashion that resembles
1126    their Haskell representation. Useful during debugging.
1127
1128    Todo: support for more closure types, and support for non pointer fields in the
1129    payload.
1130 */ 
1131
1132 void prettyPrintClosure_ (StgClosure *);
1133
1134 void prettyPrintClosure (StgClosure *obj)
1135 {
1136    prettyPrintClosure_ (obj);
1137    debugBelch ("\n");
1138 }
1139
1140 void prettyPrintClosure_ (StgClosure *obj)
1141 {
1142     StgInfoTable *info;
1143     StgConInfoTable *con_info;
1144
1145     /* collapse any indirections */
1146     unsigned int type;
1147     type = get_itbl(obj)->type;
1148            
1149     while (type == IND ||
1150            type == IND_STATIC ||
1151            type == IND_OLDGEN ||
1152            type == IND_PERM ||
1153            type == IND_OLDGEN_PERM) 
1154     {
1155       obj = ((StgInd *)obj)->indirectee;
1156       type = get_itbl(obj)->type;
1157     }
1158
1159     /* find the info table for this object */
1160     info = get_itbl(obj);
1161
1162     /* determine what kind of object we have */
1163     switch (info->type) 
1164     {
1165         /* full applications of data constructors */
1166         case CONSTR:
1167         case CONSTR_1_0: 
1168         case CONSTR_0_1:
1169         case CONSTR_1_1: 
1170         case CONSTR_0_2: 
1171         case CONSTR_2_0:
1172         case CONSTR_STATIC:
1173         case CONSTR_NOCAF_STATIC: 
1174         {
1175            nat i; 
1176            char *descriptor;
1177
1178            /* find the con_info for the constructor */
1179            con_info = get_con_itbl (obj);
1180
1181            /* obtain the name of the constructor */
1182            descriptor = GET_CON_DESC(con_info);
1183
1184            debugBelch ("(%s", descriptor);
1185
1186            /* process the payload of the closure */
1187            /* we don't handle non pointers at the moment */
1188            for (i = 0; i < info->layout.payload.ptrs; i++)
1189            {
1190               debugBelch (" ");
1191               prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1192            }
1193            debugBelch (")");
1194            break;
1195         }
1196
1197         /* if it isn't a constructor then just print the closure type */
1198         default:
1199         {
1200            debugBelch ("<%s>", info_type(obj));
1201            break;
1202         }
1203     }
1204 }
1205
1206 #else /* DEBUG */
1207 void printPtr( StgPtr p )
1208 {
1209     debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1210 }
1211   
1212 void printObj( StgClosure *obj )
1213 {
1214     debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
1215 }
1216
1217
1218 #endif /* DEBUG */