1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1994-2000.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
11 #include "rts/Bytecodes.h" /* for InstrPtr */
18 #include "Disassembler.h"
23 /* --------------------------------------------------------------------------
24 * local function decls
25 * ------------------------------------------------------------------------*/
27 static void printStdObjPayload( StgClosure *obj );
29 static void reset_table ( int size );
30 static void prepare_table ( void );
31 static void insert ( StgWord value, const char *name );
33 #if 0 /* unused but might be useful sometime */
34 static rtsBool lookup_name ( char *name, StgWord *result );
35 static void enZcode ( char *in, char *out );
37 static char unZcode ( char ch );
38 static void printZcoded ( const char *raw );
40 /* --------------------------------------------------------------------------
42 * ------------------------------------------------------------------------*/
44 void printPtr( StgPtr p )
47 raw = lookupGHCName(p);
55 void printObj( StgClosure *obj )
57 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
62 printStdObjHdr( StgClosure *obj, char* tag )
64 debugBelch("%s(",tag);
65 printPtr((StgPtr)obj->header.info);
67 debugBelch(", %s", obj->header.prof.ccs->cc->label);
72 printStdObjPayload( StgClosure *obj )
75 const StgInfoTable* info;
78 for (i = 0; i < info->layout.payload.ptrs; ++i) {
80 printPtr((StgPtr)obj->payload[i]);
82 for (j = 0; j < info->layout.payload.nptrs; ++j) {
83 debugBelch(", %pd#",obj->payload[i+j]);
89 printThunkPayload( StgThunk *obj )
92 const StgInfoTable* info;
95 for (i = 0; i < info->layout.payload.ptrs; ++i) {
97 printPtr((StgPtr)obj->payload[i]);
99 for (j = 0; j < info->layout.payload.nptrs; ++j) {
100 debugBelch(", %pd#",obj->payload[i+j]);
106 printThunkObject( StgThunk *obj, char* tag )
108 printStdObjHdr( (StgClosure *)obj, tag );
109 printThunkPayload( obj );
113 printClosure( StgClosure *obj )
115 obj = UNTAG_CLOSURE(obj);
118 info = get_itbl(obj);
120 switch ( info->type ) {
122 barf("Invalid object");
125 case CONSTR_1_0: case CONSTR_0_1:
126 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
128 case CONSTR_NOCAF_STATIC:
133 debugBelch("%s(", GET_PROF_DESC(info));
134 debugBelch("%s", obj->header.prof.ccs->cc->label);
136 debugBelch("CONSTR(");
137 printPtr((StgPtr)obj->header.info);
138 debugBelch("(tag=%d)",info->srt_bitmap);
140 for (i = 0; i < info->layout.payload.ptrs; ++i) {
142 printPtr((StgPtr)obj->payload[i]);
144 for (j = 0; j < info->layout.payload.nptrs; ++j) {
145 debugBelch(", %p#", obj->payload[i+j]);
152 case FUN_1_0: case FUN_0_1:
153 case FUN_1_1: case FUN_0_2: case FUN_2_0:
155 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
156 printPtr((StgPtr)obj->header.info);
158 debugBelch(", %s", obj->header.prof.ccs->cc->label);
160 printStdObjPayload(obj);
164 case THUNK_1_0: case THUNK_0_1:
165 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
167 /* ToDo: will this work for THUNK_STATIC too? */
169 printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
171 printThunkObject((StgThunk *)obj,"THUNK");
176 printStdObjHdr(obj, "THUNK_SELECTOR");
177 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
181 disassemble( (StgBCO*)obj );
186 StgAP* ap = (StgAP*)obj;
188 debugBelch("AP("); printPtr((StgPtr)ap->fun);
189 for (i = 0; i < ap->n_args; ++i) {
191 printPtr((P_)ap->payload[i]);
199 StgPAP* pap = (StgPAP*)obj;
201 debugBelch("PAP/%d(",pap->arity);
202 printPtr((StgPtr)pap->fun);
203 for (i = 0; i < pap->n_args; ++i) {
205 printPtr((StgPtr)pap->payload[i]);
213 StgAP_STACK* ap = (StgAP_STACK*)obj;
215 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
216 for (i = 0; i < ap->size; ++i) {
218 printPtr((P_)ap->payload[i]);
226 printPtr((StgPtr)((StgInd*)obj)->indirectee);
231 debugBelch("IND_OLDGEN(");
232 printPtr((StgPtr)((StgInd*)obj)->indirectee);
238 printPtr((StgPtr)((StgInd*)obj)->indirectee);
242 case IND_OLDGEN_PERM:
243 debugBelch("IND_OLDGEN_PERM(");
244 printPtr((StgPtr)((StgInd*)obj)->indirectee);
249 debugBelch("IND_STATIC(");
250 printPtr((StgPtr)((StgInd*)obj)->indirectee);
254 /* Cannot happen -- use default case.
264 StgUpdateFrame* u = (StgUpdateFrame*)obj;
265 debugBelch("UPDATE_FRAME(");
266 printPtr((StgPtr)GET_INFO(u));
268 printPtr((StgPtr)u->updatee);
275 StgCatchFrame* u = (StgCatchFrame*)obj;
276 debugBelch("CATCH_FRAME(");
277 printPtr((StgPtr)GET_INFO(u));
279 printPtr((StgPtr)u->handler);
286 StgStopFrame* u = (StgStopFrame*)obj;
287 debugBelch("STOP_FRAME(");
288 printPtr((StgPtr)GET_INFO(u));
294 debugBelch("CAF_BH");
304 debugBelch("ARR_WORDS(\"");
305 /* ToDo: we can't safely assume that this is a string!
306 for (i = 0; arrWordsGetChar(obj,i); ++i) {
307 putchar(arrWordsGetChar(obj,i));
309 for (i=0; i<((StgArrWords *)obj)->words; i++)
310 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
315 case MUT_ARR_PTRS_CLEAN:
316 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
319 case MUT_ARR_PTRS_DIRTY:
320 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
323 case MUT_ARR_PTRS_FROZEN:
324 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
330 StgMVar* mv = (StgMVar*)obj;
331 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
337 StgMutVar* mv = (StgMutVar*)obj;
338 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
344 StgMutVar* mv = (StgMutVar*)obj;
345 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
351 debugBelch(" key=%p value=%p finalizer=%p",
352 (StgPtr)(((StgWeak*)obj)->key),
353 (StgPtr)(((StgWeak*)obj)->value),
354 (StgPtr)(((StgWeak*)obj)->finalizer));
356 /* ToDo: chase 'link' ? */
360 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
365 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
370 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
372 debugBelch("EVACUATED(");
373 printClosure((StgEvacuated*)obj->evacuee);
379 //barf("printClosure %d",get_itbl(obj)->type);
380 debugBelch("*** printClosure: unknown type %d ****\n",
381 get_itbl(obj)->type );
382 barf("printClosure %d",get_itbl(obj)->type);
388 void printGraph( StgClosure *obj )
395 printStackObj( StgPtr sp )
397 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
399 StgClosure* c = (StgClosure*)(*sp);
400 printPtr((StgPtr)*sp);
401 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
402 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
404 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
405 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
407 if (c == (StgClosure*)&stg_ctoi_F1_info) {
408 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
410 if (c == (StgClosure*)&stg_ctoi_D1_info) {
411 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
413 if (c == (StgClosure*)&stg_ctoi_V_info) {
414 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
416 if (get_itbl(c)->type == BCO) {
417 debugBelch("\t\t\t");
418 debugBelch("BCO(...)\n");
421 debugBelch("\t\t\t");
422 printClosure ( (StgClosure*)(*sp));
431 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
437 for(i = 0; i < size; i++, bitmap >>= 1 ) {
438 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
439 if ((bitmap & 1) == 0) {
440 printPtr((P_)payload[i]);
443 debugBelch("Word# %lu\n", (lnat)payload[i]);
449 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
455 for (bmp=0; i < size; bmp++) {
456 StgWord bitmap = large_bitmap->bitmap[bmp];
458 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
459 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
460 if ((bitmap & 1) == 0) {
461 printPtr((P_)payload[i]);
464 debugBelch("Word# %lu\n", (lnat)payload[i]);
471 printStackChunk( StgPtr sp, StgPtr spBottom )
474 const StgInfoTable *info;
476 ASSERT(sp <= spBottom);
477 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
479 info = get_itbl((StgClosure *)sp);
481 switch (info->type) {
485 printObj((StgClosure*)sp);
489 printObj((StgClosure*)sp);
501 debugBelch("RET_DYN (%p)\n", r);
503 p = (P_)(r->payload);
504 printSmallBitmap(spBottom, sp,
505 RET_DYN_LIVENESS(r->liveness),
506 RET_DYN_BITMAP_SIZE);
507 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
509 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
510 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
511 debugBelch("Word# %ld\n", (long)*p);
515 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
516 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
524 debugBelch("RET_SMALL (%p)\n", info);
525 bitmap = info->layout.bitmap;
526 printSmallBitmap(spBottom, sp+1,
527 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
533 bco = ((StgBCO *)sp[1]);
535 debugBelch("RET_BCO (%p)\n", sp);
536 printLargeBitmap(spBottom, sp+2,
537 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
546 StgFunInfoTable *fun_info;
550 ret_fun = (StgRetFun *)sp;
551 fun_info = get_fun_itbl(ret_fun->fun);
552 size = ret_fun->size;
553 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
554 switch (fun_info->f.fun_type) {
556 printSmallBitmap(spBottom, sp+2,
557 BITMAP_BITS(fun_info->f.b.bitmap),
558 BITMAP_SIZE(fun_info->f.b.bitmap));
561 printLargeBitmap(spBottom, sp+2,
562 GET_FUN_LARGE_BITMAP(fun_info),
563 GET_FUN_LARGE_BITMAP(fun_info)->size);
566 printSmallBitmap(spBottom, sp+2,
567 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
568 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
575 debugBelch("unknown object %d\n", info->type);
576 barf("printStackChunk");
581 void printTSO( StgTSO *tso )
583 printStackChunk( tso->sp, tso->stack+tso->stack_size);
586 /* -----------------------------------------------------------------------------
589 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
590 -------------------------------------------------------------------------- */
592 static char *closure_type_names[] = {
601 "CONSTR_NOCAF_STATIC",
641 "MUT_ARR_PTRS_CLEAN",
642 "MUT_ARR_PTRS_DIRTY",
643 "MUT_ARR_PTRS_FROZEN",
658 "INVARIANT_CHECK_QUEUE",
669 info_type(StgClosure *closure){
670 return closure_type_names[get_itbl(closure)->type];
674 info_type_by_ip(StgInfoTable *ip){
675 return closure_type_names[ip->type];
679 info_hdr_type(StgClosure *closure, char *res){
680 strcpy(res,closure_type_names[get_itbl(closure)->type]);
683 /* --------------------------------------------------------------------------
684 * Address printing code
686 * Uses symbol table in (unstripped executable)
687 * ------------------------------------------------------------------------*/
689 /* --------------------------------------------------------------------------
690 * Simple lookup table
692 * Current implementation is pretty dumb!
693 * ------------------------------------------------------------------------*/
700 static nat table_size;
701 static struct entry* table;
704 static nat max_table_size;
706 static void reset_table( int size )
708 max_table_size = size;
710 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
713 static void prepare_table( void )
715 /* Could sort it... */
718 static void insert( StgWord value, const char *name )
720 if ( table_size >= max_table_size ) {
721 barf( "Symbol table overflow\n" );
723 table[table_size].value = value;
724 table[table_size].name = name;
725 table_size = table_size + 1;
730 static rtsBool lookup_name( char *name, StgWord *result )
733 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
735 if (i < table_size) {
736 *result = table[i].value;
744 /* Code from somewhere inside GHC (circa 1994)
746 * "std"++xs -> "Zstd"++xs
747 * char_to_c 'Z' = "ZZ"
748 * char_to_c '&' = "Za"
749 * char_to_c '|' = "Zb"
750 * char_to_c ':' = "Zc"
751 * char_to_c '/' = "Zd"
752 * char_to_c '=' = "Ze"
753 * char_to_c '>' = "Zg"
754 * char_to_c '#' = "Zh"
755 * char_to_c '<' = "Zl"
756 * char_to_c '-' = "Zm"
757 * char_to_c '!' = "Zn"
758 * char_to_c '.' = "Zo"
759 * char_to_c '+' = "Zp"
760 * char_to_c '\'' = "Zq"
761 * char_to_c '*' = "Zt"
762 * char_to_c '_' = "Zu"
763 * char_to_c c = "Z" ++ show (ord c)
765 static char unZcode( char ch )
768 case 'a' : return ('&');
769 case 'b' : return ('|');
770 case 'c' : return (':');
771 case 'd' : return ('/');
772 case 'e' : return ('=');
773 case 'g' : return ('>');
774 case 'h' : return ('#');
775 case 'l' : return ('<');
776 case 'm' : return ('-');
777 case 'n' : return ('!');
778 case 'o' : return ('.');
779 case 'p' : return ('+');
780 case 'q' : return ('\'');
781 case 't' : return ('*');
782 case 'u' : return ('_');
784 case '\0' : return ('Z');
785 default : return (ch);
790 /* Precondition: out big enough to handle output (about twice length of in) */
791 static void enZcode( char *in, char *out )
797 for( i = 0; in[i] != '\0'; ++i ) {
872 const char *lookupGHCName( void *addr )
875 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
877 if (i < table_size) {
878 return table[i].name;
884 static void printZcoded( const char *raw )
888 while ( raw[j] != '\0' ) {
890 debugBelch("%c", unZcode(raw[j+1]));
893 debugBelch("%c", unZcode(raw[j+1]));
899 /* --------------------------------------------------------------------------
900 * Symbol table loading
901 * ------------------------------------------------------------------------*/
903 /* Causing linking trouble on Win32 plats, so I'm
904 disabling this for now.
910 /* Fairly ad-hoc piece of code that seems to filter out a lot of
911 * rubbish like the obj-splitting symbols
914 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
917 /* ToDo: make this work on BFD */
918 int tp = type & N_TYPE;
919 if (tp == N_TEXT || tp == N_DATA) {
920 return (name[0] == '_' && name[1] != '_');
926 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
927 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
934 extern void DEBUG_LoadSymbols( char *name )
940 abfd = bfd_openr(name, "default");
942 barf("can't open executable %s to get symbol table", name);
944 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
950 asymbol **symbol_table;
951 long number_of_symbols;
952 long num_real_syms = 0;
955 storage_needed = bfd_get_symtab_upper_bound (abfd);
957 if (storage_needed < 0) {
958 barf("can't read symbol table");
961 if (storage_needed == 0) {
962 debugBelch("no storage needed");
965 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
967 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
969 if (number_of_symbols < 0) {
970 barf("can't canonicalise symbol table");
973 for( i = 0; i != number_of_symbols; ++i ) {
975 bfd_get_symbol_info(abfd,symbol_table[i],&info);
976 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
977 if (isReal(info.type, info.name)) {
982 IF_DEBUG(interpreter,
983 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
984 number_of_symbols, num_real_syms)
987 reset_table( num_real_syms );
989 for( i = 0; i != number_of_symbols; ++i ) {
991 bfd_get_symbol_info(abfd,symbol_table[i],&info);
992 if (isReal(info.type, info.name)) {
993 insert( info.value, info.name );
997 stgFree(symbol_table);
1002 #else /* HAVE_BFD_H */
1004 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1009 #endif /* HAVE_BFD_H */
1011 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1016 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
1019 for (; bd; bd = bd->link) {
1021 for (q = bd->start; q < bd->free; q++) {
1022 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
1025 while (HEAP_ALLOCED((StgPtr)*r) || !LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1028 debugBelch("%p = ", r);
1029 printClosure((StgClosure *)r);
1041 findPtr(P_ p, int follow)
1045 #if defined(__GNUC__)
1046 const int arr_size = 1024;
1048 #define arr_size 1024
1050 StgPtr arr[arr_size];
1054 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1055 for (s = 0; s < generations[g].n_steps; s++) {
1056 bd = generations[g].steps[s].blocks;
1057 i = findPtrBlocks(p,bd,arr,arr_size,i);
1058 bd = generations[g].steps[s].large_objects;
1059 i = findPtrBlocks(p,bd,arr,arr_size,i);
1060 if (i >= arr_size) return;
1063 if (follow && i == 1) {
1064 debugBelch("-->\n");
1069 /* prettyPrintClosure() is for printing out a closure using the data constructor
1070 names found in the info tables. Closures are printed in a fashion that resembles
1071 their Haskell representation. Useful during debugging.
1073 Todo: support for more closure types, and support for non pointer fields in the
1077 void prettyPrintClosure_ (StgClosure *);
1079 void prettyPrintClosure (StgClosure *obj)
1081 prettyPrintClosure_ (obj);
1085 void prettyPrintClosure_ (StgClosure *obj)
1088 StgConInfoTable *con_info;
1090 /* collapse any indirections */
1092 type = get_itbl(obj)->type;
1094 while (type == IND ||
1095 type == IND_STATIC ||
1096 type == IND_OLDGEN ||
1098 type == IND_OLDGEN_PERM)
1100 obj = ((StgInd *)obj)->indirectee;
1101 type = get_itbl(obj)->type;
1104 /* find the info table for this object */
1105 info = get_itbl(obj);
1107 /* determine what kind of object we have */
1110 /* full applications of data constructors */
1118 case CONSTR_NOCAF_STATIC:
1123 /* find the con_info for the constructor */
1124 con_info = get_con_itbl (obj);
1126 /* obtain the name of the constructor */
1127 descriptor = GET_CON_DESC(con_info);
1129 debugBelch ("(%s", descriptor);
1131 /* process the payload of the closure */
1132 /* we don't handle non pointers at the moment */
1133 for (i = 0; i < info->layout.payload.ptrs; i++)
1136 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1142 /* if it isn't a constructor then just print the closure type */
1145 debugBelch ("<%s>", info_type(obj));
1152 void printPtr( StgPtr p )
1154 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1157 void printObj( StgClosure *obj )
1159 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );