Printer.c 26 KB
Newer Older
1
/* -----------------------------------------------------------------------------
sof's avatar
sof committed
2
 * $Id: Printer.c,v 1.51 2002/02/04 20:26:25 sof 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
/* --------------------------------------------------------------------------
 * 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 );
44
const char *   lookupGHCName ( void *addr );
45
46
47
48
49
50
static void    printZcoded   ( const char *raw );

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

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

68
69
static inline void
printStdObjHdr( StgClosure *obj, char* tag )
70
{
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
77
78
79
80
81
82
83
84
85
86
}

static void
printStdObject( StgClosure *obj, char* tag )
{
    StgWord i, j;
    const StgInfoTable* info;

    printStdObjHdr( obj, tag );

    info = get_itbl(obj);
87
    for (i = 0; i < info->layout.payload.ptrs; ++i) {
88
        fprintf(stdout,", ");
89
        printPtr((StgPtr)obj->payload[i]);
90
91
    }
    for (j = 0; j < info->layout.payload.nptrs; ++j) {
92
        fprintf(stdout,", %pd#",obj->payload[i+j]);
93
    }
94
    fprintf(stdout,")\n");
95
96
}

97
98
void
printClosure( StgClosure *obj )
99
{
100
101
102
103
104
    StgInfoTable *info;
    
    info = get_itbl(obj);

    switch ( info->type ) {
105
106
107
    case INVALID_OBJECT:
            barf("Invalid object");
    case BCO:
108
            disassemble( (StgBCO*)obj );
109
            break;
110

111
112
113
    case MUT_VAR:
        {
	  StgMutVar* mv = (StgMutVar*)obj;
114
	  fprintf(stdout,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
115
116
117
          break;
        }

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

131
132
133
134
    case PAP:
        {
	    StgPAP* pap = stgCast(StgPAP*,obj);
            StgWord i;
135
            fprintf(stdout,"PAP("); printPtr((StgPtr)pap->fun);
136
            for (i = 0; i < pap->n_args; ++i) {
137
                fprintf(stdout,", ");
138
                printPtr((StgPtr)pap->payload[i]);
139
            }
140
            fprintf(stdout,")\n");
141
142
            break;
        }
143

144
    case FOREIGN:
145
            fprintf(stdout,"FOREIGN("); 
146
            printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
147
            fprintf(stdout,")\n"); 
148
149
            break;

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

    case IND_STATIC:
157
            fprintf(stdout,"IND_STATIC("); 
158
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
159
            fprintf(stdout,")\n"); 
160
161
162
            break;

    case IND_OLDGEN:
163
            fprintf(stdout,"IND_OLDGEN("); 
164
            printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
165
            fprintf(stdout,")\n"); 
166
167
            break;

168
    case CAF_BLACKHOLE:
169
            fprintf(stdout,"CAF_BH("); 
170
            printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
171
            fprintf(stdout,")\n"); 
172
            break;
173

174
    case SE_BLACKHOLE:
175
            fprintf(stdout,"SE_BH\n"); 
176
177
178
            break;

    case SE_CAF_BLACKHOLE:
179
            fprintf(stdout,"SE_CAF_BH\n"); 
180
181
            break;

182
    case BLACKHOLE:
183
            fprintf(stdout,"BH\n"); 
184
            break;
185

186
    case BLACKHOLE_BQ:
187
            fprintf(stdout,"BQ("); 
188
            printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
189
            fprintf(stdout,")\n"); 
190
            break;
191

192
    case TSO:
193
194
195
      fprintf(stdout,"TSO("); 
      fprintf(stdout,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
      fprintf(stdout,")\n"); 
196
197
198
199
      break;

#if defined(PAR)
    case BLOCKED_FETCH:
200
      fprintf(stdout,"BLOCKED_FETCH("); 
201
202
      printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
      printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
203
      fprintf(stdout,")\n"); 
204
205
206
      break;

    case FETCH_ME:
207
      fprintf(stdout,"FETCH_ME("); 
208
      printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
209
      fprintf(stdout,")\n"); 
210
211
      break;

212
213
#ifdef DIST      
    case REMOTE_REF:
214
      fprintf(stdout,"REMOTE_REF("); 
215
      printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
216
      fprintf(stdout,")\n"); 
217
218
219
      break;
#endif
  
220
    case FETCH_ME_BQ:
221
      fprintf(stdout,"FETCH_ME_BQ("); 
222
223
      // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
      printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
224
      fprintf(stdout,")\n"); 
225
226
      break;
#endif
227
228
#if defined(GRAN) || defined(PAR)
    case RBH:
229
      fprintf(stdout,"RBH("); 
230
      printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
231
      fprintf(stdout,")\n"); 
232
      break;
233

234
235
#endif

236
    case CONSTR:
237
238
    case CONSTR_1_0: case CONSTR_0_1:
    case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
239
240
241
242
243
244
245
246
247
    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;
248
#ifdef PROFILING
249
250
	    fprintf(stdout,"%s(", info->prof.closure_desc);
	    fprintf(stdout,"%s", obj->header.prof.ccs->cc->label);
251
#else
252
            fprintf(stdout,"CONSTR(");
253
            printPtr((StgPtr)obj->header.info);
254
            fprintf(stdout,"(tag=%d)",info->srt_len);
255
#endif
256
            for (i = 0; i < info->layout.payload.ptrs; ++i) {
257
		fprintf(stdout,", ");
258
                printPtr((StgPtr)obj->payload[i]);
259
260
            }
            for (j = 0; j < info->layout.payload.nptrs; ++j) {
261
                fprintf(stdout,", %p#", obj->payload[i+j]);
262
            }
263
            fprintf(stdout,")\n");
264
265
            break;
        }
266

daan's avatar
daan committed
267
268
269
270
271
272
273
#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);

274
            fprintf(stdout,"Row<%i>(",p->ptrs);
daan's avatar
daan committed
275
            for (i = 0; i < p->ptrs; ++i) {
276
                if (i > 0) fprintf(stdout,", ");
daan's avatar
daan committed
277
278
                printPtr((StgPtr)(p->payload[i]));
            }
279
            fprintf(stdout,")\n");
daan's avatar
daan committed
280
281
282
283
            break;
          }
#endif  

284
    case FUN:
285
286
    case FUN_1_0: case FUN_0_1: 
    case FUN_1_1: case FUN_0_2: case FUN_2_0:
287
288
289
    case FUN_STATIC:
            printStdObject(obj,"FUN");
            break;
290

291
    case THUNK:
292
293
    case THUNK_1_0: case THUNK_0_1:
    case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
294
295
    case THUNK_STATIC:
            /* ToDo: will this work for THUNK_STATIC too? */
