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");
312 debugBelch("ARR_WORDS(\"");
313 /* ToDo: we can't safely assume that this is a string!
314 for (i = 0; arrWordsGetChar(obj,i); ++i) {
315 putchar(arrWordsGetChar(obj,i));
317 for (i=0; i<((StgArrWords *)obj)->words; i++)
318 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
323 case MUT_ARR_PTRS_CLEAN:
324 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
327 case MUT_ARR_PTRS_DIRTY:
328 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
331 case MUT_ARR_PTRS_FROZEN:
332 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
338 StgMVar* mv = (StgMVar*)obj;
339 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
345 StgMutVar* mv = (StgMutVar*)obj;
346 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
352 StgMutVar* mv = (StgMutVar*)obj;
353 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
359 debugBelch(" key=%p value=%p finalizer=%p",
360 (StgPtr)(((StgWeak*)obj)->key),
361 (StgPtr)(((StgWeak*)obj)->value),
362 (StgPtr)(((StgWeak*)obj)->finalizer));
364 /* ToDo: chase 'link' ? */
368 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
373 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
379 debugBelch("BLOCKED_FETCH(");
380 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
381 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
386 debugBelch("FETCH_ME(");
387 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
392 debugBelch("FETCH_ME_BQ(");
393 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
394 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
399 #if defined(GRAN) || defined(PAR)
402 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
409 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
411 debugBelch("EVACUATED(");
412 printClosure((StgEvacuated*)obj->evacuee);
417 #if defined(PAR) && defined(DIST)
419 debugBelch("REMOTE_REF(");
420 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
426 //barf("printClosure %d",get_itbl(obj)->type);
427 debugBelch("*** printClosure: unknown type %d ****\n",
428 get_itbl(obj)->type );
429 barf("printClosure %d",get_itbl(obj)->type);
435 void printGraph( StgClosure *obj )
442 printStackObj( StgPtr sp )
444 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
446 StgClosure* c = (StgClosure*)(*sp);
447 printPtr((StgPtr)*sp);
448 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
449 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
451 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
452 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
454 if (c == (StgClosure*)&stg_ctoi_F1_info) {
455 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
457 if (c == (StgClosure*)&stg_ctoi_D1_info) {
458 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
460 if (c == (StgClosure*)&stg_ctoi_V_info) {
461 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
463 if (get_itbl(c)->type == BCO) {
464 debugBelch("\t\t\t");
465 debugBelch("BCO(...)\n");
468 debugBelch("\t\t\t");
469 printClosure ( (StgClosure*)(*sp));
478 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
484 for(i = 0; i < size; i++, bitmap >>= 1 ) {
485 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
486 if ((bitmap & 1) == 0) {
487 printPtr((P_)payload[i]);
490 debugBelch("Word# %lu\n", (lnat)payload[i]);
496 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
502 for (bmp=0; i < size; bmp++) {
503 StgWord bitmap = large_bitmap->bitmap[bmp];
505 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
506 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
507 if ((bitmap & 1) == 0) {
508 printPtr((P_)payload[i]);
511 debugBelch("Word# %lu\n", (lnat)payload[i]);
518 printStackChunk( StgPtr sp, StgPtr spBottom )
521 const StgInfoTable *info;
523 ASSERT(sp <= spBottom);
524 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
526 info = get_itbl((StgClosure *)sp);
528 switch (info->type) {
532 printObj((StgClosure*)sp);
536 printObj((StgClosure*)sp);
548 debugBelch("RET_DYN (%p)\n", r);
550 p = (P_)(r->payload);
551 printSmallBitmap(spBottom, sp,
552 RET_DYN_LIVENESS(r->liveness),
553 RET_DYN_BITMAP_SIZE);
554 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
556 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
557 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
558 debugBelch("Word# %ld\n", (long)*p);
562 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
563 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
571 debugBelch("RET_SMALL (%p)\n", info);
572 bitmap = info->layout.bitmap;
573 printSmallBitmap(spBottom, sp+1,
574 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
580 bco = ((StgBCO *)sp[1]);
582 debugBelch("RET_BCO (%p)\n", sp);
583 printLargeBitmap(spBottom, sp+2,
584 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
593 StgFunInfoTable *fun_info;
597 ret_fun = (StgRetFun *)sp;
598 fun_info = get_fun_itbl(ret_fun->fun);
599 size = ret_fun->size;
600 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
601 switch (fun_info->f.fun_type) {
603 printSmallBitmap(spBottom, sp+2,
604 BITMAP_BITS(fun_info->f.b.bitmap),
605 BITMAP_SIZE(fun_info->f.b.bitmap));
608 printLargeBitmap(spBottom, sp+2,
609 GET_FUN_LARGE_BITMAP(fun_info),
610 GET_FUN_LARGE_BITMAP(fun_info)->size);
613 printSmallBitmap(spBottom, sp+2,
614 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
615 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
622 debugBelch("unknown object %d\n", info->type);
623 barf("printStackChunk");
628 void printTSO( StgTSO *tso )
630 printStackChunk( tso->sp, tso->stack+tso->stack_size);
633 /* -----------------------------------------------------------------------------
636 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
637 -------------------------------------------------------------------------- */
639 static char *closure_type_names[] = {
648 "CONSTR_NOCAF_STATIC",
688 "MUT_ARR_PTRS_CLEAN",
689 "MUT_ARR_PTRS_DIRTY",
690 "MUT_ARR_PTRS_FROZEN",
705 "INVARIANT_CHECK_QUEUE",
716 info_type(StgClosure *closure){
717 return closure_type_names[get_itbl(closure)->type];
721 info_type_by_ip(StgInfoTable *ip){
722 return closure_type_names[ip->type];
726 info_hdr_type(StgClosure *closure, char *res){
727 strcpy(res,closure_type_names[get_itbl(closure)->type]);
730 /* --------------------------------------------------------------------------
731 * Address printing code
733 * Uses symbol table in (unstripped executable)
734 * ------------------------------------------------------------------------*/
736 /* --------------------------------------------------------------------------
737 * Simple lookup table
739 * Current implementation is pretty dumb!
740 * ------------------------------------------------------------------------*/
747 static nat table_size;
748 static struct entry* table;
751 static nat max_table_size;
753 static void reset_table( int size )
755 max_table_size = size;
757 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
760 static void prepare_table( void )
762 /* Could sort it... */
765 static void insert( StgWord value, const char *name )
767 if ( table_size >= max_table_size ) {
768 barf( "Symbol table overflow\n" );
770 table[table_size].value = value;
771 table[table_size].name = name;
772 table_size = table_size + 1;
777 static rtsBool lookup_name( char *name, StgWord *result )
780 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
782 if (i < table_size) {
783 *result = table[i].value;
791 /* Code from somewhere inside GHC (circa 1994)
793 * "std"++xs -> "Zstd"++xs
794 * char_to_c 'Z' = "ZZ"
795 * char_to_c '&' = "Za"
796 * char_to_c '|' = "Zb"
797 * char_to_c ':' = "Zc"
798 * char_to_c '/' = "Zd"
799 * char_to_c '=' = "Ze"
800 * char_to_c '>' = "Zg"
801 * char_to_c '#' = "Zh"
802 * char_to_c '<' = "Zl"
803 * char_to_c '-' = "Zm"
804 * char_to_c '!' = "Zn"
805 * char_to_c '.' = "Zo"
806 * char_to_c '+' = "Zp"
807 * char_to_c '\'' = "Zq"
808 * char_to_c '*' = "Zt"
809 * char_to_c '_' = "Zu"
810 * char_to_c c = "Z" ++ show (ord c)
812 static char unZcode( char ch )
815 case 'a' : return ('&');
816 case 'b' : return ('|');
817 case 'c' : return (':');
818 case 'd' : return ('/');
819 case 'e' : return ('=');
820 case 'g' : return ('>');
821 case 'h' : return ('#');
822 case 'l' : return ('<');
823 case 'm' : return ('-');
824 case 'n' : return ('!');
825 case 'o' : return ('.');
826 case 'p' : return ('+');
827 case 'q' : return ('\'');
828 case 't' : return ('*');
829 case 'u' : return ('_');
831 case '\0' : return ('Z');
832 default : return (ch);
837 /* Precondition: out big enough to handle output (about twice length of in) */
838 static void enZcode( char *in, char *out )
844 for( i = 0; in[i] != '\0'; ++i ) {
919 const char *lookupGHCName( void *addr )
922 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
924 if (i < table_size) {
925 return table[i].name;
931 static void printZcoded( const char *raw )
935 while ( raw[j] != '\0' ) {
937 debugBelch("%c", unZcode(raw[j+1]));
940 debugBelch("%c", unZcode(raw[j+1]));
946 /* --------------------------------------------------------------------------
947 * Symbol table loading
948 * ------------------------------------------------------------------------*/
950 /* Causing linking trouble on Win32 plats, so I'm
951 disabling this for now.
957 /* Fairly ad-hoc piece of code that seems to filter out a lot of
958 * rubbish like the obj-splitting symbols
961 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
964 /* ToDo: make this work on BFD */
965 int tp = type & N_TYPE;
966 if (tp == N_TEXT || tp == N_DATA) {
967 return (name[0] == '_' && name[1] != '_');
973 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
974 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
981 extern void DEBUG_LoadSymbols( char *name )
987 abfd = bfd_openr(name, "default");
989 barf("can't open executable %s to get symbol table", name);
991 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
997 asymbol **symbol_table;
998 long number_of_symbols;
999 long num_real_syms = 0;
1002 storage_needed = bfd_get_symtab_upper_bound (abfd);
1004 if (storage_needed < 0) {
1005 barf("can't read symbol table");
1008 if (storage_needed == 0) {
1009 debugBelch("no storage needed");
1012 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1014 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1016 if (number_of_symbols < 0) {
1017 barf("can't canonicalise symbol table");
1020 for( i = 0; i != number_of_symbols; ++i ) {
1022 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1023 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1024 if (isReal(info.type, info.name)) {
1029 IF_DEBUG(interpreter,
1030 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1031 number_of_symbols, num_real_syms)
1034 reset_table( num_real_syms );
1036 for( i = 0; i != number_of_symbols; ++i ) {
1038 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1039 if (isReal(info.type, info.name)) {
1040 insert( info.value, info.name );
1044 stgFree(symbol_table);
1049 #else /* HAVE_BFD_H */
1051 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1056 #endif /* HAVE_BFD_H */
1058 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1063 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
1066 for (; bd; bd = bd->link) {
1068 for (q = bd->start; q < bd->free; q++) {
1069 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
1072 while (HEAP_ALLOCED((StgPtr)*r) || !LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1075 debugBelch("%p = ", r);
1076 printClosure((StgClosure *)r);
1088 findPtr(P_ p, int follow)
1092 #if defined(__GNUC__)
1093 const int arr_size = 1024;
1095 #define arr_size 1024
1097 StgPtr arr[arr_size];
1101 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1102 for (s = 0; s < generations[g].n_steps; s++) {
1103 bd = generations[g].steps[s].blocks;
1104 i = findPtrBlocks(p,bd,arr,arr_size,i);
1105 bd = generations[g].steps[s].large_objects;
1106 i = findPtrBlocks(p,bd,arr,arr_size,i);
1107 if (i >= arr_size) return;
1110 if (follow && i == 1) {
1111 debugBelch("-->\n");
1116 /* prettyPrintClosure() is for printing out a closure using the data constructor
1117 names found in the info tables. Closures are printed in a fashion that resembles
1118 their Haskell representation. Useful during debugging.
1120 Todo: support for more closure types, and support for non pointer fields in the
1124 void prettyPrintClosure_ (StgClosure *);
1126 void prettyPrintClosure (StgClosure *obj)
1128 prettyPrintClosure_ (obj);
1132 void prettyPrintClosure_ (StgClosure *obj)
1135 StgConInfoTable *con_info;
1137 /* collapse any indirections */
1139 type = get_itbl(obj)->type;
1141 while (type == IND ||
1142 type == IND_STATIC ||
1143 type == IND_OLDGEN ||
1145 type == IND_OLDGEN_PERM)
1147 obj = ((StgInd *)obj)->indirectee;
1148 type = get_itbl(obj)->type;
1151 /* find the info table for this object */
1152 info = get_itbl(obj);
1154 /* determine what kind of object we have */
1157 /* full applications of data constructors */
1165 case CONSTR_NOCAF_STATIC:
1170 /* find the con_info for the constructor */
1171 con_info = get_con_itbl (obj);
1173 /* obtain the name of the constructor */
1174 descriptor = GET_CON_DESC(con_info);
1176 debugBelch ("(%s", descriptor);
1178 /* process the payload of the closure */
1179 /* we don't handle non pointers at the moment */
1180 for (i = 0; i < info->layout.payload.ptrs; i++)
1183 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1189 /* if it isn't a constructor then just print the closure type */
1192 debugBelch ("<%s>", info_type(obj));
1199 void printPtr( StgPtr p )
1201 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1204 void printObj( StgClosure *obj )
1206 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );