1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.35 2001/02/09 13:09:16 simonmar Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
19 #include "Bytecodes.h" /* for InstrPtr */
20 #include "Disassembler.h"
24 // HWL: explicit fixed header size to make debugging easier
25 int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable),
26 uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame);
28 /* --------------------------------------------------------------------------
29 * local function decls
30 * ------------------------------------------------------------------------*/
32 static void printStdObject( StgClosure *obj, char* tag );
33 static void reset_table ( int size );
34 static void prepare_table ( void );
35 static void insert ( unsigned value, const char *name );
36 #if 0 /* unused but might be useful sometime */
37 static rtsBool lookup_name ( char *name, unsigned *result );
38 static void enZcode ( char *in, char *out );
40 static char unZcode ( char ch );
41 rtsBool lookupGHCName ( StgPtr addr, const char **result );
42 static void printZcoded ( const char *raw );
44 /* --------------------------------------------------------------------------
46 * ------------------------------------------------------------------------*/
48 void printPtr( StgPtr p )
51 if (lookupGHCName( p, &raw )) {
54 fprintf(stderr, "%p", p);
58 void printObj( StgClosure *obj )
60 fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
64 static void printStdObject( StgClosure *obj, char* tag )
67 const StgInfoTable* info = get_itbl(obj);
68 fprintf(stderr,"%s(",tag);
69 printPtr((StgPtr)obj->header.info);
70 for (i = 0; i < info->layout.payload.ptrs; ++i) {
72 printPtr((StgPtr)obj->payload[i]);
74 for (j = 0; j < info->layout.payload.nptrs; ++j) {
75 fprintf(stderr,", %pd#",obj->payload[i+j]);
77 fprintf(stderr,")\n");
80 void printClosure( StgClosure *obj )
82 switch ( get_itbl(obj)->type ) {
84 barf("Invalid object");
87 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");
180 fprintf(stderr,"FETCH_ME_BQ(");
181 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
182 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
183 fprintf(stderr,")\n");
186 #if defined(GRAN) || defined(PAR)
188 fprintf(stderr,"RBH(");
189 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
190 fprintf(stderr,")\n");
196 case CONSTR_1_0: case CONSTR_0_1:
197 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
199 case CONSTR_CHARLIKE:
201 case CONSTR_NOCAF_STATIC:
203 /* We can't use printStdObject because we want to print the
207 const StgInfoTable* info = get_itbl(obj);
208 fprintf(stderr,"PACK(");
209 printPtr((StgPtr)obj->header.info);
210 fprintf(stderr,"(tag=%d)",info->srt_len);
211 for (i = 0; i < info->layout.payload.ptrs; ++i) {
212 fprintf(stderr,", ");
213 printPtr((StgPtr)obj->payload[i]);
215 for (j = 0; j < info->layout.payload.nptrs; ++j) {
216 fprintf(stderr,", %p#", obj->payload[i+j]);
218 fprintf(stderr,")\n");
223 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
224 case MUT_ARR_PTRS_FROZEN:
227 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
229 fprintf(stderr,"Row<%i>(",p->ptrs);
230 for (i = 0; i < p->ptrs; ++i) {
231 if (i > 0) fprintf(stderr,", ");
232 printPtr((StgPtr)(p->payload[i]));
234 fprintf(stderr,")\n");
240 case FUN_1_0: case FUN_0_1:
241 case FUN_1_1: case FUN_0_2: case FUN_2_0:
243 printStdObject(obj,"FUN");
247 case THUNK_1_0: case THUNK_0_1:
248 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
250 /* ToDo: will this work for THUNK_STATIC too? */
251 printStdObject(obj,"THUNK");
255 printStdObject(obj,"THUNK_SELECTOR");
261 fprintf(stderr,"ARR_WORDS(\"");
262 /* ToDo: we can't safely assume that this is a string!
263 for (i = 0; arrWordsGetChar(obj,i); ++i) {
264 putchar(arrWordsGetChar(obj,i));
266 for (i=0; i<((StgArrWords *)obj)->words; i++)
267 fprintf(stderr, "%d", ((StgArrWords *)obj)->payload[i]);
268 fprintf(stderr,"\")\n");
274 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
275 fprintf(stderr,"UpdateFrame(");
276 printPtr((StgPtr)GET_INFO(u));
278 printPtr((StgPtr)u->updatee);
280 printPtr((StgPtr)u->link);
281 fprintf(stderr,")\n");
287 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
288 fprintf(stderr,"CatchFrame(");
289 printPtr((StgPtr)GET_INFO(u));
291 printPtr((StgPtr)u->handler);
293 printPtr((StgPtr)u->link);
294 fprintf(stderr,")\n");
300 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
301 fprintf(stderr,"SeqFrame(");
302 printPtr((StgPtr)GET_INFO(u));
304 printPtr((StgPtr)u->link);
305 fprintf(stderr,")\n");
311 StgStopFrame* u = stgCast(StgStopFrame*,obj);
312 fprintf(stderr,"StopFrame(");
313 printPtr((StgPtr)GET_INFO(u));
314 fprintf(stderr,")\n");
318 //barf("printClosure %d",get_itbl(obj)->type);
319 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
320 get_itbl(obj)->type );
326 void printGraph( StgClosure *obj )
332 StgPtr printStackObj( StgPtr sp )
334 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
336 if (IS_ARG_TAG(*sp)) {
339 fprintf(stderr,"Tagged{");
340 for (i = 0; i < tag; i++) {
341 fprintf(stderr,"0x%x#", (unsigned)(*sp++));
342 if (i < tag-1) fprintf(stderr, ", ");
344 fprintf(stderr, "}\n");
346 StgClosure* c = (StgClosure*)(*sp);
347 printPtr((StgPtr)*sp);
349 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
350 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
352 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
353 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
355 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
356 fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
358 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
359 fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
362 if (get_itbl(c)->type == BCO) {
363 fprintf(stderr, "\t\t\t");
364 fprintf(stderr, "BCO(...)\n");
367 fprintf(stderr, "\t\t\t");
368 printClosure ( (StgClosure*)(*sp));
376 void printStackChunk( StgPtr sp, StgPtr spBottom )
379 const StgInfoTable *info;
381 ASSERT(sp <= spBottom);
382 while (sp < spBottom) {
383 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
384 info = get_itbl((StgClosure *)sp);
385 switch (info->type) {
388 printObj( stgCast(StgClosure*,sp) );
389 sp += sizeofW(StgUpdateFrame);
393 printObj( stgCast(StgClosure*,sp) );
394 sp += sizeofW(StgSeqFrame);
398 printObj( stgCast(StgClosure*,sp) );
399 sp += sizeofW(StgCatchFrame);
403 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
404 printObj( stgCast(StgClosure*,sp) );
408 fprintf(stderr, "RET_DYN (%p)\n", sp);
411 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
416 fprintf(stderr, "RET_SMALL (%p)\n", sp);
417 bitmap = info->layout.bitmap;
420 while (bitmap != 0) {
421 fprintf(stderr," stk[%d] (%p) = ", spBottom-sp, sp);
422 if ((bitmap & 1) == 0) {
424 fprintf(stderr,"\n");
426 fprintf(stderr,"Word# %d\n", *sp++);
429 bitmap = bitmap >> 1;
441 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
442 sp = printStackObj(sp);
446 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
448 /* check everything down to the first update frame */
449 printStackChunk( sp, stgCast(StgPtr,su) );
450 while ( stgCast(StgPtr,su) < spBottom) {
451 sp = stgCast(StgPtr,su);
452 switch (get_itbl(su)->type) {
454 printObj( stgCast(StgClosure*,su) );
455 sp += sizeofW(StgUpdateFrame);
459 printObj( stgCast(StgClosure*,su) );
460 sp += sizeofW(StgSeqFrame);
461 su = stgCast(StgSeqFrame*,su)->link;
464 printObj( stgCast(StgClosure*,su) );
465 sp += sizeofW(StgCatchFrame);
466 su = stgCast(StgCatchFrame*,su)->link;
469 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
470 printObj( stgCast(StgClosure*,su) );
473 barf("printStack: weird record found on update frame list.");
475 printStackChunk( sp, stgCast(StgPtr,su) );
477 ASSERT(stgCast(StgPtr,su) == spBottom);
480 void printTSO( StgTSO *tso )
482 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
483 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
486 /* -----------------------------------------------------------------------------
489 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
490 -------------------------------------------------------------------------- */
492 static char *closure_type_names[] = {
493 "INVALID_OBJECT", /* 0 */
495 "CONSTR_1_0", /* 2 */
496 "CONSTR_0_1", /* 3 */
497 "CONSTR_2_0", /* 4 */
498 "CONSTR_1_1", /* 5 */
499 "CONSTR_0_2", /* 6 */
500 "CONSTR_INTLIKE", /* 7 */
501 "CONSTR_CHARLIKE", /* 8 */
502 "CONSTR_STATIC", /* 9 */
503 "CONSTR_NOCAF_STATIC", /* 10 */
510 "FUN_STATIC", /* 17 */
512 "THUNK_1_0", /* 19 */
513 "THUNK_0_1", /* 20 */
514 "THUNK_2_0", /* 21 */
515 "THUNK_1_1", /* 22 */
516 "THUNK_0_2", /* 23 */
517 "THUNK_STATIC", /* 24 */
518 "THUNK_SELECTOR", /* 25 */
523 "IND_OLDGEN", /* 30 */
525 "IND_OLDGEN_PERM", /* 32 */
526 "IND_STATIC", /* 33 */
527 "CAF_BLACKHOLE", /* 36 */
529 "RET_SMALL", /* 38 */
530 "RET_VEC_SMALL", /* 39 */
532 "RET_VEC_BIG", /* 41 */
534 "UPDATE_FRAME", /* 43 */
535 "CATCH_FRAME", /* 44 */
536 "STOP_FRAME", /* 45 */
537 "SEQ_FRAME", /* 46 */
538 "BLACKHOLE", /* 47 */
539 "BLACKHOLE_BQ", /* 48 */
540 "SE_BLACKHOLE", /* 49 */
541 "SE_CAF_BLACKHOLE", /* 50 */
543 "ARR_WORDS", /* 52 */
544 "MUT_ARR_PTRS", /* 53 */
545 "MUT_ARR_PTRS_FROZEN", /* 54 */
549 "STABLE_NAME", /* 58 */
551 "BLOCKED_FETCH", /* 60 */
553 "FETCH_ME_BQ", /* 62 */
555 "EVACUATED", /* 64 */
556 "N_CLOSURE_TYPES" /* 65 */
560 info_type(StgClosure *closure){
561 return closure_type_names[get_itbl(closure)->type];
565 info_type_by_ip(StgInfoTable *ip){
566 return closure_type_names[ip->type];
570 info_hdr_type(StgClosure *closure, char *res){
571 strcpy(res,closure_type_names[get_itbl(closure)->type]);
574 /* --------------------------------------------------------------------------
575 * Address printing code
577 * Uses symbol table in (unstripped executable)
578 * ------------------------------------------------------------------------*/
580 /* --------------------------------------------------------------------------
581 * Simple lookup table
583 * Current implementation is pretty dumb!
584 * ------------------------------------------------------------------------*/
591 static nat max_table_size;
592 static nat table_size;
593 static struct entry* table;
595 static void reset_table( int size )
597 max_table_size = size;
599 table = (struct entry *) malloc(size * sizeof(struct entry));
602 static void prepare_table( void )
604 /* Could sort it... */
607 static void insert( unsigned value, const char *name )
609 if ( table_size >= max_table_size ) {
610 barf( "Symbol table overflow\n" );
612 table[table_size].value = value;
613 table[table_size].name = name;
614 table_size = table_size + 1;
619 static rtsBool lookup_name( char *name, unsigned *result )
622 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
624 if (i < table_size) {
625 *result = table[i].value;
633 /* Code from somewhere inside GHC (circa 1994)
635 * "std"++xs -> "Zstd"++xs
636 * char_to_c 'Z' = "ZZ"
637 * char_to_c '&' = "Za"
638 * char_to_c '|' = "Zb"
639 * char_to_c ':' = "Zc"
640 * char_to_c '/' = "Zd"
641 * char_to_c '=' = "Ze"
642 * char_to_c '>' = "Zg"
643 * char_to_c '#' = "Zh"
644 * char_to_c '<' = "Zl"
645 * char_to_c '-' = "Zm"
646 * char_to_c '!' = "Zn"
647 * char_to_c '.' = "Zo"
648 * char_to_c '+' = "Zp"
649 * char_to_c '\'' = "Zq"
650 * char_to_c '*' = "Zt"
651 * char_to_c '_' = "Zu"
652 * char_to_c c = "Z" ++ show (ord c)
654 static char unZcode( char ch )
657 case 'a' : return ('&');
658 case 'b' : return ('|');
659 case 'c' : return (':');
660 case 'd' : return ('/');
661 case 'e' : return ('=');
662 case 'g' : return ('>');
663 case 'h' : return ('#');
664 case 'l' : return ('<');
665 case 'm' : return ('-');
666 case 'n' : return ('!');
667 case 'o' : return ('.');
668 case 'p' : return ('+');
669 case 'q' : return ('\'');
670 case 't' : return ('*');
671 case 'u' : return ('_');
673 case '\0' : return ('Z');
674 default : return (ch);
679 /* Precondition: out big enough to handle output (about twice length of in) */
680 static void enZcode( char *in, char *out )
686 for( i = 0; in[i] != '\0'; ++i ) {
761 rtsBool lookupGHCName( StgPtr addr, const char **result )
764 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
766 if (i < table_size) {
767 *result = table[i].name;
774 static void printZcoded( const char *raw )
778 while ( raw[j] != '\0' ) {
780 fputc(unZcode(raw[j+1]),stderr);
783 fputc(raw[j],stderr);
789 /* --------------------------------------------------------------------------
790 * Symbol table loading
791 * ------------------------------------------------------------------------*/
793 /* Causing linking trouble on Win32 plats, so I'm
794 disabling this for now.
796 #if defined(HAVE_BFD_H) && !defined(_WIN32)
800 /* Fairly ad-hoc piece of code that seems to filter out a lot of
801 * rubbish like the obj-splitting symbols
804 static rtsBool isReal( flagword flags, const char *name )
807 /* ToDo: make this work on BFD */
808 int tp = type & N_TYPE;
809 if (tp == N_TEXT || tp == N_DATA) {
810 return (name[0] == '_' && name[1] != '_');
815 (void)flags; /* keep gcc -Wall happy */
817 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
818 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
825 extern void DEBUG_LoadSymbols( char *name )
831 abfd = bfd_openr(name, "default");
833 barf("can't open executable %s to get symbol table", name);
835 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
841 asymbol **symbol_table;
842 long number_of_symbols;
843 long num_real_syms = 0;
846 storage_needed = bfd_get_symtab_upper_bound (abfd);
848 if (storage_needed < 0) {
849 barf("can't read symbol table");
852 if (storage_needed == 0) {
853 belch("no storage needed");
856 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
858 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
860 if (number_of_symbols < 0) {
861 barf("can't canonicalise symbol table");
864 for( i = 0; i != number_of_symbols; ++i ) {
866 bfd_get_symbol_info(abfd,symbol_table[i],&info);
867 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
868 if (isReal(info.type, info.name)) {
874 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
875 number_of_symbols, num_real_syms)
878 reset_table( num_real_syms );
880 for( i = 0; i != number_of_symbols; ++i ) {
882 bfd_get_symbol_info(abfd,symbol_table[i],&info);
883 if (isReal(info.type, info.name)) {
884 insert( info.value, info.name );
893 #else /* HAVE_BFD_H */
895 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
900 #endif /* HAVE_BFD_H */
902 #include "StoragePriv.h"
904 void findPtr(P_ p); /* keep gcc -Wall happy */
913 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
914 for (s = 0; s < generations[g].n_steps; s++) {
915 for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
916 for (q = bd->start; q < bd->free; q++) {
927 void printPtr( StgPtr p )
929 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
932 void printObj( StgClosure *obj )
934 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );