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:
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.
272 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
273 debugBelch("UPDATE_FRAME(");
274 printPtr((StgPtr)GET_INFO(u));
276 printPtr((StgPtr)u->updatee);
283 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
284 debugBelch("CATCH_FRAME(");
285 printPtr((StgPtr)GET_INFO(u));
287 printPtr((StgPtr)u->handler);
294 StgStopFrame* u = stgCast(StgStopFrame*,obj);
295 debugBelch("STOP_FRAME(");
296 printPtr((StgPtr)GET_INFO(u));
302 debugBelch("CAF_BH");
310 debugBelch("SE_BH\n");
313 case SE_CAF_BLACKHOLE:
314 debugBelch("SE_CAF_BH\n");
320 debugBelch("ARR_WORDS(\"");
321 /* ToDo: we can't safely assume that this is a string!
322 for (i = 0; arrWordsGetChar(obj,i); ++i) {
323 putchar(arrWordsGetChar(obj,i));
325 for (i=0; i<((StgArrWords *)obj)->words; i++)
326 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
331 case MUT_ARR_PTRS_CLEAN:
332 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
335 case MUT_ARR_PTRS_DIRTY:
336 debugBelch("MUT_ARR_PTRS_DIRTY(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_CLEAN(var=%p)\n", mv->var);
359 StgMutVar* mv = (StgMutVar*)obj;
360 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
366 debugBelch(" key=%p value=%p finalizer=%p",
367 (StgPtr)(((StgWeak*)obj)->key),
368 (StgPtr)(((StgWeak*)obj)->value),
369 (StgPtr)(((StgWeak*)obj)->finalizer));
371 /* ToDo: chase 'link' ? */
375 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
380 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
386 debugBelch("BLOCKED_FETCH(");
387 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
388 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
393 debugBelch("FETCH_ME(");
394 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
399 debugBelch("FETCH_ME_BQ(");
400 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
401 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
406 #if defined(GRAN) || defined(PAR)
409 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
416 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
418 debugBelch("EVACUATED(");
419 printClosure((StgEvacuated*)obj->evacuee);
424 #if defined(PAR) && defined(DIST)
426 debugBelch("REMOTE_REF(");
427 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
433 //barf("printClosure %d",get_itbl(obj)->type);
434 debugBelch("*** printClosure: unknown type %d ****\n",
435 get_itbl(obj)->type );
436 barf("printClosure %d",get_itbl(obj)->type);
442 void printGraph( StgClosure *obj )
449 printStackObj( StgPtr sp )
451 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
453 StgClosure* c = (StgClosure*)(*sp);
454 printPtr((StgPtr)*sp);
455 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
456 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
458 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
459 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
461 if (c == (StgClosure*)&stg_ctoi_F1_info) {
462 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
464 if (c == (StgClosure*)&stg_ctoi_D1_info) {
465 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
467 if (c == (StgClosure*)&stg_ctoi_V_info) {
468 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
470 if (get_itbl(c)->type == BCO) {
471 debugBelch("\t\t\t");
472 debugBelch("BCO(...)\n");
475 debugBelch("\t\t\t");
476 printClosure ( (StgClosure*)(*sp));
485 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
491 for(i = 0; i < size; i++, bitmap >>= 1 ) {
492 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
493 if ((bitmap & 1) == 0) {
494 printPtr((P_)payload[i]);
497 debugBelch("Word# %lu\n", (lnat)payload[i]);
503 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
509 for (bmp=0; i < size; bmp++) {
510 StgWord bitmap = large_bitmap->bitmap[bmp];
512 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
513 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
514 if ((bitmap & 1) == 0) {
515 printPtr((P_)payload[i]);
518 debugBelch("Word# %lu\n", (lnat)payload[i]);
525 printStackChunk( StgPtr sp, StgPtr spBottom )
528 const StgInfoTable *info;
530 ASSERT(sp <= spBottom);
531 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
533 info = get_itbl((StgClosure *)sp);
535 switch (info->type) {
539 printObj((StgClosure*)sp);
543 printObj((StgClosure*)sp);
555 debugBelch("RET_DYN (%p)\n", r);
557 p = (P_)(r->payload);
558 printSmallBitmap(spBottom, sp,
559 RET_DYN_LIVENESS(r->liveness),
560 RET_DYN_BITMAP_SIZE);
561 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
563 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
564 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
565 debugBelch("Word# %ld\n", (long)*p);
569 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
570 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
578 debugBelch("RET_SMALL (%p)\n", info);
579 bitmap = info->layout.bitmap;
580 printSmallBitmap(spBottom, sp+1,
581 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
587 bco = ((StgBCO *)sp[1]);
589 debugBelch("RET_BCO (%p)\n", sp);
590 printLargeBitmap(spBottom, sp+2,
591 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
600 StgFunInfoTable *fun_info;
604 ret_fun = (StgRetFun *)sp;
605 fun_info = get_fun_itbl(ret_fun->fun);
606 size = ret_fun->size;
607 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
608 switch (fun_info->f.fun_type) {
610 printSmallBitmap(spBottom, sp+2,
611 BITMAP_BITS(fun_info->f.b.bitmap),
612 BITMAP_SIZE(fun_info->f.b.bitmap));
615 printLargeBitmap(spBottom, sp+2,
616 GET_FUN_LARGE_BITMAP(fun_info),
617 GET_FUN_LARGE_BITMAP(fun_info)->size);
620 printSmallBitmap(spBottom, sp+2,
621 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
622 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
629 debugBelch("unknown object %d\n", info->type);
630 barf("printStackChunk");
635 void printTSO( StgTSO *tso )
637 printStackChunk( tso->sp, tso->stack+tso->stack_size);
640 /* -----------------------------------------------------------------------------
643 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
644 -------------------------------------------------------------------------- */
646 static char *closure_type_names[] = {
655 "CONSTR_NOCAF_STATIC",
695 "MUT_ARR_PTRS_CLEAN",
696 "MUT_ARR_PTRS_DIRTY",
697 "MUT_ARR_PTRS_FROZEN",
712 "INVARIANT_CHECK_QUEUE",
723 info_type(StgClosure *closure){
724 return closure_type_names[get_itbl(closure)->type];
728 info_type_by_ip(StgInfoTable *ip){
729 return closure_type_names[ip->type];
733 info_hdr_type(StgClosure *closure, char *res){
734 strcpy(res,closure_type_names[get_itbl(closure)->type]);
737 /* --------------------------------------------------------------------------
738 * Address printing code
740 * Uses symbol table in (unstripped executable)
741 * ------------------------------------------------------------------------*/
743 /* --------------------------------------------------------------------------
744 * Simple lookup table
746 * Current implementation is pretty dumb!
747 * ------------------------------------------------------------------------*/
754 static nat table_size;
755 static struct entry* table;
758 static nat max_table_size;
760 static void reset_table( int size )
762 max_table_size = size;
764 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
767 static void prepare_table( void )
769 /* Could sort it... */
772 static void insert( StgWord value, const char *name )
774 if ( table_size >= max_table_size ) {
775 barf( "Symbol table overflow\n" );
777 table[table_size].value = value;
778 table[table_size].name = name;
779 table_size = table_size + 1;
784 static rtsBool lookup_name( char *name, StgWord *result )
787 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
789 if (i < table_size) {
790 *result = table[i].value;
798 /* Code from somewhere inside GHC (circa 1994)
800 * "std"++xs -> "Zstd"++xs
801 * char_to_c 'Z' = "ZZ"
802 * char_to_c '&' = "Za"
803 * char_to_c '|' = "Zb"
804 * char_to_c ':' = "Zc"
805 * char_to_c '/' = "Zd"
806 * char_to_c '=' = "Ze"
807 * char_to_c '>' = "Zg"
808 * char_to_c '#' = "Zh"
809 * char_to_c '<' = "Zl"
810 * char_to_c '-' = "Zm"
811 * char_to_c '!' = "Zn"
812 * char_to_c '.' = "Zo"
813 * char_to_c '+' = "Zp"
814 * char_to_c '\'' = "Zq"
815 * char_to_c '*' = "Zt"
816 * char_to_c '_' = "Zu"
817 * char_to_c c = "Z" ++ show (ord c)
819 static char unZcode( char ch )
822 case 'a' : return ('&');
823 case 'b' : return ('|');
824 case 'c' : return (':');
825 case 'd' : return ('/');
826 case 'e' : return ('=');
827 case 'g' : return ('>');
828 case 'h' : return ('#');
829 case 'l' : return ('<');
830 case 'm' : return ('-');
831 case 'n' : return ('!');
832 case 'o' : return ('.');
833 case 'p' : return ('+');
834 case 'q' : return ('\'');
835 case 't' : return ('*');
836 case 'u' : return ('_');
838 case '\0' : return ('Z');
839 default : return (ch);
844 /* Precondition: out big enough to handle output (about twice length of in) */
845 static void enZcode( char *in, char *out )
851 for( i = 0; in[i] != '\0'; ++i ) {
926 const char *lookupGHCName( void *addr )
929 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
931 if (i < table_size) {
932 return table[i].name;
938 static void printZcoded( const char *raw )
942 while ( raw[j] != '\0' ) {
944 debugBelch("%c", unZcode(raw[j+1]));
947 debugBelch("%c", unZcode(raw[j+1]));
953 /* --------------------------------------------------------------------------
954 * Symbol table loading
955 * ------------------------------------------------------------------------*/
957 /* Causing linking trouble on Win32 plats, so I'm
958 disabling this for now.
964 /* Fairly ad-hoc piece of code that seems to filter out a lot of
965 * rubbish like the obj-splitting symbols
968 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
971 /* ToDo: make this work on BFD */
972 int tp = type & N_TYPE;
973 if (tp == N_TEXT || tp == N_DATA) {
974 return (name[0] == '_' && name[1] != '_');
980 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
981 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
988 extern void DEBUG_LoadSymbols( char *name )
994 abfd = bfd_openr(name, "default");
996 barf("can't open executable %s to get symbol table", name);
998 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1003 long storage_needed;
1004 asymbol **symbol_table;
1005 long number_of_symbols;
1006 long num_real_syms = 0;
1009 storage_needed = bfd_get_symtab_upper_bound (abfd);
1011 if (storage_needed < 0) {
1012 barf("can't read symbol table");
1015 if (storage_needed == 0) {
1016 debugBelch("no storage needed");
1019 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1021 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1023 if (number_of_symbols < 0) {
1024 barf("can't canonicalise symbol table");
1027 for( i = 0; i != number_of_symbols; ++i ) {
1029 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1030 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1031 if (isReal(info.type, info.name)) {
1036 IF_DEBUG(interpreter,
1037 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1038 number_of_symbols, num_real_syms)
1041 reset_table( num_real_syms );
1043 for( i = 0; i != number_of_symbols; ++i ) {
1045 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1046 if (isReal(info.type, info.name)) {
1047 insert( info.value, info.name );
1051 stgFree(symbol_table);
1056 #else /* HAVE_BFD_H */
1058 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1063 #endif /* HAVE_BFD_H */
1065 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1068 findPtr(P_ p, int follow)
1073 #if defined(__GNUC__)
1074 const int arr_size = 1024;
1076 #define arr_size 1024
1078 StgPtr arr[arr_size];
1081 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1082 for (s = 0; s < generations[g].n_steps; s++) {
1083 bd = generations[g].steps[s].blocks;
1084 for (; bd; bd = bd->link) {
1085 for (q = bd->start; q < bd->free; q++) {
1089 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1092 debugBelch("%p = ", r);
1093 printClosure((StgClosure *)r);
1103 if (follow && i == 1) {
1104 debugBelch("-->\n");
1109 /* prettyPrintClosure() is for printing out a closure using the data constructor
1110 names found in the info tables. Closures are printed in a fashion that resembles
1111 their Haskell representation. Useful during debugging.
1113 Todo: support for more closure types, and support for non pointer fields in the
1117 void prettyPrintClosure_ (StgClosure *);
1119 void prettyPrintClosure (StgClosure *obj)
1121 prettyPrintClosure_ (obj);
1125 void prettyPrintClosure_ (StgClosure *obj)
1128 StgConInfoTable *con_info;
1130 /* collapse any indirections */
1132 type = get_itbl(obj)->type;
1134 while (type == IND ||
1135 type == IND_STATIC ||
1136 type == IND_OLDGEN ||
1138 type == IND_OLDGEN_PERM)
1140 obj = ((StgInd *)obj)->indirectee;
1141 type = get_itbl(obj)->type;
1144 /* find the info table for this object */
1145 info = get_itbl(obj);
1147 /* determine what kind of object we have */
1150 /* full applications of data constructors */
1158 case CONSTR_NOCAF_STATIC:
1163 /* find the con_info for the constructor */
1164 con_info = get_con_itbl (obj);
1166 /* obtain the name of the constructor */
1167 descriptor = con_info->con_desc;
1169 debugBelch ("(%s", descriptor);
1171 /* process the payload of the closure */
1172 /* we don't handle non pointers at the moment */
1173 for (i = 0; i < info->layout.payload.ptrs; i++)
1176 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1182 /* if it isn't a constructor then just print the closure type */
1185 debugBelch ("<%s>", info_type(obj));
1192 void printPtr( StgPtr p )
1194 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1197 void printObj( StgClosure *obj )
1199 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );