1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.44 2001/08/09 11:19:16 sewardj Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
19 #include "Bytecodes.h" /* for InstrPtr */
20 #include "Disassembler.h"
24 #if defined(GRAN) || defined(PAR)
25 // HWL: explicit fixed header size to make debugging easier
26 int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable),
27 uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame);
30 /* --------------------------------------------------------------------------
31 * local function decls
32 * ------------------------------------------------------------------------*/
34 static void printStdObject( StgClosure *obj, char* tag );
35 static void reset_table ( int size );
36 static void prepare_table ( void );
37 static void insert ( unsigned value, const char *name );
38 #if 0 /* unused but might be useful sometime */
39 static rtsBool lookup_name ( char *name, unsigned *result );
40 static void enZcode ( char *in, char *out );
42 static char unZcode ( char ch );
43 rtsBool lookupGHCName ( StgPtr addr, const char **result );
44 static void printZcoded ( const char *raw );
46 /* --------------------------------------------------------------------------
48 * ------------------------------------------------------------------------*/
50 void printPtr( StgPtr p )
53 if (lookupGHCName( p, &raw )) {
56 fprintf(stderr, "%p", p);
60 void printObj( StgClosure *obj )
62 fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
66 static void printStdObject( StgClosure *obj, char* tag )
69 const StgInfoTable* info = get_itbl(obj);
70 fprintf(stderr,"%s(",tag);
71 printPtr((StgPtr)obj->header.info);
73 fprintf(stderr,", %s", obj->header.prof.ccs->cc->label);
75 for (i = 0; i < info->layout.payload.ptrs; ++i) {
77 printPtr((StgPtr)obj->payload[i]);
79 for (j = 0; j < info->layout.payload.nptrs; ++j) {
80 fprintf(stderr,", %pd#",obj->payload[i+j]);
82 fprintf(stderr,")\n");
85 void printClosure( StgClosure *obj )
91 switch ( info->type ) {
93 barf("Invalid object");
95 disassemble( (StgBCO*)obj );
100 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
102 fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
103 for (i = 0; i < ap->n_args; ++i) {
104 fprintf(stderr,", ");
105 printPtr((P_)ap->payload[i]);
107 fprintf(stderr,")\n");
113 StgPAP* pap = stgCast(StgPAP*,obj);
115 fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
116 for (i = 0; i < pap->n_args; ++i) {
117 fprintf(stderr,", ");
118 printPtr((StgPtr)pap->payload[i]);
120 fprintf(stderr,")\n");
125 fprintf(stderr,"FOREIGN(");
126 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
127 fprintf(stderr,")\n");
131 fprintf(stderr,"IND(");
132 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
133 fprintf(stderr,")\n");
137 fprintf(stderr,"IND_STATIC(");
138 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
139 fprintf(stderr,")\n");
143 fprintf(stderr,"IND_OLDGEN(");
144 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
145 fprintf(stderr,")\n");
149 fprintf(stderr,"CAF_BH(");
150 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
151 fprintf(stderr,")\n");
155 fprintf(stderr,"SE_BH\n");
158 case SE_CAF_BLACKHOLE:
159 fprintf(stderr,"SE_CAF_BH\n");
163 fprintf(stderr,"BH\n");
167 fprintf(stderr,"BQ(");
168 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
169 fprintf(stderr,")\n");
173 fprintf(stderr,"TSO(");
174 fprintf(stderr,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
175 fprintf(stderr,")\n");
180 fprintf(stderr,"BLOCKED_FETCH(");
181 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
182 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
183 fprintf(stderr,")\n");
187 fprintf(stderr,"FETCH_ME(");
188 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
189 fprintf(stderr,")\n");
194 fprintf(stderr,"REMOTE_REF(");
195 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
196 fprintf(stderr,")\n");
201 fprintf(stderr,"FETCH_ME_BQ(");
202 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
203 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
204 fprintf(stderr,")\n");
207 #if defined(GRAN) || defined(PAR)
209 fprintf(stderr,"RBH(");
210 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
211 fprintf(stderr,")\n");
217 case CONSTR_1_0: case CONSTR_0_1:
218 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
220 case CONSTR_CHARLIKE:
222 case CONSTR_NOCAF_STATIC:
224 /* We can't use printStdObject because we want to print the
229 fprintf(stderr,"%s(", info->prof.closure_desc);
230 fprintf(stderr,"%s", obj->header.prof.ccs->cc->label);
232 fprintf(stderr,"CONSTR(");
233 printPtr((StgPtr)obj->header.info);
234 fprintf(stderr,"(tag=%d)",info->srt_len);
236 for (i = 0; i < info->layout.payload.ptrs; ++i) {
237 fprintf(stderr,", ");
238 printPtr((StgPtr)obj->payload[i]);
240 for (j = 0; j < info->layout.payload.nptrs; ++j) {
241 fprintf(stderr,", %p#", obj->payload[i+j]);
243 fprintf(stderr,")\n");
248 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
249 case MUT_ARR_PTRS_FROZEN:
252 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
254 fprintf(stderr,"Row<%i>(",p->ptrs);
255 for (i = 0; i < p->ptrs; ++i) {
256 if (i > 0) fprintf(stderr,", ");
257 printPtr((StgPtr)(p->payload[i]));
259 fprintf(stderr,")\n");
265 case FUN_1_0: case FUN_0_1:
266 case FUN_1_1: case FUN_0_2: case FUN_2_0:
268 printStdObject(obj,"FUN");
272 case THUNK_1_0: case THUNK_0_1:
273 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
275 /* ToDo: will this work for THUNK_STATIC too? */
277 printStdObject(obj,info->prof.closure_desc);
279 printStdObject(obj,"THUNK");
284 printStdObject(obj,"THUNK_SELECTOR");
290 fprintf(stderr,"ARR_WORDS(\"");
291 /* ToDo: we can't safely assume that this is a string!
292 for (i = 0; arrWordsGetChar(obj,i); ++i) {
293 putchar(arrWordsGetChar(obj,i));
295 for (i=0; i<((StgArrWords *)obj)->words; i++)
296 fprintf(stderr, "%ld", ((StgArrWords *)obj)->payload[i]);
297 fprintf(stderr,"\")\n");
303 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
304 fprintf(stderr,"UpdateFrame(");
305 printPtr((StgPtr)GET_INFO(u));
307 printPtr((StgPtr)u->updatee);
309 printPtr((StgPtr)u->link);
310 fprintf(stderr,")\n");
316 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
317 fprintf(stderr,"CatchFrame(");
318 printPtr((StgPtr)GET_INFO(u));
320 printPtr((StgPtr)u->handler);
322 printPtr((StgPtr)u->link);
323 fprintf(stderr,")\n");
329 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
330 fprintf(stderr,"SeqFrame(");
331 printPtr((StgPtr)GET_INFO(u));
333 printPtr((StgPtr)u->link);
334 fprintf(stderr,")\n");
340 StgStopFrame* u = stgCast(StgStopFrame*,obj);
341 fprintf(stderr,"StopFrame(");
342 printPtr((StgPtr)GET_INFO(u));
343 fprintf(stderr,")\n");
347 //barf("printClosure %d",get_itbl(obj)->type);
348 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
349 get_itbl(obj)->type );
350 barf("printClosure %d",get_itbl(obj)->type);
356 void printGraph( StgClosure *obj )
362 StgPtr printStackObj( StgPtr sp )
364 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
366 if (IS_ARG_TAG(*sp)) {
369 fprintf(stderr,"Tagged{");
370 for (i = 0; i < tag; i++) {
371 fprintf(stderr,"0x%x#", (unsigned)(*sp++));
372 if (i < tag-1) fprintf(stderr, ", ");
374 fprintf(stderr, "}\n");
376 StgClosure* c = (StgClosure*)(*sp);
377 printPtr((StgPtr)*sp);
378 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
379 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
381 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
382 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
384 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
385 fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
387 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
388 fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
390 if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
391 fprintf(stderr, "\t\t\tstg_ctoi_ret_V_info\n" );
393 if (get_itbl(c)->type == BCO) {
394 fprintf(stderr, "\t\t\t");
395 fprintf(stderr, "BCO(...)\n");
398 fprintf(stderr, "\t\t\t");
399 printClosure ( (StgClosure*)(*sp));
407 void printStackChunk( StgPtr sp, StgPtr spBottom )
410 const StgInfoTable *info;
412 ASSERT(sp <= spBottom);
413 while (sp < spBottom) {
414 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
415 info = get_itbl((StgClosure *)sp);
416 switch (info->type) {
419 printObj( stgCast(StgClosure*,sp) );
420 sp += sizeofW(StgUpdateFrame);
424 printObj( stgCast(StgClosure*,sp) );
425 sp += sizeofW(StgSeqFrame);
429 printObj( stgCast(StgClosure*,sp) );
430 sp += sizeofW(StgCatchFrame);
434 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
435 printObj( stgCast(StgClosure*,sp) );
439 fprintf(stderr, "RET_DYN (%p)\n", sp);
442 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
447 fprintf(stderr, "RET_SMALL (%p)\n", sp);
448 bitmap = info->layout.bitmap;
451 while (bitmap != 0) {
452 fprintf(stderr," stk[%ld] (%p) = ", spBottom-sp, sp);
453 if ((bitmap & 1) == 0) {
455 fprintf(stderr,"\n");
457 fprintf(stderr,"Word# %ld\n", *sp++);
460 bitmap = bitmap >> 1;
472 fprintf(stderr,"Stack[%ld] (%p) = ", spBottom-sp, sp);
473 sp = printStackObj(sp);
477 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
479 /* check everything down to the first update frame */
480 printStackChunk( sp, stgCast(StgPtr,su) );
481 while ( stgCast(StgPtr,su) < spBottom) {
482 sp = stgCast(StgPtr,su);
483 switch (get_itbl(su)->type) {
485 printObj( stgCast(StgClosure*,su) );
486 sp += sizeofW(StgUpdateFrame);
490 printObj( stgCast(StgClosure*,su) );
491 sp += sizeofW(StgSeqFrame);
492 su = stgCast(StgSeqFrame*,su)->link;
495 printObj( stgCast(StgClosure*,su) );
496 sp += sizeofW(StgCatchFrame);
497 su = stgCast(StgCatchFrame*,su)->link;
500 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
501 printObj( stgCast(StgClosure*,su) );
504 barf("printStack: weird record found on update frame list.");
506 printStackChunk( sp, stgCast(StgPtr,su) );
508 ASSERT(stgCast(StgPtr,su) == spBottom);
511 void printTSO( StgTSO *tso )
513 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
514 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
517 /* -----------------------------------------------------------------------------
520 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
521 -------------------------------------------------------------------------- */
523 static char *closure_type_names[] = {
524 "INVALID_OBJECT", /* 0 */
526 "CONSTR_1_0", /* 2 */
527 "CONSTR_0_1", /* 3 */
528 "CONSTR_2_0", /* 4 */
529 "CONSTR_1_1", /* 5 */
530 "CONSTR_0_2", /* 6 */
531 "CONSTR_INTLIKE", /* 7 */
532 "CONSTR_CHARLIKE", /* 8 */
533 "CONSTR_STATIC", /* 9 */
534 "CONSTR_NOCAF_STATIC", /* 10 */
541 "FUN_STATIC", /* 17 */
543 "THUNK_1_0", /* 19 */
544 "THUNK_0_1", /* 20 */
545 "THUNK_2_0", /* 21 */
546 "THUNK_1_1", /* 22 */
547 "THUNK_0_2", /* 23 */
548 "THUNK_STATIC", /* 24 */
549 "THUNK_SELECTOR", /* 25 */
554 "IND_OLDGEN", /* 30 */
556 "IND_OLDGEN_PERM", /* 32 */
557 "IND_STATIC", /* 33 */
558 "CAF_BLACKHOLE", /* 36 */
560 "RET_SMALL", /* 38 */
561 "RET_VEC_SMALL", /* 39 */
563 "RET_VEC_BIG", /* 41 */
565 "UPDATE_FRAME", /* 43 */
566 "CATCH_FRAME", /* 44 */
567 "STOP_FRAME", /* 45 */
568 "SEQ_FRAME", /* 46 */
569 "BLACKHOLE", /* 47 */
570 "BLACKHOLE_BQ", /* 48 */
571 "SE_BLACKHOLE", /* 49 */
572 "SE_CAF_BLACKHOLE", /* 50 */
574 "ARR_WORDS", /* 52 */
575 "MUT_ARR_PTRS", /* 53 */
576 "MUT_ARR_PTRS_FROZEN", /* 54 */
580 "STABLE_NAME", /* 58 */
582 "BLOCKED_FETCH", /* 60 */
584 "FETCH_ME_BQ", /* 62 */
586 "EVACUATED", /* 64 */
587 "REMOTE_REF", /* 65 */
588 "N_CLOSURE_TYPES" /* 66 */
592 info_type(StgClosure *closure){
593 return closure_type_names[get_itbl(closure)->type];
597 info_type_by_ip(StgInfoTable *ip){
598 return closure_type_names[ip->type];
602 info_hdr_type(StgClosure *closure, char *res){
603 strcpy(res,closure_type_names[get_itbl(closure)->type]);
606 /* --------------------------------------------------------------------------
607 * Address printing code
609 * Uses symbol table in (unstripped executable)
610 * ------------------------------------------------------------------------*/
612 /* --------------------------------------------------------------------------
613 * Simple lookup table
615 * Current implementation is pretty dumb!
616 * ------------------------------------------------------------------------*/
623 static nat max_table_size;
624 static nat table_size;
625 static struct entry* table;
627 static void reset_table( int size )
629 max_table_size = size;
631 table = (struct entry *) malloc(size * sizeof(struct entry));
634 static void prepare_table( void )
636 /* Could sort it... */
639 static void insert( unsigned value, const char *name )
641 if ( table_size >= max_table_size ) {
642 barf( "Symbol table overflow\n" );
644 table[table_size].value = value;
645 table[table_size].name = name;
646 table_size = table_size + 1;
651 static rtsBool lookup_name( char *name, unsigned *result )
654 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
656 if (i < table_size) {
657 *result = table[i].value;
665 /* Code from somewhere inside GHC (circa 1994)
667 * "std"++xs -> "Zstd"++xs
668 * char_to_c 'Z' = "ZZ"
669 * char_to_c '&' = "Za"
670 * char_to_c '|' = "Zb"
671 * char_to_c ':' = "Zc"
672 * char_to_c '/' = "Zd"
673 * char_to_c '=' = "Ze"
674 * char_to_c '>' = "Zg"
675 * char_to_c '#' = "Zh"
676 * char_to_c '<' = "Zl"
677 * char_to_c '-' = "Zm"
678 * char_to_c '!' = "Zn"
679 * char_to_c '.' = "Zo"
680 * char_to_c '+' = "Zp"
681 * char_to_c '\'' = "Zq"
682 * char_to_c '*' = "Zt"
683 * char_to_c '_' = "Zu"
684 * char_to_c c = "Z" ++ show (ord c)
686 static char unZcode( char ch )
689 case 'a' : return ('&');
690 case 'b' : return ('|');
691 case 'c' : return (':');
692 case 'd' : return ('/');
693 case 'e' : return ('=');
694 case 'g' : return ('>');
695 case 'h' : return ('#');
696 case 'l' : return ('<');
697 case 'm' : return ('-');
698 case 'n' : return ('!');
699 case 'o' : return ('.');
700 case 'p' : return ('+');
701 case 'q' : return ('\'');
702 case 't' : return ('*');
703 case 'u' : return ('_');
705 case '\0' : return ('Z');
706 default : return (ch);
711 /* Precondition: out big enough to handle output (about twice length of in) */
712 static void enZcode( char *in, char *out )
718 for( i = 0; in[i] != '\0'; ++i ) {
793 rtsBool lookupGHCName( StgPtr addr, const char **result )
796 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
798 if (i < table_size) {
799 *result = table[i].name;
806 static void printZcoded( const char *raw )
810 while ( raw[j] != '\0' ) {
812 fputc(unZcode(raw[j+1]),stderr);
815 fputc(raw[j],stderr);
821 /* --------------------------------------------------------------------------
822 * Symbol table loading
823 * ------------------------------------------------------------------------*/
825 /* Causing linking trouble on Win32 plats, so I'm
826 disabling this for now.
832 /* Fairly ad-hoc piece of code that seems to filter out a lot of
833 * rubbish like the obj-splitting symbols
836 static rtsBool isReal( flagword flags, const char *name )
839 /* ToDo: make this work on BFD */
840 int tp = type & N_TYPE;
841 if (tp == N_TEXT || tp == N_DATA) {
842 return (name[0] == '_' && name[1] != '_');
847 (void)flags; /* keep gcc -Wall happy */
849 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
850 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
857 extern void DEBUG_LoadSymbols( char *name )
863 abfd = bfd_openr(name, "default");
865 barf("can't open executable %s to get symbol table", name);
867 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
873 asymbol **symbol_table;
874 long number_of_symbols;
875 long num_real_syms = 0;
878 storage_needed = bfd_get_symtab_upper_bound (abfd);
880 if (storage_needed < 0) {
881 barf("can't read symbol table");
884 if (storage_needed == 0) {
885 belch("no storage needed");
888 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
890 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
892 if (number_of_symbols < 0) {
893 barf("can't canonicalise symbol table");
896 for( i = 0; i != number_of_symbols; ++i ) {
898 bfd_get_symbol_info(abfd,symbol_table[i],&info);
899 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
900 if (isReal(info.type, info.name)) {
906 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
907 number_of_symbols, num_real_syms)
910 reset_table( num_real_syms );
912 for( i = 0; i != number_of_symbols; ++i ) {
914 bfd_get_symbol_info(abfd,symbol_table[i],&info);
915 if (isReal(info.type, info.name)) {
916 insert( info.value, info.name );
925 #else /* HAVE_BFD_H */
927 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
932 #endif /* HAVE_BFD_H */
934 #include "StoragePriv.h"
936 void findPtr(P_ p, int); /* keep gcc -Wall happy */
939 findPtr(P_ p, int follow)
944 const int arr_size = 1024;
945 StgPtr arr[arr_size];
948 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
949 for (s = 0; s < generations[g].n_steps; s++) {
950 if (RtsFlags.GcFlags.generations == 1) {
951 bd = generations[g].steps[s].to_blocks;
953 bd = generations[g].steps[s].blocks;
955 for (; bd; bd = bd->link) {
956 for (q = bd->start; q < bd->free; q++) {
960 while (!LOOKS_LIKE_GHC_INFO(*r)) { r--; };
961 fprintf(stderr, "%p = ", r);
962 printClosure((StgClosure *)r);
972 if (follow && i == 1) {
973 fprintf(stderr, "-->\n");
979 void printPtr( StgPtr p )
981 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
984 void printObj( StgClosure *obj )
986 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );