1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.64 2004/09/03 15:28:35 simonmar Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
20 #include "Bytecodes.h" /* for InstrPtr */
21 #include "Disassembler.h"
27 #if defined(GRAN) || defined(PAR)
28 // HWL: explicit fixed header size to make debugging easier
29 int fixed_hs = sizeof(StgHeader), itbl_sz = sizeofW(StgInfoTable),
30 uf_sz=sizeofW(StgUpdateFrame);
33 /* --------------------------------------------------------------------------
34 * local function decls
35 * ------------------------------------------------------------------------*/
37 static void printStdObject( StgClosure *obj, char* tag );
38 static void printStdObjPayload( StgClosure *obj );
40 static void reset_table ( int size );
41 static void prepare_table ( void );
42 static void insert ( unsigned value, const char *name );
44 #if 0 /* unused but might be useful sometime */
45 static rtsBool lookup_name ( char *name, unsigned *result );
46 static void enZcode ( char *in, char *out );
48 static char unZcode ( char ch );
49 const char * lookupGHCName ( void *addr );
50 static void printZcoded ( const char *raw );
52 /* --------------------------------------------------------------------------
54 * ------------------------------------------------------------------------*/
56 void printPtr( StgPtr p )
59 raw = lookupGHCName(p);
67 void printObj( StgClosure *obj )
69 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
74 printStdObjHdr( StgClosure *obj, char* tag )
76 debugBelch("%s(",tag);
77 printPtr((StgPtr)obj->header.info);
79 debugBelch(", %s", obj->header.prof.ccs->cc->label);
84 printStdObjPayload( StgClosure *obj )
87 const StgInfoTable* info;
90 for (i = 0; i < info->layout.payload.ptrs; ++i) {
92 printPtr((StgPtr)obj->payload[i]);
94 for (j = 0; j < info->layout.payload.nptrs; ++j) {
95 debugBelch(", %pd#",obj->payload[i+j]);
101 printStdObject( StgClosure *obj, char* tag )
103 printStdObjHdr( obj, tag );
104 printStdObjPayload( obj );
108 printClosure( StgClosure *obj )
112 info = get_itbl(obj);
114 switch ( info->type ) {
116 barf("Invalid object");
119 case CONSTR_1_0: case CONSTR_0_1:
120 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
122 case CONSTR_CHARLIKE:
124 case CONSTR_NOCAF_STATIC:
126 /* We can't use printStdObject because we want to print the
131 debugBelch("%s(", info->prof.closure_desc);
132 debugBelch("%s", obj->header.prof.ccs->cc->label);
134 debugBelch("CONSTR(");
135 printPtr((StgPtr)obj->header.info);
136 debugBelch("(tag=%d)",info->srt_bitmap);
138 for (i = 0; i < info->layout.payload.ptrs; ++i) {
140 printPtr((StgPtr)obj->payload[i]);
142 for (j = 0; j < info->layout.payload.nptrs; ++j) {
143 debugBelch(", %p#", obj->payload[i+j]);
150 case FUN_1_0: case FUN_0_1:
151 case FUN_1_1: case FUN_0_2: case FUN_2_0:
153 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
154 printPtr((StgPtr)obj->header.info);
156 debugBelch(", %s", obj->header.prof.ccs->cc->label);
158 printStdObjPayload(obj);
162 case THUNK_1_0: case THUNK_0_1:
163 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
165 /* ToDo: will this work for THUNK_STATIC too? */
167 printStdObject(obj,info->prof.closure_desc);
169 printStdObject(obj,"THUNK");
174 printStdObjHdr(obj, "THUNK_SELECTOR");
175 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
179 disassemble( (StgBCO*)obj );
184 StgPAP* ap = stgCast(StgPAP*,obj);
186 debugBelch("AP("); printPtr((StgPtr)ap->fun);
187 for (i = 0; i < ap->n_args; ++i) {
189 printPtr((P_)ap->payload[i]);
197 StgPAP* pap = stgCast(StgPAP*,obj);
199 debugBelch("PAP/%d(",pap->arity);
200 printPtr((StgPtr)pap->fun);
201 for (i = 0; i < pap->n_args; ++i) {
203 printPtr((StgPtr)pap->payload[i]);
211 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
213 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
214 for (i = 0; i < ap->size; ++i) {
216 printPtr((P_)ap->payload[i]);
224 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
229 debugBelch("IND_OLDGEN(");
230 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
236 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
240 case IND_OLDGEN_PERM:
241 debugBelch("IND_OLDGEN_PERM(");
242 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
247 debugBelch("IND_STATIC(");
248 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
252 /* Cannot happen -- use default case.
264 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
265 debugBelch("UPDATE_FRAME(");
266 printPtr((StgPtr)GET_INFO(u));
268 printPtr((StgPtr)u->updatee);
275 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
276 debugBelch("CATCH_FRAME(");
277 printPtr((StgPtr)GET_INFO(u));
279 printPtr((StgPtr)u->handler);
286 StgStopFrame* u = stgCast(StgStopFrame*,obj);
287 debugBelch("STOP_FRAME(");
288 printPtr((StgPtr)GET_INFO(u));
294 debugBelch("CAF_BH(");
295 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
305 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
310 debugBelch("SE_BH\n");
313 case SE_CAF_BLACKHOLE:
314 debugBelch("SE_CAF_BH\n");
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));
325 for (i=0; i<((StgArrWords *)obj)->words; i++)
326 debugBelch("%u", ((StgArrWords *)obj)->payload[i]);
332 debugBelch("MUT_ARR_PTRS(size=%d)\n", ((StgMutArrPtrs *)obj)->ptrs);
335 case MUT_ARR_PTRS_FROZEN:
336 #if !defined(XMLAMBDA)
337 debugBelch("MUT_ARR_PTRS_FROZEN(size=%d)\n", ((StgMutArrPtrs *)obj)->ptrs);
341 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
343 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
345 debugBelch("Row<%i>(",p->ptrs);
346 for (i = 0; i < p->ptrs; ++i) {
347 if (i > 0) debugBelch(", ");
348 printPtr((StgPtr)(p->payload[i]));
357 StgMutVar* mv = (StgMutVar*)obj;
358 debugBelch("MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
364 debugBelch(" key=%p value=%p finalizer=%p",
365 (StgPtr)(((StgWeak*)obj)->key),
366 (StgPtr)(((StgWeak*)obj)->value),
367 (StgPtr)(((StgWeak*)obj)->finalizer));
369 /* ToDo: chase 'link' ? */
373 debugBelch("FOREIGN(");
374 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
379 debugBelch("STABLE_NAME(%d)\n", ((StgStableName*)obj)->sn);
384 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
390 debugBelch("BLOCKED_FETCH(");
391 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
392 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
397 debugBelch("FETCH_ME(");
398 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
403 debugBelch("FETCH_ME_BQ(");
404 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
405 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
410 #if defined(GRAN) || defined(PAR)
413 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
420 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
422 debugBelch("EVACUATED(");
423 printClosure((StgEvacuated*)obj->evacuee);
428 #if defined(PAR) && defined(DIST)
430 debugBelch("REMOTE_REF(");
431 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
437 //barf("printClosure %d",get_itbl(obj)->type);
438 debugBelch("*** printClosure: unknown type %d ****\n",
439 get_itbl(obj)->type );
440 barf("printClosure %d",get_itbl(obj)->type);
446 void printGraph( StgClosure *obj )
453 printStackObj( StgPtr sp )
455 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
457 StgClosure* c = (StgClosure*)(*sp);
458 printPtr((StgPtr)*sp);
459 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
460 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
462 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
463 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
465 if (c == (StgClosure*)&stg_ctoi_F1_info) {
466 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
468 if (c == (StgClosure*)&stg_ctoi_D1_info) {
469 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
471 if (c == (StgClosure*)&stg_ctoi_V_info) {
472 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
474 if (get_itbl(c)->type == BCO) {
475 debugBelch("\t\t\t");
476 debugBelch("BCO(...)\n");
479 debugBelch("\t\t\t");
480 printClosure ( (StgClosure*)(*sp));
489 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
495 for(i = 0; i < size; i++, bitmap >>= 1 ) {
496 debugBelch(" stk[%d] (%p) = ", spBottom-(payload+i), payload+i);
497 if ((bitmap & 1) == 0) {
498 printPtr((P_)payload[i]);
501 debugBelch("Word# %d\n", payload[i]);
507 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
513 for (bmp=0; i < size; bmp++) {
514 StgWord bitmap = large_bitmap->bitmap[bmp];
516 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
517 debugBelch(" stk[%d] (%p) = ", spBottom-(payload+i), payload+i);
518 if ((bitmap & 1) == 0) {
519 printPtr((P_)payload[i]);
522 debugBelch("Word# %d\n", payload[i]);
529 printStackChunk( StgPtr sp, StgPtr spBottom )
532 const StgInfoTable *info;
534 ASSERT(sp <= spBottom);
535 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
537 info = get_itbl((StgClosure *)sp);
539 switch (info->type) {
544 printObj((StgClosure*)sp);
556 debugBelch("RET_DYN (%p)\n", r);
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;
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);
570 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
571 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
580 debugBelch("RET_SMALL (%p)\n", sp);
581 bitmap = info->layout.bitmap;
582 printSmallBitmap(spBottom, sp+1,
583 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
589 bco = ((StgBCO *)sp[1]);
591 debugBelch("RET_BCO (%p)\n", sp);
592 printLargeBitmap(spBottom, sp+2,
593 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
603 StgFunInfoTable *fun_info;
607 ret_fun = (StgRetFun *)sp;
608 fun_info = get_fun_itbl(ret_fun->fun);
609 size = ret_fun->size;
610 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
611 switch (fun_info->f.fun_type) {
613 printSmallBitmap(spBottom, sp+1,
614 BITMAP_BITS(fun_info->f.bitmap),
615 BITMAP_SIZE(fun_info->f.bitmap));
618 printLargeBitmap(spBottom, sp+2,
619 (StgLargeBitmap *)fun_info->f.bitmap,
620 BITMAP_SIZE(fun_info->f.bitmap));
623 printSmallBitmap(spBottom, sp+1,
624 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
625 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
632 debugBelch("unknown object %d\n", info->type);
633 barf("printStackChunk");
638 void printTSO( StgTSO *tso )
640 printStackChunk( tso->sp, tso->stack+tso->stack_size);
643 /* -----------------------------------------------------------------------------
646 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
647 -------------------------------------------------------------------------- */
649 static char *closure_type_names[] = {
660 "CONSTR_NOCAF_STATIC",
703 "MUT_ARR_PTRS_FROZEN",
720 info_type(StgClosure *closure){
721 return closure_type_names[get_itbl(closure)->type];
725 info_type_by_ip(StgInfoTable *ip){
726 return closure_type_names[ip->type];
730 info_hdr_type(StgClosure *closure, char *res){
731 strcpy(res,closure_type_names[get_itbl(closure)->type]);
734 /* --------------------------------------------------------------------------
735 * Address printing code
737 * Uses symbol table in (unstripped executable)
738 * ------------------------------------------------------------------------*/
740 /* --------------------------------------------------------------------------
741 * Simple lookup table
743 * Current implementation is pretty dumb!
744 * ------------------------------------------------------------------------*/
751 static nat table_size;
752 static struct entry* table;
755 static nat max_table_size;
757 static void reset_table( int size )
759 max_table_size = size;
761 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
764 static void prepare_table( void )
766 /* Could sort it... */
769 static void insert( unsigned value, const char *name )
771 if ( table_size >= max_table_size ) {
772 barf( "Symbol table overflow\n" );
774 table[table_size].value = value;
775 table[table_size].name = name;
776 table_size = table_size + 1;
781 static rtsBool lookup_name( char *name, unsigned *result )
784 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
786 if (i < table_size) {
787 *result = table[i].value;
795 /* Code from somewhere inside GHC (circa 1994)
797 * "std"++xs -> "Zstd"++xs
798 * char_to_c 'Z' = "ZZ"
799 * char_to_c '&' = "Za"
800 * char_to_c '|' = "Zb"
801 * char_to_c ':' = "Zc"
802 * char_to_c '/' = "Zd"
803 * char_to_c '=' = "Ze"
804 * char_to_c '>' = "Zg"
805 * char_to_c '#' = "Zh"
806 * char_to_c '<' = "Zl"
807 * char_to_c '-' = "Zm"
808 * char_to_c '!' = "Zn"
809 * char_to_c '.' = "Zo"
810 * char_to_c '+' = "Zp"
811 * char_to_c '\'' = "Zq"
812 * char_to_c '*' = "Zt"
813 * char_to_c '_' = "Zu"
814 * char_to_c c = "Z" ++ show (ord c)
816 static char unZcode( char ch )
819 case 'a' : return ('&');
820 case 'b' : return ('|');
821 case 'c' : return (':');
822 case 'd' : return ('/');
823 case 'e' : return ('=');
824 case 'g' : return ('>');
825 case 'h' : return ('#');
826 case 'l' : return ('<');
827 case 'm' : return ('-');
828 case 'n' : return ('!');
829 case 'o' : return ('.');
830 case 'p' : return ('+');
831 case 'q' : return ('\'');
832 case 't' : return ('*');
833 case 'u' : return ('_');
835 case '\0' : return ('Z');
836 default : return (ch);
841 /* Precondition: out big enough to handle output (about twice length of in) */
842 static void enZcode( char *in, char *out )
848 for( i = 0; in[i] != '\0'; ++i ) {
923 const char *lookupGHCName( void *addr )
926 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
928 if (i < table_size) {
929 return table[i].name;
935 static void printZcoded( const char *raw )
939 while ( raw[j] != '\0' ) {
941 debugBelch("%c", unZcode(raw[j+1]));
944 debugBelch("%c", unZcode(raw[j+1]));
950 /* --------------------------------------------------------------------------
951 * Symbol table loading
952 * ------------------------------------------------------------------------*/
954 /* Causing linking trouble on Win32 plats, so I'm
955 disabling this for now.
961 /* Fairly ad-hoc piece of code that seems to filter out a lot of
962 * rubbish like the obj-splitting symbols
965 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
968 /* ToDo: make this work on BFD */
969 int tp = type & N_TYPE;
970 if (tp == N_TEXT || tp == N_DATA) {
971 return (name[0] == '_' && name[1] != '_');
977 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
978 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
985 extern void DEBUG_LoadSymbols( char *name )
991 abfd = bfd_openr(name, "default");
993 barf("can't open executable %s to get symbol table", name);
995 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1000 long storage_needed;
1001 asymbol **symbol_table;
1002 long number_of_symbols;
1003 long num_real_syms = 0;
1006 storage_needed = bfd_get_symtab_upper_bound (abfd);
1008 if (storage_needed < 0) {
1009 barf("can't read symbol table");
1012 if (storage_needed == 0) {
1013 debugBelch("no storage needed");
1016 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1018 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1020 if (number_of_symbols < 0) {
1021 barf("can't canonicalise symbol table");
1024 for( i = 0; i != number_of_symbols; ++i ) {
1026 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1027 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1028 if (isReal(info.type, info.name)) {
1033 IF_DEBUG(interpreter,
1034 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1035 number_of_symbols, num_real_syms)
1038 reset_table( num_real_syms );
1040 for( i = 0; i != number_of_symbols; ++i ) {
1042 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1043 if (isReal(info.type, info.name)) {
1044 insert( info.value, info.name );
1048 stgFree(symbol_table);
1053 #else /* HAVE_BFD_H */
1055 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1060 #endif /* HAVE_BFD_H */
1062 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1065 findPtr(P_ p, int follow)
1070 #if defined(__GNUC__)
1071 const int arr_size = 1024;
1073 #define arr_size 1024
1075 StgPtr arr[arr_size];
1078 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1079 for (s = 0; s < generations[g].n_steps; s++) {
1080 if (RtsFlags.GcFlags.generations == 1) {
1081 bd = generations[g].steps[s].to_blocks;
1083 bd = generations[g].steps[s].blocks;
1085 for (; bd; bd = bd->link) {
1086 for (q = bd->start; q < bd->free; q++) {
1090 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1093 debugBelch("%p = ", r);
1094 printClosure((StgClosure *)r);
1104 if (follow && i == 1) {
1105 debugBelch("-->\n");
1111 void printPtr( StgPtr p )
1113 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1116 void printObj( StgClosure *obj )
1118 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );