1 /* -----------------------------------------------------------------------------
2 * $Id: Printer.c,v 1.53 2002/07/24 18:18:13 sof Exp $
4 * (c) The GHC Team, 1994-2000.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
22 #include "Bytecodes.h" /* for InstrPtr */
23 #include "Disassembler.h"
28 #if defined(GRAN) || defined(PAR)
29 // HWL: explicit fixed header size to make debugging easier
30 int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable),
31 uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame);
34 /* --------------------------------------------------------------------------
35 * local function decls
36 * ------------------------------------------------------------------------*/
38 static void printStdObject( StgClosure *obj, char* tag );
39 static void reset_table ( int size );
40 static void prepare_table ( void );
41 static void insert ( unsigned value, const char *name );
42 #if 0 /* unused but might be useful sometime */
43 static rtsBool lookup_name ( char *name, unsigned *result );
44 static void enZcode ( char *in, char *out );
46 static char unZcode ( char ch );
47 const char * lookupGHCName ( void *addr );
48 static void printZcoded ( const char *raw );
50 /* --------------------------------------------------------------------------
52 * ------------------------------------------------------------------------*/
54 void printPtr( StgPtr p )
57 raw = lookupGHCName(p);
61 fprintf(stdout, "%p", p);
65 void printObj( StgClosure *obj )
67 fprintf(stdout,"Object "); printPtr((StgPtr)obj); fprintf(stdout," = ");
72 printStdObjHdr( StgClosure *obj, char* tag )
74 fprintf(stdout,"%s(",tag);
75 printPtr((StgPtr)obj->header.info);
77 fprintf(stdout,", %s", obj->header.prof.ccs->cc->label);
82 printStdObject( StgClosure *obj, char* tag )
85 const StgInfoTable* info;
87 printStdObjHdr( obj, tag );
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(stdout,", %pd#",obj->payload[i+j]);
97 fprintf(stdout,")\n");
101 printClosure( StgClosure *obj )
105 info = get_itbl(obj);
107 switch ( info->type ) {
109 barf("Invalid object");
111 disassemble( (StgBCO*)obj );
116 StgMutVar* mv = (StgMutVar*)obj;
117 fprintf(stdout,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
123 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
125 fprintf(stdout,"AP_UPD("); printPtr((StgPtr)ap->fun);
126 for (i = 0; i < ap->n_args; ++i) {
127 fprintf(stdout,", ");
128 printPtr((P_)ap->payload[i]);
130 fprintf(stdout,")\n");
136 StgPAP* pap = stgCast(StgPAP*,obj);
138 fprintf(stdout,"PAP("); printPtr((StgPtr)pap->fun);
139 for (i = 0; i < pap->n_args; ++i) {
140 fprintf(stdout,", ");
141 printPtr((StgPtr)pap->payload[i]);
143 fprintf(stdout,")\n");
148 fprintf(stdout,"FOREIGN(");
149 printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
150 fprintf(stdout,")\n");
154 fprintf(stdout,"IND(");
155 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
156 fprintf(stdout,")\n");
160 fprintf(stdout,"IND_STATIC(");
161 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
162 fprintf(stdout,")\n");
166 fprintf(stdout,"IND_OLDGEN(");
167 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
168 fprintf(stdout,")\n");
172 fprintf(stdout,"CAF_BH(");
173 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
174 fprintf(stdout,")\n");
178 fprintf(stdout,"SE_BH\n");
181 case SE_CAF_BLACKHOLE:
182 fprintf(stdout,"SE_CAF_BH\n");
186 fprintf(stdout,"BH\n");
190 fprintf(stdout,"BQ(");
191 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
192 fprintf(stdout,")\n");
196 fprintf(stdout,"TSO(");
197 fprintf(stdout,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
198 fprintf(stdout,")\n");
203 fprintf(stdout,"BLOCKED_FETCH(");
204 printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
205 printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
206 fprintf(stdout,")\n");
210 fprintf(stdout,"FETCH_ME(");
211 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
212 fprintf(stdout,")\n");
217 fprintf(stdout,"REMOTE_REF(");
218 printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
219 fprintf(stdout,")\n");
224 fprintf(stdout,"FETCH_ME_BQ(");
225 // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
226 printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
227 fprintf(stdout,")\n");
230 #if defined(GRAN) || defined(PAR)
232 fprintf(stdout,"RBH(");
233 printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
234 fprintf(stdout,")\n");
240 case CONSTR_1_0: case CONSTR_0_1:
241 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
243 case CONSTR_CHARLIKE:
245 case CONSTR_NOCAF_STATIC:
247 /* We can't use printStdObject because we want to print the
252 fprintf(stdout,"%s(", info->prof.closure_desc);
253 fprintf(stdout,"%s", obj->header.prof.ccs->cc->label);
255 fprintf(stdout,"CONSTR(");
256 printPtr((StgPtr)obj->header.info);
257 fprintf(stdout,"(tag=%d)",info->srt_len);
259 for (i = 0; i < info->layout.payload.ptrs; ++i) {
260 fprintf(stdout,", ");
261 printPtr((StgPtr)obj->payload[i]);
263 for (j = 0; j < info->layout.payload.nptrs; ++j) {
264 fprintf(stdout,", %p#", obj->payload[i+j]);
266 fprintf(stdout,")\n");
271 /* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
272 case MUT_ARR_PTRS_FROZEN:
275 StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
277 fprintf(stdout,"Row<%i>(",p->ptrs);
278 for (i = 0; i < p->ptrs; ++i) {
279 if (i > 0) fprintf(stdout,", ");
280 printPtr((StgPtr)(p->payload[i]));
282 fprintf(stdout,")\n");
288 case FUN_1_0: case FUN_0_1:
289 case FUN_1_1: case FUN_0_2: case FUN_2_0:
291 printStdObject(obj,"FUN");
295 case THUNK_1_0: case THUNK_0_1:
296 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
298 /* ToDo: will this work for THUNK_STATIC too? */
300 printStdObject(obj,info->prof.closure_desc);
302 printStdObject(obj,"THUNK");
307 printStdObjHdr(obj, "THUNK_SELECTOR");
308 fprintf(stdout, ", %p)\n", ((StgSelector *)obj)->selectee);
314 fprintf(stdout,"ARR_WORDS(\"");
315 /* ToDo: we can't safely assume that this is a string!
316 for (i = 0; arrWordsGetChar(obj,i); ++i) {
317 putchar(arrWordsGetChar(obj,i));
319 for (i=0; i<((StgArrWords *)obj)->words; i++)
320 fprintf(stdout, "%u", ((StgArrWords *)obj)->payload[i]);
321 fprintf(stdout,"\")\n");
327 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
328 fprintf(stdout,"UpdateFrame(");
329 printPtr((StgPtr)GET_INFO(u));
331 printPtr((StgPtr)u->updatee);
333 printPtr((StgPtr)u->link);
334 fprintf(stdout,")\n");
340 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
341 fprintf(stdout,"CatchFrame(");
342 printPtr((StgPtr)GET_INFO(u));
344 printPtr((StgPtr)u->handler);
346 printPtr((StgPtr)u->link);
347 fprintf(stdout,")\n");
353 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
354 fprintf(stdout,"SeqFrame(");
355 printPtr((StgPtr)GET_INFO(u));
357 printPtr((StgPtr)u->link);
358 fprintf(stdout,")\n");
364 StgStopFrame* u = stgCast(StgStopFrame*,obj);
365 fprintf(stdout,"StopFrame(");
366 printPtr((StgPtr)GET_INFO(u));
367 fprintf(stdout,")\n");
371 //barf("printClosure %d",get_itbl(obj)->type);
372 fprintf(stdout, "*** printClosure: unknown type %d ****\n",
373 get_itbl(obj)->type );
374 barf("printClosure %d",get_itbl(obj)->type);
380 void printGraph( StgClosure *obj )
386 StgPtr printStackObj( StgPtr sp )
388 /*fprintf(stdout,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
390 if (IS_ARG_TAG(*sp)) {
393 fprintf(stdout,"Tagged{");
394 for (i = 0; i < tag; i++) {
395 fprintf(stdout,"0x%x#", (unsigned)(*sp++));
396 if (i < tag-1) fprintf(stdout, ", ");
398 fprintf(stdout, "}\n");
400 StgClosure* c = (StgClosure*)(*sp);
401 printPtr((StgPtr)*sp);
402 if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
403 fprintf(stdout, "\t\t\tstg_ctoi_ret_R1p_info\n" );
405 if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
406 fprintf(stdout, "\t\t\tstg_ctoi_ret_R1n_info\n" );
408 if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
409 fprintf(stdout, "\t\t\tstg_ctoi_ret_F1_info\n" );
411 if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
412 fprintf(stdout, "\t\t\tstg_ctoi_ret_D1_info\n" );
414 if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
415 fprintf(stdout, "\t\t\tstg_ctoi_ret_V_info\n" );
417 if (get_itbl(c)->type == BCO) {
418 fprintf(stdout, "\t\t\t");
419 fprintf(stdout, "BCO(...)\n");
422 fprintf(stdout, "\t\t\t");
423 printClosure ( (StgClosure*)(*sp));
431 void printStackChunk( StgPtr sp, StgPtr spBottom )
434 const StgInfoTable *info;
436 ASSERT(sp <= spBottom);
437 while (sp < spBottom) {
438 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
439 info = get_itbl((StgClosure *)sp);
440 switch (info->type) {
443 printObj( stgCast(StgClosure*,sp) );
444 sp += sizeofW(StgUpdateFrame);
448 printObj( stgCast(StgClosure*,sp) );
449 sp += sizeofW(StgSeqFrame);
453 printObj( stgCast(StgClosure*,sp) );
454 sp += sizeofW(StgCatchFrame);
458 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
459 printObj( stgCast(StgClosure*,sp) );
463 fprintf(stdout, "RET_DYN (%p)\n", sp);
466 fprintf(stdout, "Bitmap: 0x%x\n", bitmap);
471 fprintf(stdout, "RET_SMALL (%p)\n", sp);
472 bitmap = info->layout.bitmap;
475 while (bitmap != 0) {
476 fprintf(stdout," stk[%ld] (%p) = ", spBottom-sp, sp);
477 if ((bitmap & 1) == 0) {
479 fprintf(stdout,"\n");
481 fprintf(stdout,"Word# %ld\n", *sp);
484 bitmap = bitmap >> 1;
496 fprintf(stdout,"Stack[%ld] (%p) = ", spBottom-sp, sp);
497 sp = printStackObj(sp);
501 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
503 /* check everything down to the first update frame */
504 printStackChunk( sp, stgCast(StgPtr,su) );
505 while ( stgCast(StgPtr,su) < spBottom) {
506 sp = stgCast(StgPtr,su);
507 switch (get_itbl(su)->type) {
509 printObj( stgCast(StgClosure*,su) );
510 sp += sizeofW(StgUpdateFrame);
514 printObj( stgCast(StgClosure*,su) );
515 sp += sizeofW(StgSeqFrame);
516 su = stgCast(StgSeqFrame*,su)->link;
519 printObj( stgCast(StgClosure*,su) );
520 sp += sizeofW(StgCatchFrame);
521 su = stgCast(StgCatchFrame*,su)->link;
524 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
525 printObj( stgCast(StgClosure*,su) );
528 barf("printStack: weird record found on update frame list.");
530 printStackChunk( sp, stgCast(StgPtr,su) );
532 ASSERT(stgCast(StgPtr,su) == spBottom);
535 void printTSO( StgTSO *tso )
537 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
538 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
541 /* -----------------------------------------------------------------------------
544 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
545 -------------------------------------------------------------------------- */
547 static char *closure_type_names[] = {
548 "INVALID_OBJECT", /* 0 */
550 "CONSTR_1_0", /* 2 */
551 "CONSTR_0_1", /* 3 */
552 "CONSTR_2_0", /* 4 */
553 "CONSTR_1_1", /* 5 */
554 "CONSTR_0_2", /* 6 */
555 "CONSTR_INTLIKE", /* 7 */
556 "CONSTR_CHARLIKE", /* 8 */
557 "CONSTR_STATIC", /* 9 */
558 "CONSTR_NOCAF_STATIC", /* 10 */
565 "FUN_STATIC", /* 17 */
567 "THUNK_1_0", /* 19 */
568 "THUNK_0_1", /* 20 */
569 "THUNK_2_0", /* 21 */
570 "THUNK_1_1", /* 22 */
571 "THUNK_0_2", /* 23 */
572 "THUNK_STATIC", /* 24 */
573 "THUNK_SELECTOR", /* 25 */
578 "IND_OLDGEN", /* 30 */
580 "IND_OLDGEN_PERM", /* 32 */
581 "IND_STATIC", /* 33 */
582 "CAF_BLACKHOLE", /* 36 */
584 "RET_SMALL", /* 38 */
585 "RET_VEC_SMALL", /* 39 */
587 "RET_VEC_BIG", /* 41 */
589 "UPDATE_FRAME", /* 43 */
590 "CATCH_FRAME", /* 44 */
591 "STOP_FRAME", /* 45 */
592 "SEQ_FRAME", /* 46 */
593 "BLACKHOLE", /* 47 */
594 "BLACKHOLE_BQ", /* 48 */
595 "SE_BLACKHOLE", /* 49 */
596 "SE_CAF_BLACKHOLE", /* 50 */
598 "ARR_WORDS", /* 52 */
599 "MUT_ARR_PTRS", /* 53 */
600 "MUT_ARR_PTRS_FROZEN", /* 54 */
604 "STABLE_NAME", /* 58 */
606 "BLOCKED_FETCH", /* 60 */
608 "FETCH_ME_BQ", /* 62 */
610 "EVACUATED", /* 64 */
611 "REMOTE_REF", /* 65 */
612 "N_CLOSURE_TYPES" /* 66 */
616 info_type(StgClosure *closure){
617 return closure_type_names[get_itbl(closure)->type];
621 info_type_by_ip(StgInfoTable *ip){
622 return closure_type_names[ip->type];
626 info_hdr_type(StgClosure *closure, char *res){
627 strcpy(res,closure_type_names[get_itbl(closure)->type]);
630 /* --------------------------------------------------------------------------
631 * Address printing code
633 * Uses symbol table in (unstripped executable)
634 * ------------------------------------------------------------------------*/
636 /* --------------------------------------------------------------------------
637 * Simple lookup table
639 * Current implementation is pretty dumb!
640 * ------------------------------------------------------------------------*/
647 static nat max_table_size;
648 static nat table_size;
649 static struct entry* table;
651 static void reset_table( int size )
653 max_table_size = size;
655 table = (struct entry *) malloc(size * sizeof(struct entry));
658 static void prepare_table( void )
660 /* Could sort it... */
663 static void insert( unsigned value, const char *name )
665 if ( table_size >= max_table_size ) {
666 barf( "Symbol table overflow\n" );
668 table[table_size].value = value;
669 table[table_size].name = name;
670 table_size = table_size + 1;
675 static rtsBool lookup_name( char *name, unsigned *result )
678 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
680 if (i < table_size) {
681 *result = table[i].value;
689 /* Code from somewhere inside GHC (circa 1994)
691 * "std"++xs -> "Zstd"++xs
692 * char_to_c 'Z' = "ZZ"
693 * char_to_c '&' = "Za"
694 * char_to_c '|' = "Zb"
695 * char_to_c ':' = "Zc"
696 * char_to_c '/' = "Zd"
697 * char_to_c '=' = "Ze"
698 * char_to_c '>' = "Zg"
699 * char_to_c '#' = "Zh"
700 * char_to_c '<' = "Zl"
701 * char_to_c '-' = "Zm"
702 * char_to_c '!' = "Zn"
703 * char_to_c '.' = "Zo"
704 * char_to_c '+' = "Zp"
705 * char_to_c '\'' = "Zq"
706 * char_to_c '*' = "Zt"
707 * char_to_c '_' = "Zu"
708 * char_to_c c = "Z" ++ show (ord c)
710 static char unZcode( char ch )
713 case 'a' : return ('&');
714 case 'b' : return ('|');
715 case 'c' : return (':');
716 case 'd' : return ('/');
717 case 'e' : return ('=');
718 case 'g' : return ('>');
719 case 'h' : return ('#');
720 case 'l' : return ('<');
721 case 'm' : return ('-');
722 case 'n' : return ('!');
723 case 'o' : return ('.');
724 case 'p' : return ('+');
725 case 'q' : return ('\'');
726 case 't' : return ('*');
727 case 'u' : return ('_');
729 case '\0' : return ('Z');
730 default : return (ch);
735 /* Precondition: out big enough to handle output (about twice length of in) */
736 static void enZcode( char *in, char *out )
742 for( i = 0; in[i] != '\0'; ++i ) {
817 const char *lookupGHCName( void *addr )
820 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
822 if (i < table_size) {
823 return table[i].name;
829 static void printZcoded( const char *raw )
833 while ( raw[j] != '\0' ) {
835 fputc(unZcode(raw[j+1]),stdout);
838 fputc(raw[j],stdout);
844 /* --------------------------------------------------------------------------
845 * Symbol table loading
846 * ------------------------------------------------------------------------*/
848 /* Causing linking trouble on Win32 plats, so I'm
849 disabling this for now.
855 /* Fairly ad-hoc piece of code that seems to filter out a lot of
856 * rubbish like the obj-splitting symbols
859 static rtsBool isReal( flagword flags, const char *name )
862 /* ToDo: make this work on BFD */
863 int tp = type & N_TYPE;
864 if (tp == N_TEXT || tp == N_DATA) {
865 return (name[0] == '_' && name[1] != '_');
870 (void)flags; /* keep gcc -Wall happy */
872 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
873 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
880 extern void DEBUG_LoadSymbols( char *name )
886 abfd = bfd_openr(name, "default");
888 barf("can't open executable %s to get symbol table", name);
890 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
896 asymbol **symbol_table;
897 long number_of_symbols;
898 long num_real_syms = 0;
901 storage_needed = bfd_get_symtab_upper_bound (abfd);
903 if (storage_needed < 0) {
904 barf("can't read symbol table");
907 if (storage_needed == 0) {
908 belch("no storage needed");
911 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
913 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
915 if (number_of_symbols < 0) {
916 barf("can't canonicalise symbol table");
919 for( i = 0; i != number_of_symbols; ++i ) {
921 bfd_get_symbol_info(abfd,symbol_table[i],&info);
922 /*fprintf(stdout,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
923 if (isReal(info.type, info.name)) {
929 fprintf(stdout,"Loaded %ld symbols. Of which %ld are real symbols\n",
930 number_of_symbols, num_real_syms)
933 reset_table( num_real_syms );
935 for( i = 0; i != number_of_symbols; ++i ) {
937 bfd_get_symbol_info(abfd,symbol_table[i],&info);
938 if (isReal(info.type, info.name)) {
939 insert( info.value, info.name );
948 #else /* HAVE_BFD_H */
950 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
955 #endif /* HAVE_BFD_H */
957 #include "StoragePriv.h"
959 void findPtr(P_ p, int); /* keep gcc -Wall happy */
962 findPtr(P_ p, int follow)
967 const int arr_size = 1024;
968 StgPtr arr[arr_size];
971 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
972 for (s = 0; s < generations[g].n_steps; s++) {
973 if (RtsFlags.GcFlags.generations == 1) {
974 bd = generations[g].steps[s].to_blocks;
976 bd = generations[g].steps[s].blocks;
978 for (; bd; bd = bd->link) {
979 for (q = bd->start; q < bd->free; q++) {
983 while (!LOOKS_LIKE_GHC_INFO(*r) || (P_)*r == NULL) {
986 fprintf(stdout, "%p = ", r);
987 printClosure((StgClosure *)r);
997 if (follow && i == 1) {
998 fprintf(stdout, "-->\n");
1004 void printPtr( StgPtr p )
1006 fprintf(stdout, "ptr 0x%p (enable -DDEBUG for more info) " , p );
1009 void printObj( StgClosure *obj )
1011 fprintf(stdout, "obj 0x%p (enable -DDEBUG for more info) " , obj );