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]);
336 debugBelch("MUT_ARR_PTRS(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
339 case MUT_ARR_PTRS_FROZEN:
340 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
345 StgMVar* mv = (StgMVar*)obj;
346 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
352 StgMutVar* mv = (StgMutVar*)obj;
353 debugBelch("MUT_VAR(var=%p)\n", mv->var);
359 debugBelch(" key=%p value=%p finalizer=%p",
360 (StgPtr)(((StgWeak*)obj)->key),
361 (StgPtr)(((StgWeak*)obj)->value),
362 (StgPtr)(((StgWeak*)obj)->finalizer));
364 /* ToDo: chase 'link' ? */
368 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
373 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
379 debugBelch("BLOCKED_FETCH(");
380 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
381 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
386 debugBelch("FETCH_ME(");
387 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
392 debugBelch("FETCH_ME_BQ(");
393 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
394 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
399 #if defined(GRAN) || defined(PAR)
402 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
409 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
411 debugBelch("EVACUATED(");
412 printClosure((StgEvacuated*)obj->evacuee);
417 #if defined(PAR) && defined(DIST)
419 debugBelch("REMOTE_REF(");
420 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
426 //barf("printClosure %d",get_itbl(obj)->type);
427 debugBelch("*** printClosure: unknown type %d ****\n",
428 get_itbl(obj)->type );
429 barf("printClosure %d",get_itbl(obj)->type);
435 void printGraph( StgClosure *obj )
442 printStackObj( StgPtr sp )
444 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
446 StgClosure* c = (StgClosure*)(*sp);
447 printPtr((StgPtr)*sp);
448 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
449 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
451 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
452 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
454 if (c == (StgClosure*)&stg_ctoi_F1_info) {
455 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
457 if (c == (StgClosure*)&stg_ctoi_D1_info) {
458 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
460 if (c == (StgClosure*)&stg_ctoi_V_info) {
461 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
463 if (get_itbl(c)->type == BCO) {
464 debugBelch("\t\t\t");
465 debugBelch("BCO(...)\n");
468 debugBelch("\t\t\t");
469 printClosure ( (StgClosure*)(*sp));
478 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
484 for(i = 0; i < size; i++, bitmap >>= 1 ) {
485 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
486 if ((bitmap & 1) == 0) {
487 printPtr((P_)payload[i]);
490 debugBelch("Word# %lu\n", (lnat)payload[i]);
496 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
502 for (bmp=0; i < size; bmp++) {
503 StgWord bitmap = large_bitmap->bitmap[bmp];
505 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
506 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
507 if ((bitmap & 1) == 0) {
508 printPtr((P_)payload[i]);
511 debugBelch("Word# %lu\n", (lnat)payload[i]);
518 printStackChunk( StgPtr sp, StgPtr spBottom )
521 const StgInfoTable *info;
523 ASSERT(sp <= spBottom);
524 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
526 info = get_itbl((StgClosure *)sp);
528 switch (info->type) {
533 printObj((StgClosure*)sp);
545 debugBelch("RET_DYN (%p)\n", r);
547 p = (P_)(r->payload);
548 printSmallBitmap(spBottom, sp,
549 RET_DYN_LIVENESS(r->liveness),
550 RET_DYN_BITMAP_SIZE);
551 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
553 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
554 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
555 debugBelch("Word# %ld\n", (long)*p);
559 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
560 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
569 debugBelch("RET_SMALL (%p)\n", sp);
570 bitmap = info->layout.bitmap;
571 printSmallBitmap(spBottom, sp+1,
572 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
578 bco = ((StgBCO *)sp[1]);
580 debugBelch("RET_BCO (%p)\n", sp);
581 printLargeBitmap(spBottom, sp+2,
582 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
592 StgFunInfoTable *fun_info;
596 ret_fun = (StgRetFun *)sp;
597 fun_info = get_fun_itbl(ret_fun->fun);
598 size = ret_fun->size;
599 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
600 switch (fun_info->f.fun_type) {
602 printSmallBitmap(spBottom, sp+1,
603 BITMAP_BITS(fun_info->f.b.bitmap),
604 BITMAP_SIZE(fun_info->f.b.bitmap));
607 printLargeBitmap(spBottom, sp+2,
608 GET_FUN_LARGE_BITMAP(fun_info),
609 GET_FUN_LARGE_BITMAP(fun_info)->size);
612 printSmallBitmap(spBottom, sp+1,
613 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
614 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
621 debugBelch("unknown object %d\n", info->type);
622 barf("printStackChunk");
627 void printTSO( StgTSO *tso )
629 printStackChunk( tso->sp, tso->stack+tso->stack_size);
632 /* -----------------------------------------------------------------------------
635 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
636 -------------------------------------------------------------------------- */
638 static char *closure_type_names[] = {
649 "CONSTR_NOCAF_STATIC",
692 "MUT_ARR_PTRS_FROZEN",
715 info_type(StgClosure *closure){
716 return closure_type_names[get_itbl(closure)->type];
720 info_type_by_ip(StgInfoTable *ip){
721 return closure_type_names[ip->type];
725 info_hdr_type(StgClosure *closure, char *res){
726 strcpy(res,closure_type_names[get_itbl(closure)->type]);
729 /* --------------------------------------------------------------------------
730 * Address printing code
732 * Uses symbol table in (unstripped executable)
733 * ------------------------------------------------------------------------*/
735 /* --------------------------------------------------------------------------
736 * Simple lookup table
738 * Current implementation is pretty dumb!
739 * ------------------------------------------------------------------------*/
746 static nat table_size;
747 static struct entry* table;
750 static nat max_table_size;
752 static void reset_table( int size )
754 max_table_size = size;
756 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
759 static void prepare_table( void )
761 /* Could sort it... */
764 static void insert( unsigned value, const char *name )
766 if ( table_size >= max_table_size ) {
767 barf( "Symbol table overflow\n" );
769 table[table_size].value = value;
770 table[table_size].name = name;
771 table_size = table_size + 1;
776 static rtsBool lookup_name( char *name, unsigned *result )
779 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
781 if (i < table_size) {
782 *result = table[i].value;
790 /* Code from somewhere inside GHC (circa 1994)
792 * "std"++xs -> "Zstd"++xs
793 * char_to_c 'Z' = "ZZ"
794 * char_to_c '&' = "Za"
795 * char_to_c '|' = "Zb"
796 * char_to_c ':' = "Zc"
797 * char_to_c '/' = "Zd"
798 * char_to_c '=' = "Ze"
799 * char_to_c '>' = "Zg"
800 * char_to_c '#' = "Zh"
801 * char_to_c '<' = "Zl"
802 * char_to_c '-' = "Zm"
803 * char_to_c '!' = "Zn"
804 * char_to_c '.' = "Zo"
805 * char_to_c '+' = "Zp"
806 * char_to_c '\'' = "Zq"
807 * char_to_c '*' = "Zt"
808 * char_to_c '_' = "Zu"
809 * char_to_c c = "Z" ++ show (ord c)
811 static char unZcode( char ch )
814 case 'a' : return ('&');
815 case 'b' : return ('|');
816 case 'c' : return (':');
817 case 'd' : return ('/');
818 case 'e' : return ('=');
819 case 'g' : return ('>');
820 case 'h' : return ('#');
821 case 'l' : return ('<');
822 case 'm' : return ('-');
823 case 'n' : return ('!');
824 case 'o' : return ('.');
825 case 'p' : return ('+');
826 case 'q' : return ('\'');
827 case 't' : return ('*');
828 case 'u' : return ('_');
830 case '\0' : return ('Z');
831 default : return (ch);
836 /* Precondition: out big enough to handle output (about twice length of in) */
837 static void enZcode( char *in, char *out )
843 for( i = 0; in[i] != '\0'; ++i ) {
918 const char *lookupGHCName( void *addr )
921 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
923 if (i < table_size) {
924 return table[i].name;
930 static void printZcoded( const char *raw )
934 while ( raw[j] != '\0' ) {
936 debugBelch("%c", unZcode(raw[j+1]));
939 debugBelch("%c", unZcode(raw[j+1]));
945 /* --------------------------------------------------------------------------
946 * Symbol table loading
947 * ------------------------------------------------------------------------*/
949 /* Causing linking trouble on Win32 plats, so I'm
950 disabling this for now.
956 /* Fairly ad-hoc piece of code that seems to filter out a lot of
957 * rubbish like the obj-splitting symbols
960 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
963 /* ToDo: make this work on BFD */
964 int tp = type & N_TYPE;
965 if (tp == N_TEXT || tp == N_DATA) {
966 return (name[0] == '_' && name[1] != '_');
972 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
973 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
980 extern void DEBUG_LoadSymbols( char *name )
986 abfd = bfd_openr(name, "default");
988 barf("can't open executable %s to get symbol table", name);
990 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
996 asymbol **symbol_table;
997 long number_of_symbols;
998 long num_real_syms = 0;
1001 storage_needed = bfd_get_symtab_upper_bound (abfd);
1003 if (storage_needed < 0) {
1004 barf("can't read symbol table");
1007 if (storage_needed == 0) {
1008 debugBelch("no storage needed");
1011 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1013 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1015 if (number_of_symbols < 0) {
1016 barf("can't canonicalise symbol table");
1019 for( i = 0; i != number_of_symbols; ++i ) {
1021 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1022 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1023 if (isReal(info.type, info.name)) {
1028 IF_DEBUG(interpreter,
1029 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1030 number_of_symbols, num_real_syms)
1033 reset_table( num_real_syms );
1035 for( i = 0; i != number_of_symbols; ++i ) {
1037 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1038 if (isReal(info.type, info.name)) {
1039 insert( info.value, info.name );
1043 stgFree(symbol_table);
1048 #else /* HAVE_BFD_H */
1050 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1055 #endif /* HAVE_BFD_H */
1057 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1060 findPtr(P_ p, int follow)
1065 #if defined(__GNUC__)
1066 const int arr_size = 1024;
1068 #define arr_size 1024
1070 StgPtr arr[arr_size];
1073 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1074 for (s = 0; s < generations[g].n_steps; s++) {
1075 bd = generations[g].steps[s].blocks;
1076 for (; bd; bd = bd->link) {
1077 for (q = bd->start; q < bd->free; q++) {
1081 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1084 debugBelch("%p = ", r);
1085 printClosure((StgClosure *)r);
1095 if (follow && i == 1) {
1096 debugBelch("-->\n");
1102 void printPtr( StgPtr p )
1104 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1107 void printObj( StgClosure *obj )
1109 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );