1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.50 2001/12/06 14:33:02 simonmar Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
20 #include "Bytecodes.h" /* for InstrPtr */
21 #include "Disassembler.h"
25 #if defined(GRAN) || defined(PAR)
26 // HWL: explicit fixed header size to make debugging easier
27 int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable),
28 uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame);
31 /* --------------------------------------------------------------------------
32 * local function decls
33 * ------------------------------------------------------------------------*/
35 static void printStdObject( StgClosure *obj, char* tag );
36 static void reset_table ( int size );
37 static void prepare_table ( void );
38 static void insert ( unsigned value, const char *name );
39 #if 0 /* unused but might be useful sometime */
40 static rtsBool lookup_name ( char *name, unsigned *result );
41 static void enZcode ( char *in, char *out );
43 static char unZcode ( char ch );
44 const char * lookupGHCName ( void *addr );
45 static void printZcoded ( const char *raw );
47 /* --------------------------------------------------------------------------
49 * ------------------------------------------------------------------------*/
51 void printPtr( StgPtr p )
54 raw = lookupGHCName(p);
58 fprintf(stdout, "%p", p);
62 void printObj( StgClosure *obj )
64 fprintf(stdout,"Object "); printPtr((StgPtr)obj); fprintf(stdout," = ");
69 printStdObjHdr( StgClosure *obj, char* tag )
71 fprintf(stdout,"%s(",tag);
72 printPtr((StgPtr)obj->header.info);
74 fprintf(stdout,", %s", obj->header.prof.ccs->cc->label);
79 printStdObject( StgClosure *obj, char* tag )
82 const StgInfoTable* info;
84 printStdObjHdr( obj, tag );
87 for (i = 0; i < info->layout.payload.ptrs; ++i) {
89 printPtr((StgPtr)obj->payload[i]);
91 for (j = 0; j < info->layout.payload.nptrs; ++j) {
92 fprintf(stdout,", %pd#",obj->payload[i+j]);
94 fprintf(stdout,")\n");
98 printClosure( StgClosure *obj )
102 info = get_itbl(obj);
104 switch ( info->type ) {
106 barf("Invalid object");
108 disassemble( (StgBCO*)obj );
113 StgMutVar* mv = (StgMutVar*)obj;
114 fprintf(stdout,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
120 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
122 fprintf(stdout,"AP_UPD("); printPtr((StgPtr)ap->fun);
123 for (i = 0; i < ap->n_args; ++i) {
124 fprintf(stdout,", ");
125 printPtr((P_)ap->payload[i]);
127 fprintf(stdout,")\n");
133 StgPAP* pap = stgCast(StgPAP*,obj);
135 fprintf(stdout,"PAP("); printPtr((StgPtr)pap->fun);
136 for (i = 0; i < pap->n_args; ++i) {
137 fprintf(stdout,", ");
138 printPtr((StgPtr)pap->payload[i]);
140 fprintf(stdout,")\n");
145 fprintf(stdout,"FOREIGN(");
146 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
147 fprintf(stdout,")\n");
151 fprintf(stdout,"IND(");
152 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
153 fprintf(stdout,")\n");
157 fprintf(stdout,"IND_STATIC(");
158 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
159 fprintf(stdout,")\n");
163 fprintf(stdout,"IND_OLDGEN(");
164 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
165 fprintf(stdout,")\n");
169 fprintf(stdout,"CAF_BH(");
170 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
171 fprintf(stdout,")\n");
175 fprintf(stdout,"SE_BH\n");
178 case SE_CAF_BLACKHOLE:
179 fprintf(stdout,"SE_CAF_BH\n");
183 fprintf(stdout,"BH\n");
187 fprintf(stdout,"BQ(");
188 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
189 fprintf(stdout,")\n");
193 fprintf(stdout,"TSO(");
194 fprintf(stdout,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
195 fprintf(stdout,")\n");
200 fprintf(stdout,"BLOCKED_FETCH(");
201 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
202 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
203 fprintf(stdout,")\n");
207 fprintf(stdout,"FETCH_ME(");
208 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
209 fprintf(stdout,")\n");
214 fprintf(stdout,"REMOTE_REF(");
215 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
216 fprintf(stdout,")\n");
221 fprintf(stdout,"FETCH_ME_BQ(");
222 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
223 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
224 fprintf(stdout,")\n");
227 #if defined(GRAN) || defined(PAR)
229 fprintf(stdout,"RBH(");
230 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
231 fprintf(stdout,")\n");
237 case CONSTR_1_0: case CONSTR_0_1:
238 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
240 case CONSTR_CHARLIKE:
242 case CONSTR_NOCAF_STATIC:
244 /* We can't use printStdObject because we want to print the
249 fprintf(stdout,"%s(", info->prof.closure_desc);
250 fprintf(stdout,"%s", obj->header.prof.ccs->cc->label);
252 fprintf(stdout,"CONSTR(");
253 printPtr((StgPtr)obj->header.info);
254 fprintf(stdout,"(tag=%d)",info->srt_len);
256 for (i = 0; i < info->layout.payload.ptrs; ++i) {
257 fprintf(stdout,", ");
258 printPtr((StgPtr)obj->payload[i]);
260 for (j = 0; j < info->layout.payload.nptrs; ++j) {
261 fprintf(stdout,", %p#", obj->payload[i+j]);
263 fprintf(stdout,")\n");
268 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
269 case MUT_ARR_PTRS_FROZEN:
272 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
274 fprintf(stdout,"Row<%i>(",p->ptrs);
275 for (i = 0; i < p->ptrs; ++i) {
276 if (i > 0) fprintf(stdout,", ");
277 printPtr((StgPtr)(p->payload[i]));
279 fprintf(stdout,")\n");
285 case FUN_1_0: case FUN_0_1:
286 case FUN_1_1: case FUN_0_2: case FUN_2_0:
288 printStdObject(obj,"FUN");
292 case THUNK_1_0: case THUNK_0_1:
293 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
295 /* ToDo: will this work for THUNK_STATIC too? */
297 printStdObject(obj,info->prof.closure_desc);
299 printStdObject(obj,"THUNK");
304 printStdObjHdr(obj, "THUNK_SELECTOR");
305 fprintf(stdout, ", %p)\n", ((StgSelector *)obj)->selectee);
311 fprintf(stdout,"ARR_WORDS(\"");
312 /* ToDo: we can't safely assume that this is a string!
313 for (i = 0; arrWordsGetChar(obj,i); ++i) {
314 putchar(arrWordsGetChar(obj,i));
316 for (i=0; i<((StgArrWords *)obj)->words; i++)
317 fprintf(stdout, "%ld", ((StgArrWords *)obj)->payload[i]);
318 fprintf(stdout,"\")\n");
324 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
325 fprintf(stdout,"UpdateFrame(");
326 printPtr((StgPtr)GET_INFO(u));
328 printPtr((StgPtr)u->updatee);
330 printPtr((StgPtr)u->link);
331 fprintf(stdout,")\n");
337 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
338 fprintf(stdout,"CatchFrame(");
339 printPtr((StgPtr)GET_INFO(u));
341 printPtr((StgPtr)u->handler);
343 printPtr((StgPtr)u->link);
344 fprintf(stdout,")\n");
350 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
351 fprintf(stdout,"SeqFrame(");
352 printPtr((StgPtr)GET_INFO(u));
354 printPtr((StgPtr)u->link);
355 fprintf(stdout,")\n");
361 StgStopFrame* u = stgCast(StgStopFrame*,obj);
362 fprintf(stdout,"StopFrame(");
363 printPtr((StgPtr)GET_INFO(u));
364 fprintf(stdout,")\n");
368 //barf("printClosure %d",get_itbl(obj)->type);
369 fprintf(stdout, "*** printClosure: unknown type %d ****\n",
370 get_itbl(obj)->type );
371 barf("printClosure %d",get_itbl(obj)->type);
377 void printGraph( StgClosure *obj )
383 StgPtr printStackObj( StgPtr sp )
385 /*fprintf(stdout,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
387 if (IS_ARG_TAG(*sp)) {
390 fprintf(stdout,"Tagged{");
391 for (i = 0; i < tag; i++) {
392 fprintf(stdout,"0x%x#", (unsigned)(*sp++));
393 if (i < tag-1) fprintf(stdout, ", ");
395 fprintf(stdout, "}\n");
397 StgClosure* c = (StgClosure*)(*sp);
398 printPtr((StgPtr)*sp);
399 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
400 fprintf(stdout, "\t\t\tstg_ctoi_ret_R1p_info\n" );
402 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
403 fprintf(stdout, "\t\t\tstg_ctoi_ret_R1n_info\n" );
405 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
406 fprintf(stdout, "\t\t\tstg_ctoi_ret_F1_info\n" );
408 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
409 fprintf(stdout, "\t\t\tstg_ctoi_ret_D1_info\n" );
411 if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
412 fprintf(stdout, "\t\t\tstg_ctoi_ret_V_info\n" );
414 if (get_itbl(c)->type == BCO) {
415 fprintf(stdout, "\t\t\t");
416 fprintf(stdout, "BCO(...)\n");
419 fprintf(stdout, "\t\t\t");
420 printClosure ( (StgClosure*)(*sp));
428 void printStackChunk( StgPtr sp, StgPtr spBottom )
431 const StgInfoTable *info;
433 ASSERT(sp <= spBottom);
434 while (sp < spBottom) {
435 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
436 info = get_itbl((StgClosure *)sp);
437 switch (info->type) {
440 printObj( stgCast(StgClosure*,sp) );
441 sp += sizeofW(StgUpdateFrame);
445 printObj( stgCast(StgClosure*,sp) );
446 sp += sizeofW(StgSeqFrame);
450 printObj( stgCast(StgClosure*,sp) );
451 sp += sizeofW(StgCatchFrame);
455 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
456 printObj( stgCast(StgClosure*,sp) );
460 fprintf(stdout, "RET_DYN (%p)\n", sp);
463 fprintf(stdout, "Bitmap: 0x%x\n", bitmap);
468 fprintf(stdout, "RET_SMALL (%p)\n", sp);
469 bitmap = info->layout.bitmap;
472 while (bitmap != 0) {
473 fprintf(stdout," stk[%ld] (%p) = ", spBottom-sp, sp);
474 if ((bitmap & 1) == 0) {
476 fprintf(stdout,"\n");
478 fprintf(stdout,"Word# %ld\n", *sp);
481 bitmap = bitmap >> 1;
493 fprintf(stdout,"Stack[%ld] (%p) = ", spBottom-sp, sp);
494 sp = printStackObj(sp);
498 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
500 /* check everything down to the first update frame */
501 printStackChunk( sp, stgCast(StgPtr,su) );
502 while ( stgCast(StgPtr,su) < spBottom) {
503 sp = stgCast(StgPtr,su);
504 switch (get_itbl(su)->type) {
506 printObj( stgCast(StgClosure*,su) );
507 sp += sizeofW(StgUpdateFrame);
511 printObj( stgCast(StgClosure*,su) );
512 sp += sizeofW(StgSeqFrame);
513 su = stgCast(StgSeqFrame*,su)->link;
516 printObj( stgCast(StgClosure*,su) );
517 sp += sizeofW(StgCatchFrame);
518 su = stgCast(StgCatchFrame*,su)->link;
521 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
522 printObj( stgCast(StgClosure*,su) );
525 barf("printStack: weird record found on update frame list.");
527 printStackChunk( sp, stgCast(StgPtr,su) );
529 ASSERT(stgCast(StgPtr,su) == spBottom);
532 void printTSO( StgTSO *tso )
534 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
535 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
538 /* -----------------------------------------------------------------------------
541 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
542 -------------------------------------------------------------------------- */
544 static char *closure_type_names[] = {
545 "INVALID_OBJECT", /* 0 */
547 "CONSTR_1_0", /* 2 */
548 "CONSTR_0_1", /* 3 */
549 "CONSTR_2_0", /* 4 */
550 "CONSTR_1_1", /* 5 */
551 "CONSTR_0_2", /* 6 */
552 "CONSTR_INTLIKE", /* 7 */
553 "CONSTR_CHARLIKE", /* 8 */
554 "CONSTR_STATIC", /* 9 */
555 "CONSTR_NOCAF_STATIC", /* 10 */
562 "FUN_STATIC", /* 17 */
564 "THUNK_1_0", /* 19 */
565 "THUNK_0_1", /* 20 */
566 "THUNK_2_0", /* 21 */
567 "THUNK_1_1", /* 22 */
568 "THUNK_0_2", /* 23 */
569 "THUNK_STATIC", /* 24 */
570 "THUNK_SELECTOR", /* 25 */
575 "IND_OLDGEN", /* 30 */
577 "IND_OLDGEN_PERM", /* 32 */
578 "IND_STATIC", /* 33 */
579 "CAF_BLACKHOLE", /* 36 */
581 "RET_SMALL", /* 38 */
582 "RET_VEC_SMALL", /* 39 */
584 "RET_VEC_BIG", /* 41 */
586 "UPDATE_FRAME", /* 43 */
587 "CATCH_FRAME", /* 44 */
588 "STOP_FRAME", /* 45 */
589 "SEQ_FRAME", /* 46 */
590 "BLACKHOLE", /* 47 */
591 "BLACKHOLE_BQ", /* 48 */
592 "SE_BLACKHOLE", /* 49 */
593 "SE_CAF_BLACKHOLE", /* 50 */
595 "ARR_WORDS", /* 52 */
596 "MUT_ARR_PTRS", /* 53 */
597 "MUT_ARR_PTRS_FROZEN", /* 54 */
601 "STABLE_NAME", /* 58 */
603 "BLOCKED_FETCH", /* 60 */
605 "FETCH_ME_BQ", /* 62 */
607 "EVACUATED", /* 64 */
608 "REMOTE_REF", /* 65 */
609 "N_CLOSURE_TYPES" /* 66 */
613 info_type(StgClosure *closure){
614 return closure_type_names[get_itbl(closure)->type];
618 info_type_by_ip(StgInfoTable *ip){
619 return closure_type_names[ip->type];
623 info_hdr_type(StgClosure *closure, char *res){
624 strcpy(res,closure_type_names[get_itbl(closure)->type]);
627 /* --------------------------------------------------------------------------
628 * Address printing code
630 * Uses symbol table in (unstripped executable)
631 * ------------------------------------------------------------------------*/
633 /* --------------------------------------------------------------------------
634 * Simple lookup table
636 * Current implementation is pretty dumb!
637 * ------------------------------------------------------------------------*/
644 static nat max_table_size;
645 static nat table_size;
646 static struct entry* table;
648 static void reset_table( int size )
650 max_table_size = size;
652 table = (struct entry *) malloc(size * sizeof(struct entry));
655 static void prepare_table( void )
657 /* Could sort it... */
660 static void insert( unsigned value, const char *name )
662 if ( table_size >= max_table_size ) {
663 barf( "Symbol table overflow\n" );
665 table[table_size].value = value;
666 table[table_size].name = name;
667 table_size = table_size + 1;
672 static rtsBool lookup_name( char *name, unsigned *result )
675 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
677 if (i < table_size) {
678 *result = table[i].value;
686 /* Code from somewhere inside GHC (circa 1994)
688 * "std"++xs -> "Zstd"++xs
689 * char_to_c 'Z' = "ZZ"
690 * char_to_c '&' = "Za"
691 * char_to_c '|' = "Zb"
692 * char_to_c ':' = "Zc"
693 * char_to_c '/' = "Zd"
694 * char_to_c '=' = "Ze"
695 * char_to_c '>' = "Zg"
696 * char_to_c '#' = "Zh"
697 * char_to_c '<' = "Zl"
698 * char_to_c '-' = "Zm"
699 * char_to_c '!' = "Zn"
700 * char_to_c '.' = "Zo"
701 * char_to_c '+' = "Zp"
702 * char_to_c '\'' = "Zq"
703 * char_to_c '*' = "Zt"
704 * char_to_c '_' = "Zu"
705 * char_to_c c = "Z" ++ show (ord c)
707 static char unZcode( char ch )
710 case 'a' : return ('&');
711 case 'b' : return ('|');
712 case 'c' : return (':');
713 case 'd' : return ('/');
714 case 'e' : return ('=');
715 case 'g' : return ('>');
716 case 'h' : return ('#');
717 case 'l' : return ('<');
718 case 'm' : return ('-');
719 case 'n' : return ('!');
720 case 'o' : return ('.');
721 case 'p' : return ('+');
722 case 'q' : return ('\'');
723 case 't' : return ('*');
724 case 'u' : return ('_');
726 case '\0' : return ('Z');
727 default : return (ch);
732 /* Precondition: out big enough to handle output (about twice length of in) */
733 static void enZcode( char *in, char *out )
739 for( i = 0; in[i] != '\0'; ++i ) {
814 const char *lookupGHCName( void *addr )
817 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
819 if (i < table_size) {
820 return table[i].name;
826 static void printZcoded( const char *raw )
830 while ( raw[j] != '\0' ) {
832 fputc(unZcode(raw[j+1]),stdout);
835 fputc(raw[j],stdout);
841 /* --------------------------------------------------------------------------
842 * Symbol table loading
843 * ------------------------------------------------------------------------*/
845 /* Causing linking trouble on Win32 plats, so I'm
846 disabling this for now.
852 /* Fairly ad-hoc piece of code that seems to filter out a lot of
853 * rubbish like the obj-splitting symbols
856 static rtsBool isReal( flagword flags, const char *name )
859 /* ToDo: make this work on BFD */
860 int tp = type & N_TYPE;
861 if (tp == N_TEXT || tp == N_DATA) {
862 return (name[0] == '_' && name[1] != '_');
867 (void)flags; /* keep gcc -Wall happy */
869 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
870 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
877 extern void DEBUG_LoadSymbols( char *name )
883 abfd = bfd_openr(name, "default");
885 barf("can't open executable %s to get symbol table", name);
887 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
893 asymbol **symbol_table;
894 long number_of_symbols;
895 long num_real_syms = 0;
898 storage_needed = bfd_get_symtab_upper_bound (abfd);
900 if (storage_needed < 0) {
901 barf("can't read symbol table");
904 if (storage_needed == 0) {
905 belch("no storage needed");
908 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
910 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
912 if (number_of_symbols < 0) {
913 barf("can't canonicalise symbol table");
916 for( i = 0; i != number_of_symbols; ++i ) {
918 bfd_get_symbol_info(abfd,symbol_table[i],&info);
919 /*fprintf(stdout,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
920 if (isReal(info.type, info.name)) {
926 fprintf(stdout,"Loaded %ld symbols. Of which %ld are real symbols\n",
927 number_of_symbols, num_real_syms)
930 reset_table( num_real_syms );
932 for( i = 0; i != number_of_symbols; ++i ) {
934 bfd_get_symbol_info(abfd,symbol_table[i],&info);
935 if (isReal(info.type, info.name)) {
936 insert( info.value, info.name );
945 #else /* HAVE_BFD_H */
947 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
952 #endif /* HAVE_BFD_H */
954 #include "StoragePriv.h"
956 void findPtr(P_ p, int); /* keep gcc -Wall happy */
959 findPtr(P_ p, int follow)
964 const int arr_size = 1024;
965 StgPtr arr[arr_size];
968 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
969 for (s = 0; s < generations[g].n_steps; s++) {
970 if (RtsFlags.GcFlags.generations == 1) {
971 bd = generations[g].steps[s].to_blocks;
973 bd = generations[g].steps[s].blocks;
975 for (; bd; bd = bd->link) {
976 for (q = bd->start; q < bd->free; q++) {
980 while (!LOOKS_LIKE_GHC_INFO(*r) || *r == NULL) {
983 fprintf(stdout, "%p = ", r);
984 printClosure((StgClosure *)r);
994 if (follow && i == 1) {
995 fprintf(stdout, "-->\n");
1001 void printPtr( StgPtr p )
1003 fprintf(stdout, "ptr 0x%p (enable -DDEBUG for more info) " , p );
1006 void printObj( StgClosure *obj )
1008 fprintf(stdout, "obj 0x%p (enable -DDEBUG for more info) " , obj );