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", (lnat)((StgArrWords *)obj)->payload[i]);
335 case MUT_ARR_PTRS_CLEAN:
336 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
339 case MUT_ARR_PTRS_DIRTY:
340 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
343 case MUT_ARR_PTRS_FROZEN:
344 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
349 StgMVar* mv = (StgMVar*)obj;
350 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
356 StgMutVar* mv = (StgMutVar*)obj;
357 debugBelch("MUT_VAR(var=%p)\n", mv->var);
363 debugBelch(" key=%p value=%p finalizer=%p",
364 (StgPtr)(((StgWeak*)obj)->key),
365 (StgPtr)(((StgWeak*)obj)->value),
366 (StgPtr)(((StgWeak*)obj)->finalizer));
368 /* ToDo: chase 'link' ? */
372 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
377 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
383 debugBelch("BLOCKED_FETCH(");
384 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
385 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
390 debugBelch("FETCH_ME(");
391 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
396 debugBelch("FETCH_ME_BQ(");
397 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
398 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
403 #if defined(GRAN) || defined(PAR)
406 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
413 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
415 debugBelch("EVACUATED(");
416 printClosure((StgEvacuated*)obj->evacuee);
421 #if defined(PAR) && defined(DIST)
423 debugBelch("REMOTE_REF(");
424 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
430 //barf("printClosure %d",get_itbl(obj)->type);
431 debugBelch("*** printClosure: unknown type %d ****\n",
432 get_itbl(obj)->type );
433 barf("printClosure %d",get_itbl(obj)->type);
439 void printGraph( StgClosure *obj )
446 printStackObj( StgPtr sp )
448 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
450 StgClosure* c = (StgClosure*)(*sp);
451 printPtr((StgPtr)*sp);
452 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
453 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
455 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
456 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
458 if (c == (StgClosure*)&stg_ctoi_F1_info) {
459 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
461 if (c == (StgClosure*)&stg_ctoi_D1_info) {
462 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
464 if (c == (StgClosure*)&stg_ctoi_V_info) {
465 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
467 if (get_itbl(c)->type == BCO) {
468 debugBelch("\t\t\t");
469 debugBelch("BCO(...)\n");
472 debugBelch("\t\t\t");
473 printClosure ( (StgClosure*)(*sp));
482 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
488 for(i = 0; i < size; i++, bitmap >>= 1 ) {
489 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
490 if ((bitmap & 1) == 0) {
491 printPtr((P_)payload[i]);
494 debugBelch("Word# %lu\n", (lnat)payload[i]);
500 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
506 for (bmp=0; i < size; bmp++) {
507 StgWord bitmap = large_bitmap->bitmap[bmp];
509 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
510 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
511 if ((bitmap & 1) == 0) {
512 printPtr((P_)payload[i]);
515 debugBelch("Word# %lu\n", (lnat)payload[i]);
522 printStackChunk( StgPtr sp, StgPtr spBottom )
525 const StgInfoTable *info;
527 ASSERT(sp <= spBottom);
528 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
530 info = get_itbl((StgClosure *)sp);
532 switch (info->type) {
537 printObj((StgClosure*)sp);
549 debugBelch("RET_DYN (%p)\n", r);
551 p = (P_)(r->payload);
552 printSmallBitmap(spBottom, sp,
553 RET_DYN_LIVENESS(r->liveness),
554 RET_DYN_BITMAP_SIZE);
555 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
557 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
558 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
559 debugBelch("Word# %ld\n", (long)*p);
563 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
564 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
573 debugBelch("RET_SMALL (%p)\n", sp);
574 bitmap = info->layout.bitmap;
575 printSmallBitmap(spBottom, sp+1,
576 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
582 bco = ((StgBCO *)sp[1]);
584 debugBelch("RET_BCO (%p)\n", sp);
585 printLargeBitmap(spBottom, sp+2,
586 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
596 StgFunInfoTable *fun_info;
600 ret_fun = (StgRetFun *)sp;
601 fun_info = get_fun_itbl(ret_fun->fun);
602 size = ret_fun->size;
603 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
604 switch (fun_info->f.fun_type) {
606 printSmallBitmap(spBottom, sp+1,
607 BITMAP_BITS(fun_info->f.b.bitmap),
608 BITMAP_SIZE(fun_info->f.b.bitmap));
611 printLargeBitmap(spBottom, sp+2,
612 GET_FUN_LARGE_BITMAP(fun_info),
613 GET_FUN_LARGE_BITMAP(fun_info)->size);
616 printSmallBitmap(spBottom, sp+1,
617 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
618 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
625 debugBelch("unknown object %d\n", info->type);
626 barf("printStackChunk");
631 void printTSO( StgTSO *tso )
633 printStackChunk( tso->sp, tso->stack+tso->stack_size);
636 /* -----------------------------------------------------------------------------
639 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
640 -------------------------------------------------------------------------- */
642 static char *closure_type_names[] = {
653 "CONSTR_NOCAF_STATIC",
696 "MUT_ARR_PTRS_FROZEN",
719 info_type(StgClosure *closure){
720 return closure_type_names[get_itbl(closure)->type];
724 info_type_by_ip(StgInfoTable *ip){
725 return closure_type_names[ip->type];
729 info_hdr_type(StgClosure *closure, char *res){
730 strcpy(res,closure_type_names[get_itbl(closure)->type]);
733 /* --------------------------------------------------------------------------
734 * Address printing code
736 * Uses symbol table in (unstripped executable)
737 * ------------------------------------------------------------------------*/
739 /* --------------------------------------------------------------------------
740 * Simple lookup table
742 * Current implementation is pretty dumb!
743 * ------------------------------------------------------------------------*/
750 static nat table_size;
751 static struct entry* table;
754 static nat max_table_size;
756 static void reset_table( int size )
758 max_table_size = size;
760 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
763 static void prepare_table( void )
765 /* Could sort it... */
768 static void insert( unsigned value, const char *name )
770 if ( table_size >= max_table_size ) {
771 barf( "Symbol table overflow\n" );
773 table[table_size].value = value;
774 table[table_size].name = name;
775 table_size = table_size + 1;
780 static rtsBool lookup_name( char *name, unsigned *result )
783 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
785 if (i < table_size) {
786 *result = table[i].value;
794 /* Code from somewhere inside GHC (circa 1994)
796 * "std"++xs -> "Zstd"++xs
797 * char_to_c 'Z' = "ZZ"
798 * char_to_c '&' = "Za"
799 * char_to_c '|' = "Zb"
800 * char_to_c ':' = "Zc"
801 * char_to_c '/' = "Zd"
802 * char_to_c '=' = "Ze"
803 * char_to_c '>' = "Zg"
804 * char_to_c '#' = "Zh"
805 * char_to_c '<' = "Zl"
806 * char_to_c '-' = "Zm"
807 * char_to_c '!' = "Zn"
808 * char_to_c '.' = "Zo"
809 * char_to_c '+' = "Zp"
810 * char_to_c '\'' = "Zq"
811 * char_to_c '*' = "Zt"
812 * char_to_c '_' = "Zu"
813 * char_to_c c = "Z" ++ show (ord c)
815 static char unZcode( char ch )
818 case 'a' : return ('&');
819 case 'b' : return ('|');
820 case 'c' : return (':');
821 case 'd' : return ('/');
822 case 'e' : return ('=');
823 case 'g' : return ('>');
824 case 'h' : return ('#');
825 case 'l' : return ('<');
826 case 'm' : return ('-');
827 case 'n' : return ('!');
828 case 'o' : return ('.');
829 case 'p' : return ('+');
830 case 'q' : return ('\'');
831 case 't' : return ('*');
832 case 'u' : return ('_');
834 case '\0' : return ('Z');
835 default : return (ch);
840 /* Precondition: out big enough to handle output (about twice length of in) */
841 static void enZcode( char *in, char *out )
847 for( i = 0; in[i] != '\0'; ++i ) {
922 const char *lookupGHCName( void *addr )
925 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
927 if (i < table_size) {
928 return table[i].name;
934 static void printZcoded( const char *raw )
938 while ( raw[j] != '\0' ) {
940 debugBelch("%c", unZcode(raw[j+1]));
943 debugBelch("%c", unZcode(raw[j+1]));
949 /* --------------------------------------------------------------------------
950 * Symbol table loading
951 * ------------------------------------------------------------------------*/
953 /* Causing linking trouble on Win32 plats, so I'm
954 disabling this for now.
960 /* Fairly ad-hoc piece of code that seems to filter out a lot of
961 * rubbish like the obj-splitting symbols
964 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
967 /* ToDo: make this work on BFD */
968 int tp = type & N_TYPE;
969 if (tp == N_TEXT || tp == N_DATA) {
970 return (name[0] == '_' && name[1] != '_');
976 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
977 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
984 extern void DEBUG_LoadSymbols( char *name )
990 abfd = bfd_openr(name, "default");
992 barf("can't open executable %s to get symbol table", name);
994 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1000 asymbol **symbol_table;
1001 long number_of_symbols;
1002 long num_real_syms = 0;
1005 storage_needed = bfd_get_symtab_upper_bound (abfd);
1007 if (storage_needed < 0) {
1008 barf("can't read symbol table");
1011 if (storage_needed == 0) {
1012 debugBelch("no storage needed");
1015 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1017 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1019 if (number_of_symbols < 0) {
1020 barf("can't canonicalise symbol table");
1023 for( i = 0; i != number_of_symbols; ++i ) {
1025 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1026 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1027 if (isReal(info.type, info.name)) {
1032 IF_DEBUG(interpreter,
1033 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1034 number_of_symbols, num_real_syms)
1037 reset_table( num_real_syms );
1039 for( i = 0; i != number_of_symbols; ++i ) {
1041 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1042 if (isReal(info.type, info.name)) {
1043 insert( info.value, info.name );
1047 stgFree(symbol_table);
1052 #else /* HAVE_BFD_H */
1054 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1059 #endif /* HAVE_BFD_H */
1061 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1064 findPtr(P_ p, int follow)
1069 #if defined(__GNUC__)
1070 const int arr_size = 1024;
1072 #define arr_size 1024
1074 StgPtr arr[arr_size];
1077 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1078 for (s = 0; s < generations[g].n_steps; s++) {
1079 bd = generations[g].steps[s].blocks;
1080 for (; bd; bd = bd->link) {
1081 for (q = bd->start; q < bd->free; q++) {
1085 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1088 debugBelch("%p = ", r);
1089 printClosure((StgClosure *)r);
1099 if (follow && i == 1) {
1100 debugBelch("-->\n");
1106 void printPtr( StgPtr p )
1108 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1111 void printObj( StgClosure *obj )
1113 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );