1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.63 2004/08/13 13:10:23 simonmar Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
22 #include "Bytecodes.h" /* for InstrPtr */
23 #include "Disassembler.h"
29 #if defined(GRAN) || defined(PAR)
30 // HWL: explicit fixed header size to make debugging easier
31 int fixed_hs = sizeof(StgHeader), itbl_sz = sizeofW(StgInfoTable),
32 uf_sz=sizeofW(StgUpdateFrame);
35 /* --------------------------------------------------------------------------
36 * local function decls
37 * ------------------------------------------------------------------------*/
39 static void printStdObject( StgClosure *obj, char* tag );
40 static void printStdObjPayload( StgClosure *obj );
42 static void reset_table ( int size );
43 static void prepare_table ( void );
44 static void insert ( unsigned value, const char *name );
46 #if 0 /* unused but might be useful sometime */
47 static rtsBool lookup_name ( char *name, unsigned *result );
48 static void enZcode ( char *in, char *out );
50 static char unZcode ( char ch );
51 const char * lookupGHCName ( void *addr );
52 static void printZcoded ( const char *raw );
54 /* --------------------------------------------------------------------------
56 * ------------------------------------------------------------------------*/
58 void printPtr( StgPtr p )
61 raw = lookupGHCName(p);
65 fprintf(stderr, "%p", p);
69 void printObj( StgClosure *obj )
71 fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
76 printStdObjHdr( StgClosure *obj, char* tag )
78 fprintf(stderr,"%s(",tag);
79 printPtr((StgPtr)obj->header.info);
81 fprintf(stderr,", %s", obj->header.prof.ccs->cc->label);
86 printStdObjPayload( StgClosure *obj )
89 const StgInfoTable* info;
92 for (i = 0; i < info->layout.payload.ptrs; ++i) {
94 printPtr((StgPtr)obj->payload[i]);
96 for (j = 0; j < info->layout.payload.nptrs; ++j) {
97 fprintf(stderr,", %pd#",obj->payload[i+j]);
99 fprintf(stderr,")\n");
103 printStdObject( StgClosure *obj, char* tag )
105 printStdObjHdr( obj, tag );
106 printStdObjPayload( obj );
110 printClosure( StgClosure *obj )
114 info = get_itbl(obj);
116 switch ( info->type ) {
118 barf("Invalid object");
121 case CONSTR_1_0: case CONSTR_0_1:
122 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
124 case CONSTR_CHARLIKE:
126 case CONSTR_NOCAF_STATIC:
128 /* We can't use printStdObject because we want to print the
133 fprintf(stderr,"%s(", info->prof.closure_desc);
134 fprintf(stderr,"%s", obj->header.prof.ccs->cc->label);
136 fprintf(stderr,"CONSTR(");
137 printPtr((StgPtr)obj->header.info);
138 fprintf(stderr,"(tag=%d)",info->srt_bitmap);
140 for (i = 0; i < info->layout.payload.ptrs; ++i) {
141 fprintf(stderr,", ");
142 printPtr((StgPtr)obj->payload[i]);
144 for (j = 0; j < info->layout.payload.nptrs; ++j) {
145 fprintf(stderr,", %p#", obj->payload[i+j]);
147 fprintf(stderr,")\n");
152 case FUN_1_0: case FUN_0_1:
153 case FUN_1_1: case FUN_0_2: case FUN_2_0:
155 fprintf(stderr,"FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
156 printPtr((StgPtr)obj->header.info);
158 fprintf(stderr,", %s", obj->header.prof.ccs->cc->label);
160 printStdObjPayload(obj);
164 case THUNK_1_0: case THUNK_0_1:
165 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
167 /* ToDo: will this work for THUNK_STATIC too? */
169 printStdObject(obj,info->prof.closure_desc);
171 printStdObject(obj,"THUNK");
176 printStdObjHdr(obj, "THUNK_SELECTOR");
177 fprintf(stderr, ", %p)\n", ((StgSelector *)obj)->selectee);
181 disassemble( (StgBCO*)obj );
186 StgPAP* ap = stgCast(StgPAP*,obj);
188 fprintf(stderr,"AP("); printPtr((StgPtr)ap->fun);
189 for (i = 0; i < ap->n_args; ++i) {
190 fprintf(stderr,", ");
191 printPtr((P_)ap->payload[i]);
193 fprintf(stderr,")\n");
199 StgPAP* pap = stgCast(StgPAP*,obj);
201 fprintf(stderr,"PAP/%d(",pap->arity);
202 printPtr((StgPtr)pap->fun);
203 for (i = 0; i < pap->n_args; ++i) {
204 fprintf(stderr,", ");
205 printPtr((StgPtr)pap->payload[i]);
207 fprintf(stderr,")\n");
213 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
215 fprintf(stderr,"AP_STACK("); printPtr((StgPtr)ap->fun);
216 for (i = 0; i < ap->size; ++i) {
217 fprintf(stderr,", ");
218 printPtr((P_)ap->payload[i]);
220 fprintf(stderr,")\n");
225 fprintf(stderr,"IND(");
226 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
227 fprintf(stderr,")\n");
231 fprintf(stderr,"IND_OLDGEN(");
232 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
233 fprintf(stderr,")\n");
237 fprintf(stderr,"IND(");
238 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
239 fprintf(stderr,")\n");
242 case IND_OLDGEN_PERM:
243 fprintf(stderr,"IND_OLDGEN_PERM(");
244 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
245 fprintf(stderr,")\n");
249 fprintf(stderr,"IND_STATIC(");
250 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
251 fprintf(stderr,")\n");
254 /* Cannot happen -- use default case.
266 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
267 fprintf(stderr,"UPDATE_FRAME(");
268 printPtr((StgPtr)GET_INFO(u));
270 printPtr((StgPtr)u->updatee);
271 fprintf(stderr,")\n");
277 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
278 fprintf(stderr,"CATCH_FRAME(");
279 printPtr((StgPtr)GET_INFO(u));
281 printPtr((StgPtr)u->handler);
282 fprintf(stderr,")\n");
288 StgStopFrame* u = stgCast(StgStopFrame*,obj);
289 fprintf(stderr,"STOP_FRAME(");
290 printPtr((StgPtr)GET_INFO(u));
291 fprintf(stderr,")\n");
296 fprintf(stderr,"CAF_BH(");
297 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
298 fprintf(stderr,")\n");
302 fprintf(stderr,"BH\n");
306 fprintf(stderr,"BQ(");
307 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
308 fprintf(stderr,")\n");
312 fprintf(stderr,"SE_BH\n");
315 case SE_CAF_BLACKHOLE:
316 fprintf(stderr,"SE_CAF_BH\n");
322 fprintf(stderr,"ARR_WORDS(\"");
323 /* ToDo: we can't safely assume that this is a string!
324 for (i = 0; arrWordsGetChar(obj,i); ++i) {
325 putchar(arrWordsGetChar(obj,i));
327 for (i=0; i<((StgArrWords *)obj)->words; i++)
328 fprintf(stderr, "%u", ((StgArrWords *)obj)->payload[i]);
329 fprintf(stderr,"\")\n");
334 fprintf(stderr,"MUT_ARR_PTRS(size=%d)\n", ((StgMutArrPtrs *)obj)->ptrs);
337 case MUT_ARR_PTRS_FROZEN:
338 #if !defined(XMLAMBDA)
339 fprintf(stderr,"MUT_ARR_PTRS_FROZEN(size=%d)\n", ((StgMutArrPtrs *)obj)->ptrs);
343 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
345 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
347 fprintf(stderr,"Row<%i>(",p->ptrs);
348 for (i = 0; i < p->ptrs; ++i) {
349 if (i > 0) fprintf(stderr,", ");
350 printPtr((StgPtr)(p->payload[i]));
352 fprintf(stderr,")\n");
359 StgMutVar* mv = (StgMutVar*)obj;
360 fprintf(stderr,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
365 fprintf(stderr,"WEAK(");
366 fprintf(stderr," key=%p value=%p finalizer=%p",
367 (StgPtr)(((StgWeak*)obj)->key),
368 (StgPtr)(((StgWeak*)obj)->value),
369 (StgPtr)(((StgWeak*)obj)->finalizer));
370 fprintf(stderr,")\n");
371 /* ToDo: chase 'link' ? */
375 fprintf(stderr,"FOREIGN(");
376 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
377 fprintf(stderr,")\n");
381 fprintf(stderr,"STABLE_NAME(%d)\n", ((StgStableName*)obj)->sn);
385 fprintf(stderr,"TSO(");
386 fprintf(stderr,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
387 fprintf(stderr,")\n");
392 fprintf(stderr,"BLOCKED_FETCH(");
393 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
394 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
395 fprintf(stderr,")\n");
399 fprintf(stderr,"FETCH_ME(");
400 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
401 fprintf(stderr,")\n");
405 fprintf(stderr,"FETCH_ME_BQ(");
406 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
407 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
408 fprintf(stderr,")\n");
412 #if defined(GRAN) || defined(PAR)
414 fprintf(stderr,"RBH(");
415 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
416 fprintf(stderr,")\n");
422 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
424 fprintf(stderr,"EVACUATED(");
425 printClosure((StgEvacuated*)obj->evacuee);
426 fprintf(stderr,")\n");
430 #if defined(PAR) && defined(DIST)
432 fprintf(stderr,"REMOTE_REF(");
433 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
434 fprintf(stderr,")\n");
439 //barf("printClosure %d",get_itbl(obj)->type);
440 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
441 get_itbl(obj)->type );
442 barf("printClosure %d",get_itbl(obj)->type);
448 void printGraph( StgClosure *obj )
455 printStackObj( StgPtr sp )
457 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
459 StgClosure* c = (StgClosure*)(*sp);
460 printPtr((StgPtr)*sp);
461 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
462 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
464 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
465 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
467 if (c == (StgClosure*)&stg_ctoi_F1_info) {
468 fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
470 if (c == (StgClosure*)&stg_ctoi_D1_info) {
471 fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
473 if (c == (StgClosure*)&stg_ctoi_V_info) {
474 fprintf(stderr, "\t\t\tstg_ctoi_ret_V_info\n" );
476 if (get_itbl(c)->type == BCO) {
477 fprintf(stderr, "\t\t\t");
478 fprintf(stderr, "BCO(...)\n");
481 fprintf(stderr, "\t\t\t");
482 printClosure ( (StgClosure*)(*sp));
491 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
497 for(i = 0; i < size; i++, bitmap >>= 1 ) {
498 fprintf(stderr," stk[%d] (%p) = ", spBottom-(payload+i), payload+i);
499 if ((bitmap & 1) == 0) {
500 printPtr((P_)payload[i]);
501 fprintf(stderr,"\n");
503 fprintf(stderr,"Word# %d\n", payload[i]);
509 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
515 for (bmp=0; i < size; bmp++) {
516 StgWord bitmap = large_bitmap->bitmap[bmp];
518 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
519 fprintf(stderr," stk[%d] (%p) = ", spBottom-(payload+i), payload+i);
520 if ((bitmap & 1) == 0) {
521 printPtr((P_)payload[i]);
522 fprintf(stderr,"\n");
524 fprintf(stderr,"Word# %d\n", payload[i]);
531 printStackChunk( StgPtr sp, StgPtr spBottom )
534 const StgInfoTable *info;
536 ASSERT(sp <= spBottom);
537 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
539 info = get_itbl((StgClosure *)sp);
541 switch (info->type) {
546 printObj((StgClosure*)sp);
558 fprintf(stderr, "RET_DYN (%p)\n", r);
560 p = (P_)(r->payload);
561 printSmallBitmap(spBottom, sp,
562 RET_DYN_LIVENESS(r->liveness),
563 RET_DYN_BITMAP_SIZE);
564 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
566 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
567 fprintf(stderr," stk[%ld] (%p) = ", (long)(spBottom-p), p);
568 fprintf(stderr,"Word# %ld\n", (long)*p);
572 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
573 fprintf(stderr," stk[%ld] (%p) = ", (long)(spBottom-p), p);
582 fprintf(stderr, "RET_SMALL (%p)\n", sp);
583 bitmap = info->layout.bitmap;
584 printSmallBitmap(spBottom, sp+1,
585 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
591 bco = ((StgBCO *)sp[1]);
593 fprintf(stderr, "RET_BCO (%p)\n", sp);
594 printLargeBitmap(spBottom, sp+2,
595 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
605 StgFunInfoTable *fun_info;
609 ret_fun = (StgRetFun *)sp;
610 fun_info = get_fun_itbl(ret_fun->fun);
611 size = ret_fun->size;
612 fprintf(stderr,"RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
613 switch (fun_info->f.fun_type) {
615 printSmallBitmap(spBottom, sp+1,
616 BITMAP_BITS(fun_info->f.bitmap),
617 BITMAP_SIZE(fun_info->f.bitmap));
620 printLargeBitmap(spBottom, sp+2,
621 (StgLargeBitmap *)fun_info->f.bitmap,
622 BITMAP_SIZE(fun_info->f.bitmap));
625 printSmallBitmap(spBottom, sp+1,
626 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
627 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
634 fprintf(stderr, "unknown object %d\n", info->type);
635 barf("printStackChunk");
640 void printTSO( StgTSO *tso )
642 printStackChunk( tso->sp, tso->stack+tso->stack_size);
645 /* -----------------------------------------------------------------------------
648 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
649 -------------------------------------------------------------------------- */
651 static char *closure_type_names[] = {
662 "CONSTR_NOCAF_STATIC",
705 "MUT_ARR_PTRS_FROZEN",
722 info_type(StgClosure *closure){
723 return closure_type_names[get_itbl(closure)->type];
727 info_type_by_ip(StgInfoTable *ip){
728 return closure_type_names[ip->type];
732 info_hdr_type(StgClosure *closure, char *res){
733 strcpy(res,closure_type_names[get_itbl(closure)->type]);
736 /* --------------------------------------------------------------------------
737 * Address printing code
739 * Uses symbol table in (unstripped executable)
740 * ------------------------------------------------------------------------*/
742 /* --------------------------------------------------------------------------
743 * Simple lookup table
745 * Current implementation is pretty dumb!
746 * ------------------------------------------------------------------------*/
753 static nat table_size;
754 static struct entry* table;
757 static nat max_table_size;
759 static void reset_table( int size )
761 max_table_size = size;
763 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
766 static void prepare_table( void )
768 /* Could sort it... */
771 static void insert( unsigned value, const char *name )
773 if ( table_size >= max_table_size ) {
774 barf( "Symbol table overflow\n" );
776 table[table_size].value = value;
777 table[table_size].name = name;
778 table_size = table_size + 1;
783 static rtsBool lookup_name( char *name, unsigned *result )
786 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
788 if (i < table_size) {
789 *result = table[i].value;
797 /* Code from somewhere inside GHC (circa 1994)
799 * "std"++xs -> "Zstd"++xs
800 * char_to_c 'Z' = "ZZ"
801 * char_to_c '&' = "Za"
802 * char_to_c '|' = "Zb"
803 * char_to_c ':' = "Zc"
804 * char_to_c '/' = "Zd"
805 * char_to_c '=' = "Ze"
806 * char_to_c '>' = "Zg"
807 * char_to_c '#' = "Zh"
808 * char_to_c '<' = "Zl"
809 * char_to_c '-' = "Zm"
810 * char_to_c '!' = "Zn"
811 * char_to_c '.' = "Zo"
812 * char_to_c '+' = "Zp"
813 * char_to_c '\'' = "Zq"
814 * char_to_c '*' = "Zt"
815 * char_to_c '_' = "Zu"
816 * char_to_c c = "Z" ++ show (ord c)
818 static char unZcode( char ch )
821 case 'a' : return ('&');
822 case 'b' : return ('|');
823 case 'c' : return (':');
824 case 'd' : return ('/');
825 case 'e' : return ('=');
826 case 'g' : return ('>');
827 case 'h' : return ('#');
828 case 'l' : return ('<');
829 case 'm' : return ('-');
830 case 'n' : return ('!');
831 case 'o' : return ('.');
832 case 'p' : return ('+');
833 case 'q' : return ('\'');
834 case 't' : return ('*');
835 case 'u' : return ('_');
837 case '\0' : return ('Z');
838 default : return (ch);
843 /* Precondition: out big enough to handle output (about twice length of in) */
844 static void enZcode( char *in, char *out )
850 for( i = 0; in[i] != '\0'; ++i ) {
925 const char *lookupGHCName( void *addr )
928 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
930 if (i < table_size) {
931 return table[i].name;
937 static void printZcoded( const char *raw )
941 while ( raw[j] != '\0' ) {
943 fputc(unZcode(raw[j+1]),stderr);
946 fputc(raw[j],stderr);
952 /* --------------------------------------------------------------------------
953 * Symbol table loading
954 * ------------------------------------------------------------------------*/
956 /* Causing linking trouble on Win32 plats, so I'm
957 disabling this for now.
963 /* Fairly ad-hoc piece of code that seems to filter out a lot of
964 * rubbish like the obj-splitting symbols
967 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
970 /* ToDo: make this work on BFD */
971 int tp = type & N_TYPE;
972 if (tp == N_TEXT || tp == N_DATA) {
973 return (name[0] == '_' && name[1] != '_');
979 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
980 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
987 extern void DEBUG_LoadSymbols( char *name )
993 abfd = bfd_openr(name, "default");
995 barf("can't open executable %s to get symbol table", name);
997 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1002 long storage_needed;
1003 asymbol **symbol_table;
1004 long number_of_symbols;
1005 long num_real_syms = 0;
1008 storage_needed = bfd_get_symtab_upper_bound (abfd);
1010 if (storage_needed < 0) {
1011 barf("can't read symbol table");
1014 if (storage_needed == 0) {
1015 belch("no storage needed");
1018 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1020 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1022 if (number_of_symbols < 0) {
1023 barf("can't canonicalise symbol table");
1026 for( i = 0; i != number_of_symbols; ++i ) {
1028 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1029 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1030 if (isReal(info.type, info.name)) {
1035 IF_DEBUG(interpreter,
1036 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
1037 number_of_symbols, num_real_syms)
1040 reset_table( num_real_syms );
1042 for( i = 0; i != number_of_symbols; ++i ) {
1044 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1045 if (isReal(info.type, info.name)) {
1046 insert( info.value, info.name );
1050 stgFree(symbol_table);
1055 #else /* HAVE_BFD_H */
1057 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1062 #endif /* HAVE_BFD_H */
1064 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1067 findPtr(P_ p, int follow)
1072 #if defined(__GNUC__)
1073 const int arr_size = 1024;
1075 #define arr_size 1024
1077 StgPtr arr[arr_size];
1080 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1081 for (s = 0; s < generations[g].n_steps; s++) {
1082 if (RtsFlags.GcFlags.generations == 1) {
1083 bd = generations[g].steps[s].to_blocks;
1085 bd = generations[g].steps[s].blocks;
1087 for (; bd; bd = bd->link) {
1088 for (q = bd->start; q < bd->free; q++) {
1092 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1095 fprintf(stderr, "%p = ", r);
1096 printClosure((StgClosure *)r);
1106 if (follow && i == 1) {
1107 fprintf(stderr, "-->\n");
1113 void printPtr( StgPtr p )
1115 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
1118 void printObj( StgClosure *obj )
1120 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );