2 /* -----------------------------------------------------------------------------
3 * $Id: Printer.c,v 1.8 1999/03/03 19:16:29 sof 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 * ------------------------------------------------------------------------*/
43 extern void* itblNames[];
44 extern int nItblNames;
45 char* lookupHugsItblName ( void* v )
48 for (i = 0; i < nItblNames; i += 2)
49 if (itblNames[i] == v) return itblNames[i+1];
54 extern 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(payloadPtr(obj,i));
87 for (j = 0; j < info->layout.payload.nptrs; ++j) {
88 fprintf(stderr,", %xd#",payloadWord(obj,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(payloadPtr(ap,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(payloadPtr(pap,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); /* should be null */
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,"BH\n");
186 fprintf(stderr,"BQ(");
187 printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
188 fprintf(stderr,")\n");
192 case CONSTR_1_0: case CONSTR_0_1:
193 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
195 case CONSTR_CHARLIKE:
197 case CONSTR_NOCAF_STATIC:
199 /* We can't use printStdObject because we want to print the
203 const StgInfoTable* info = get_itbl(obj);
204 fprintf(stderr,"PACK(");
205 printPtr((StgPtr)obj->header.info);
206 fprintf(stderr,"(tag=%d)",info->srt_len);
207 for (i = 0; i < info->layout.payload.ptrs; ++i) {
208 fprintf(stderr,", ");
209 printPtr(payloadPtr(obj,i));
211 for (j = 0; j < info->layout.payload.nptrs; ++j) {
212 fprintf(stderr,", %x#",payloadWord(obj,i+j));
214 fprintf(stderr,")\n");
219 case FUN_1_0: case FUN_0_1:
220 case FUN_1_1: case FUN_0_2: case FUN_2_0:
222 printStdObject(obj,"FUN");
226 case THUNK_1_0: case THUNK_0_1:
227 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
229 /* ToDo: will this work for THUNK_STATIC too? */
230 printStdObject(obj,"THUNK");
236 fprintf(stderr,"ARR_WORDS(\"");
237 /* ToDo: we can't safely assume that this is a string! */
238 for (i = 0; arrWordsGetChar(obj,i); ++i) {
239 putchar(arrWordsGetChar(obj,i));
241 fprintf(stderr,"\")\n");
247 StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
248 fprintf(stderr,"UpdateFrame(");
249 printPtr((StgPtr)GET_INFO(u));
251 printPtr((StgPtr)u->updatee);
253 printPtr((StgPtr)u->link);
254 fprintf(stderr,")\n");
260 StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
261 fprintf(stderr,"CatchFrame(");
262 printPtr((StgPtr)GET_INFO(u));
264 printPtr((StgPtr)u->handler);
266 printPtr((StgPtr)u->link);
267 fprintf(stderr,")\n");
273 StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
274 fprintf(stderr,"SeqFrame(");
275 printPtr((StgPtr)GET_INFO(u));
277 printPtr((StgPtr)u->link);
278 fprintf(stderr,")\n");
284 StgStopFrame* u = stgCast(StgStopFrame*,obj);
285 fprintf(stderr,"StopFrame(");
286 printPtr((StgPtr)GET_INFO(u));
287 fprintf(stderr,")\n");
291 //barf("printClosure %d",get_itbl(obj)->type);
292 fprintf(stderr, "*** printClosure: unknown type %d ****\n",get_itbl(obj)->type );
297 StgPtr printStackObj( StgPtr sp )
299 /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
301 if (IS_ARG_TAG(*sp)) {
304 StackTag tag = (StackTag)*sp;
307 barf("printStackObj: ILLEGAL_TAG");
310 fprintf(stderr,"RealWorld#\n");
313 fprintf(stderr,"Int# %d\n", *(StgInt*)(sp+1));
316 fprintf(stderr,"Int64# %lld\n", *(StgInt64*)(sp+1));
319 fprintf(stderr,"Word# %d\n", *(StgWord*)(sp+1));
322 fprintf(stderr,"Addr# "); printPtr(*(StgAddr*)(sp+1)); fprintf(stderr,"\n");
325 fprintf(stderr,"Char# %d\n", *(StgChar*)(sp+1));
328 fprintf(stderr,"Float# %f\n", PK_FLT(sp+1));
331 fprintf(stderr,"Double# %f\n", PK_DBL(sp+1));
334 barf("printStackObj: unrecognised ARGTAG %d",tag);
336 sp += 1 + ARG_SIZE(tag);
338 #else /* !DEBUG_EXTRA */
342 fprintf(stderr,"Tag: %d words\n", tag);
343 for (i = 0; i < tag; i++) {
344 fprintf(stderr,"Word# %d\n", *sp++);
350 StgClosure* c = (StgClosure*)(*sp);
351 printPtr((StgPtr)*sp);
353 if (c == &ret_bco_info) {
354 fprintf(stderr, "\t\t");
355 fprintf(stderr, "ret_bco_info\n" );
358 if (IS_HUGS_CONSTR_INFO(GET_INFO(c))) {
359 fprintf(stderr, "\t\t\t");
360 fprintf(stderr, "ConstrInfoTable\n" );
362 if (get_itbl(c)->type == BCO) {
363 fprintf(stderr, "\t\t\t");
364 fprintf(stderr, "BCO(...)\n");
367 fprintf(stderr, "\t\t\t");
368 printClosure ( (StgClosure*)(*sp));
376 void printStackChunk( StgPtr sp, StgPtr spBottom )
379 const StgInfoTable *info;
381 ASSERT(sp <= spBottom);
382 while (sp < spBottom) {
383 if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO((void*)*sp)) {
384 info = get_itbl((StgClosure *)sp);
385 switch (info->type) {
388 printObj( stgCast(StgClosure*,sp) );
389 sp += sizeofW(StgUpdateFrame);
393 printObj( stgCast(StgClosure*,sp) );
394 sp += sizeofW(StgSeqFrame);
398 printObj( stgCast(StgClosure*,sp) );
399 sp += sizeofW(StgCatchFrame);
403 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
404 printObj( stgCast(StgClosure*,sp) );
408 fprintf(stderr, "RET_DYN (%p)\n", sp);
411 fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
416 fprintf(stderr, "RET_SMALL (%p)\n", sp);
417 bitmap = info->layout.bitmap;
420 while (bitmap != 0) {
421 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
422 if ((bitmap & 1) == 0) {
424 fprintf(stderr,"\n");
426 fprintf(stderr,"Word# %d\n", *sp++);
429 bitmap = bitmap >> 1;
441 fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
442 sp = printStackObj(sp);
446 void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
448 /* check everything down to the first update frame */
449 printStackChunk( sp, stgCast(StgPtr,su) );
450 while ( stgCast(StgPtr,su) < spBottom) {
451 sp = stgCast(StgPtr,su);
452 switch (get_itbl(su)->type) {
454 printObj( stgCast(StgClosure*,su) );
455 sp += sizeofW(StgUpdateFrame);
459 printObj( stgCast(StgClosure*,su) );
460 sp += sizeofW(StgSeqFrame);
461 su = stgCast(StgSeqFrame*,su)->link;
464 printObj( stgCast(StgClosure*,su) );
465 sp += sizeofW(StgCatchFrame);
466 su = stgCast(StgCatchFrame*,su)->link;
469 /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
470 printObj( stgCast(StgClosure*,su) );
473 barf("printStack: weird record found on update frame list.");
475 printStackChunk( sp, stgCast(StgPtr,su) );
477 ASSERT(stgCast(StgPtr,su) == spBottom);
480 void printTSO( StgTSO *tso )
482 printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
483 /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
487 /* --------------------------------------------------------------------------
488 * Address printing code
490 * Uses symbol table in (unstripped executable)
491 * ------------------------------------------------------------------------*/
493 /* --------------------------------------------------------------------------
494 * Simple lookup table
496 * Current implementation is pretty dumb!
497 * ------------------------------------------------------------------------*/
504 static nat max_table_size;
505 static nat table_size;
506 static struct entry* table;
508 static void reset_table( int size )
510 max_table_size = size;
512 table = (struct entry *) malloc(size * sizeof(struct entry));
515 static void prepare_table( void )
517 /* Could sort it... */
520 static void insert( unsigned value, const char *name )
522 if ( table_size >= max_table_size ) {
523 barf( "Symbol table overflow\n" );
525 table[table_size].value = value;
526 table[table_size].name = name;
527 table_size = table_size + 1;
532 static rtsBool lookup_name( char *name, unsigned *result )
535 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
537 if (i < table_size) {
538 *result = table[i].value;
546 /* Code from somewhere inside GHC (circa 1994)
548 * "std"++xs -> "Zstd"++xs
549 * char_to_c 'Z' = "ZZ"
550 * char_to_c '&' = "Za"
551 * char_to_c '|' = "Zb"
552 * char_to_c ':' = "Zc"
553 * char_to_c '/' = "Zd"
554 * char_to_c '=' = "Ze"
555 * char_to_c '>' = "Zg"
556 * char_to_c '#' = "Zh"
557 * char_to_c '<' = "Zl"
558 * char_to_c '-' = "Zm"
559 * char_to_c '!' = "Zn"
560 * char_to_c '.' = "Zo"
561 * char_to_c '+' = "Zp"
562 * char_to_c '\'' = "Zq"
563 * char_to_c '*' = "Zt"
564 * char_to_c '_' = "Zu"
565 * char_to_c c = "Z" ++ show (ord c)
567 static char unZcode( char ch )
570 case 'a' : return ('&');
571 case 'b' : return ('|');
572 case 'c' : return (':');
573 case 'd' : return ('/');
574 case 'e' : return ('=');
575 case 'g' : return ('>');
576 case 'h' : return ('#');
577 case 'l' : return ('<');
578 case 'm' : return ('-');
579 case 'n' : return ('!');
580 case 'o' : return ('.');
581 case 'p' : return ('+');
582 case 'q' : return ('\'');
583 case 't' : return ('*');
584 case 'u' : return ('_');
586 case '\0' : return ('Z');
587 default : return (ch);
592 /* Precondition: out big enough to handle output (about twice length of in) */
593 static void enZcode( char *in, char *out )
599 for( i = 0; in[i] != '\0'; ++i ) {
674 rtsBool lookupGHCName( StgPtr addr, const char **result )
677 for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
679 if (i < table_size) {
680 *result = table[i].name;
687 static void printZcoded( const char *raw )
691 while ( raw[j] != '\0' ) {
693 fputc(unZcode(raw[j+1]),stderr);
696 fputc(raw[j],stderr);
702 /* --------------------------------------------------------------------------
703 * Symbol table loading
704 * ------------------------------------------------------------------------*/
710 /* Fairly ad-hoc piece of code that seems to filter out a lot of
711 * rubbish like the obj-splitting symbols
714 static rtsBool isReal( flagword flags, const char *name )
717 /* ToDo: make this work on BFD */
718 int tp = type & N_TYPE;
719 if (tp == N_TEXT || tp == N_DATA) {
720 return (name[0] == '_' && name[1] != '_');
726 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
727 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
734 extern void DEBUG_LoadSymbols( char *name )
741 abfd = bfd_openr(name, "default");
743 barf("can't open executable %s to get symbol table", name);
745 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
752 asymbol **symbol_table;
753 long number_of_symbols;
754 long num_real_syms = 0;
757 storage_needed = bfd_get_symtab_upper_bound (abfd);
759 if (storage_needed < 0) {
760 barf("can't read symbol table");
763 if (storage_needed == 0) {
764 belch("no storage needed");
767 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
769 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
771 if (number_of_symbols < 0) {
772 barf("can't canonicalise symbol table");
775 for( i = 0; i != number_of_symbols; ++i ) {
777 bfd_get_symbol_info(abfd,symbol_table[i],&info);
778 /*fprintf(stderr,"\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
779 if (isReal(info.type, info.name)) {
785 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n",
786 number_of_symbols, num_real_syms)
789 reset_table( num_real_syms );
791 for( i = 0; i != number_of_symbols; ++i ) {
793 bfd_get_symbol_info(abfd,symbol_table[i],&info);
794 if (isReal(info.type, info.name)) {
795 insert( info.value, info.name );
804 #else /* HAVE_BFD_H */
806 extern void DEBUG_LoadSymbols( char *name )
811 #endif /* HAVE_BFD_H */