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:
140 debugBelch("%s(", info->prof.closure_desc);
141 debugBelch("%s", obj->header.prof.ccs->cc->label);
143 debugBelch("CONSTR(");
144 printPtr((StgPtr)obj->header.info);
145 debugBelch("(tag=%d)",info->srt_bitmap);
147 for (i = 0; i < info->layout.payload.ptrs; ++i) {
149 printPtr((StgPtr)obj->payload[i]);
151 for (j = 0; j < info->layout.payload.nptrs; ++j) {
152 debugBelch(", %p#", obj->payload[i+j]);
159 case FUN_1_0: case FUN_0_1:
160 case FUN_1_1: case FUN_0_2: case FUN_2_0:
162 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
163 printPtr((StgPtr)obj->header.info);
165 debugBelch(", %s", obj->header.prof.ccs->cc->label);
167 printStdObjPayload(obj);
171 case THUNK_1_0: case THUNK_0_1:
172 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
174 /* ToDo: will this work for THUNK_STATIC too? */
176 printThunkObject((StgThunk *)obj,info->prof.closure_desc);
178 printThunkObject((StgThunk *)obj,"THUNK");
183 printStdObjHdr(obj, "THUNK_SELECTOR");
184 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
188 disassemble( (StgBCO*)obj );
193 StgAP* ap = stgCast(StgAP*,obj);
195 debugBelch("AP("); printPtr((StgPtr)ap->fun);
196 for (i = 0; i < ap->n_args; ++i) {
198 printPtr((P_)ap->payload[i]);
206 StgPAP* pap = stgCast(StgPAP*,obj);
208 debugBelch("PAP/%d(",pap->arity);
209 printPtr((StgPtr)pap->fun);
210 for (i = 0; i < pap->n_args; ++i) {
212 printPtr((StgPtr)pap->payload[i]);
220 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
222 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
223 for (i = 0; i < ap->size; ++i) {
225 printPtr((P_)ap->payload[i]);
233 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
238 debugBelch("IND_OLDGEN(");
239 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
245 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
249 case IND_OLDGEN_PERM:
250 debugBelch("IND_OLDGEN_PERM(");
251 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
256 debugBelch("IND_STATIC(");
257 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
261 /* Cannot happen -- use default case.
273 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
274 debugBelch("UPDATE_FRAME(");
275 printPtr((StgPtr)GET_INFO(u));
277 printPtr((StgPtr)u->updatee);
284 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
285 debugBelch("CATCH_FRAME(");
286 printPtr((StgPtr)GET_INFO(u));
288 printPtr((StgPtr)u->handler);
295 StgStopFrame* u = stgCast(StgStopFrame*,obj);
296 debugBelch("STOP_FRAME(");
297 printPtr((StgPtr)GET_INFO(u));
303 debugBelch("CAF_BH");
311 debugBelch("SE_BH\n");
314 case SE_CAF_BLACKHOLE:
315 debugBelch("SE_CAF_BH\n");
321 debugBelch("ARR_WORDS(\"");
322 /* ToDo: we can't safely assume that this is a string!
323 for (i = 0; arrWordsGetChar(obj,i); ++i) {
324 putchar(arrWordsGetChar(obj,i));
326 for (i=0; i<((StgArrWords *)obj)->words; i++)
327 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
332 case MUT_ARR_PTRS_CLEAN:
333 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
336 case MUT_ARR_PTRS_DIRTY:
337 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
340 case MUT_ARR_PTRS_FROZEN:
341 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);
580 debugBelch("RET_SMALL (%p)\n", info);
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, fun_info->f.fun_type);
611 switch (fun_info->f.fun_type) {
613 printSmallBitmap(spBottom, sp+2,
614 BITMAP_BITS(fun_info->f.b.bitmap),
615 BITMAP_SIZE(fun_info->f.b.bitmap));
618 printLargeBitmap(spBottom, sp+2,
619 GET_FUN_LARGE_BITMAP(fun_info),
620 GET_FUN_LARGE_BITMAP(fun_info)->size);
623 printSmallBitmap(spBottom, sp+2,
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[] = {
658 "CONSTR_NOCAF_STATIC",
700 "MUT_ARR_PTRS_CLEAN",
701 "MUT_ARR_PTRS_DIRTY",
702 "MUT_ARR_PTRS_FROZEN",
717 "INVARIANT_CHECK_QUEUE",
728 info_type(StgClosure *closure){
729 return closure_type_names[get_itbl(closure)->type];
733 info_type_by_ip(StgInfoTable *ip){
734 return closure_type_names[ip->type];
738 info_hdr_type(StgClosure *closure, char *res){
739 strcpy(res,closure_type_names[get_itbl(closure)->type]);
742 /* --------------------------------------------------------------------------
743 * Address printing code
745 * Uses symbol table in (unstripped executable)
746 * ------------------------------------------------------------------------*/
748 /* --------------------------------------------------------------------------
749 * Simple lookup table
751 * Current implementation is pretty dumb!
752 * ------------------------------------------------------------------------*/
759 static nat table_size;
760 static struct entry* table;
763 static nat max_table_size;
765 static void reset_table( int size )
767 max_table_size = size;
769 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
772 static void prepare_table( void )
774 /* Could sort it... */
777 static void insert( StgWord value, const char *name )
779 if ( table_size >= max_table_size ) {
780 barf( "Symbol table overflow\n" );
782 table[table_size].value = value;
783 table[table_size].name = name;
784 table_size = table_size + 1;
789 static rtsBool lookup_name( char *name, StgWord *result )
792 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
794 if (i < table_size) {
795 *result = table[i].value;
803 /* Code from somewhere inside GHC (circa 1994)
805 * "std"++xs -> "Zstd"++xs
806 * char_to_c 'Z' = "ZZ"
807 * char_to_c '&' = "Za"
808 * char_to_c '|' = "Zb"
809 * char_to_c ':' = "Zc"
810 * char_to_c '/' = "Zd"
811 * char_to_c '=' = "Ze"
812 * char_to_c '>' = "Zg"
813 * char_to_c '#' = "Zh"
814 * char_to_c '<' = "Zl"
815 * char_to_c '-' = "Zm"
816 * char_to_c '!' = "Zn"
817 * char_to_c '.' = "Zo"
818 * char_to_c '+' = "Zp"
819 * char_to_c '\'' = "Zq"
820 * char_to_c '*' = "Zt"
821 * char_to_c '_' = "Zu"
822 * char_to_c c = "Z" ++ show (ord c)
824 static char unZcode( char ch )
827 case 'a' : return ('&');
828 case 'b' : return ('|');
829 case 'c' : return (':');
830 case 'd' : return ('/');
831 case 'e' : return ('=');
832 case 'g' : return ('>');
833 case 'h' : return ('#');
834 case 'l' : return ('<');
835 case 'm' : return ('-');
836 case 'n' : return ('!');
837 case 'o' : return ('.');
838 case 'p' : return ('+');
839 case 'q' : return ('\'');
840 case 't' : return ('*');
841 case 'u' : return ('_');
843 case '\0' : return ('Z');
844 default : return (ch);
849 /* Precondition: out big enough to handle output (about twice length of in) */
850 static void enZcode( char *in, char *out )
856 for( i = 0; in[i] != '\0'; ++i ) {
931 const char *lookupGHCName( void *addr )
934 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
936 if (i < table_size) {
937 return table[i].name;
943 static void printZcoded( const char *raw )
947 while ( raw[j] != '\0' ) {
949 debugBelch("%c", unZcode(raw[j+1]));
952 debugBelch("%c", unZcode(raw[j+1]));
958 /* --------------------------------------------------------------------------
959 * Symbol table loading
960 * ------------------------------------------------------------------------*/
962 /* Causing linking trouble on Win32 plats, so I'm
963 disabling this for now.
969 /* Fairly ad-hoc piece of code that seems to filter out a lot of
970 * rubbish like the obj-splitting symbols
973 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
976 /* ToDo: make this work on BFD */
977 int tp = type & N_TYPE;
978 if (tp == N_TEXT || tp == N_DATA) {
979 return (name[0] == '_' && name[1] != '_');
985 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
986 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
993 extern void DEBUG_LoadSymbols( char *name )
999 abfd = bfd_openr(name, "default");
1001 barf("can't open executable %s to get symbol table", name);
1003 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1008 long storage_needed;
1009 asymbol **symbol_table;
1010 long number_of_symbols;
1011 long num_real_syms = 0;
1014 storage_needed = bfd_get_symtab_upper_bound (abfd);
1016 if (storage_needed < 0) {
1017 barf("can't read symbol table");
1020 if (storage_needed == 0) {
1021 debugBelch("no storage needed");
1024 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1026 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1028 if (number_of_symbols < 0) {
1029 barf("can't canonicalise symbol table");
1032 for( i = 0; i != number_of_symbols; ++i ) {
1034 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1035 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1036 if (isReal(info.type, info.name)) {
1041 IF_DEBUG(interpreter,
1042 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1043 number_of_symbols, num_real_syms)
1046 reset_table( num_real_syms );
1048 for( i = 0; i != number_of_symbols; ++i ) {
1050 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1051 if (isReal(info.type, info.name)) {
1052 insert( info.value, info.name );
1056 stgFree(symbol_table);
1061 #else /* HAVE_BFD_H */
1063 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1068 #endif /* HAVE_BFD_H */
1070 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1073 findPtr(P_ p, int follow)
1078 #if defined(__GNUC__)
1079 const int arr_size = 1024;
1081 #define arr_size 1024
1083 StgPtr arr[arr_size];
1086 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1087 for (s = 0; s < generations[g].n_steps; s++) {
1088 bd = generations[g].steps[s].blocks;
1089 for (; bd; bd = bd->link) {
1090 for (q = bd->start; q < bd->free; q++) {
1094 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1097 debugBelch("%p = ", r);
1098 printClosure((StgClosure *)r);
1108 if (follow && i == 1) {
1109 debugBelch("-->\n");
1115 void printPtr( StgPtr p )
1117 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1120 void printObj( StgClosure *obj )
1122 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );