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