Advertise ghci support in the package description
[ghc-heap-view.git] / cbits / HeapView.c
1 #include "Rts.h"
2
3 StgWord gtc_heap_view_closureSize(StgClosure *closure) {
4     ASSERT(LOOKS_LIKE_CLOSURE_PTR(closure));
5     return closure_sizeW(closure);
6 }
7
8 static void
9 gtc_heap_view_closure_ptrs_in_large_bitmap(StgClosure *ptrs[], StgWord *nptrs, StgClosure **p, StgLargeBitmap *large_bitmap, nat size )
10 {
11     nat i, j, b;
12     StgWord bitmap;
13
14     b = 0;
15
16     for (i = 0; i < size; b++) {
17         bitmap = large_bitmap->bitmap[b];
18         j = stg_min(size-i, BITS_IN(W_));
19         i += j;
20         for (; j > 0; j--, p++) {
21             if ((bitmap & 1) == 0) {
22                 ptrs[(*nptrs)++] = *p;
23             }
24             bitmap = bitmap >> 1;
25         }            
26     }
27 }
28
29 // from rts/Printer.c
30 char *gtc_heap_view_closure_type_names[] = {
31  [INVALID_OBJECT]        = "INVALID_OBJECT",
32  [CONSTR]                = "CONSTR",
33  [CONSTR_1_0]            = "CONSTR_1_0",
34  [CONSTR_0_1]            = "CONSTR_0_1",
35  [CONSTR_2_0]            = "CONSTR_2_0",
36  [CONSTR_1_1]            = "CONSTR_1_1",
37  [CONSTR_0_2]            = "CONSTR_0_2",
38  [CONSTR_STATIC]         = "CONSTR_STATIC",
39  [CONSTR_NOCAF_STATIC]   = "CONSTR_NOCAF_STATIC",
40  [FUN]                   = "FUN",
41  [FUN_1_0]               = "FUN_1_0",
42  [FUN_0_1]               = "FUN_0_1",
43  [FUN_2_0]               = "FUN_2_0",
44  [FUN_1_1]               = "FUN_1_1",
45  [FUN_0_2]               = "FUN_0_2",
46  [FUN_STATIC]            = "FUN_STATIC",
47  [THUNK]                 = "THUNK",
48  [THUNK_1_0]             = "THUNK_1_0",
49  [THUNK_0_1]             = "THUNK_0_1",
50  [THUNK_2_0]             = "THUNK_2_0",
51  [THUNK_1_1]             = "THUNK_1_1",
52  [THUNK_0_2]             = "THUNK_0_2",
53  [THUNK_STATIC]          = "THUNK_STATIC",
54  [THUNK_SELECTOR]        = "THUNK_SELECTOR",
55  [BCO]                   = "BCO",
56  [AP]                    = "AP",
57  [PAP]                   = "PAP",
58  [AP_STACK]              = "AP_STACK",
59  [IND]                   = "IND",
60  [IND_PERM]              = "IND_PERM",
61  [IND_STATIC]            = "IND_STATIC",
62  [RET_BCO]               = "RET_BCO",
63  [RET_SMALL]             = "RET_SMALL",
64  [RET_BIG]               = "RET_BIG",
65  [RET_DYN]               = "RET_DYN",
66  [RET_FUN]               = "RET_FUN",
67  [UPDATE_FRAME]          = "UPDATE_FRAME",
68  [CATCH_FRAME]           = "CATCH_FRAME",
69  [UNDERFLOW_FRAME]       = "UNDERFLOW_FRAME",
70  [STOP_FRAME]            = "STOP_FRAME",
71  [BLACKHOLE]             = "BLACKHOLE",
72  [BLOCKING_QUEUE]        = "BLOCKING_QUEUE",
73  [MVAR_CLEAN]            = "MVAR_CLEAN",
74  [MVAR_DIRTY]            = "MVAR_DIRTY",
75  [ARR_WORDS]             = "ARR_WORDS",
76  [MUT_ARR_PTRS_CLEAN]    = "MUT_ARR_PTRS_CLEAN",
77  [MUT_ARR_PTRS_DIRTY]    = "MUT_ARR_PTRS_DIRTY",
78  [MUT_ARR_PTRS_FROZEN0]  = "MUT_ARR_PTRS_FROZEN0",
79  [MUT_ARR_PTRS_FROZEN]   = "MUT_ARR_PTRS_FROZEN",
80  [MUT_VAR_CLEAN]         = "MUT_VAR_CLEAN",
81  [MUT_VAR_DIRTY]         = "MUT_VAR_DIRTY",
82  [WEAK]                  = "WEAK",
83  [PRIM]                  = "PRIM",
84  [MUT_PRIM]              = "MUT_PRIM",
85  [TSO]                   = "TSO",
86  [STACK]                 = "STACK",
87  [TREC_CHUNK]            = "TREC_CHUNK",
88  [ATOMICALLY_FRAME]      = "ATOMICALLY_FRAME",
89  [CATCH_RETRY_FRAME]     = "CATCH_RETRY_FRAME",
90  [CATCH_STM_FRAME]       = "CATCH_STM_FRAME",
91  [WHITEHOLE]             = "WHITEHOLE"
92 };
93
94
95 void gtc_heap_view_closure_ptrs_in_pap_payload(StgClosure *ptrs[], StgWord *nptrs, StgClosure *fun, StgClosure **payload, StgWord size) {
96     StgWord bitmap;
97     StgFunInfoTable *fun_info;
98
99     fun_info = get_fun_itbl(UNTAG_CLOSURE(fun));
100     // ASSERT(fun_info->i.type != PAP);
101     StgClosure **p = payload;
102
103     switch (fun_info->f.fun_type) {
104     case ARG_GEN:
105         bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
106         goto small_bitmap;
107     case ARG_GEN_BIG:
108         gtc_heap_view_closure_ptrs_in_large_bitmap(ptrs, nptrs, payload, GET_FUN_LARGE_BITMAP(fun_info), size);
109         break;
110     case ARG_BCO:
111         gtc_heap_view_closure_ptrs_in_large_bitmap(ptrs, nptrs, payload, BCO_BITMAP(fun), size);
112         break;
113     default:
114         bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
115     small_bitmap:
116         while (size > 0) {
117             if ((bitmap & 1) == 0) {
118                 ptrs[(*nptrs)++] = *p;
119             }
120             bitmap = bitmap >> 1;
121             p++;
122             size--;
123         }
124         break;
125     }
126 }
127
128 StgMutArrPtrs *gtc_heap_view_closurePtrs(Capability *cap, StgClosure *closure) {
129     ASSERT(LOOKS_LIKE_CLOSURE_PTR(closure));
130
131     StgWord size = gtc_heap_view_closureSize(closure);
132     StgWord nptrs = 0;
133     StgWord i;
134
135     // First collect all pointers here, with the comfortable memory bound
136     // of the whole closure. Afterwards we know how many pointers are in
137     // the closure and then we can allocate space on the heap and copy them
138     // there
139     StgClosure *ptrs[size];
140
141     StgClosure **end;
142     StgClosure **ptr;
143
144     StgInfoTable *info = get_itbl(closure);
145     StgThunkInfoTable *thunk_info;
146     StgFunInfoTable *fun_info;
147
148     switch (info->type) {
149         case INVALID_OBJECT:
150             barf("Invalid Object");
151             break;
152
153         // No pointers
154         case ARR_WORDS:
155             break;
156
157         // Default layout
158         case CONSTR_1_0:
159         case CONSTR_0_1:
160         case CONSTR_2_0:
161         case CONSTR_1_1:
162         case CONSTR_0_2:
163         case CONSTR:
164         case CONSTR_STATIC:
165         case CONSTR_NOCAF_STATIC:
166         case PRIM:
167
168         case FUN:
169         case FUN_1_0:
170         case FUN_0_1:
171         case FUN_1_1:
172         case FUN_2_0:
173         case FUN_0_2:
174         case FUN_STATIC:
175             end = closure->payload + info->layout.payload.ptrs;
176             for (ptr = closure->payload; ptr < end; ptr++) {
177                 ptrs[nptrs++] = *ptr;
178             }
179             break;
180
181         case THUNK:
182         case THUNK_1_0:
183         case THUNK_0_1:
184         case THUNK_1_1:
185         case THUNK_2_0:
186         case THUNK_0_2:
187         case THUNK_STATIC:
188             end = ((StgThunk *)closure)->payload + info->layout.payload.ptrs;
189             for (ptr = ((StgThunk *)closure)->payload; ptr < end; ptr++) {
190                 ptrs[nptrs++] = *ptr;
191             }
192             break;
193
194         case THUNK_SELECTOR:
195             ptrs[nptrs++] = ((StgSelector *)closure)->selectee;
196             break;
197             
198         case AP:
199             ptrs[nptrs++] = ((StgAP *)closure)->fun;
200             gtc_heap_view_closure_ptrs_in_pap_payload(ptrs, &nptrs,
201                 ((StgAP *)closure)->fun,
202                 ((StgAP *)closure)->payload,
203                 ((StgAP *)closure)->n_args);
204             break;
205             
206         case PAP:
207             ptrs[nptrs++] = ((StgPAP *)closure)->fun;
208             gtc_heap_view_closure_ptrs_in_pap_payload(ptrs, &nptrs,
209                 ((StgPAP *)closure)->fun,
210                 ((StgPAP *)closure)->payload,
211                 ((StgPAP *)closure)->n_args);
212             break;
213             
214         case AP_STACK:
215             ptrs[nptrs++] = ((StgAP_STACK *)closure)->fun;
216             for (i = 0; i < ((StgAP_STACK *)closure)->size; ++i) {
217                 ptrs[nptrs++] = ((StgAP_STACK *)closure)->payload[i];
218             }
219             break;
220             
221         case BCO:
222             ptrs[nptrs++] = (StgClosure *)((StgBCO *)closure)->instrs;
223             ptrs[nptrs++] = (StgClosure *)((StgBCO *)closure)->literals;
224             ptrs[nptrs++] = (StgClosure *)((StgBCO *)closure)->ptrs;
225             break;
226             
227         case IND:
228         case IND_PERM:
229         case IND_STATIC:
230         case BLACKHOLE:
231             ptrs[nptrs++] = (StgClosure *)(((StgInd *)closure)->indirectee);
232             break;
233
234         case MUT_ARR_PTRS_CLEAN:
235         case MUT_ARR_PTRS_DIRTY:
236         case MUT_ARR_PTRS_FROZEN:
237         case MUT_ARR_PTRS_FROZEN0:
238             for (i = 0; i < ((StgMutArrPtrs *)closure)->ptrs; ++i) {
239                 ptrs[nptrs++] = ((StgMutArrPtrs *)closure)->payload[i];
240             }
241             break;
242         case MUT_VAR_CLEAN:
243             ptrs[nptrs++] = ((StgMutVar *)closure)->var;
244             break;
245         case MVAR_CLEAN:
246             ptrs[nptrs++] = (StgClosure *)((StgMVar *)closure)->head;
247             ptrs[nptrs++] = (StgClosure *)((StgMVar *)closure)->tail;
248             ptrs[nptrs++] = ((StgMVar *)closure)->value;
249             break;
250
251         default:
252             //fprintf(stderr,"closurePtrs: Cannot handle type %s yet\n", gtc_heap_view_closure_type_names[info->type]);
253             break;
254     }
255
256     size = nptrs + mutArrPtrsCardTableSize(nptrs);
257     StgMutArrPtrs *arr = 
258         (StgMutArrPtrs *)allocate(cap, sizeofW(StgMutArrPtrs) + size);
259     TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), nptrs, 0);
260     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCCS);
261     arr->ptrs = nptrs;
262     arr->size = size;
263
264     for (i = 0; i<nptrs; i++) {
265         arr->payload[i] = ptrs[i];
266     }
267
268     return arr;
269 }