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