2 * (c)2006 Galois Connections, Inc.
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 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
41 #define PRIuWORD64 "lu"
43 #define PRIuWORD64 "llu"
46 HpcModuleInfo *modules = 0;
47 HpcModuleInfo *nextModule = 0;
48 int totalTixes = 0; // total number of tix boxes.
50 static char *tixFilename;
52 static void failure(char *msg) {
53 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
54 fprintf(stderr,"Hpc failure: %s\n",msg);
56 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
58 fprintf(stderr,"(perhaps remove .tix file?)\n");
63 static int init_open(FILE *file) {
68 tix_ch = getc(tixFile);
72 static void expect(char c) {
74 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
75 failure("parse error when reading .tix file");
77 tix_ch = getc(tixFile);
80 static void ws(void) {
81 while (tix_ch == ' ') {
82 tix_ch = getc(tixFile);
86 static char *expectString(void) {
90 while (tix_ch != '"') {
91 tmp[tmp_ix++] = tix_ch;
92 tix_ch = getc(tixFile);
101 static StgWord64 expectWord64(void) {
103 while (isdigit(tix_ch)) {
104 tmp = tmp * 10 + (tix_ch -'0');
105 tix_ch = getc(tixFile);
113 HpcModuleInfo *tmpModule;
125 while(tix_ch != ']') {
126 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
137 tmpModule -> modName = expectString();
139 tmpModule -> hashNo = (unsigned int)expectWord64();
141 tmpModule -> tickCount = (int)expectWord64();
142 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
143 tmpModule -> tickOffset = totalTixes;
144 totalTixes += tmpModule -> tickCount;
148 for(i = 0;i < tmpModule->tickCount;i++) {
149 tmpModule->tixArr[i] = expectWord64();
162 nextModule->next=tmpModule;
164 nextModule=tmpModule;
175 static void hpc_init(void) {
177 if (hpc_inited != 0) {
182 hpc_tixdir = getenv("HPCTIXDIR");
184 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,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,"%" PRIuWORD64,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) {