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.
271 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
272 debugBelch("UPDATE_FRAME(");
273 printPtr((StgPtr)GET_INFO(u));
275 printPtr((StgPtr)u->updatee);
282 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
283 debugBelch("CATCH_FRAME(");
284 printPtr((StgPtr)GET_INFO(u));
286 printPtr((StgPtr)u->handler);
293 StgStopFrame* u = stgCast(StgStopFrame*,obj);
294 debugBelch("STOP_FRAME(");
295 printPtr((StgPtr)GET_INFO(u));
301 debugBelch("CAF_BH");
309 debugBelch("SE_BH\n");
312 case SE_CAF_BLACKHOLE:
313 debugBelch("SE_CAF_BH\n");
319 debugBelch("ARR_WORDS(\"");
320 /* ToDo: we can't safely assume that this is a string!
321 for (i = 0; arrWordsGetChar(obj,i); ++i) {
322 putchar(arrWordsGetChar(obj,i));
324 for (i=0; i<((StgArrWords *)obj)->words; i++)
325 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
330 case MUT_ARR_PTRS_CLEAN:
331 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
334 case MUT_ARR_PTRS_DIRTY:
335 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
338 case MUT_ARR_PTRS_FROZEN:
339 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
344 StgMVar* mv = (StgMVar*)obj;
345 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
351 StgMutVar* mv = (StgMutVar*)obj;
352 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
358 StgMutVar* mv = (StgMutVar*)obj;
359 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
365 debugBelch(" key=%p value=%p finalizer=%p",
366 (StgPtr)(((StgWeak*)obj)->key),
367 (StgPtr)(((StgWeak*)obj)->value),
368 (StgPtr)(((StgWeak*)obj)->finalizer));
370 /* ToDo: chase 'link' ? */
374 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
379 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
385 debugBelch("BLOCKED_FETCH(");
386 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
387 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
392 debugBelch("FETCH_ME(");
393 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
398 debugBelch("FETCH_ME_BQ(");
399 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
400 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
405 #if defined(GRAN) || defined(PAR)
408 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
415 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
417 debugBelch("EVACUATED(");
418 printClosure((StgEvacuated*)obj->evacuee);
423 #if defined(PAR) && defined(DIST)
425 debugBelch("REMOTE_REF(");
426 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
432 //barf("printClosure %d",get_itbl(obj)->type);
433 debugBelch("*** printClosure: unknown type %d ****\n",
434 get_itbl(obj)->type );
435 barf("printClosure %d",get_itbl(obj)->type);
441 void printGraph( StgClosure *obj )
448 printStackObj( StgPtr sp )
450 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
452 StgClosure* c = (StgClosure*)(*sp);
453 printPtr((StgPtr)*sp);
454 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
455 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
457 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
458 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
460 if (c == (StgClosure*)&stg_ctoi_F1_info) {
461 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
463 if (c == (StgClosure*)&stg_ctoi_D1_info) {
464 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
466 if (c == (StgClosure*)&stg_ctoi_V_info) {
467 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
469 if (get_itbl(c)->type == BCO) {
470 debugBelch("\t\t\t");
471 debugBelch("BCO(...)\n");
474 debugBelch("\t\t\t");
475 printClosure ( (StgClosure*)(*sp));
484 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
490 for(i = 0; i < size; i++, bitmap >>= 1 ) {
491 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
492 if ((bitmap & 1) == 0) {
493 printPtr((P_)payload[i]);
496 debugBelch("Word# %lu\n", (lnat)payload[i]);
502 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
508 for (bmp=0; i < size; bmp++) {
509 StgWord bitmap = large_bitmap->bitmap[bmp];
511 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
512 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
513 if ((bitmap & 1) == 0) {
514 printPtr((P_)payload[i]);
517 debugBelch("Word# %lu\n", (lnat)payload[i]);
524 printStackChunk( StgPtr sp, StgPtr spBottom )
527 const StgInfoTable *info;
529 ASSERT(sp <= spBottom);
530 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
532 info = get_itbl((StgClosure *)sp);
534 switch (info->type) {
538 printObj((StgClosure*)sp);
542 printObj((StgClosure*)sp);
554 debugBelch("RET_DYN (%p)\n", r);
556 p = (P_)(r->payload);
557 printSmallBitmap(spBottom, sp,
558 RET_DYN_LIVENESS(r->liveness),
559 RET_DYN_BITMAP_SIZE);
560 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
562 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
563 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
564 debugBelch("Word# %ld\n", (long)*p);
568 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
569 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
577 debugBelch("RET_SMALL (%p)\n", info);
578 bitmap = info->layout.bitmap;
579 printSmallBitmap(spBottom, sp+1,
580 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
586 bco = ((StgBCO *)sp[1]);
588 debugBelch("RET_BCO (%p)\n", sp);
589 printLargeBitmap(spBottom, sp+2,
590 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
599 StgFunInfoTable *fun_info;
603 ret_fun = (StgRetFun *)sp;
604 fun_info = get_fun_itbl(ret_fun->fun);
605 size = ret_fun->size;
606 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
607 switch (fun_info->f.fun_type) {
609 printSmallBitmap(spBottom, sp+2,
610 BITMAP_BITS(fun_info->f.b.bitmap),
611 BITMAP_SIZE(fun_info->f.b.bitmap));
614 printLargeBitmap(spBottom, sp+2,
615 GET_FUN_LARGE_BITMAP(fun_info),
616 GET_FUN_LARGE_BITMAP(fun_info)->size);
619 printSmallBitmap(spBottom, sp+2,
620 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
621 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
628 debugBelch("unknown object %d\n", info->type);
629 barf("printStackChunk");
634 void printTSO( StgTSO *tso )
636 printStackChunk( tso->sp, tso->stack+tso->stack_size);
639 /* -----------------------------------------------------------------------------
642 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
643 -------------------------------------------------------------------------- */
645 static char *closure_type_names[] = {
654 "CONSTR_NOCAF_STATIC",
694 "MUT_ARR_PTRS_CLEAN",
695 "MUT_ARR_PTRS_DIRTY",
696 "MUT_ARR_PTRS_FROZEN",
711 "INVARIANT_CHECK_QUEUE",
722 info_type(StgClosure *closure){
723 return closure_type_names[get_itbl(closure)->type];
727 info_type_by_ip(StgInfoTable *ip){
728 return closure_type_names[ip->type];
732 info_hdr_type(StgClosure *closure, char *res){
733 strcpy(res,closure_type_names[get_itbl(closure)->type]);
736 /* --------------------------------------------------------------------------
737 * Address printing code
739 * Uses symbol table in (unstripped executable)
740 * ------------------------------------------------------------------------*/
742 /* --------------------------------------------------------------------------
743 * Simple lookup table
745 * Current implementation is pretty dumb!
746 * ------------------------------------------------------------------------*/
753 static nat table_size;
754 static struct entry* table;
757 static nat max_table_size;
759 static void reset_table( int size )
761 max_table_size = size;
763 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
766 static void prepare_table( void )
768 /* Could sort it... */
771 static void insert( StgWord value, const char *name )
773 if ( table_size >= max_table_size ) {
774 barf( "Symbol table overflow\n" );
776 table[table_size].value = value;
777 table[table_size].name = name;
778 table_size = table_size + 1;
783 static rtsBool lookup_name( char *name, StgWord *result )
786 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
788 if (i < table_size) {
789 *result = table[i].value;
797 /* Code from somewhere inside GHC (circa 1994)
799 * "std"++xs -> "Zstd"++xs
800 * char_to_c 'Z' = "ZZ"
801 * char_to_c '&' = "Za"
802 * char_to_c '|' = "Zb"
803 * char_to_c ':' = "Zc"
804 * char_to_c '/' = "Zd"
805 * char_to_c '=' = "Ze"
806 * char_to_c '>' = "Zg"
807 * char_to_c '#' = "Zh"
808 * char_to_c '<' = "Zl"
809 * char_to_c '-' = "Zm"
810 * char_to_c '!' = "Zn"
811 * char_to_c '.' = "Zo"
812 * char_to_c '+' = "Zp"
813 * char_to_c '\'' = "Zq"
814 * char_to_c '*' = "Zt"
815 * char_to_c '_' = "Zu"
816 * char_to_c c = "Z" ++ show (ord c)
818 static char unZcode( char ch )
821 case 'a' : return ('&');
822 case 'b' : return ('|');
823 case 'c' : return (':');
824 case 'd' : return ('/');
825 case 'e' : return ('=');
826 case 'g' : return ('>');
827 case 'h' : return ('#');
828 case 'l' : return ('<');
829 case 'm' : return ('-');
830 case 'n' : return ('!');
831 case 'o' : return ('.');
832 case 'p' : return ('+');
833 case 'q' : return ('\'');
834 case 't' : return ('*');
835 case 'u' : return ('_');
837 case '\0' : return ('Z');
838 default : return (ch);
843 /* Precondition: out big enough to handle output (about twice length of in) */
844 static void enZcode( char *in, char *out )
850 for( i = 0; in[i] != '\0'; ++i ) {
925 const char *lookupGHCName( void *addr )
928 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
930 if (i < table_size) {
931 return table[i].name;
937 static void printZcoded( const char *raw )
941 while ( raw[j] != '\0' ) {
943 debugBelch("%c", unZcode(raw[j+1]));
946 debugBelch("%c", unZcode(raw[j+1]));
952 /* --------------------------------------------------------------------------
953 * Symbol table loading
954 * ------------------------------------------------------------------------*/
956 /* Causing linking trouble on Win32 plats, so I'm
957 disabling this for now.
963 /* Fairly ad-hoc piece of code that seems to filter out a lot of
964 * rubbish like the obj-splitting symbols
967 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
970 /* ToDo: make this work on BFD */
971 int tp = type & N_TYPE;
972 if (tp == N_TEXT || tp == N_DATA) {
973 return (name[0] == '_' && name[1] != '_');
979 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
980 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
987 extern void DEBUG_LoadSymbols( char *name )
993 abfd = bfd_openr(name, "default");
995 barf("can't open executable %s to get symbol table", name);
997 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1002 long storage_needed;
1003 asymbol **symbol_table;
1004 long number_of_symbols;
1005 long num_real_syms = 0;
1008 storage_needed = bfd_get_symtab_upper_bound (abfd);
1010 if (storage_needed < 0) {
1011 barf("can't read symbol table");
1014 if (storage_needed == 0) {
1015 debugBelch("no storage needed");
1018 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1020 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1022 if (number_of_symbols < 0) {
1023 barf("can't canonicalise symbol table");
1026 for( i = 0; i != number_of_symbols; ++i ) {
1028 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1029 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1030 if (isReal(info.type, info.name)) {
1035 IF_DEBUG(interpreter,
1036 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1037 number_of_symbols, num_real_syms)
1040 reset_table( num_real_syms );
1042 for( i = 0; i != number_of_symbols; ++i ) {
1044 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1045 if (isReal(info.type, info.name)) {
1046 insert( info.value, info.name );
1050 stgFree(symbol_table);
1055 #else /* HAVE_BFD_H */
1057 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1062 #endif /* HAVE_BFD_H */
1064 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1067 findPtr(P_ p, int follow)
1072 #if defined(__GNUC__)
1073 const int arr_size = 1024;
1075 #define arr_size 1024
1077 StgPtr arr[arr_size];
1080 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1081 for (s = 0; s < generations[g].n_steps; s++) {
1082 bd = generations[g].steps[s].blocks;
1083 for (; bd; bd = bd->link) {
1084 for (q = bd->start; q < bd->free; q++) {
1088 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1091 debugBelch("%p = ", r);
1092 printClosure((StgClosure *)r);
1102 if (follow && i == 1) {
1103 debugBelch("-->\n");
1109 void printPtr( StgPtr p )
1111 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1114 void printObj( StgClosure *obj )
1116 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );