More OS X build fixes
[ghc.git] / rts / Printer.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1994-2000.
4 *
5 * Heap printer
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "rts/Bytecodes.h" /* for InstrPtr */
12
13 #include "sm/Storage.h"
14 #include "Printer.h"
15 #include "RtsUtils.h"
16
17 #include <string.h>
18
19 #ifdef DEBUG
20
21 #include "Disassembler.h"
22 #include "Apply.h"
23
24 /* --------------------------------------------------------------------------
25 * local function decls
26 * ------------------------------------------------------------------------*/
27
28 static void printStdObjPayload( StgClosure *obj );
29 #ifdef USING_LIBBFD
30 static void reset_table ( int size );
31 static void prepare_table ( void );
32 static void insert ( StgWord value, const char *name );
33 #endif
34 #if 0 /* unused but might be useful sometime */
35 static rtsBool lookup_name ( char *name, StgWord *result );
36 static void enZcode ( char *in, char *out );
37 #endif
38 static char unZcode ( char ch );
39 static void printZcoded ( const char *raw );
40
41 /* --------------------------------------------------------------------------
42 * Printer
43 * ------------------------------------------------------------------------*/
44
45 void printPtr( StgPtr p )
46 {
47 const char *raw;
48 raw = lookupGHCName(p);
49 if (raw != NULL) {
50 printZcoded(raw);
51 } else {
52 debugBelch("%p", p);
53 }
54 }
55
56 void printObj( StgClosure *obj )
57 {
58 debugBelch("Object "); printPtr((StgPtr)obj); debugBelch(" = ");
59 printClosure(obj);
60 }
61
62 STATIC_INLINE void
63 printStdObjHdr( StgClosure *obj, char* tag )
64 {
65 debugBelch("%s(",tag);
66 printPtr((StgPtr)obj->header.info);
67 #ifdef PROFILING
68 debugBelch(", %s", obj->header.prof.ccs->cc->label);
69 #endif
70 }
71
72 static void
73 printStdObjPayload( StgClosure *obj )
74 {
75 StgWord i, j;
76 const StgInfoTable* info;
77
78 info = get_itbl(obj);
79 for (i = 0; i < info->layout.payload.ptrs; ++i) {
80 debugBelch(", ");
81 printPtr((StgPtr)obj->payload[i]);
82 }
83 for (j = 0; j < info->layout.payload.nptrs; ++j) {
84 debugBelch(", %pd#",obj->payload[i+j]);
85 }
86 debugBelch(")\n");
87 }
88
89 static void
90 printThunkPayload( StgThunk *obj )
91 {
92 StgWord i, j;
93 const StgInfoTable* info;
94
95 info = get_itbl((StgClosure *)obj);
96 for (i = 0; i < info->layout.payload.ptrs; ++i) {
97 debugBelch(", ");
98 printPtr((StgPtr)obj->payload[i]);
99 }
100 for (j = 0; j < info->layout.payload.nptrs; ++j) {
101 debugBelch(", %pd#",obj->payload[i+j]);
102 }
103 debugBelch(")\n");
104 }
105
106 static void
107 printThunkObject( StgThunk *obj, char* tag )
108 {
109 printStdObjHdr( (StgClosure *)obj, tag );
110 printThunkPayload( obj );
111 }
112
113 void
114 printClosure( StgClosure *obj )
115 {
116 obj = UNTAG_CLOSURE(obj);
117
118 StgInfoTable *info;
119 info = get_itbl(obj);
120
121 switch ( info->type ) {
122 case INVALID_OBJECT:
123 barf("Invalid object");
124
125 case CONSTR:
126 case CONSTR_1_0: case CONSTR_0_1:
127 case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
128 case CONSTR_STATIC:
129 case CONSTR_NOCAF_STATIC:
130 {
131 StgWord i, j;
132 StgConInfoTable *con_info = get_con_itbl (obj);
133
134 debugBelch("%s(", GET_CON_DESC(con_info));
135 for (i = 0; i < info->layout.payload.ptrs; ++i) {
136 if (i != 0) debugBelch(", ");
137 printPtr((StgPtr)obj->payload[i]);
138 }
139 for (j = 0; j < info->layout.payload.nptrs; ++j) {
140 if (i != 0 || j != 0) debugBelch(", ");
141 debugBelch("%p#", obj->payload[i+j]);
142 }
143 debugBelch(")\n");
144 break;
145 }
146
147 case FUN:
148 case FUN_1_0: case FUN_0_1:
149 case FUN_1_1: case FUN_0_2: case FUN_2_0:
150 case FUN_STATIC:
151 debugBelch("FUN/%d(",(int)itbl_to_fun_itbl(info)->f.arity);
152 printPtr((StgPtr)obj->header.info);
153 #ifdef PROFILING
154 debugBelch(", %s", obj->header.prof.ccs->cc->label);
155 #endif
156 printStdObjPayload(obj);
157 break;
158
159 case PRIM:
160 debugBelch("PRIM(");
161 printPtr((StgPtr)obj->header.info);
162 printStdObjPayload(obj);
163 break;
164
165 case THUNK:
166 case THUNK_1_0: case THUNK_0_1:
167 case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
168 case THUNK_STATIC:
169 /* ToDo: will this work for THUNK_STATIC too? */
170 #ifdef PROFILING
171 printThunkObject((StgThunk *)obj,GET_PROF_DESC(info));
172 #else
173 printThunkObject((StgThunk *)obj,"THUNK");
174 #endif
175 break;
176
177 case THUNK_SELECTOR:
178 printStdObjHdr(obj, "THUNK_SELECTOR");
179 debugBelch(", %p)\n", ((StgSelector *)obj)->selectee);
180 break;
181
182 case BCO:
183 disassemble( (StgBCO*)obj );
184 break;
185
186 case AP:
187 {
188 StgAP* ap = (StgAP*)obj;
189 StgWord i;
190 debugBelch("AP("); printPtr((StgPtr)ap->fun);
191 for (i = 0; i < ap->n_args; ++i) {
192 debugBelch(", ");
193 printPtr((P_)ap->payload[i]);
194 }
195 debugBelch(")\n");
196 break;
197 }
198
199 case PAP:
200 {
201 StgPAP* pap = (StgPAP*)obj;
202 StgWord i;
203 debugBelch("PAP/%d(",(int)pap->arity);
204 printPtr((StgPtr)pap->fun);
205 for (i = 0; i < pap->n_args; ++i) {
206 debugBelch(", ");
207 printPtr((StgPtr)pap->payload[i]);
208 }
209 debugBelch(")\n");
210 break;
211 }
212
213 case AP_STACK:
214 {
215 StgAP_STACK* ap = (StgAP_STACK*)obj;
216 StgWord i;
217 debugBelch("AP_STACK("); printPtr((StgPtr)ap->fun);
218 for (i = 0; i < ap->size; ++i) {
219 debugBelch(", ");
220 printPtr((P_)ap->payload[i]);
221 }
222 debugBelch(")\n");
223 break;
224 }
225
226 case IND:
227 debugBelch("IND(");
228 printPtr((StgPtr)((StgInd*)obj)->indirectee);
229 debugBelch(")\n");
230 break;
231
232 case IND_PERM:
233 debugBelch("IND(");
234 printPtr((StgPtr)((StgInd*)obj)->indirectee);
235 debugBelch(")\n");
236 break;
237
238 case IND_STATIC:
239 debugBelch("IND_STATIC(");
240 printPtr((StgPtr)((StgInd*)obj)->indirectee);
241 debugBelch(")\n");
242 break;
243
244 case BLACKHOLE:
245 debugBelch("BLACKHOLE(");
246 printPtr((StgPtr)((StgInd*)obj)->indirectee);
247 debugBelch(")\n");
248 break;
249
250 /* Cannot happen -- use default case.
251 case RET_BCO:
252 case RET_SMALL:
253 case RET_BIG:
254 case RET_DYN:
255 case RET_FUN:
256 */
257
258 case UPDATE_FRAME:
259 {
260 StgUpdateFrame* u = (StgUpdateFrame*)obj;
261 debugBelch("UPDATE_FRAME(");
262 printPtr((StgPtr)GET_INFO(u));
263 debugBelch(",");
264 printPtr((StgPtr)u->updatee);
265 debugBelch(")\n");
266 break;
267 }
268
269 case CATCH_FRAME:
270 {
271 StgCatchFrame* u = (StgCatchFrame*)obj;
272 debugBelch("CATCH_FRAME(");
273 printPtr((StgPtr)GET_INFO(u));
274 debugBelch(",");
275 printPtr((StgPtr)u->handler);
276 debugBelch(")\n");
277 break;
278 }
279
280 case UNDERFLOW_FRAME:
281 {
282 StgUnderflowFrame* u = (StgUnderflowFrame*)obj;
283 debugBelch("UNDERFLOW_FRAME(");
284 printPtr((StgPtr)u->next_chunk);
285 debugBelch(")\n");
286 break;
287 }
288
289 case STOP_FRAME:
290 {
291 StgStopFrame* u = (StgStopFrame*)obj;
292 debugBelch("STOP_FRAME(");
293 printPtr((StgPtr)GET_INFO(u));
294 debugBelch(")\n");
295 break;
296 }
297
298 case ARR_WORDS:
299 {
300 StgWord i;
301 debugBelch("ARR_WORDS(\"");
302 for (i=0; i<arr_words_words((StgArrWords *)obj); i++)
303 debugBelch("%" FMT_Word, (W_)((StgArrWords *)obj)->payload[i]);
304 debugBelch("\")\n");
305 break;
306 }
307
308 case MUT_ARR_PTRS_CLEAN:
309 debugBelch("MUT_ARR_PTRS_CLEAN(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
310 break;
311
312 case MUT_ARR_PTRS_DIRTY:
313 debugBelch("MUT_ARR_PTRS_DIRTY(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
314 break;
315
316 case MUT_ARR_PTRS_FROZEN:
317 debugBelch("MUT_ARR_PTRS_FROZEN(size=%" FMT_Word ")\n", (W_)((StgMutArrPtrs *)obj)->ptrs);
318 break;
319
320 case MVAR_CLEAN:
321 case MVAR_DIRTY:
322 {
323 StgMVar* mv = (StgMVar*)obj;
324 debugBelch("MVAR(head=%p, tail=%p, value=%p)\n", mv->head, mv->tail, mv->value);
325 break;
326 }
327
328 case MUT_VAR_CLEAN:
329 {
330 StgMutVar* mv = (StgMutVar*)obj;
331 debugBelch("MUT_VAR_CLEAN(var=%p)\n", mv->var);
332 break;
333 }
334
335 case MUT_VAR_DIRTY:
336 {
337 StgMutVar* mv = (StgMutVar*)obj;
338 debugBelch("MUT_VAR_DIRTY(var=%p)\n", mv->var);
339 break;
340 }
341
342 case WEAK:
343 debugBelch("WEAK(");
344 debugBelch(" key=%p value=%p finalizer=%p",
345 (StgPtr)(((StgWeak*)obj)->key),
346 (StgPtr)(((StgWeak*)obj)->value),
347 (StgPtr)(((StgWeak*)obj)->finalizer));
348 debugBelch(")\n");
349 /* ToDo: chase 'link' ? */
350 break;
351
352 case TSO:
353 debugBelch("TSO(");
354 debugBelch("%lu (%p)",(unsigned long)(((StgTSO*)obj)->id), (StgTSO*)obj);
355 debugBelch(")\n");
356 break;
357
358 case STACK:
359 debugBelch("STACK");
360 break;
361
362 #if 0
363 /* Symptomatic of a problem elsewhere, have it fall-through & fail */
364 case EVACUATED:
365 debugBelch("EVACUATED(");
366 printClosure((StgEvacuated*)obj->evacuee);
367 debugBelch(")\n");
368 break;
369 #endif
370
371 default:
372 //barf("printClosure %d",get_itbl(obj)->type);
373 debugBelch("*** printClosure: unknown type %d ****\n",
374 (int)get_itbl(obj)->type );
375 barf("printClosure %d",get_itbl(obj)->type);
376 return;
377 }
378 }
379
380 /*
381 void printGraph( StgClosure *obj )
382 {
383 printClosure(obj);
384 }
385 */
386
387 StgPtr
388 printStackObj( StgPtr sp )
389 {
390 /*debugBelch("Stack[%d] = ", &stgStack[STACK_SIZE] - sp); */
391
392 StgClosure* c = (StgClosure*)(*sp);
393 printPtr((StgPtr)*sp);
394 if (c == (StgClosure*)&stg_ctoi_R1p_info) {
395 debugBelch("\t\t\tstg_ctoi_ret_R1p_info\n" );
396 } else
397 if (c == (StgClosure*)&stg_ctoi_R1n_info) {
398 debugBelch("\t\t\tstg_ctoi_ret_R1n_info\n" );
399 } else
400 if (c == (StgClosure*)&stg_ctoi_F1_info) {
401 debugBelch("\t\t\tstg_ctoi_ret_F1_info\n" );
402 } else
403 if (c == (StgClosure*)&stg_ctoi_D1_info) {
404 debugBelch("\t\t\tstg_ctoi_ret_D1_info\n" );
405 } else
406 if (c == (StgClosure*)&stg_ctoi_V_info) {
407 debugBelch("\t\t\tstg_ctoi_ret_V_info\n" );
408 } else
409 if (get_itbl(c)->type == BCO) {
410 debugBelch("\t\t\t");
411 debugBelch("BCO(...)\n");
412 }
413 else {
414 debugBelch("\t\t\t");
415 printClosure ( (StgClosure*)(*sp));
416 }
417 sp += 1;
418
419 return sp;
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 printObj((StgClosure*)sp);
479 continue;
480
481 case RET_DYN:
482 {
483 StgRetDyn* r;
484 StgPtr p;
485 StgWord dyn;
486 nat size;
487
488 r = (StgRetDyn *)sp;
489 dyn = r->liveness;
490 debugBelch("RET_DYN (%p)\n", r);
491
492 p = (P_)(r->payload);
493 printSmallBitmap(spBottom, sp,
494 RET_DYN_LIVENESS(r->liveness),
495 RET_DYN_BITMAP_SIZE);
496 p += RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE;
497
498 for (size = RET_DYN_NONPTRS(dyn); size > 0; size--) {
499 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
500 debugBelch("Word# %ld\n", (long)*p);
501 p++;
502 }
503
504 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
505 debugBelch(" stk[%ld] (%p) = ", (long)(spBottom-p), p);
506 printPtr(p);
507 p++;
508 }
509 continue;
510 }
511
512 case RET_SMALL:
513 debugBelch("RET_SMALL (%p)\n", info);
514 bitmap = info->layout.bitmap;
515 printSmallBitmap(spBottom, sp+1,
516 BITMAP_BITS(bitmap), BITMAP_SIZE(bitmap));
517 continue;
518
519 case RET_BCO: {
520 StgBCO *bco;
521
522 bco = ((StgBCO *)sp[1]);
523
524 debugBelch("RET_BCO (%p)\n", sp);
525 printLargeBitmap(spBottom, sp+2,
526 BCO_BITMAP(bco), BCO_BITMAP_SIZE(bco));
527 continue;
528 }
529
530 case RET_BIG:
531 barf("todo");
532
533 case RET_FUN:
534 {
535 StgFunInfoTable *fun_info;
536 StgRetFun *ret_fun;
537
538 ret_fun = (StgRetFun *)sp;
539 fun_info = get_fun_itbl(ret_fun->fun);
540 debugBelch("RET_FUN (%p) (type=%d)\n", ret_fun->fun, (int)fun_info->f.fun_type);
541 switch (fun_info->f.fun_type) {
542 case ARG_GEN:
543 printSmallBitmap(spBottom, sp+2,
544 BITMAP_BITS(fun_info->f.b.bitmap),
545 BITMAP_SIZE(fun_info->f.b.bitmap));
546 break;
547 case ARG_GEN_BIG:
548 printLargeBitmap(spBottom, sp+2,
549 GET_FUN_LARGE_BITMAP(fun_info),
550 GET_FUN_LARGE_BITMAP(fun_info)->size);
551 break;
552 default:
553 printSmallBitmap(spBottom, sp+2,
554 BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]),
555 BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]));
556 break;
557 }
558 continue;
559 }
560
561 default:
562 debugBelch("unknown object %d\n", (int)info->type);
563 barf("printStackChunk");
564 }
565 }
566 }
567
568 void printTSO( StgTSO *tso )
569 {
570 printStackChunk( tso->stackobj->sp,
571 tso->stackobj->stack+tso->stackobj->stack_size);
572 }
573
574 /* --------------------------------------------------------------------------
575 * Address printing code
576 *
577 * Uses symbol table in (unstripped executable)
578 * ------------------------------------------------------------------------*/
579
580 /* --------------------------------------------------------------------------
581 * Simple lookup table
582 *
583 * Current implementation is pretty dumb!
584 * ------------------------------------------------------------------------*/
585
586 struct entry {
587 StgWord value;
588 const char *name;
589 };
590
591 static nat table_size;
592 static struct entry* table;
593
594 #ifdef USING_LIBBFD
595 static nat max_table_size;
596
597 static void reset_table( int size )
598 {
599 max_table_size = size;
600 table_size = 0;
601 table = (struct entry *)stgMallocBytes(size * sizeof(struct entry), "Printer.c:reset_table()");
602 }
603
604 static void prepare_table( void )
605 {
606 /* Could sort it... */
607 }
608
609 static void insert( StgWord value, const char *name )
610 {
611 if ( table_size >= max_table_size ) {
612 barf( "Symbol table overflow\n" );
613 }
614 table[table_size].value = value;
615 table[table_size].name = name;
616 table_size = table_size + 1;
617 }
618 #endif
619
620 #if 0
621 static rtsBool lookup_name( char *name, StgWord *result )
622 {
623 nat i;
624 for( i = 0; i < table_size && strcmp(name,table[i].name) != 0; ++i ) {
625 }
626 if (i < table_size) {
627 *result = table[i].value;
628 return rtsTrue;
629 } else {
630 return rtsFalse;
631 }
632 }
633 #endif
634
635 /* Code from somewhere inside GHC (circa 1994)
636 * * Z-escapes:
637 * "std"++xs -> "Zstd"++xs
638 * char_to_c 'Z' = "ZZ"
639 * char_to_c '&' = "Za"
640 * char_to_c '|' = "Zb"
641 * char_to_c ':' = "Zc"
642 * char_to_c '/' = "Zd"
643 * char_to_c '=' = "Ze"
644 * char_to_c '>' = "Zg"
645 * char_to_c '#' = "Zh"
646 * char_to_c '<' = "Zl"
647 * char_to_c '-' = "Zm"
648 * char_to_c '!' = "Zn"
649 * char_to_c '.' = "Zo"
650 * char_to_c '+' = "Zp"
651 * char_to_c '\'' = "Zq"
652 * char_to_c '*' = "Zt"
653 * char_to_c '_' = "Zu"
654 * char_to_c c = "Z" ++ show (ord c)
655 */
656 static char unZcode( char ch )
657 {
658 switch (ch) {
659 case 'a' : return ('&');
660 case 'b' : return ('|');
661 case 'c' : return (':');
662 case 'd' : return ('/');
663 case 'e' : return ('=');
664 case 'g' : return ('>');
665 case 'h' : return ('#');
666 case 'l' : return ('<');
667 case 'm' : return ('-');
668 case 'n' : return ('!');
669 case 'o' : return ('.');
670 case 'p' : return ('+');
671 case 'q' : return ('\'');
672 case 't' : return ('*');
673 case 'u' : return ('_');
674 case 'Z' :
675 case '\0' : return ('Z');
676 default : return (ch);
677 }
678 }
679
680 #if 0
681 /* Precondition: out big enough to handle output (about twice length of in) */
682 static void enZcode( char *in, char *out )
683 {
684 int i, j;
685
686 j = 0;
687 out[ j++ ] = '_';
688 for( i = 0; in[i] != '\0'; ++i ) {
689 switch (in[i]) {
690 case 'Z' :
691 out[j++] = 'Z';
692 out[j++] = 'Z';
693 break;
694 case '&' :
695 out[j++] = 'Z';
696 out[j++] = 'a';
697 break;
698 case '|' :
699 out[j++] = 'Z';
700 out[j++] = 'b';
701 break;
702 case ':' :
703 out[j++] = 'Z';
704 out[j++] = 'c';
705 break;
706 case '/' :
707 out[j++] = 'Z';
708 out[j++] = 'd';
709 break;
710 case '=' :
711 out[j++] = 'Z';
712 out[j++] = 'e';
713 break;
714 case '>' :
715 out[j++] = 'Z';
716 out[j++] = 'g';
717 break;
718 case '#' :
719 out[j++] = 'Z';
720 out[j++] = 'h';
721 break;
722 case '<' :
723 out[j++] = 'Z';
724 out[j++] = 'l';
725 break;
726 case '-' :
727 out[j++] = 'Z';
728 out[j++] = 'm';
729 break;
730 case '!' :
731 out[j++] = 'Z';
732 out[j++] = 'n';
733 break;
734 case '.' :
735 out[j++] = 'Z';
736 out[j++] = 'o';
737 break;
738 case '+' :
739 out[j++] = 'Z';
740 out[j++] = 'p';
741 break;
742 case '\'' :
743 out[j++] = 'Z';
744 out[j++] = 'q';
745 break;
746 case '*' :
747 out[j++] = 'Z';
748 out[j++] = 't';
749 break;
750 case '_' :
751 out[j++] = 'Z';
752 out[j++] = 'u';
753 break;
754 default :
755 out[j++] = in[i];
756 break;
757 }
758 }
759 out[j] = '\0';
760 }
761 #endif
762
763 const char *lookupGHCName( void *addr )
764 {
765 nat i;
766 for( i = 0; i < table_size && table[i].value != (StgWord) addr; ++i ) {
767 }
768 if (i < table_size) {
769 return table[i].name;
770 } else {
771 return NULL;
772 }
773 }
774
775 static void printZcoded( const char *raw )
776 {
777 nat j = 0;
778
779 while ( raw[j] != '\0' ) {
780 if (raw[j] == 'Z') {
781 debugBelch("%c", unZcode(raw[j+1]));
782 j = j + 2;
783 } else {
784 debugBelch("%c", unZcode(raw[j+1]));
785 j = j + 1;
786 }
787 }
788 }
789
790 /* --------------------------------------------------------------------------
791 * Symbol table loading
792 * ------------------------------------------------------------------------*/
793
794 /* Causing linking trouble on Win32 plats, so I'm
795 disabling this for now.
796 */
797 #ifdef USING_LIBBFD
798
799 #include <bfd.h>
800
801 /* Fairly ad-hoc piece of code that seems to filter out a lot of
802 * rubbish like the obj-splitting symbols
803 */
804
805 static rtsBool isReal( flagword flags STG_UNUSED, const char *name )
806 {
807 #if 0
808 /* ToDo: make this work on BFD */
809 int tp = type & N_TYPE;
810 if (tp == N_TEXT || tp == N_DATA) {
811 return (name[0] == '_' && name[1] != '_');
812 } else {
813 return rtsFalse;
814 }
815 #else
816 if (*name == '\0' ||
817 (name[0] == 'g' && name[1] == 'c' && name[2] == 'c') ||
818 (name[0] == 'c' && name[1] == 'c' && name[2] == '.')) {
819 return rtsFalse;
820 }
821 return rtsTrue;
822 #endif
823 }
824
825 extern void DEBUG_LoadSymbols( char *name )
826 {
827 bfd* abfd;
828 char **matching;
829
830 bfd_init();
831 abfd = bfd_openr(name, "default");
832 if (abfd == NULL) {
833 barf("can't open executable %s to get symbol table", name);
834 }
835 if (!bfd_check_format_matches (abfd, bfd_object, &matching)) {
836 barf("mismatch");
837 }
838
839 {
840 long storage_needed;
841 asymbol **symbol_table;
842 long number_of_symbols;
843 long num_real_syms = 0;
844 long i;
845
846 storage_needed = bfd_get_symtab_upper_bound (abfd);
847
848 if (storage_needed < 0) {
849 barf("can't read symbol table");
850 }
851 #if 0
852 if (storage_needed == 0) {
853 debugBelch("no storage needed");
854 }
855 #endif
856 symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols");
857
858 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
859
860 if (number_of_symbols < 0) {
861 barf("can't canonicalise symbol table");
862 }
863
864 for( i = 0; i != number_of_symbols; ++i ) {
865 symbol_info info;
866 bfd_get_symbol_info(abfd,symbol_table[i],&info);
867 /*debugBelch("\t%c\t0x%x \t%s\n",info.type,(nat)info.value,info.name); */
868 if (isReal(info.type, info.name)) {
869 num_real_syms += 1;
870 }
871 }
872
873 IF_DEBUG(interpreter,
874 debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n",
875 number_of_symbols, num_real_syms)
876 );
877
878 reset_table( num_real_syms );
879
880 for( i = 0; i != number_of_symbols; ++i ) {
881 symbol_info info;
882 bfd_get_symbol_info(abfd,symbol_table[i],&info);
883 if (isReal(info.type, info.name)) {
884 insert( info.value, info.name );
885 }
886 }
887
888 stgFree(symbol_table);
889 }
890 prepare_table();
891 }
892
893 #else /* HAVE_BFD_H */
894
895 extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
896 {
897 /* nothing, yet */
898 }
899
900 #endif /* HAVE_BFD_H */
901
902 void findPtr(P_ p, int); /* keep gcc -Wall happy */
903
904 int searched = 0;
905
906 static int
907 findPtrBlocks (StgPtr p, bdescr *bd, StgPtr arr[], int arr_size, int i)
908 {
909 StgPtr q, r, end;
910 for (; bd; bd = bd->link) {
911 searched++;
912 for (q = bd->start; q < bd->free; q++) {
913 if (UNTAG_CLOSURE((StgClosure*)*q) == (StgClosure *)p) {
914 if (i < arr_size) {
915 for (r = bd->start; r < bd->free; r = end) {
916 // skip over zeroed-out slop
917 while (*r == 0) r++;
918 if (!LOOKS_LIKE_CLOSURE_PTR(r)) {
919 debugBelch("%p found at %p, no closure at %p\n",
920 p, q, r);
921 break;
922 }
923 end = r + closure_sizeW((StgClosure*)r);
924 if (q < end) {
925 debugBelch("%p = ", r);
926 printClosure((StgClosure *)r);
927 arr[i++] = r;
928 break;
929 }
930 }
931 if (r >= bd->free) {
932 debugBelch("%p found at %p, closure?", p, q);
933 }
934 } else {
935 return i;
936 }
937 }
938 }
939 }
940 return i;
941 }
942
943 void
944 findPtr(P_ p, int follow)
945 {
946 nat g, n;
947 bdescr *bd;
948 const int arr_size = 1024;
949 StgPtr arr[arr_size];
950 int i = 0;
951 searched = 0;
952
953 for (n = 0; n < n_capabilities; n++) {
954 bd = nurseries[i].blocks;
955 i = findPtrBlocks(p,bd,arr,arr_size,i);
956 if (i >= arr_size) return;
957 }
958
959 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
960 bd = generations[g].blocks;
961 i = findPtrBlocks(p,bd,arr,arr_size,i);
962 bd = generations[g].large_objects;
963 i = findPtrBlocks(p,bd,arr,arr_size,i);
964 if (i >= arr_size) return;
965 }
966 if (follow && i == 1) {
967 debugBelch("-->\n");
968 findPtr(arr[0], 1);
969 }
970 }
971
972 /* prettyPrintClosure() is for printing out a closure using the data constructor
973 names found in the info tables. Closures are printed in a fashion that resembles
974 their Haskell representation. Useful during debugging.
975
976 Todo: support for more closure types, and support for non pointer fields in the
977 payload.
978 */
979
980 void prettyPrintClosure_ (StgClosure *);
981
982 void prettyPrintClosure (StgClosure *obj)
983 {
984 prettyPrintClosure_ (obj);
985 debugBelch ("\n");
986 }
987
988 void prettyPrintClosure_ (StgClosure *obj)
989 {
990 StgInfoTable *info;
991 StgConInfoTable *con_info;
992
993 /* collapse any indirections */
994 unsigned int type;
995 type = get_itbl(obj)->type;
996
997 while (type == IND ||
998 type == IND_STATIC ||
999 type == IND_PERM)
1000 {
1001 obj = ((StgInd *)obj)->indirectee;
1002 type = get_itbl(obj)->type;
1003 }
1004
1005 /* find the info table for this object */
1006 info = get_itbl(obj);
1007
1008 /* determine what kind of object we have */
1009 switch (info->type)
1010 {
1011 /* full applications of data constructors */
1012 case CONSTR:
1013 case CONSTR_1_0:
1014 case CONSTR_0_1:
1015 case CONSTR_1_1:
1016 case CONSTR_0_2:
1017 case CONSTR_2_0:
1018 case CONSTR_STATIC:
1019 case CONSTR_NOCAF_STATIC:
1020 {
1021 nat i;
1022 char *descriptor;
1023
1024 /* find the con_info for the constructor */
1025 con_info = get_con_itbl (obj);
1026
1027 /* obtain the name of the constructor */
1028 descriptor = GET_CON_DESC(con_info);
1029
1030 debugBelch ("(%s", descriptor);
1031
1032 /* process the payload of the closure */
1033 /* we don't handle non pointers at the moment */
1034 for (i = 0; i < info->layout.payload.ptrs; i++)
1035 {
1036 debugBelch (" ");
1037 prettyPrintClosure_ ((StgClosure *) obj->payload[i]);
1038 }
1039 debugBelch (")");
1040 break;
1041 }
1042
1043 /* if it isn't a constructor then just print the closure type */
1044 default:
1045 {
1046 debugBelch ("<%s>", info_type(obj));
1047 break;
1048 }
1049 }
1050 }
1051
1052 char *what_next_strs[] = {
1053 [0] = "(unknown)",
1054 [ThreadRunGHC] = "ThreadRunGHC",
1055 [ThreadInterpret] = "ThreadInterpret",
1056 [ThreadKilled] = "ThreadKilled",
1057 [ThreadComplete] = "ThreadComplete"
1058 };
1059
1060 #else /* DEBUG */
1061 void printPtr( StgPtr p )
1062 {
1063 debugBelch("ptr 0x%p (enable -DDEBUG for more info) " , p );
1064 }
1065
1066 void printObj( StgClosure *obj )
1067 {
1068 debugBelch("obj 0x%p (enable -DDEBUG for more info) " , obj );
1069 }
1070
1071
1072 #endif /* DEBUG */
1073
1074 /* -----------------------------------------------------------------------------
1075 Closure types
1076
1077 NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
1078 -------------------------------------------------------------------------- */
1079
1080 char *closure_type_names[] = {
1081 [INVALID_OBJECT] = "INVALID_OBJECT",
1082 [CONSTR] = "CONSTR",
1083 [CONSTR_1_0] = "CONSTR_1_0",
1084 [CONSTR_0_1] = "CONSTR_0_1",
1085 [CONSTR_2_0] = "CONSTR_2_0",
1086 [CONSTR_1_1] = "CONSTR_1_1",
1087 [CONSTR_0_2] = "CONSTR_0_2",
1088 [CONSTR_STATIC] = "CONSTR_STATIC",
1089 [CONSTR_NOCAF_STATIC] = "CONSTR_NOCAF_STATIC",
1090 [FUN] = "FUN",
1091 [FUN_1_0] = "FUN_1_0",
1092 [FUN_0_1] = "FUN_0_1",
1093 [FUN_2_0] = "FUN_2_0",
1094 [FUN_1_1] = "FUN_1_1",
1095 [FUN_0_2] = "FUN_0_2",
1096 [FUN_STATIC] = "FUN_STATIC",
1097 [THUNK] = "THUNK",
1098 [THUNK_1_0] = "THUNK_1_0",
1099 [THUNK_0_1] = "THUNK_0_1",
1100 [THUNK_2_0] = "THUNK_2_0",
1101 [THUNK_1_1] = "THUNK_1_1",
1102 [THUNK_0_2] = "THUNK_0_2",
1103 [THUNK_STATIC] = "THUNK_STATIC",
1104 [THUNK_SELECTOR] = "THUNK_SELECTOR",
1105 [BCO] = "BCO",
1106 [AP] = "AP",
1107 [PAP] = "PAP",
1108 [AP_STACK] = "AP_STACK",
1109 [IND] = "IND",
1110 [IND_PERM] = "IND_PERM",
1111 [IND_STATIC] = "IND_STATIC",
1112 [RET_BCO] = "RET_BCO",
1113 [RET_SMALL] = "RET_SMALL",
1114 [RET_BIG] = "RET_BIG",
1115 [RET_DYN] = "RET_DYN",
1116 [RET_FUN] = "RET_FUN",
1117 [UPDATE_FRAME] = "UPDATE_FRAME",
1118 [CATCH_FRAME] = "CATCH_FRAME",
1119 [UNDERFLOW_FRAME] = "UNDERFLOW_FRAME",
1120 [STOP_FRAME] = "STOP_FRAME",
1121 [BLOCKING_QUEUE] = "BLOCKING_QUEUE",
1122 [BLACKHOLE] = "BLACKHOLE",
1123 [MVAR_CLEAN] = "MVAR_CLEAN",
1124 [MVAR_DIRTY] = "MVAR_DIRTY",
1125 [ARR_WORDS] = "ARR_WORDS",
1126 [MUT_ARR_PTRS_CLEAN] = "MUT_ARR_PTRS_CLEAN",
1127 [MUT_ARR_PTRS_DIRTY] = "MUT_ARR_PTRS_DIRTY",
1128 [MUT_ARR_PTRS_FROZEN0] = "MUT_ARR_PTRS_FROZEN0",
1129 [MUT_ARR_PTRS_FROZEN] = "MUT_ARR_PTRS_FROZEN",
1130 [MUT_VAR_CLEAN] = "MUT_VAR_CLEAN",
1131 [MUT_VAR_DIRTY] = "MUT_VAR_DIRTY",
1132 [WEAK] = "WEAK",
1133 [PRIM] = "PRIM",
1134 [MUT_PRIM] = "MUT_PRIM",
1135 [TSO] = "TSO",
1136 [STACK] = "STACK",
1137 [TREC_CHUNK] = "TREC_CHUNK",
1138 [ATOMICALLY_FRAME] = "ATOMICALLY_FRAME",
1139 [CATCH_RETRY_FRAME] = "CATCH_RETRY_FRAME",
1140 [CATCH_STM_FRAME] = "CATCH_STM_FRAME",
1141 [WHITEHOLE] = "WHITEHOLE"
1142 };
1143
1144 char *
1145 info_type(StgClosure *closure){
1146 return closure_type_names[get_itbl(closure)->type];
1147 }
1148
1149 char *
1150 info_type_by_ip(StgInfoTable *ip){
1151 return closure_type_names[ip->type];
1152 }
1153
1154 void
1155 info_hdr_type(StgClosure *closure, char *res){
1156 strcpy(res,closure_type_names[get_itbl(closure)->type]);
1157 }
1158