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