1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.61 2003/10/16 20:54:12 sof 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 = FIXED_HS, 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)->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_ret_R1p_info) {
462 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
464 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
465 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
467 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
468 fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
470 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
471 fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
473 if (c == (StgClosure*)&stg_ctoi_ret_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 GET_LIVENESS(r->liveness), RET_DYN_BITMAP_SIZE);
563 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
565 for (size = GET_NONPTRS(dyn); size > 0; size--) {
566 fprintf(stderr," stk[%ld] (%p) = ", (long)(spBottom-p), p);
567 fprintf(stderr,"Word# %ld\n", (long)*p);
571 for (size = GET_PTRS(dyn); size > 0; size--) {
572 fprintf(stderr," stk[%ld] (%p) = ", (long)(spBottom-p), p);
581 fprintf(stderr, "RET_SMALL (%p)\n", sp);
582 bitmap = info->layout.bitmap;
583 printSmallBitmap(spBottom, sp+1,
584 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
590 bco = ((StgBCO *)sp[1]);
592 fprintf(stderr, "RET_BCO (%p)\n", sp);
593 printLargeBitmap(spBottom, sp+2,
594 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
604 StgFunInfoTable *fun_info;
608 ret_fun = (StgRetFun *)sp;
609 fun_info = get_fun_itbl(ret_fun->fun);
610 size = ret_fun->size;
611 fprintf(stderr,"RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->fun_type);
612 switch (fun_info->fun_type) {
614 printSmallBitmap(spBottom, sp+1,
615 BITMAP_BITS(fun_info->bitmap),
616 BITMAP_SIZE(fun_info->bitmap));
619 printLargeBitmap(spBottom, sp+2,
620 (StgLargeBitmap *)fun_info->bitmap,
621 BITMAP_SIZE(fun_info->bitmap));
624 printSmallBitmap(spBottom, sp+1,
625 BITMAP_BITS(stg_arg_bitmaps[fun_info->fun_type]),
626 BITMAP_SIZE(stg_arg_bitmaps[fun_info->fun_type]));
633 fprintf(stderr, "unknown object %d\n", info->type);
634 barf("printStackChunk");
639 void printTSO( StgTSO *tso )
641 printStackChunk( tso->sp, tso->stack+tso->stack_size);
644 /* -----------------------------------------------------------------------------
647 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
648 -------------------------------------------------------------------------- */
650 static char *closure_type_names[] = {
661 "CONSTR_NOCAF_STATIC",
704 "MUT_ARR_PTRS_FROZEN",
721 info_type(StgClosure *closure){
722 return closure_type_names[get_itbl(closure)->type];
726 info_type_by_ip(StgInfoTable *ip){
727 return closure_type_names[ip->type];
731 info_hdr_type(StgClosure *closure, char *res){
732 strcpy(res,closure_type_names[get_itbl(closure)->type]);
735 /* --------------------------------------------------------------------------
736 * Address printing code
738 * Uses symbol table in (unstripped executable)
739 * ------------------------------------------------------------------------*/
741 /* --------------------------------------------------------------------------
742 * Simple lookup table
744 * Current implementation is pretty dumb!
745 * ------------------------------------------------------------------------*/
752 static nat table_size;
753 static struct entry* table;
756 static nat max_table_size;
758 static void reset_table( int size )
760 max_table_size = size;
762 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
765 static void prepare_table( void )
767 /* Could sort it... */
770 static void insert( unsigned value, const char *name )
772 if ( table_size >= max_table_size ) {
773 barf( "Symbol table overflow\n" );
775 table[table_size].value = value;
776 table[table_size].name = name;
777 table_size = table_size + 1;
782 static rtsBool lookup_name( char *name, unsigned *result )
785 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
787 if (i < table_size) {
788 *result = table[i].value;
796 /* Code from somewhere inside GHC (circa 1994)
798 * "std"++xs -> "Zstd"++xs
799 * char_to_c 'Z' = "ZZ"
800 * char_to_c '&' = "Za"
801 * char_to_c '|' = "Zb"
802 * char_to_c ':' = "Zc"
803 * char_to_c '/' = "Zd"
804 * char_to_c '=' = "Ze"
805 * char_to_c '>' = "Zg"
806 * char_to_c '#' = "Zh"
807 * char_to_c '<' = "Zl"
808 * char_to_c '-' = "Zm"
809 * char_to_c '!' = "Zn"
810 * char_to_c '.' = "Zo"
811 * char_to_c '+' = "Zp"
812 * char_to_c '\'' = "Zq"
813 * char_to_c '*' = "Zt"
814 * char_to_c '_' = "Zu"
815 * char_to_c c = "Z" ++ show (ord c)
817 static char unZcode( char ch )
820 case 'a' : return ('&');
821 case 'b' : return ('|');
822 case 'c' : return (':');
823 case 'd' : return ('/');
824 case 'e' : return ('=');
825 case 'g' : return ('>');
826 case 'h' : return ('#');
827 case 'l' : return ('<');
828 case 'm' : return ('-');
829 case 'n' : return ('!');
830 case 'o' : return ('.');
831 case 'p' : return ('+');
832 case 'q' : return ('\'');
833 case 't' : return ('*');
834 case 'u' : return ('_');
836 case '\0' : return ('Z');
837 default : return (ch);
842 /* Precondition: out big enough to handle output (about twice length of in) */
843 static void enZcode( char *in, char *out )
849 for( i = 0; in[i] != '\0'; ++i ) {
924 const char *lookupGHCName( void *addr )
927 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
929 if (i < table_size) {
930 return table[i].name;
936 static void printZcoded( const char *raw )
940 while ( raw[j] != '\0' ) {
942 fputc(unZcode(raw[j+1]),stderr);
945 fputc(raw[j],stderr);
951 /* --------------------------------------------------------------------------
952 * Symbol table loading
953 * ------------------------------------------------------------------------*/
955 /* Causing linking trouble on Win32 plats, so I'm
956 disabling this for now.
962 /* Fairly ad-hoc piece of code that seems to filter out a lot of
963 * rubbish like the obj-splitting symbols
966 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
969 /* ToDo: make this work on BFD */
970 int tp = type & N_TYPE;
971 if (tp == N_TEXT || tp == N_DATA) {
972 return (name[0] == '_' && name[1] != '_');
978 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
979 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
986 extern void DEBUG_LoadSymbols( char *name )
992 abfd = bfd_openr(name, "default");
994 barf("can't open executable %s to get symbol table", name);
996 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1001 long storage_needed;
1002 asymbol **symbol_table;
1003 long number_of_symbols;
1004 long num_real_syms = 0;
1007 storage_needed = bfd_get_symtab_upper_bound (abfd);
1009 if (storage_needed < 0) {
1010 barf("can't read symbol table");
1013 if (storage_needed == 0) {
1014 belch("no storage needed");
1017 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1019 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1021 if (number_of_symbols < 0) {
1022 barf("can't canonicalise symbol table");
1025 for( i = 0; i != number_of_symbols; ++i ) {
1027 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1028 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1029 if (isReal(info.type, info.name)) {
1034 IF_DEBUG(interpreter,
1035 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
1036 number_of_symbols, num_real_syms)
1039 reset_table( num_real_syms );
1041 for( i = 0; i != number_of_symbols; ++i ) {
1043 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1044 if (isReal(info.type, info.name)) {
1045 insert( info.value, info.name );
1049 stgFree(symbol_table);
1054 #else /* HAVE_BFD_H */
1056 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1061 #endif /* HAVE_BFD_H */
1063 #include "StoragePriv.h"
1065 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1068 findPtr(P_ p, int follow)
1073 #if defined(__GNUC__)
1074 const int arr_size = 1024;
1076 #define arr_size 1024
1078 StgPtr arr[arr_size];
1081 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1082 for (s = 0; s < generations[g].n_steps; s++) {
1083 if (RtsFlags.GcFlags.generations == 1) {
1084 bd = generations[g].steps[s].to_blocks;
1086 bd = generations[g].steps[s].blocks;
1088 for (; bd; bd = bd->link) {
1089 for (q = bd->start; q < bd->free; q++) {
1093 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1096 fprintf(stderr, "%p = ", r);
1097 printClosure((StgClosure *)r);
1107 if (follow && i == 1) {
1108 fprintf(stderr, "-->\n");
1114 void printPtr( StgPtr p )
1116 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
1119 void printObj( StgClosure *obj )
1121 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );