2 * (c)2006 Galois Connections, Inc.
16 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
23 static int hpc_inited = 0; // Have you started this component?
24 static FILE *tixFile; // file being read/written
25 static int tix_ch; // current char
27 typedef struct _Info {
28 char *modName; // name of module
29 int tickCount; // number of ticks
30 int tickOffset; // offset into a single large .tix Array
31 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
37 StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file.
38 int totalTixes = 0; // total number of tix boxes.
42 static char *tixFilename;
44 static void failure(char *msg) {
45 printf("Hpc failure: %s\n",msg);
46 printf("(perhaps remove .tix file?)\n");
51 static int init_open(char *filename)
53 tixFile = fopen(filename,"r");
57 tix_ch = getc(tixFile);
61 static void expect(char c) {
63 printf("Hpc: parse failed (%c,%c)\n",tix_ch,c);
66 tix_ch = getc(tixFile);
69 static void ws(void) {
70 while (tix_ch == ' ') {
71 tix_ch = getc(tixFile);
75 static char *expectString(void) {
79 while (tix_ch != '"') {
80 tmp[tmp_ix++] = tix_ch;
81 tix_ch = getc(tixFile);
90 static StgWord64 expectWord64(void) {
92 while (isdigit(tix_ch)) {
93 tmp = tmp * 10 + (tix_ch -'0');
94 tix_ch = getc(tixFile);
99 static void hpc_init(void) {
103 if (hpc_inited != 0) {
109 tixFilename = (char *) malloc(strlen(prog_name) + 6);
110 sprintf(tixFilename, "%s.tix", prog_name);
112 if (init_open(tixFilename)) {
124 while(tix_ch != ']') {
125 tmpModule = (Info *)calloc(1,sizeof(Info));
128 tmpModule -> modName = expectString();
132 tmpModule -> tickCount = (int)expectWord64();
137 tmpModule -> tickOffset = totalTixes;
138 totalTixes += tmpModule -> tickCount;
140 tmpModule -> tixArr = 0;
145 nextModule->next=tmpModule;
147 nextModule=tmpModule;
155 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
158 for(i = 0;i < totalTixes;i++) {
163 tixBoxes[i] = expectWord64();
172 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
173 * This memory can be uninitized, because we will initialize it with either the contents
174 * of the tix file, or all zeros.
178 hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) {
179 Info *tmpModule, *lastModule;
183 printf("hs_hpc_module(%s,%d)\n",modName,modCount);
191 for(;tmpModule != 0;tmpModule = tmpModule->next) {
192 if (!strcmp(tmpModule->modName,modName)) {
193 if (tmpModule->tickCount != modCount) {
194 failure("inconsistent number of tick boxes");
196 assert(tmpModule->tixArr == 0);
197 assert(tixBoxes != 0);
198 tmpModule->tixArr = tixArr;
199 for(i=0;i < modCount;i++) {
200 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
204 lastModule = tmpModule;
206 // Did not find entry so add one on.
207 tmpModule = (Info *)calloc(1,sizeof(Info));
208 tmpModule->modName = modName;
209 tmpModule->tickCount = modCount;
211 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
213 tmpModule->tickOffset = 0;
215 tmpModule->tixArr = tixArr;
216 for(i=0;i < modCount;i++) {
224 lastModule->next=tmpModule;
228 printf("end: hs_hpc_module\n");
232 /* This is called after all the modules have registered their local tixboxes,
233 * and does a sanity check: are we good to go?
240 printf("startupHpc\n");
243 if (hpc_inited == 0) {
250 for(;tmpModule != 0;tmpModule = tmpModule->next) {
251 if (!tmpModule->tixArr) {
252 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
254 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
261 /* Called at the end of execution, to write out the Hpc *.tix file
262 * for this exection. Safe to call, even if coverage is not used.
273 if (hpc_inited == 0) {
277 FILE *f = fopen(tixFilename,"w");
281 fprintf(f,"Tix 0 [");
283 for(;tmpModule != 0;tmpModule = tmpModule->next) {
289 fprintf(f,"(\"%s\",%d)",
291 tmpModule->tickCount);
293 fprintf(stderr,"%s: %d (offset=%d)\n",
295 tmpModule->tickCount,
296 tmpModule->tickOffset);
303 for(;tmpModule != 0;tmpModule = tmpModule->next) {
304 if (!tmpModule->tixArr) {
305 fprintf(stderr,"warning: module %s did not register any hpc tick data\n",
309 for(i = 0;i < tmpModule->tickCount;i++) {
316 if (tmpModule->tixArr) {
317 fprintf(f,"%lld",tmpModule->tixArr[i]);