296
297
298
#ifdef PROFILING
	    printStdObject(obj,info->prof.closure_desc);
#else
299
            printStdObject(obj,"THUNK");
300
#endif
301
            break;
302
303

    case THUNK_SELECTOR:
304
305
306
	printStdObjHdr(obj, "THUNK_SELECTOR");
	fprintf(stdout, ", %p)\n", ((StgSelector *)obj)->selectee);
	break;
307

308
309
310
    case ARR_WORDS:
        {
            StgWord i;
311
            fprintf(stdout,"ARR_WORDS(\"");
312
            /* ToDo: we can't safely assume that this is a string! 
313
314
            for (i = 0; arrWordsGetChar(obj,i); ++i) {
                putchar(arrWordsGetChar(obj,i));
315
316
		} */
	    for (i=0; i<((StgArrWords *)obj)->words; i++)
sof's avatar
sof committed
317
	      fprintf(stdout, "%u", ((StgArrWords *)obj)->payload[i]);
318
            fprintf(stdout,"\")\n");
319
320
            break;
        }
321

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

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

348
349
350
    case SEQ_FRAME:
        {
            StgSeqFrame* u = stgCast(StgSeqFrame*,obj);
351
            fprintf(stdout,"SeqFrame(");
352
            printPtr((StgPtr)GET_INFO(u));
353
            fprintf(stdout,",");
354
            printPtr((StgPtr)u->link);
355
            fprintf(stdout,")\n"); 
356
357
            break;
        }
358

359
360
361
    case STOP_FRAME:
        {
            StgStopFrame* u = stgCast(StgStopFrame*,obj);
362
            fprintf(stdout,"StopFrame(");
363
            printPtr((StgPtr)GET_INFO(u));
364
            fprintf(stdout,")\n"); 
365
366
367
            break;
        }
    default:
368
            //barf("printClosure %d",get_itbl(obj)->type);
369
            fprintf(stdout, "*** printClosure: unknown type %d ****\n",
370
                    get_itbl(obj)->type );
371
            barf("printClosure %d",get_itbl(obj)->type);
372
373
374
375
            return;
    }
}

376
377
378
379
380
381
382
/*
void printGraph( StgClosure *obj )
{
 printClosure(obj);
}
*/

383
384
StgPtr printStackObj( StgPtr sp )
{
385
    /*fprintf(stdout,"Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
386
387

    if (IS_ARG_TAG(*sp)) {
388
389
        nat i;
        StgWord tag = *sp++;
390
        fprintf(stdout,"Tagged{");
391
        for (i = 0; i < tag; i++) {
392
393
            fprintf(stdout,"0x%x#", (unsigned)(*sp++));
            if (i < tag-1) fprintf(stdout, ", ");
394
        }
395
        fprintf(stdout, "}\n");
396
    } else {
397
        StgClosure* c = (StgClosure*)(*sp);
398
        printPtr((StgPtr)*sp);
399
        if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
400
           fprintf(stdout, "\t\t\tstg_ctoi_ret_R1p_info\n" );
401
402
	} else
        if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
403
           fprintf(stdout, "\t\t\tstg_ctoi_ret_R1n_info\n" );
404
	} else
405
        if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
406
           fprintf(stdout, "\t\t\tstg_ctoi_ret_F1_info\n" );
407
408
	} else
        if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
409
           fprintf(stdout, "\t\t\tstg_ctoi_ret_D1_info\n" );
410
	} else
411
        if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
412
           fprintf(stdout, "\t\t\tstg_ctoi_ret_V_info\n" );
413
	} else
414
        if (get_itbl(c)->type == BCO) {
415
416
           fprintf(stdout, "\t\t\t");
           fprintf(stdout, "BCO(...)\n"); 
417
418
        }
        else {
419
           fprintf(stdout, "\t\t\t");
420
421
           printClosure ( (StgClosure*)(*sp));
        }
422
423
424
425
426
427
428
429
        sp += 1;
    }
    return sp;
    
}

void printStackChunk( StgPtr sp, StgPtr spBottom )
{
ken's avatar
ken committed
430
    StgWord bitmap;
431
432
433
434
    const StgInfoTable *info;

    ASSERT(sp <= spBottom);
    while (sp < spBottom) {
435
      if (!IS_ARG_TAG(*sp) && LOOKS_LIKE_GHC_INFO(*sp)) {
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	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:
460
	  fprintf(stdout, "RET_DYN (%p)\n", sp);
461
462
	  bitmap = *++sp;
	  ++sp;
463
	  fprintf(stdout, "Bitmap: 0x%x\n", bitmap);
464
465
466
467
	  goto small_bitmap;

	case RET_SMALL:
	case RET_VEC_SMALL:
468
	  fprintf(stdout, "RET_SMALL (%p)\n", sp);
469
470
471
472
	  bitmap = info->layout.bitmap;
	  sp++;
	small_bitmap:
	  while (bitmap != 0) {
473
	    fprintf(stdout,"   stk[%ld] (%p) = ", spBottom-sp, sp);
474
475
	    if ((bitmap & 1) == 0) {
	      printPtr((P_)*sp);
476
	      fprintf(stdout,"\n");
477
	    } else {
478
	      fprintf(stdout,"Word# %ld\n", *sp);
479
480
481
482
483
484
485
486
487
488
489
490
491
492
	    }	      
	    sp++;
	    bitmap = bitmap >> 1;
	    }
	  continue;

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

	default:
	  break;
	}
      }
493
      fprintf(stdout,"Stack[%ld] (%p) = ", spBottom-sp, sp);
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
      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); */
}

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
/* -----------------------------------------------------------------------------
   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 */
