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(");
294 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
304 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
309 debugBelch("SE_BH\n");
312 case SE_CAF_BLACKHOLE:
313 debugBelch("SE_CAF_BH\n");
319 debugBelch("ARR_WORDS(\"");
320 /* ToDo: we can't safely assume that this is a string!
321 for (i = 0; arrWordsGetChar(obj,i); ++i) {
322 putchar(arrWordsGetChar(obj,i));
324 for (i=0; i<((StgArrWords *)obj)->words; i++)
325 debugBelch("%lu", ((StgArrWords *)obj)->payload[i]);
331 debugBelch("MUT_ARR_PTRS(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
334 case MUT_ARR_PTRS_FROZEN:
335 #if !defined(XMLAMBDA)
336 debugBelch("MUT_ARR_PTRS_FROZEN(size=%ld)\n", ((StgMutArrPtrs *)obj)->ptrs);
340 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
342 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
344 debugBelch("Row<%i>(",p->ptrs);
345 for (i = 0; i < p->ptrs; ++i) {
346 if (i > 0) debugBelch(", ");
347 printPtr((StgPtr)(p->payload[i]));
356 StgMVar* mv = (StgMVar*)obj;
357 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
363 StgMutVar* mv = (StgMutVar*)obj;
364 debugBelch("MUT_VAR(var=%p)\n", mv->var);
370 debugBelch(" key=%p value=%p finalizer=%p",
371 (StgPtr)(((StgWeak*)obj)->key),
372 (StgPtr)(((StgWeak*)obj)->value),
373 (StgPtr)(((StgWeak*)obj)->finalizer));
375 /* ToDo: chase 'link' ? */
379 debugBelch("FOREIGN(");
380 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
385 debugBelch("STABLE_NAME(%ld)\n", ((StgStableName*)obj)->sn);
390 debugBelch("%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
396 debugBelch("BLOCKED_FETCH(");
397 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
398 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
403 debugBelch("FETCH_ME(");
404 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
409 debugBelch("FETCH_ME_BQ(");
410 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
411 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
416 #if defined(GRAN) || defined(PAR)
419 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
426 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
428 debugBelch("EVACUATED(");
429 printClosure((StgEvacuated*)obj->evacuee);
434 #if defined(PAR) && defined(DIST)
436 debugBelch("REMOTE_REF(");
437 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
443 //barf("printClosure %d",get_itbl(obj)->type);
444 debugBelch("*** printClosure: unknown type %d ****\n",
445 get_itbl(obj)->type );
446 barf("printClosure %d",get_itbl(obj)->type);
452 void printGraph( StgClosure *obj )
459 printStackObj( StgPtr sp )
461 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
463 StgClosure* c = (StgClosure*)(*sp);
464 printPtr((StgPtr)*sp);
465 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
466 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
468 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
469 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
471 if (c == (StgClosure*)&stg_ctoi_F1_info) {
472 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
474 if (c == (StgClosure*)&stg_ctoi_D1_info) {
475 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
477 if (c == (StgClosure*)&stg_ctoi_V_info) {
478 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
480 if (get_itbl(c)->type == BCO) {
481 debugBelch("\t\t\t");
482 debugBelch("BCO(...)\n");
485 debugBelch("\t\t\t");
486 printClosure ( (StgClosure*)(*sp));
495 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
501 for(i = 0; i < size; i++, bitmap >>= 1 ) {
502 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
503 if ((bitmap & 1) == 0) {
504 printPtr((P_)payload[i]);
507 debugBelch("Word# %ld\n", payload[i]);
513 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
519 for (bmp=0; i < size; bmp++) {
520 StgWord bitmap = large_bitmap->bitmap[bmp];
522 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
523 debugBelch(" stk[%ld] (%p) = ", spBottom-(payload+i), payload+i);
524 if ((bitmap & 1) == 0) {
525 printPtr((P_)payload[i]);
528 debugBelch("Word# %ld\n", payload[i]);
535 printStackChunk( StgPtr sp, StgPtr spBottom )
538 const StgInfoTable *info;
540 ASSERT(sp <= spBottom);
541 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
543 info = get_itbl((StgClosure *)sp);
545 switch (info->type) {
550 printObj((StgClosure*)sp);
562 debugBelch("RET_DYN (%p)\n", r);
564 p = (P_)(r->payload);
565 printSmallBitmap(spBottom, sp,
566 RET_DYN_LIVENESS(r->liveness),
567 RET_DYN_BITMAP_SIZE);
568 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
570 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
571 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
572 debugBelch("Word# %ld\n", (long)*p);
576 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
577 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
586 debugBelch("RET_SMALL (%p)\n", sp);
587 bitmap = info->layout.bitmap;
588 printSmallBitmap(spBottom, sp+1,
589 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
595 bco = ((StgBCO *)sp[1]);
597 debugBelch("RET_BCO (%p)\n", sp);
598 printLargeBitmap(spBottom, sp+2,
599 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
609 StgFunInfoTable *fun_info;
613 ret_fun = (StgRetFun *)sp;
614 fun_info = get_fun_itbl(ret_fun->fun);
615 size = ret_fun->size;
616 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun, fun_info->f.fun_type);
617 switch (fun_info->f.fun_type) {
619 printSmallBitmap(spBottom, sp+1,
620 BITMAP_BITS(fun_info->f.b.bitmap),
621 BITMAP_SIZE(fun_info->f.b.bitmap));
624 printLargeBitmap(spBottom, sp+2,
625 GET_FUN_LARGE_BITMAP(fun_info),
626 GET_FUN_LARGE_BITMAP(fun_info)->size);
629 printSmallBitmap(spBottom, sp+1,
630 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
631 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
638 debugBelch("unknown object %d\n", info->type);
639 barf("printStackChunk");
644 void printTSO( StgTSO *tso )
646 printStackChunk( tso->sp, tso->stack+tso->stack_size);
649 /* -----------------------------------------------------------------------------
652 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
653 -------------------------------------------------------------------------- */
655 static char *closure_type_names[] = {
666 "CONSTR_NOCAF_STATIC",
709 "MUT_ARR_PTRS_FROZEN",
732 info_type(StgClosure *closure){
733 return closure_type_names[get_itbl(closure)->type];
737 info_type_by_ip(StgInfoTable *ip){
738 return closure_type_names[ip->type];
742 info_hdr_type(StgClosure *closure, char *res){
743 strcpy(res,closure_type_names[get_itbl(closure)->type]);
746 /* --------------------------------------------------------------------------
747 * Address printing code
749 * Uses symbol table in (unstripped executable)
750 * ------------------------------------------------------------------------*/
752 /* --------------------------------------------------------------------------
753 * Simple lookup table
755 * Current implementation is pretty dumb!
756 * ------------------------------------------------------------------------*/
763 static nat table_size;
764 static struct entry* table;
767 static nat max_table_size;
769 static void reset_table( int size )
771 max_table_size = size;
773 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
776 static void prepare_table( void )
778 /* Could sort it... */
781 static void insert( unsigned value, const char *name )
783 if ( table_size >= max_table_size ) {
784 barf( "Symbol table overflow\n" );
786 table[table_size].value = value;
787 table[table_size].name = name;
788 table_size = table_size + 1;
793 static rtsBool lookup_name( char *name, unsigned *result )
796 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
798 if (i < table_size) {
799 *result = table[i].value;
807 /* Code from somewhere inside GHC (circa 1994)
809 * "std"++xs -> "Zstd"++xs
810 * char_to_c 'Z' = "ZZ"
811 * char_to_c '&' = "Za"
812 * char_to_c '|' = "Zb"
813 * char_to_c ':' = "Zc"
814 * char_to_c '/' = "Zd"
815 * char_to_c '=' = "Ze"
816 * char_to_c '>' = "Zg"
817 * char_to_c '#' = "Zh"
818 * char_to_c '<' = "Zl"
819 * char_to_c '-' = "Zm"
820 * char_to_c '!' = "Zn"
821 * char_to_c '.' = "Zo"
822 * char_to_c '+' = "Zp"
823 * char_to_c '\'' = "Zq"
824 * char_to_c '*' = "Zt"
825 * char_to_c '_' = "Zu"
826 * char_to_c c = "Z" ++ show (ord c)
828 static char unZcode( char ch )
831 case 'a' : return ('&');
832 case 'b' : return ('|');
833 case 'c' : return (':');
834 case 'd' : return ('/');
835 case 'e' : return ('=');
836 case 'g' : return ('>');
837 case 'h' : return ('#');
838 case 'l' : return ('<');
839 case 'm' : return ('-');
840 case 'n' : return ('!');
841 case 'o' : return ('.');
842 case 'p' : return ('+');
843 case 'q' : return ('\'');
844 case 't' : return ('*');
845 case 'u' : return ('_');
847 case '\0' : return ('Z');
848 default : return (ch);
853 /* Precondition: out big enough to handle output (about twice length of in) */
854 static void enZcode( char *in, char *out )
860 for( i = 0; in[i] != '\0'; ++i ) {
935 const char *lookupGHCName( void *addr )
938 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
940 if (i < table_size) {
941 return table[i].name;
947 static void printZcoded( const char *raw )
951 while ( raw[j] != '\0' ) {
953 debugBelch("%c", unZcode(raw[j+1]));
956 debugBelch("%c", unZcode(raw[j+1]));
962 /* --------------------------------------------------------------------------
963 * Symbol table loading
964 * ------------------------------------------------------------------------*/
966 /* Causing linking trouble on Win32 plats, so I'm
967 disabling this for now.
973 /* Fairly ad-hoc piece of code that seems to filter out a lot of
974 * rubbish like the obj-splitting symbols
977 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
980 /* ToDo: make this work on BFD */
981 int tp = type & N_TYPE;
982 if (tp == N_TEXT || tp == N_DATA) {
983 return (name[0] == '_' && name[1] != '_');
989 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
990 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
997 extern void DEBUG_LoadSymbols( char *name )
1003 abfd = bfd_openr(name, "default");
1005 barf("can't open executable %s to get symbol table", name);
1007 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
1012 long storage_needed;
1013 asymbol **symbol_table;
1014 long number_of_symbols;
1015 long num_real_syms = 0;
1018 storage_needed = bfd_get_symtab_upper_bound (abfd);
1020 if (storage_needed < 0) {
1021 barf("can't read symbol table");
1024 if (storage_needed == 0) {
1025 debugBelch("no storage needed");
1028 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
1030 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1032 if (number_of_symbols < 0) {
1033 barf("can't canonicalise symbol table");
1036 for( i = 0; i != number_of_symbols; ++i ) {
1038 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1039 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
1040 if (isReal(info.type, info.name)) {
1045 IF_DEBUG(interpreter,
1046 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
1047 number_of_symbols, num_real_syms)
1050 reset_table( num_real_syms );
1052 for( i = 0; i != number_of_symbols; ++i ) {
1054 bfd_get_symbol_info(abfd,symbol_table[i],&info);
1055 if (isReal(info.type, info.name)) {
1056 insert( info.value, info.name );
1060 stgFree(symbol_table);
1065 #else /* HAVE_BFD_H */
1067 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
1072 #endif /* HAVE_BFD_H */
1074 void findPtr(P_ p, int); /* keep gcc -Wall happy */
1077 findPtr(P_ p, int follow)
1082 #if defined(__GNUC__)
1083 const int arr_size = 1024;
1085 #define arr_size 1024
1087 StgPtr arr[arr_size];
1090 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1091 for (s = 0; s < generations[g].n_steps; s++) {
1092 if (RtsFlags.GcFlags.generations == 1) {
1093 bd = generations[g].steps[s].to_blocks;
1095 bd = generations[g].steps[s].blocks;
1097 for (; bd; bd = bd->link) {
1098 for (q = bd->start; q < bd->free; q++) {
1102 while (!LOOKS_LIKE_INFO_PTR(*r) || (P_)*r == NULL) {
1105 debugBelch("%p = ", r);
1106 printClosure((StgClosure *)r);
1116 if (follow && i == 1) {
1117 debugBelch("-->\n");
1123 void printPtr( StgPtr p )
1125 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1128 void printObj( StgClosure *obj )
1130 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );