1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.29 2000/12/11 12:40:24 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 * ------------------------------------------------------------------------*/
49 char* lookupHugsItblName ( void* itbl );
52 void printPtr( StgPtr p )
58 if (lookupGHCName( p, &raw )) {
61 } else if ((raw = lookupHugsName(p)) != 0) {
62 fprintf(stderr, "%s", raw);
63 } else if ((str = lookupHugsItblName(p)) != 0) {
64 fprintf(stderr, "%p=%s", p, str);
67 fprintf(stderr, "%p", p);
71 void printObj( StgClosure *obj )
73 fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
77 static void printStdObject( StgClosure *obj, char* tag )
80 const StgInfoTable* info = get_itbl(obj);
81 fprintf(stderr,"%s(",tag);
82 printPtr((StgPtr)obj->header.info);
83 for (i = 0; i < info->layout.payload.ptrs; ++i) {
85 printPtr((StgPtr)obj->payload[i]);
87 for (j = 0; j < info->layout.payload.nptrs; ++j) {
88 fprintf(stderr,", %pd#",obj->payload[i+j]);
90 fprintf(stderr,")\n");
93 void printClosure( StgClosure *obj )
95 switch ( get_itbl(obj)->type ) {
97 barf("Invalid object");
100 fprintf(stderr,"BCO\n");
101 disassemble(stgCast(StgBCO*,obj),"\t");
107 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
109 fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
110 for (i = 0; i < ap->n_args; ++i) {
111 fprintf(stderr,", ");
112 printPtr((P_)ap->payload[i]);
114 fprintf(stderr,")\n");
120 StgPAP* pap = stgCast(StgPAP*,obj);
122 fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
123 for (i = 0; i < pap->n_args; ++i) {
124 fprintf(stderr,", ");
125 printPtr((StgPtr)pap->payload[i]);
127 fprintf(stderr,")\n");
132 fprintf(stderr,"IND(");
133 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
134 fprintf(stderr,")\n");
138 fprintf(stderr,"IND_STATIC(");
139 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
140 fprintf(stderr,")\n");
144 fprintf(stderr,"IND_OLDGEN(");
145 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
146 fprintf(stderr,")\n");
151 StgCAF* caf = stgCast(StgCAF*,obj);
152 fprintf(stderr,"CAF_UNENTERED(");
153 printPtr((StgPtr)caf->body);
154 fprintf(stderr,", ");
155 printPtr((StgPtr)caf->value); /* should be null */
156 fprintf(stderr,", ");
157 printPtr((StgPtr)caf->link);
158 fprintf(stderr,")\n");
164 StgCAF* caf = stgCast(StgCAF*,obj);
165 fprintf(stderr,"CAF_ENTERED(");
166 printPtr((StgPtr)caf->body);
167 fprintf(stderr,", ");
168 printPtr((StgPtr)caf->value);
169 fprintf(stderr,", ");
170 printPtr((StgPtr)caf->link);
171 fprintf(stderr,")\n");
176 fprintf(stderr,"CAF_BH(");
177 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
178 fprintf(stderr,")\n");
182 fprintf(stderr,"SE_BH\n");
185 case SE_CAF_BLACKHOLE:
186 fprintf(stderr,"SE_CAF_BH\n");
190 fprintf(stderr,"BH\n");
194 fprintf(stderr,"BQ(");
195 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
196 fprintf(stderr,")\n");
200 fprintf(stderr,"TSO(");
201 fprintf(stderr,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
202 fprintf(stderr,")\n");
207 fprintf(stderr,"BLOCKED_FETCH(");
208 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
209 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
210 fprintf(stderr,")\n");
214 fprintf(stderr,"FETCH_ME(");
215 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
216 fprintf(stderr,")\n");
220 fprintf(stderr,"FETCH_ME_BQ(");
221 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
222 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
223 fprintf(stderr,")\n");
226 #if defined(GRAN) || defined(PAR)
228 fprintf(stderr,"RBH(");
229 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
230 fprintf(stderr,")\n");
236 case CONSTR_1_0: case CONSTR_0_1:
237 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
239 case CONSTR_CHARLIKE:
241 case CONSTR_NOCAF_STATIC:
243 /* We can't use printStdObject because we want to print the
247 const StgInfoTable* info = get_itbl(obj);
248 fprintf(stderr,"PACK(");
249 printPtr((StgPtr)obj->header.info);
250 fprintf(stderr,"(tag=%d)",info->srt_len);
251 for (i = 0; i < info->layout.payload.ptrs; ++i) {
252 fprintf(stderr,", ");
253 printPtr((StgPtr)obj->payload[i]);
255 for (j = 0; j < info->layout.payload.nptrs; ++j) {
256 fprintf(stderr,", %p#", obj->payload[i+j]);
258 fprintf(stderr,")\n");
263 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
264 case MUT_ARR_PTRS_FROZEN:
267 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
269 fprintf(stderr,"Row<%i>(",p->ptrs);
270 for (i = 0; i < p->ptrs; ++i) {
271 if (i > 0) fprintf(stderr,", ");
272 printPtr((StgPtr)(p->payload[i]));
274 fprintf(stderr,")\n");
280 case FUN_1_0: case FUN_0_1:
281 case FUN_1_1: case FUN_0_2: case FUN_2_0:
283 printStdObject(obj,"FUN");
287 case THUNK_1_0: case THUNK_0_1:
288 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
290 /* ToDo: will this work for THUNK_STATIC too? */
291 printStdObject(obj,"THUNK");
295 printStdObject(obj,"THUNK_SELECTOR");
301 fprintf(stderr,"ARR_WORDS(\"");
302 /* ToDo: we can't safely assume that this is a string!
303 for (i = 0; arrWordsGetChar(obj,i); ++i) {
304 putchar(arrWordsGetChar(obj,i));
306 for (i=0; i<((StgArrWords *)obj)->words; i++)
307 fprintf(stderr, "%d", ((StgArrWords *)obj)->payload[i]);
308 fprintf(stderr,"\")\n");
314 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
315 fprintf(stderr,"UpdateFrame(");
316 printPtr((StgPtr)GET_INFO(u));
318 printPtr((StgPtr)u->updatee);
320 printPtr((StgPtr)u->link);
321 fprintf(stderr,")\n");
327 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
328 fprintf(stderr,"CatchFrame(");
329 printPtr((StgPtr)GET_INFO(u));
331 printPtr((StgPtr)u->handler);
333 printPtr((StgPtr)u->link);
334 fprintf(stderr,")\n");
340 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
341 fprintf(stderr,"SeqFrame(");
342 printPtr((StgPtr)GET_INFO(u));
344 printPtr((StgPtr)u->link);
345 fprintf(stderr,")\n");
351 StgStopFrame* u = stgCast(StgStopFrame*,obj);
352 fprintf(stderr,"StopFrame(");
353 printPtr((StgPtr)GET_INFO(u));
354 fprintf(stderr,")\n");
358 //barf("printClosure %d",get_itbl(obj)->type);
359 fprintf(stderr, "*** printClosure: unknown type %d ****\n",
360 get_itbl(obj)->type );
366 void printGraph( StgClosure *obj )
372 StgPtr printStackObj( StgPtr sp )
374 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
376 if (IS_ARG_TAG(*sp)) {
379 fprintf(stderr,"Tagged{");
380 for (i = 0; i < tag; i++) {
381 fprintf(stderr,"0x%x#", (unsigned)(*sp++));
382 if (i < tag-1) fprintf(stderr, ", ");
384 fprintf(stderr, "}\n");
386 StgClosure* c = (StgClosure*)(*sp);
387 printPtr((StgPtr)*sp);
389 if (c == &ret_bco_info) {
390 fprintf(stderr, "\t\t");
391 fprintf(stderr, "ret_bco_info\n" );
393 if (IS_HUGS_CONSTR_INFO(GET_INFO(c))) {
394 fprintf(stderr, "\t\t\t");
395 fprintf(stderr, "ConstrInfoTable\n" );
398 if (get_itbl(c)->type == BCO) {
399 fprintf(stderr, "\t\t\t");
400 fprintf(stderr, "BCO(...)\n");
403 fprintf(stderr, "\t\t\t");
404 printClosure ( (StgClosure*)(*sp));
412 void printStackChunk( StgPtr sp, StgPtr spBottom )
415 const StgInfoTable *info;
417 ASSERT(sp <= spBottom);
418 while (sp < spBottom) {
419 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
420 info = get_itbl((StgClosure *)sp);
421 switch (info->type) {
424 printObj( stgCast(StgClosure*,sp) );
425 sp += sizeofW(StgUpdateFrame);
429 printObj( stgCast(StgClosure*,sp) );
430 sp += sizeofW(StgSeqFrame);
434 printObj( stgCast(StgClosure*,sp) );
435 sp += sizeofW(StgCatchFrame);
439 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
440 printObj( stgCast(StgClosure*,sp) );
444 fprintf(stderr, "RET_DYN (%p)\n", sp);
447 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
452 fprintf(stderr, "RET_SMALL (%p)\n", sp);
453 bitmap = info->layout.bitmap;
456 while (bitmap != 0) {
457 fprintf(stderr," stk[%d] (%p) = ", spBottom-sp, sp);
458 if ((bitmap & 1) == 0) {
460 fprintf(stderr,"\n");
462 fprintf(stderr,"Word# %d\n", *sp++);
465 bitmap = bitmap >> 1;
477 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
478 sp = printStackObj(sp);
482 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
484 /* check everything down to the first update frame */
485 printStackChunk( sp, stgCast(StgPtr,su) );
486 while ( stgCast(StgPtr,su) < spBottom) {
487 sp = stgCast(StgPtr,su);
488 switch (get_itbl(su)->type) {
490 printObj( stgCast(StgClosure*,su) );
491 sp += sizeofW(StgUpdateFrame);
495 printObj( stgCast(StgClosure*,su) );
496 sp += sizeofW(StgSeqFrame);
497 su = stgCast(StgSeqFrame*,su)->link;
500 printObj( stgCast(StgClosure*,su) );
501 sp += sizeofW(StgCatchFrame);
502 su = stgCast(StgCatchFrame*,su)->link;
505 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
506 printObj( stgCast(StgClosure*,su) );
509 barf("printStack: weird record found on update frame list.");
511 printStackChunk( sp, stgCast(StgPtr,su) );
513 ASSERT(stgCast(StgPtr,su) == spBottom);
516 void printTSO( StgTSO *tso )
518 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
519 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
522 /* -----------------------------------------------------------------------------
525 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
526 -------------------------------------------------------------------------- */
528 static char *closure_type_names[] = {
529 "INVALID_OBJECT", /* 0 */
531 "CONSTR_1_0", /* 2 */
532 "CONSTR_0_1", /* 3 */
533 "CONSTR_2_0", /* 4 */
534 "CONSTR_1_1", /* 5 */
535 "CONSTR_0_2", /* 6 */
536 "CONSTR_INTLIKE", /* 7 */
537 "CONSTR_CHARLIKE", /* 8 */
538 "CONSTR_STATIC", /* 9 */
539 "CONSTR_NOCAF_STATIC", /* 10 */
546 "FUN_STATIC", /* 17 */
548 "THUNK_1_0", /* 19 */
549 "THUNK_0_1", /* 20 */
550 "THUNK_2_0", /* 21 */
551 "THUNK_1_1", /* 22 */
552 "THUNK_0_2", /* 23 */
553 "THUNK_STATIC", /* 24 */
554 "THUNK_SELECTOR", /* 25 */
559 "IND_OLDGEN", /* 30 */
561 "IND_OLDGEN_PERM", /* 32 */
562 "IND_STATIC", /* 33 */
563 "CAF_UNENTERED", /* 34 */
564 "CAF_ENTERED", /* 35 */
565 "CAF_BLACKHOLE", /* 36 */
567 "RET_SMALL", /* 38 */
568 "RET_VEC_SMALL", /* 39 */
570 "RET_VEC_BIG", /* 41 */
572 "UPDATE_FRAME", /* 43 */
573 "CATCH_FRAME", /* 44 */
574 "STOP_FRAME", /* 45 */
575 "SEQ_FRAME", /* 46 */
576 "BLACKHOLE", /* 47 */
577 "BLACKHOLE_BQ", /* 48 */
578 "SE_BLACKHOLE", /* 49 */
579 "SE_CAF_BLACKHOLE", /* 50 */
581 "ARR_WORDS", /* 52 */
582 "MUT_ARR_PTRS", /* 53 */
583 "MUT_ARR_PTRS_FROZEN", /* 54 */
587 "STABLE_NAME", /* 58 */
589 "BLOCKED_FETCH", /* 60 */
591 "FETCH_ME_BQ", /* 62 */
593 "EVACUATED", /* 64 */
594 "N_CLOSURE_TYPES" /* 65 */
598 info_type(StgClosure *closure){
599 return closure_type_names[get_itbl(closure)->type];
603 info_type_by_ip(StgInfoTable *ip){
604 return closure_type_names[ip->type];
608 info_hdr_type(StgClosure *closure, char *res){
609 strcpy(res,closure_type_names[get_itbl(closure)->type]);
612 /* --------------------------------------------------------------------------
613 * Address printing code
615 * Uses symbol table in (unstripped executable)
616 * ------------------------------------------------------------------------*/
618 /* --------------------------------------------------------------------------
619 * Simple lookup table
621 * Current implementation is pretty dumb!
622 * ------------------------------------------------------------------------*/
629 static nat max_table_size;
630 static nat table_size;
631 static struct entry* table;
633 static void reset_table( int size )
635 max_table_size = size;
637 table = (struct entry *) malloc(size * sizeof(struct entry));
640 static void prepare_table( void )
642 /* Could sort it... */
645 static void insert( unsigned value, const char *name )
647 if ( table_size >= max_table_size ) {
648 barf( "Symbol table overflow\n" );
650 table[table_size].value = value;
651 table[table_size].name = name;
652 table_size = table_size + 1;
657 static rtsBool lookup_name( char *name, unsigned *result )
660 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
662 if (i < table_size) {
663 *result = table[i].value;
671 /* Code from somewhere inside GHC (circa 1994)
673 * "std"++xs -> "Zstd"++xs
674 * char_to_c 'Z' = "ZZ"
675 * char_to_c '&' = "Za"
676 * char_to_c '|' = "Zb"
677 * char_to_c ':' = "Zc"
678 * char_to_c '/' = "Zd"
679 * char_to_c '=' = "Ze"
680 * char_to_c '>' = "Zg"
681 * char_to_c '#' = "Zh"
682 * char_to_c '<' = "Zl"
683 * char_to_c '-' = "Zm"
684 * char_to_c '!' = "Zn"
685 * char_to_c '.' = "Zo"
686 * char_to_c '+' = "Zp"
687 * char_to_c '\'' = "Zq"
688 * char_to_c '*' = "Zt"
689 * char_to_c '_' = "Zu"
690 * char_to_c c = "Z" ++ show (ord c)
692 static char unZcode( char ch )
695 case 'a' : return ('&');
696 case 'b' : return ('|');
697 case 'c' : return (':');
698 case 'd' : return ('/');
699 case 'e' : return ('=');
700 case 'g' : return ('>');
701 case 'h' : return ('#');
702 case 'l' : return ('<');
703 case 'm' : return ('-');
704 case 'n' : return ('!');
705 case 'o' : return ('.');
706 case 'p' : return ('+');
707 case 'q' : return ('\'');
708 case 't' : return ('*');
709 case 'u' : return ('_');
711 case '\0' : return ('Z');
712 default : return (ch);
717 /* Precondition: out big enough to handle output (about twice length of in) */
718 static void enZcode( char *in, char *out )
724 for( i = 0; in[i] != '\0'; ++i ) {
799 rtsBool lookupGHCName( StgPtr addr, const char **result )
802 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
804 if (i < table_size) {
805 *result = table[i].name;
812 static void printZcoded( const char *raw )
816 while ( raw[j] != '\0' ) {
818 fputc(unZcode(raw[j+1]),stderr);
821 fputc(raw[j],stderr);
827 /* --------------------------------------------------------------------------
828 * Symbol table loading
829 * ------------------------------------------------------------------------*/
831 /* Causing linking trouble on Win32 plats, so I'm
832 disabling this for now.
834 #if defined(HAVE_BFD_H) && !defined(_WIN32)
838 /* Fairly ad-hoc piece of code that seems to filter out a lot of
839 * rubbish like the obj-splitting symbols
842 static rtsBool isReal( flagword flags, const char *name )
845 /* ToDo: make this work on BFD */
846 int tp = type & N_TYPE;
847 if (tp == N_TEXT || tp == N_DATA) {
848 return (name[0] == '_' && name[1] != '_');
853 (void)flags; /* keep gcc -Wall happy */
855 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
856 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
863 extern void DEBUG_LoadSymbols( char *name )
869 abfd = bfd_openr(name, "default");
871 barf("can't open executable %s to get symbol table", name);
873 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
879 asymbol **symbol_table;
880 long number_of_symbols;
881 long num_real_syms = 0;
884 storage_needed = bfd_get_symtab_upper_bound (abfd);
886 if (storage_needed < 0) {
887 barf("can't read symbol table");
890 if (storage_needed == 0) {
891 belch("no storage needed");
894 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
896 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
898 if (number_of_symbols < 0) {
899 barf("can't canonicalise symbol table");
902 for( i = 0; i != number_of_symbols; ++i ) {
904 bfd_get_symbol_info(abfd,symbol_table[i],&info);
905 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
906 if (isReal(info.type, info.name)) {
912 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
913 number_of_symbols, num_real_syms)
916 reset_table( num_real_syms );
918 for( i = 0; i != number_of_symbols; ++i ) {
920 bfd_get_symbol_info(abfd,symbol_table[i],&info);
921 if (isReal(info.type, info.name)) {
922 insert( info.value, info.name );
931 #else /* HAVE_BFD_H */
933 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
938 #endif /* HAVE_BFD_H */
940 #include "StoragePriv.h"
942 void findPtr(P_ p); /* keep gcc -Wall happy */
951 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
952 for (s = 0; s < generations[g].n_steps; s++) {
953 for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
954 for (q = bd->start; q < bd->free; q++) {
965 void printPtr( StgPtr p )
967 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
970 void printObj( StgClosure *obj )
972 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );