rts: More const correct-ness fixes
[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 default:
390 //barf("printClosure %d",get_itbl(obj)->type);
391 debugBelch("*** printClosure: unknown type %d ****\n",
392 (int)get_itbl(obj)->type );
393 barf("printClosure %d",get_itbl(obj)->type);
394 return;
395 }
396 }
397
398 // If you know you have an UPDATE_FRAME, but want to know exactly which.
399 const char *info_update_frame(const StgClosure *closure)
400 {
401 // Note: We intentionally don't take the info table pointer as
402 // an argument. As it will be confusing whether one should pass
403 // it pointing to the code or struct members when compiling with
404 // TABLES_NEXT_TO_CODE.
405 const StgInfoTable *info = closure->header.info;
406 if (info == &stg_upd_frame_info) {
407 return "NORMAL_UPDATE_FRAME";
408 } else if (info == &stg_bh_upd_frame_info) {
409 return "BH_UPDATE_FRAME";
410 } else if (info == &stg_marked_upd_frame_info) {
411 return "MARKED_UPDATE_FRAME";
412 } else {
413 return "ERROR: Not an update frame!!!";
414 }
415 }
416
417 /*
418 void printGraph( StgClosure *obj )
419 {
420 printClosure(obj);
421 }
422 */
423
424 static void
425 printSmallBitmap( StgPtr spBottom, StgPtr payload, StgWord bitmap,
426 uint32_t size )
427 {
428 uint32_t i;
429
430 for(i = 0; i < size; i++, bitmap >>= 1 ) {
431 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-(payload+i)), payload+i);
432 if ((bitmap & 1) == 0) {
433 printPtr((P_)payload[i]);
434 debugBelch("\n");
435 } else {
436 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
437 }
438 }
439 }
440
441 static void
442 printLargeBitmap( StgPtr spBottom, StgPtr payload, StgLargeBitmap* large_bitmap,
443 uint32_t size )
444 {
445 StgWord bmp;
446 uint32_t i, j;
447
448 i = 0;
449 for (bmp=0; i < size; bmp++) {
450 StgWord bitmap = large_bitmap->bitmap[bmp];
451 j = 0;
452 for(; i < size && j < BITS_IN(W_); j++, i++, bitmap >>= 1 ) {
453 debugBelch(" stk[%" FMT_Word "] (%p) = ", (W_)(spBottom-(payload+i)), payload+i);
454 if ((bitmap & 1) == 0) {
455 printPtr((P_)payload[i]);
456 debugBelch("\n");
457 } else {
458 debugBelch("Word# %" FMT_Word "\n", (W_)payload[i]);
459 }
460 }
461 }
462 }
463
464 void
465 printStackChunk( StgPtr sp, StgPtr spBottom )
466 {
467 StgWord bitmap;
468 const StgInfoTable *info;
469
470 ASSERT(sp <= spBottom);
471 for (; sp < spBottom; sp += stack_frame_sizeW((StgClosure *)sp)) {
472
473 info = get_itbl((StgClosure *)sp);
474
475 switch (info->type) {
476
477 case UPDATE_FRAME:
478 case CATCH_FRAME:
479 case UNDERFLOW_FRAME:
480 case STOP_FRAME:
481 printClosure((StgClosure*)sp);
482 continue;
483
484 case RET_SMALL: {
485 StgWord c = *sp;
486 if (c == (StgWord)&stg_ctoi_R1p_info) {
487 debugBelch("tstg_ctoi_ret_R1p_info\n" );
488 } else if (c == (StgWord)&stg_ctoi_R1n_info) {
489 debugBelch("stg_ctoi_ret_R1n_info\n" );
490 } else if (c == (StgWord)&stg_ctoi_F1_info) {
491 debugBelch("stg_ctoi_ret_F1_info\n" );
492 } else if (c == (StgWord)&stg_ctoi_D1_info) {
493 debugBelch("stg_ctoi_ret_D1_info\n" );
494 } else if (c == (StgWord)&stg_ctoi_V_info) {
495 debugBelch("stg_ctoi_ret_V_info\n" );
496 } else if (c == (StgWord)&stg_ap_v_info) {
497 debugBelch("stg_ap_v_info\n" );
498 } else if (c == (StgWord)&stg_ap_f_info) {
499 debugBelch("stg_ap_f_info\n" );
500 } else if (c == (StgWord)&stg_ap_d_info) {
501 debugBelch("stg_ap_d_info\n" );
502 } else if (c == (StgWord)&stg_ap_l_info) {
503 debugBelch("stg_ap_l_info\n" );
504 } else if (c == (StgWord)&stg_ap_n_info) {
505 debugBelch("stg_ap_n_info\n" );
506 } else if (c == (StgWord)&stg_ap_p_info) {
507 debugBelch("stg_ap_p_info\n" );
508 } else if (c == (StgWord)&stg_ap_pp_info) {
509 debugBelch("stg_ap_pp_info\n" );
510 } else if (c == (StgWord)&stg_ap_ppp_info) {
511 debugBelch("stg_ap_ppp_info\n" );
512 } else if (c == (StgWord)&stg_ap_pppp_info) {
513 debugBelch("stg_ap_pppp_info\n" );
514 } else if (c == (StgWord)&stg_ap_ppppp_info) {
515 debugBelch("stg_ap_ppppp_info\n" );
516 } else if (c == (StgWord)&stg_ap_pppppp_info) {
517 debugBelch("stg_ap_pppppp_info\n" );
518 #ifdef PROFILING
519 } else if (c == (StgWord)&stg_restore_cccs_info) {
520 debugBelch("stg_restore_cccs_info\n" );
521 fprintCCS(stderr, (CostCentreStack*)sp[1]);
522 debugBelch("\n" );
523 continue;
524 #endif
525 } else {
526 debugBelch("RET_SMALL (%p)\n", info);
527 }
528 bitmap = info->layout.bitmap;
529 printSmallBitmap(spBottom, sp+1,
530 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
531 continue;
532 }
533
534 case RET_BCO: {
535 StgBCO *bco;
536
537 bco = ((StgBCO *)sp[1]);
538
539 debugBelch("RET_BCO (%p)\n", sp);
540 printLargeBitmap(spBottom, sp+2,
541 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
542 continue;
543 }
544
545 case RET_BIG:
546 barf("todo");
547
548 case RET_FUN:
549 {
550 const StgFunInfoTable *fun_info;
551 StgRetFun *ret_fun;
552
553 ret_fun = (StgRetFun *)sp;
554 fun_info = get_fun_itbl(ret_fun->fun);
555 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, (int)fun_info->f.fun_type);
556 switch (fun_info->f.fun_type) {
557 case ARG_GEN:
558 printSmallBitmap(spBottom, sp+2,
559 BITMAP_BITS(fun_info->f.b.bitmap),
560 BITMAP_SIZE(fun_info->f.b.bitmap));
561 break;
562 case ARG_GEN_BIG:
563 printLargeBitmap(spBottom, sp+2,
564 GET_FUN_LARGE_BITMAP(fun_info),
565 GET_FUN_LARGE_BITMAP(fun_info)->size);
566 break;
567 default:
568 printSmallBitmap(spBottom, sp+2,
569 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
570 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
571 break;
572 }
573 continue;
574 }
575
576 default:
577 debugBelch("unknown object %d\n", (int)info->type);
578 barf("printStackChunk");
579 }
580 }
581 }
582
583 void printTSO( StgTSO *tso )
584 {
585 printStackChunk( tso->stackobj->sp,
586 tso->stackobj->stack+tso->stackobj->stack_size);
587 }
588
589 /* --------------------------------------------------------------------------
590 * Address printing code
591 *
592 * Uses symbol table in (unstripped executable)
593 * ------------------------------------------------------------------------*/
594
595 /* --------------------------------------------------------------------------
596 * Simple lookup table
597 * address -> function name
598 * ------------------------------------------------------------------------*/
599
600 static HashTable * add_to_fname_table = NULL;
601
602 const char *lookupGHCName( void *addr )
603 {
604 if (add_to_fname_table == NULL)
605 return NULL;
606
607 return lookupHashTable(add_to_fname_table, (StgWord)addr);
608 }
609
610 /* --------------------------------------------------------------------------
611 * Symbol table loading
612 * ------------------------------------------------------------------------*/
613
614 /* Causing linking trouble on Win32 plats, so I'm
615 disabling this for now.
616 */
617 #ifdef USING_LIBBFD
618 # define PACKAGE 1
619 # define PACKAGE_VERSION 1
620 /* Those PACKAGE_* defines are workarounds for bfd:
621 * https://sourceware.org/bugzilla/show_bug.cgi?id=14243
622 * ghc's build system filter PACKAGE_* values out specifically to avoid clashes
623 * with user's autoconf-based Cabal packages.
624 * It's a shame <bfd.h> checks for unrelated fields instead of actually used
625 * macros.
626 */
627 # include <bfd.h>
628
629 /* Fairly ad-hoc piece of code that seems to filter out a lot of
630 * rubbish like the obj-splitting symbols
631 */
632
633 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
634 {
635 #if 0
636 /* ToDo: make this work on BFD */
637 int tp = type & N_TYPE;
638 if (tp == N_TEXT || tp == N_DATA) {
639 return (name[0] == '_' && name[1] != '_');
640 } else {
641 return rtsFalse;
642 }
643 #else
644 if (*name == '\0' ||
645 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
646 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
647 return rtsFalse;
648 }
649 return rtsTrue;
650 #endif
651 }
652
653 extern void DEBUG_LoadSymbols( const char *name )
654 {
655 bfd* abfd;
656 char **matching;
657
658 bfd_init();
659 abfd = bfd_openr(name, "default");
660 if (abfd == NULL) {
661 barf("can't open executable %s to get symbol table", name);
662 }
663 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
664 barf("mismatch");
665 }
666
667 {
668 long storage_needed;
669 asymbol **symbol_table;
670 long number_of_symbols;
671 long num_real_syms = 0;
672 long i;
673
674 storage_needed = bfd_get_symtab_upper_bound (abfd);
675
676 if (storage_needed < 0) {
677 barf("can't read symbol table");
678 }
679 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
680
681 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
682
683 if (number_of_symbols < 0) {
684 barf("can't canonicalise symbol table");
685 }
686
687 if (add_to_fname_table == NULL)
688 add_to_fname_table = allocHashTable();
689
690 for( i = 0; i != number_of_symbols; ++i ) {
691 symbol_info info;
692 bfd_get_symbol_info(abfd,symbol_table[i],&info);
693 if (isReal(info.type, info.name)) {
694 insertHashTable(add_to_fname_table,
695 info.value, (void*)info.name);
696 num_real_syms += 1;
697 }
698 }
699
700 IF_DEBUG(interpreter,
701 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
702 number_of_symbols, num_real_syms)
703 );
704
705 stgFree(symbol_table);
706 }
707 }
708
709 #else /* USING_LIBBFD */
710
711 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
712 {
713 /* nothing, yet */
714 }
715
716 #endif /* USING_LIBBFD */
717
718 void findPtr(P_ p, int); /* keep gcc -Wall happy */
719
720 int searched = 0;
721
722 static int
723 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
724 {
725 StgPtr q, r, end;
726 for (; bd; bd = bd->link) {
727 searched++;
728 for (q = bd->start; q < bd->free; q++) {
729 if (UNTAG_CONST_CLOSURE((StgClosure*)*q) == (const StgClosure *)p) {
730 if (i < arr_size) {
731 for (r = bd->start; r < bd->free; r = end) {
732 // skip over zeroed-out slop
733 while (*r == 0) r++;
734 if (!LOOKS_LIKE_CLOSURE_PTR(r)) {
735 debugBelch("%p found at %p, no closure at %p\n",
736 p, q, r);
737 break;
738 }
739 end = r + closure_sizeW((StgClosure*)r);
740 if (q < end) {
741 debugBelch("%p = ", r);
742 printClosure((StgClosure *)r);
743 arr[i++] = r;
744 break;
745 }
746 }
747 if (r >= bd->free) {
748 debugBelch("%p found at %p, closure?", p, q);
749 }
750 } else {
751 return i;
752 }
753 }
754 }
755 }
756 return i;
757 }
758
759 void
760 findPtr(P_ p, int follow)
761 {
762 uint32_t g, n;
763 bdescr *bd;
764 const int arr_size = 1024;
765 StgPtr arr[arr_size];
766 int i = 0;
767 searched = 0;
768
769 for (n = 0; n < n_capabilities; n++) {
770 bd = nurseries[i].blocks;
771 i = findPtrBlocks(p,bd,arr,arr_size,i);
772 if (i >= arr_size) return;
773 }
774
775 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
776 bd = generations[g].blocks;
777 i = findPtrBlocks(p,bd,arr,arr_size,i);
778 bd = generations[g].large_objects;
779 i = findPtrBlocks(p,bd,arr,arr_size,i);
780 if (i >= arr_size) return;
781 }
782 if (follow && i == 1) {
783 debugBelch("-->\n");
784 findPtr(arr[0], 1);
785 }
786 }
787
788 /* prettyPrintClosure() is for printing out a closure using the data constructor
789 names found in the info tables. Closures are printed in a fashion that resembles
790 their Haskell representation. Useful during debugging.
791
792 Todo: support for more closure types, and support for non pointer fields in the
793 payload.
794 */
795
796 void prettyPrintClosure_ (const StgClosure *);
797
798 void prettyPrintClosure (const StgClosure *obj)
799 {
800 prettyPrintClosure_ (obj);
801 debugBelch ("\n");
802 }
803
804 void prettyPrintClosure_ (const StgClosure *obj)
805 {
806 const StgInfoTable *info;
807
808 /* collapse any indirections */
809 unsigned int type;
810 type = get_itbl(obj)->type;
811
812 while (type == IND ||
813 type == IND_STATIC)
814 {
815 obj = ((StgInd *)obj)->indirectee;
816 type = get_itbl(obj)->type;
817 }
818
819 /* find the info table for this object */
820 info = get_itbl(obj);
821
822 /* determine what kind of object we have */
823 switch (info->type)
824 {
825 /* full applications of data constructors */
826 case CONSTR:
827 case CONSTR_1_0:
828 case CONSTR_0_1:
829 case CONSTR_1_1:
830 case CONSTR_0_2:
831 case CONSTR_2_0:
832 case CONSTR_STATIC:
833 case CONSTR_NOCAF_STATIC:
834 {
835 const StgConInfoTable *con_info;
836 const char *descriptor;
837 uint32_t i;
838
839 /* find the con_info for the constructor */
840 con_info = get_con_itbl (obj);
841
842 /* obtain the name of the constructor */
843 descriptor = GET_CON_DESC(con_info);
844
845 debugBelch ("(%s", descriptor);
846
847 /* process the payload of the closure */
848 /* we don't handle non pointers at the moment */
849 for (i = 0; i < info->layout.payload.ptrs; i++)
850 {
851 debugBelch (" ");
852 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
853 }
854 debugBelch (")");
855 break;
856 }
857
858 /* if it isn't a constructor then just print the closure type */
859 default:
860 {
861 debugBelch ("<%s>", info_type(obj));
862 break;
863 }
864 }
865 }
866
867 const char *what_next_strs[] = {
868 [0] = "(unknown)",
869 [ThreadRunGHC] = "ThreadRunGHC",
870 [ThreadInterpret] = "ThreadInterpret",
871 [ThreadKilled] = "ThreadKilled",
872 [ThreadComplete] = "ThreadComplete"
873 };
874
875 #else /* DEBUG */
876 void printPtr( StgPtr p )
877 {
878 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
879 }
880
881 void printObj( StgClosure *obj )
882 {
883 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
884 }
885
886
887 #endif /* DEBUG */
888
889 /* -----------------------------------------------------------------------------
890 Closure types
891
892 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
893 -------------------------------------------------------------------------- */
894
895 const char *closure_type_names[] = {
896 [INVALID_OBJECT] = "INVALID_OBJECT",
897 [CONSTR] = "CONSTR",
898 [CONSTR_1_0] = "CONSTR_1_0",
899 [CONSTR_0_1] = "CONSTR_0_1",
900 [CONSTR_2_0] = "CONSTR_2_0",
901 [CONSTR_1_1] = "CONSTR_1_1",
902 [CONSTR_0_2] = "CONSTR_0_2",
903 [CONSTR_STATIC] = "CONSTR_STATIC",
904 [CONSTR_NOCAF_STATIC] = "CONSTR_NOCAF_STATIC",
905 [FUN] = "FUN",
906 [FUN_1_0] = "FUN_1_0",
907 [FUN_0_1] = "FUN_0_1",
908 [FUN_2_0] = "FUN_2_0",
909 [FUN_1_1] = "FUN_1_1",
910 [FUN_0_2] = "FUN_0_2",
911 [FUN_STATIC] = "FUN_STATIC",
912 [THUNK] = "THUNK",
913 [THUNK_1_0] = "THUNK_1_0",
914 [THUNK_0_1] = "THUNK_0_1",
915 [THUNK_2_0] = "THUNK_2_0",
916 [THUNK_1_1] = "THUNK_1_1",
917 [THUNK_0_2] = "THUNK_0_2",
918 [THUNK_STATIC] = "THUNK_STATIC",
919 [THUNK_SELECTOR] = "THUNK_SELECTOR",
920 [BCO] = "BCO",
921 [AP] = "AP",
922 [PAP] = "PAP",
923 [AP_STACK] = "AP_STACK",
924 [IND] = "IND",
925 [IND_STATIC] = "IND_STATIC",
926 [RET_BCO] = "RET_BCO",
927 [RET_SMALL] = "RET_SMALL",
928 [RET_BIG] = "RET_BIG",
929 [RET_FUN] = "RET_FUN",
930 [UPDATE_FRAME] = "UPDATE_FRAME",
931 [CATCH_FRAME] = "CATCH_FRAME",
932 [UNDERFLOW_FRAME] = "UNDERFLOW_FRAME",
933 [STOP_FRAME] = "STOP_FRAME",
934 [BLOCKING_QUEUE] = "BLOCKING_QUEUE",
935 [BLACKHOLE] = "BLACKHOLE",
936 [MVAR_CLEAN] = "MVAR_CLEAN",
937 [MVAR_DIRTY] = "MVAR_DIRTY",
938 [TVAR] = "TVAR",
939 [ARR_WORDS] = "ARR_WORDS",
940 [MUT_ARR_PTRS_CLEAN] = "MUT_ARR_PTRS_CLEAN",
941 [MUT_ARR_PTRS_DIRTY] = "MUT_ARR_PTRS_DIRTY",
942 [MUT_ARR_PTRS_FROZEN0] = "MUT_ARR_PTRS_FROZEN0",
943 [MUT_ARR_PTRS_FROZEN] = "MUT_ARR_PTRS_FROZEN",
944 [MUT_VAR_CLEAN] = "MUT_VAR_CLEAN",
945 [MUT_VAR_DIRTY] = "MUT_VAR_DIRTY",
946 [WEAK] = "WEAK",
947 [PRIM] = "PRIM",
948 [MUT_PRIM] = "MUT_PRIM",
949 [TSO] = "TSO",
950 [STACK] = "STACK",
951 [TREC_CHUNK] = "TREC_CHUNK",
952 [ATOMICALLY_FRAME] = "ATOMICALLY_FRAME",
953 [CATCH_RETRY_FRAME] = "CATCH_RETRY_FRAME",
954 [CATCH_STM_FRAME] = "CATCH_STM_FRAME",
955 [WHITEHOLE] = "WHITEHOLE"
956 };
957
958 const char *
959 info_type(const StgClosure *closure){
960 return closure_type_names[get_itbl(closure)->type];
961 }
962
963 const char *
964 info_type_by_ip(const StgInfoTable *ip){
965 return closure_type_names[ip->type];
966 }
967
968 void
969 info_hdr_type(const StgClosure *closure, char *res){
970 strcpy(res,closure_type_names[get_itbl(closure)->type]);
971 }