1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.36 2001/02/11 17:51:08 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");
86 disassemble( (StgBCO*)obj );
91 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
93 fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
94 for (i = 0; i < ap->n_args; ++i) {
96 printPtr((P_)ap->payload[i]);
98 fprintf(stderr,")\n");
104 StgPAP* pap = stgCast(StgPAP*,obj);
106 fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
107 for (i = 0; i < pap->n_args; ++i) {
108 fprintf(stderr,", ");
109 printPtr((StgPtr)pap->payload[i]);
111 fprintf(stderr,")\n");
116 fprintf(stderr,"IND(");
117 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
118 fprintf(stderr,")\n");
122 fprintf(stderr,"IND_STATIC(");
123 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
124 fprintf(stderr,")\n");
128 fprintf(stderr,"IND_OLDGEN(");
129 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
130 fprintf(stderr,")\n");
134 fprintf(stderr,"CAF_BH(");
135 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
136 fprintf(stderr,")\n");
140 fprintf(stderr,"SE_BH\n");
143 case SE_CAF_BLACKHOLE:
144 fprintf(stderr,"SE_CAF_BH\n");
148 fprintf(stderr,"BH\n");
152 fprintf(stderr,"BQ(");
153 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
154 fprintf(stderr,")\n");
158 fprintf(stderr,"TSO(");
159 fprintf(stderr,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
160 fprintf(stderr,")\n");
165 fprintf(stderr,"BLOCKED_FETCH(");
166 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
167 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
168 fprintf(stderr,")\n");
172 fprintf(stderr,"FETCH_ME(");
173 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
174 fprintf(stderr,")\n");
178 fprintf(stderr,"FETCH_ME_BQ(");
179 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
180 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
181 fprintf(stderr,")\n");
184 #if defined(GRAN) || defined(PAR)
186 fprintf(stderr,"RBH(");
187 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
188 fprintf(stderr,")\n");
194 case CONSTR_1_0: case CONSTR_0_1:
195 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
197 case CONSTR_CHARLIKE:
199 case CONSTR_NOCAF_STATIC:
201 /* We can't use printStdObject because we want to print the
205 const StgInfoTable* info = get_itbl(obj);
206 fprintf(stderr,"PACK(");
207 printPtr((StgPtr)obj->header.info);
208 fprintf(stderr,"(tag=%d)",info->srt_len);
209 for (i = 0; i < info->layout.payload.ptrs; ++i) {
210 fprintf(stderr,", ");
211 printPtr((StgPtr)obj->payload[i]);
213 for (j = 0; j < info->layout.payload.nptrs; ++j) {
214 fprintf(stderr,", %p#", obj->payload[i+j]);
216 fprintf(stderr,")\n");
221 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
222 case MUT_ARR_PTRS_FROZEN:
225 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
227 fprintf(stderr,"Row<%i>(",p->ptrs);
228 for (i = 0; i < p->ptrs; ++i) {
229 if (i > 0) fprintf(stderr,", ");
230 printPtr((StgPtr)(p->payload[i]));
232 fprintf(stderr,")\n");
238 case FUN_1_0: case FUN_0_1:
239 case FUN_1_1: case FUN_0_2: case FUN_2_0:
241 printStdObject(obj,"FUN");
245 case THUNK_1_0: case THUNK_0_1:
246 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
248 /* ToDo: will this work for THUNK_STATIC too? */
249 printStdObject(obj,"THUNK");
253 printStdObject(obj,"THUNK_SELECTOR");
259 fprintf(stderr,"ARR_WORDS(\"");
260 /* ToDo: we can't safely assume that this is a string!
261 for (i = 0; arrWordsGetChar(obj,i); ++i) {
262 putchar(arrWordsGetChar(obj,i));
264 for (i=0; i<((StgArrWords *)obj)->words; i++)
265 fprintf(stderr, "%d", ((StgArrWords *)obj)->payload[i]);
266 fprintf(stderr,"\")\n");
272 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
273 fprintf(stderr,"UpdateFrame(");
274 printPtr((StgPtr)GET_INFO(u));
276 printPtr((StgPtr)u->updatee);
278 printPtr((StgPtr)u->link);
279 fprintf(stderr,")\n");
285 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
286 fprintf(stderr,"CatchFrame(");
287 printPtr((StgPtr)GET_INFO(u));
289 printPtr((StgPtr)u->handler);
291 printPtr((StgPtr)u->link);
292 fprintf(stderr,")\n");
298 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
299 fprintf(stderr,"SeqFrame(");
300 printPtr((StgPtr)GET_INFO(u));
302 printPtr((StgPtr)u->link);
303 fprintf(stderr,")\n");
309 StgStopFrame* u = stgCast(StgStopFrame*,obj);
310 fprintf(stderr,"StopFrame(");
311 printPtr((StgPtr)GET_INFO(u));
312 fprintf(stderr,")\n");
316 //barf("printClosure %d",get_itbl(obj)->type);
317 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
318 get_itbl(obj)->type );
324 void printGraph( StgClosure *obj )
330 StgPtr printStackObj( StgPtr sp )
332 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
334 if (IS_ARG_TAG(*sp)) {
337 fprintf(stderr,"Tagged{");
338 for (i = 0; i < tag; i++) {
339 fprintf(stderr,"0x%x#", (unsigned)(*sp++));
340 if (i < tag-1) fprintf(stderr, ", ");
342 fprintf(stderr, "}\n");
344 StgClosure* c = (StgClosure*)(*sp);
345 printPtr((StgPtr)*sp);
346 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
347 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
349 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
350 fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
352 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
353 fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
355 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
356 fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
358 if (get_itbl(c)->type == BCO) {
359 fprintf(stderr, "\t\t\t");
360 fprintf(stderr, "BCO(...)\n");
363 fprintf(stderr, "\t\t\t");
364 printClosure ( (StgClosure*)(*sp));
372 void printStackChunk( StgPtr sp, StgPtr spBottom )
375 const StgInfoTable *info;
377 ASSERT(sp <= spBottom);
378 while (sp < spBottom) {
379 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
380 info = get_itbl((StgClosure *)sp);
381 switch (info->type) {
384 printObj( stgCast(StgClosure*,sp) );
385 sp += sizeofW(StgUpdateFrame);
389 printObj( stgCast(StgClosure*,sp) );
390 sp += sizeofW(StgSeqFrame);
394 printObj( stgCast(StgClosure*,sp) );
395 sp += sizeofW(StgCatchFrame);
399 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
400 printObj( stgCast(StgClosure*,sp) );
404 fprintf(stderr, "RET_DYN (%p)\n", sp);
407 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
412 fprintf(stderr, "RET_SMALL (%p)\n", sp);
413 bitmap = info->layout.bitmap;
416 while (bitmap != 0) {
417 fprintf(stderr," stk[%d] (%p) = ", spBottom-sp, sp);
418 if ((bitmap & 1) == 0) {
420 fprintf(stderr,"\n");
422 fprintf(stderr,"Word# %d\n", *sp++);
425 bitmap = bitmap >> 1;
437 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
438 sp = printStackObj(sp);
442 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
444 /* check everything down to the first update frame */
445 printStackChunk( sp, stgCast(StgPtr,su) );
446 while ( stgCast(StgPtr,su) < spBottom) {
447 sp = stgCast(StgPtr,su);
448 switch (get_itbl(su)->type) {
450 printObj( stgCast(StgClosure*,su) );
451 sp += sizeofW(StgUpdateFrame);
455 printObj( stgCast(StgClosure*,su) );
456 sp += sizeofW(StgSeqFrame);
457 su = stgCast(StgSeqFrame*,su)->link;
460 printObj( stgCast(StgClosure*,su) );
461 sp += sizeofW(StgCatchFrame);
462 su = stgCast(StgCatchFrame*,su)->link;
465 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
466 printObj( stgCast(StgClosure*,su) );
469 barf("printStack: weird record found on update frame list.");
471 printStackChunk( sp, stgCast(StgPtr,su) );
473 ASSERT(stgCast(StgPtr,su) == spBottom);
476 void printTSO( StgTSO *tso )
478 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
479 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
482 /* -----------------------------------------------------------------------------
485 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
486 -------------------------------------------------------------------------- */
488 static char *closure_type_names[] = {
489 "INVALID_OBJECT", /* 0 */
491 "CONSTR_1_0", /* 2 */
492 "CONSTR_0_1", /* 3 */
493 "CONSTR_2_0", /* 4 */
494 "CONSTR_1_1", /* 5 */
495 "CONSTR_0_2", /* 6 */
496 "CONSTR_INTLIKE", /* 7 */
497 "CONSTR_CHARLIKE", /* 8 */
498 "CONSTR_STATIC", /* 9 */
499 "CONSTR_NOCAF_STATIC", /* 10 */
506 "FUN_STATIC", /* 17 */
508 "THUNK_1_0", /* 19 */
509 "THUNK_0_1", /* 20 */
510 "THUNK_2_0", /* 21 */
511 "THUNK_1_1", /* 22 */
512 "THUNK_0_2", /* 23 */
513 "THUNK_STATIC", /* 24 */
514 "THUNK_SELECTOR", /* 25 */
519 "IND_OLDGEN", /* 30 */
521 "IND_OLDGEN_PERM", /* 32 */
522 "IND_STATIC", /* 33 */
523 "CAF_BLACKHOLE", /* 36 */
525 "RET_SMALL", /* 38 */
526 "RET_VEC_SMALL", /* 39 */
528 "RET_VEC_BIG", /* 41 */
530 "UPDATE_FRAME", /* 43 */
531 "CATCH_FRAME", /* 44 */
532 "STOP_FRAME", /* 45 */
533 "SEQ_FRAME", /* 46 */
534 "BLACKHOLE", /* 47 */
535 "BLACKHOLE_BQ", /* 48 */
536 "SE_BLACKHOLE", /* 49 */
537 "SE_CAF_BLACKHOLE", /* 50 */
539 "ARR_WORDS", /* 52 */
540 "MUT_ARR_PTRS", /* 53 */
541 "MUT_ARR_PTRS_FROZEN", /* 54 */
545 "STABLE_NAME", /* 58 */
547 "BLOCKED_FETCH", /* 60 */
549 "FETCH_ME_BQ", /* 62 */
551 "EVACUATED", /* 64 */
552 "N_CLOSURE_TYPES" /* 65 */
556 info_type(StgClosure *closure){
557 return closure_type_names[get_itbl(closure)->type];
561 info_type_by_ip(StgInfoTable *ip){
562 return closure_type_names[ip->type];
566 info_hdr_type(StgClosure *closure, char *res){
567 strcpy(res,closure_type_names[get_itbl(closure)->type]);
570 /* --------------------------------------------------------------------------
571 * Address printing code
573 * Uses symbol table in (unstripped executable)
574 * ------------------------------------------------------------------------*/
576 /* --------------------------------------------------------------------------
577 * Simple lookup table
579 * Current implementation is pretty dumb!
580 * ------------------------------------------------------------------------*/
587 static nat max_table_size;
588 static nat table_size;
589 static struct entry* table;
591 static void reset_table( int size )
593 max_table_size = size;
595 table = (struct entry *) malloc(size * sizeof(struct entry));
598 static void prepare_table( void )
600 /* Could sort it... */
603 static void insert( unsigned value, const char *name )
605 if ( table_size >= max_table_size ) {
606 barf( "Symbol table overflow\n" );
608 table[table_size].value = value;
609 table[table_size].name = name;
610 table_size = table_size + 1;
615 static rtsBool lookup_name( char *name, unsigned *result )
618 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
620 if (i < table_size) {
621 *result = table[i].value;
629 /* Code from somewhere inside GHC (circa 1994)
631 * "std"++xs -> "Zstd"++xs
632 * char_to_c 'Z' = "ZZ"
633 * char_to_c '&' = "Za"
634 * char_to_c '|' = "Zb"
635 * char_to_c ':' = "Zc"
636 * char_to_c '/' = "Zd"
637 * char_to_c '=' = "Ze"
638 * char_to_c '>' = "Zg"
639 * char_to_c '#' = "Zh"
640 * char_to_c '<' = "Zl"
641 * char_to_c '-' = "Zm"
642 * char_to_c '!' = "Zn"
643 * char_to_c '.' = "Zo"
644 * char_to_c '+' = "Zp"
645 * char_to_c '\'' = "Zq"
646 * char_to_c '*' = "Zt"
647 * char_to_c '_' = "Zu"
648 * char_to_c c = "Z" ++ show (ord c)
650 static char unZcode( char ch )
653 case 'a' : return ('&');
654 case 'b' : return ('|');
655 case 'c' : return (':');
656 case 'd' : return ('/');
657 case 'e' : return ('=');
658 case 'g' : return ('>');
659 case 'h' : return ('#');
660 case 'l' : return ('<');
661 case 'm' : return ('-');
662 case 'n' : return ('!');
663 case 'o' : return ('.');
664 case 'p' : return ('+');
665 case 'q' : return ('\'');
666 case 't' : return ('*');
667 case 'u' : return ('_');
669 case '\0' : return ('Z');
670 default : return (ch);
675 /* Precondition: out big enough to handle output (about twice length of in) */
676 static void enZcode( char *in, char *out )
682 for( i = 0; in[i] != '\0'; ++i ) {
757 rtsBool lookupGHCName( StgPtr addr, const char **result )
760 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
762 if (i < table_size) {
763 *result = table[i].name;
770 static void printZcoded( const char *raw )
774 while ( raw[j] != '\0' ) {
776 fputc(unZcode(raw[j+1]),stderr);
779 fputc(raw[j],stderr);
785 /* --------------------------------------------------------------------------
786 * Symbol table loading
787 * ------------------------------------------------------------------------*/
789 /* Causing linking trouble on Win32 plats, so I'm
790 disabling this for now.
792 #if defined(HAVE_BFD_H) && !defined(_WIN32)
796 /* Fairly ad-hoc piece of code that seems to filter out a lot of
797 * rubbish like the obj-splitting symbols
800 static rtsBool isReal( flagword flags, const char *name )
803 /* ToDo: make this work on BFD */
804 int tp = type & N_TYPE;
805 if (tp == N_TEXT || tp == N_DATA) {
806 return (name[0] == '_' && name[1] != '_');
811 (void)flags; /* keep gcc -Wall happy */
813 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
814 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
821 extern void DEBUG_LoadSymbols( char *name )
827 abfd = bfd_openr(name, "default");
829 barf("can't open executable %s to get symbol table", name);
831 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
837 asymbol **symbol_table;
838 long number_of_symbols;
839 long num_real_syms = 0;
842 storage_needed = bfd_get_symtab_upper_bound (abfd);
844 if (storage_needed < 0) {
845 barf("can't read symbol table");
848 if (storage_needed == 0) {
849 belch("no storage needed");
852 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
854 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
856 if (number_of_symbols < 0) {
857 barf("can't canonicalise symbol table");
860 for( i = 0; i != number_of_symbols; ++i ) {
862 bfd_get_symbol_info(abfd,symbol_table[i],&info);
863 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
864 if (isReal(info.type, info.name)) {
870 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
871 number_of_symbols, num_real_syms)
874 reset_table( num_real_syms );
876 for( i = 0; i != number_of_symbols; ++i ) {
878 bfd_get_symbol_info(abfd,symbol_table[i],&info);
879 if (isReal(info.type, info.name)) {
880 insert( info.value, info.name );
889 #else /* HAVE_BFD_H */
891 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
896 #endif /* HAVE_BFD_H */
898 #include "StoragePriv.h"
900 void findPtr(P_ p); /* keep gcc -Wall happy */
909 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
910 for (s = 0; s < generations[g].n_steps; s++) {
911 for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
912 for (q = bd->start; q < bd->free; q++) {
923 void printPtr( StgPtr p )
925 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
928 void printObj( StgClosure *obj )
930 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );