Printer.c 22 KB
Newer Older
1

2
/* -----------------------------------------------------------------------------
3
 * $Id: Printer.c,v 1.15 1999/07/14 13:44:19 simonmar Exp $
4
 *
5
 * Copyright (c) 1994-1999.
6
7
8
9
10
11
 *
 * Heap printer
 * 
 * ---------------------------------------------------------------------------*/

#include "Rts.h"
12
#include "Printer.h"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#ifdef DEBUG

#include "RtsUtils.h"
#include "RtsFlags.h"
#include "Bytecodes.h"  /* for InstrPtr */
#include "Disassembler.h"

/* --------------------------------------------------------------------------
 * local function decls
 * ------------------------------------------------------------------------*/

static void    printStdObject( StgClosure *obj, char* tag );
static void    reset_table   ( int size );
static void    prepare_table ( void );
static void    insert        ( unsigned value, const char *name );
#if 0 /* unused but might be useful sometime */
static rtsBool lookup_name   ( char *name, unsigned *result );
static void    enZcode       ( char *in, char *out );
#endif
static char    unZcode       ( char ch );
rtsBool lookupGHCName ( StgPtr addr, const char **result );
static void    printZcoded   ( const char *raw );

/* --------------------------------------------------------------------------
 * Printer
 * ------------------------------------------------------------------------*/

41

42
#ifdef INTERPRETER
43
44
45
46
47
48
49
50
51
extern void* itblNames[];
extern int   nItblNames;
char* lookupHugsItblName ( void* v )
{
   int i;
   for (i = 0; i < nItblNames; i += 2)
      if (itblNames[i] == v) return itblNames[i+1];
   return NULL;
}
52
#endif
53

54
void printPtr( StgPtr p )
55
{
56
#ifdef INTERPRETER
57
    char* str;
58
#endif
59
60
61
62
63
64
    const char *raw;
    if (lookupGHCName( p, &raw )) {
        printZcoded(raw);
#ifdef INTERPRETER
    } else if ((raw = lookupHugsName(p)) != 0) {
        fprintf(stderr, "%s", raw);
65
66
    } else if ((str = lookupHugsItblName(p)) != 0) {
        fprintf(stderr, "%p=%s", p, str);
67
#endif
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    } else {
        fprintf(stderr, "%p", p);
    }
}
  
void printObj( StgClosure *obj )
{
    fprintf(stderr,"Object "); printPtr((StgPtr)obj); fprintf(stderr," = ");
    printClosure(obj);
}

static void printStdObject( StgClosure *obj, char* tag )
{
    StgWord i, j;
    const StgInfoTable* info = get_itbl(obj);
    fprintf(stderr,"%s(",tag);
84
    printPtr((StgPtr)obj->header.info);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    for (i = 0; i < info->layout.payload.ptrs; ++i) {
        fprintf(stderr,", ");
        printPtr(payloadPtr(obj,i));
    }
    for (j = 0; j < info->layout.payload.nptrs; ++j) {
        fprintf(stderr,", %xd#",payloadWord(obj,i+j));
    }
    fprintf(stderr,")\n");
}

void printClosure( StgClosure *obj )
{
    switch ( get_itbl(obj)->type ) {
    case INVALID_OBJECT:
            barf("Invalid object");
#ifdef INTERPRETER
    case BCO:
            fprintf(stderr,"BCO\n");
            disassemble(stgCast(StgBCO*,obj),"\t");
            break;
#endif
106

107
108
109
110
111
112
113
114
115
116
117
118
    case AP_UPD:
        {
	    StgAP_UPD* ap = stgCast(StgAP_UPD*,obj);
            StgWord i;
            fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
            for (i = 0; i < ap->n_args; ++i) {
                fprintf(stderr,", ");
                printPtr(payloadPtr(ap,i));
            }
            fprintf(stderr,")\n");
            break;
        }
119

120
121
122
123
    case PAP:
        {
	    StgPAP* pap = stgCast(StgPAP*,obj);
            StgWord i;
124
            fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
125
126
127
128
129
130
131
            for (i = 0; i < pap->n_args; ++i) {
                fprintf(stderr,", ");
                printPtr(payloadPtr(pap,i));
            }
            fprintf(stderr,")\n");
            break;
        }
132

133
134
135
136
137
    case IND:
            fprintf(stderr,"IND("); 
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
            fprintf(stderr,")\n"); 
            break;
138
139
140
141
142
143
144
145
146
147
148
149
150

    case IND_STATIC:
            fprintf(stderr,"IND_STATIC("); 
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
            fprintf(stderr,")\n"); 
            break;

    case IND_OLDGEN:
            fprintf(stderr,"IND_OLDGEN("); 
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
            fprintf(stderr,")\n"); 
            break;

151
152
153
154
155
156
157
158
159
160
161
162
    case CAF_UNENTERED:
        {
	    StgCAF* caf = stgCast(StgCAF*,obj);
            fprintf(stderr,"CAF_UNENTERED("); 
            printPtr((StgPtr)caf->body);
            fprintf(stderr,", ");
            printPtr((StgPtr)caf->value); /* should be null */
            fprintf(stderr,", ");
            printPtr((StgPtr)caf->link);  /* should be null */
            fprintf(stderr,")\n"); 
            break;
        }
163

164
165
166
167
168
169
170
171
172
173
174
175
    case CAF_ENTERED:
        {
	    StgCAF* caf = stgCast(StgCAF*,obj);
            fprintf(stderr,"CAF_ENTERED("); 
            printPtr((StgPtr)caf->body);
            fprintf(stderr,", ");
            printPtr((StgPtr)caf->value);
            fprintf(stderr,", ");
            printPtr((StgPtr)caf->link);
            fprintf(stderr,")\n"); 
            break;
        }
176

177
178
    case CAF_BLACKHOLE:
            fprintf(stderr,"CAF_BH("); 
179
            printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
180
181
            fprintf(stderr,")\n"); 
            break;
182

183
184
185
186
187
188
189
190
    case SE_BLACKHOLE:
            fprintf(stderr,"SE_BH\n"); 
            break;

    case SE_CAF_BLACKHOLE:
            fprintf(stderr,"SE_CAF_BH\n"); 
            break;

191
    case BLACKHOLE:
192
193
            fprintf(stderr,"BH\n"); 
            break;
194

195
196
    case BLACKHOLE_BQ:
            fprintf(stderr,"BQ("); 
197
            printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
198
199
            fprintf(stderr,")\n"); 
            break;
200

201
    case CONSTR:
202
203
    case CONSTR_1_0: case CONSTR_0_1:
    case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
204
205
206
207
208
209
210
211
212
213
214
    case CONSTR_INTLIKE:
    case CONSTR_CHARLIKE:
    case CONSTR_STATIC:
    case CONSTR_NOCAF_STATIC:
        {
            /* We can't use printStdObject because we want to print the
             * tag as well.
	     */
            StgWord i, j;
            const StgInfoTable* info = get_itbl(obj);
            fprintf(stderr,"PACK(");
215
            printPtr((StgPtr)obj->header.info);
216
217
218
219
220
221
222
223
224
225
226
            fprintf(stderr,"(tag=%d)",info->srt_len);
            for (i = 0; i < info->layout.payload.ptrs; ++i) {
                fprintf(stderr,", ");
                printPtr(payloadPtr(obj,i));
            }
            for (j = 0; j < info->layout.payload.nptrs; ++j) {
                fprintf(stderr,", %x#",payloadWord(obj,i+j));
            }
            fprintf(stderr,")\n");
            break;
        }
227

228
    case FUN:
229
230
    case FUN_1_0: case FUN_0_1: 
    case FUN_1_1: case FUN_0_2: case FUN_2_0:
231
232
233
    case FUN_STATIC:
            printStdObject(obj,"FUN");
            break;
234

235
    case THUNK:
236
237
    case THUNK_1_0: case THUNK_0_1:
    case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    case THUNK_STATIC:
            /* ToDo: will this work for THUNK_STATIC too? */
            printStdObject(obj,"THUNK");
            break;
#if 0
    case ARR_WORDS:
        {
            StgWord i;
            fprintf(stderr,"ARR_WORDS(\"");
            /* ToDo: we can't safely assume that this is a string! */
            for (i = 0; arrWordsGetChar(obj,i); ++i) {
                putchar(arrWordsGetChar(obj,i));
            }
            fprintf(stderr,"\")\n");
            break;
        }
#endif
    case UPDATE_FRAME:
        {
            StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
            fprintf(stderr,"UpdateFrame(");
            printPtr((StgPtr)GET_INFO(u));
            fprintf(stderr,",");
            printPtr((StgPtr)u->updatee);
            fprintf(stderr,",");
            printPtr((StgPtr)u->link);
            fprintf(stderr,")\n"); 
            break;
        }
267

268
269
270
271
272
273
274
275
276
277
278
279
    case CATCH_FRAME:
        {
            StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
            fprintf(stderr,"CatchFrame(");
            printPtr((StgPtr)GET_INFO(u));
            fprintf(stderr,",");
            printPtr((StgPtr)u->handler);
            fprintf(stderr,",");
            printPtr((StgPtr)u->link);
            fprintf(stderr,")\n"); 
            break;
        }
280

281
282
283
284
285
286
287
288
289
290
    case SEQ_FRAME:
        {
            StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
            fprintf(stderr,"SeqFrame(");
            printPtr((StgPtr)GET_INFO(u));
            fprintf(stderr,",");
            printPtr((StgPtr)u->link);
            fprintf(stderr,")\n"); 
            break;
        }
291

292
293
294
295
296
297
298
299
300
    case STOP_FRAME:
        {
            StgStopFrame* u = stgCast(StgStopFrame*,obj);
            fprintf(stderr,"StopFrame(");
            printPtr((StgPtr)GET_INFO(u));
            fprintf(stderr,")\n"); 
            break;
        }
    default:
301
302
            //barf("printClosure %d",get_itbl(obj)->type);
            fprintf(stderr, "*** printClosure: unknown type %d ****\n",get_itbl(obj)->type );
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
            return;
    }
}

StgPtr printStackObj( StgPtr sp )
{
    /*fprintf(stderr,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */

    if (IS_ARG_TAG(*sp)) {

#ifdef DEBUG_EXTRA
        StackTag tag = (StackTag)*sp;
        switch ( tag ) {
        case ILLEGAL_TAG:
                barf("printStackObj: ILLEGAL_TAG");
                break;
        case REALWORLD_TAG:
                fprintf(stderr,"RealWorld#\n");
                break;
        case INT_TAG:
                fprintf(stderr,"Int# %d\n", *(StgInt*)(sp+1));
                break;
        case INT64_TAG:
                fprintf(stderr,"Int64# %lld\n", *(StgInt64*)(sp+1));
                break;
        case WORD_TAG:
                fprintf(stderr,"Word# %d\n", *(StgWord*)(sp+1));
                break;
        case ADDR_TAG:
                fprintf(stderr,"Addr# "); printPtr(*(StgAddr*)(sp+1)); fprintf(stderr,"\n");
                break;
        case CHAR_TAG:
                fprintf(stderr,"Char# %d\n", *(StgChar*)(sp+1));
                break;
        case FLOAT_TAG:
                fprintf(stderr,"Float# %f\n", PK_FLT(sp+1));
                break;
        case DOUBLE_TAG:
                fprintf(stderr,"Double# %f\n", PK_DBL(sp+1));
                break;
        default:
                barf("printStackObj: unrecognised ARGTAG %d",tag);
        }
        sp += 1 + ARG_SIZE(tag);

#else /* !DEBUG_EXTRA */
	{
	    StgWord tag = *sp++;
	    nat i;
	    fprintf(stderr,"Tag: %d words\n", tag);
	    for (i = 0; i < tag; i++) {
	        fprintf(stderr,"Word# %d\n", *sp++);
	    }
	}
#endif

    } else {
360
        StgClosure* c = (StgClosure*)(*sp);
361
        printPtr((StgPtr)*sp);
362
#ifdef INTERPRETER
363
364
365
366
367
368
369
370
        if (c == &ret_bco_info) {
           fprintf(stderr, "\t\t");
           fprintf(stderr, "ret_bco_info\n" );
	} else
        if (IS_HUGS_CONSTR_INFO(GET_INFO(c))) {
           fprintf(stderr, "\t\t\t");
           fprintf(stderr, "ConstrInfoTable\n" );
        } else
371
#endif
372
373
374
375
376
377
378
379
        if (get_itbl(c)->type == BCO) {
           fprintf(stderr, "\t\t\t");
           fprintf(stderr, "BCO(...)\n"); 
        }
        else {
           fprintf(stderr, "\t\t\t");
           printClosure ( (StgClosure*)(*sp));
        }
380
381
382
383
384
385
386
387
        sp += 1;
    }
    return sp;
    
}

void printStackChunk( StgPtr sp, StgPtr spBottom )
{
sof's avatar
sof committed
388
    StgWord32 bitmap;
389
390
391
392
    const StgInfoTable *info;

    ASSERT(sp <= spBottom);
    while (sp < spBottom) {
393
      if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
	info = get_itbl((StgClosure *)sp);
	switch (info->type) {

	case UPDATE_FRAME:
	    printObj( stgCast(StgClosure*,sp) );
	    sp += sizeofW(StgUpdateFrame);
	    continue;

	case SEQ_FRAME:
	    printObj( stgCast(StgClosure*,sp) );
	    sp += sizeofW(StgSeqFrame);
	    continue;

	case CATCH_FRAME:
	    printObj( stgCast(StgClosure*,sp) );
	    sp += sizeofW(StgCatchFrame);
	    continue;

	case STOP_FRAME:
	    /* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
	    printObj( stgCast(StgClosure*,sp) );
	    continue;

	case RET_DYN:
	  fprintf(stderr, "RET_DYN (%p)\n", sp);
	  bitmap = *++sp;
	  ++sp;
	  fprintf(stderr, "Bitmap: 0x%x\n", bitmap);
	  goto small_bitmap;

	case RET_SMALL:
	case RET_VEC_SMALL:
	  fprintf(stderr, "RET_SMALL (%p)\n", sp);
	  bitmap = info->layout.bitmap;
	  sp++;
	small_bitmap:
	  while (bitmap != 0) {
	    fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
	    if ((bitmap & 1) == 0) {
	      printPtr((P_)*sp);
	      fprintf(stderr,"\n");
	    } else {
	      fprintf(stderr,"Word# %d\n", *sp++);
	    }	      
	    sp++;
	    bitmap = bitmap >> 1;
	    }
	  continue;

	case RET_BIG:
	case RET_VEC_BIG:
	  barf("todo");

	default:
	  break;
	}
      }
      fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
      sp = printStackObj(sp);
    }
}

void printStack( StgPtr sp, StgPtr spBottom, StgUpdateFrame* su )
{
    /* check everything down to the first update frame */
    printStackChunk( sp, stgCast(StgPtr,su) );
    while ( stgCast(StgPtr,su) < spBottom) {
	sp = stgCast(StgPtr,su);
	switch (get_itbl(su)->type) {
	case UPDATE_FRAME:
                printObj( stgCast(StgClosure*,su) );
                sp += sizeofW(StgUpdateFrame);
		su = su->link;
		break;
	case SEQ_FRAME:
                printObj( stgCast(StgClosure*,su) );
                sp += sizeofW(StgSeqFrame);
		su = stgCast(StgSeqFrame*,su)->link;
		break;
	case CATCH_FRAME:
                printObj( stgCast(StgClosure*,su) );
                sp += sizeofW(StgCatchFrame);
		su = stgCast(StgCatchFrame*,su)->link;
		break;
	case STOP_FRAME:
		/* not quite: ASSERT(stgCast(StgPtr,su) == spBottom); */
                printObj( stgCast(StgClosure*,su) );
		return;
	default:
		barf("printStack: weird record found on update frame list.");
	}
	printStackChunk( sp, stgCast(StgPtr,su) );
    }
    ASSERT(stgCast(StgPtr,su) == spBottom);
}

void printTSO( StgTSO *tso )
{
    printStack( tso->sp, tso->stack+tso->stack_size,tso->su);
    /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
}


/* --------------------------------------------------------------------------
 * Address printing code
 *
 * Uses symbol table in (unstripped executable)
 * ------------------------------------------------------------------------*/

/* --------------------------------------------------------------------------
 * Simple lookup table
 *
 * Current implementation is pretty dumb!
 * ------------------------------------------------------------------------*/

struct entry {
    nat value;
    const char *name;
};

static nat max_table_size;
static nat table_size;
static struct entry* table;

static void reset_table( int size )
{
    max_table_size = size;
    table_size = 0;
    table = (struct entry *) malloc(size * sizeof(struct entry));
}

static void prepare_table( void )
{
    /* Could sort it...  */
}

static void insert( unsigned value, const char *name )
{
    if ( table_size >= max_table_size ) {
        barf( "Symbol table overflow\n" );
    }
    table[table_size].value = value;
    table[table_size].name = name;
    table_size = table_size + 1;
}


#if 0
static rtsBool lookup_name( char *name, unsigned *result )
{
    int i;
    for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
    }
    if (i < table_size) {
        *result = table[i].value;
        return rtsTrue;
    } else {
        return rtsFalse;
    }
}
#endif

/* Code from somewhere inside GHC (circa 1994)
 * * Z-escapes:
 *     "std"++xs -> "Zstd"++xs
 *     char_to_c 'Z'  = "ZZ"
 *     char_to_c '&'  = "Za"
 *     char_to_c '|'  = "Zb"
 *     char_to_c ':'  = "Zc"
 *     char_to_c '/'  = "Zd"
 *     char_to_c '='  = "Ze"
 *     char_to_c '>'  = "Zg"
 *     char_to_c '#'  = "Zh"
 *     char_to_c '<'  = "Zl"
 *     char_to_c '-'  = "Zm"
 *     char_to_c '!'  = "Zn"
 *     char_to_c '.'  = "Zo"
 *     char_to_c '+'  = "Zp"
 *     char_to_c '\'' = "Zq"
 *     char_to_c '*'  = "Zt"
 *     char_to_c '_'  = "Zu"
 *     char_to_c c    = "Z" ++ show (ord c)
 */
static char unZcode( char ch )
{
    switch (ch) {
    case 'a'  : return ('&');
    case 'b'  : return ('|');
    case 'c'  : return (':');
    case 'd'  : return ('/');
    case 'e'  : return ('=');
    case 'g'  : return ('>');
    case 'h'  : return ('#');
    case 'l'  : return ('<');
    case 'm'  : return ('-');
    case 'n'  : return ('!');
    case 'o'  : return ('.');
    case 'p'  : return ('+');
    case 'q'  : return ('\'');
    case 't'  : return ('*');
    case 'u'  : return ('_');
    case 'Z'  :
    case '\0' : return ('Z');
    default   : return (ch);
    }
}

#if 0
/* Precondition: out big enough to handle output (about twice length of in) */
static void enZcode( char *in, char *out )
{
    int i, j;

    j = 0;
    out[ j++ ] = '_';
    for( i = 0; in[i] != '\0'; ++i ) {
        switch (in[i]) {
        case 'Z'  : 
                out[j++] = 'Z';
                out[j++] = 'Z';
                break;
        case '&'  : 
                out[j++] = 'Z';
                out[j++] = 'a';
                break;
        case '|'  : 
                out[j++] = 'Z';
                out[j++] = 'b';
                break;
        case ':'  : 
                out[j++] = 'Z';
                out[j++] = 'c';
                break;
        case '/'  : 
                out[j++] = 'Z';
                out[j++] = 'd';
                break;
        case '='  : 
                out[j++] = 'Z';
                out[j++] = 'e';
                break;
        case '>'  : 
                out[j++] = 'Z';
                out[j++] = 'g';
                break;
        case '#'  : 
                out[j++] = 'Z';
                out[j++] = 'h';
                break;
        case '<'  : 
                out[j++] = 'Z';
                out[j++] = 'l';
                break;
        case '-'  : 
                out[j++] = 'Z';
                out[j++] = 'm';
                break;
        case '!'  : 
                out[j++] = 'Z';
                out[j++] = 'n';
                break;
        case '.'  : 
                out[j++] = 'Z';
                out[j++] = 'o';
                break;
        case '+'  : 
                out[j++] = 'Z';
                out[j++] = 'p';
                break;
        case '\'' : 
                out[j++] = 'Z';
                out[j++] = 'q';
                break;
        case '*'  : 
                out[j++] = 'Z';
                out[j++] = 't';
                break;
        case '_'  : 
                out[j++] = 'Z';
                out[j++] = 'u';
                break;
        default :
                out[j++] = in[i];
                break;
        }
    }
    out[j] = '\0';
}
#endif

rtsBool lookupGHCName( StgPtr addr, const char **result )
{
    nat i;
    for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
    }
    if (i < table_size) {
        *result = table[i].name;
        return rtsTrue;
    } else {
        return rtsFalse;
    }
}

static void printZcoded( const char *raw )
{
    nat j = 0;
    
    while ( raw[j] != '\0' ) {
        if (raw[j] == 'Z') {
            fputc(unZcode(raw[j+1]),stderr);
            j = j + 2;
        } else {
            fputc(raw[j],stderr);
            j = j + 1;
        }
    }
}

/* --------------------------------------------------------------------------
 * Symbol table loading
 * ------------------------------------------------------------------------*/

sof's avatar
sof committed
716
717
718
719
/* Causing linking trouble on Win32 plats, so I'm
   disabling this for now. 
*/
#if defined(HAVE_BFD_H) && !defined(_WIN32)
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737

#include <bfd.h>

/* Fairly ad-hoc piece of code that seems to filter out a lot of
 * rubbish like the obj-splitting symbols
 */

static rtsBool isReal( flagword flags, const char *name )
{
#if 0
    /* ToDo: make this work on BFD */
    int tp = type & N_TYPE;    
    if (tp == N_TEXT || tp == N_DATA) {
        return (name[0] == '_' && name[1] != '_');
    } else {
        return rtsFalse;
    }
#else
738
    (void)flags;   /* keep gcc -Wall happy */
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
    if (*name == '\0'  || 
	(name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
	(name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
	return rtsFalse;
    }
    return rtsTrue;
#endif
}

extern void DEBUG_LoadSymbols( char *name )
{
    bfd* abfd;
    char **matching;

    bfd_init();
    abfd = bfd_openr(name, "default");
    if (abfd == NULL) {
	barf("can't open executable %s to get symbol table", name);
    }
    if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
	barf("mismatch");
    }

    {
	long storage_needed;
	asymbol **symbol_table;
	long number_of_symbols;
        long num_real_syms = 0;
	long i;
     
	storage_needed = bfd_get_symtab_upper_bound (abfd);
     
	if (storage_needed < 0) {
	    barf("can't read symbol table");
	}     
#if 0
	if (storage_needed == 0) {
	    belch("no storage needed");
	}
#endif
	symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");

	number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
     
	if (number_of_symbols < 0) {
	    barf("can't canonicalise symbol table");
	}

        for( i = 0; i != number_of_symbols; ++i ) {
            symbol_info info;
            bfd_get_symbol_info(abfd,symbol_table[i],&info);
            /*fprintf(stderr,"\t%c\t0x%x      \t%s\n",info.type,(nat)info.value,info.name); */
            if (isReal(info.type, info.name)) {
                num_real_syms += 1;
            }
        }
    
        IF_DEBUG(evaluator,
                 fprintf(stderr,"Loaded %ld symbols. Of which %ld are real symbols\n", 
                         number_of_symbols, num_real_syms)
                 );

        reset_table( num_real_syms );
    
        for( i = 0; i != number_of_symbols; ++i ) {
            symbol_info info;
            bfd_get_symbol_info(abfd,symbol_table[i],&info);
            if (isReal(info.type, info.name)) {
                insert( info.value, info.name );
            }
        }
        
        free(symbol_table);
    }
    prepare_table();
}

#else /* HAVE_BFD_H */

extern void DEBUG_LoadSymbols( char *name )
{
  /* nothing, yet */
}

#endif /* HAVE_BFD_H */

825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include "StoragePriv.h"

void
findPtr(P_ p)
{
  nat s, g;
  P_ q;
  bdescr *bd;

  for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
    for (s = 0; s < generations[g].n_steps; s++) {
      for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
	for (q = bd->start; q < bd->free; q++) {
	  if (*q == (W_)p) {
	    printf("%p\n", q);
	  }
	}
      }
    }
  }
}

847
848
849
850
851
852
853
854
855
856
#else /* DEBUG */
void printPtr( StgPtr p )
{
    fprintf(stderr, "ptr 0x%p (enable -DDEBUG for more info) " , p );
}
  
void printObj( StgClosure *obj )
{
    fprintf(stderr, "obj 0x%p (enable -DDEBUG for more info) " , obj );
}
857
#endif /* DEBUG */