2 * (c)2006 Galois Connections, Inc.
20 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
25 static int hpc_inited = 0; // Have you started this component?
26 static FILE *tixFile; // file being read/written
27 static int tix_ch; // current char
29 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
31 #define PRIuWORD64 "lu"
33 #define PRIuWORD64 "llu"
36 HpcModuleInfo *modules = 0;
37 HpcModuleInfo *nextModule = 0;
38 int totalTixes = 0; // total number of tix boxes.
40 static char *tixFilename;
42 static void failure(char *msg) {
43 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
44 fprintf(stderr,"Hpc failure: %s\n",msg);
46 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
48 fprintf(stderr,"(perhaps remove .tix file?)\n");
53 static int init_open(FILE *file) {
58 tix_ch = getc(tixFile);
62 static void expect(char c) {
64 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
65 failure("parse error when reading .tix file");
67 tix_ch = getc(tixFile);
70 static void ws(void) {
71 while (tix_ch == ' ') {
72 tix_ch = getc(tixFile);
76 static char *expectString(void) {
80 while (tix_ch != '"') {
81 tmp[tmp_ix++] = tix_ch;
82 tix_ch = getc(tixFile);
91 static StgWord64 expectWord64(void) {
93 while (isdigit(tix_ch)) {
94 tmp = tmp * 10 + (tix_ch -'0');
95 tix_ch = getc(tixFile);
103 HpcModuleInfo *tmpModule;
115 while(tix_ch != ']') {
116 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
127 tmpModule -> modName = expectString();
129 tmpModule -> hashNo = (unsigned int)expectWord64();
131 tmpModule -> tickCount = (int)expectWord64();
132 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
133 tmpModule -> tickOffset = totalTixes;
134 totalTixes += tmpModule -> tickCount;
138 for(i = 0;i < tmpModule->tickCount;i++) {
139 tmpModule->tixArr[i] = expectWord64();
152 nextModule->next=tmpModule;
154 nextModule=tmpModule;
165 static void hpc_init(void) {
166 if (hpc_inited != 0) {
171 tixFilename = (char *) malloc(strlen(prog_name) + 6);
172 sprintf(tixFilename, "%s.tix", prog_name);
174 if (init_open(fopen(tixFilename,"r"))) {
179 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
180 * This memory can be uninitized, because we will initialize it with either the contents
181 * of the tix file, or all zeros.
185 hs_hpc_module(char *modName,
189 HpcModuleInfo *tmpModule, *lastModule;
193 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
200 for(;tmpModule != 0;tmpModule = tmpModule->next) {
201 if (!strcmp(tmpModule->modName,modName)) {
202 if (tmpModule->tickCount != modCount) {
203 failure("inconsistent number of tick boxes");
205 assert(tmpModule->tixArr != 0);
206 if (tmpModule->hashNo != modHashNo) {
207 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
208 failure("module mismatch with .tix/.mix file hash number");
209 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
213 for(i=0;i < modCount;i++) {
214 tixArr[i] = tmpModule->tixArr[i];
216 tmpModule->tixArr = tixArr;
217 return tmpModule->tickOffset;
219 lastModule = tmpModule;
221 // Did not find entry so add one on.
222 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
223 tmpModule->modName = modName;
224 tmpModule->tickCount = modCount;
225 tmpModule->hashNo = modHashNo;
227 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
229 tmpModule->tickOffset = 0;
231 tmpModule->tixArr = tixArr;
232 for(i=0;i < modCount;i++) {
240 lastModule->next=tmpModule;
243 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
249 /* This is called after all the modules have registered their local tixboxes,
250 * and does a sanity check: are we good to go?
255 debugTrace(DEBUG_hpc,"startupHpc");
257 if (hpc_inited == 0) {
265 HpcModuleInfo *tmpModule;
266 int i, inner_comma, outer_comma;
276 for(;tmpModule != 0;tmpModule = tmpModule->next) {
282 fprintf(f," TixModule \"%s\" %u %u [",
285 tmpModule->tickCount);
286 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
288 tmpModule->tickCount,
290 tmpModule->tickOffset);
293 for(i = 0;i < tmpModule->tickCount;i++) {
300 if (tmpModule->tixArr) {
301 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
313 /* Called at the end of execution, to write out the Hpc *.tix file
314 * for this exection. Safe to call, even if coverage is not used.
318 debugTrace(DEBUG_hpc,"exitHpc");
320 if (hpc_inited == 0) {
324 FILE *f = fopen(tixFilename,"w");
328 //////////////////////////////////////////////////////////////////////////////
329 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
330 // to be first class.
332 HpcModuleInfo *hs_hpc_rootModule(void) {