1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.49 2001/11/28 15:43:23 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," = ");
68 static void printStdObject( StgClosure *obj, char* tag )
71 const StgInfoTable* info = get_itbl(obj);
72 fprintf(stdout,"%s(",tag);
73 printPtr((StgPtr)obj->header.info);
75 fprintf(stdout,", %s", obj->header.prof.ccs->cc->label);
77 for (i = 0; i < info->layout.payload.ptrs; ++i) {
79 printPtr((StgPtr)obj->payload[i]);
81 for (j = 0; j < info->layout.payload.nptrs; ++j) {
82 fprintf(stdout,", %pd#",obj->payload[i+j]);
84 fprintf(stdout,")\n");
87 void printClosure( StgClosure *obj )
93 switch ( info->type ) {
95 barf("Invalid object");
97 disassemble( (StgBCO*)obj );
102 StgMutVar* mv = (StgMutVar*)obj;
103 fprintf(stderr,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
109 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
111 fprintf(stdout,"AP_UPD("); printPtr((StgPtr)ap->fun);
112 for (i = 0; i < ap->n_args; ++i) {
113 fprintf(stdout,", ");
114 printPtr((P_)ap->payload[i]);
116 fprintf(stdout,")\n");
122 StgPAP* pap = stgCast(StgPAP*,obj);
124 fprintf(stdout,"PAP("); printPtr((StgPtr)pap->fun);
125 for (i = 0; i < pap->n_args; ++i) {
126 fprintf(stdout,", ");
127 printPtr((StgPtr)pap->payload[i]);
129 fprintf(stdout,")\n");
134 fprintf(stderr,"FOREIGN(");
135 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
136 fprintf(stderr,")\n");
140 fprintf(stdout,"IND(");
141 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
142 fprintf(stdout,")\n");
146 fprintf(stdout,"IND_STATIC(");
147 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
148 fprintf(stdout,")\n");
152 fprintf(stdout,"IND_OLDGEN(");
153 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
154 fprintf(stdout,")\n");
158 fprintf(stdout,"CAF_BH(");
159 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
160 fprintf(stdout,")\n");
164 fprintf(stdout,"SE_BH\n");
167 case SE_CAF_BLACKHOLE:
168 fprintf(stdout,"SE_CAF_BH\n");
172 fprintf(stdout,"BH\n");
176 fprintf(stdout,"BQ(");
177 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
178 fprintf(stdout,")\n");
182 fprintf(stdout,"TSO(");
183 fprintf(stdout,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
184 fprintf(stdout,")\n");
189 fprintf(stdout,"BLOCKED_FETCH(");
190 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
191 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
192 fprintf(stdout,")\n");
196 fprintf(stdout,"FETCH_ME(");
197 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
198 fprintf(stdout,")\n");
203 fprintf(stdout,"REMOTE_REF(");
204 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
205 fprintf(stdout,")\n");
210 fprintf(stdout,"FETCH_ME_BQ(");
211 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
212 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
213 fprintf(stdout,")\n");
216 #if defined(GRAN) || defined(PAR)
218 fprintf(stdout,"RBH(");
219 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
220 fprintf(stdout,")\n");
226 case CONSTR_1_0: case CONSTR_0_1:
227 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
229 case CONSTR_CHARLIKE:
231 case CONSTR_NOCAF_STATIC:
233 /* We can't use printStdObject because we want to print the
238 fprintf(stdout,"%s(", info->prof.closure_desc);
239 fprintf(stdout,"%s", obj->header.prof.ccs->cc->label);
241 fprintf(stdout,"CONSTR(");
242 printPtr((StgPtr)obj->header.info);
243 fprintf(stdout,"(tag=%d)",info->srt_len);
245 for (i = 0; i < info->layout.payload.ptrs; ++i) {
246 fprintf(stdout,", ");
247 printPtr((StgPtr)obj->payload[i]);
249 for (j = 0; j < info->layout.payload.nptrs; ++j) {
250 fprintf(stdout,", %p#", obj->payload[i+j]);
252 fprintf(stdout,")\n");
257 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
258 case MUT_ARR_PTRS_FROZEN:
261 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
263 fprintf(stdout,"Row<%i>(",p->ptrs);
264 for (i = 0; i < p->ptrs; ++i) {
265 if (i > 0) fprintf(stdout,", ");
266 printPtr((StgPtr)(p->payload[i]));
268 fprintf(stdout,")\n");
274 case FUN_1_0: case FUN_0_1:
275 case FUN_1_1: case FUN_0_2: case FUN_2_0:
277 printStdObject(obj,"FUN");
281 case THUNK_1_0: case THUNK_0_1:
282 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
284 /* ToDo: will this work for THUNK_STATIC too? */
286 printStdObject(obj,info->prof.closure_desc);
288 printStdObject(obj,"THUNK");
293 printStdObject(obj,"THUNK_SELECTOR");
299 fprintf(stdout,"ARR_WORDS(\"");
300 /* ToDo: we can't safely assume that this is a string!
301 for (i = 0; arrWordsGetChar(obj,i); ++i) {
302 putchar(arrWordsGetChar(obj,i));
304 for (i=0; i<((StgArrWords *)obj)->words; i++)
305 fprintf(stderr, "%ld", ((StgArrWords *)obj)->payload[i]);
306 fprintf(stderr,"\")\n");
312 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
313 fprintf(stdout,"UpdateFrame(");
314 printPtr((StgPtr)GET_INFO(u));
316 printPtr((StgPtr)u->updatee);
318 printPtr((StgPtr)u->link);
319 fprintf(stdout,")\n");
325 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
326 fprintf(stdout,"CatchFrame(");
327 printPtr((StgPtr)GET_INFO(u));
329 printPtr((StgPtr)u->handler);
331 printPtr((StgPtr)u->link);
332 fprintf(stdout,")\n");
338 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
339 fprintf(stdout,"SeqFrame(");
340 printPtr((StgPtr)GET_INFO(u));
342 printPtr((StgPtr)u->link);
343 fprintf(stdout,")\n");
349 StgStopFrame* u = stgCast(StgStopFrame*,obj);
350 fprintf(stdout,"StopFrame(");
351 printPtr((StgPtr)GET_INFO(u));
352 fprintf(stdout,")\n");
356 //barf("printClosure %d",get_itbl(obj)->type);
357 fprintf(stdout, "*** printClosure: unknown type %d ****\n",
358 get_itbl(obj)->type );
359 barf("printClosure %d",get_itbl(obj)->type);
365 void printGraph( StgClosure *obj )
371 StgPtr printStackObj( StgPtr sp )
373 /*fprintf(stdout,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
375 if (IS_ARG_TAG(*sp)) {
378 fprintf(stdout,"Tagged{");
379 for (i = 0; i < tag; i++) {
380 fprintf(stdout,"0x%x#", (unsigned)(*sp++));
381 if (i < tag-1) fprintf(stdout, ", ");
383 fprintf(stdout, "}\n");
385 StgClosure* c = (StgClosure*)(*sp);
386 printPtr((StgPtr)*sp);
387 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
388 fprintf(stdout, "\t\t\tstg_ctoi_ret_R1p_info\n" );
390 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
391 fprintf(stdout, "\t\t\tstg_ctoi_ret_R1n_info\n" );
393 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
394 fprintf(stdout, "\t\t\tstg_ctoi_ret_F1_info\n" );
396 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
397 fprintf(stdout, "\t\t\tstg_ctoi_ret_D1_info\n" );
399 if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
400 fprintf(stdout, "\t\t\tstg_ctoi_ret_V_info\n" );
402 if (get_itbl(c)->type == BCO) {
403 fprintf(stdout, "\t\t\t");
404 fprintf(stdout, "BCO(...)\n");
407 fprintf(stdout, "\t\t\t");
408 printClosure ( (StgClosure*)(*sp));
416 void printStackChunk( StgPtr sp, StgPtr spBottom )
419 const StgInfoTable *info;
421 ASSERT(sp <= spBottom);
422 while (sp < spBottom) {
423 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
424 info = get_itbl((StgClosure *)sp);
425 switch (info->type) {
428 printObj( stgCast(StgClosure*,sp) );
429 sp += sizeofW(StgUpdateFrame);
433 printObj( stgCast(StgClosure*,sp) );
434 sp += sizeofW(StgSeqFrame);
438 printObj( stgCast(StgClosure*,sp) );
439 sp += sizeofW(StgCatchFrame);
443 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
444 printObj( stgCast(StgClosure*,sp) );
448 fprintf(stdout, "RET_DYN (%p)\n", sp);
451 fprintf(stdout, "Bitmap: 0x%x\n", bitmap);
456 fprintf(stdout, "RET_SMALL (%p)\n", sp);
457 bitmap = info->layout.bitmap;
460 while (bitmap != 0) {
461 fprintf(stderr," stk[%ld] (%p) = ", spBottom-sp, sp);
462 if ((bitmap & 1) == 0) {
464 fprintf(stdout,"\n");
466 fprintf(stderr,"Word# %ld\n", *sp);
469 bitmap = bitmap >> 1;
481 fprintf(stderr,"Stack[%ld] (%p) = ", spBottom-sp, sp);
482 sp = printStackObj(sp);
486 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
488 /* check everything down to the first update frame */
489 printStackChunk( sp, stgCast(StgPtr,su) );
490 while ( stgCast(StgPtr,su) < spBottom) {
491 sp = stgCast(StgPtr,su);
492 switch (get_itbl(su)->type) {
494 printObj( stgCast(StgClosure*,su) );
495 sp += sizeofW(StgUpdateFrame);
499 printObj( stgCast(StgClosure*,su) );
500 sp += sizeofW(StgSeqFrame);
501 su = stgCast(StgSeqFrame*,su)->link;
504 printObj( stgCast(StgClosure*,su) );
505 sp += sizeofW(StgCatchFrame);
506 su = stgCast(StgCatchFrame*,su)->link;
509 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
510 printObj( stgCast(StgClosure*,su) );
513 barf("printStack: weird record found on update frame list.");
515 printStackChunk( sp, stgCast(StgPtr,su) );
517 ASSERT(stgCast(StgPtr,su) == spBottom);
520 void printTSO( StgTSO *tso )
522 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
523 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
526 /* -----------------------------------------------------------------------------
529 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
530 -------------------------------------------------------------------------- */
532 static char *closure_type_names[] = {
533 "INVALID_OBJECT", /* 0 */
535 "CONSTR_1_0", /* 2 */
536 "CONSTR_0_1", /* 3 */
537 "CONSTR_2_0", /* 4 */
538 "CONSTR_1_1", /* 5 */
539 "CONSTR_0_2", /* 6 */
540 "CONSTR_INTLIKE", /* 7 */
541 "CONSTR_CHARLIKE", /* 8 */
542 "CONSTR_STATIC", /* 9 */
543 "CONSTR_NOCAF_STATIC", /* 10 */
550 "FUN_STATIC", /* 17 */
552 "THUNK_1_0", /* 19 */
553 "THUNK_0_1", /* 20 */
554 "THUNK_2_0", /* 21 */
555 "THUNK_1_1", /* 22 */
556 "THUNK_0_2", /* 23 */
557 "THUNK_STATIC", /* 24 */
558 "THUNK_SELECTOR", /* 25 */
563 "IND_OLDGEN", /* 30 */
565 "IND_OLDGEN_PERM", /* 32 */
566 "IND_STATIC", /* 33 */
567 "CAF_BLACKHOLE", /* 36 */
569 "RET_SMALL", /* 38 */
570 "RET_VEC_SMALL", /* 39 */
572 "RET_VEC_BIG", /* 41 */
574 "UPDATE_FRAME", /* 43 */
575 "CATCH_FRAME", /* 44 */
576 "STOP_FRAME", /* 45 */
577 "SEQ_FRAME", /* 46 */
578 "BLACKHOLE", /* 47 */
579 "BLACKHOLE_BQ", /* 48 */
580 "SE_BLACKHOLE", /* 49 */
581 "SE_CAF_BLACKHOLE", /* 50 */
583 "ARR_WORDS", /* 52 */
584 "MUT_ARR_PTRS", /* 53 */
585 "MUT_ARR_PTRS_FROZEN", /* 54 */
589 "STABLE_NAME", /* 58 */
591 "BLOCKED_FETCH", /* 60 */
593 "FETCH_ME_BQ", /* 62 */
595 "EVACUATED", /* 64 */
596 "REMOTE_REF", /* 65 */
597 "N_CLOSURE_TYPES" /* 66 */
601 info_type(StgClosure *closure){
602 return closure_type_names[get_itbl(closure)->type];
606 info_type_by_ip(StgInfoTable *ip){
607 return closure_type_names[ip->type];
611 info_hdr_type(StgClosure *closure, char *res){
612 strcpy(res,closure_type_names[get_itbl(closure)->type]);
615 /* --------------------------------------------------------------------------
616 * Address printing code
618 * Uses symbol table in (unstripped executable)
619 * ------------------------------------------------------------------------*/
621 /* --------------------------------------------------------------------------
622 * Simple lookup table
624 * Current implementation is pretty dumb!
625 * ------------------------------------------------------------------------*/
632 static nat max_table_size;
633 static nat table_size;
634 static struct entry* table;
636 static void reset_table( int size )
638 max_table_size = size;
640 table = (struct entry *) malloc(size * sizeof(struct entry));
643 static void prepare_table( void )
645 /* Could sort it... */
648 static void insert( unsigned value, const char *name )
650 if ( table_size >= max_table_size ) {
651 barf( "Symbol table overflow\n" );
653 table[table_size].value = value;
654 table[table_size].name = name;
655 table_size = table_size + 1;
660 static rtsBool lookup_name( char *name, unsigned *result )
663 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
665 if (i < table_size) {
666 *result = table[i].value;
674 /* Code from somewhere inside GHC (circa 1994)
676 * "std"++xs -> "Zstd"++xs
677 * char_to_c 'Z' = "ZZ"
678 * char_to_c '&' = "Za"
679 * char_to_c '|' = "Zb"
680 * char_to_c ':' = "Zc"
681 * char_to_c '/' = "Zd"
682 * char_to_c '=' = "Ze"
683 * char_to_c '>' = "Zg"
684 * char_to_c '#' = "Zh"
685 * char_to_c '<' = "Zl"
686 * char_to_c '-' = "Zm"
687 * char_to_c '!' = "Zn"
688 * char_to_c '.' = "Zo"
689 * char_to_c '+' = "Zp"
690 * char_to_c '\'' = "Zq"
691 * char_to_c '*' = "Zt"
692 * char_to_c '_' = "Zu"
693 * char_to_c c = "Z" ++ show (ord c)
695 static char unZcode( char ch )
698 case 'a' : return ('&');
699 case 'b' : return ('|');
700 case 'c' : return (':');
701 case 'd' : return ('/');
702 case 'e' : return ('=');
703 case 'g' : return ('>');
704 case 'h' : return ('#');
705 case 'l' : return ('<');
706 case 'm' : return ('-');
707 case 'n' : return ('!');
708 case 'o' : return ('.');
709 case 'p' : return ('+');
710 case 'q' : return ('\'');
711 case 't' : return ('*');
712 case 'u' : return ('_');
714 case '\0' : return ('Z');
715 default : return (ch);
720 /* Precondition: out big enough to handle output (about twice length of in) */
721 static void enZcode( char *in, char *out )
727 for( i = 0; in[i] != '\0'; ++i ) {
802 const char *lookupGHCName( void *addr )
805 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
807 if (i < table_size) {
808 return table[i].name;
814 static void printZcoded( const char *raw )
818 while ( raw[j] != '\0' ) {
820 fputc(unZcode(raw[j+1]),stdout);
823 fputc(raw[j],stdout);
829 /* --------------------------------------------------------------------------
830 * Symbol table loading
831 * ------------------------------------------------------------------------*/
833 /* Causing linking trouble on Win32 plats, so I'm
834 disabling this for now.
840 /* Fairly ad-hoc piece of code that seems to filter out a lot of
841 * rubbish like the obj-splitting symbols
844 static rtsBool isReal( flagword flags, const char *name )
847 /* ToDo: make this work on BFD */
848 int tp = type & N_TYPE;
849 if (tp == N_TEXT || tp == N_DATA) {
850 return (name[0] == '_' && name[1] != '_');
855 (void)flags; /* keep gcc -Wall happy */
857 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
858 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
865 extern void DEBUG_LoadSymbols( char *name )
871 abfd = bfd_openr(name, "default");
873 barf("can't open executable %s to get symbol table", name);
875 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
881 asymbol **symbol_table;
882 long number_of_symbols;
883 long num_real_syms = 0;
886 storage_needed = bfd_get_symtab_upper_bound (abfd);
888 if (storage_needed < 0) {
889 barf("can't read symbol table");
892 if (storage_needed == 0) {
893 belch("no storage needed");
896 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
898 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
900 if (number_of_symbols < 0) {
901 barf("can't canonicalise symbol table");
904 for( i = 0; i != number_of_symbols; ++i ) {
906 bfd_get_symbol_info(abfd,symbol_table[i],&info);
907 /*fprintf(stdout,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
908 if (isReal(info.type, info.name)) {
914 fprintf(stdout,"Loaded %ld symbols. Of which %ld are real symbols\n",
915 number_of_symbols, num_real_syms)
918 reset_table( num_real_syms );
920 for( i = 0; i != number_of_symbols; ++i ) {
922 bfd_get_symbol_info(abfd,symbol_table[i],&info);
923 if (isReal(info.type, info.name)) {
924 insert( info.value, info.name );
933 #else /* HAVE_BFD_H */
935 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
940 #endif /* HAVE_BFD_H */
942 #include "StoragePriv.h"
944 void findPtr(P_ p, int); /* keep gcc -Wall happy */
947 findPtr(P_ p, int follow)
952 const int arr_size = 1024;
953 StgPtr arr[arr_size];
956 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
957 for (s = 0; s < generations[g].n_steps; s++) {
958 if (RtsFlags.GcFlags.generations == 1) {
959 bd = generations[g].steps[s].to_blocks;
961 bd = generations[g].steps[s].blocks;
963 for (; bd; bd = bd->link) {
964 for (q = bd->start; q < bd->free; q++) {
968 while (!LOOKS_LIKE_GHC_INFO(*r)) { r--; };
969 fprintf(stdout, "%p = ", r);
970 printClosure((StgClosure *)r);
980 if (follow && i == 1) {
981 fprintf(stdout, "-->\n");
987 void printPtr( StgPtr p )
989 fprintf(stdout, "ptr 0x%p (enable -DDEBUG for more info) " , p );
992 void printObj( StgClosure *obj )
994 fprintf(stdout, "obj 0x%p (enable -DDEBUG for more info) " , obj );