1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1994-2000.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
19 #include "Bytecodes.h" /* for InstrPtr */
20 #include "Disassembler.h"
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);
32 /* --------------------------------------------------------------------------
33 * local function decls
34 * ------------------------------------------------------------------------*/
36 static void printStdObjPayload( StgClosure *obj );
38 static void reset_table ( int size );
39 static void prepare_table ( void );
40 static void insert ( StgWord value, const char *name );
42 #if 0 /* unused but might be useful sometime */
43 static rtsBool lookup_name ( char *name, StgWord *result );
44 static void enZcode ( char *in, char *out );
46 static char unZcode ( char ch );
47 const char * lookupGHCName ( void *addr );
48 static void printZcoded ( const char *raw );
50 /* --------------------------------------------------------------------------
52 * ------------------------------------------------------------------------*/
54 void printPtr( StgPtr p )
57 raw = lookupGHCName(p);
65 void printObj( StgClosure *obj )
67 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
72 printStdObjHdr( StgClosure *obj, char* tag )
74 debugBelch("%s(",tag);
75 printPtr((StgPtr)obj->header.info);
77 debugBelch(", %s", obj->header.prof.ccs->cc->label);
82 printStdObjPayload( StgClosure *obj )
85 const StgInfoTable* info;
88 for (i = 0; i < info->layout.payload.ptrs; ++i) {
90 printPtr((StgPtr)obj->payload[i]);
92 for (j = 0; j < info->layout.payload.nptrs; ++j) {
93 debugBelch(", %pd#",obj->payload[i+j]);
99 printThunkPayload( StgThunk *obj )
102 const StgInfoTable* info;
104 info = get_itbl(obj);
105 for (i = 0; i < info->layout.payload.ptrs; ++i) {
107 printPtr((StgPtr)obj->payload[i]);
109 for (j = 0; j < info->layout.payload.nptrs; ++j) {
110 debugBelch(", %pd#",obj->payload[i+j]);
116 printThunkObject( StgThunk *obj, char* tag )
118 printStdObjHdr( (StgClosure *)obj, tag );
119 printThunkPayload( obj );
123 printClosure( StgClosure *obj )
127 info = get_itbl(obj);
129 switch ( info->type ) {
131 barf("Invalid object");
134 case CONSTR_1_0: case CONSTR_0_1:
135 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
137 case CONSTR_NOCAF_STATIC:
141 debugBelch("%s(", info->prof.closure_desc);
142 debugBelch("%s", obj->header.prof.ccs->cc->label);
144 debugBelch("CONSTR(");
145 printPtr((StgPtr)obj->header.info);
146 debugBelch("(tag=%d)",info->srt_bitmap);
148 for (i = 0; i < info->layout.payload.ptrs; ++i) {
150 printPtr((StgPtr)obj->payload[i]);
152 for (j = 0; j < info->layout.payload.nptrs; ++j) {
153 debugBelch(", %p#", obj->payload[i+j]);
160 case FUN_1_0: case FUN_0_1:
161 case FUN_1_1: case FUN_0_2: case FUN_2_0:
163 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
164 printPtr((StgPtr)obj->header.info);
166 debugBelch(", %s", obj->header.prof.ccs->cc->label);
168 printStdObjPayload(obj);
172 case THUNK_1_0: case THUNK_0_1:
173 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
175 /* ToDo: will this work for THUNK_STATIC too? */
177 printThunkObject((StgThunk *)obj,info->prof.closure_desc);
179 printThunkObject((StgThunk *)obj,"THUNK");
184 printStdObjHdr(obj, "THUNK_SELECTOR");
185 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
189 disassemble( (StgBCO*)obj );
194 StgAP* ap = stgCast(StgAP*,obj);
196 debugBelch("AP("); printPtr((StgPtr)ap->fun);
197 for (i = 0; i < ap->n_args; ++i) {
199 printPtr((P_)ap->payload[i]);
207 StgPAP* pap = stgCast(StgPAP*,obj);
209 debugBelch("PAP/%d(",pap->arity);
210 printPtr((StgPtr)pap->fun);
211 for (i = 0; i < pap->n_args; ++i) {
213 printPtr((StgPtr)pap->payload[i]);
221 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
223 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
224 for (i = 0; i < ap->size; ++i) {
226 printPtr((P_)ap->payload[i]);
234 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
239 debugBelch("IND_OLDGEN(");
240 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
246 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
250 case IND_OLDGEN_PERM:
251 debugBelch("IND_OLDGEN_PERM(");
252 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
257 debugBelch("IND_STATIC(");
258 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
262 /* Cannot happen -- use default case.
274 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
275 debugBelch("UPDATE_FRAME(");
276 printPtr((StgPtr)GET_INFO(u));
278 printPtr((StgPtr)u->updatee);
285 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
286 debugBelch("CATCH_FRAME(");
287 printPtr((StgPtr)GET_INFO(u));
289 printPtr((StgPtr)u->handler);
296 StgStopFrame* u = stgCast(StgStopFrame*,obj);
297 debugBelch("STOP_FRAME(");
298 printPtr((StgPtr)GET_INFO(u));
304 debugBelch("CAF_BH");
312 debugBelch("SE_BH\n");
315 case SE_CAF_BLACKHOLE:
316 debugBelch("SE_CAF_BH\n");
322 debugBelch("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 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
333 case MUT_ARR_PTRS_CLEAN:
334 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
337 case MUT_ARR_PTRS_DIRTY:
338 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
341 case MUT_ARR_PTRS_FROZEN:
342 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
347 StgMVar* mv = (StgMVar*)obj;
348 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
354 StgMutVar* mv = (StgMutVar*)obj;
355 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
361 StgMutVar* mv = (StgMutVar*)obj;
362 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
368 debugBelch(" key=%p value=%p finalizer=%p",
369 (StgPtr)(((StgWeak*)obj)->key),
370 (StgPtr)(((StgWeak*)obj)->value),
371 (StgPtr)(((StgWeak*)obj)->finalizer));
373 /* ToDo: chase 'link' ? */
377 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
382 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
388 debugBelch("BLOCKED_FETCH(");
389 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
390 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
395 debugBelch("FETCH_ME(");
396 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
401 debugBelch("FETCH_ME_BQ(");
402 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
403 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
408 #if defined(GRAN) || defined(PAR)
411 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
418 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
420 debugBelch("EVACUATED(");
421 printClosure((StgEvacuated*)obj->evacuee);
426 #if defined(PAR) && defined(DIST)
428 debugBelch("REMOTE_REF(");
429 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
435 //barf("printClosure %d",get_itbl(obj)->type);
436 debugBelch("*** printClosure: unknown type %d ****\n",
437 get_itbl(obj)->type );
438 barf("printClosure %d",get_itbl(obj)->type);
444 void printGraph( StgClosure *obj )
451 printStackObj( StgPtr sp )
453 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
455 StgClosure* c = (StgClosure*)(*sp);
456 printPtr((StgPtr)*sp);
457 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
458 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
460 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
461 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
463 if (c == (StgClosure*)&stg_ctoi_F1_info) {
464 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
466 if (c == (StgClosure*)&stg_ctoi_D1_info) {
467 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
469 if (c == (StgClosure*)&stg_ctoi_V_info) {
470 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
472 if (get_itbl(c)->type == BCO) {
473 debugBelch("\t\t\t");
474 debugBelch("BCO(...)\n");
477 debugBelch("\t\t\t");
478 printClosure ( (StgClosure*)(*sp));
487 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
493 for(i = 0; i < size; i++, bitmap >>= 1 ) {
494 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
495 if ((bitmap & 1) == 0) {
496 printPtr((P_)payload[i]);
499 debugBelch("Word# %lu\n", (lnat)payload[i]);
505 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
511 for (bmp=0; i < size; bmp++) {
512 StgWord bitmap = large_bitmap->bitmap[bmp];
514 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
515 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
516 if ((bitmap & 1) == 0) {
517 printPtr((P_)payload[i]);
520 debugBelch("Word# %lu\n", (lnat)payload[i]);
527 printStackChunk( StgPtr sp, StgPtr spBottom )
530 const StgInfoTable *info;
532 ASSERT(sp <= spBottom);
533 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
535 info = get_itbl((StgClosure *)sp);
537 switch (info->type) {
541 printObj((StgClosure*)sp);
545 printObj((StgClosure*)sp);
557 debugBelch("RET_DYN (%p)\n", r);
559 p = (P_)(r->payload);
560 printSmallBitmap(spBottom, sp,
561 RET_DYN_LIVENESS(r->liveness),
562 RET_DYN_BITMAP_SIZE);
563 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
565 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
566 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
567 debugBelch("Word# %ld\n", (long)*p);
571 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
572 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
581 debugBelch("RET_SMALL (%p)\n", info);
582 bitmap = info->layout.bitmap;
583 printSmallBitmap(spBottom, sp+1,
584 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
590 bco = ((StgBCO *)sp[1]);
592 debugBelch("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 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
612 switch (fun_info->f.fun_type) {
614 printSmallBitmap(spBottom, sp+2,
615 BITMAP_BITS(fun_info->f.b.bitmap),
616 BITMAP_SIZE(fun_info->f.b.bitmap));
619 printLargeBitmap(spBottom, sp+2,
620 GET_FUN_LARGE_BITMAP(fun_info),
621 GET_FUN_LARGE_BITMAP(fun_info)->size);
624 printSmallBitmap(spBottom, sp+2,
625 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
626 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
633 debugBelch("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[] = {
659 "CONSTR_NOCAF_STATIC",
701 "MUT_ARR_PTRS_CLEAN",
702 "MUT_ARR_PTRS_DIRTY",
703 "MUT_ARR_PTRS_FROZEN",
718 "INVARIANT_CHECK_QUEUE",
729 info_type(StgClosure *closure){
730 return closure_type_names[get_itbl(closure)->type];
734 info_type_by_ip(StgInfoTable *ip){
735 return closure_type_names[ip->type];
739 info_hdr_type(StgClosure *closure, char *res){
740 strcpy(res,closure_type_names[get_itbl(closure)->type]);
743 /* --------------------------------------------------------------------------
744 * Address printing code
746 * Uses symbol table in (unstripped executable)
747 * ------------------------------------------------------------------------*/
749 /* --------------------------------------------------------------------------
750 * Simple lookup table
752 * Current implementation is pretty dumb!
753 * ------------------------------------------------------------------------*/
760 static nat table_size;
761 static struct entry* table;
764 static nat max_table_size;
766 static void reset_table( int size )
768 max_table_size = size;
770 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
773 static void prepare_table( void )
775 /* Could sort it... */
778 static void insert( StgWord value, const char *name )
780 if ( table_size >= max_table_size ) {
781 barf( "Symbol table overflow\n" );
783 table[table_size].value = value;
784 table[table_size].name = name;
785 table_size = table_size + 1;
790 static rtsBool lookup_name( char *name, StgWord *result )
793 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
795 if (i < table_size) {
796 *result = table[i].value;
804 /* Code from somewhere inside GHC (circa 1994)
806 * "std"++xs -> "Zstd"++xs
807 * char_to_c 'Z' = "ZZ"
808 * char_to_c '&' = "Za"
809 * char_to_c '|' = "Zb"
810 * char_to_c ':' = "Zc"
811 * char_to_c '/' = "Zd"
812 * char_to_c '=' = "Ze"
813 * char_to_c '>' = "Zg"
814 * char_to_c '#' = "Zh"
815 * char_to_c '<' = "Zl"
816 * char_to_c '-' = "Zm"
817 * char_to_c '!' = "Zn"
818 * char_to_c '.' = "Zo"
819 * char_to_c '+' = "Zp"
820 * char_to_c '\'' = "Zq"
821 * char_to_c '*' = "Zt"
822 * char_to_c '_' = "Zu"
823 * char_to_c c = "Z" ++ show (ord c)
825 static char unZcode( char ch )
828 case 'a' : return ('&');
829 case 'b' : return ('|');
830 case 'c' : return (':');
831 case 'd' : return ('/');
832 case 'e' : return ('=');
833 case 'g' : return ('>');
834 case 'h' : return ('#');
835 case 'l' : return ('<');
836 case 'm' : return ('-');
837 case 'n' : return ('!');
838 case 'o' : return ('.');
839 case 'p' : return ('+');
840 case 'q' : return ('\'');
841 case 't' : return ('*');
842 case 'u' : return ('_');
844 case '\0' : return ('Z');
845 default : return (ch);
850 /* Precondition: out big enough to handle output (about twice length of in) */
851 static void enZcode( char *in, char *out )
857 for( i = 0; in[i] != '\0'; ++i ) {
932 const char *lookupGHCName( void *addr )
935 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
937 if (i < table_size) {
938 return table[i].name;
944 static void printZcoded( const char *raw )
948 while ( raw[j] != '\0' ) {
950 debugBelch("%c", unZcode(raw[j+1]));
953 debugBelch("%c", unZcode(raw[j+1]));
959 /* --------------------------------------------------------------------------
960 * Symbol table loading
961 * ------------------------------------------------------------------------*/
963 /* Causing linking trouble on Win32 plats, so I'm
964 disabling this for now.
970 /* Fairly ad-hoc piece of code that seems to filter out a lot of
971 * rubbish like the obj-splitting symbols
974 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
977 /* ToDo: make this work on BFD */
978 int tp = type & N_TYPE;
979 if (tp == N_TEXT || tp == N_DATA) {
980 return (name[0] == '_' && name[1] != '_');
986 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
987 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
994 extern void DEBUG_LoadSymbols( char *name )
1000 abfd = bfd_openr(name, "default");
1002 barf("can't open executable %s to get symbol table", name);
1004 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1009 long storage_needed;
1010 asymbol **symbol_table;
1011 long number_of_symbols;
1012 long num_real_syms = 0;
1015 storage_needed = bfd_get_symtab_upper_bound (abfd);
1017 if (storage_needed < 0) {
1018 barf("can't read symbol table");
1021 if (storage_needed == 0) {
1022 debugBelch("no storage needed");
1025 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1027 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1029 if (number_of_symbols < 0) {
1030 barf("can't canonicalise symbol table");
1033 for( i = 0; i != number_of_symbols; ++i ) {
1035 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1036 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1037 if (isReal(info.type, info.name)) {
1042 IF_DEBUG(interpreter,
1043 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1044 number_of_symbols, num_real_syms)
1047 reset_table( num_real_syms );
1049 for( i = 0; i != number_of_symbols; ++i ) {
1051 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1052 if (isReal(info.type, info.name)) {
1053 insert( info.value, info.name );
1057 stgFree(symbol_table);
1062 #else /* HAVE_BFD_H */
1064 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1069 #endif /* HAVE_BFD_H */
1071 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1074 findPtr(P_ p, int follow)
1079 #if defined(__GNUC__)
1080 const int arr_size = 1024;
1082 #define arr_size 1024
1084 StgPtr arr[arr_size];
1087 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1088 for (s = 0; s < generations[g].n_steps; s++) {
1089 bd = generations[g].steps[s].blocks;
1090 for (; bd; bd = bd->link) {
1091 for (q = bd->start; q < bd->free; q++) {
1095 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1098 debugBelch("%p = ", r);
1099 printClosure((StgClosure *)r);
1109 if (follow && i == 1) {
1110 debugBelch("-->\n");
1116 void printPtr( StgPtr p )
1118 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1121 void printObj( StgClosure *obj )
1123 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );