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