1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.38 2001/03/22 03:51:10 hwloidl 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);
72 for (i = 0; i < info->layout.payload.ptrs; ++i) {
74 printPtr((StgPtr)obj->payload[i]);
76 for (j = 0; j < info->layout.payload.nptrs; ++j) {
77 fprintf(stderr,", %pd#",obj->payload[i+j]);
79 fprintf(stderr,")\n");
82 void printClosure( StgClosure *obj )
84 switch ( get_itbl(obj)->type ) {
86 barf("Invalid object");
88 disassemble( (StgBCO*)obj );
93 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
95 fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
96 for (i = 0; i < ap->n_args; ++i) {
98 printPtr((P_)ap->payload[i]);
100 fprintf(stderr,")\n");
106 StgPAP* pap = stgCast(StgPAP*,obj);
108 fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
109 for (i = 0; i < pap->n_args; ++i) {
110 fprintf(stderr,", ");
111 printPtr((StgPtr)pap->payload[i]);
113 fprintf(stderr,")\n");
118 fprintf(stderr,"IND(");
119 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
120 fprintf(stderr,")\n");
124 fprintf(stderr,"IND_STATIC(");
125 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
126 fprintf(stderr,")\n");
130 fprintf(stderr,"IND_OLDGEN(");
131 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
132 fprintf(stderr,")\n");
136 fprintf(stderr,"CAF_BH(");
137 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
138 fprintf(stderr,")\n");
142 fprintf(stderr,"SE_BH\n");
145 case SE_CAF_BLACKHOLE:
146 fprintf(stderr,"SE_CAF_BH\n");
150 fprintf(stderr,"BH\n");
154 fprintf(stderr,"BQ(");
155 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
156 fprintf(stderr,")\n");
160 fprintf(stderr,"TSO(");
161 fprintf(stderr,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
162 fprintf(stderr,")\n");
167 fprintf(stderr,"BLOCKED_FETCH(");
168 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
169 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
170 fprintf(stderr,")\n");
174 fprintf(stderr,"FETCH_ME(");
175 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
176 fprintf(stderr,")\n");
181 fprintf(stderr,"REMOTE_REF(");
182 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
183 fprintf(stderr,")\n");
188 fprintf(stderr,"FETCH_ME_BQ(");
189 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
190 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
191 fprintf(stderr,")\n");
194 #if defined(GRAN) || defined(PAR)
196 fprintf(stderr,"RBH(");
197 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
198 fprintf(stderr,")\n");
204 case CONSTR_1_0: case CONSTR_0_1:
205 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
207 case CONSTR_CHARLIKE:
209 case CONSTR_NOCAF_STATIC:
211 /* We can't use printStdObject because we want to print the
215 const StgInfoTable* info = get_itbl(obj);
216 fprintf(stderr,"PACK(");
217 printPtr((StgPtr)obj->header.info);
218 fprintf(stderr,"(tag=%d)",info->srt_len);
219 for (i = 0; i < info->layout.payload.ptrs; ++i) {
220 fprintf(stderr,", ");
221 printPtr((StgPtr)obj->payload[i]);
223 for (j = 0; j < info->layout.payload.nptrs; ++j) {
224 fprintf(stderr,", %p#", obj->payload[i+j]);
226 fprintf(stderr,")\n");
231 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
232 case MUT_ARR_PTRS_FROZEN:
235 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
237 fprintf(stderr,"Row<%i>(",p->ptrs);
238 for (i = 0; i < p->ptrs; ++i) {
239 if (i > 0) fprintf(stderr,", ");
240 printPtr((StgPtr)(p->payload[i]));
242 fprintf(stderr,")\n");
248 case FUN_1_0: case FUN_0_1:
249 case FUN_1_1: case FUN_0_2: case FUN_2_0:
251 printStdObject(obj,"FUN");
255 case THUNK_1_0: case THUNK_0_1:
256 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
258 /* ToDo: will this work for THUNK_STATIC too? */
259 printStdObject(obj,"THUNK");
263 printStdObject(obj,"THUNK_SELECTOR");
269 fprintf(stderr,"ARR_WORDS(\"");
270 /* ToDo: we can't safely assume that this is a string!
271 for (i = 0; arrWordsGetChar(obj,i); ++i) {
272 putchar(arrWordsGetChar(obj,i));
274 for (i=0; i<((StgArrWords *)obj)->words; i++)
275 fprintf(stderr, "%d", ((StgArrWords *)obj)->payload[i]);
276 fprintf(stderr,"\")\n");
282 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
283 fprintf(stderr,"UpdateFrame(");
284 printPtr((StgPtr)GET_INFO(u));
286 printPtr((StgPtr)u->updatee);
288 printPtr((StgPtr)u->link);
289 fprintf(stderr,")\n");
295 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
296 fprintf(stderr,"CatchFrame(");
297 printPtr((StgPtr)GET_INFO(u));
299 printPtr((StgPtr)u->handler);
301 printPtr((StgPtr)u->link);
302 fprintf(stderr,")\n");
308 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
309 fprintf(stderr,"SeqFrame(");
310 printPtr((StgPtr)GET_INFO(u));
312 printPtr((StgPtr)u->link);
313 fprintf(stderr,")\n");
319 StgStopFrame* u = stgCast(StgStopFrame*,obj);
320 fprintf(stderr,"StopFrame(");
321 printPtr((StgPtr)GET_INFO(u));
322 fprintf(stderr,")\n");
326 //barf("printClosure %d",get_itbl(obj)->type);
327 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
328 get_itbl(obj)->type );
334 void printGraph( StgClosure *obj )
340 StgPtr printStackObj( StgPtr sp )
342 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
344 if (IS_ARG_TAG(*sp)) {
347 fprintf(stderr,"Tagged{");
348 for (i = 0; i < tag; i++) {
349 fprintf(stderr,"0x%x#", (unsigned)(*sp++));
350 if (i < tag-1) fprintf(stderr, ", ");
352 fprintf(stderr, "}\n");
354 StgClosure* c = (StgClosure*)(*sp);
355 printPtr((StgPtr)*sp);
356 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
357 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
359 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
360 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
362 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
363 fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
365 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
366 fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
368 if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
369 fprintf(stderr, "\t\t\tstg_ctoi_ret_V_info\n" );
371 if (get_itbl(c)->type == BCO) {
372 fprintf(stderr, "\t\t\t");
373 fprintf(stderr, "BCO(...)\n");
376 fprintf(stderr, "\t\t\t");
377 printClosure ( (StgClosure*)(*sp));
385 void printStackChunk( StgPtr sp, StgPtr spBottom )
388 const StgInfoTable *info;
390 ASSERT(sp <= spBottom);
391 while (sp < spBottom) {
392 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
393 info = get_itbl((StgClosure *)sp);
394 switch (info->type) {
397 printObj( stgCast(StgClosure*,sp) );
398 sp += sizeofW(StgUpdateFrame);
402 printObj( stgCast(StgClosure*,sp) );
403 sp += sizeofW(StgSeqFrame);
407 printObj( stgCast(StgClosure*,sp) );
408 sp += sizeofW(StgCatchFrame);
412 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
413 printObj( stgCast(StgClosure*,sp) );
417 fprintf(stderr, "RET_DYN (%p)\n", sp);
420 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
425 fprintf(stderr, "RET_SMALL (%p)\n", sp);
426 bitmap = info->layout.bitmap;
429 while (bitmap != 0) {
430 fprintf(stderr," stk[%d] (%p) = ", spBottom-sp, sp);
431 if ((bitmap & 1) == 0) {
433 fprintf(stderr,"\n");
435 fprintf(stderr,"Word# %d\n", *sp++);
438 bitmap = bitmap >> 1;
450 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
451 sp = printStackObj(sp);
455 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
457 /* check everything down to the first update frame */
458 printStackChunk( sp, stgCast(StgPtr,su) );
459 while ( stgCast(StgPtr,su) < spBottom) {
460 sp = stgCast(StgPtr,su);
461 switch (get_itbl(su)->type) {
463 printObj( stgCast(StgClosure*,su) );
464 sp += sizeofW(StgUpdateFrame);
468 printObj( stgCast(StgClosure*,su) );
469 sp += sizeofW(StgSeqFrame);
470 su = stgCast(StgSeqFrame*,su)->link;
473 printObj( stgCast(StgClosure*,su) );
474 sp += sizeofW(StgCatchFrame);
475 su = stgCast(StgCatchFrame*,su)->link;
478 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
479 printObj( stgCast(StgClosure*,su) );
482 barf("printStack: weird record found on update frame list.");
484 printStackChunk( sp, stgCast(StgPtr,su) );
486 ASSERT(stgCast(StgPtr,su) == spBottom);
489 void printTSO( StgTSO *tso )
491 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
492 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
495 /* -----------------------------------------------------------------------------
498 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
499 -------------------------------------------------------------------------- */
501 static char *closure_type_names[] = {
502 "INVALID_OBJECT", /* 0 */
504 "CONSTR_1_0", /* 2 */
505 "CONSTR_0_1", /* 3 */
506 "CONSTR_2_0", /* 4 */
507 "CONSTR_1_1", /* 5 */
508 "CONSTR_0_2", /* 6 */
509 "CONSTR_INTLIKE", /* 7 */
510 "CONSTR_CHARLIKE", /* 8 */
511 "CONSTR_STATIC", /* 9 */
512 "CONSTR_NOCAF_STATIC", /* 10 */
519 "FUN_STATIC", /* 17 */
521 "THUNK_1_0", /* 19 */
522 "THUNK_0_1", /* 20 */
523 "THUNK_2_0", /* 21 */
524 "THUNK_1_1", /* 22 */
525 "THUNK_0_2", /* 23 */
526 "THUNK_STATIC", /* 24 */
527 "THUNK_SELECTOR", /* 25 */
532 "IND_OLDGEN", /* 30 */
534 "IND_OLDGEN_PERM", /* 32 */
535 "IND_STATIC", /* 33 */
536 "CAF_BLACKHOLE", /* 36 */
538 "RET_SMALL", /* 38 */
539 "RET_VEC_SMALL", /* 39 */
541 "RET_VEC_BIG", /* 41 */
543 "UPDATE_FRAME", /* 43 */
544 "CATCH_FRAME", /* 44 */
545 "STOP_FRAME", /* 45 */
546 "SEQ_FRAME", /* 46 */
547 "BLACKHOLE", /* 47 */
548 "BLACKHOLE_BQ", /* 48 */
549 "SE_BLACKHOLE", /* 49 */
550 "SE_CAF_BLACKHOLE", /* 50 */
552 "ARR_WORDS", /* 52 */
553 "MUT_ARR_PTRS", /* 53 */
554 "MUT_ARR_PTRS_FROZEN", /* 54 */
558 "STABLE_NAME", /* 58 */
560 "BLOCKED_FETCH", /* 60 */
562 "FETCH_ME_BQ", /* 62 */
564 "EVACUATED", /* 64 */
565 "REMOTE_REF", /* 65 */
566 "N_CLOSURE_TYPES" /* 66 */
570 info_type(StgClosure *closure){
571 return closure_type_names[get_itbl(closure)->type];
575 info_type_by_ip(StgInfoTable *ip){
576 return closure_type_names[ip->type];
580 info_hdr_type(StgClosure *closure, char *res){
581 strcpy(res,closure_type_names[get_itbl(closure)->type]);
584 /* --------------------------------------------------------------------------
585 * Address printing code
587 * Uses symbol table in (unstripped executable)
588 * ------------------------------------------------------------------------*/
590 /* --------------------------------------------------------------------------
591 * Simple lookup table
593 * Current implementation is pretty dumb!
594 * ------------------------------------------------------------------------*/
601 static nat max_table_size;
602 static nat table_size;
603 static struct entry* table;
605 static void reset_table( int size )
607 max_table_size = size;
609 table = (struct entry *) malloc(size * sizeof(struct entry));
612 static void prepare_table( void )
614 /* Could sort it... */
617 static void insert( unsigned value, const char *name )
619 if ( table_size >= max_table_size ) {
620 barf( "Symbol table overflow\n" );
622 table[table_size].value = value;
623 table[table_size].name = name;
624 table_size = table_size + 1;
629 static rtsBool lookup_name( char *name, unsigned *result )
632 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
634 if (i < table_size) {
635 *result = table[i].value;
643 /* Code from somewhere inside GHC (circa 1994)
645 * "std"++xs -> "Zstd"++xs
646 * char_to_c 'Z' = "ZZ"
647 * char_to_c '&' = "Za"
648 * char_to_c '|' = "Zb"
649 * char_to_c ':' = "Zc"
650 * char_to_c '/' = "Zd"
651 * char_to_c '=' = "Ze"
652 * char_to_c '>' = "Zg"
653 * char_to_c '#' = "Zh"
654 * char_to_c '<' = "Zl"
655 * char_to_c '-' = "Zm"
656 * char_to_c '!' = "Zn"
657 * char_to_c '.' = "Zo"
658 * char_to_c '+' = "Zp"
659 * char_to_c '\'' = "Zq"
660 * char_to_c '*' = "Zt"
661 * char_to_c '_' = "Zu"
662 * char_to_c c = "Z" ++ show (ord c)
664 static char unZcode( char ch )
667 case 'a' : return ('&');
668 case 'b' : return ('|');
669 case 'c' : return (':');
670 case 'd' : return ('/');
671 case 'e' : return ('=');
672 case 'g' : return ('>');
673 case 'h' : return ('#');
674 case 'l' : return ('<');
675 case 'm' : return ('-');
676 case 'n' : return ('!');
677 case 'o' : return ('.');
678 case 'p' : return ('+');
679 case 'q' : return ('\'');
680 case 't' : return ('*');
681 case 'u' : return ('_');
683 case '\0' : return ('Z');
684 default : return (ch);
689 /* Precondition: out big enough to handle output (about twice length of in) */
690 static void enZcode( char *in, char *out )
696 for( i = 0; in[i] != '\0'; ++i ) {
771 rtsBool lookupGHCName( StgPtr addr, const char **result )
774 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
776 if (i < table_size) {
777 *result = table[i].name;
784 static void printZcoded( const char *raw )
788 while ( raw[j] != '\0' ) {
790 fputc(unZcode(raw[j+1]),stderr);
793 fputc(raw[j],stderr);
799 /* --------------------------------------------------------------------------
800 * Symbol table loading
801 * ------------------------------------------------------------------------*/
803 /* Causing linking trouble on Win32 plats, so I'm
804 disabling this for now.
806 #if defined(HAVE_BFD_H) && !defined(_WIN32) && !defined(PAR) && !defined(GRAN)
810 /* Fairly ad-hoc piece of code that seems to filter out a lot of
811 * rubbish like the obj-splitting symbols
814 static rtsBool isReal( flagword flags, const char *name )
817 /* ToDo: make this work on BFD */
818 int tp = type & N_TYPE;
819 if (tp == N_TEXT || tp == N_DATA) {
820 return (name[0] == '_' && name[1] != '_');
825 (void)flags; /* keep gcc -Wall happy */
827 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
828 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
835 extern void DEBUG_LoadSymbols( char *name )
841 abfd = bfd_openr(name, "default");
843 barf("can't open executable %s to get symbol table", name);
845 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
851 asymbol **symbol_table;
852 long number_of_symbols;
853 long num_real_syms = 0;
856 storage_needed = bfd_get_symtab_upper_bound (abfd);
858 if (storage_needed < 0) {
859 barf("can't read symbol table");
862 if (storage_needed == 0) {
863 belch("no storage needed");
866 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
868 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
870 if (number_of_symbols < 0) {
871 barf("can't canonicalise symbol table");
874 for( i = 0; i != number_of_symbols; ++i ) {
876 bfd_get_symbol_info(abfd,symbol_table[i],&info);
877 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
878 if (isReal(info.type, info.name)) {
884 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
885 number_of_symbols, num_real_syms)
888 reset_table( num_real_syms );
890 for( i = 0; i != number_of_symbols; ++i ) {
892 bfd_get_symbol_info(abfd,symbol_table[i],&info);
893 if (isReal(info.type, info.name)) {
894 insert( info.value, info.name );
903 #else /* HAVE_BFD_H */
905 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
910 #endif /* HAVE_BFD_H */
912 #include "StoragePriv.h"
914 void findPtr(P_ p); /* keep gcc -Wall happy */
923 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
924 for (s = 0; s < generations[g].n_steps; s++) {
925 for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
926 for (q = bd->start; q < bd->free; q++) {
937 void printPtr( StgPtr p )
939 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
942 void printObj( StgClosure *obj )
944 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );