Fix more warnings
[ghc.git] / rts / Hpc.c
1 /*
2 * (c)2006 Galois Connections, Inc.
3 */
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10
11 #include "Rts.h"
12 #include "Hpc.h"
13 #include "Trace.h"
14
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18
19
20 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
21 * inside GHC.
22 *
23 */
24
25 static int hpc_inited = 0; // Have you started this component?
26 static pid_t hpc_pid = 0; // pid of this process at hpc-boot time.
27 // Only this pid will read or write .tix file(s).
28 static FILE *tixFile; // file being read/written
29 static int tix_ch; // current char
30
31 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
32 #if SIZEOF_LONG == 8
33 #define PRIuWORD64 "lu"
34 #else
35 #define PRIuWORD64 "llu"
36 #endif
37
38 HpcModuleInfo *modules = 0;
39 HpcModuleInfo *nextModule = 0;
40 int totalTixes = 0; // total number of tix boxes.
41
42 static char *tixFilename;
43
44 static void failure(char *msg) {
45 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
46 fprintf(stderr,"Hpc failure: %s\n",msg);
47 if (tixFilename) {
48 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
49 } else {
50 fprintf(stderr,"(perhaps remove .tix file?)\n");
51 }
52 exit(-1);
53 }
54
55 static int init_open(FILE *file) {
56 tixFile = file;
57 if (tixFile == 0) {
58 return 0;
59 }
60 tix_ch = getc(tixFile);
61 return 1;
62 }
63
64 static void expect(char c) {
65 if (tix_ch != c) {
66 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
67 failure("parse error when reading .tix file");
68 }
69 tix_ch = getc(tixFile);
70 }
71
72 static void ws(void) {
73 while (tix_ch == ' ') {
74 tix_ch = getc(tixFile);
75 }
76 }
77
78 static char *expectString(void) {
79 char tmp[256], *res;
80 int tmp_ix = 0;
81 expect('"');
82 while (tix_ch != '"') {
83 tmp[tmp_ix++] = tix_ch;
84 tix_ch = getc(tixFile);
85 }
86 tmp[tmp_ix++] = 0;
87 expect('"');
88 res = malloc(tmp_ix);
89 strcpy(res,tmp);
90 return res;
91 }
92
93 static StgWord64 expectWord64(void) {
94 StgWord64 tmp = 0;
95 while (isdigit(tix_ch)) {
96 tmp = tmp * 10 + (tix_ch -'0');
97 tix_ch = getc(tixFile);
98 }
99 return tmp;
100 }
101
102 static void
103 readTix(void) {
104 unsigned int i;
105 HpcModuleInfo *tmpModule;
106
107 totalTixes = 0;
108
109 ws();
110 expect('T');
111 expect('i');
112 expect('x');
113 ws();
114 expect('[');
115 ws();
116
117 while(tix_ch != ']') {
118 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
119 expect('T');
120 expect('i');
121 expect('x');
122 expect('M');
123 expect('o');
124 expect('d');
125 expect('u');
126 expect('l');
127 expect('e');
128 ws();
129 tmpModule -> modName = expectString();
130 ws();
131 tmpModule -> hashNo = (unsigned int)expectWord64();
132 ws();
133 tmpModule -> tickCount = (int)expectWord64();
134 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
135 tmpModule -> tickOffset = totalTixes;
136 totalTixes += tmpModule -> tickCount;
137 ws();
138 expect('[');
139 ws();
140 for(i = 0;i < tmpModule->tickCount;i++) {
141 tmpModule->tixArr[i] = expectWord64();
142 ws();
143 if (tix_ch == ',') {
144 expect(',');
145 ws();
146 }
147 }
148 expect(']');
149 ws();
150
151 if (!modules) {
152 modules = tmpModule;
153 } else {
154 nextModule->next=tmpModule;
155 }
156 nextModule=tmpModule;
157
158 if (tix_ch == ',') {
159 expect(',');
160 ws();
161 }
162 }
163 expect(']');
164 fclose(tixFile);
165 }
166
167 static void hpc_init(void) {
168 if (hpc_inited != 0) {
169 return;
170 }
171 hpc_inited = 1;
172 hpc_pid = getpid();
173
174 tixFilename = (char *) malloc(strlen(prog_name) + 6);
175 sprintf(tixFilename, "%s.tix", prog_name);
176
177 if (init_open(fopen(tixFilename,"r"))) {
178 readTix();
179 }
180 }
181
182 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
183 * This memory can be uninitized, because we will initialize it with either the contents
184 * of the tix file, or all zeros.
185 */
186
187 int
188 hs_hpc_module(char *modName,
189 StgWord32 modCount,
190 StgWord32 modHashNo,
191 StgWord64 *tixArr) {
192 HpcModuleInfo *tmpModule, *lastModule;
193 unsigned int i;
194 int offset = 0;
195
196 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,(nat)modCount);
197
198 hpc_init();
199
200 tmpModule = modules;
201 lastModule = 0;
202
203 for(;tmpModule != 0;tmpModule = tmpModule->next) {
204 if (!strcmp(tmpModule->modName,modName)) {
205 if (tmpModule->tickCount != modCount) {
206 failure("inconsistent number of tick boxes");
207 }
208 assert(tmpModule->tixArr != 0);
209 if (tmpModule->hashNo != modHashNo) {
210 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
211 failure("module mismatch with .tix/.mix file hash number");
212 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
213 exit(-1);
214
215 }
216 for(i=0;i < modCount;i++) {
217 tixArr[i] = tmpModule->tixArr[i];
218 }
219 tmpModule->tixArr = tixArr;
220 return tmpModule->tickOffset;
221 }
222 lastModule = tmpModule;
223 }
224 // Did not find entry so add one on.
225 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
226 tmpModule->modName = modName;
227 tmpModule->tickCount = modCount;
228 tmpModule->hashNo = modHashNo;
229 if (lastModule) {
230 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
231 } else {
232 tmpModule->tickOffset = 0;
233 }
234 tmpModule->tixArr = tixArr;
235 for(i=0;i < modCount;i++) {
236 tixArr[i] = 0;
237 }
238 tmpModule->next = 0;
239
240 if (!modules) {
241 modules = tmpModule;
242 } else {
243 lastModule->next=tmpModule;
244 }
245
246 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
247
248 return offset;
249 }
250
251
252 /* This is called after all the modules have registered their local tixboxes,
253 * and does a sanity check: are we good to go?
254 */
255
256 void
257 startupHpc(void) {
258 debugTrace(DEBUG_hpc,"startupHpc");
259
260 if (hpc_inited == 0) {
261 return;
262 }
263 }
264
265
266 static void
267 writeTix(FILE *f) {
268 HpcModuleInfo *tmpModule;
269 unsigned int i, inner_comma, outer_comma;
270
271 outer_comma = 0;
272
273 if (f == 0) {
274 return;
275 }
276
277 fprintf(f,"Tix [");
278 tmpModule = modules;
279 for(;tmpModule != 0;tmpModule = tmpModule->next) {
280 if (outer_comma) {
281 fprintf(f,",");
282 } else {
283 outer_comma = 1;
284 }
285 fprintf(f," TixModule \"%s\" %u %u [",
286 tmpModule->modName,
287 (nat)tmpModule->hashNo,
288 (nat)tmpModule->tickCount);
289 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
290 tmpModule->modName,
291 (nat)tmpModule->tickCount,
292 (nat)tmpModule->hashNo,
293 (nat)tmpModule->tickOffset);
294
295 inner_comma = 0;
296 for(i = 0;i < tmpModule->tickCount;i++) {
297 if (inner_comma) {
298 fprintf(f,",");
299 } else {
300 inner_comma = 1;
301 }
302
303 if (tmpModule->tixArr) {
304 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
305 } else {
306 fprintf(f,"0");
307 }
308 }
309 fprintf(f,"]");
310 }
311 fprintf(f,"]\n");
312
313 fclose(f);
314 }
315
316 /* Called at the end of execution, to write out the Hpc *.tix file
317 * for this exection. Safe to call, even if coverage is not used.
318 */
319 void
320 exitHpc(void) {
321 debugTrace(DEBUG_hpc,"exitHpc");
322
323 if (hpc_inited == 0) {
324 return;
325 }
326
327 // Only write the tix file if you are the original process.
328 // Any sub-process from use of fork from inside Haskell will
329 // not clober the .tix file.
330
331 if (hpc_pid == getpid()) {
332 FILE *f = fopen(tixFilename,"w");
333 writeTix(f);
334 }
335 }
336
337 //////////////////////////////////////////////////////////////////////////////
338 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
339 // to be first class.
340
341 HpcModuleInfo *hs_hpc_rootModule(void) {
342 return modules;
343 }