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