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 )
124 obj = UNTAG_CLOSURE(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_NOCAF_STATIC:
142 debugBelch("%s(", GET_PROF_DESC(info));
143 debugBelch("%s", obj->header.prof.ccs->cc->label);
145 debugBelch("CONSTR(");
146 printPtr((StgPtr)obj->header.info);
147 debugBelch("(tag=%d)",info->srt_bitmap);
149 for (i = 0; i < info->layout.payload.ptrs; ++i) {
151 printPtr((StgPtr)obj->payload[i]);
153 for (j = 0; j < info->layout.payload.nptrs; ++j) {
154 debugBelch(", %p#", obj->payload[i+j]);
161 case FUN_1_0: case FUN_0_1:
162 case FUN_1_1: case FUN_0_2: case FUN_2_0:
164 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
165 printPtr((StgPtr)obj->header.info);
167 debugBelch(", %s", obj->header.prof.ccs->cc->label);
169 printStdObjPayload(obj);
173 case THUNK_1_0: case THUNK_0_1:
174 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
176 /* ToDo: will this work for THUNK_STATIC too? */
178 printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
180 printThunkObject((StgThunk *)obj,"THUNK");
185 printStdObjHdr(obj, "THUNK_SELECTOR");
186 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
190 disassemble( (StgBCO*)obj );
195 StgAP* ap = stgCast(StgAP*,obj);
197 debugBelch("AP("); printPtr((StgPtr)ap->fun);
198 for (i = 0; i < ap->n_args; ++i) {
200 printPtr((P_)ap->payload[i]);
208 StgPAP* pap = stgCast(StgPAP*,obj);
210 debugBelch("PAP/%d(",pap->arity);
211 printPtr((StgPtr)pap->fun);
212 for (i = 0; i < pap->n_args; ++i) {
214 printPtr((StgPtr)pap->payload[i]);
222 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
224 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
225 for (i = 0; i < ap->size; ++i) {
227 printPtr((P_)ap->payload[i]);
235 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
240 debugBelch("IND_OLDGEN(");
241 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
247 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
251 case IND_OLDGEN_PERM:
252 debugBelch("IND_OLDGEN_PERM(");
253 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
258 debugBelch("IND_STATIC(");
259 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
263 /* Cannot happen -- use default case.
273 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
274 debugBelch("UPDATE_FRAME(");
275 printPtr((StgPtr)GET_INFO(u));
277 printPtr((StgPtr)u->updatee);
284 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
285 debugBelch("CATCH_FRAME(");
286 printPtr((StgPtr)GET_INFO(u));
288 printPtr((StgPtr)u->handler);
295 StgStopFrame* u = stgCast(StgStopFrame*,obj);
296 debugBelch("STOP_FRAME(");
297 printPtr((StgPtr)GET_INFO(u));
303 debugBelch("CAF_BH");
313 debugBelch("ARR_WORDS(\"");
314 /* ToDo: we can't safely assume that this is a string!
315 for (i = 0; arrWordsGetChar(obj,i); ++i) {
316 putchar(arrWordsGetChar(obj,i));
318 for (i=0; i<((StgArrWords *)obj)->words; i++)
319 debugBelch("%lu", (lnat)((StgArrWords *)obj)->payload[i]);
324 case MUT_ARR_PTRS_CLEAN:
325 debugBelch("MUT_ARR_PTRS_CLEAN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
328 case MUT_ARR_PTRS_DIRTY:
329 debugBelch("MUT_ARR_PTRS_DIRTY(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
332 case MUT_ARR_PTRS_FROZEN:
333 debugBelch("MUT_ARR_PTRS_FROZEN(size=%lu)\n", (lnat)((StgMutArrPtrs *)obj)->ptrs);
339 StgMVar* mv = (StgMVar*)obj;
340 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
346 StgMutVar* mv = (StgMutVar*)obj;
347 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
353 StgMutVar* mv = (StgMutVar*)obj;
354 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
360 debugBelch(" key=%p value=%p finalizer=%p",
361 (StgPtr)(((StgWeak*)obj)->key),
362 (StgPtr)(((StgWeak*)obj)->value),
363 (StgPtr)(((StgWeak*)obj)->finalizer));
365 /* ToDo: chase 'link' ? */
369 debugBelch("STABLE_NAME(%lu)\n", (lnat)((StgStableName*)obj)->sn);
374 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
380 debugBelch("BLOCKED_FETCH(");
381 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
382 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
387 debugBelch("FETCH_ME(");
388 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
393 debugBelch("FETCH_ME_BQ(");
394 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
395 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
400 #if defined(GRAN) || defined(PAR)
403 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
410 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
412 debugBelch("EVACUATED(");
413 printClosure((StgEvacuated*)obj->evacuee);
418 #if defined(PAR) && defined(DIST)
420 debugBelch("REMOTE_REF(");
421 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
427 //barf("printClosure %d",get_itbl(obj)->type);
428 debugBelch("*** printClosure: unknown type %d ****\n",
429 get_itbl(obj)->type );
430 barf("printClosure %d",get_itbl(obj)->type);
436 void printGraph( StgClosure *obj )
443 printStackObj( StgPtr sp )
445 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
447 StgClosure* c = (StgClosure*)(*sp);
448 printPtr((StgPtr)*sp);
449 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
450 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
452 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
453 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
455 if (c == (StgClosure*)&stg_ctoi_F1_info) {
456 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
458 if (c == (StgClosure*)&stg_ctoi_D1_info) {
459 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
461 if (c == (StgClosure*)&stg_ctoi_V_info) {
462 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
464 if (get_itbl(c)->type == BCO) {
465 debugBelch("\t\t\t");
466 debugBelch("BCO(...)\n");
469 debugBelch("\t\t\t");
470 printClosure ( (StgClosure*)(*sp));
479 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
485 for(i = 0; i < size; i++, bitmap >>= 1 ) {
486 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
487 if ((bitmap & 1) == 0) {
488 printPtr((P_)payload[i]);
491 debugBelch("Word# %lu\n", (lnat)payload[i]);
497 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
503 for (bmp=0; i < size; bmp++) {
504 StgWord bitmap = large_bitmap->bitmap[bmp];
506 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
507 debugBelch(" stk[%lu] (%p) = ", (lnat)(spBottom-(payload+i)), payload+i);
508 if ((bitmap & 1) == 0) {
509 printPtr((P_)payload[i]);
512 debugBelch("Word# %lu\n", (lnat)payload[i]);
519 printStackChunk( StgPtr sp, StgPtr spBottom )
522 const StgInfoTable *info;
524 ASSERT(sp <= spBottom);
525 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
527 info = get_itbl((StgClosure *)sp);
529 switch (info->type) {
533 printObj((StgClosure*)sp);
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);
572 debugBelch("RET_SMALL (%p)\n", info);
573 bitmap = info->layout.bitmap;
574 printSmallBitmap(spBottom, sp+1,
575 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
581 bco = ((StgBCO *)sp[1]);
583 debugBelch("RET_BCO (%p)\n", sp);
584 printLargeBitmap(spBottom, sp+2,
585 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
594 StgFunInfoTable *fun_info;
598 ret_fun = (StgRetFun *)sp;
599 fun_info = get_fun_itbl(ret_fun->fun);
600 size = ret_fun->size;
601 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, fun_info->f.fun_type);
602 switch (fun_info->f.fun_type) {
604 printSmallBitmap(spBottom, sp+2,
605 BITMAP_BITS(fun_info->f.b.bitmap),
606 BITMAP_SIZE(fun_info->f.b.bitmap));
609 printLargeBitmap(spBottom, sp+2,
610 GET_FUN_LARGE_BITMAP(fun_info),
611 GET_FUN_LARGE_BITMAP(fun_info)->size);
614 printSmallBitmap(spBottom, sp+2,
615 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
616 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
623 debugBelch("unknown object %d\n", info->type);
624 barf("printStackChunk");
629 void printTSO( StgTSO *tso )
631 printStackChunk( tso->sp, tso->stack+tso->stack_size);
634 /* -----------------------------------------------------------------------------
637 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
638 -------------------------------------------------------------------------- */
640 static char *closure_type_names[] = {
649 "CONSTR_NOCAF_STATIC",
689 "MUT_ARR_PTRS_CLEAN",
690 "MUT_ARR_PTRS_DIRTY",
691 "MUT_ARR_PTRS_FROZEN",
706 "INVARIANT_CHECK_QUEUE",
717 info_type(StgClosure *closure){
718 return closure_type_names[get_itbl(closure)->type];
722 info_type_by_ip(StgInfoTable *ip){
723 return closure_type_names[ip->type];
727 info_hdr_type(StgClosure *closure, char *res){
728 strcpy(res,closure_type_names[get_itbl(closure)->type]);
731 /* --------------------------------------------------------------------------
732 * Address printing code
734 * Uses symbol table in (unstripped executable)
735 * ------------------------------------------------------------------------*/
737 /* --------------------------------------------------------------------------
738 * Simple lookup table
740 * Current implementation is pretty dumb!
741 * ------------------------------------------------------------------------*/
748 static nat table_size;
749 static struct entry* table;
752 static nat max_table_size;
754 static void reset_table( int size )
756 max_table_size = size;
758 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
761 static void prepare_table( void )
763 /* Could sort it... */
766 static void insert( StgWord value, const char *name )
768 if ( table_size >= max_table_size ) {
769 barf( "Symbol table overflow\n" );
771 table[table_size].value = value;
772 table[table_size].name = name;
773 table_size = table_size + 1;
778 static rtsBool lookup_name( char *name, StgWord *result )
781 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
783 if (i < table_size) {
784 *result = table[i].value;
792 /* Code from somewhere inside GHC (circa 1994)
794 * "std"++xs -> "Zstd"++xs
795 * char_to_c 'Z' = "ZZ"
796 * char_to_c '&' = "Za"
797 * char_to_c '|' = "Zb"
798 * char_to_c ':' = "Zc"
799 * char_to_c '/' = "Zd"
800 * char_to_c '=' = "Ze"
801 * char_to_c '>' = "Zg"
802 * char_to_c '#' = "Zh"
803 * char_to_c '<' = "Zl"
804 * char_to_c '-' = "Zm"
805 * char_to_c '!' = "Zn"
806 * char_to_c '.' = "Zo"
807 * char_to_c '+' = "Zp"
808 * char_to_c '\'' = "Zq"
809 * char_to_c '*' = "Zt"
810 * char_to_c '_' = "Zu"
811 * char_to_c c = "Z" ++ show (ord c)
813 static char unZcode( char ch )
816 case 'a' : return ('&');
817 case 'b' : return ('|');
818 case 'c' : return (':');
819 case 'd' : return ('/');
820 case 'e' : return ('=');
821 case 'g' : return ('>');
822 case 'h' : return ('#');
823 case 'l' : return ('<');
824 case 'm' : return ('-');
825 case 'n' : return ('!');
826 case 'o' : return ('.');
827 case 'p' : return ('+');
828 case 'q' : return ('\'');
829 case 't' : return ('*');
830 case 'u' : return ('_');
832 case '\0' : return ('Z');
833 default : return (ch);
838 /* Precondition: out big enough to handle output (about twice length of in) */
839 static void enZcode( char *in, char *out )
845 for( i = 0; in[i] != '\0'; ++i ) {
920 const char *lookupGHCName( void *addr )
923 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
925 if (i < table_size) {
926 return table[i].name;
932 static void printZcoded( const char *raw )
936 while ( raw[j] != '\0' ) {
938 debugBelch("%c", unZcode(raw[j+1]));
941 debugBelch("%c", unZcode(raw[j+1]));
947 /* --------------------------------------------------------------------------
948 * Symbol table loading
949 * ------------------------------------------------------------------------*/
951 /* Causing linking trouble on Win32 plats, so I'm
952 disabling this for now.
958 /* Fairly ad-hoc piece of code that seems to filter out a lot of
959 * rubbish like the obj-splitting symbols
962 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
965 /* ToDo: make this work on BFD */
966 int tp = type & N_TYPE;
967 if (tp == N_TEXT || tp == N_DATA) {
968 return (name[0] == '_' && name[1] != '_');
974 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
975 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
982 extern void DEBUG_LoadSymbols( char *name )
988 abfd = bfd_openr(name, "default");
990 barf("can't open executable %s to get symbol table", name);
992 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
998 asymbol **symbol_table;
999 long number_of_symbols;
1000 long num_real_syms = 0;
1003 storage_needed = bfd_get_symtab_upper_bound (abfd);
1005 if (storage_needed < 0) {
1006 barf("can't read symbol table");
1009 if (storage_needed == 0) {
1010 debugBelch("no storage needed");
1013 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1015 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1017 if (number_of_symbols < 0) {
1018 barf("can't canonicalise symbol table");
1021 for( i = 0; i != number_of_symbols; ++i ) {
1023 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1024 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1025 if (isReal(info.type, info.name)) {
1030 IF_DEBUG(interpreter,
1031 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1032 number_of_symbols, num_real_syms)
1035 reset_table( num_real_syms );
1037 for( i = 0; i != number_of_symbols; ++i ) {
1039 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1040 if (isReal(info.type, info.name)) {
1041 insert( info.value, info.name );
1045 stgFree(symbol_table);
1050 #else /* HAVE_BFD_H */
1052 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1057 #endif /* HAVE_BFD_H */
1059 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1064 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
1067 for (; bd; bd = bd->link) {
1069 for (q = bd->start; q < bd->free; q++) {
1070 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
1073 while (HEAP_ALLOCED((StgPtr)*r) || !LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1076 debugBelch("%p = ", r);
1077 printClosure((StgClosure *)r);
1089 findPtr(P_ p, int follow)
1093 #if defined(__GNUC__)
1094 const int arr_size = 1024;
1096 #define arr_size 1024
1098 StgPtr arr[arr_size];
1102 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1103 for (s = 0; s < generations[g].n_steps; s++) {
1104 bd = generations[g].steps[s].blocks;
1105 i = findPtrBlocks(p,bd,arr,arr_size,i);
1106 bd = generations[g].steps[s].large_objects;
1107 i = findPtrBlocks(p,bd,arr,arr_size,i);
1108 if (i >= arr_size) return;
1111 if (follow && i == 1) {
1112 debugBelch("-->\n");
1117 /* prettyPrintClosure() is for printing out a closure using the data constructor
1118 names found in the info tables. Closures are printed in a fashion that resembles
1119 their Haskell representation. Useful during debugging.
1121 Todo: support for more closure types, and support for non pointer fields in the
1125 void prettyPrintClosure_ (StgClosure *);
1127 void prettyPrintClosure (StgClosure *obj)
1129 prettyPrintClosure_ (obj);
1133 void prettyPrintClosure_ (StgClosure *obj)
1136 StgConInfoTable *con_info;
1138 /* collapse any indirections */
1140 type = get_itbl(obj)->type;
1142 while (type == IND ||
1143 type == IND_STATIC ||
1144 type == IND_OLDGEN ||
1146 type == IND_OLDGEN_PERM)
1148 obj = ((StgInd *)obj)->indirectee;
1149 type = get_itbl(obj)->type;
1152 /* find the info table for this object */
1153 info = get_itbl(obj);
1155 /* determine what kind of object we have */
1158 /* full applications of data constructors */
1166 case CONSTR_NOCAF_STATIC:
1171 /* find the con_info for the constructor */
1172 con_info = get_con_itbl (obj);
1174 /* obtain the name of the constructor */
1175 descriptor = GET_CON_DESC(con_info);
1177 debugBelch ("(%s", descriptor);
1179 /* process the payload of the closure */
1180 /* we don't handle non pointers at the moment */
1181 for (i = 0; i < info->layout.payload.ptrs; i++)
1184 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1190 /* if it isn't a constructor then just print the closure type */
1193 debugBelch ("<%s>", info_type(obj));
1200 void printPtr( StgPtr p )
1202 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1205 void printObj( StgClosure *obj )
1207 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );