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 ( unsigned value, const char *name );
42 #if 0 /* unused but might be useful sometime */
43 static rtsBool lookup_name ( char *name, unsigned *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_CHARLIKE:
139 case CONSTR_NOCAF_STATIC:
143 debugBelch("%s(", info->prof.closure_desc);
144 debugBelch("%s", obj->header.prof.ccs->cc->label);
146 debugBelch("CONSTR(");
147 printPtr((StgPtr)obj->header.info);
148 debugBelch("(tag=%d)",info->srt_bitmap);
150 for (i = 0; i < info->layout.payload.ptrs; ++i) {
152 printPtr((StgPtr)obj->payload[i]);
154 for (j = 0; j < info->layout.payload.nptrs; ++j) {
155 debugBelch(", %p#", obj->payload[i+j]);
162 case FUN_1_0: case FUN_0_1:
163 case FUN_1_1: case FUN_0_2: case FUN_2_0:
165 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
166 printPtr((StgPtr)obj->header.info);
168 debugBelch(", %s", obj->header.prof.ccs->cc->label);
170 printStdObjPayload(obj);
174 case THUNK_1_0: case THUNK_0_1:
175 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
177 /* ToDo: will this work for THUNK_STATIC too? */
179 printThunkObject((StgThunk *)obj,info->prof.closure_desc);
181 printThunkObject((StgThunk *)obj,"THUNK");
186 printStdObjHdr(obj, "THUNK_SELECTOR");
187 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
191 disassemble( (StgBCO*)obj );
196 StgAP* ap = stgCast(StgAP*,obj);
198 debugBelch("AP("); printPtr((StgPtr)ap->fun);
199 for (i = 0; i < ap->n_args; ++i) {
201 printPtr((P_)ap->payload[i]);
209 StgPAP* pap = stgCast(StgPAP*,obj);
211 debugBelch("PAP/%d(",pap->arity);
212 printPtr((StgPtr)pap->fun);
213 for (i = 0; i < pap->n_args; ++i) {
215 printPtr((StgPtr)pap->payload[i]);
223 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
225 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
226 for (i = 0; i < ap->size; ++i) {
228 printPtr((P_)ap->payload[i]);
236 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
241 debugBelch("IND_OLDGEN(");
242 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
248 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
252 case IND_OLDGEN_PERM:
253 debugBelch("IND_OLDGEN_PERM(");
254 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
259 debugBelch("IND_STATIC(");
260 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
264 /* Cannot happen -- use default case.
276 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
277 debugBelch("UPDATE_FRAME(");
278 printPtr((StgPtr)GET_INFO(u));
280 printPtr((StgPtr)u->updatee);
287 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
288 debugBelch("CATCH_FRAME(");
289 printPtr((StgPtr)GET_INFO(u));
291 printPtr((StgPtr)u->handler);
298 StgStopFrame* u = stgCast(StgStopFrame*,obj);
299 debugBelch("STOP_FRAME(");
300 printPtr((StgPtr)GET_INFO(u));
306 debugBelch("CAF_BH");
314 debugBelch("SE_BH\n");
317 case SE_CAF_BLACKHOLE:
318 debugBelch("SE_CAF_BH\n");
324 debugBelch("ARR_WORDS(\"");
325 /* ToDo: we can't safely assume that this is a string!
326 for (i = 0; arrWordsGetChar(obj,i); ++i) {
327 putchar(arrWordsGetChar(obj,i));
329 for (i=0; i<((StgArrWords *)obj)->words; i++)
330 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
335 case MUT_ARR_PTRS_CLEAN:
336 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
339 case MUT_ARR_PTRS_DIRTY:
340 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
343 case MUT_ARR_PTRS_FROZEN:
344 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
349 StgMVar* mv = (StgMVar*)obj;
350 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
356 StgMutVar* mv = (StgMutVar*)obj;
357 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
363 StgMutVar* mv = (StgMutVar*)obj;
364 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
370 debugBelch(" key=%p value=%p finalizer=%p",
371 (StgPtr)(((StgWeak*)obj)->key),
372 (StgPtr)(((StgWeak*)obj)->value),
373 (StgPtr)(((StgWeak*)obj)->finalizer));
375 /* ToDo: chase 'link' ? */
379 debugBelch("STABLE_NAME(%lu)\n", (lnat)((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[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
497 if ((bitmap & 1) == 0) {
498 printPtr((P_)payload[i]);
501 debugBelch("Word# %lu\n", (lnat)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[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
518 if ((bitmap & 1) == 0) {
519 printPtr((P_)payload[i]);
522 debugBelch("Word# %lu\n", (lnat)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) {
543 printObj((StgClosure*)sp);
547 printObj((StgClosure*)sp);
559 debugBelch("RET_DYN (%p)\n", r);
561 p = (P_)(r->payload);
562 printSmallBitmap(spBottom, sp,
563 RET_DYN_LIVENESS(r->liveness),
564 RET_DYN_BITMAP_SIZE);
565 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
567 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
568 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
569 debugBelch("Word# %ld\n", (long)*p);
573 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
574 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
583 debugBelch("RET_SMALL (%p)\n", info);
584 bitmap = info->layout.bitmap;
585 printSmallBitmap(spBottom, sp+1,
586 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
592 bco = ((StgBCO *)sp[1]);
594 debugBelch("RET_BCO (%p)\n", sp);
595 printLargeBitmap(spBottom, sp+2,
596 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
606 StgFunInfoTable *fun_info;
610 ret_fun = (StgRetFun *)sp;
611 fun_info = get_fun_itbl(ret_fun->fun);
612 size = ret_fun->size;
613 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
614 switch (fun_info->f.fun_type) {
616 printSmallBitmap(spBottom, sp+2,
617 BITMAP_BITS(fun_info->f.b.bitmap),
618 BITMAP_SIZE(fun_info->f.b.bitmap));
621 printLargeBitmap(spBottom, sp+2,
622 GET_FUN_LARGE_BITMAP(fun_info),
623 GET_FUN_LARGE_BITMAP(fun_info)->size);
626 printSmallBitmap(spBottom, sp+2,
627 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
628 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
635 debugBelch("unknown object %d\n", info->type);
636 barf("printStackChunk");
641 void printTSO( StgTSO *tso )
643 printStackChunk( tso->sp, tso->stack+tso->stack_size);
646 /* -----------------------------------------------------------------------------
649 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
650 -------------------------------------------------------------------------- */
652 static char *closure_type_names[] = {
663 "CONSTR_NOCAF_STATIC",
705 "MUT_ARR_PTRS_CLEAN",
706 "MUT_ARR_PTRS_DIRTY",
707 "MUT_ARR_PTRS_FROZEN",
731 info_type(StgClosure *closure){
732 return closure_type_names[get_itbl(closure)->type];
736 info_type_by_ip(StgInfoTable *ip){
737 return closure_type_names[ip->type];
741 info_hdr_type(StgClosure *closure, char *res){
742 strcpy(res,closure_type_names[get_itbl(closure)->type]);
745 /* --------------------------------------------------------------------------
746 * Address printing code
748 * Uses symbol table in (unstripped executable)
749 * ------------------------------------------------------------------------*/
751 /* --------------------------------------------------------------------------
752 * Simple lookup table
754 * Current implementation is pretty dumb!
755 * ------------------------------------------------------------------------*/
762 static nat table_size;
763 static struct entry* table;
766 static nat max_table_size;
768 static void reset_table( int size )
770 max_table_size = size;
772 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
775 static void prepare_table( void )
777 /* Could sort it... */
780 static void insert( unsigned value, const char *name )
782 if ( table_size >= max_table_size ) {
783 barf( "Symbol table overflow\n" );
785 table[table_size].value = value;
786 table[table_size].name = name;
787 table_size = table_size + 1;
792 static rtsBool lookup_name( char *name, unsigned *result )
795 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
797 if (i < table_size) {
798 *result = table[i].value;
806 /* Code from somewhere inside GHC (circa 1994)
808 * "std"++xs -> "Zstd"++xs
809 * char_to_c 'Z' = "ZZ"
810 * char_to_c '&' = "Za"
811 * char_to_c '|' = "Zb"
812 * char_to_c ':' = "Zc"
813 * char_to_c '/' = "Zd"
814 * char_to_c '=' = "Ze"
815 * char_to_c '>' = "Zg"
816 * char_to_c '#' = "Zh"
817 * char_to_c '<' = "Zl"
818 * char_to_c '-' = "Zm"
819 * char_to_c '!' = "Zn"
820 * char_to_c '.' = "Zo"
821 * char_to_c '+' = "Zp"
822 * char_to_c '\'' = "Zq"
823 * char_to_c '*' = "Zt"
824 * char_to_c '_' = "Zu"
825 * char_to_c c = "Z" ++ show (ord c)
827 static char unZcode( char ch )
830 case 'a' : return ('&');
831 case 'b' : return ('|');
832 case 'c' : return (':');
833 case 'd' : return ('/');
834 case 'e' : return ('=');
835 case 'g' : return ('>');
836 case 'h' : return ('#');
837 case 'l' : return ('<');
838 case 'm' : return ('-');
839 case 'n' : return ('!');
840 case 'o' : return ('.');
841 case 'p' : return ('+');
842 case 'q' : return ('\'');
843 case 't' : return ('*');
844 case 'u' : return ('_');
846 case '\0' : return ('Z');
847 default : return (ch);
852 /* Precondition: out big enough to handle output (about twice length of in) */
853 static void enZcode( char *in, char *out )
859 for( i = 0; in[i] != '\0'; ++i ) {
934 const char *lookupGHCName( void *addr )
937 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
939 if (i < table_size) {
940 return table[i].name;
946 static void printZcoded( const char *raw )
950 while ( raw[j] != '\0' ) {
952 debugBelch("%c", unZcode(raw[j+1]));
955 debugBelch("%c", unZcode(raw[j+1]));
961 /* --------------------------------------------------------------------------
962 * Symbol table loading
963 * ------------------------------------------------------------------------*/
965 /* Causing linking trouble on Win32 plats, so I'm
966 disabling this for now.
972 /* Fairly ad-hoc piece of code that seems to filter out a lot of
973 * rubbish like the obj-splitting symbols
976 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
979 /* ToDo: make this work on BFD */
980 int tp = type & N_TYPE;
981 if (tp == N_TEXT || tp == N_DATA) {
982 return (name[0] == '_' && name[1] != '_');
988 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
989 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
996 extern void DEBUG_LoadSymbols( char *name )
1002 abfd = bfd_openr(name, "default");
1004 barf("can't open executable %s to get symbol table", name);
1006 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1011 long storage_needed;
1012 asymbol **symbol_table;
1013 long number_of_symbols;
1014 long num_real_syms = 0;
1017 storage_needed = bfd_get_symtab_upper_bound (abfd);
1019 if (storage_needed < 0) {
1020 barf("can't read symbol table");
1023 if (storage_needed == 0) {
1024 debugBelch("no storage needed");
1027 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1029 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1031 if (number_of_symbols < 0) {
1032 barf("can't canonicalise symbol table");
1035 for( i = 0; i != number_of_symbols; ++i ) {
1037 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1038 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1039 if (isReal(info.type, info.name)) {
1044 IF_DEBUG(interpreter,
1045 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1046 number_of_symbols, num_real_syms)
1049 reset_table( num_real_syms );
1051 for( i = 0; i != number_of_symbols; ++i ) {
1053 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1054 if (isReal(info.type, info.name)) {
1055 insert( info.value, info.name );
1059 stgFree(symbol_table);
1064 #else /* HAVE_BFD_H */
1066 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1071 #endif /* HAVE_BFD_H */
1073 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1076 findPtr(P_ p, int follow)
1081 #if defined(__GNUC__)
1082 const int arr_size = 1024;
1084 #define arr_size 1024
1086 StgPtr arr[arr_size];
1089 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1090 for (s = 0; s < generations[g].n_steps; s++) {
1091 bd = generations[g].steps[s].blocks;
1092 for (; bd; bd = bd->link) {
1093 for (q = bd->start; q < bd->free; q++) {
1097 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1100 debugBelch("%p = ", r);
1101 printClosure((StgClosure *)r);
1111 if (follow && i == 1) {
1112 debugBelch("-->\n");
1118 void printPtr( StgPtr p )
1120 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1123 void printObj( StgClosure *obj )
1125 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );