Add more sanity checking
[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 #ifndef GHC_7_7
66  [RET_DYN]               = "RET_DYN",
67 #endif
68  [RET_FUN]               = "RET_FUN",
69  [UPDATE_FRAME]          = "UPDATE_FRAME",
70  [CATCH_FRAME]           = "CATCH_FRAME",
71  [UNDERFLOW_FRAME]       = "UNDERFLOW_FRAME",
72  [STOP_FRAME]            = "STOP_FRAME",
73  [BLACKHOLE]             = "BLACKHOLE",
74  [BLOCKING_QUEUE]        = "BLOCKING_QUEUE",
75  [MVAR_CLEAN]            = "MVAR_CLEAN",
76  [MVAR_DIRTY]            = "MVAR_DIRTY",
77 #ifdef GHC_7_7
78  [TVAR]                  = "TVAR",
79 #endif
80  [ARR_WORDS]             = "ARR_WORDS",
81  [MUT_ARR_PTRS_CLEAN]    = "MUT_ARR_PTRS_CLEAN",
82  [MUT_ARR_PTRS_DIRTY]    = "MUT_ARR_PTRS_DIRTY",
83  [MUT_ARR_PTRS_FROZEN0]  = "MUT_ARR_PTRS_FROZEN0",
84  [MUT_ARR_PTRS_FROZEN]   = "MUT_ARR_PTRS_FROZEN",
85  [MUT_VAR_CLEAN]         = "MUT_VAR_CLEAN",
86  [MUT_VAR_DIRTY]         = "MUT_VAR_DIRTY",
87  [WEAK]                  = "WEAK",
88  [PRIM]                  = "PRIM",
89  [MUT_PRIM]              = "MUT_PRIM",
90  [TSO]                   = "TSO",
91  [STACK]                 = "STACK",
92  [TREC_CHUNK]            = "TREC_CHUNK",
93  [ATOMICALLY_FRAME]      = "ATOMICALLY_FRAME",
94  [CATCH_RETRY_FRAME]     = "CATCH_RETRY_FRAME",
95  [CATCH_STM_FRAME]       = "CATCH_STM_FRAME",
96  [WHITEHOLE]             = "WHITEHOLE"
97 };
98
99
100 void gtc_heap_view_closure_ptrs_in_pap_payload(StgClosure *ptrs[], StgWord *nptrs, StgClosure *fun, StgClosure **payload, StgWord size) {
101     StgWord bitmap;
102     StgFunInfoTable *fun_info;
103
104     fun_info = get_fun_itbl(UNTAG_CLOSURE(fun));
105     // ASSERT(fun_info->i.type != PAP);
106     StgClosure **p = payload;
107
108     switch (fun_info->f.fun_type) {
109     case ARG_GEN:
110         bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
111         goto small_bitmap;
112     case ARG_GEN_BIG:
113         gtc_heap_view_closure_ptrs_in_large_bitmap(ptrs, nptrs, payload, GET_FUN_LARGE_BITMAP(fun_info), size);
114         break;
115     case ARG_BCO:
116         gtc_heap_view_closure_ptrs_in_large_bitmap(ptrs, nptrs, payload, BCO_BITMAP(fun), size);
117         break;
118     default:
119         bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
120     small_bitmap:
121         while (size > 0) {
122             if ((bitmap & 1) == 0) {
123                 ptrs[(*nptrs)++] = *p;
124             }
125             bitmap = bitmap >> 1;
126             p++;
127             size--;
128         }
129         break;
130     }
131 }
132
133 StgMutArrPtrs *gtc_heap_view_closurePtrs(Capability *cap, StgClosure *closure) {
134     ASSERT(LOOKS_LIKE_CLOSURE_PTR(closure));
135
136     StgWord size = gtc_heap_view_closureSize(closure);
137     StgWord nptrs = 0;
138     StgWord i;
139
140     // First collect all pointers here, with the comfortable memory bound
141     // of the whole closure. Afterwards we know how many pointers are in
142     // the closure and then we can allocate space on the heap and copy them
143     // there
144     StgClosure *ptrs[size];
145
146     StgClosure **end;
147     StgClosure **ptr;
148
149     StgInfoTable *info = get_itbl(closure);
150     StgThunkInfoTable *thunk_info;
151     StgFunInfoTable *fun_info;
152
153     switch (info->type) {
154         case INVALID_OBJECT:
155             barf("Invalid Object");
156             break;
157
158         // No pointers
159         case ARR_WORDS:
160             break;
161
162         // Default layout
163         case CONSTR_1_0:
164         case CONSTR_0_1:
165         case CONSTR_2_0:
166         case CONSTR_1_1:
167         case CONSTR_0_2:
168         case CONSTR:
169         case CONSTR_STATIC:
170         case CONSTR_NOCAF_STATIC:
171         case PRIM:
172
173         case FUN:
174         case FUN_1_0:
175         case FUN_0_1:
176         case FUN_1_1:
177         case FUN_2_0:
178         case FUN_0_2:
179         case FUN_STATIC:
180             end = closure->payload + info->layout.payload.ptrs;
181             for (ptr = closure->payload; ptr < end; ptr++) {
182                 ptrs[nptrs++] = *ptr;
183             }
184             break;
185
186         case THUNK:
187         case THUNK_1_0:
188         case THUNK_0_1:
189         case THUNK_1_1:
190         case THUNK_2_0:
191         case THUNK_0_2:
192         case THUNK_STATIC:
193             end = ((StgThunk *)closure)->payload + info->layout.payload.ptrs;
194             for (ptr = ((StgThunk *)closure)->payload; ptr < end; ptr++) {
195                 ptrs[nptrs++] = *ptr;
196             }
197             break;
198
199         case THUNK_SELECTOR:
200             ptrs[nptrs++] = ((StgSelector *)closure)->selectee;
201             break;
202             
203         case AP:
204             ptrs[nptrs++] = ((StgAP *)closure)->fun;
205             gtc_heap_view_closure_ptrs_in_pap_payload(ptrs, &nptrs,
206                 ((StgAP *)closure)->fun,
207                 ((StgAP *)closure)->payload,
208                 ((StgAP *)closure)->n_args);
209             break;
210             
211         case PAP:
212             ptrs[nptrs++] = ((StgPAP *)closure)->fun;
213             gtc_heap_view_closure_ptrs_in_pap_payload(ptrs, &nptrs,
214                 ((StgPAP *)closure)->fun,
215                 ((StgPAP *)closure)->payload,
216                 ((StgPAP *)closure)->n_args);
217             break;
218             
219         case AP_STACK:
220             ptrs[nptrs++] = ((StgAP_STACK *)closure)->fun;
221             for (i = 0; i < ((StgAP_STACK *)closure)->size; ++i) {
222                 ptrs[nptrs++] = ((StgAP_STACK *)closure)->payload[i];
223             }
224             break;
225             
226         case BCO:
227             ptrs[nptrs++] = (StgClosure *)((StgBCO *)closure)->instrs;
228             ptrs[nptrs++] = (StgClosure *)((StgBCO *)closure)->literals;
229             ptrs[nptrs++] = (StgClosure *)((StgBCO *)closure)->ptrs;
230             break;
231             
232         case IND:
233         case IND_PERM:
234         case IND_STATIC:
235         case BLACKHOLE:
236             ptrs[nptrs++] = (StgClosure *)(((StgInd *)closure)->indirectee);
237             break;
238
239         case MUT_ARR_PTRS_CLEAN:
240         case MUT_ARR_PTRS_DIRTY:
241         case MUT_ARR_PTRS_FROZEN:
242         case MUT_ARR_PTRS_FROZEN0:
243             for (i = 0; i < ((StgMutArrPtrs *)closure)->ptrs; ++i) {
244                 ptrs[nptrs++] = ((StgMutArrPtrs *)closure)->payload[i];
245             }
246             break;
247         case MUT_VAR_CLEAN:
248             ptrs[nptrs++] = ((StgMutVar *)closure)->var;
249             break;
250         case MVAR_CLEAN:
251             ptrs[nptrs++] = (StgClosure *)((StgMVar *)closure)->head;
252             ptrs[nptrs++] = (StgClosure *)((StgMVar *)closure)->tail;
253             ptrs[nptrs++] = ((StgMVar *)closure)->value;
254             break;
255
256         default:
257             //fprintf(stderr,"closurePtrs: Cannot handle type %s yet\n", gtc_heap_view_closure_type_names[info->type]);
258             break;
259     }
260
261     size = nptrs + mutArrPtrsCardTableSize(nptrs);
262     StgMutArrPtrs *arr = 
263         (StgMutArrPtrs *)allocate(cap, sizeofW(StgMutArrPtrs) + size);
264     TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), nptrs, 0);
265     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCCS);
266     arr->ptrs = nptrs;
267     arr->size = size;
268
269     for (i = 0; i<nptrs; i++) {
270         arr->payload[i] = ptrs[i];
271     }
272
273     return arr;
274 }