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