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",
727 info_type(StgClosure *closure){
728 return closure_type_names[get_itbl(closure)->type];
732 info_type_by_ip(StgInfoTable *ip){
733 return closure_type_names[ip->type];
737 info_hdr_type(StgClosure *closure, char *res){
738 strcpy(res,closure_type_names[get_itbl(closure)->type]);
741 /* --------------------------------------------------------------------------
742 * Address printing code
744 * Uses symbol table in (unstripped executable)
745 * ------------------------------------------------------------------------*/
747 /* --------------------------------------------------------------------------
748 * Simple lookup table
750 * Current implementation is pretty dumb!
751 * ------------------------------------------------------------------------*/
758 static nat table_size;
759 static struct entry* table;
762 static nat max_table_size;
764 static void reset_table( int size )
766 max_table_size = size;
768 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
771 static void prepare_table( void )
773 /* Could sort it... */
776 static void insert( StgWord value, const char *name )
778 if ( table_size >= max_table_size ) {
779 barf( "Symbol table overflow\n" );
781 table[table_size].value = value;
782 table[table_size].name = name;
783 table_size = table_size + 1;
788 static rtsBool lookup_name( char *name, StgWord *result )
791 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
793 if (i < table_size) {
794 *result = table[i].value;
802 /* Code from somewhere inside GHC (circa 1994)
804 * "std"++xs -> "Zstd"++xs
805 * char_to_c 'Z' = "ZZ"
806 * char_to_c '&' = "Za"
807 * char_to_c '|' = "Zb"
808 * char_to_c ':' = "Zc"
809 * char_to_c '/' = "Zd"
810 * char_to_c '=' = "Ze"
811 * char_to_c '>' = "Zg"
812 * char_to_c '#' = "Zh"
813 * char_to_c '<' = "Zl"
814 * char_to_c '-' = "Zm"
815 * char_to_c '!' = "Zn"
816 * char_to_c '.' = "Zo"
817 * char_to_c '+' = "Zp"
818 * char_to_c '\'' = "Zq"
819 * char_to_c '*' = "Zt"
820 * char_to_c '_' = "Zu"
821 * char_to_c c = "Z" ++ show (ord c)
823 static char unZcode( char ch )
826 case 'a' : return ('&');
827 case 'b' : return ('|');
828 case 'c' : return (':');
829 case 'd' : return ('/');
830 case 'e' : return ('=');
831 case 'g' : return ('>');
832 case 'h' : return ('#');
833 case 'l' : return ('<');
834 case 'm' : return ('-');
835 case 'n' : return ('!');
836 case 'o' : return ('.');
837 case 'p' : return ('+');
838 case 'q' : return ('\'');
839 case 't' : return ('*');
840 case 'u' : return ('_');
842 case '\0' : return ('Z');
843 default : return (ch);
848 /* Precondition: out big enough to handle output (about twice length of in) */
849 static void enZcode( char *in, char *out )
855 for( i = 0; in[i] != '\0'; ++i ) {
930 const char *lookupGHCName( void *addr )
933 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
935 if (i < table_size) {
936 return table[i].name;
942 static void printZcoded( const char *raw )
946 while ( raw[j] != '\0' ) {
948 debugBelch("%c", unZcode(raw[j+1]));
951 debugBelch("%c", unZcode(raw[j+1]));
957 /* --------------------------------------------------------------------------
958 * Symbol table loading
959 * ------------------------------------------------------------------------*/
961 /* Causing linking trouble on Win32 plats, so I'm
962 disabling this for now.
968 /* Fairly ad-hoc piece of code that seems to filter out a lot of
969 * rubbish like the obj-splitting symbols
972 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
975 /* ToDo: make this work on BFD */
976 int tp = type & N_TYPE;
977 if (tp == N_TEXT || tp == N_DATA) {
978 return (name[0] == '_' && name[1] != '_');
984 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
985 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
992 extern void DEBUG_LoadSymbols( char *name )
998 abfd = bfd_openr(name, "default");
1000 barf("can't open executable %s to get symbol table", name);
1002 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1007 long storage_needed;
1008 asymbol **symbol_table;
1009 long number_of_symbols;
1010 long num_real_syms = 0;
1013 storage_needed = bfd_get_symtab_upper_bound (abfd);
1015 if (storage_needed < 0) {
1016 barf("can't read symbol table");
1019 if (storage_needed == 0) {
1020 debugBelch("no storage needed");
1023 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1025 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1027 if (number_of_symbols < 0) {
1028 barf("can't canonicalise symbol table");
1031 for( i = 0; i != number_of_symbols; ++i ) {
1033 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1034 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1035 if (isReal(info.type, info.name)) {
1040 IF_DEBUG(interpreter,
1041 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1042 number_of_symbols, num_real_syms)
1045 reset_table( num_real_syms );
1047 for( i = 0; i != number_of_symbols; ++i ) {
1049 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1050 if (isReal(info.type, info.name)) {
1051 insert( info.value, info.name );
1055 stgFree(symbol_table);
1060 #else /* HAVE_BFD_H */
1062 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1067 #endif /* HAVE_BFD_H */
1069 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1072 findPtr(P_ p, int follow)
1077 #if defined(__GNUC__)
1078 const int arr_size = 1024;
1080 #define arr_size 1024
1082 StgPtr arr[arr_size];
1085 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1086 for (s = 0; s < generations[g].n_steps; s++) {
1087 bd = generations[g].steps[s].blocks;
1088 for (; bd; bd = bd->link) {
1089 for (q = bd->start; q < bd->free; q++) {
1093 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1096 debugBelch("%p = ", r);
1097 printClosure((StgClosure *)r);
1107 if (follow && i == 1) {
1108 debugBelch("-->\n");
1114 void printPtr( StgPtr p )
1116 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1119 void printObj( StgClosure *obj )
1121 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );