Printer.c 25.9 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: Printer.c,v 1.48 2001/11/20 16:17:23 simonmar Exp $
3
 *
4
 * (c) The GHC Team, 1994-2000.
5
6
7
8
9
 *
 * Heap printer
 * 
 * ---------------------------------------------------------------------------*/

10
#include "PosixSource.h"
11
#include "Rts.h"
12
#include "Printer.h"
13
14
15
16
17

#ifdef DEBUG

#include "RtsUtils.h"
#include "RtsFlags.h"
18
19
#include "MBlock.h"
#include "Storage.h"
20
21
22
#include "Bytecodes.h"  /* for InstrPtr */
#include "Disassembler.h"

23
24
#include "Printer.h"

25
#if defined(GRAN) || defined(PAR)
26
27
28
// HWL: explicit fixed header size to make debugging easier
int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable), 
    uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame); 
29
#endif
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* --------------------------------------------------------------------------
 * 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
 * ------------------------------------------------------------------------*/

51
void printPtr( StgPtr p )
52
53
54
55
56
{
    const char *raw;
    if (lookupGHCName( p, &raw )) {
        printZcoded(raw);
    } else {
57
        fprintf(stdout, "%p", p);
58
59
60
61
62
    }
}
  
void printObj( StgClosure *obj )
{
63
    fprintf(stdout,"Object "); printPtr((StgPtr)obj); fprintf(stdout," = ");
64
65
66
67
68
69
70
    printClosure(obj);
}

static void printStdObject( StgClosure *obj, char* tag )
{
    StgWord i, j;
    const StgInfoTable* info = get_itbl(obj);
71
    fprintf(stdout,"%s(",tag);
72
    printPtr((StgPtr)obj->header.info);
73
#ifdef PROFILING
74
    fprintf(stdout,", %s", obj->header.prof.ccs->cc->label);
75
#endif
76
    for (i = 0; i < info->layout.payload.ptrs; ++i) {
77
        fprintf(stdout,", ");
78
        printPtr((StgPtr)obj->payload[i]);
79
80
    }
    for (j = 0; j < info->layout.payload.nptrs; ++j) {
81
        fprintf(stdout,", %pd#",obj->payload[i+j]);
82
    }
83
    fprintf(stdout,")\n");
84
85
86
87
}

void printClosure( StgClosure *obj )
{
88
89
90
91
92
    StgInfoTable *info;
    
    info = get_itbl(obj);

    switch ( info->type ) {
93
94
95
    case INVALID_OBJECT:
            barf("Invalid object");
    case BCO:
96
            disassemble( (StgBCO*)obj );
97
            break;
98

99
100
101
102
103
104
105
    case MUT_VAR:
        {
	  StgMutVar* mv = (StgMutVar*)obj;
	  fprintf(stderr,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
          break;
        }

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

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

132
133
134
135
136
137
    case FOREIGN:
            fprintf(stderr,"FOREIGN("); 
            printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
            fprintf(stderr,")\n"); 
            break;

138
    case IND:
139
            fprintf(stdout,"IND("); 
140
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
141
            fprintf(stdout,")\n"); 
142
            break;
143
144

    case IND_STATIC:
145
            fprintf(stdout,"IND_STATIC("); 
146
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
147
            fprintf(stdout,")\n"); 
148
149
150
            break;

    case IND_OLDGEN:
151
            fprintf(stdout,"IND_OLDGEN("); 
152
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
153
            fprintf(stdout,")\n"); 
154
155
            break;

156
    case CAF_BLACKHOLE:
157
            fprintf(stdout,"CAF_BH("); 
158
            printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
159
            fprintf(stdout,")\n"); 
160
            break;
161

162
    case SE_BLACKHOLE:
163
            fprintf(stdout,"SE_BH\n"); 
164
165
166
            break;

    case SE_CAF_BLACKHOLE:
167
            fprintf(stdout,"SE_CAF_BH\n"); 
168
169
            break;

170
    case BLACKHOLE:
171
            fprintf(stdout,"BH\n"); 
172
            break;
173

174
    case BLACKHOLE_BQ:
175
            fprintf(stdout,"BQ("); 
176
            printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
177
            fprintf(stdout,")\n"); 
178
            break;
179

180
    case TSO:
181
182
183
      fprintf(stdout,"TSO("); 
      fprintf(stdout,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
      fprintf(stdout,")\n"); 
184
185
186
187
      break;

#if defined(PAR)
    case BLOCKED_FETCH:
188
      fprintf(stdout,"BLOCKED_FETCH("); 
189
190
      printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
      printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
191
      fprintf(stdout,")\n"); 
192
193
194
      break;

    case FETCH_ME:
195
      fprintf(stdout,"FETCH_ME("); 
196
      printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
197
      fprintf(stdout,")\n"); 
198
199
      break;

200
201
#ifdef DIST      
    case REMOTE_REF:
202
      fprintf(stdout,"REMOTE_REF("); 
203
      printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
204
      fprintf(stdout,")\n"); 
205
206
207
      break;
#endif
  
208
    case FETCH_ME_BQ:
209
      fprintf(stdout,"FETCH_ME_BQ("); 
210
211
      // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
      printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
212
      fprintf(stdout,")\n"); 
213
214
      break;
#endif
215
216
#if defined(GRAN) || defined(PAR)
    case RBH:
217
      fprintf(stdout,"RBH("); 
218
      printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
219
      fprintf(stdout,")\n"); 
220
      break;
221

222
223
#endif

224
    case CONSTR:
225
226
    case CONSTR_1_0: case CONSTR_0_1:
    case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
227
228
229
230
231
232
233
234
235
    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;
236
#ifdef PROFILING
237
238
	    fprintf(stdout,"%s(", info->prof.closure_desc);
	    fprintf(stdout,"%s", obj->header.prof.ccs->cc->label);
239
#else
240
            fprintf(stdout,"CONSTR(");
241
            printPtr((StgPtr)obj->header.info);
242
            fprintf(stdout,"(tag=%d)",info->srt_len);
243
#endif
244
            for (i = 0; i < info->layout.payload.ptrs; ++i) {
245
		fprintf(stdout,", ");
246
                printPtr((StgPtr)obj->payload[i]);
247
248
            }
            for (j = 0; j < info->layout.payload.nptrs; ++j) {
249
                fprintf(stdout,", %p#", obj->payload[i+j]);
250
            }
251
            fprintf(stdout,")\n");
252
253
            break;
        }
254

daan's avatar
daan committed
255
256
257
258
259
260
261
#ifdef XMLAMBDA
/* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
    case MUT_ARR_PTRS_FROZEN:
          {
            StgWord i;
            StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);

262
            fprintf(stdout,"Row<%i>(",p->ptrs);
daan's avatar
daan committed
263
            for (i = 0; i < p->ptrs; ++i) {
264
                if (i > 0) fprintf(stdout,", ");
daan's avatar
daan committed
265
266
                printPtr((StgPtr)(p->payload[i]));
            }
267
            fprintf(stdout,")\n");
daan's avatar
daan committed
268
269
270
271
            break;
          }
#endif  

272
    case FUN:
273
274
    case FUN_1_0: case FUN_0_1: 
    case FUN_1_1: case FUN_0_2: case FUN_2_0:
275
276
277
    case FUN_STATIC:
            printStdObject(obj,"FUN");
            break;
278

279
    case THUNK:
280
281
    case THUNK_1_0: case THUNK_0_1:
    case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
282
283
    case THUNK_STATIC:
            /* ToDo: will this work for THUNK_STATIC too? */
284
285
286
#ifdef PROFILING
	    printStdObject(obj,info->prof.closure_desc);
#else
287
            printStdObject(obj,"THUNK");
288
#endif
289
            break;
290
291
292
293
294

    case THUNK_SELECTOR:
            printStdObject(obj,"THUNK_SELECTOR");
            break;

295
296
297
    case ARR_WORDS:
        {
            StgWord i;
298
            fprintf(stdout,"ARR_WORDS(\"");
299
            /* ToDo: we can't safely assume that this is a string! 
300
301
            for (i = 0; arrWordsGetChar(obj,i); ++i) {
                putchar(arrWordsGetChar(obj,i));
302
303
		} */
	    for (i=0; i<((StgArrWords *)obj)->words; i++)
ken's avatar
ken committed
304
	      fprintf(stderr, "%ld", ((StgArrWords *)obj)->payload[i]);
305
306
307
            fprintf(stderr,"\")\n");
            break;
        }
308

309
310
311
    case UPDATE_FRAME:
        {
            StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
312
            fprintf(stdout,"UpdateFrame(");
313
            printPtr((StgPtr)GET_INFO(u));
314
            fprintf(stdout,",");
315
            printPtr((StgPtr)u->updatee);
316
            fprintf(stdout,",");
317
            printPtr((StgPtr)u->link);
318
            fprintf(stdout,")\n"); 
319
320
            break;
        }
321

322
323
324
    case CATCH_FRAME:
        {
            StgCatchFrame* u = stgCast(StgCatchFrame*,obj);
325
            fprintf(stdout,"CatchFrame(");
326
            printPtr((StgPtr)GET_INFO(u));
327
            fprintf(stdout,",");
328
            printPtr((StgPtr)u->handler);
329
            fprintf(stdout,",");
330
            printPtr((StgPtr)u->link);
331
            fprintf(stdout,")\n"); 
332
333
            break;
        }
334

335
336
337
    case SEQ_FRAME:
        {
            StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
338
            fprintf(stdout,"SeqFrame(");
339
            printPtr((StgPtr)GET_INFO(u));
340
            fprintf(stdout,",");
341
            printPtr((StgPtr)u->link);
342
            fprintf(stdout,")\n"); 
343
344
            break;
        }
345

346
347
348
    case STOP_FRAME:
        {
            StgStopFrame* u = stgCast(StgStopFrame*,obj);
349
            fprintf(stdout,"StopFrame(");
350
            printPtr((StgPtr)GET_INFO(u));
351
            fprintf(stdout,")\n"); 
352
353
354
            break;
        }
    default:
355
            //barf("printClosure %d",get_itbl(obj)->type);
356
            fprintf(stdout, "*** printClosure: unknown type %d ****\n",
357
                    get_itbl(obj)->type );
358
            barf("printClosure %d",get_itbl(obj)->type);
359
360
361
362
            return;
    }
}

363
364
365
366
367
368
369
/*
void printGraph( StgClosure *obj )
{
 printClosure(obj);
}
*/

370
371
StgPtr printStackObj( StgPtr sp )
{
372
    /*fprintf(stdout,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
373
374

    if (IS_ARG_TAG(*sp)) {
375
376
        nat i;
        StgWord tag = *sp++;
377
        fprintf(stdout,"Tagged{");
378
        for (i = 0; i < tag; i++) {
379
380
            fprintf(stdout,"0x%x#", (unsigned)(*sp++));
            if (i < tag-1) fprintf(stdout, ", ");
381
        }
382
        fprintf(stdout, "}\n");
383
    } else {
384
        StgClosure* c = (StgClosure*)(*sp);
385
        printPtr((StgPtr)*sp);
386
        if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
387
           fprintf(stdout, "\t\t\tstg_ctoi_ret_R1p_info\n" );
388
389
	} else
        if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
390
           fprintf(stdout, "\t\t\tstg_ctoi_ret_R1n_info\n" );
391
	} else
392
        if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
393
           fprintf(stdout, "\t\t\tstg_ctoi_ret_F1_info\n" );
394
395
	} else
        if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
396
           fprintf(stdout, "\t\t\tstg_ctoi_ret_D1_info\n" );
397
	} else
398
        if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
399
           fprintf(stdout, "\t\t\tstg_ctoi_ret_V_info\n" );
400
	} else
401
        if (get_itbl(c)->type == BCO) {
402
403
           fprintf(stdout, "\t\t\t");
           fprintf(stdout, "BCO(...)\n"); 
404
405
        }
        else {
406
           fprintf(stdout, "\t\t\t");
407
408
           printClosure ( (StgClosure*)(*sp));
        }
409
410
411
412
413
414
415
416
        sp += 1;
    }
    return sp;
    
}

void printStackChunk( StgPtr sp, StgPtr spBottom )
{
ken's avatar
ken committed
417
    StgWord bitmap;
418
419
420
421
    const StgInfoTable *info;

    ASSERT(sp <= spBottom);
    while (sp < spBottom) {
422
      if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
	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:
447
	  fprintf(stdout, "RET_DYN (%p)\n", sp);
448
449
	  bitmap = *++sp;
	  ++sp;
450
	  fprintf(stdout, "Bitmap: 0x%x\n", bitmap);
451
452
453
454
	  goto small_bitmap;

	case RET_SMALL:
	case RET_VEC_SMALL:
455
	  fprintf(stdout, "RET_SMALL (%p)\n", sp);
456
457
458
459
	  bitmap = info->layout.bitmap;
	  sp++;
	small_bitmap:
	  while (bitmap != 0) {
ken's avatar
ken committed
460
	    fprintf(stderr,"   stk[%ld] (%p) = ", spBottom-sp, sp);
461
462
	    if ((bitmap & 1) == 0) {
	      printPtr((P_)*sp);
463
	      fprintf(stdout,"\n");
464
	    } else {
465
	      fprintf(stderr,"Word# %ld\n", *sp);
466
467
468
469
470
471
472
473
474
475
476
477
478
479
	    }	      
	    sp++;
	    bitmap = bitmap >> 1;
	    }
	  continue;

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

	default:
	  break;
	}
      }
ken's avatar
ken committed
480
      fprintf(stderr,"Stack[%ld] (%p) = ", spBottom-sp, sp);
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
      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); */
}

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
/* -----------------------------------------------------------------------------
   Closure types
   
   NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
   -------------------------------------------------------------------------- */

static char *closure_type_names[] = {
  "INVALID_OBJECT",          	/* 0  */
  "CONSTR",                  	/* 1  */
  "CONSTR_1_0",			/* 2  */
  "CONSTR_0_1",			/* 3  */
  "CONSTR_2_0",			/* 4  */
  "CONSTR_1_1",			/* 5  */
  "CONSTR_0_2",			/* 6  */
  "CONSTR_INTLIKE",	        /* 7  */
  "CONSTR_CHARLIKE",	        /* 8  */
  "CONSTR_STATIC",	        /* 9  */
  "CONSTR_NOCAF_STATIC",     	/* 10 */
  "FUN",		        /* 11 */
  "FUN_1_0",		  	/* 12 */
  "FUN_0_1",		  	/* 13 */
  "FUN_2_0",		  	/* 14 */
  "FUN_1_1",		  	/* 15 */
  "FUN_0_2",			/* 16 */
  "FUN_STATIC",	        	/* 17 */
  "THUNK",		        /* 18 */
  "THUNK_1_0",	  		/* 19 */
  "THUNK_0_1",	  		/* 20 */
  "THUNK_2_0",	  		/* 21 */
  "THUNK_1_1",	  		/* 22 */
  "THUNK_0_2",			/* 23 */
  "THUNK_STATIC",	        /* 24 */
  "THUNK_SELECTOR",	        /* 25 */
  "BCO",		        /* 26 */
  "AP_UPD",		        /* 27 */
  "PAP",			/* 28 */
  "IND",		        /* 29 */
  "IND_OLDGEN",	        	/* 30 */
  "IND_PERM",	        	/* 31 */
  "IND_OLDGEN_PERM",	        /* 32 */
  "IND_STATIC",	        	/* 33 */
  "CAF_BLACKHOLE",		/* 36 */
  "RET_BCO",                 	/* 37 */
  "RET_SMALL",	        	/* 38 */
  "RET_VEC_SMALL",	        /* 39 */
  "RET_BIG",		        /* 40 */
  "RET_VEC_BIG",	        /* 41 */
  "RET_DYN",		        /* 42 */
  "UPDATE_FRAME",	        /* 43 */
  "CATCH_FRAME",	        /* 44 */
  "STOP_FRAME",	        	/* 45 */
  "SEQ_FRAME",	        	/* 46 */
  "BLACKHOLE",	        	/* 47 */
  "BLACKHOLE_BQ",	        /* 48 */
  "SE_BLACKHOLE",		/* 49 */
  "SE_CAF_BLACKHOLE",		/* 50 */
  "MVAR",		        /* 51 */
  "ARR_WORDS",	        	/* 52 */
  "MUT_ARR_PTRS",	        /* 53 */
  "MUT_ARR_PTRS_FROZEN",     	/* 54 */
  "MUT_VAR",		        /* 55 */
  "WEAK",		        /* 56 */
  "FOREIGN",		        /* 57 */
  "STABLE_NAME",	        /* 58 */
  "TSO",		        /* 59 */
  "BLOCKED_FETCH",	        /* 60 */
591
592
593
594
  "FETCH_ME",                   /* 61 */
  "FETCH_ME_BQ",                /* 62 */
  "RBH",                        /* 63 */
  "EVACUATED",                  /* 64 */
595
596
  "REMOTE_REF",                 /* 65 */
  "N_CLOSURE_TYPES"         	/* 66 */
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
};

char *
info_type(StgClosure *closure){ 
  return closure_type_names[get_itbl(closure)->type];
}

char *
info_type_by_ip(StgInfoTable *ip){ 
  return closure_type_names[ip->type];
}

void
info_hdr_type(StgClosure *closure, char *res){ 
  strcpy(res,closure_type_names[get_itbl(closure)->type]);
}
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
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

/* --------------------------------------------------------------------------
 * 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') {
820
            fputc(unZcode(raw[j+1]),stdout);
821
822
            j = j + 2;
        } else {
823
            fputc(raw[j],stdout);
824
825
826
827
828
829
830
831
832
            j = j + 1;
        }
    }
}

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

sof's avatar
sof committed
833
834
835
/* Causing linking trouble on Win32 plats, so I'm
   disabling this for now. 
*/
ken's avatar
ken committed
836
#ifdef USING_LIBBFD
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854

#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
855
    (void)flags;   /* keep gcc -Wall happy */
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
    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);
907
            /*fprintf(stdout,"\t%c\t0x%x      \t%s\n",info.type,(nat)info.value,info.name); */
908
909
910
911
912
913
            if (isReal(info.type, info.name)) {
                num_real_syms += 1;
            }
        }
    
        IF_DEBUG(evaluator,
914
                 fprintf(stdout,"Loaded %ld symbols. Of which %ld are real symbols\n", 
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
                         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 */

935
extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
936
{
rrt's avatar
rrt committed
937
  /* nothing, yet */
938
939
940
941
}

#endif /* HAVE_BFD_H */

942
943
#include "StoragePriv.h"

944
void findPtr(P_ p, int);		/* keep gcc -Wall happy */
945

946
void
947
findPtr(P_ p, int follow)
948
949
{
  nat s, g;
950
  P_ q, r;
951
  bdescr *bd;
952
953
954
  const int arr_size = 1024;
  StgPtr arr[arr_size];
  int i = 0;
955
956

  for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
957
958
      for (s = 0; s < generations[g].n_steps; s++) {
	  if (RtsFlags.GcFlags.generations == 1) {
959
	      bd = generations[g].steps[s].to_blocks;
960
961
962
963
964
965
966
967
968
	  } else {
	      bd = generations[g].steps[s].blocks;
	  }
	  for (; bd; bd = bd->link) {
	      for (q = bd->start; q < bd->free; q++) {
		  if (*q == (W_)p) {
		      if (i < arr_size) {
			  r = q;
			  while (!LOOKS_LIKE_GHC_INFO(*r)) { r--; };
969
			  fprintf(stdout, "%p = ", r);
970
971
972
973
974
975
976
			  printClosure((StgClosure *)r);
			  arr[i++] = r;
		      } else {
			  return;
		      }
		  }
	      }
977
978
	  }
      }
979
980
  }
  if (follow && i == 1) {
981
      fprintf(stdout, "-->\n");
982
      findPtr(arr[0], 1);
983
984
985
  }
}

986
987
988
#else /* DEBUG */
void printPtr( StgPtr p )
{
989
    fprintf(stdout, "ptr 0x%p (enable -DDEBUG for more info) " , p );
990
991
992
993
}
  
void printObj( StgClosure *obj )
{
994
    fprintf(stdout, "obj 0x%p (enable -DDEBUG for more info) " , obj );
995
}
996
#endif /* DEBUG */