2 /* -----------------------------------------------------------------------------
3 * $Id: Printer.c,v 1.13 1999/05/11 16:47:54 keithw Exp $
5 * Copyright (c) 1994-1999.
9 * ---------------------------------------------------------------------------*/
17 #include "Bytecodes.h" /* for InstrPtr */
18 #include "Disassembler.h"
22 /* --------------------------------------------------------------------------
23 * local function decls
24 * ------------------------------------------------------------------------*/
26 static void printStdObject( StgClosure *obj, char* tag );
27 static void reset_table ( int size );
28 static void prepare_table ( void );
29 static void insert ( unsigned value, const char *name );
30 #if 0 /* unused but might be useful sometime */
31 static rtsBool lookup_name ( char *name, unsigned *result );
32 static void enZcode ( char *in, char *out );
34 static char unZcode ( char ch );
35 rtsBool lookupGHCName ( StgPtr addr, const char **result );
36 static void printZcoded ( const char *raw );
38 /* --------------------------------------------------------------------------
40 * ------------------------------------------------------------------------*/
44 extern void* itblNames[];
45 extern int nItblNames;
46 char* lookupHugsItblName ( void* v )
49 for (i = 0; i < nItblNames; i += 2)
50 if (itblNames[i] == v) return itblNames[i+1];
55 void printPtr( StgPtr p )
59 if (lookupGHCName( p, &raw )) {
62 } else if ((raw = lookupHugsName(p)) != 0) {
63 fprintf(stderr, "%s", raw);
64 } else if ((str = lookupHugsItblName(p)) != 0) {
65 fprintf(stderr, "%p=%s", p, str);
68 fprintf(stderr, "%p", p);
72 void printObj( StgClosure *obj )
74 fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
78 static void printStdObject( StgClosure *obj, char* tag )
81 const StgInfoTable* info = get_itbl(obj);
82 fprintf(stderr,"%s(",tag);
83 printPtr((StgPtr)obj->header.info);
84 for (i = 0; i < info->layout.payload.ptrs; ++i) {
86 printPtr(payloadPtr(obj,i));
88 for (j = 0; j < info->layout.payload.nptrs; ++j) {
89 fprintf(stderr,", %xd#",payloadWord(obj,i+j));
91 fprintf(stderr,")\n");
94 void printClosure( StgClosure *obj )
96 switch ( get_itbl(obj)->type ) {
98 barf("Invalid object");
101 fprintf(stderr,"BCO\n");
102 disassemble(stgCast(StgBCO*,obj),"\t");
108 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
110 fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
111 for (i = 0; i < ap->n_args; ++i) {
112 fprintf(stderr,", ");
113 printPtr(payloadPtr(ap,i));
115 fprintf(stderr,")\n");
121 StgPAP* pap = stgCast(StgPAP*,obj);
123 fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
124 for (i = 0; i < pap->n_args; ++i) {
125 fprintf(stderr,", ");
126 printPtr(payloadPtr(pap,i));
128 fprintf(stderr,")\n");
133 fprintf(stderr,"IND(");
134 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
135 fprintf(stderr,")\n");
139 fprintf(stderr,"IND_STATIC(");
140 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
141 fprintf(stderr,")\n");
145 fprintf(stderr,"IND_OLDGEN(");
146 printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
147 fprintf(stderr,")\n");
152 StgCAF* caf = stgCast(StgCAF*,obj);
153 fprintf(stderr,"CAF_UNENTERED(");
154 printPtr((StgPtr)caf->body);
155 fprintf(stderr,", ");
156 printPtr((StgPtr)caf->value); /* should be null */
157 fprintf(stderr,", ");
158 printPtr((StgPtr)caf->link); /* should be null */
159 fprintf(stderr,")\n");
165 StgCAF* caf = stgCast(StgCAF*,obj);
166 fprintf(stderr,"CAF_ENTERED(");
167 printPtr((StgPtr)caf->body);
168 fprintf(stderr,", ");
169 printPtr((StgPtr)caf->value);
170 fprintf(stderr,", ");
171 printPtr((StgPtr)caf->link);
172 fprintf(stderr,")\n");
177 fprintf(stderr,"CAF_BH(");
178 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
179 fprintf(stderr,")\n");
183 fprintf(stderr,"SE_BH\n");
186 case SE_CAF_BLACKHOLE:
187 fprintf(stderr,"SE_CAF_BH\n");
191 fprintf(stderr,"BH\n");
195 fprintf(stderr,"BQ(");
196 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
197 fprintf(stderr,")\n");
201 case CONSTR_1_0: case CONSTR_0_1:
202 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
204 case CONSTR_CHARLIKE:
206 case CONSTR_NOCAF_STATIC:
208 /* We can't use printStdObject because we want to print the
212 const StgInfoTable* info = get_itbl(obj);
213 fprintf(stderr,"PACK(");
214 printPtr((StgPtr)obj->header.info);
215 fprintf(stderr,"(tag=%d)",info->srt_len);
216 for (i = 0; i < info->layout.payload.ptrs; ++i) {
217 fprintf(stderr,", ");
218 printPtr(payloadPtr(obj,i));
220 for (j = 0; j < info->layout.payload.nptrs; ++j) {
221 fprintf(stderr,", %x#",payloadWord(obj,i+j));
223 fprintf(stderr,")\n");
228 case FUN_1_0: case FUN_0_1:
229 case FUN_1_1: case FUN_0_2: case FUN_2_0:
231 printStdObject(obj,"FUN");
235 case THUNK_1_0: case THUNK_0_1:
236 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
238 /* ToDo: will this work for THUNK_STATIC too? */
239 printStdObject(obj,"THUNK");
245 fprintf(stderr,"ARR_WORDS(\"");
246 /* ToDo: we can't safely assume that this is a string! */
247 for (i = 0; arrWordsGetChar(obj,i); ++i) {
248 putchar(arrWordsGetChar(obj,i));
250 fprintf(stderr,"\")\n");
256 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
257 fprintf(stderr,"UpdateFrame(");
258 printPtr((StgPtr)GET_INFO(u));
260 printPtr((StgPtr)u->updatee);
262 printPtr((StgPtr)u->link);
263 fprintf(stderr,")\n");
269 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
270 fprintf(stderr,"CatchFrame(");
271 printPtr((StgPtr)GET_INFO(u));
273 printPtr((StgPtr)u->handler);
275 printPtr((StgPtr)u->link);
276 fprintf(stderr,")\n");
282 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
283 fprintf(stderr,"SeqFrame(");
284 printPtr((StgPtr)GET_INFO(u));
286 printPtr((StgPtr)u->link);
287 fprintf(stderr,")\n");
293 StgStopFrame* u = stgCast(StgStopFrame*,obj);
294 fprintf(stderr,"StopFrame(");
295 printPtr((StgPtr)GET_INFO(u));
296 fprintf(stderr,")\n");
300 //barf("printClosure %d",get_itbl(obj)->type);
301 fprintf(stderr, "*** printClosure: unknown type %d ****\n",get_itbl(obj)->type );
306 StgPtr printStackObj( StgPtr sp )
308 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
310 if (IS_ARG_TAG(*sp)) {
313 StackTag tag = (StackTag)*sp;
316 barf("printStackObj: ILLEGAL_TAG");
319 fprintf(stderr,"RealWorld#\n");
322 fprintf(stderr,"Int# %d\n", *(StgInt*)(sp+1));
325 fprintf(stderr,"Int64# %lld\n", *(StgInt64*)(sp+1));
328 fprintf(stderr,"Word# %d\n", *(StgWord*)(sp+1));
331 fprintf(stderr,"Addr# "); printPtr(*(StgAddr*)(sp+1)); fprintf(stderr,"\n");
334 fprintf(stderr,"Char# %d\n", *(StgChar*)(sp+1));
337 fprintf(stderr,"Float# %f\n", PK_FLT(sp+1));
340 fprintf(stderr,"Double# %f\n", PK_DBL(sp+1));
343 barf("printStackObj: unrecognised ARGTAG %d",tag);
345 sp += 1 + ARG_SIZE(tag);
347 #else /* !DEBUG_EXTRA */
351 fprintf(stderr,"Tag: %d words\n", tag);
352 for (i = 0; i < tag; i++) {
353 fprintf(stderr,"Word# %d\n", *sp++);
359 StgClosure* c = (StgClosure*)(*sp);
360 printPtr((StgPtr)*sp);
362 if (c == &ret_bco_info) {
363 fprintf(stderr, "\t\t");
364 fprintf(stderr, "ret_bco_info\n" );
366 if (IS_HUGS_CONSTR_INFO(GET_INFO(c))) {
367 fprintf(stderr, "\t\t\t");
368 fprintf(stderr, "ConstrInfoTable\n" );
371 if (get_itbl(c)->type == BCO) {
372 fprintf(stderr, "\t\t\t");
373 fprintf(stderr, "BCO(...)\n");
376 fprintf(stderr, "\t\t\t");
377 printClosure ( (StgClosure*)(*sp));
385 void printStackChunk( StgPtr sp, StgPtr spBottom )
388 const StgInfoTable *info;
390 ASSERT(sp <= spBottom);
391 while (sp < spBottom) {
392 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
393 info = get_itbl((StgClosure *)sp);
394 switch (info->type) {
397 printObj( stgCast(StgClosure*,sp) );
398 sp += sizeofW(StgUpdateFrame);
402 printObj( stgCast(StgClosure*,sp) );
403 sp += sizeofW(StgSeqFrame);
407 printObj( stgCast(StgClosure*,sp) );
408 sp += sizeofW(StgCatchFrame);
412 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
413 printObj( stgCast(StgClosure*,sp) );
417 fprintf(stderr, "RET_DYN (%p)\n", sp);
420 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
425 fprintf(stderr, "RET_SMALL (%p)\n", sp);
426 bitmap = info->layout.bitmap;
429 while (bitmap != 0) {
430 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
431 if ((bitmap & 1) == 0) {
433 fprintf(stderr,"\n");
435 fprintf(stderr,"Word# %d\n", *sp++);
438 bitmap = bitmap >> 1;
450 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
451 sp = printStackObj(sp);
455 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
457 /* check everything down to the first update frame */
458 printStackChunk( sp, stgCast(StgPtr,su) );
459 while ( stgCast(StgPtr,su) < spBottom) {
460 sp = stgCast(StgPtr,su);
461 switch (get_itbl(su)->type) {
463 printObj( stgCast(StgClosure*,su) );
464 sp += sizeofW(StgUpdateFrame);
468 printObj( stgCast(StgClosure*,su) );
469 sp += sizeofW(StgSeqFrame);
470 su = stgCast(StgSeqFrame*,su)->link;
473 printObj( stgCast(StgClosure*,su) );
474 sp += sizeofW(StgCatchFrame);
475 su = stgCast(StgCatchFrame*,su)->link;
478 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
479 printObj( stgCast(StgClosure*,su) );
482 barf("printStack: weird record found on update frame list.");
484 printStackChunk( sp, stgCast(StgPtr,su) );
486 ASSERT(stgCast(StgPtr,su) == spBottom);
489 void printTSO( StgTSO *tso )
491 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
492 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
496 /* --------------------------------------------------------------------------
497 * Address printing code
499 * Uses symbol table in (unstripped executable)
500 * ------------------------------------------------------------------------*/
502 /* --------------------------------------------------------------------------
503 * Simple lookup table
505 * Current implementation is pretty dumb!
506 * ------------------------------------------------------------------------*/
513 static nat max_table_size;
514 static nat table_size;
515 static struct entry* table;
517 static void reset_table( int size )
519 max_table_size = size;
521 table = (struct entry *) malloc(size * sizeof(struct entry));
524 static void prepare_table( void )
526 /* Could sort it... */
529 static void insert( unsigned value, const char *name )
531 if ( table_size >= max_table_size ) {
532 barf( "Symbol table overflow\n" );
534 table[table_size].value = value;
535 table[table_size].name = name;
536 table_size = table_size + 1;
541 static rtsBool lookup_name( char *name, unsigned *result )
544 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
546 if (i < table_size) {
547 *result = table[i].value;
555 /* Code from somewhere inside GHC (circa 1994)
557 * "std"++xs -> "Zstd"++xs
558 * char_to_c 'Z' = "ZZ"
559 * char_to_c '&' = "Za"
560 * char_to_c '|' = "Zb"
561 * char_to_c ':' = "Zc"
562 * char_to_c '/' = "Zd"
563 * char_to_c '=' = "Ze"
564 * char_to_c '>' = "Zg"
565 * char_to_c '#' = "Zh"
566 * char_to_c '<' = "Zl"
567 * char_to_c '-' = "Zm"
568 * char_to_c '!' = "Zn"
569 * char_to_c '.' = "Zo"
570 * char_to_c '+' = "Zp"
571 * char_to_c '\'' = "Zq"
572 * char_to_c '*' = "Zt"
573 * char_to_c '_' = "Zu"
574 * char_to_c c = "Z" ++ show (ord c)
576 static char unZcode( char ch )
579 case 'a' : return ('&');
580 case 'b' : return ('|');
581 case 'c' : return (':');
582 case 'd' : return ('/');
583 case 'e' : return ('=');
584 case 'g' : return ('>');
585 case 'h' : return ('#');
586 case 'l' : return ('<');
587 case 'm' : return ('-');
588 case 'n' : return ('!');
589 case 'o' : return ('.');
590 case 'p' : return ('+');
591 case 'q' : return ('\'');
592 case 't' : return ('*');
593 case 'u' : return ('_');
595 case '\0' : return ('Z');
596 default : return (ch);
601 /* Precondition: out big enough to handle output (about twice length of in) */
602 static void enZcode( char *in, char *out )
608 for( i = 0; in[i] != '\0'; ++i ) {
683 rtsBool lookupGHCName( StgPtr addr, const char **result )
686 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
688 if (i < table_size) {
689 *result = table[i].name;
696 static void printZcoded( const char *raw )
700 while ( raw[j] != '\0' ) {
702 fputc(unZcode(raw[j+1]),stderr);
705 fputc(raw[j],stderr);
711 /* --------------------------------------------------------------------------
712 * Symbol table loading
713 * ------------------------------------------------------------------------*/
715 /* Causing linking trouble on Win32 plats, so I'm
716 disabling this for now.
718 #if defined(HAVE_BFD_H) && !defined(_WIN32)
722 /* Fairly ad-hoc piece of code that seems to filter out a lot of
723 * rubbish like the obj-splitting symbols
726 static rtsBool isReal( flagword flags, const char *name )
729 /* ToDo: make this work on BFD */
730 int tp = type & N_TYPE;
731 if (tp == N_TEXT || tp == N_DATA) {
732 return (name[0] == '_' && name[1] != '_');
738 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
739 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
746 extern void DEBUG_LoadSymbols( char *name )
752 abfd = bfd_openr(name, "default");
754 barf("can't open executable %s to get symbol table", name);
756 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
762 asymbol **symbol_table;
763 long number_of_symbols;
764 long num_real_syms = 0;
767 storage_needed = bfd_get_symtab_upper_bound (abfd);
769 if (storage_needed < 0) {
770 barf("can't read symbol table");
773 if (storage_needed == 0) {
774 belch("no storage needed");
777 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
779 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
781 if (number_of_symbols < 0) {
782 barf("can't canonicalise symbol table");
785 for( i = 0; i != number_of_symbols; ++i ) {
787 bfd_get_symbol_info(abfd,symbol_table[i],&info);
788 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
789 if (isReal(info.type, info.name)) {
795 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
796 number_of_symbols, num_real_syms)
799 reset_table( num_real_syms );
801 for( i = 0; i != number_of_symbols; ++i ) {
803 bfd_get_symbol_info(abfd,symbol_table[i],&info);
804 if (isReal(info.type, info.name)) {
805 insert( info.value, info.name );
814 #else /* HAVE_BFD_H */
816 extern void DEBUG_LoadSymbols( char *name )
821 #endif /* HAVE_BFD_H */
824 void printPtr( StgPtr p )
826 fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
829 void printObj( StgClosure *obj )
831 fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );