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