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)
35 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
37 #define PRIuWORD64 "lu"
39 #define PRIuWORD64 "llu"
44 StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file.
45 int totalTixes = 0; // total number of tix boxes.
49 static char *tixFilename;
51 static void failure(char *msg) {
52 printf("Hpc failure: %s\n",msg);
53 printf("(perhaps remove .tix file?)\n");
58 static int init_open(char *filename)
60 tixFile = fopen(filename,"r");
64 tix_ch = getc(tixFile);
68 static void expect(char c) {
70 printf("Hpc: parse failed (%c,%c)\n",tix_ch,c);
73 tix_ch = getc(tixFile);
76 static void ws(void) {
77 while (tix_ch == ' ') {
78 tix_ch = getc(tixFile);
82 static char *expectString(void) {
86 while (tix_ch != '"') {
87 tmp[tmp_ix++] = tix_ch;
88 tix_ch = getc(tixFile);
97 static StgWord64 expectWord64(void) {
99 while (isdigit(tix_ch)) {
100 tmp = tmp * 10 + (tix_ch -'0');
101 tix_ch = getc(tixFile);
106 static void hpc_init(void) {
110 if (hpc_inited != 0) {
116 tixFilename = (char *) malloc(strlen(prog_name) + 6);
117 sprintf(tixFilename, "%s.tix", prog_name);
119 if (init_open(tixFilename)) {
131 while(tix_ch != ']') {
132 tmpModule = (Info *)calloc(1,sizeof(Info));
135 tmpModule -> modName = expectString();
139 tmpModule -> tickCount = (int)expectWord64();
144 tmpModule -> tickOffset = totalTixes;
145 totalTixes += tmpModule -> tickCount;
147 tmpModule -> tixArr = 0;
152 nextModule->next=tmpModule;
154 nextModule=tmpModule;
162 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
165 for(i = 0;i < totalTixes;i++) {
170 tixBoxes[i] = expectWord64();
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,int modCount,StgWord64 *tixArr) {
186 Info *tmpModule, *lastModule;
190 printf("hs_hpc_module(%s,%d)\n",modName,modCount);
198 for(;tmpModule != 0;tmpModule = tmpModule->next) {
199 if (!strcmp(tmpModule->modName,modName)) {
200 if (tmpModule->tickCount != modCount) {
201 failure("inconsistent number of tick boxes");
203 assert(tmpModule->tixArr == 0);
204 assert(tixBoxes != 0);
205 tmpModule->tixArr = tixArr;
206 for(i=0;i < modCount;i++) {
207 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
211 lastModule = tmpModule;
213 // Did not find entry so add one on.
214 tmpModule = (Info *)calloc(1,sizeof(Info));
215 tmpModule->modName = modName;
216 tmpModule->tickCount = modCount;
218 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
220 tmpModule->tickOffset = 0;
222 tmpModule->tixArr = tixArr;
223 for(i=0;i < modCount;i++) {
231 lastModule->next=tmpModule;
235 printf("end: hs_hpc_module\n");
239 /* This is called after all the modules have registered their local tixboxes,
240 * and does a sanity check: are we good to go?
247 printf("startupHpc\n");
250 if (hpc_inited == 0) {
257 for(;tmpModule != 0;tmpModule = tmpModule->next) {
258 if (!tmpModule->tixArr) {
259 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
261 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
268 /* Called at the end of execution, to write out the Hpc *.tix file
269 * for this exection. Safe to call, even if coverage is not used.
280 if (hpc_inited == 0) {
284 FILE *f = fopen(tixFilename,"w");
288 fprintf(f,"Tix 0 [");
290 for(;tmpModule != 0;tmpModule = tmpModule->next) {
296 fprintf(f,"(\"%s\",%d)",
298 tmpModule->tickCount);
300 fprintf(stderr,"%s: %d (offset=%d)\n",
302 tmpModule->tickCount,
303 tmpModule->tickOffset);
310 for(;tmpModule != 0;tmpModule = tmpModule->next) {
311 if (!tmpModule->tixArr) {
312 fprintf(stderr,"warning: module %s did not register any hpc tick data\n",
316 for(i = 0;i < tmpModule->tickCount;i++) {
323 if (tmpModule->tixArr) {
324 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);