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", ((StgArrWords *)obj)->payload[i]);
336 debugBelch("MUT_ARR_PTRS(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
339 case MUT_ARR_PTRS_FROZEN:
340 #if !defined(XMLAMBDA)
341 debugBelch("MUT_ARR_PTRS_FROZEN(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
345 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
347 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
349 debugBelch("Row<%i>(",p->ptrs);
350 for (i = 0; i < p->ptrs; ++i) {
351 if (i > 0) debugBelch(", ");
352 printPtr((StgPtr)(p->payload[i]));
361 StgMVar* mv = (StgMVar*)obj;
362 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
368 StgMutVar* mv = (StgMutVar*)obj;
369 debugBelch("MUT_VAR(var=%p)\n", mv->var);
375 debugBelch(" key=%p value=%p finalizer=%p",
376 (StgPtr)(((StgWeak*)obj)->key),
377 (StgPtr)(((StgWeak*)obj)->value),
378 (StgPtr)(((StgWeak*)obj)->finalizer));
380 /* ToDo: chase 'link' ? */
384 debugBelch("STABLE_NAME(%ld)\n", ((StgStableName*)obj)->sn);
389 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
395 debugBelch("BLOCKED_FETCH(");
396 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
397 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
402 debugBelch("FETCH_ME(");
403 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
408 debugBelch("FETCH_ME_BQ(");
409 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
410 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
415 #if defined(GRAN) || defined(PAR)
418 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
425 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
427 debugBelch("EVACUATED(");
428 printClosure((StgEvacuated*)obj->evacuee);
433 #if defined(PAR) && defined(DIST)
435 debugBelch("REMOTE_REF(");
436 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
442 //barf("printClosure %d",get_itbl(obj)->type);
443 debugBelch("*** printClosure: unknown type %d ****\n",
444 get_itbl(obj)->type );
445 barf("printClosure %d",get_itbl(obj)->type);
451 void printGraph( StgClosure *obj )
458 printStackObj( StgPtr sp )
460 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
462 StgClosure* c = (StgClosure*)(*sp);
463 printPtr((StgPtr)*sp);
464 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
465 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
467 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
468 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
470 if (c == (StgClosure*)&stg_ctoi_F1_info) {
471 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
473 if (c == (StgClosure*)&stg_ctoi_D1_info) {
474 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
476 if (c == (StgClosure*)&stg_ctoi_V_info) {
477 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
479 if (get_itbl(c)->type == BCO) {
480 debugBelch("\t\t\t");
481 debugBelch("BCO(...)\n");
484 debugBelch("\t\t\t");
485 printClosure ( (StgClosure*)(*sp));
494 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
500 for(i = 0; i < size; i++, bitmap >>= 1 ) {
501 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
502 if ((bitmap & 1) == 0) {
503 printPtr((P_)payload[i]);
506 debugBelch("Word# %ld\n", payload[i]);
512 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
518 for (bmp=0; i < size; bmp++) {
519 StgWord bitmap = large_bitmap->bitmap[bmp];
521 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
522 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
523 if ((bitmap & 1) == 0) {
524 printPtr((P_)payload[i]);
527 debugBelch("Word# %ld\n", payload[i]);
534 printStackChunk( StgPtr sp, StgPtr spBottom )
537 const StgInfoTable *info;
539 ASSERT(sp <= spBottom);
540 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
542 info = get_itbl((StgClosure *)sp);
544 switch (info->type) {
549 printObj((StgClosure*)sp);
561 debugBelch("RET_DYN (%p)\n", r);
563 p = (P_)(r->payload);
564 printSmallBitmap(spBottom, sp,
565 RET_DYN_LIVENESS(r->liveness),
566 RET_DYN_BITMAP_SIZE);
567 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
569 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
570 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
571 debugBelch("Word# %ld\n", (long)*p);
575 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
576 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
585 debugBelch("RET_SMALL (%p)\n", sp);
586 bitmap = info->layout.bitmap;
587 printSmallBitmap(spBottom, sp+1,
588 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
594 bco = ((StgBCO *)sp[1]);
596 debugBelch("RET_BCO (%p)\n", sp);
597 printLargeBitmap(spBottom, sp+2,
598 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
608 StgFunInfoTable *fun_info;
612 ret_fun = (StgRetFun *)sp;
613 fun_info = get_fun_itbl(ret_fun->fun);
614 size = ret_fun->size;
615 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
616 switch (fun_info->f.fun_type) {
618 printSmallBitmap(spBottom, sp+1,
619 BITMAP_BITS(fun_info->f.b.bitmap),
620 BITMAP_SIZE(fun_info->f.b.bitmap));
623 printLargeBitmap(spBottom, sp+2,
624 GET_FUN_LARGE_BITMAP(fun_info),
625 GET_FUN_LARGE_BITMAP(fun_info)->size);
628 printSmallBitmap(spBottom, sp+1,
629 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
630 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
637 debugBelch("unknown object %d\n", info->type);
638 barf("printStackChunk");
643 void printTSO( StgTSO *tso )
645 printStackChunk( tso->sp, tso->stack+tso->stack_size);
648 /* -----------------------------------------------------------------------------
651 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
652 -------------------------------------------------------------------------- */
654 static char *closure_type_names[] = {
665 "CONSTR_NOCAF_STATIC",
708 "MUT_ARR_PTRS_FROZEN",
731 info_type(StgClosure *closure){
732 return closure_type_names[get_itbl(closure)->type];
736 info_type_by_ip(StgInfoTable *ip){
737 return closure_type_names[ip->type];
741 info_hdr_type(StgClosure *closure, char *res){
742 strcpy(res,closure_type_names[get_itbl(closure)->type]);
745 /* --------------------------------------------------------------------------
746 * Address printing code
748 * Uses symbol table in (unstripped executable)
749 * ------------------------------------------------------------------------*/
751 /* --------------------------------------------------------------------------
752 * Simple lookup table
754 * Current implementation is pretty dumb!
755 * ------------------------------------------------------------------------*/
762 static nat table_size;
763 static struct entry* table;
766 static nat max_table_size;
768 static void reset_table( int size )
770 max_table_size = size;
772 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
775 static void prepare_table( void )
777 /* Could sort it... */
780 static void insert( unsigned value, const char *name )
782 if ( table_size >= max_table_size ) {
783 barf( "Symbol table overflow\n" );
785 table[table_size].value = value;
786 table[table_size].name = name;
787 table_size = table_size + 1;
792 static rtsBool lookup_name( char *name, unsigned *result )
795 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
797 if (i < table_size) {
798 *result = table[i].value;
806 /* Code from somewhere inside GHC (circa 1994)
808 * "std"++xs -> "Zstd"++xs
809 * char_to_c 'Z' = "ZZ"
810 * char_to_c '&' = "Za"
811 * char_to_c '|' = "Zb"
812 * char_to_c ':' = "Zc"
813 * char_to_c '/' = "Zd"
814 * char_to_c '=' = "Ze"
815 * char_to_c '>' = "Zg"
816 * char_to_c '#' = "Zh"
817 * char_to_c '<' = "Zl"
818 * char_to_c '-' = "Zm"
819 * char_to_c '!' = "Zn"
820 * char_to_c '.' = "Zo"
821 * char_to_c '+' = "Zp"
822 * char_to_c '\'' = "Zq"
823 * char_to_c '*' = "Zt"
824 * char_to_c '_' = "Zu"
825 * char_to_c c = "Z" ++ show (ord c)
827 static char unZcode( char ch )
830 case 'a' : return ('&');
831 case 'b' : return ('|');
832 case 'c' : return (':');
833 case 'd' : return ('/');
834 case 'e' : return ('=');
835 case 'g' : return ('>');
836 case 'h' : return ('#');
837 case 'l' : return ('<');
838 case 'm' : return ('-');
839 case 'n' : return ('!');
840 case 'o' : return ('.');
841 case 'p' : return ('+');
842 case 'q' : return ('\'');
843 case 't' : return ('*');
844 case 'u' : return ('_');
846 case '\0' : return ('Z');
847 default : return (ch);
852 /* Precondition: out big enough to handle output (about twice length of in) */
853 static void enZcode( char *in, char *out )
859 for( i = 0; in[i] != '\0'; ++i ) {
934 const char *lookupGHCName( void *addr )
937 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
939 if (i < table_size) {
940 return table[i].name;
946 static void printZcoded( const char *raw )
950 while ( raw[j] != '\0' ) {
952 debugBelch("%c", unZcode(raw[j+1]));
955 debugBelch("%c", unZcode(raw[j+1]));
961 /* --------------------------------------------------------------------------
962 * Symbol table loading
963 * ------------------------------------------------------------------------*/
965 /* Causing linking trouble on Win32 plats, so I'm
966 disabling this for now.
972 /* Fairly ad-hoc piece of code that seems to filter out a lot of
973 * rubbish like the obj-splitting symbols
976 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
979 /* ToDo: make this work on BFD */
980 int tp = type & N_TYPE;
981 if (tp == N_TEXT || tp == N_DATA) {
982 return (name[0] == '_' && name[1] != '_');
988 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
989 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
996 extern void DEBUG_LoadSymbols( char *name )
1002 abfd = bfd_openr(name, "default");
1004 barf("can't open executable %s to get symbol table", name);
1006 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1011 long storage_needed;
1012 asymbol **symbol_table;
1013 long number_of_symbols;
1014 long num_real_syms = 0;
1017 storage_needed = bfd_get_symtab_upper_bound (abfd);
1019 if (storage_needed < 0) {
1020 barf("can't read symbol table");
1023 if (storage_needed == 0) {
1024 debugBelch("no storage needed");
1027 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1029 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1031 if (number_of_symbols < 0) {
1032 barf("can't canonicalise symbol table");
1035 for( i = 0; i != number_of_symbols; ++i ) {
1037 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1038 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1039 if (isReal(info.type, info.name)) {
1044 IF_DEBUG(interpreter,
1045 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1046 number_of_symbols, num_real_syms)
1049 reset_table( num_real_syms );
1051 for( i = 0; i != number_of_symbols; ++i ) {
1053 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1054 if (isReal(info.type, info.name)) {
1055 insert( info.value, info.name );
1059 stgFree(symbol_table);
1064 #else /* HAVE_BFD_H */
1066 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1071 #endif /* HAVE_BFD_H */
1073 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1076 findPtr(P_ p, int follow)
1081 #if defined(__GNUC__)
1082 const int arr_size = 1024;
1084 #define arr_size 1024
1086 StgPtr arr[arr_size];
1089 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1090 for (s = 0; s < generations[g].n_steps; s++) {
1091 bd = generations[g].steps[s].blocks;
1092 for (; bd; bd = bd->link) {
1093 for (q = bd->start; q < bd->free; q++) {
1097 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1100 debugBelch("%p = ", r);
1101 printClosure((StgClosure *)r);
1111 if (follow && i == 1) {
1112 debugBelch("-->\n");
1118 void printPtr( StgPtr p )
1120 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1123 void printObj( StgClosure *obj )
1125 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );