Make start address of `osReserveHeapMemory` tunable via command line -xb
[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 #ifdef PROFILING
21 #include "Profiling.h"
22 #endif
23
24 #include <string.h>
25
26 #ifdef 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 #ifdef 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_STATIC:
126 case CONSTR_NOCAF_STATIC:
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 #ifdef 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 #ifdef 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:
313 debugBelch("MUT_ARR_PTRS_FROZEN(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:
327 debugBelch("SMALL_MUT_ARR_PTRS_FROZEN(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 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
336 break;
337 }
338
339 case TVAR:
340 {
341 StgTVar* tv = (StgTVar*)obj;
342 debugBelch("TVAR(value=%p, wq=%p, num_updates=%" FMT_Word ")\n", tv->current_value, tv->first_watch_queue_entry, tv->num_updates);
343 break;
344 }
345
346 case MUT_VAR_CLEAN:
347 {
348 StgMutVar* mv = (StgMutVar*)obj;
349 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
350 break;
351 }
352
353 case MUT_VAR_DIRTY:
354 {
355 StgMutVar* mv = (StgMutVar*)obj;
356 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
357 break;
358 }
359
360 case WEAK:
361 debugBelch("WEAK(");
362 debugBelch(" key=%p value=%p finalizer=%p",
363 (StgPtr)(((StgWeak*)obj)->key),
364 (StgPtr)(((StgWeak*)obj)->value),
365 (StgPtr)(((StgWeak*)obj)->finalizer));
366 debugBelch(")\n");
367 /* ToDo: chase 'link' ? */
368 break;
369
370 case TSO:
371 debugBelch("TSO(");
372 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
373 debugBelch(")\n");
374 break;
375
376 case STACK:
377 debugBelch("STACK");
378 break;
379
380 #if 0
381 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
382 case EVACUATED:
383 debugBelch("EVACUATED(");
384 printClosure((StgEvacuated*)obj->evacuee);
385 debugBelch(")\n");
386 break;
387 #endif
388
389 case COMPACT_NFDATA:
390 debugBelch("COMPACT_NFDATA(size=%" FMT_Word ")\n",
391 (W_)((StgCompactNFData *)obj)->totalDataW * sizeof(W_));
392 break;
393
394
395 default:
396 //barf("printClosure %d",get_itbl(obj)->type);
397 debugBelch("*** printClosure: unknown type %d ****\n",
398 (int)get_itbl(obj)->type );
399 barf("printClosure %d",get_itbl(obj)->type);
400 return;
401 }
402 }
403
404 // If you know you have an UPDATE_FRAME, but want to know exactly which.
405 const char *info_update_frame(const StgClosure *closure)
406 {
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 static void
431 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap,
432 uint32_t size )
433 {
434 uint32_t i;
435
436 for(i = 0; i < size; i++, bitmap >>= 1 ) {
437 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
438 if ((bitmap & 1) == 0) {
439 printPtr((P_)payload[i]);
440 debugBelch("\n");
441 } else {
442 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
443 }
444 }
445 }
446
447 static void
448 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap,
449 uint32_t size )
450 {
451 StgWord bmp;
452 uint32_t i, j;
453
454 i = 0;
455 for (bmp=0; i < size; bmp++) {
456 StgWord bitmap = large_bitmap->bitmap[bmp];
457 j = 0;
458 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
459 debugBelch(" stk[%" FMT_Word "] (%p) = ", (W_)(spBottom-(payload+i)), payload+i);
460 if ((bitmap & 1) == 0) {
461 printPtr((P_)payload[i]);
462 debugBelch("\n");
463 } else {
464 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
465 }
466 }
467 }
468 }
469
470 void
471 printStackChunk( StgPtr sp, StgPtr spBottom )
472 {
473 StgWord bitmap;
474 const StgInfoTable *info;
475
476 ASSERT(sp <= spBottom);
477 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
478
479 info = get_itbl((StgClosure *)sp);
480
481 switch (info->type) {
482
483 case UPDATE_FRAME:
484 case CATCH_FRAME:
485 case UNDERFLOW_FRAME:
486 case STOP_FRAME:
487 printClosure((StgClosure*)sp);
488 continue;
489
490 case RET_SMALL: {
491 StgWord c = *sp;
492 if (c == (StgWord)&stg_ctoi_R1p_info) {
493 debugBelch("tstg_ctoi_ret_R1p_info\n" );
494 } else if (c == (StgWord)&stg_ctoi_R1n_info) {
495 debugBelch("stg_ctoi_ret_R1n_info\n" );
496 } else if (c == (StgWord)&stg_ctoi_F1_info) {
497 debugBelch("stg_ctoi_ret_F1_info\n" );
498 } else if (c == (StgWord)&stg_ctoi_D1_info) {
499 debugBelch("stg_ctoi_ret_D1_info\n" );
500 } else if (c == (StgWord)&stg_ctoi_V_info) {
501 debugBelch("stg_ctoi_ret_V_info\n" );
502 } else if (c == (StgWord)&stg_ap_v_info) {
503 debugBelch("stg_ap_v_info\n" );
504 } else if (c == (StgWord)&stg_ap_f_info) {
505 debugBelch("stg_ap_f_info\n" );
506 } else if (c == (StgWord)&stg_ap_d_info) {
507 debugBelch("stg_ap_d_info\n" );
508 } else if (c == (StgWord)&stg_ap_l_info) {
509 debugBelch("stg_ap_l_info\n" );
510 } else if (c == (StgWord)&stg_ap_n_info) {
511 debugBelch("stg_ap_n_info\n" );
512 } else if (c == (StgWord)&stg_ap_p_info) {
513 debugBelch("stg_ap_p_info\n" );
514 } else if (c == (StgWord)&stg_ap_pp_info) {
515 debugBelch("stg_ap_pp_info\n" );
516 } else if (c == (StgWord)&stg_ap_ppp_info) {
517 debugBelch("stg_ap_ppp_info\n" );
518 } else if (c == (StgWord)&stg_ap_pppp_info) {
519 debugBelch("stg_ap_pppp_info\n" );
520 } else if (c == (StgWord)&stg_ap_ppppp_info) {
521 debugBelch("stg_ap_ppppp_info\n" );
522 } else if (c == (StgWord)&stg_ap_pppppp_info) {
523 debugBelch("stg_ap_pppppp_info\n" );
524 #ifdef PROFILING
525 } else if (c == (StgWord)&stg_restore_cccs_info) {
526 debugBelch("stg_restore_cccs_info\n" );
527 fprintCCS(stderr, (CostCentreStack*)sp[1]);
528 debugBelch("\n" );
529 continue;
530 #endif
531 } else {
532 debugBelch("RET_SMALL (%p)\n", info);
533 }
534 bitmap = info->layout.bitmap;
535 printSmallBitmap(spBottom, sp+1,
536 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
537 continue;
538 }
539
540 case RET_BCO: {
541 StgBCO *bco;
542
543 bco = ((StgBCO *)sp[1]);
544
545 debugBelch("RET_BCO (%p)\n", sp);
546 printLargeBitmap(spBottom, sp+2,
547 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
548 continue;
549 }
550
551 case RET_BIG:
552 barf("todo");
553
554 case RET_FUN:
555 {
556 const StgFunInfoTable *fun_info;
557 StgRetFun *ret_fun;
558
559 ret_fun = (StgRetFun *)sp;
560 fun_info = get_fun_itbl(ret_fun->fun);
561 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, (int)fun_info->f.fun_type);
562 switch (fun_info->f.fun_type) {
563 case ARG_GEN:
564 printSmallBitmap(spBottom, sp+2,
565 BITMAP_BITS(fun_info->f.b.bitmap),
566 BITMAP_SIZE(fun_info->f.b.bitmap));
567 break;
568 case ARG_GEN_BIG:
569 printLargeBitmap(spBottom, sp+2,
570 GET_FUN_LARGE_BITMAP(fun_info),
571 GET_FUN_LARGE_BITMAP(fun_info)->size);
572 break;
573 default:
574 printSmallBitmap(spBottom, sp+2,
575 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
576 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
577 break;
578 }
579 continue;
580 }
581
582 default:
583 debugBelch("unknown object %d\n", (int)info->type);
584 barf("printStackChunk");
585 }
586 }
587 }
588
589 void printTSO( StgTSO *tso )
590 {
591 printStackChunk( tso->stackobj->sp,
592 tso->stackobj->stack+tso->stackobj->stack_size);
593 }
594
595 /* --------------------------------------------------------------------------
596 * Address printing code
597 *
598 * Uses symbol table in (unstripped executable)
599 * ------------------------------------------------------------------------*/
600
601 /* --------------------------------------------------------------------------
602 * Simple lookup table
603 * address -> function name
604 * ------------------------------------------------------------------------*/
605
606 static HashTable * add_to_fname_table = NULL;
607
608 const char *lookupGHCName( void *addr )
609 {
610 if (add_to_fname_table == NULL)
611 return NULL;
612
613 return lookupHashTable(add_to_fname_table, (StgWord)addr);
614 }
615
616 /* --------------------------------------------------------------------------
617 * Symbol table loading
618 * ------------------------------------------------------------------------*/
619
620 /* Causing linking trouble on Win32 plats, so I'm
621 disabling this for now.
622 */
623 #ifdef USING_LIBBFD
624 # define PACKAGE 1
625 # define PACKAGE_VERSION 1
626 /* Those PACKAGE_* defines are workarounds for bfd:
627 * https://sourceware.org/bugzilla/show_bug.cgi?id=14243
628 * ghc's build system filter PACKAGE_* values out specifically to avoid clashes
629 * with user's autoconf-based Cabal packages.
630 * It's a shame <bfd.h> checks for unrelated fields instead of actually used
631 * macros.
632 */
633 # include <bfd.h>
634
635 /* Fairly ad-hoc piece of code that seems to filter out a lot of
636 * rubbish like the obj-splitting symbols
637 */
638
639 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
640 {
641 #if 0
642 /* ToDo: make this work on BFD */
643 int tp = type & N_TYPE;
644 if (tp == N_TEXT || tp == N_DATA) {
645 return (name[0] == '_' && name[1] != '_');
646 } else {
647 return rtsFalse;
648 }
649 #else
650 if (*name == '\0' ||
651 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
652 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
653 return rtsFalse;
654 }
655 return rtsTrue;
656 #endif
657 }
658
659 extern void DEBUG_LoadSymbols( const char *name )
660 {
661 bfd* abfd;
662 char **matching;
663
664 bfd_init();
665 abfd = bfd_openr(name, "default");
666 if (abfd == NULL) {
667 barf("can't open executable %s to get symbol table", name);
668 }
669 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
670 barf("mismatch");
671 }
672
673 {
674 long storage_needed;
675 asymbol **symbol_table;
676 long number_of_symbols;
677 long num_real_syms = 0;
678 long i;
679
680 storage_needed = bfd_get_symtab_upper_bound (abfd);
681
682 if (storage_needed < 0) {
683 barf("can't read symbol table");
684 }
685 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
686
687 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
688
689 if (number_of_symbols < 0) {
690 barf("can't canonicalise symbol table");
691 }
692
693 if (add_to_fname_table == NULL)
694 add_to_fname_table = allocHashTable();
695
696 for( i = 0; i != number_of_symbols; ++i ) {
697 symbol_info info;
698 bfd_get_symbol_info(abfd,symbol_table[i],&info);
699 if (isReal(info.type, info.name)) {
700 insertHashTable(add_to_fname_table,
701 info.value, (void*)info.name);
702 num_real_syms += 1;
703 }
704 }
705
706 IF_DEBUG(interpreter,
707 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
708 number_of_symbols, num_real_syms)
709 );
710
711 stgFree(symbol_table);
712 }
713 }
714
715 #else /* USING_LIBBFD */
716
717 extern void DEBUG_LoadSymbols( const char *name STG_UNUSED )
718 {
719 /* nothing, yet */
720 }
721
722 #endif /* USING_LIBBFD */
723
724 void findPtr(P_ p, int); /* keep gcc -Wall happy */
725
726 int searched = 0;
727
728 static int
729 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
730 {
731 StgPtr q, r, end;
732 for (; bd; bd = bd->link) {
733 searched++;
734 for (q = bd->start; q < bd->free; q++) {
735 if (UNTAG_CONST_CLOSURE((StgClosure*)*q) == (const StgClosure *)p) {
736 if (i < arr_size) {
737 for (r = bd->start; r < bd->free; r = end) {
738 // skip over zeroed-out slop
739 while (*r == 0) r++;
740 if (!LOOKS_LIKE_CLOSURE_PTR(r)) {
741 debugBelch("%p found at %p, no closure at %p\n",
742 p, q, r);
743 break;
744 }
745 end = r + closure_sizeW((StgClosure*)r);
746 if (q < end) {
747 debugBelch("%p = ", r);
748 printClosure((StgClosure *)r);
749 arr[i++] = r;
750 break;
751 }
752 }
753 if (r >= bd->free) {
754 debugBelch("%p found at %p, closure?", p, q);
755 }
756 } else {
757 return i;
758 }
759 }
760 }
761 }
762 return i;
763 }
764
765 void
766 findPtr(P_ p, int follow)
767 {
768 uint32_t g, n;
769 bdescr *bd;
770 const int arr_size = 1024;
771 StgPtr arr[arr_size];
772 int i = 0;
773 searched = 0;
774
775 for (n = 0; n < n_capabilities; n++) {
776 bd = nurseries[i].blocks;
777 i = findPtrBlocks(p,bd,arr,arr_size,i);
778 if (i >= arr_size) return;
779 }
780
781 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
782 bd = generations[g].blocks;
783 i = findPtrBlocks(p,bd,arr,arr_size,i);
784 bd = generations[g].large_objects;
785 i = findPtrBlocks(p,bd,arr,arr_size,i);
786 if (i >= arr_size) return;
787 }
788 if (follow && i == 1) {
789 debugBelch("-->\n");
790 findPtr(arr[0], 1);
791 }
792 }
793
794 const char *what_next_strs[] = {
795 [0] = "(unknown)",
796 [ThreadRunGHC] = "ThreadRunGHC",
797 [ThreadInterpret] = "ThreadInterpret",
798 [ThreadKilled] = "ThreadKilled",
799 [ThreadComplete] = "ThreadComplete"
800 };
801
802 #else /* DEBUG */
803 void printPtr( StgPtr p )
804 {
805 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
806 }
807
808 void printObj( StgClosure *obj )
809 {
810 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
811 }
812
813
814 #endif /* DEBUG */
815
816 /* -----------------------------------------------------------------------------
817 Closure types
818
819 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
820 -------------------------------------------------------------------------- */
821
822 const char *closure_type_names[] = {
823 [INVALID_OBJECT] = "INVALID_OBJECT",
824 [CONSTR] = "CONSTR",
825 [CONSTR_1_0] = "CONSTR_1_0",
826 [CONSTR_0_1] = "CONSTR_0_1",
827 [CONSTR_2_0] = "CONSTR_2_0",
828 [CONSTR_1_1] = "CONSTR_1_1",
829 [CONSTR_0_2] = "CONSTR_0_2",
830 [CONSTR_STATIC] = "CONSTR_STATIC",
831 [CONSTR_NOCAF_STATIC] = "CONSTR_NOCAF_STATIC",
832 [FUN] = "FUN",
833 [FUN_1_0] = "FUN_1_0",
834 [FUN_0_1] = "FUN_0_1",
835 [FUN_2_0] = "FUN_2_0",
836 [FUN_1_1] = "FUN_1_1",
837 [FUN_0_2] = "FUN_0_2",
838 [FUN_STATIC] = "FUN_STATIC",
839 [THUNK] = "THUNK",
840 [THUNK_1_0] = "THUNK_1_0",
841 [THUNK_0_1] = "THUNK_0_1",
842 [THUNK_2_0] = "THUNK_2_0",
843 [THUNK_1_1] = "THUNK_1_1",
844 [THUNK_0_2] = "THUNK_0_2",
845 [THUNK_STATIC] = "THUNK_STATIC",
846 [THUNK_SELECTOR] = "THUNK_SELECTOR",
847 [BCO] = "BCO",
848 [AP] = "AP",
849 [PAP] = "PAP",
850 [AP_STACK] = "AP_STACK",
851 [IND] = "IND",
852 [IND_STATIC] = "IND_STATIC",
853 [RET_BCO] = "RET_BCO",
854 [RET_SMALL] = "RET_SMALL",
855 [RET_BIG] = "RET_BIG",
856 [RET_FUN] = "RET_FUN",
857 [UPDATE_FRAME] = "UPDATE_FRAME",
858 [CATCH_FRAME] = "CATCH_FRAME",
859 [UNDERFLOW_FRAME] = "UNDERFLOW_FRAME",
860 [STOP_FRAME] = "STOP_FRAME",
861 [BLOCKING_QUEUE] = "BLOCKING_QUEUE",
862 [BLACKHOLE] = "BLACKHOLE",
863 [MVAR_CLEAN] = "MVAR_CLEAN",
864 [MVAR_DIRTY] = "MVAR_DIRTY",
865 [TVAR] = "TVAR",
866 [ARR_WORDS] = "ARR_WORDS",
867 [MUT_ARR_PTRS_CLEAN] = "MUT_ARR_PTRS_CLEAN",
868 [MUT_ARR_PTRS_DIRTY] = "MUT_ARR_PTRS_DIRTY",
869 [MUT_ARR_PTRS_FROZEN0] = "MUT_ARR_PTRS_FROZEN0",
870 [MUT_ARR_PTRS_FROZEN] = "MUT_ARR_PTRS_FROZEN",
871 [MUT_VAR_CLEAN] = "MUT_VAR_CLEAN",
872 [MUT_VAR_DIRTY] = "MUT_VAR_DIRTY",
873 [WEAK] = "WEAK",
874 [PRIM] = "PRIM",
875 [MUT_PRIM] = "MUT_PRIM",
876 [TSO] = "TSO",
877 [STACK] = "STACK",
878 [TREC_CHUNK] = "TREC_CHUNK",
879 [ATOMICALLY_FRAME] = "ATOMICALLY_FRAME",
880 [CATCH_RETRY_FRAME] = "CATCH_RETRY_FRAME",
881 [CATCH_STM_FRAME] = "CATCH_STM_FRAME",
882 [WHITEHOLE] = "WHITEHOLE",
883 [COMPACT_NFDATA] = "COMPACT_NFDATA"
884 };
885
886 const char *
887 info_type(const StgClosure *closure){
888 return closure_type_names[get_itbl(closure)->type];
889 }
890
891 const char *
892 info_type_by_ip(const StgInfoTable *ip){
893 return closure_type_names[ip->type];
894 }
895
896 void
897 info_hdr_type(const StgClosure *closure, char *res){
898 strcpy(res,closure_type_names[get_itbl(closure)->type]);
899 }