604
605
606
607
  "FETCH_ME",                   /* 61 */
  "FETCH_ME_BQ",                /* 62 */
  "RBH",                        /* 63 */
  "EVACUATED",                  /* 64 */
608
609
  "REMOTE_REF",                 /* 65 */
  "N_CLOSURE_TYPES"         	/* 66 */
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
};

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]);
}
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

/* --------------------------------------------------------------------------
 * 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

814
const char *lookupGHCName( void *addr )
815
816
817
818
819
{
    nat i;
    for( i = 0; i < table_size && table[i].value != (unsigned) addr; ++i ) {
    }
    if (i < table_size) {
820
        return table[i].name;
821
    } else {
822
        return NULL;
823
824
825
826
827
828
829
830
831
    }
}

static void printZcoded( const char *raw )
{
    nat j = 0;
    
    while ( raw[j] != '\0' ) {
        if (raw[j] == 'Z') {
832
            fputc(unZcode(raw[j+1]),stdout);
833
834
            j = j + 2;
        } else {
835
            fputc(raw[j],stdout);
836
837
838
839
840
841
842
843
844
            j = j + 1;
        }
    }
}

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

sof's avatar
sof committed
845
846
847
/* Causing linking trouble on Win32 plats, so I'm
   disabling this for now. 
*/
ken's avatar
ken committed
848
#ifdef USING_LIBBFD
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

#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
867
    (void)flags;   /* keep gcc -Wall happy */
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
907
908
909
910
911
912
913
914
915
916
917
918
    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);
919
            /*fprintf(stdout,"\t%c\t0x%x      \t%s\n",info.type,(nat)info.value,info.name); */
920
921
922
923
924
925
            if (isReal(info.type, info.name)) {
                num_real_syms += 1;
            }
        }
    
        IF_DEBUG(evaluator,
926
                 fprintf(stdout,"Loaded %ld symbols. Of which %ld are real symbols\n", 
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
                         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 */

947
extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
948
{
rrt's avatar
rrt committed
949
  /* nothing, yet */
950
951
952
953
}

#endif /* HAVE_BFD_H */

954
955
#include "StoragePriv.h"

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

958
void
959
findPtr(P_ p, int follow)
960
961
{
  nat s, g;
962
  P_ q, r;
963
  bdescr *bd;
964
965
966
  const int arr_size = 1024;
  StgPtr arr[arr_size];
  int i = 0;
967
968

  for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
969
970
      for (s = 0; s < generations[g].n_steps; s++) {
	  if (RtsFlags.GcFlags.generations == 1) {
971
	      bd = generations[g].steps[s].to_blocks;
972
973
974
975
976
977
978
979
	  } 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;
980
981
982
			  while (!LOOKS_LIKE_GHC_INFO(*r) || *r == NULL) {
			      r--;
			  }
983
			  fprintf(stdout, "%p = ", r);
984
985
986
987
988
989
990
			  printClosure((StgClosure *)r);
			  arr[i++] = r;
		      } else {
			  return;
		      }
		  }
	      }
991
992
	  }
      }
993
994
  }
  if (follow && i == 1) {
995
      fprintf(stdout, "-->\n");
996
      findPtr(arr[0], 1);
997
998
999
  }
}

1000
1001
1002
#else /* DEBUG */
void printPtr( StgPtr p )
{
1003
    fprintf(stdout, "ptr 0x%p (enable -DDEBUG for more info) " , p );
1004
1005
1006
1007
}
  
void printObj( StgClosure *obj )
{
1008
    fprintf(stdout, "obj 0x%p (enable -DDEBUG for more info) " , obj );
1009
}
1010
#endif /* DEBUG */