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 printStdObject( StgClosure *obj, char* tag );
37 static void printStdObjPayload( StgClosure *obj );
39 static void reset_table ( int size );
40 static void prepare_table ( void );
41 static void insert ( unsigned value, const char *name );
43 #if 0 /* unused but might be useful sometime */
44 static rtsBool lookup_name ( char *name, unsigned *result );
45 static void enZcode ( char *in, char *out );
47 static char unZcode ( char ch );
48 const char * lookupGHCName ( void *addr );
49 static void printZcoded ( const char *raw );
51 /* --------------------------------------------------------------------------
53 * ------------------------------------------------------------------------*/
55 void printPtr( StgPtr p )
58 raw = lookupGHCName(p);
66 void printObj( StgClosure *obj )
68 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
73 printStdObjHdr( StgClosure *obj, char* tag )
75 debugBelch("%s(",tag);
76 printPtr((StgPtr)obj->header.info);
78 debugBelch(", %s", obj->header.prof.ccs->cc->label);
83 printStdObjPayload( StgClosure *obj )
86 const StgInfoTable* info;
89 for (i = 0; i < info->layout.payload.ptrs; ++i) {
91 printPtr((StgPtr)obj->payload[i]);
93 for (j = 0; j < info->layout.payload.nptrs; ++j) {
94 debugBelch(", %pd#",obj->payload[i+j]);
100 printStdObject( StgClosure *obj, char* tag )
102 printStdObjHdr( obj, tag );
103 printStdObjPayload( obj );
107 printClosure( StgClosure *obj )
111 info = get_itbl(obj);
113 switch ( info->type ) {
115 barf("Invalid object");
118 case CONSTR_1_0: case CONSTR_0_1:
119 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
121 case CONSTR_CHARLIKE:
123 case CONSTR_NOCAF_STATIC:
125 /* We can't use printStdObject because we want to print the
130 debugBelch("%s(", info->prof.closure_desc);
131 debugBelch("%s", obj->header.prof.ccs->cc->label);
133 debugBelch("CONSTR(");
134 printPtr((StgPtr)obj->header.info);
135 debugBelch("(tag=%d)",info->srt_bitmap);
137 for (i = 0; i < info->layout.payload.ptrs; ++i) {
139 printPtr((StgPtr)obj->payload[i]);
141 for (j = 0; j < info->layout.payload.nptrs; ++j) {
142 debugBelch(", %p#", obj->payload[i+j]);
149 case FUN_1_0: case FUN_0_1:
150 case FUN_1_1: case FUN_0_2: case FUN_2_0:
152 debugBelch("FUN/%d(",itbl_to_fun_itbl(info)->f.arity);
153 printPtr((StgPtr)obj->header.info);
155 debugBelch(", %s", obj->header.prof.ccs->cc->label);
157 printStdObjPayload(obj);
161 case THUNK_1_0: case THUNK_0_1:
162 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
164 /* ToDo: will this work for THUNK_STATIC too? */
166 printStdObject(obj,info->prof.closure_desc);
168 printStdObject(obj,"THUNK");
173 printStdObjHdr(obj, "THUNK_SELECTOR");
174 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
178 disassemble( (StgBCO*)obj );
183 StgPAP* ap = stgCast(StgPAP*,obj);
185 debugBelch("AP("); printPtr((StgPtr)ap->fun);
186 for (i = 0; i < ap->n_args; ++i) {
188 printPtr((P_)ap->payload[i]);
196 StgPAP* pap = stgCast(StgPAP*,obj);
198 debugBelch("PAP/%d(",pap->arity);
199 printPtr((StgPtr)pap->fun);
200 for (i = 0; i < pap->n_args; ++i) {
202 printPtr((StgPtr)pap->payload[i]);
210 StgAP_STACK* ap = stgCast(StgAP_STACK*,obj);
212 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
213 for (i = 0; i < ap->size; ++i) {
215 printPtr((P_)ap->payload[i]);
223 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
228 debugBelch("IND_OLDGEN(");
229 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
235 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
239 case IND_OLDGEN_PERM:
240 debugBelch("IND_OLDGEN_PERM(");
241 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
246 debugBelch("IND_STATIC(");
247 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
251 /* Cannot happen -- use default case.
263 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
264 debugBelch("UPDATE_FRAME(");
265 printPtr((StgPtr)GET_INFO(u));
267 printPtr((StgPtr)u->updatee);
274 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
275 debugBelch("CATCH_FRAME(");
276 printPtr((StgPtr)GET_INFO(u));
278 printPtr((StgPtr)u->handler);
285 StgStopFrame* u = stgCast(StgStopFrame*,obj);
286 debugBelch("STOP_FRAME(");
287 printPtr((StgPtr)GET_INFO(u));
293 debugBelch("CAF_BH");
301 debugBelch("SE_BH\n");
304 case SE_CAF_BLACKHOLE:
305 debugBelch("SE_CAF_BH\n");
311 debugBelch("ARR_WORDS(\"");
312 /* ToDo: we can't safely assume that this is a string!
313 for (i = 0; arrWordsGetChar(obj,i); ++i) {
314 putchar(arrWordsGetChar(obj,i));
316 for (i=0; i<((StgArrWords *)obj)->words; i++)
317 debugBelch("%lu", ((StgArrWords *)obj)->payload[i]);
323 debugBelch("MUT_ARR_PTRS(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
326 case MUT_ARR_PTRS_FROZEN:
327 #if !defined(XMLAMBDA)
328 debugBelch("MUT_ARR_PTRS_FROZEN(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
332 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
334 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
336 debugBelch("Row<%i>(",p->ptrs);
337 for (i = 0; i < p->ptrs; ++i) {
338 if (i > 0) debugBelch(", ");
339 printPtr((StgPtr)(p->payload[i]));
348 StgMVar* mv = (StgMVar*)obj;
349 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
355 StgMutVar* mv = (StgMutVar*)obj;
356 debugBelch("MUT_VAR(var=%p)\n", mv->var);
362 debugBelch(" key=%p value=%p finalizer=%p",
363 (StgPtr)(((StgWeak*)obj)->key),
364 (StgPtr)(((StgWeak*)obj)->value),
365 (StgPtr)(((StgWeak*)obj)->finalizer));
367 /* ToDo: chase 'link' ? */
371 debugBelch("FOREIGN(");
372 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
377 debugBelch("STABLE_NAME(%ld)\n", ((StgStableName*)obj)->sn);
382 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
388 debugBelch("BLOCKED_FETCH(");
389 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
390 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
395 debugBelch("FETCH_ME(");
396 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
401 debugBelch("FETCH_ME_BQ(");
402 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
403 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
408 #if defined(GRAN) || defined(PAR)
411 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
418 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
420 debugBelch("EVACUATED(");
421 printClosure((StgEvacuated*)obj->evacuee);
426 #if defined(PAR) && defined(DIST)
428 debugBelch("REMOTE_REF(");
429 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
435 //barf("printClosure %d",get_itbl(obj)->type);
436 debugBelch("*** printClosure: unknown type %d ****\n",
437 get_itbl(obj)->type );
438 barf("printClosure %d",get_itbl(obj)->type);
444 void printGraph( StgClosure *obj )
451 printStackObj( StgPtr sp )
453 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
455 StgClosure* c = (StgClosure*)(*sp);
456 printPtr((StgPtr)*sp);
457 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
458 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
460 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
461 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
463 if (c == (StgClosure*)&stg_ctoi_F1_info) {
464 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
466 if (c == (StgClosure*)&stg_ctoi_D1_info) {
467 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
469 if (c == (StgClosure*)&stg_ctoi_V_info) {
470 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
472 if (get_itbl(c)->type == BCO) {
473 debugBelch("\t\t\t");
474 debugBelch("BCO(...)\n");
477 debugBelch("\t\t\t");
478 printClosure ( (StgClosure*)(*sp));
487 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
493 for(i = 0; i < size; i++, bitmap >>= 1 ) {
494 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
495 if ((bitmap & 1) == 0) {
496 printPtr((P_)payload[i]);
499 debugBelch("Word# %ld\n", payload[i]);
505 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
511 for (bmp=0; i < size; bmp++) {
512 StgWord bitmap = large_bitmap->bitmap[bmp];
514 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
515 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
516 if ((bitmap & 1) == 0) {
517 printPtr((P_)payload[i]);
520 debugBelch("Word# %ld\n", payload[i]);
527 printStackChunk( StgPtr sp, StgPtr spBottom )
530 const StgInfoTable *info;
532 ASSERT(sp <= spBottom);
533 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
535 info = get_itbl((StgClosure *)sp);
537 switch (info->type) {
542 printObj((StgClosure*)sp);
554 debugBelch("RET_DYN (%p)\n", r);
556 p = (P_)(r->payload);
557 printSmallBitmap(spBottom, sp,
558 RET_DYN_LIVENESS(r->liveness),
559 RET_DYN_BITMAP_SIZE);
560 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
562 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
563 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
564 debugBelch("Word# %ld\n", (long)*p);
568 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
569 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
578 debugBelch("RET_SMALL (%p)\n", sp);
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));
601 StgFunInfoTable *fun_info;
605 ret_fun = (StgRetFun *)sp;
606 fun_info = get_fun_itbl(ret_fun->fun);
607 size = ret_fun->size;
608 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
609 switch (fun_info->f.fun_type) {
611 printSmallBitmap(spBottom, sp+1,
612 BITMAP_BITS(fun_info->f.b.bitmap),
613 BITMAP_SIZE(fun_info->f.b.bitmap));
616 printLargeBitmap(spBottom, sp+2,
617 GET_FUN_LARGE_BITMAP(fun_info),
618 GET_FUN_LARGE_BITMAP(fun_info)->size);
621 printSmallBitmap(spBottom, sp+1,
622 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
623 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
630 debugBelch("unknown object %d\n", info->type);
631 barf("printStackChunk");
636 void printTSO( StgTSO *tso )
638 printStackChunk( tso->sp, tso->stack+tso->stack_size);
641 /* -----------------------------------------------------------------------------
644 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
645 -------------------------------------------------------------------------- */
647 static char *closure_type_names[] = {
658 "CONSTR_NOCAF_STATIC",
701 "MUT_ARR_PTRS_FROZEN",
724 info_type(StgClosure *closure){
725 return closure_type_names[get_itbl(closure)->type];
729 info_type_by_ip(StgInfoTable *ip){
730 return closure_type_names[ip->type];
734 info_hdr_type(StgClosure *closure, char *res){
735 strcpy(res,closure_type_names[get_itbl(closure)->type]);
738 /* --------------------------------------------------------------------------
739 * Address printing code
741 * Uses symbol table in (unstripped executable)
742 * ------------------------------------------------------------------------*/
744 /* --------------------------------------------------------------------------
745 * Simple lookup table
747 * Current implementation is pretty dumb!
748 * ------------------------------------------------------------------------*/
755 static nat table_size;
756 static struct entry* table;
759 static nat max_table_size;
761 static void reset_table( int size )
763 max_table_size = size;
765 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
768 static void prepare_table( void )
770 /* Could sort it... */
773 static void insert( unsigned value, const char *name )
775 if ( table_size >= max_table_size ) {
776 barf( "Symbol table overflow\n" );
778 table[table_size].value = value;
779 table[table_size].name = name;
780 table_size = table_size + 1;
785 static rtsBool lookup_name( char *name, unsigned *result )
788 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
790 if (i < table_size) {
791 *result = table[i].value;
799 /* Code from somewhere inside GHC (circa 1994)
801 * "std"++xs -> "Zstd"++xs
802 * char_to_c 'Z' = "ZZ"
803 * char_to_c '&' = "Za"
804 * char_to_c '|' = "Zb"
805 * char_to_c ':' = "Zc"
806 * char_to_c '/' = "Zd"
807 * char_to_c '=' = "Ze"
808 * char_to_c '>' = "Zg"
809 * char_to_c '#' = "Zh"
810 * char_to_c '<' = "Zl"
811 * char_to_c '-' = "Zm"
812 * char_to_c '!' = "Zn"
813 * char_to_c '.' = "Zo"
814 * char_to_c '+' = "Zp"
815 * char_to_c '\'' = "Zq"
816 * char_to_c '*' = "Zt"
817 * char_to_c '_' = "Zu"
818 * char_to_c c = "Z" ++ show (ord c)
820 static char unZcode( char ch )
823 case 'a' : return ('&');
824 case 'b' : return ('|');
825 case 'c' : return (':');
826 case 'd' : return ('/');
827 case 'e' : return ('=');
828 case 'g' : return ('>');
829 case 'h' : return ('#');
830 case 'l' : return ('<');
831 case 'm' : return ('-');
832 case 'n' : return ('!');
833 case 'o' : return ('.');
834 case 'p' : return ('+');
835 case 'q' : return ('\'');
836 case 't' : return ('*');
837 case 'u' : return ('_');
839 case '\0' : return ('Z');
840 default : return (ch);
845 /* Precondition: out big enough to handle output (about twice length of in) */
846 static void enZcode( char *in, char *out )
852 for( i = 0; in[i] != '\0'; ++i ) {
927 const char *lookupGHCName( void *addr )
930 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
932 if (i < table_size) {
933 return table[i].name;
939 static void printZcoded( const char *raw )
943 while ( raw[j] != '\0' ) {
945 debugBelch("%c", unZcode(raw[j+1]));
948 debugBelch("%c", unZcode(raw[j+1]));
954 /* --------------------------------------------------------------------------
955 * Symbol table loading
956 * ------------------------------------------------------------------------*/
958 /* Causing linking trouble on Win32 plats, so I'm
959 disabling this for now.
965 /* Fairly ad-hoc piece of code that seems to filter out a lot of
966 * rubbish like the obj-splitting symbols
969 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
972 /* ToDo: make this work on BFD */
973 int tp = type & N_TYPE;
974 if (tp == N_TEXT || tp == N_DATA) {
975 return (name[0] == '_' && name[1] != '_');
981 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
982 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
989 extern void DEBUG_LoadSymbols( char *name )
995 abfd = bfd_openr(name, "default");
997 barf("can't open executable %s to get symbol table", name);
999 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1004 long storage_needed;
1005 asymbol **symbol_table;
1006 long number_of_symbols;
1007 long num_real_syms = 0;
1010 storage_needed = bfd_get_symtab_upper_bound (abfd);
1012 if (storage_needed < 0) {
1013 barf("can't read symbol table");
1016 if (storage_needed == 0) {
1017 debugBelch("no storage needed");
1020 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1022 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1024 if (number_of_symbols < 0) {
1025 barf("can't canonicalise symbol table");
1028 for( i = 0; i != number_of_symbols; ++i ) {
1030 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1031 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1032 if (isReal(info.type, info.name)) {
1037 IF_DEBUG(interpreter,
1038 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1039 number_of_symbols, num_real_syms)
1042 reset_table( num_real_syms );
1044 for( i = 0; i != number_of_symbols; ++i ) {
1046 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1047 if (isReal(info.type, info.name)) {
1048 insert( info.value, info.name );
1052 stgFree(symbol_table);
1057 #else /* HAVE_BFD_H */
1059 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1064 #endif /* HAVE_BFD_H */
1066 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1069 findPtr(P_ p, int follow)
1074 #if defined(__GNUC__)
1075 const int arr_size = 1024;
1077 #define arr_size 1024
1079 StgPtr arr[arr_size];
1082 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1083 for (s = 0; s < generations[g].n_steps; s++) {
1084 if (RtsFlags.GcFlags.generations == 1) {
1085 bd = generations[g].steps[s].to_blocks;
1087 bd = generations[g].steps[s].blocks;
1089 for (; bd; bd = bd->link) {
1090 for (q = bd->start; q < bd->free; q++) {
1094 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1097 debugBelch("%p = ", r);
1098 printClosure((StgClosure *)r);
1108 if (follow && i == 1) {
1109 debugBelch("-->\n");
1115 void printPtr( StgPtr p )
1117 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1120 void printObj( StgClosure *obj )
1122 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );