cd4b9a1bb32f15964695ad9d2d9eb010e922c85f
[ghc.git] / rts / Printer.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1994-2000.
4 *
5 * Heap printer
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "rts/Bytecodes.h" /* for InstrPtr */
12
13 #include "sm/Storage.h"
14 #include "Printer.h"
15 #include "RtsUtils.h"
16
17 #include <string.h>
18
19 #ifdef DEBUG
20
21 #include "Disassembler.h"
22 #include "Apply.h"
23
24 /* --------------------------------------------------------------------------
25 * local function decls
26 * ------------------------------------------------------------------------*/
27
28 static void printStdObjPayload( StgClosure *obj );
29 #ifdef USING_LIBBFD
30 static void reset_table ( int size );
31 static void prepare_table ( void );
32 static void insert ( StgWord value, const char *name );
33 #endif
34 #if 0 /* unused but might be useful sometime */
35 static rtsBool lookup_name ( char *name, StgWord *result );
36 #endif
37
38 /* --------------------------------------------------------------------------
39 * Printer
40 * ------------------------------------------------------------------------*/
41
42 void printPtr( StgPtr p )
43 {
44 const char *raw;
45 raw = lookupGHCName(p);
46 if (raw != NULL) {
47 debugBelch("<%s>", raw);
48 debugBelch("[%p]", p);
49 } else {
50 debugBelch("%p", p);
51 }
52 }
53
54 void printObj( StgClosure *obj )
55 {
56 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
57 printClosure(obj);
58 }
59
60 STATIC_INLINE void
61 printStdObjHdr( StgClosure *obj, char* tag )
62 {
63 debugBelch("%s(",tag);
64 printPtr((StgPtr)obj->header.info);
65 #ifdef PROFILING
66 debugBelch(", %s", obj->header.prof.ccs->cc->label);
67 #endif
68 }
69
70 static void
71 printStdObjPayload( StgClosure *obj )
72 {
73 StgWord i, j;
74 const StgInfoTable* info;
75
76 info = get_itbl(obj);
77 for (i = 0; i < info->layout.payload.ptrs; ++i) {
78 debugBelch(", ");
79 printPtr((StgPtr)obj->payload[i]);
80 }
81 for (j = 0; j < info->layout.payload.nptrs; ++j) {
82 debugBelch(", %pd#",obj->payload[i+j]);
83 }
84 debugBelch(")\n");
85 }
86
87 static void
88 printThunkPayload( StgThunk *obj )
89 {
90 StgWord i, j;
91 const StgInfoTable* info;
92
93 info = get_itbl((StgClosure *)obj);
94 for (i = 0; i < info->layout.payload.ptrs; ++i) {
95 debugBelch(", ");
96 printPtr((StgPtr)obj->payload[i]);
97 }
98 for (j = 0; j < info->layout.payload.nptrs; ++j) {
99 debugBelch(", %pd#",obj->payload[i+j]);
100 }
101 debugBelch(")\n");
102 }
103
104 static void
105 printThunkObject( StgThunk *obj, char* tag )
106 {
107 printStdObjHdr( (StgClosure *)obj, tag );
108 printThunkPayload( obj );
109 }
110
111 void
112 printClosure( StgClosure *obj )
113 {
114 obj = UNTAG_CLOSURE(obj);
115
116 StgInfoTable *info;
117 info = get_itbl(obj);
118
119 switch ( info->type ) {
120 case INVALID_OBJECT:
121 barf("Invalid object");
122
123 case CONSTR:
124 case CONSTR_1_0: case CONSTR_0_1:
125 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
126 case CONSTR_STATIC:
127 case CONSTR_NOCAF_STATIC:
128 {
129 StgWord i, j;
130 StgConInfoTable *con_info = get_con_itbl (obj);
131
132 debugBelch("%s(", GET_CON_DESC(con_info));
133 for (i = 0; i < info->layout.payload.ptrs; ++i) {
134 if (i != 0) debugBelch(", ");
135 printPtr((StgPtr)obj->payload[i]);
136 }
137 for (j = 0; j < info->layout.payload.nptrs; ++j) {
138 if (i != 0 || j != 0) debugBelch(", ");
139 debugBelch("%p#", obj->payload[i+j]);
140 }
141 debugBelch(")\n");
142 break;
143 }
144
145 case FUN:
146 case FUN_1_0: case FUN_0_1:
147 case FUN_1_1: case FUN_0_2: case FUN_2_0:
148 case FUN_STATIC:
149 debugBelch("FUN/%d(",(int)itbl_to_fun_itbl(info)->f.arity);
150 printPtr((StgPtr)obj->header.info);
151 #ifdef PROFILING
152 debugBelch(", %s", obj->header.prof.ccs->cc->label);
153 #endif
154 printStdObjPayload(obj);
155 break;
156
157 case PRIM:
158 debugBelch("PRIM(");
159 printPtr((StgPtr)obj->header.info);
160 printStdObjPayload(obj);
161 break;
162
163 case MUT_PRIM:
164 debugBelch("MUT_PRIM(");
165 printPtr((StgPtr)obj->header.info);
166 printStdObjPayload(obj);
167 break;
168
169 case THUNK:
170 case THUNK_1_0: case THUNK_0_1:
171 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
172 case THUNK_STATIC:
173 /* ToDo: will this work for THUNK_STATIC too? */
174 #ifdef PROFILING
175 printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
176 #else
177 printThunkObject((StgThunk *)obj,"THUNK");
178 #endif
179 break;
180
181 case THUNK_SELECTOR:
182 printStdObjHdr(obj, "THUNK_SELECTOR");
183 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
184 break;
185
186 case BCO:
187 disassemble( (StgBCO*)obj );
188 break;
189
190 case AP:
191 {
192 StgAP* ap = (StgAP*)obj;
193 StgWord i;
194 debugBelch("AP("); printPtr((StgPtr)ap->fun);
195 for (i = 0; i < ap->n_args; ++i) {
196 debugBelch(", ");
197 printPtr((P_)ap->payload[i]);
198 }
199 debugBelch(")\n");
200 break;
201 }
202
203 case PAP:
204 {
205 StgPAP* pap = (StgPAP*)obj;
206 StgWord i;
207 debugBelch("PAP/%d(",(int)pap->arity);
208 printPtr((StgPtr)pap->fun);
209 for (i = 0; i < pap->n_args; ++i) {
210 debugBelch(", ");
211 printPtr((StgPtr)pap->payload[i]);
212 }
213 debugBelch(")\n");
214 break;
215 }
216
217 case AP_STACK:
218 {
219 StgAP_STACK* ap = (StgAP_STACK*)obj;
220 StgWord i;
221 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
222 for (i = 0; i < ap->size; ++i) {
223 debugBelch(", ");
224 printPtr((P_)ap->payload[i]);
225 }
226 debugBelch(")\n");
227 break;
228 }
229
230 case IND:
231 debugBelch("IND(");
232 printPtr((StgPtr)((StgInd*)obj)->indirectee);
233 debugBelch(")\n");
234 break;
235
236 case IND_PERM:
237 debugBelch("IND(");
238 printPtr((StgPtr)((StgInd*)obj)->indirectee);
239 debugBelch(")\n");
240 break;
241
242 case IND_STATIC:
243 debugBelch("IND_STATIC(");
244 printPtr((StgPtr)((StgInd*)obj)->indirectee);
245 debugBelch(")\n");
246 break;
247
248 case BLACKHOLE:
249 debugBelch("BLACKHOLE(");
250 printPtr((StgPtr)((StgInd*)obj)->indirectee);
251 debugBelch(")\n");
252 break;
253
254 /* Cannot happen -- use default case.
255 case RET_BCO:
256 case RET_SMALL:
257 case RET_BIG:
258 case RET_FUN:
259 */
260
261 case UPDATE_FRAME:
262 {
263 StgUpdateFrame* u = (StgUpdateFrame*)obj;
264 debugBelch("%s(", info_update_frame(obj));
265 printPtr((StgPtr)GET_INFO((StgClosure *)u));
266 debugBelch(",");
267 printPtr((StgPtr)u->updatee);
268 debugBelch(")\n");
269 break;
270 }
271
272 case CATCH_FRAME:
273 {
274 StgCatchFrame* u = (StgCatchFrame*)obj;
275 debugBelch("CATCH_FRAME(");
276 printPtr((StgPtr)GET_INFO((StgClosure *)u));
277 debugBelch(",");
278 printPtr((StgPtr)u->handler);
279 debugBelch(")\n");
280 break;
281 }
282
283 case UNDERFLOW_FRAME:
284 {
285 StgUnderflowFrame* u = (StgUnderflowFrame*)obj;
286 debugBelch("UNDERFLOW_FRAME(");
287 printPtr((StgPtr)u->next_chunk);
288 debugBelch(")\n");
289 break;
290 }
291
292 case STOP_FRAME:
293 {
294 StgStopFrame* u = (StgStopFrame*)obj;
295 debugBelch("STOP_FRAME(");
296 printPtr((StgPtr)GET_INFO((StgClosure *)u));
297 debugBelch(")\n");
298 break;
299 }
300
301 case ARR_WORDS:
302 {
303 StgWord i;
304 debugBelch("ARR_WORDS(\"");
305 for (i=0; i<arr_words_words((StgArrWords *)obj); i++)
306 debugBelch("%" FMT_Word, (W_)((StgArrWords *)obj)->payload[i]);
307 debugBelch("\")\n");
308 break;
309 }
310
311 case MUT_ARR_PTRS_CLEAN:
312 debugBelch("MUT_ARR_PTRS_CLEAN(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
313 break;
314
315 case MUT_ARR_PTRS_DIRTY:
316 debugBelch("MUT_ARR_PTRS_DIRTY(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
317 break;
318
319 case MUT_ARR_PTRS_FROZEN:
320 debugBelch("MUT_ARR_PTRS_FROZEN(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
321 break;
322
323 case SMALL_MUT_ARR_PTRS_CLEAN:
324 debugBelch("SMALL_MUT_ARR_PTRS_CLEAN(size=%" FMT_Word ")\n",
325 (W_)((StgSmallMutArrPtrs *)obj)->ptrs);
326 break;
327
328 case SMALL_MUT_ARR_PTRS_DIRTY:
329 debugBelch("SMALL_MUT_ARR_PTRS_DIRTY(size=%" FMT_Word ")\n",
330 (W_)((StgSmallMutArrPtrs *)obj)->ptrs);
331 break;
332
333 case SMALL_MUT_ARR_PTRS_FROZEN:
334 debugBelch("SMALL_MUT_ARR_PTRS_FROZEN(size=%" FMT_Word ")\n",
335 (W_)((StgSmallMutArrPtrs *)obj)->ptrs);
336 break;
337
338 case MVAR_CLEAN:
339 case MVAR_DIRTY:
340 {
341 StgMVar* mv = (StgMVar*)obj;
342 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
343 break;
344 }
345
346 case TVAR:
347 {
348 StgTVar* tv = (StgTVar*)obj;
349 debugBelch("TVAR(value=%p, wq=%p, num_updates=%" FMT_Word ")\n", tv->current_value, tv->first_watch_queue_entry, tv->num_updates);
350 break;
351 }
352
353 case MUT_VAR_CLEAN:
354 {
355 StgMutVar* mv = (StgMutVar*)obj;
356 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
357 break;
358 }
359
360 case MUT_VAR_DIRTY:
361 {
362 StgMutVar* mv = (StgMutVar*)obj;
363 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
364 break;
365 }
366
367 case WEAK:
368 debugBelch("WEAK(");
369 debugBelch(" key=%p value=%p finalizer=%p",
370 (StgPtr)(((StgWeak*)obj)->key),
371 (StgPtr)(((StgWeak*)obj)->value),
372 (StgPtr)(((StgWeak*)obj)->finalizer));
373 debugBelch(")\n");
374 /* ToDo: chase 'link' ? */
375 break;
376
377 case TSO:
378 debugBelch("TSO(");
379 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
380 debugBelch(")\n");
381 break;
382
383 case STACK:
384 debugBelch("STACK");
385 break;
386
387 #if 0
388 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
389 case EVACUATED:
390 debugBelch("EVACUATED(");
391 printClosure((StgEvacuated*)obj->evacuee);
392 debugBelch(")\n");
393 break;
394 #endif
395
396 default:
397 //barf("printClosure %d",get_itbl(obj)->type);
398 debugBelch("*** printClosure: unknown type %d ****\n",
399 (int)get_itbl(obj)->type );
400 barf("printClosure %d",get_itbl(obj)->type);
401 return;
402 }
403 }
404
405 // If you know you have an UPDATE_FRAME, but want to know exactly which.
406 char *info_update_frame(StgClosure *closure) {
407 // Note: We intentionally don't take the info table pointer as
408 // an argument. As it will be confusing whether one should pass
409 // it pointing to the code or struct members when compiling with
410 // TABLES_NEXT_TO_CODE.
411 const StgInfoTable *info = closure->header.info;
412 if (info == &stg_upd_frame_info) {
413 return "NORMAL_UPDATE_FRAME";
414 } else if (info == &stg_bh_upd_frame_info) {
415 return "BH_UPDATE_FRAME";
416 } else if (info == &stg_marked_upd_frame_info) {
417 return "MARKED_UPDATE_FRAME";
418 } else {
419 return "ERROR: Not an update frame!!!";
420 }
421 }
422
423 /*
424 void printGraph( StgClosure *obj )
425 {
426 printClosure(obj);
427 }
428 */
429
430 StgPtr
431 printStackObj( StgPtr sp )
432 {
433 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
434
435 StgClosure* c = (StgClosure*)(*sp);
436 printPtr((StgPtr)*sp);
437 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
438 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
439 } else
440 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
441 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
442 } else
443 if (c == (StgClosure*)&stg_ctoi_F1_info) {
444 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
445 } else
446 if (c == (StgClosure*)&stg_ctoi_D1_info) {
447 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
448 } else
449 if (c == (StgClosure*)&stg_ctoi_V_info) {
450 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
451 } else
452 if (get_itbl(c)->type == BCO) {
453 debugBelch("\t\t\t");
454 debugBelch("BCO(...)\n");
455 }
456 else {
457 debugBelch("\t\t\t");
458 printClosure ( (StgClosure*)(*sp));
459 }
460 sp += 1;
461
462 return sp;
463
464 }
465
466 static void
467 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap, nat size )
468 {
469 nat i;
470
471 for(i = 0; i < size; i++, bitmap >>= 1 ) {
472 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
473 if ((bitmap & 1) == 0) {
474 printPtr((P_)payload[i]);
475 debugBelch("\n");
476 } else {
477 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
478 }
479 }
480 }
481
482 static void
483 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap, nat size )
484 {
485 StgWord bmp;
486 nat i, j;
487
488 i = 0;
489 for (bmp=0; i < size; bmp++) {
490 StgWord bitmap = large_bitmap->bitmap[bmp];
491 j = 0;
492 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
493 debugBelch(" stk[%" FMT_Word "] (%p) = ", (W_)(spBottom-(payload+i)), payload+i);
494 if ((bitmap & 1) == 0) {
495 printPtr((P_)payload[i]);
496 debugBelch("\n");
497 } else {
498 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
499 }
500 }
501 }
502 }
503
504 void
505 printStackChunk( StgPtr sp, StgPtr spBottom )
506 {
507 StgWord bitmap;
508 const StgInfoTable *info;
509
510 ASSERT(sp <= spBottom);
511 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
512
513 info = get_itbl((StgClosure *)sp);
514
515 switch (info->type) {
516
517 case UPDATE_FRAME:
518 case CATCH_FRAME:
519 case UNDERFLOW_FRAME:
520 case STOP_FRAME:
521 printObj((StgClosure*)sp);
522 continue;
523
524 case RET_SMALL:
525 debugBelch("RET_SMALL (%p)\n", info);
526 bitmap = info->layout.bitmap;
527 printSmallBitmap(spBottom, sp+1,
528 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
529 continue;
530
531 case RET_BCO: {
532 StgBCO *bco;
533
534 bco = ((StgBCO *)sp[1]);
535
536 debugBelch("RET_BCO (%p)\n", sp);
537 printLargeBitmap(spBottom, sp+2,
538 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
539 continue;
540 }
541
542 case RET_BIG:
543 barf("todo");
544
545 case RET_FUN:
546 {
547 StgFunInfoTable *fun_info;
548 StgRetFun *ret_fun;
549
550 ret_fun = (StgRetFun *)sp;
551 fun_info = get_fun_itbl(ret_fun->fun);
552 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, (int)fun_info->f.fun_type);
553 switch (fun_info->f.fun_type) {
554 case ARG_GEN:
555 printSmallBitmap(spBottom, sp+2,
556 BITMAP_BITS(fun_info->f.b.bitmap),
557 BITMAP_SIZE(fun_info->f.b.bitmap));
558 break;
559 case ARG_GEN_BIG:
560 printLargeBitmap(spBottom, sp+2,
561 GET_FUN_LARGE_BITMAP(fun_info),
562 GET_FUN_LARGE_BITMAP(fun_info)->size);
563 break;
564 default:
565 printSmallBitmap(spBottom, sp+2,
566 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
567 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
568 break;
569 }
570 continue;
571 }
572
573 default:
574 debugBelch("unknown object %d\n", (int)info->type);
575 barf("printStackChunk");
576 }
577 }
578 }
579
580 void printTSO( StgTSO *tso )
581 {
582 printStackChunk( tso->stackobj->sp,
583 tso->stackobj->stack+tso->stackobj->stack_size);
584 }
585
586 /* --------------------------------------------------------------------------
587 * Address printing code
588 *
589 * Uses symbol table in (unstripped executable)
590 * ------------------------------------------------------------------------*/
591
592 /* --------------------------------------------------------------------------
593 * Simple lookup table
594 *
595 * Current implementation is pretty dumb!
596 * ------------------------------------------------------------------------*/
597
598 struct entry {
599 StgWord value;
600 const char *name;
601 };
602
603 static nat table_size;
604 static struct entry* table;
605
606 #ifdef USING_LIBBFD
607 static nat max_table_size;
608
609 static void reset_table( int size )
610 {
611 max_table_size = size;
612 table_size = 0;
613 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
614 }
615
616 static void prepare_table( void )
617 {
618 /* Could sort it... */
619 }
620
621 static void insert( StgWord value, const char *name )
622 {
623 if ( table_size >= max_table_size ) {
624 barf( "Symbol table overflow\n" );
625 }
626 table[table_size].value = value;
627 table[table_size].name = name;
628 table_size = table_size + 1;
629 }
630 #endif
631
632 #if 0
633 static rtsBool lookup_name( char *name, StgWord *result )
634 {
635 nat i;
636 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
637 }
638 if (i < table_size) {
639 *result = table[i].value;
640 return rtsTrue;
641 } else {
642 return rtsFalse;
643 }
644 }
645 #endif
646
647 const char *lookupGHCName( void *addr )
648 {
649 nat i;
650 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
651 }
652 if (i < table_size) {
653 return table[i].name;
654 } else {
655 return NULL;
656 }
657 }
658
659 /* --------------------------------------------------------------------------
660 * Symbol table loading
661 * ------------------------------------------------------------------------*/
662
663 /* Causing linking trouble on Win32 plats, so I'm
664 disabling this for now.
665 */
666 #ifdef USING_LIBBFD
667
668 #include <bfd.h>
669
670 /* Fairly ad-hoc piece of code that seems to filter out a lot of
671 * rubbish like the obj-splitting symbols
672 */
673
674 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
675 {
676 #if 0
677 /* ToDo: make this work on BFD */
678 int tp = type & N_TYPE;
679 if (tp == N_TEXT || tp == N_DATA) {
680 return (name[0] == '_' && name[1] != '_');
681 } else {
682 return rtsFalse;
683 }
684 #else
685 if (*name == '\0' ||
686 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
687 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
688 return rtsFalse;
689 }
690 return rtsTrue;
691 #endif
692 }
693
694 extern void DEBUG_LoadSymbols( char *name )
695 {
696 bfd* abfd;
697 char **matching;
698
699 bfd_init();
700 abfd = bfd_openr(name, "default");
701 if (abfd == NULL) {
702 barf("can't open executable %s to get symbol table", name);
703 }
704 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
705 barf("mismatch");
706 }
707
708 {
709 long storage_needed;
710 asymbol **symbol_table;
711 long number_of_symbols;
712 long num_real_syms = 0;
713 long i;
714
715 storage_needed = bfd_get_symtab_upper_bound (abfd);
716
717 if (storage_needed < 0) {
718 barf("can't read symbol table");
719 }
720 #if 0
721 if (storage_needed == 0) {
722 debugBelch("no storage needed");
723 }
724 #endif
725 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
726
727 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
728
729 if (number_of_symbols < 0) {
730 barf("can't canonicalise symbol table");
731 }
732
733 for( i = 0; i != number_of_symbols; ++i ) {
734 symbol_info info;
735 bfd_get_symbol_info(abfd,symbol_table[i],&info);
736 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
737 if (isReal(info.type, info.name)) {
738 num_real_syms += 1;
739 }
740 }
741
742 IF_DEBUG(interpreter,
743 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
744 number_of_symbols, num_real_syms)
745 );
746
747 reset_table( num_real_syms );
748
749 for( i = 0; i != number_of_symbols; ++i ) {
750 symbol_info info;
751 bfd_get_symbol_info(abfd,symbol_table[i],&info);
752 if (isReal(info.type, info.name)) {
753 insert( info.value, info.name );
754 }
755 }
756
757 stgFree(symbol_table);
758 }
759 prepare_table();
760 }
761
762 #else /* HAVE_BFD_H */
763
764 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
765 {
766 /* nothing, yet */
767 }
768
769 #endif /* HAVE_BFD_H */
770
771 void findPtr(P_ p, int); /* keep gcc -Wall happy */
772
773 int searched = 0;
774
775 static int
776 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
777 {
778 StgPtr q, r, end;
779 for (; bd; bd = bd->link) {
780 searched++;
781 for (q = bd->start; q < bd->free; q++) {
782 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
783 if (i < arr_size) {
784 for (r = bd->start; r < bd->free; r = end) {
785 // skip over zeroed-out slop
786 while (*r == 0) r++;
787 if (!LOOKS_LIKE_CLOSURE_PTR(r)) {
788 debugBelch("%p found at %p, no closure at %p\n",
789 p, q, r);
790 break;
791 }
792 end = r + closure_sizeW((StgClosure*)r);
793 if (q < end) {
794 debugBelch("%p = ", r);
795 printClosure((StgClosure *)r);
796 arr[i++] = r;
797 break;
798 }
799 }
800 if (r >= bd->free) {
801 debugBelch("%p found at %p, closure?", p, q);
802 }
803 } else {
804 return i;
805 }
806 }
807 }
808 }
809 return i;
810 }
811
812 void
813 findPtr(P_ p, int follow)
814 {
815 nat g, n;
816 bdescr *bd;
817 const int arr_size = 1024;
818 StgPtr arr[arr_size];
819 int i = 0;
820 searched = 0;
821
822 for (n = 0; n < n_capabilities; n++) {
823 bd = nurseries[i].blocks;
824 i = findPtrBlocks(p,bd,arr,arr_size,i);
825 if (i >= arr_size) return;
826 }
827
828 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
829 bd = generations[g].blocks;
830 i = findPtrBlocks(p,bd,arr,arr_size,i);
831 bd = generations[g].large_objects;
832 i = findPtrBlocks(p,bd,arr,arr_size,i);
833 if (i >= arr_size) return;
834 }
835 if (follow && i == 1) {
836 debugBelch("-->\n");
837 findPtr(arr[0], 1);
838 }
839 }
840
841 /* prettyPrintClosure() is for printing out a closure using the data constructor
842 names found in the info tables. Closures are printed in a fashion that resembles
843 their Haskell representation. Useful during debugging.
844
845 Todo: support for more closure types, and support for non pointer fields in the
846 payload.
847 */
848
849 void prettyPrintClosure_ (StgClosure *);
850
851 void prettyPrintClosure (StgClosure *obj)
852 {
853 prettyPrintClosure_ (obj);
854 debugBelch ("\n");
855 }
856
857 void prettyPrintClosure_ (StgClosure *obj)
858 {
859 StgInfoTable *info;
860 StgConInfoTable *con_info;
861
862 /* collapse any indirections */
863 unsigned int type;
864 type = get_itbl(obj)->type;
865
866 while (type == IND ||
867 type == IND_STATIC ||
868 type == IND_PERM)
869 {
870 obj = ((StgInd *)obj)->indirectee;
871 type = get_itbl(obj)->type;
872 }
873
874 /* find the info table for this object */
875 info = get_itbl(obj);
876
877 /* determine what kind of object we have */
878 switch (info->type)
879 {
880 /* full applications of data constructors */
881 case CONSTR:
882 case CONSTR_1_0:
883 case CONSTR_0_1:
884 case CONSTR_1_1:
885 case CONSTR_0_2:
886 case CONSTR_2_0:
887 case CONSTR_STATIC:
888 case CONSTR_NOCAF_STATIC:
889 {
890 nat i;
891 char *descriptor;
892
893 /* find the con_info for the constructor */
894 con_info = get_con_itbl (obj);
895
896 /* obtain the name of the constructor */
897 descriptor = GET_CON_DESC(con_info);
898
899 debugBelch ("(%s", descriptor);
900
901 /* process the payload of the closure */
902 /* we don't handle non pointers at the moment */
903 for (i = 0; i < info->layout.payload.ptrs; i++)
904 {
905 debugBelch (" ");
906 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
907 }
908 debugBelch (")");
909 break;
910 }
911
912 /* if it isn't a constructor then just print the closure type */
913 default:
914 {
915 debugBelch ("<%s>", info_type(obj));
916 break;
917 }
918 }
919 }
920
921 char *what_next_strs[] = {
922 [0] = "(unknown)",
923 [ThreadRunGHC] = "ThreadRunGHC",
924 [ThreadInterpret] = "ThreadInterpret",
925 [ThreadKilled] = "ThreadKilled",
926 [ThreadComplete] = "ThreadComplete"
927 };
928
929 #else /* DEBUG */
930 void printPtr( StgPtr p )
931 {
932 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
933 }
934
935 void printObj( StgClosure *obj )
936 {
937 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
938 }
939
940
941 #endif /* DEBUG */
942
943 /* -----------------------------------------------------------------------------
944 Closure types
945
946 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
947 -------------------------------------------------------------------------- */
948
949 char *closure_type_names[] = {
950 [INVALID_OBJECT] = "INVALID_OBJECT",
951 [CONSTR] = "CONSTR",
952 [CONSTR_1_0] = "CONSTR_1_0",
953 [CONSTR_0_1] = "CONSTR_0_1",
954 [CONSTR_2_0] = "CONSTR_2_0",
955 [CONSTR_1_1] = "CONSTR_1_1",
956 [CONSTR_0_2] = "CONSTR_0_2",
957 [CONSTR_STATIC] = "CONSTR_STATIC",
958 [CONSTR_NOCAF_STATIC] = "CONSTR_NOCAF_STATIC",
959 [FUN] = "FUN",
960 [FUN_1_0] = "FUN_1_0",
961 [FUN_0_1] = "FUN_0_1",
962 [FUN_2_0] = "FUN_2_0",
963 [FUN_1_1] = "FUN_1_1",
964 [FUN_0_2] = "FUN_0_2",
965 [FUN_STATIC] = "FUN_STATIC",
966 [THUNK] = "THUNK",
967 [THUNK_1_0] = "THUNK_1_0",
968 [THUNK_0_1] = "THUNK_0_1",
969 [THUNK_2_0] = "THUNK_2_0",
970 [THUNK_1_1] = "THUNK_1_1",
971 [THUNK_0_2] = "THUNK_0_2",
972 [THUNK_STATIC] = "THUNK_STATIC",
973 [THUNK_SELECTOR] = "THUNK_SELECTOR",
974 [BCO] = "BCO",
975 [AP] = "AP",
976 [PAP] = "PAP",
977 [AP_STACK] = "AP_STACK",
978 [IND] = "IND",
979 [IND_PERM] = "IND_PERM",
980 [IND_STATIC] = "IND_STATIC",
981 [RET_BCO] = "RET_BCO",
982 [RET_SMALL] = "RET_SMALL",
983 [RET_BIG] = "RET_BIG",
984 [RET_FUN] = "RET_FUN",
985 [UPDATE_FRAME] = "UPDATE_FRAME",
986 [CATCH_FRAME] = "CATCH_FRAME",
987 [UNDERFLOW_FRAME] = "UNDERFLOW_FRAME",
988 [STOP_FRAME] = "STOP_FRAME",
989 [BLOCKING_QUEUE] = "BLOCKING_QUEUE",
990 [BLACKHOLE] = "BLACKHOLE",
991 [MVAR_CLEAN] = "MVAR_CLEAN",
992 [MVAR_DIRTY] = "MVAR_DIRTY",
993 [TVAR] = "TVAR",
994 [ARR_WORDS] = "ARR_WORDS",
995 [MUT_ARR_PTRS_CLEAN] = "MUT_ARR_PTRS_CLEAN",
996 [MUT_ARR_PTRS_DIRTY] = "MUT_ARR_PTRS_DIRTY",
997 [MUT_ARR_PTRS_FROZEN0] = "MUT_ARR_PTRS_FROZEN0",
998 [MUT_ARR_PTRS_FROZEN] = "MUT_ARR_PTRS_FROZEN",
999 [MUT_VAR_CLEAN] = "MUT_VAR_CLEAN",
1000 [MUT_VAR_DIRTY] = "MUT_VAR_DIRTY",
1001 [WEAK] = "WEAK",
1002 [PRIM] = "PRIM",
1003 [MUT_PRIM] = "MUT_PRIM",
1004 [TSO] = "TSO",
1005 [STACK] = "STACK",
1006 [TREC_CHUNK] = "TREC_CHUNK",
1007 [ATOMICALLY_FRAME] = "ATOMICALLY_FRAME",
1008 [CATCH_RETRY_FRAME] = "CATCH_RETRY_FRAME",
1009 [CATCH_STM_FRAME] = "CATCH_STM_FRAME",
1010 [WHITEHOLE] = "WHITEHOLE"
1011 };
1012
1013 char *
1014 info_type(StgClosure *closure){
1015 return closure_type_names[get_itbl(closure)->type];
1016 }
1017
1018 char *
1019 info_type_by_ip(StgInfoTable *ip){
1020 return closure_type_names[ip->type];
1021 }
1022
1023 void
1024 info_hdr_type(StgClosure *closure, char *res){
1025 strcpy(res,closure_type_names[get_itbl(closure)->type]);
1026 }
1027
1028
1029 // Local Variables:
1030 // mode: C
1031 // fill-column: 80
1032 // indent-tabs-mode: nil
1033 // c-basic-offset: 4
1034 // buffer-file-coding-system: utf-8-unix
1035 // End: