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 // 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 GNU_ATTRIBUTE(__noreturn__)
54 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
55 fprintf(stderr,"Hpc failure: %s\n",msg);
57 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
59 fprintf(stderr,"(perhaps remove .tix file?)\n");
64 static int init_open(FILE *file) {
69 tix_ch = getc(tixFile);
73 static void expect(char c) {
75 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
76 failure("parse error when reading .tix file");
78 tix_ch = getc(tixFile);
81 static void ws(void) {
82 while (tix_ch == ' ') {
83 tix_ch = getc(tixFile);
87 static char *expectString(void) {
91 while (tix_ch != '"') {
92 tmp[tmp_ix++] = tix_ch;
93 tix_ch = getc(tixFile);
102 static StgWord64 expectWord64(void) {
104 while (isdigit(tix_ch)) {
105 tmp = tmp * 10 + (tix_ch -'0');
106 tix_ch = getc(tixFile);
114 HpcModuleInfo *tmpModule;
126 while(tix_ch != ']') {
127 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
138 tmpModule -> modName = expectString();
140 tmpModule -> hashNo = (unsigned int)expectWord64();
142 tmpModule -> tickCount = (int)expectWord64();
143 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
144 tmpModule -> tickOffset = totalTixes;
145 totalTixes += tmpModule -> tickCount;
149 for(i = 0;i < tmpModule->tickCount;i++) {
150 tmpModule->tixArr[i] = expectWord64();
163 nextModule->next=tmpModule;
165 nextModule=tmpModule;
176 static void hpc_init(void) {
179 if (hpc_inited != 0) {
184 hpc_tixdir = getenv("HPCTIXDIR");
185 hpc_tixfile = getenv("HPCTIXFILE");
187 /* XXX Check results of mallocs/strdups, and check we are requesting
189 if (hpc_tixfile != NULL) {
190 tixFilename = strdup(hpc_tixfile);
191 } else if (hpc_tixdir != NULL) {
192 /* Make sure the directory is present;
193 * conditional code for mkdir lifted from lndir.c
198 mkdir(hpc_tixdir,0777);
200 /* Then, try open the file
202 tixFilename = (char *) malloc(strlen(hpc_tixdir) + strlen(prog_name) + 12);
203 sprintf(tixFilename,"%s/%s-%d.tix",hpc_tixdir,prog_name,(int)hpc_pid);
205 tixFilename = (char *) malloc(strlen(prog_name) + 6);
206 sprintf(tixFilename, "%s.tix", prog_name);
209 if (init_open(fopen(tixFilename,"r"))) {
214 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
215 * This memory can be uninitized, because we will initialize it with either the contents
216 * of the tix file, or all zeros.
220 hs_hpc_module(char *modName,
224 HpcModuleInfo *tmpModule, *lastModule;
228 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,(nat)modCount);
235 for(;tmpModule != 0;tmpModule = tmpModule->next) {
236 if (!strcmp(tmpModule->modName,modName)) {
237 if (tmpModule->tickCount != modCount) {
238 failure("inconsistent number of tick boxes");
240 assert(tmpModule->tixArr != 0);
241 if (tmpModule->hashNo != modHashNo) {
242 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
243 failure("module mismatch with .tix/.mix file hash number");
244 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
248 for(i=0;i < modCount;i++) {
249 tixArr[i] = tmpModule->tixArr[i];
251 tmpModule->tixArr = tixArr;
252 return tmpModule->tickOffset;
254 lastModule = tmpModule;
256 // Did not find entry so add one on.
257 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
258 tmpModule->modName = modName;
259 tmpModule->tickCount = modCount;
260 tmpModule->hashNo = modHashNo;
262 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
264 tmpModule->tickOffset = 0;
266 tmpModule->tixArr = tixArr;
267 for(i=0;i < modCount;i++) {
275 lastModule->next=tmpModule;
278 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
284 /* This is called after all the modules have registered their local tixboxes,
285 * and does a sanity check: are we good to go?
290 debugTrace(DEBUG_hpc,"startupHpc");
292 if (hpc_inited == 0) {
300 HpcModuleInfo *tmpModule;
301 unsigned int i, inner_comma, outer_comma;
311 for(;tmpModule != 0;tmpModule = tmpModule->next) {
317 fprintf(f," TixModule \"%s\" %u %u [",
319 (nat)tmpModule->hashNo,
320 (nat)tmpModule->tickCount);
321 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
323 (nat)tmpModule->tickCount,
324 (nat)tmpModule->hashNo,
325 (nat)tmpModule->tickOffset);
328 for(i = 0;i < tmpModule->tickCount;i++) {
335 if (tmpModule->tixArr) {
336 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
348 /* Called at the end of execution, to write out the Hpc *.tix file
349 * for this exection. Safe to call, even if coverage is not used.
353 debugTrace(DEBUG_hpc,"exitHpc");
355 if (hpc_inited == 0) {
359 // Only write the tix file if you are the original process.
360 // Any sub-process from use of fork from inside Haskell will
361 // not clober the .tix file.
363 if (hpc_pid == getpid()) {
364 FILE *f = fopen(tixFilename,"w");
369 //////////////////////////////////////////////////////////////////////////////
370 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
371 // to be first class.
373 HpcModuleInfo *hs_hpc_rootModule(void) {