2 * (c)2006 Galois Connections, Inc.
5 #include "PosixSource.h"
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
28 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
33 static int hpc_inited = 0; // Have you started this component?
34 static pid_t hpc_pid = 0; // pid of this process at hpc-boot time.
35 // Only this pid will read or write .tix file(s).
36 static FILE *tixFile; // file being read/written
37 static int tix_ch; // current char
39 HpcModuleInfo *modules = 0;
40 HpcModuleInfo *nextModule = 0;
41 int totalTixes = 0; // total number of tix boxes.
43 static char *tixFilename;
45 static void GNU_ATTRIBUTE(__noreturn__)
47 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
48 fprintf(stderr,"Hpc failure: %s\n",msg);
50 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
52 fprintf(stderr,"(perhaps remove .tix file?)\n");
57 static int init_open(FILE *file) {
62 tix_ch = getc(tixFile);
66 static void expect(char c) {
68 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
69 failure("parse error when reading .tix file");
71 tix_ch = getc(tixFile);
74 static void ws(void) {
75 while (tix_ch == ' ') {
76 tix_ch = getc(tixFile);
80 static char *expectString(void) {
84 while (tix_ch != '"') {
85 tmp[tmp_ix++] = tix_ch;
86 tix_ch = getc(tixFile);
95 static StgWord64 expectWord64(void) {
97 while (isdigit(tix_ch)) {
98 tmp = tmp * 10 + (tix_ch -'0');
99 tix_ch = getc(tixFile);
107 HpcModuleInfo *tmpModule;
119 while(tix_ch != ']') {
120 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
131 tmpModule -> modName = expectString();
133 tmpModule -> hashNo = (unsigned int)expectWord64();
135 tmpModule -> tickCount = (int)expectWord64();
136 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
137 tmpModule -> tickOffset = totalTixes;
138 totalTixes += tmpModule -> tickCount;
142 for(i = 0;i < tmpModule->tickCount;i++) {
143 tmpModule->tixArr[i] = expectWord64();
156 nextModule->next=tmpModule;
158 nextModule=tmpModule;
169 static void hpc_init(void) {
172 if (hpc_inited != 0) {
177 hpc_tixdir = getenv("HPCTIXDIR");
178 hpc_tixfile = getenv("HPCTIXFILE");
180 /* XXX Check results of mallocs/strdups, and check we are requesting
182 if (hpc_tixfile != NULL) {
183 tixFilename = strdup(hpc_tixfile);
184 } else if (hpc_tixdir != NULL) {
185 /* Make sure the directory is present;
186 * conditional code for mkdir lifted from lndir.c
191 mkdir(hpc_tixdir,0777);
193 /* Then, try open the file
195 tixFilename = (char *) malloc(strlen(hpc_tixdir) + strlen(prog_name) + 12);
196 sprintf(tixFilename,"%s/%s-%d.tix",hpc_tixdir,prog_name,(int)hpc_pid);
198 tixFilename = (char *) malloc(strlen(prog_name) + 6);
199 sprintf(tixFilename, "%s.tix", prog_name);
202 if (init_open(fopen(tixFilename,"r"))) {
207 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
208 * This memory can be uninitized, because we will initialize it with either the contents
209 * of the tix file, or all zeros.
213 hs_hpc_module(char *modName,
217 HpcModuleInfo *tmpModule, *lastModule;
221 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,(nat)modCount);
228 for(;tmpModule != 0;tmpModule = tmpModule->next) {
229 if (!strcmp(tmpModule->modName,modName)) {
230 if (tmpModule->tickCount != modCount) {
231 failure("inconsistent number of tick boxes");
233 assert(tmpModule->tixArr != 0);
234 if (tmpModule->hashNo != modHashNo) {
235 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
236 failure("module mismatch with .tix/.mix file hash number");
237 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
241 for(i=0;i < modCount;i++) {
242 tixArr[i] = tmpModule->tixArr[i];
244 tmpModule->tixArr = tixArr;
245 return tmpModule->tickOffset;
247 lastModule = tmpModule;
249 // Did not find entry so add one on.
250 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
251 tmpModule->modName = modName;
252 tmpModule->tickCount = modCount;
253 tmpModule->hashNo = modHashNo;
255 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
257 tmpModule->tickOffset = 0;
259 tmpModule->tixArr = tixArr;
260 for(i=0;i < modCount;i++) {
268 lastModule->next=tmpModule;
271 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
277 /* This is called after all the modules have registered their local tixboxes,
278 * and does a sanity check: are we good to go?
283 debugTrace(DEBUG_hpc,"startupHpc");
285 if (hpc_inited == 0) {
293 HpcModuleInfo *tmpModule;
294 unsigned int i, inner_comma, outer_comma;
304 for(;tmpModule != 0;tmpModule = tmpModule->next) {
310 fprintf(f," TixModule \"%s\" %u %u [",
312 (nat)tmpModule->hashNo,
313 (nat)tmpModule->tickCount);
314 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
316 (nat)tmpModule->tickCount,
317 (nat)tmpModule->hashNo,
318 (nat)tmpModule->tickOffset);
321 for(i = 0;i < tmpModule->tickCount;i++) {
328 if (tmpModule->tixArr) {
329 fprintf(f,"%" FMT_Word64,tmpModule->tixArr[i]);
341 /* Called at the end of execution, to write out the Hpc *.tix file
342 * for this exection. Safe to call, even if coverage is not used.
346 debugTrace(DEBUG_hpc,"exitHpc");
348 if (hpc_inited == 0) {
352 // Only write the tix file if you are the original process.
353 // Any sub-process from use of fork from inside Haskell will
354 // not clober the .tix file.
356 if (hpc_pid == getpid()) {
357 FILE *f = fopen(tixFilename,"w");
362 //////////////////////////////////////////////////////////////////////////////
363 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
364 // to be first class.
366 HpcModuleInfo *hs_hpc_rootModule(void) {