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", ((StgArrWords *)obj)->payload[i]);
336 debugBelch("MUT_ARR_PTRS(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
339 case MUT_ARR_PTRS_FROZEN:
340 #if !defined(XMLAMBDA)
341 debugBelch("MUT_ARR_PTRS_FROZEN(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
345 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
347 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
349 debugBelch("Row<%i>(",p->ptrs);
350 for (i = 0; i < p->ptrs; ++i) {
351 if (i > 0) debugBelch(", ");
352 printPtr((StgPtr)(p->payload[i]));
361 StgMVar* mv = (StgMVar*)obj;
362 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
368 StgMutVar* mv = (StgMutVar*)obj;
369 debugBelch("MUT_VAR(var=%p)\n", mv->var);
375 debugBelch(" key=%p value=%p finalizer=%p",
376 (StgPtr)(((StgWeak*)obj)->key),
377 (StgPtr)(((StgWeak*)obj)->value),
378 (StgPtr)(((StgWeak*)obj)->finalizer));
380 /* ToDo: chase 'link' ? */
384 debugBelch("FOREIGN(");
385 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
390 debugBelch("STABLE_NAME(%ld)\n", ((StgStableName*)obj)->sn);
395 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
401 debugBelch("BLOCKED_FETCH(");
402 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
403 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
408 debugBelch("FETCH_ME(");
409 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
414 debugBelch("FETCH_ME_BQ(");
415 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
416 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
421 #if defined(GRAN) || defined(PAR)
424 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
431 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
433 debugBelch("EVACUATED(");
434 printClosure((StgEvacuated*)obj->evacuee);
439 #if defined(PAR) && defined(DIST)
441 debugBelch("REMOTE_REF(");
442 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
448 //barf("printClosure %d",get_itbl(obj)->type);
449 debugBelch("*** printClosure: unknown type %d ****\n",
450 get_itbl(obj)->type );
451 barf("printClosure %d",get_itbl(obj)->type);
457 void printGraph( StgClosure *obj )
464 printStackObj( StgPtr sp )
466 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
468 StgClosure* c = (StgClosure*)(*sp);
469 printPtr((StgPtr)*sp);
470 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
471 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
473 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
474 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
476 if (c == (StgClosure*)&stg_ctoi_F1_info) {
477 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
479 if (c == (StgClosure*)&stg_ctoi_D1_info) {
480 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
482 if (c == (StgClosure*)&stg_ctoi_V_info) {
483 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
485 if (get_itbl(c)->type == BCO) {
486 debugBelch("\t\t\t");
487 debugBelch("BCO(...)\n");
490 debugBelch("\t\t\t");
491 printClosure ( (StgClosure*)(*sp));
500 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
506 for(i = 0; i < size; i++, bitmap >>= 1 ) {
507 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
508 if ((bitmap & 1) == 0) {
509 printPtr((P_)payload[i]);
512 debugBelch("Word# %ld\n", payload[i]);
518 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
524 for (bmp=0; i < size; bmp++) {
525 StgWord bitmap = large_bitmap->bitmap[bmp];
527 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
528 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
529 if ((bitmap & 1) == 0) {
530 printPtr((P_)payload[i]);
533 debugBelch("Word# %ld\n", payload[i]);
540 printStackChunk( StgPtr sp, StgPtr spBottom )
543 const StgInfoTable *info;
545 ASSERT(sp <= spBottom);
546 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
548 info = get_itbl((StgClosure *)sp);
550 switch (info->type) {
555 printObj((StgClosure*)sp);
567 debugBelch("RET_DYN (%p)\n", r);
569 p = (P_)(r->payload);
570 printSmallBitmap(spBottom, sp,
571 RET_DYN_LIVENESS(r->liveness),
572 RET_DYN_BITMAP_SIZE);
573 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
575 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
576 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
577 debugBelch("Word# %ld\n", (long)*p);
581 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
582 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
591 debugBelch("RET_SMALL (%p)\n", sp);
592 bitmap = info->layout.bitmap;
593 printSmallBitmap(spBottom, sp+1,
594 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
600 bco = ((StgBCO *)sp[1]);
602 debugBelch("RET_BCO (%p)\n", sp);
603 printLargeBitmap(spBottom, sp+2,
604 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
614 StgFunInfoTable *fun_info;
618 ret_fun = (StgRetFun *)sp;
619 fun_info = get_fun_itbl(ret_fun->fun);
620 size = ret_fun->size;
621 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
622 switch (fun_info->f.fun_type) {
624 printSmallBitmap(spBottom, sp+1,
625 BITMAP_BITS(fun_info->f.b.bitmap),
626 BITMAP_SIZE(fun_info->f.b.bitmap));
629 printLargeBitmap(spBottom, sp+2,
630 GET_FUN_LARGE_BITMAP(fun_info),
631 GET_FUN_LARGE_BITMAP(fun_info)->size);
634 printSmallBitmap(spBottom, sp+1,
635 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
636 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
643 debugBelch("unknown object %d\n", info->type);
644 barf("printStackChunk");
649 void printTSO( StgTSO *tso )
651 printStackChunk( tso->sp, tso->stack+tso->stack_size);
654 /* -----------------------------------------------------------------------------
657 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
658 -------------------------------------------------------------------------- */
660 static char *closure_type_names[] = {
671 "CONSTR_NOCAF_STATIC",
714 "MUT_ARR_PTRS_FROZEN",
737 info_type(StgClosure *closure){
738 return closure_type_names[get_itbl(closure)->type];
742 info_type_by_ip(StgInfoTable *ip){
743 return closure_type_names[ip->type];
747 info_hdr_type(StgClosure *closure, char *res){
748 strcpy(res,closure_type_names[get_itbl(closure)->type]);
751 /* --------------------------------------------------------------------------
752 * Address printing code
754 * Uses symbol table in (unstripped executable)
755 * ------------------------------------------------------------------------*/
757 /* --------------------------------------------------------------------------
758 * Simple lookup table
760 * Current implementation is pretty dumb!
761 * ------------------------------------------------------------------------*/
768 static nat table_size;
769 static struct entry* table;
772 static nat max_table_size;
774 static void reset_table( int size )
776 max_table_size = size;
778 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
781 static void prepare_table( void )
783 /* Could sort it... */
786 static void insert( unsigned value, const char *name )
788 if ( table_size >= max_table_size ) {
789 barf( "Symbol table overflow\n" );
791 table[table_size].value = value;
792 table[table_size].name = name;
793 table_size = table_size + 1;
798 static rtsBool lookup_name( char *name, unsigned *result )
801 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
803 if (i < table_size) {
804 *result = table[i].value;
812 /* Code from somewhere inside GHC (circa 1994)
814 * "std"++xs -> "Zstd"++xs
815 * char_to_c 'Z' = "ZZ"
816 * char_to_c '&' = "Za"
817 * char_to_c '|' = "Zb"
818 * char_to_c ':' = "Zc"
819 * char_to_c '/' = "Zd"
820 * char_to_c '=' = "Ze"
821 * char_to_c '>' = "Zg"
822 * char_to_c '#' = "Zh"
823 * char_to_c '<' = "Zl"
824 * char_to_c '-' = "Zm"
825 * char_to_c '!' = "Zn"
826 * char_to_c '.' = "Zo"
827 * char_to_c '+' = "Zp"
828 * char_to_c '\'' = "Zq"
829 * char_to_c '*' = "Zt"
830 * char_to_c '_' = "Zu"
831 * char_to_c c = "Z" ++ show (ord c)
833 static char unZcode( char ch )
836 case 'a' : return ('&');
837 case 'b' : return ('|');
838 case 'c' : return (':');
839 case 'd' : return ('/');
840 case 'e' : return ('=');
841 case 'g' : return ('>');
842 case 'h' : return ('#');
843 case 'l' : return ('<');
844 case 'm' : return ('-');
845 case 'n' : return ('!');
846 case 'o' : return ('.');
847 case 'p' : return ('+');
848 case 'q' : return ('\'');
849 case 't' : return ('*');
850 case 'u' : return ('_');
852 case '\0' : return ('Z');
853 default : return (ch);
858 /* Precondition: out big enough to handle output (about twice length of in) */
859 static void enZcode( char *in, char *out )
865 for( i = 0; in[i] != '\0'; ++i ) {
940 const char *lookupGHCName( void *addr )
943 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
945 if (i < table_size) {
946 return table[i].name;
952 static void printZcoded( const char *raw )
956 while ( raw[j] != '\0' ) {
958 debugBelch("%c", unZcode(raw[j+1]));
961 debugBelch("%c", unZcode(raw[j+1]));
967 /* --------------------------------------------------------------------------
968 * Symbol table loading
969 * ------------------------------------------------------------------------*/
971 /* Causing linking trouble on Win32 plats, so I'm
972 disabling this for now.
978 /* Fairly ad-hoc piece of code that seems to filter out a lot of
979 * rubbish like the obj-splitting symbols
982 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
985 /* ToDo: make this work on BFD */
986 int tp = type & N_TYPE;
987 if (tp == N_TEXT || tp == N_DATA) {
988 return (name[0] == '_' && name[1] != '_');
994 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
995 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
1002 extern void DEBUG_LoadSymbols( char *name )
1008 abfd = bfd_openr(name, "default");
1010 barf("can't open executable %s to get symbol table", name);
1012 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1017 long storage_needed;
1018 asymbol **symbol_table;
1019 long number_of_symbols;
1020 long num_real_syms = 0;
1023 storage_needed = bfd_get_symtab_upper_bound (abfd);
1025 if (storage_needed < 0) {
1026 barf("can't read symbol table");
1029 if (storage_needed == 0) {
1030 debugBelch("no storage needed");
1033 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1035 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1037 if (number_of_symbols < 0) {
1038 barf("can't canonicalise symbol table");
1041 for( i = 0; i != number_of_symbols; ++i ) {
1043 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1044 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1045 if (isReal(info.type, info.name)) {
1050 IF_DEBUG(interpreter,
1051 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1052 number_of_symbols, num_real_syms)
1055 reset_table( num_real_syms );
1057 for( i = 0; i != number_of_symbols; ++i ) {
1059 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1060 if (isReal(info.type, info.name)) {
1061 insert( info.value, info.name );
1065 stgFree(symbol_table);
1070 #else /* HAVE_BFD_H */
1072 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1077 #endif /* HAVE_BFD_H */
1079 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1082 findPtr(P_ p, int follow)
1087 #if defined(__GNUC__)
1088 const int arr_size = 1024;
1090 #define arr_size 1024
1092 StgPtr arr[arr_size];
1095 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1096 for (s = 0; s < generations[g].n_steps; s++) {
1097 if (RtsFlags.GcFlags.generations == 1) {
1098 bd = generations[g].steps[s].to_blocks;
1100 bd = generations[g].steps[s].blocks;
1102 for (; bd; bd = bd->link) {
1103 for (q = bd->start; q < bd->free; q++) {
1107 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1110 debugBelch("%p = ", r);
1111 printClosure((StgClosure *)r);
1121 if (follow && i == 1) {
1122 debugBelch("-->\n");
1128 void printPtr( StgPtr p )
1130 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1133 void printObj( StgClosure *obj )
1135 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );