1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1994-2000.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
18 #include "Bytecodes.h" /* for InstrPtr */
19 #include "Disassembler.h"
25 #if defined(GRAN) || defined(PAR)
26 // HWL: explicit fixed header size to make debugging easier
27 int fixed_hs = sizeof(StgHeader), itbl_sz = sizeofW(StgInfoTable),
28 uf_sz=sizeofW(StgUpdateFrame);
31 /* --------------------------------------------------------------------------
32 * local function decls
33 * ------------------------------------------------------------------------*/
35 static void printStdObjPayload( StgClosure *obj );
37 static void reset_table ( int size );
38 static void prepare_table ( void );
39 static void insert ( StgWord value, const char *name );
41 #if 0 /* unused but might be useful sometime */
42 static rtsBool lookup_name ( char *name, StgWord *result );
43 static void enZcode ( char *in, char *out );
45 static char unZcode ( char ch );
46 const char * lookupGHCName ( void *addr );
47 static void printZcoded ( const char *raw );
49 /* --------------------------------------------------------------------------
51 * ------------------------------------------------------------------------*/
53 void printPtr( StgPtr p )
56 raw = lookupGHCName(p);
64 void printObj( StgClosure *obj )
66 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
71 printStdObjHdr( StgClosure *obj, char* tag )
73 debugBelch("%s(",tag);
74 printPtr((StgPtr)obj->header.info);
76 debugBelch(", %s", obj->header.prof.ccs->cc->label);
81 printStdObjPayload( StgClosure *obj )
84 const StgInfoTable* info;
87 for (i = 0; i < info->layout.payload.ptrs; ++i) {
89 printPtr((StgPtr)obj->payload[i]);
91 for (j = 0; j < info->layout.payload.nptrs; ++j) {
92 debugBelch(", %pd#",obj->payload[i+j]);
98 printThunkPayload( StgThunk *obj )
101 const StgInfoTable* info;
103 info = get_itbl(obj);
104 for (i = 0; i < info->layout.payload.ptrs; ++i) {
106 printPtr((StgPtr)obj->payload[i]);
108 for (j = 0; j < info->layout.payload.nptrs; ++j) {
109 debugBelch(", %pd#",obj->payload[i+j]);
115 printThunkObject( StgThunk *obj, char* tag )
117 printStdObjHdr( (StgClosure *)obj, tag );
118 printThunkPayload( obj );
122 printClosure( StgClosure *obj )
126 info = get_itbl(obj);
128 switch ( info->type ) {
130 barf("Invalid object");
133 case CONSTR_1_0: case CONSTR_0_1:
134 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
136 case CONSTR_NOCAF_STATIC:
141 debugBelch("%s(", GET_PROF_DESC(info));
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,GET_PROF_DESC(info));
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.
272 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
273 debugBelch("UPDATE_FRAME(");
274 printPtr((StgPtr)GET_INFO(u));
276 printPtr((StgPtr)u->updatee);
283 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
284 debugBelch("CATCH_FRAME(");
285 printPtr((StgPtr)GET_INFO(u));
287 printPtr((StgPtr)u->handler);
294 StgStopFrame* u = stgCast(StgStopFrame*,obj);
295 debugBelch("STOP_FRAME(");
296 printPtr((StgPtr)GET_INFO(u));
302 debugBelch("CAF_BH");
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("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
331 case MUT_ARR_PTRS_CLEAN:
332 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
335 case MUT_ARR_PTRS_DIRTY:
336 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
339 case MUT_ARR_PTRS_FROZEN:
340 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
346 StgMVar* mv = (StgMVar*)obj;
347 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
353 StgMutVar* mv = (StgMutVar*)obj;
354 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
360 StgMutVar* mv = (StgMutVar*)obj;
361 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
367 debugBelch(" key=%p value=%p finalizer=%p",
368 (StgPtr)(((StgWeak*)obj)->key),
369 (StgPtr)(((StgWeak*)obj)->value),
370 (StgPtr)(((StgWeak*)obj)->finalizer));
372 /* ToDo: chase 'link' ? */
376 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
381 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
387 debugBelch("BLOCKED_FETCH(");
388 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
389 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
394 debugBelch("FETCH_ME(");
395 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
400 debugBelch("FETCH_ME_BQ(");
401 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
402 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
407 #if defined(GRAN) || defined(PAR)
410 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
417 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
419 debugBelch("EVACUATED(");
420 printClosure((StgEvacuated*)obj->evacuee);
425 #if defined(PAR) && defined(DIST)
427 debugBelch("REMOTE_REF(");
428 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
434 //barf("printClosure %d",get_itbl(obj)->type);
435 debugBelch("*** printClosure: unknown type %d ****\n",
436 get_itbl(obj)->type );
437 barf("printClosure %d",get_itbl(obj)->type);
443 void printGraph( StgClosure *obj )
450 printStackObj( StgPtr sp )
452 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
454 StgClosure* c = (StgClosure*)(*sp);
455 printPtr((StgPtr)*sp);
456 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
457 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
459 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
460 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
462 if (c == (StgClosure*)&stg_ctoi_F1_info) {
463 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
465 if (c == (StgClosure*)&stg_ctoi_D1_info) {
466 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
468 if (c == (StgClosure*)&stg_ctoi_V_info) {
469 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
471 if (get_itbl(c)->type == BCO) {
472 debugBelch("\t\t\t");
473 debugBelch("BCO(...)\n");
476 debugBelch("\t\t\t");
477 printClosure ( (StgClosure*)(*sp));
486 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
492 for(i = 0; i < size; i++, bitmap >>= 1 ) {
493 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
494 if ((bitmap & 1) == 0) {
495 printPtr((P_)payload[i]);
498 debugBelch("Word# %lu\n", (lnat)payload[i]);
504 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
510 for (bmp=0; i < size; bmp++) {
511 StgWord bitmap = large_bitmap->bitmap[bmp];
513 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
514 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
515 if ((bitmap & 1) == 0) {
516 printPtr((P_)payload[i]);
519 debugBelch("Word# %lu\n", (lnat)payload[i]);
526 printStackChunk( StgPtr sp, StgPtr spBottom )
529 const StgInfoTable *info;
531 ASSERT(sp <= spBottom);
532 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
534 info = get_itbl((StgClosure *)sp);
536 switch (info->type) {
540 printObj((StgClosure*)sp);
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);
579 debugBelch("RET_SMALL (%p)\n", info);
580 bitmap = info->layout.bitmap;
581 printSmallBitmap(spBottom, sp+1,
582 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
588 bco = ((StgBCO *)sp[1]);
590 debugBelch("RET_BCO (%p)\n", sp);
591 printLargeBitmap(spBottom, sp+2,
592 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
601 StgFunInfoTable *fun_info;
605 ret_fun = (StgRetFun *)sp;
606 fun_info = get_fun_itbl(ret_fun->fun);
607 size = ret_fun->size;
608 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
609 switch (fun_info->f.fun_type) {
611 printSmallBitmap(spBottom, sp+2,
612 BITMAP_BITS(fun_info->f.b.bitmap),
613 BITMAP_SIZE(fun_info->f.b.bitmap));
616 printLargeBitmap(spBottom, sp+2,
617 GET_FUN_LARGE_BITMAP(fun_info),
618 GET_FUN_LARGE_BITMAP(fun_info)->size);
621 printSmallBitmap(spBottom, sp+2,
622 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
623 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
630 debugBelch("unknown object %d\n", info->type);
631 barf("printStackChunk");
636 void printTSO( StgTSO *tso )
638 printStackChunk( tso->sp, tso->stack+tso->stack_size);
641 /* -----------------------------------------------------------------------------
644 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
645 -------------------------------------------------------------------------- */
647 static char *closure_type_names[] = {
656 "CONSTR_NOCAF_STATIC",
696 "MUT_ARR_PTRS_CLEAN",
697 "MUT_ARR_PTRS_DIRTY",
698 "MUT_ARR_PTRS_FROZEN",
713 "INVARIANT_CHECK_QUEUE",
724 info_type(StgClosure *closure){
725 return closure_type_names[get_itbl(closure)->type];
729 info_type_by_ip(StgInfoTable *ip){
730 return closure_type_names[ip->type];
734 info_hdr_type(StgClosure *closure, char *res){
735 strcpy(res,closure_type_names[get_itbl(closure)->type]);
738 /* --------------------------------------------------------------------------
739 * Address printing code
741 * Uses symbol table in (unstripped executable)
742 * ------------------------------------------------------------------------*/
744 /* --------------------------------------------------------------------------
745 * Simple lookup table
747 * Current implementation is pretty dumb!
748 * ------------------------------------------------------------------------*/
755 static nat table_size;
756 static struct entry* table;
759 static nat max_table_size;
761 static void reset_table( int size )
763 max_table_size = size;
765 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
768 static void prepare_table( void )
770 /* Could sort it... */
773 static void insert( StgWord value, const char *name )
775 if ( table_size >= max_table_size ) {
776 barf( "Symbol table overflow\n" );
778 table[table_size].value = value;
779 table[table_size].name = name;
780 table_size = table_size + 1;
785 static rtsBool lookup_name( char *name, StgWord *result )
788 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
790 if (i < table_size) {
791 *result = table[i].value;
799 /* Code from somewhere inside GHC (circa 1994)
801 * "std"++xs -> "Zstd"++xs
802 * char_to_c 'Z' = "ZZ"
803 * char_to_c '&' = "Za"
804 * char_to_c '|' = "Zb"
805 * char_to_c ':' = "Zc"
806 * char_to_c '/' = "Zd"
807 * char_to_c '=' = "Ze"
808 * char_to_c '>' = "Zg"
809 * char_to_c '#' = "Zh"
810 * char_to_c '<' = "Zl"
811 * char_to_c '-' = "Zm"
812 * char_to_c '!' = "Zn"
813 * char_to_c '.' = "Zo"
814 * char_to_c '+' = "Zp"
815 * char_to_c '\'' = "Zq"
816 * char_to_c '*' = "Zt"
817 * char_to_c '_' = "Zu"
818 * char_to_c c = "Z" ++ show (ord c)
820 static char unZcode( char ch )
823 case 'a' : return ('&');
824 case 'b' : return ('|');
825 case 'c' : return (':');
826 case 'd' : return ('/');
827 case 'e' : return ('=');
828 case 'g' : return ('>');
829 case 'h' : return ('#');
830 case 'l' : return ('<');
831 case 'm' : return ('-');
832 case 'n' : return ('!');
833 case 'o' : return ('.');
834 case 'p' : return ('+');
835 case 'q' : return ('\'');
836 case 't' : return ('*');
837 case 'u' : return ('_');
839 case '\0' : return ('Z');
840 default : return (ch);
845 /* Precondition: out big enough to handle output (about twice length of in) */
846 static void enZcode( char *in, char *out )
852 for( i = 0; in[i] != '\0'; ++i ) {
927 const char *lookupGHCName( void *addr )
930 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
932 if (i < table_size) {
933 return table[i].name;
939 static void printZcoded( const char *raw )
943 while ( raw[j] != '\0' ) {
945 debugBelch("%c", unZcode(raw[j+1]));
948 debugBelch("%c", unZcode(raw[j+1]));
954 /* --------------------------------------------------------------------------
955 * Symbol table loading
956 * ------------------------------------------------------------------------*/
958 /* Causing linking trouble on Win32 plats, so I'm
959 disabling this for now.
965 /* Fairly ad-hoc piece of code that seems to filter out a lot of
966 * rubbish like the obj-splitting symbols
969 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
972 /* ToDo: make this work on BFD */
973 int tp = type & N_TYPE;
974 if (tp == N_TEXT || tp == N_DATA) {
975 return (name[0] == '_' && name[1] != '_');
981 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
982 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
989 extern void DEBUG_LoadSymbols( char *name )
995 abfd = bfd_openr(name, "default");
997 barf("can't open executable %s to get symbol table", name);
999 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1004 long storage_needed;
1005 asymbol **symbol_table;
1006 long number_of_symbols;
1007 long num_real_syms = 0;
1010 storage_needed = bfd_get_symtab_upper_bound (abfd);
1012 if (storage_needed < 0) {
1013 barf("can't read symbol table");
1016 if (storage_needed == 0) {
1017 debugBelch("no storage needed");
1020 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1022 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1024 if (number_of_symbols < 0) {
1025 barf("can't canonicalise symbol table");
1028 for( i = 0; i != number_of_symbols; ++i ) {
1030 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1031 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1032 if (isReal(info.type, info.name)) {
1037 IF_DEBUG(interpreter,
1038 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1039 number_of_symbols, num_real_syms)
1042 reset_table( num_real_syms );
1044 for( i = 0; i != number_of_symbols; ++i ) {
1046 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1047 if (isReal(info.type, info.name)) {
1048 insert( info.value, info.name );
1052 stgFree(symbol_table);
1057 #else /* HAVE_BFD_H */
1059 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1064 #endif /* HAVE_BFD_H */
1066 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1071 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
1074 for (; bd; bd = bd->link) {
1076 for (q = bd->start; q < bd->free; q++) {
1077 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
1080 while (HEAP_ALLOCED((StgPtr)*r) || !LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1083 debugBelch("%p = ", r);
1084 printClosure((StgClosure *)r);
1096 findPtr(P_ p, int follow)
1100 #if defined(__GNUC__)
1101 const int arr_size = 1024;
1103 #define arr_size 1024
1105 StgPtr arr[arr_size];
1109 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1110 for (s = 0; s < generations[g].n_steps; s++) {
1111 bd = generations[g].steps[s].blocks;
1112 i = findPtrBlocks(p,bd,arr,arr_size,i);
1113 bd = generations[g].steps[s].large_objects;
1114 i = findPtrBlocks(p,bd,arr,arr_size,i);
1115 if (i >= arr_size) return;
1118 if (follow && i == 1) {
1119 debugBelch("-->\n");
1124 /* prettyPrintClosure() is for printing out a closure using the data constructor
1125 names found in the info tables. Closures are printed in a fashion that resembles
1126 their Haskell representation. Useful during debugging.
1128 Todo: support for more closure types, and support for non pointer fields in the
1132 void prettyPrintClosure_ (StgClosure *);
1134 void prettyPrintClosure (StgClosure *obj)
1136 prettyPrintClosure_ (obj);
1140 void prettyPrintClosure_ (StgClosure *obj)
1143 StgConInfoTable *con_info;
1145 /* collapse any indirections */
1147 type = get_itbl(obj)->type;
1149 while (type == IND ||
1150 type == IND_STATIC ||
1151 type == IND_OLDGEN ||
1153 type == IND_OLDGEN_PERM)
1155 obj = ((StgInd *)obj)->indirectee;
1156 type = get_itbl(obj)->type;
1159 /* find the info table for this object */
1160 info = get_itbl(obj);
1162 /* determine what kind of object we have */
1165 /* full applications of data constructors */
1173 case CONSTR_NOCAF_STATIC:
1178 /* find the con_info for the constructor */
1179 con_info = get_con_itbl (obj);
1181 /* obtain the name of the constructor */
1182 descriptor = GET_CON_DESC(con_info);
1184 debugBelch ("(%s", descriptor);
1186 /* process the payload of the closure */
1187 /* we don't handle non pointers at the moment */
1188 for (i = 0; i < info->layout.payload.ptrs; i++)
1191 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1197 /* if it isn't a constructor then just print the closure type */
1200 debugBelch ("<%s>", info_type(obj));
1207 void printPtr( StgPtr p )
1209 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1212 void printObj( StgClosure *obj )
1214 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );