1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.23 2000/03/31 03:09:36 hwloidl Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
17 #include "Bytecodes.h" /* for InstrPtr */
18 #include "Disassembler.h"
22 // HWL: explicit fixed header size to make debugging easier
23 int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable),
24 uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame);
26 /* --------------------------------------------------------------------------
27 * local function decls
28 * ------------------------------------------------------------------------*/
30 static void printStdObject( StgClosure *obj, char* tag );
31 static void reset_table ( int size );
32 static void prepare_table ( void );
33 static void insert ( unsigned value, const char *name );
34 #if 0 /* unused but might be useful sometime */
35 static rtsBool lookup_name ( char *name, unsigned *result );
36 static void enZcode ( char *in, char *out );
38 static char unZcode ( char ch );
39 rtsBool lookupGHCName ( StgPtr addr, const char **result );
40 static void printZcoded ( const char *raw );
42 /* --------------------------------------------------------------------------
44 * ------------------------------------------------------------------------*/
48 extern void* itblNames[];
49 extern int nItblNames;
50 char* lookupHugsItblName ( void* v )
53 for (i = 0; i < nItblNames; i += 2)
54 if (itblNames[i] == v) return itblNames[i+1];
59 void printPtr( StgPtr p )
65 if (lookupGHCName( p, &raw )) {
68 } else if ((raw = lookupHugsName(p)) != 0) {
69 fprintf(stderr, "%s", raw);
70 } else if ((str = lookupHugsItblName(p)) != 0) {
71 fprintf(stderr, "%p=%s", p, str);
74 fprintf(stderr, "%p", p);
78 void printObj( StgClosure *obj )
80 fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
84 static void printStdObject( StgClosure *obj, char* tag )
87 const StgInfoTable* info = get_itbl(obj);
88 fprintf(stderr,"%s(",tag);
89 printPtr((StgPtr)obj->header.info);
90 for (i = 0; i < info->layout.payload.ptrs; ++i) {
92 printPtr((StgPtr)obj->payload[i]);
94 for (j = 0; j < info->layout.payload.nptrs; ++j) {
95 fprintf(stderr,", %pd#",obj->payload[i+j]);
97 fprintf(stderr,")\n");
100 void printClosure( StgClosure *obj )
102 switch ( get_itbl(obj)->type ) {
104 barf("Invalid object");
107 fprintf(stderr,"BCO\n");
108 disassemble(stgCast(StgBCO*,obj),"\t");
114 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
116 fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
117 for (i = 0; i < ap->n_args; ++i) {
118 fprintf(stderr,", ");
119 printPtr(ap->payload[i]);
121 fprintf(stderr,")\n");
127 StgPAP* pap = stgCast(StgPAP*,obj);
129 fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
130 for (i = 0; i < pap->n_args; ++i) {
131 fprintf(stderr,", ");
132 printPtr((StgPtr)pap->payload[i]);
134 fprintf(stderr,")\n");
139 fprintf(stderr,"IND(");
140 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
141 fprintf(stderr,")\n");
145 fprintf(stderr,"IND_STATIC(");
146 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
147 fprintf(stderr,")\n");
151 fprintf(stderr,"IND_OLDGEN(");
152 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
153 fprintf(stderr,")\n");
158 StgCAF* caf = stgCast(StgCAF*,obj);
159 fprintf(stderr,"CAF_UNENTERED(");
160 printPtr((StgPtr)caf->body);
161 fprintf(stderr,", ");
162 printPtr((StgPtr)caf->value); /* should be null */
163 fprintf(stderr,", ");
164 printPtr((StgPtr)caf->link); /* should be null */
165 fprintf(stderr,")\n");
171 StgCAF* caf = stgCast(StgCAF*,obj);
172 fprintf(stderr,"CAF_ENTERED(");
173 printPtr((StgPtr)caf->body);
174 fprintf(stderr,", ");
175 printPtr((StgPtr)caf->value);
176 fprintf(stderr,", ");
177 printPtr((StgPtr)caf->link);
178 fprintf(stderr,")\n");
183 fprintf(stderr,"CAF_BH(");
184 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
185 fprintf(stderr,")\n");
189 fprintf(stderr,"SE_BH\n");
192 case SE_CAF_BLACKHOLE:
193 fprintf(stderr,"SE_CAF_BH\n");
197 fprintf(stderr,"BH\n");
201 fprintf(stderr,"BQ(");
202 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
203 fprintf(stderr,")\n");
207 fprintf(stderr,"TSO(");
208 fprintf(stderr,"%d (%x)",
209 stgCast(StgTSO*,obj)->id, stgCast(StgTSO*,obj));
210 fprintf(stderr,")\n");
215 fprintf(stderr,"BLOCKED_FETCH(");
216 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
217 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
218 fprintf(stderr,")\n");
222 fprintf(stderr,"FETCH_ME(");
223 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
224 fprintf(stderr,")\n");
228 fprintf(stderr,"FETCH_ME_BQ(");
229 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
230 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
231 fprintf(stderr,")\n");
234 #if defined(GRAN) || defined(PAR)
236 fprintf(stderr,"RBH(");
237 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
238 fprintf(stderr,")\n");
244 case CONSTR_1_0: case CONSTR_0_1:
245 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
247 case CONSTR_CHARLIKE:
249 case CONSTR_NOCAF_STATIC:
251 /* We can't use printStdObject because we want to print the
255 const StgInfoTable* info = get_itbl(obj);
256 fprintf(stderr,"PACK(");
257 printPtr((StgPtr)obj->header.info);
258 fprintf(stderr,"(tag=%d)",info->srt_len);
259 for (i = 0; i < info->layout.payload.ptrs; ++i) {
260 fprintf(stderr,", ");
261 printPtr((StgPtr)obj->payload[i]);
263 for (j = 0; j < info->layout.payload.nptrs; ++j) {
264 fprintf(stderr,", %p#", obj->payload[i+j]);
266 fprintf(stderr,")\n");
271 case FUN_1_0: case FUN_0_1:
272 case FUN_1_1: case FUN_0_2: case FUN_2_0:
274 printStdObject(obj,"FUN");
278 case THUNK_1_0: case THUNK_0_1:
279 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
281 /* ToDo: will this work for THUNK_STATIC too? */
282 printStdObject(obj,"THUNK");
286 printStdObject(obj,"THUNK_SELECTOR");
292 fprintf(stderr,"ARR_WORDS(\"");
293 /* ToDo: we can't safely assume that this is a string!
294 for (i = 0; arrWordsGetChar(obj,i); ++i) {
295 putchar(arrWordsGetChar(obj,i));
297 for (i=0; i<((StgArrWords *)obj)->words; i++)
298 fprintf(stderr, "%d", ((StgArrWords *)obj)->payload[i]);
299 fprintf(stderr,"\")\n");
305 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
306 fprintf(stderr,"UpdateFrame(");
307 printPtr((StgPtr)GET_INFO(u));
309 printPtr((StgPtr)u->updatee);
311 printPtr((StgPtr)u->link);
312 fprintf(stderr,")\n");
318 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
319 fprintf(stderr,"CatchFrame(");
320 printPtr((StgPtr)GET_INFO(u));
322 printPtr((StgPtr)u->handler);
324 printPtr((StgPtr)u->link);
325 fprintf(stderr,")\n");
331 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
332 fprintf(stderr,"SeqFrame(");
333 printPtr((StgPtr)GET_INFO(u));
335 printPtr((StgPtr)u->link);
336 fprintf(stderr,")\n");
342 StgStopFrame* u = stgCast(StgStopFrame*,obj);
343 fprintf(stderr,"StopFrame(");
344 printPtr((StgPtr)GET_INFO(u));
345 fprintf(stderr,")\n");
349 //barf("printClosure %d",get_itbl(obj)->type);
350 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
351 get_itbl(obj)->type );
357 void printGraph( StgClosure *obj )
363 StgPtr printStackObj( StgPtr sp )
365 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
367 if (IS_ARG_TAG(*sp)) {
370 fprintf(stderr,"Tagged{");
371 for (i = 0; i < tag; i++) {
372 fprintf(stderr,"0x%x#", (unsigned)(*sp++));
373 if (i < tag-1) fprintf(stderr, ", ");
375 fprintf(stderr, "}\n");
377 StgClosure* c = (StgClosure*)(*sp);
378 printPtr((StgPtr)*sp);
380 if (c == &ret_bco_info) {
381 fprintf(stderr, "\t\t");
382 fprintf(stderr, "ret_bco_info\n" );
384 if (IS_HUGS_CONSTR_INFO(GET_INFO(c))) {
385 fprintf(stderr, "\t\t\t");
386 fprintf(stderr, "ConstrInfoTable\n" );
389 if (get_itbl(c)->type == BCO) {
390 fprintf(stderr, "\t\t\t");
391 fprintf(stderr, "BCO(...)\n");
394 fprintf(stderr, "\t\t\t");
395 printClosure ( (StgClosure*)(*sp));
403 void printStackChunk( StgPtr sp, StgPtr spBottom )
406 const StgInfoTable *info;
408 ASSERT(sp <= spBottom);
409 while (sp < spBottom) {
410 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
411 info = get_itbl((StgClosure *)sp);
412 switch (info->type) {
415 printObj( stgCast(StgClosure*,sp) );
416 sp += sizeofW(StgUpdateFrame);
420 printObj( stgCast(StgClosure*,sp) );
421 sp += sizeofW(StgSeqFrame);
425 printObj( stgCast(StgClosure*,sp) );
426 sp += sizeofW(StgCatchFrame);
430 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
431 printObj( stgCast(StgClosure*,sp) );
435 fprintf(stderr, "RET_DYN (%p)\n", sp);
438 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
443 fprintf(stderr, "RET_SMALL (%p)\n", sp);
444 bitmap = info->layout.bitmap;
447 while (bitmap != 0) {
448 fprintf(stderr," stk[%d] (%p) = ", spBottom-sp, sp);
449 if ((bitmap & 1) == 0) {
451 fprintf(stderr,"\n");
453 fprintf(stderr,"Word# %d\n", *sp++);
456 bitmap = bitmap >> 1;
468 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
469 sp = printStackObj(sp);
473 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
475 /* check everything down to the first update frame */
476 printStackChunk( sp, stgCast(StgPtr,su) );
477 while ( stgCast(StgPtr,su) < spBottom) {
478 sp = stgCast(StgPtr,su);
479 switch (get_itbl(su)->type) {
481 printObj( stgCast(StgClosure*,su) );
482 sp += sizeofW(StgUpdateFrame);
486 printObj( stgCast(StgClosure*,su) );
487 sp += sizeofW(StgSeqFrame);
488 su = stgCast(StgSeqFrame*,su)->link;
491 printObj( stgCast(StgClosure*,su) );
492 sp += sizeofW(StgCatchFrame);
493 su = stgCast(StgCatchFrame*,su)->link;
496 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
497 printObj( stgCast(StgClosure*,su) );
500 barf("printStack: weird record found on update frame list.");
502 printStackChunk( sp, stgCast(StgPtr,su) );
504 ASSERT(stgCast(StgPtr,su) == spBottom);
507 void printTSO( StgTSO *tso )
509 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
510 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
513 /* -----------------------------------------------------------------------------
516 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
517 -------------------------------------------------------------------------- */
519 static char *closure_type_names[] = {
520 "INVALID_OBJECT", /* 0 */
522 "CONSTR_1_0", /* 2 */
523 "CONSTR_0_1", /* 3 */
524 "CONSTR_2_0", /* 4 */
525 "CONSTR_1_1", /* 5 */
526 "CONSTR_0_2", /* 6 */
527 "CONSTR_INTLIKE", /* 7 */
528 "CONSTR_CHARLIKE", /* 8 */
529 "CONSTR_STATIC", /* 9 */
530 "CONSTR_NOCAF_STATIC", /* 10 */
537 "FUN_STATIC", /* 17 */
539 "THUNK_1_0", /* 19 */
540 "THUNK_0_1", /* 20 */
541 "THUNK_2_0", /* 21 */
542 "THUNK_1_1", /* 22 */
543 "THUNK_0_2", /* 23 */
544 "THUNK_STATIC", /* 24 */
545 "THUNK_SELECTOR", /* 25 */
550 "IND_OLDGEN", /* 30 */
552 "IND_OLDGEN_PERM", /* 32 */
553 "IND_STATIC", /* 33 */
554 "CAF_UNENTERED", /* 34 */
555 "CAF_ENTERED", /* 35 */
556 "CAF_BLACKHOLE", /* 36 */
558 "RET_SMALL", /* 38 */
559 "RET_VEC_SMALL", /* 39 */
561 "RET_VEC_BIG", /* 41 */
563 "UPDATE_FRAME", /* 43 */
564 "CATCH_FRAME", /* 44 */
565 "STOP_FRAME", /* 45 */
566 "SEQ_FRAME", /* 46 */
567 "BLACKHOLE", /* 47 */
568 "BLACKHOLE_BQ", /* 48 */
569 "SE_BLACKHOLE", /* 49 */
570 "SE_CAF_BLACKHOLE", /* 50 */
572 "ARR_WORDS", /* 52 */
573 "MUT_ARR_PTRS", /* 53 */
574 "MUT_ARR_PTRS_FROZEN", /* 54 */
578 "STABLE_NAME", /* 58 */
580 "BLOCKED_FETCH", /* 60 */
582 "FETCH_ME_BQ", /* 62 */
584 "EVACUATED", /* 64 */
585 "N_CLOSURE_TYPES" /* 65 */
589 info_type(StgClosure *closure){
590 return closure_type_names[get_itbl(closure)->type];
594 info_type_by_ip(StgInfoTable *ip){
595 return closure_type_names[ip->type];
599 info_hdr_type(StgClosure *closure, char *res){
600 strcpy(res,closure_type_names[get_itbl(closure)->type]);
603 /* --------------------------------------------------------------------------
604 * Address printing code
606 * Uses symbol table in (unstripped executable)
607 * ------------------------------------------------------------------------*/
609 /* --------------------------------------------------------------------------
610 * Simple lookup table
612 * Current implementation is pretty dumb!
613 * ------------------------------------------------------------------------*/
620 static nat max_table_size;
621 static nat table_size;
622 static struct entry* table;
624 static void reset_table( int size )
626 max_table_size = size;
628 table = (struct entry *) malloc(size * sizeof(struct entry));
631 static void prepare_table( void )
633 /* Could sort it... */
636 static void insert( unsigned value, const char *name )
638 if ( table_size >= max_table_size ) {
639 barf( "Symbol table overflow\n" );
641 table[table_size].value = value;
642 table[table_size].name = name;
643 table_size = table_size + 1;
648 static rtsBool lookup_name( char *name, unsigned *result )
651 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
653 if (i < table_size) {
654 *result = table[i].value;
662 /* Code from somewhere inside GHC (circa 1994)
664 * "std"++xs -> "Zstd"++xs
665 * char_to_c 'Z' = "ZZ"
666 * char_to_c '&' = "Za"
667 * char_to_c '|' = "Zb"
668 * char_to_c ':' = "Zc"
669 * char_to_c '/' = "Zd"
670 * char_to_c '=' = "Ze"
671 * char_to_c '>' = "Zg"
672 * char_to_c '#' = "Zh"
673 * char_to_c '<' = "Zl"
674 * char_to_c '-' = "Zm"
675 * char_to_c '!' = "Zn"
676 * char_to_c '.' = "Zo"
677 * char_to_c '+' = "Zp"
678 * char_to_c '\'' = "Zq"
679 * char_to_c '*' = "Zt"
680 * char_to_c '_' = "Zu"
681 * char_to_c c = "Z" ++ show (ord c)
683 static char unZcode( char ch )
686 case 'a' : return ('&');
687 case 'b' : return ('|');
688 case 'c' : return (':');
689 case 'd' : return ('/');
690 case 'e' : return ('=');
691 case 'g' : return ('>');
692 case 'h' : return ('#');
693 case 'l' : return ('<');
694 case 'm' : return ('-');
695 case 'n' : return ('!');
696 case 'o' : return ('.');
697 case 'p' : return ('+');
698 case 'q' : return ('\'');
699 case 't' : return ('*');
700 case 'u' : return ('_');
702 case '\0' : return ('Z');
703 default : return (ch);
708 /* Precondition: out big enough to handle output (about twice length of in) */
709 static void enZcode( char *in, char *out )
715 for( i = 0; in[i] != '\0'; ++i ) {
790 rtsBool lookupGHCName( StgPtr addr, const char **result )
793 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
795 if (i < table_size) {
796 *result = table[i].name;
803 static void printZcoded( const char *raw )
807 while ( raw[j] != '\0' ) {
809 fputc(unZcode(raw[j+1]),stderr);
812 fputc(raw[j],stderr);
818 /* --------------------------------------------------------------------------
819 * Symbol table loading
820 * ------------------------------------------------------------------------*/
822 /* Causing linking trouble on Win32 plats, so I'm
823 disabling this for now.
825 /* For now, BFD support is unconditionally disabled -- HWL */
826 #if 0 /* HWL */ && defined(HAVE_BFD_H) && !defined(_WIN32)
830 /* Fairly ad-hoc piece of code that seems to filter out a lot of
831 * rubbish like the obj-splitting symbols
834 static rtsBool isReal( flagword flags, const char *name )
837 /* ToDo: make this work on BFD */
838 int tp = type & N_TYPE;
839 if (tp == N_TEXT || tp == N_DATA) {
840 return (name[0] == '_' && name[1] != '_');
845 (void)flags; /* keep gcc -Wall happy */
847 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
848 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
855 extern void DEBUG_LoadSymbols( char *name )
861 abfd = bfd_openr(name, "default");
863 barf("can't open executable %s to get symbol table", name);
865 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
871 asymbol **symbol_table;
872 long number_of_symbols;
873 long num_real_syms = 0;
876 storage_needed = bfd_get_symtab_upper_bound (abfd);
878 if (storage_needed < 0) {
879 barf("can't read symbol table");
882 if (storage_needed == 0) {
883 belch("no storage needed");
886 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
888 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
890 if (number_of_symbols < 0) {
891 barf("can't canonicalise symbol table");
894 for( i = 0; i != number_of_symbols; ++i ) {
896 bfd_get_symbol_info(abfd,symbol_table[i],&info);
897 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
898 if (isReal(info.type, info.name)) {
904 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
905 number_of_symbols, num_real_syms)
908 reset_table( num_real_syms );
910 for( i = 0; i != number_of_symbols; ++i ) {
912 bfd_get_symbol_info(abfd,symbol_table[i],&info);
913 if (isReal(info.type, info.name)) {
914 insert( info.value, info.name );
923 #else /* HAVE_BFD_H */
925 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
930 #endif /* HAVE_BFD_H */
932 #include "StoragePriv.h"
941 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
942 for (s = 0; s < generations[g].n_steps; s++) {
943 for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
944 for (q = bd->start; q < bd->free; q++) {
955 void printPtr( StgPtr p )
957 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
960 void printObj( StgClosure *obj )
962 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );