1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1994-2000.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
18 #include "Bytecodes.h" /* for InstrPtr */
19 #include "Disassembler.h"
25 /* --------------------------------------------------------------------------
26 * local function decls
27 * ------------------------------------------------------------------------*/
29 static void printStdObjPayload( StgClosure *obj );
31 static void reset_table ( int size );
32 static void prepare_table ( void );
33 static void insert ( StgWord value, const char *name );
35 #if 0 /* unused but might be useful sometime */
36 static rtsBool lookup_name ( char *name, StgWord *result );
37 static void enZcode ( char *in, char *out );
39 static char unZcode ( char ch );
40 const char * lookupGHCName ( void *addr );
41 static void printZcoded ( const char *raw );
43 /* --------------------------------------------------------------------------
45 * ------------------------------------------------------------------------*/
47 void printPtr( StgPtr p )
50 raw = lookupGHCName(p);
58 void printObj( StgClosure *obj )
60 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
65 printStdObjHdr( StgClosure *obj, char* tag )
67 debugBelch("%s(",tag);
68 printPtr((StgPtr)obj->header.info);
70 debugBelch(", %s", obj->header.prof.ccs->cc->label);
75 printStdObjPayload( StgClosure *obj )
78 const StgInfoTable* info;
81 for (i = 0; i < info->layout.payload.ptrs; ++i) {
83 printPtr((StgPtr)obj->payload[i]);
85 for (j = 0; j < info->layout.payload.nptrs; ++j) {
86 debugBelch(", %pd#",obj->payload[i+j]);
92 printThunkPayload( StgThunk *obj )
95 const StgInfoTable* info;
98 for (i = 0; i < info->layout.payload.ptrs; ++i) {
100 printPtr((StgPtr)obj->payload[i]);
102 for (j = 0; j < info->layout.payload.nptrs; ++j) {
103 debugBelch(", %pd#",obj->payload[i+j]);
109 printThunkObject( StgThunk *obj, char* tag )
111 printStdObjHdr( (StgClosure *)obj, tag );
112 printThunkPayload( obj );
116 printClosure( StgClosure *obj )
118 obj = UNTAG_CLOSURE(obj);
121 info = get_itbl(obj);
123 switch ( info->type ) {
125 barf("Invalid object");
128 case CONSTR_1_0: case CONSTR_0_1:
129 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
131 case CONSTR_NOCAF_STATIC:
136 debugBelch("%s(", GET_PROF_DESC(info));
137 debugBelch("%s", obj->header.prof.ccs->cc->label);
139 debugBelch("CONSTR(");
140 printPtr((StgPtr)obj->header.info);
141 debugBelch("(tag=%d)",info->srt_bitmap);
143 for (i = 0; i < info->layout.payload.ptrs; ++i) {
145 printPtr((StgPtr)obj->payload[i]);
147 for (j = 0; j < info->layout.payload.nptrs; ++j) {
148 debugBelch(", %p#", obj->payload[i+j]);
155 case FUN_1_0: case FUN_0_1:
156 case FUN_1_1: case FUN_0_2: case FUN_2_0:
158 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
159 printPtr((StgPtr)obj->header.info);
161 debugBelch(", %s", obj->header.prof.ccs->cc->label);
163 printStdObjPayload(obj);
167 case THUNK_1_0: case THUNK_0_1:
168 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
170 /* ToDo: will this work for THUNK_STATIC too? */
172 printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
174 printThunkObject((StgThunk *)obj,"THUNK");
179 printStdObjHdr(obj, "THUNK_SELECTOR");
180 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
184 disassemble( (StgBCO*)obj );
189 StgAP* ap = stgCast(StgAP*,obj);
191 debugBelch("AP("); printPtr((StgPtr)ap->fun);
192 for (i = 0; i < ap->n_args; ++i) {
194 printPtr((P_)ap->payload[i]);
202 StgPAP* pap = stgCast(StgPAP*,obj);
204 debugBelch("PAP/%d(",pap->arity);
205 printPtr((StgPtr)pap->fun);
206 for (i = 0; i < pap->n_args; ++i) {
208 printPtr((StgPtr)pap->payload[i]);
216 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
218 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
219 for (i = 0; i < ap->size; ++i) {
221 printPtr((P_)ap->payload[i]);
229 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
234 debugBelch("IND_OLDGEN(");
235 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
241 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
245 case IND_OLDGEN_PERM:
246 debugBelch("IND_OLDGEN_PERM(");
247 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
252 debugBelch("IND_STATIC(");
253 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
257 /* Cannot happen -- use default case.
267 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
268 debugBelch("UPDATE_FRAME(");
269 printPtr((StgPtr)GET_INFO(u));
271 printPtr((StgPtr)u->updatee);
278 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
279 debugBelch("CATCH_FRAME(");
280 printPtr((StgPtr)GET_INFO(u));
282 printPtr((StgPtr)u->handler);
289 StgStopFrame* u = stgCast(StgStopFrame*,obj);
290 debugBelch("STOP_FRAME(");
291 printPtr((StgPtr)GET_INFO(u));
297 debugBelch("CAF_BH");
307 debugBelch("ARR_WORDS(\"");
308 /* ToDo: we can't safely assume that this is a string!
309 for (i = 0; arrWordsGetChar(obj,i); ++i) {
310 putchar(arrWordsGetChar(obj,i));
312 for (i=0; i<((StgArrWords *)obj)->words; i++)
313 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
318 case MUT_ARR_PTRS_CLEAN:
319 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
322 case MUT_ARR_PTRS_DIRTY:
323 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
326 case MUT_ARR_PTRS_FROZEN:
327 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
333 StgMVar* mv = (StgMVar*)obj;
334 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
340 StgMutVar* mv = (StgMutVar*)obj;
341 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
347 StgMutVar* mv = (StgMutVar*)obj;
348 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
354 debugBelch(" key=%p value=%p finalizer=%p",
355 (StgPtr)(((StgWeak*)obj)->key),
356 (StgPtr)(((StgWeak*)obj)->value),
357 (StgPtr)(((StgWeak*)obj)->finalizer));
359 /* ToDo: chase 'link' ? */
363 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
368 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
373 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
375 debugBelch("EVACUATED(");
376 printClosure((StgEvacuated*)obj->evacuee);
382 //barf("printClosure %d",get_itbl(obj)->type);
383 debugBelch("*** printClosure: unknown type %d ****\n",
384 get_itbl(obj)->type );
385 barf("printClosure %d",get_itbl(obj)->type);
391 void printGraph( StgClosure *obj )
398 printStackObj( StgPtr sp )
400 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
402 StgClosure* c = (StgClosure*)(*sp);
403 printPtr((StgPtr)*sp);
404 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
405 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
407 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
408 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
410 if (c == (StgClosure*)&stg_ctoi_F1_info) {
411 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
413 if (c == (StgClosure*)&stg_ctoi_D1_info) {
414 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
416 if (c == (StgClosure*)&stg_ctoi_V_info) {
417 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
419 if (get_itbl(c)->type == BCO) {
420 debugBelch("\t\t\t");
421 debugBelch("BCO(...)\n");
424 debugBelch("\t\t\t");
425 printClosure ( (StgClosure*)(*sp));
434 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
440 for(i = 0; i < size; i++, bitmap >>= 1 ) {
441 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
442 if ((bitmap & 1) == 0) {
443 printPtr((P_)payload[i]);
446 debugBelch("Word# %lu\n", (lnat)payload[i]);
452 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
458 for (bmp=0; i < size; bmp++) {
459 StgWord bitmap = large_bitmap->bitmap[bmp];
461 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
462 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
463 if ((bitmap & 1) == 0) {
464 printPtr((P_)payload[i]);
467 debugBelch("Word# %lu\n", (lnat)payload[i]);
474 printStackChunk( StgPtr sp, StgPtr spBottom )
477 const StgInfoTable *info;
479 ASSERT(sp <= spBottom);
480 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
482 info = get_itbl((StgClosure *)sp);
484 switch (info->type) {
488 printObj((StgClosure*)sp);
492 printObj((StgClosure*)sp);
504 debugBelch("RET_DYN (%p)\n", r);
506 p = (P_)(r->payload);
507 printSmallBitmap(spBottom, sp,
508 RET_DYN_LIVENESS(r->liveness),
509 RET_DYN_BITMAP_SIZE);
510 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
512 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
513 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
514 debugBelch("Word# %ld\n", (long)*p);
518 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
519 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
527 debugBelch("RET_SMALL (%p)\n", info);
528 bitmap = info->layout.bitmap;
529 printSmallBitmap(spBottom, sp+1,
530 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
536 bco = ((StgBCO *)sp[1]);
538 debugBelch("RET_BCO (%p)\n", sp);
539 printLargeBitmap(spBottom, sp+2,
540 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
549 StgFunInfoTable *fun_info;
553 ret_fun = (StgRetFun *)sp;
554 fun_info = get_fun_itbl(ret_fun->fun);
555 size = ret_fun->size;
556 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
557 switch (fun_info->f.fun_type) {
559 printSmallBitmap(spBottom, sp+2,
560 BITMAP_BITS(fun_info->f.b.bitmap),
561 BITMAP_SIZE(fun_info->f.b.bitmap));
564 printLargeBitmap(spBottom, sp+2,
565 GET_FUN_LARGE_BITMAP(fun_info),
566 GET_FUN_LARGE_BITMAP(fun_info)->size);
569 printSmallBitmap(spBottom, sp+2,
570 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
571 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
578 debugBelch("unknown object %d\n", info->type);
579 barf("printStackChunk");
584 void printTSO( StgTSO *tso )
586 printStackChunk( tso->sp, tso->stack+tso->stack_size);
589 /* -----------------------------------------------------------------------------
592 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
593 -------------------------------------------------------------------------- */
595 static char *closure_type_names[] = {
604 "CONSTR_NOCAF_STATIC",
644 "MUT_ARR_PTRS_CLEAN",
645 "MUT_ARR_PTRS_DIRTY",
646 "MUT_ARR_PTRS_FROZEN",
661 "INVARIANT_CHECK_QUEUE",
672 info_type(StgClosure *closure){
673 return closure_type_names[get_itbl(closure)->type];
677 info_type_by_ip(StgInfoTable *ip){
678 return closure_type_names[ip->type];
682 info_hdr_type(StgClosure *closure, char *res){
683 strcpy(res,closure_type_names[get_itbl(closure)->type]);
686 /* --------------------------------------------------------------------------
687 * Address printing code
689 * Uses symbol table in (unstripped executable)
690 * ------------------------------------------------------------------------*/
692 /* --------------------------------------------------------------------------
693 * Simple lookup table
695 * Current implementation is pretty dumb!
696 * ------------------------------------------------------------------------*/
703 static nat table_size;
704 static struct entry* table;
707 static nat max_table_size;
709 static void reset_table( int size )
711 max_table_size = size;
713 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
716 static void prepare_table( void )
718 /* Could sort it... */
721 static void insert( StgWord value, const char *name )
723 if ( table_size >= max_table_size ) {
724 barf( "Symbol table overflow\n" );
726 table[table_size].value = value;
727 table[table_size].name = name;
728 table_size = table_size + 1;
733 static rtsBool lookup_name( char *name, StgWord *result )
736 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
738 if (i < table_size) {
739 *result = table[i].value;
747 /* Code from somewhere inside GHC (circa 1994)
749 * "std"++xs -> "Zstd"++xs
750 * char_to_c 'Z' = "ZZ"
751 * char_to_c '&' = "Za"
752 * char_to_c '|' = "Zb"
753 * char_to_c ':' = "Zc"
754 * char_to_c '/' = "Zd"
755 * char_to_c '=' = "Ze"
756 * char_to_c '>' = "Zg"
757 * char_to_c '#' = "Zh"
758 * char_to_c '<' = "Zl"
759 * char_to_c '-' = "Zm"
760 * char_to_c '!' = "Zn"
761 * char_to_c '.' = "Zo"
762 * char_to_c '+' = "Zp"
763 * char_to_c '\'' = "Zq"
764 * char_to_c '*' = "Zt"
765 * char_to_c '_' = "Zu"
766 * char_to_c c = "Z" ++ show (ord c)
768 static char unZcode( char ch )
771 case 'a' : return ('&');
772 case 'b' : return ('|');
773 case 'c' : return (':');
774 case 'd' : return ('/');
775 case 'e' : return ('=');
776 case 'g' : return ('>');
777 case 'h' : return ('#');
778 case 'l' : return ('<');
779 case 'm' : return ('-');
780 case 'n' : return ('!');
781 case 'o' : return ('.');
782 case 'p' : return ('+');
783 case 'q' : return ('\'');
784 case 't' : return ('*');
785 case 'u' : return ('_');
787 case '\0' : return ('Z');
788 default : return (ch);
793 /* Precondition: out big enough to handle output (about twice length of in) */
794 static void enZcode( char *in, char *out )
800 for( i = 0; in[i] != '\0'; ++i ) {
875 const char *lookupGHCName( void *addr )
878 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
880 if (i < table_size) {
881 return table[i].name;
887 static void printZcoded( const char *raw )
891 while ( raw[j] != '\0' ) {
893 debugBelch("%c", unZcode(raw[j+1]));
896 debugBelch("%c", unZcode(raw[j+1]));
902 /* --------------------------------------------------------------------------
903 * Symbol table loading
904 * ------------------------------------------------------------------------*/
906 /* Causing linking trouble on Win32 plats, so I'm
907 disabling this for now.
913 /* Fairly ad-hoc piece of code that seems to filter out a lot of
914 * rubbish like the obj-splitting symbols
917 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
920 /* ToDo: make this work on BFD */
921 int tp = type & N_TYPE;
922 if (tp == N_TEXT || tp == N_DATA) {
923 return (name[0] == '_' && name[1] != '_');
929 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
930 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
937 extern void DEBUG_LoadSymbols( char *name )
943 abfd = bfd_openr(name, "default");
945 barf("can't open executable %s to get symbol table", name);
947 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
953 asymbol **symbol_table;
954 long number_of_symbols;
955 long num_real_syms = 0;
958 storage_needed = bfd_get_symtab_upper_bound (abfd);
960 if (storage_needed < 0) {
961 barf("can't read symbol table");
964 if (storage_needed == 0) {
965 debugBelch("no storage needed");
968 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
970 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
972 if (number_of_symbols < 0) {
973 barf("can't canonicalise symbol table");
976 for( i = 0; i != number_of_symbols; ++i ) {
978 bfd_get_symbol_info(abfd,symbol_table[i],&info);
979 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
980 if (isReal(info.type, info.name)) {
985 IF_DEBUG(interpreter,
986 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
987 number_of_symbols, num_real_syms)
990 reset_table( num_real_syms );
992 for( i = 0; i != number_of_symbols; ++i ) {
994 bfd_get_symbol_info(abfd,symbol_table[i],&info);
995 if (isReal(info.type, info.name)) {
996 insert( info.value, info.name );
1000 stgFree(symbol_table);
1005 #else /* HAVE_BFD_H */
1007 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1012 #endif /* HAVE_BFD_H */
1014 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1019 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
1022 for (; bd; bd = bd->link) {
1024 for (q = bd->start; q < bd->free; q++) {
1025 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
1028 while (HEAP_ALLOCED((StgPtr)*r) || !LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1031 debugBelch("%p = ", r);
1032 printClosure((StgClosure *)r);
1044 findPtr(P_ p, int follow)
1048 #if defined(__GNUC__)
1049 const int arr_size = 1024;
1051 #define arr_size 1024
1053 StgPtr arr[arr_size];
1057 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1058 for (s = 0; s < generations[g].n_steps; s++) {
1059 bd = generations[g].steps[s].blocks;
1060 i = findPtrBlocks(p,bd,arr,arr_size,i);
1061 bd = generations[g].steps[s].large_objects;
1062 i = findPtrBlocks(p,bd,arr,arr_size,i);
1063 if (i >= arr_size) return;
1066 if (follow && i == 1) {
1067 debugBelch("-->\n");
1072 /* prettyPrintClosure() is for printing out a closure using the data constructor
1073 names found in the info tables. Closures are printed in a fashion that resembles
1074 their Haskell representation. Useful during debugging.
1076 Todo: support for more closure types, and support for non pointer fields in the
1080 void prettyPrintClosure_ (StgClosure *);
1082 void prettyPrintClosure (StgClosure *obj)
1084 prettyPrintClosure_ (obj);
1088 void prettyPrintClosure_ (StgClosure *obj)
1091 StgConInfoTable *con_info;
1093 /* collapse any indirections */
1095 type = get_itbl(obj)->type;
1097 while (type == IND ||
1098 type == IND_STATIC ||
1099 type == IND_OLDGEN ||
1101 type == IND_OLDGEN_PERM)
1103 obj = ((StgInd *)obj)->indirectee;
1104 type = get_itbl(obj)->type;
1107 /* find the info table for this object */
1108 info = get_itbl(obj);
1110 /* determine what kind of object we have */
1113 /* full applications of data constructors */
1121 case CONSTR_NOCAF_STATIC:
1126 /* find the con_info for the constructor */
1127 con_info = get_con_itbl (obj);
1129 /* obtain the name of the constructor */
1130 descriptor = GET_CON_DESC(con_info);
1132 debugBelch ("(%s", descriptor);
1134 /* process the payload of the closure */
1135 /* we don't handle non pointers at the moment */
1136 for (i = 0; i < info->layout.payload.ptrs; i++)
1139 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1145 /* if it isn't a constructor then just print the closure type */
1148 debugBelch ("<%s>", info_type(obj));
1155 void printPtr( StgPtr p )
1157 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1160 void printObj( StgClosure *obj )
1162 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );