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