Add a forgotten newline in a debug print
[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 * 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 #if defined(PROFILING)
546 } else if (c == (StgWord)&stg_restore_cccs_info) {
547 debugBelch("stg_restore_cccs_info\n" );
548 fprintCCS(stderr, (CostCentreStack*)sp[1]);
549 debugBelch("\n" );
550 continue;
551 } else if (c == (StgWord)&stg_restore_cccs_eval_info) {
552 debugBelch("stg_restore_cccs_eval_info\n" );
553 fprintCCS(stderr, (CostCentreStack*)sp[1]);
554 debugBelch("\n" );
555 continue;
556 #endif
557 } else {
558 debugBelch("RET_SMALL (%p)\n", info);
559 }
560 bitmap = info->layout.bitmap;
561 printSmallBitmap(spBottom, sp+1,
562 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
563 continue;
564 }
565
566 case RET_BCO: {
567 StgBCO *bco;
568
569 bco = ((StgBCO *)sp[1]);
570
571 debugBelch("RET_BCO (%p)\n", sp);
572 printLargeBitmap(spBottom, sp+2,
573 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
574 continue;
575 }
576
577 case RET_BIG:
578 barf("todo");
579
580 case RET_FUN:
581 {
582 const StgFunInfoTable *fun_info;
583 StgRetFun *ret_fun;
584
585 ret_fun = (StgRetFun *)sp;
586 fun_info = get_fun_itbl(ret_fun->fun);
587 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, (int)fun_info->f.fun_type);
588 switch (fun_info->f.fun_type) {
589 case ARG_GEN:
590 printSmallBitmap(spBottom, sp+2,
591 BITMAP_BITS(fun_info->f.b.bitmap),
592 BITMAP_SIZE(fun_info->f.b.bitmap));
593 break;
594 case ARG_GEN_BIG:
595 printLargeBitmap(spBottom, sp+2,
596 GET_FUN_LARGE_BITMAP(fun_info),
597 GET_FUN_LARGE_BITMAP(fun_info)->size);
598 break;
599 default:
600 printSmallBitmap(spBottom, sp+2,
601 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
602 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
603 break;
604 }
605 continue;
606 }
607
608 default:
609 debugBelch("unknown object %d\n", (int)info->type);
610 barf("printStackChunk");
611 }
612 }
613 }
614
615 void printTSO( StgTSO *tso )
616 {
617 printStackChunk( tso->stackobj->sp,
618 tso->stackobj->stack+tso->stackobj->stack_size);
619 }
620
621 /* --------------------------------------------------------------------------
622 * Address printing code
623 *
624 * Uses symbol table in (unstripped executable)
625 * ------------------------------------------------------------------------*/
626
627 /* --------------------------------------------------------------------------
628 * Simple lookup table
629 * address -> function name
630 * ------------------------------------------------------------------------*/
631
632 static HashTable * add_to_fname_table = NULL;
633
634 const char *lookupGHCName( void *addr )
635 {
636 if (add_to_fname_table == NULL)
637 return NULL;
638
639 return lookupHashTable(add_to_fname_table, (StgWord)addr);
640 }
641
642 /* --------------------------------------------------------------------------
643 * Symbol table loading
644 * ------------------------------------------------------------------------*/
645
646 /* Causing linking trouble on Win32 plats, so I'm
647 disabling this for now.
648 */
649 #if defined(USING_LIBBFD)
650 # define PACKAGE 1
651 # define PACKAGE_VERSION 1
652 /* Those PACKAGE_* defines are workarounds for bfd:
653 * https://sourceware.org/bugzilla/show_bug.cgi?id=14243
654 * ghc's build system filter PACKAGE_* values out specifically to avoid clashes
655 * with user's autoconf-based Cabal packages.
656 * It's a shame <bfd.h> checks for unrelated fields instead of actually used
657 * macros.
658 */
659 # include <bfd.h>
660
661 /* Fairly ad-hoc piece of code that seems to filter out a lot of
662 * rubbish like the obj-splitting symbols
663 */
664
665 static bool isReal( flagword flags STG_UNUSED, const char *name )
666 {
667 #if 0
668 /* ToDo: make this work on BFD */
669 int tp = type & N_TYPE;
670 if (tp == N_TEXT || tp == N_DATA) {
671 return (name[0] == '_' && name[1] != '_');
672 } else {
673 return false;
674 }
675 #else
676 if (*name == '\0' ||
677 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
678 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
679 return false;
680 }
681 return true;
682 #endif
683 }
684
685 extern void DEBUG_LoadSymbols( const char *name )
686 {
687 bfd* abfd;
688 char **matching;
689
690 bfd_init();
691 abfd = bfd_openr(name, "default");
692 if (abfd == NULL) {
693 barf("can't open executable %s to get symbol table", name);
694 }
695 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
696 barf("mismatch");
697 }
698
699 {
700 long storage_needed;
701 asymbol **symbol_table;
702 long number_of_symbols;
703 long num_real_syms = 0;
704 long i;
705
706 storage_needed = bfd_get_symtab_upper_bound (abfd);
707
708 if (storage_needed < 0) {
709 barf("can't read symbol table");
710 }
711 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
712
713 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
714
715 if (number_of_symbols < 0) {
716 barf("can't canonicalise symbol table");
717 }
718
719 if (add_to_fname_table == NULL)
720 add_to_fname_table = allocHashTable();
721
722 for( i = 0; i != number_of_symbols; ++i ) {
723 symbol_info info;
724 bfd_get_symbol_info(abfd,symbol_table[i],&info);
725 if (isReal(info.type, info.name)) {
726 insertHashTable(add_to_fname_table,
727 info.value, (void*)info.name);
728 num_real_syms += 1;
729 }
730 }
731
732 IF_DEBUG(interpreter,
733 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
734 number_of_symbols, num_real_syms)
735 );
736
737 stgFree(symbol_table);
738 }
739 }
740
741 #else /* USING_LIBBFD */
742
743 extern void DEBUG_LoadSymbols( const char *name STG_UNUSED )
744 {
745 /* nothing, yet */
746 }
747
748 #endif /* USING_LIBBFD */
749
750 void findPtr(P_ p, int); /* keep gcc -Wall happy */
751
752 int searched = 0;
753
754 static int
755 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
756 {
757 StgPtr q, r, end;
758 for (; bd; bd = bd->link) {
759 searched++;
760 for (q = bd->start; q < bd->free; q++) {
761 if (UNTAG_CONST_CLOSURE((StgClosure*)*q) == (const StgClosure *)p) {
762 if (i < arr_size) {
763 for (r = bd->start; r < bd->free; r = end) {
764 // skip over zeroed-out slop
765 while (*r == 0) r++;
766 if (!LOOKS_LIKE_CLOSURE_PTR(r)) {
767 debugBelch("%p found at %p, no closure at %p\n",
768 p, q, r);
769 break;
770 }
771 end = r + closure_sizeW((StgClosure*)r);
772 if (q < end) {
773 debugBelch("%p = ", r);
774 printClosure((StgClosure *)r);
775 arr[i++] = r;
776 break;
777 }
778 }
779 if (r >= bd->free) {
780 debugBelch("%p found at %p, closure?", p, q);
781 }
782 } else {
783 return i;
784 }
785 }
786 }
787 }
788 return i;
789 }
790
791 void
792 findPtr(P_ p, int follow)
793 {
794 uint32_t g, n;
795 bdescr *bd;
796 const int arr_size = 1024;
797 StgPtr arr[arr_size];
798 int i = 0;
799 searched = 0;
800
801 for (n = 0; n < n_capabilities; n++) {
802 bd = nurseries[i].blocks;
803 i = findPtrBlocks(p,bd,arr,arr_size,i);
804 if (i >= arr_size) return;
805 }
806
807 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
808 bd = generations[g].blocks;
809 i = findPtrBlocks(p,bd,arr,arr_size,i);
810 bd = generations[g].large_objects;
811 i = findPtrBlocks(p,bd,arr,arr_size,i);
812 if (i >= arr_size) return;
813 }
814 if (follow && i == 1) {
815 debugBelch("-->\n");
816 findPtr(arr[0], 1);
817 }
818 }
819
820 const char *what_next_strs[] = {
821 [0] = "(unknown)",
822 [ThreadRunGHC] = "ThreadRunGHC",
823 [ThreadInterpret] = "ThreadInterpret",
824 [ThreadKilled] = "ThreadKilled",
825 [ThreadComplete] = "ThreadComplete"
826 };
827
828 #else /* DEBUG */
829 void printPtr( StgPtr p )
830 {
831 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
832 }
833
834 void printObj( StgClosure *obj )
835 {
836 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
837 }
838
839
840 #endif /* DEBUG */
841
842 /* -----------------------------------------------------------------------------
843 Closure types
844
845 NOTE: must be kept in sync with the closure types in
846 includes/rts/storage/ClosureTypes.h
847 -------------------------------------------------------------------------- */
848
849 const char *closure_type_names[] = {
850 [INVALID_OBJECT] = "INVALID_OBJECT",
851 [CONSTR] = "CONSTR",
852 [CONSTR_1_0] = "CONSTR_1_0",
853 [CONSTR_0_1] = "CONSTR_0_1",
854 [CONSTR_2_0] = "CONSTR_2_0",
855 [CONSTR_1_1] = "CONSTR_1_1",
856 [CONSTR_0_2] = "CONSTR_0_2",
857 [CONSTR_NOCAF] = "CONSTR_NOCAF",
858 [FUN] = "FUN",
859 [FUN_1_0] = "FUN_1_0",
860 [FUN_0_1] = "FUN_0_1",
861 [FUN_2_0] = "FUN_2_0",
862 [FUN_1_1] = "FUN_1_1",
863 [FUN_0_2] = "FUN_0_2",
864 [FUN_STATIC] = "FUN_STATIC",
865 [THUNK] = "THUNK",
866 [THUNK_1_0] = "THUNK_1_0",
867 [THUNK_0_1] = "THUNK_0_1",
868 [THUNK_2_0] = "THUNK_2_0",
869 [THUNK_1_1] = "THUNK_1_1",
870 [THUNK_0_2] = "THUNK_0_2",
871 [THUNK_STATIC] = "THUNK_STATIC",
872 [THUNK_SELECTOR] = "THUNK_SELECTOR",
873 [BCO] = "BCO",
874 [AP] = "AP",
875 [PAP] = "PAP",
876 [AP_STACK] = "AP_STACK",
877 [IND] = "IND",
878 [IND_STATIC] = "IND_STATIC",
879 [RET_BCO] = "RET_BCO",
880 [RET_SMALL] = "RET_SMALL",
881 [RET_BIG] = "RET_BIG",
882 [RET_FUN] = "RET_FUN",
883 [UPDATE_FRAME] = "UPDATE_FRAME",
884 [CATCH_FRAME] = "CATCH_FRAME",
885 [UNDERFLOW_FRAME] = "UNDERFLOW_FRAME",
886 [STOP_FRAME] = "STOP_FRAME",
887 [BLOCKING_QUEUE] = "BLOCKING_QUEUE",
888 [BLACKHOLE] = "BLACKHOLE",
889 [MVAR_CLEAN] = "MVAR_CLEAN",
890 [MVAR_DIRTY] = "MVAR_DIRTY",
891 [TVAR] = "TVAR",
892 [ARR_WORDS] = "ARR_WORDS",
893 [MUT_ARR_PTRS_CLEAN] = "MUT_ARR_PTRS_CLEAN",
894 [MUT_ARR_PTRS_DIRTY] = "MUT_ARR_PTRS_DIRTY",
895 [MUT_ARR_PTRS_FROZEN0] = "MUT_ARR_PTRS_FROZEN0",
896 [MUT_ARR_PTRS_FROZEN] = "MUT_ARR_PTRS_FROZEN",
897 [MUT_VAR_CLEAN] = "MUT_VAR_CLEAN",
898 [MUT_VAR_DIRTY] = "MUT_VAR_DIRTY",
899 [WEAK] = "WEAK",
900 [PRIM] = "PRIM",
901 [MUT_PRIM] = "MUT_PRIM",
902 [TSO] = "TSO",
903 [STACK] = "STACK",
904 [TREC_CHUNK] = "TREC_CHUNK",
905 [ATOMICALLY_FRAME] = "ATOMICALLY_FRAME",
906 [CATCH_RETRY_FRAME] = "CATCH_RETRY_FRAME",
907 [CATCH_STM_FRAME] = "CATCH_STM_FRAME",
908 [WHITEHOLE] = "WHITEHOLE",
909 [SMALL_MUT_ARR_PTRS_CLEAN] = "SMALL_MUT_ARR_PTRS_CLEAN",
910 [SMALL_MUT_ARR_PTRS_DIRTY] = "SMALL_MUT_ARR_PTRS_DIRTY",
911 [SMALL_MUT_ARR_PTRS_FROZEN0] = "SMALL_MUT_ARR_PTRS_FROZEN0",
912 [SMALL_MUT_ARR_PTRS_FROZEN] = "SMALL_MUT_ARR_PTRS_FROZEN",
913 [COMPACT_NFDATA] = "COMPACT_NFDATA"
914 };
915
916 #if N_CLOSURE_TYPES != 64
917 #error Closure types changed: update Printer.c!
918 #endif
919
920 const char *
921 info_type(const StgClosure *closure){
922 return closure_type_names[get_itbl(closure)->type];
923 }
924
925 const char *
926 info_type_by_ip(const StgInfoTable *ip){
927 return closure_type_names[ip->type];
928 }
929
930 void
931 info_hdr_type(const StgClosure *closure, char *res){
932 strcpy(res,closure_type_names[get_itbl(closure)->type]);
933 }