2 * (c)2006 Galois Connections, Inc.
15 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
22 static int hpc_inited = 0; // Have you started this component?
23 static FILE *tixFile; // file being read/written
24 static int tix_ch; // current char
25 static StgWord64 magicTixNumber; // Magic/Hash number to mark .tix files
27 static int hpc_ticks_inited = 0; // Have you started the dynamic external ticking?
28 static FILE *rixFile; // The tracer file/pipe
30 typedef struct _Info {
31 char *modName; // name of module
32 int tickCount; // number of ticks
33 int tickOffset; // offset into a single large .tix Array
34 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
38 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
40 #define PRIuWORD64 "lu"
42 #define PRIuWORD64 "llu"
47 StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file.
48 int totalTixes = 0; // total number of tix boxes.
52 static char *tixFilename;
54 static void failure(char *msg) {
55 printf("Hpc failure: %s\n",msg);
56 printf("(perhaps remove .tix file?)\n");
61 static int init_open(char *filename)
63 tixFile = fopen(filename,"r");
67 tix_ch = getc(tixFile);
71 static void expect(char c) {
73 printf("Hpc: parse failed (%c,%c)\n",tix_ch,c);
76 tix_ch = getc(tixFile);
79 static void ws(void) {
80 while (tix_ch == ' ') {
81 tix_ch = getc(tixFile);
85 static char *expectString(void) {
89 while (tix_ch != '"') {
90 tmp[tmp_ix++] = tix_ch;
91 tix_ch = getc(tixFile);
100 static StgWord64 expectWord64(void) {
102 while (isdigit(tix_ch)) {
103 tmp = tmp * 10 + (tix_ch -'0');
104 tix_ch = getc(tixFile);
109 static void hpc_init(void) {
113 if (hpc_inited != 0) {
119 tixFilename = (char *) malloc(strlen(prog_name) + 6);
120 sprintf(tixFilename, "%s.tix", prog_name);
122 if (init_open(tixFilename)) {
130 magicTixNumber = expectWord64();
134 while(tix_ch != ']') {
135 tmpModule = (Info *)calloc(1,sizeof(Info));
138 tmpModule -> modName = expectString();
142 tmpModule -> tickCount = (int)expectWord64();
147 tmpModule -> tickOffset = totalTixes;
148 totalTixes += tmpModule -> tickCount;
150 tmpModule -> tixArr = 0;
155 nextModule->next=tmpModule;
157 nextModule=tmpModule;
166 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
169 for(i = 0;i < totalTixes;i++) {
174 tixBoxes[i] = expectWord64();
181 // later, we will find a binary specific
182 magicTixNumber = (StgWord64)0;
186 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
187 * This memory can be uninitized, because we will initialize it with either the contents
188 * of the tix file, or all zeros.
192 hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) {
193 Info *tmpModule, *lastModule;
198 printf("hs_hpc_module(%s,%d)\n",modName,modCount);
206 for(;tmpModule != 0;tmpModule = tmpModule->next) {
207 if (!strcmp(tmpModule->modName,modName)) {
208 if (tmpModule->tickCount != modCount) {
209 failure("inconsistent number of tick boxes");
211 assert(tmpModule->tixArr == 0);
212 assert(tixBoxes != 0);
213 tmpModule->tixArr = tixArr;
214 for(i=0;i < modCount;i++) {
215 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
217 return tmpModule->tickOffset;
219 lastModule = tmpModule;
221 // Did not find entry so add one on.
222 tmpModule = (Info *)calloc(1,sizeof(Info));
223 tmpModule->modName = modName;
224 tmpModule->tickCount = modCount;
226 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
228 tmpModule->tickOffset = 0;
230 tmpModule->tixArr = tixArr;
231 for(i=0;i < modCount;i++) {
239 lastModule->next=tmpModule;
243 printf("end: hs_hpc_module\n");
250 * Called on *every* exception thrown
254 // Assumes that we have had at least *one* tick first.
255 // All exceptions before the first tick are not reported.
256 // The only time this might be an issue is in bootstrapping code,
257 // so this is a feature.
258 if (hpc_inited != 0 && hpc_ticks_inited != 0) {
259 fprintf(rixFile,"Throw\n");
263 /* Called on every tick
267 hs_hpc_tick(int globIx) {
268 int threadId = 0; // for now, assume single thread
269 // TODO: work out how to get the thread Id to here.
272 #if DEBUG_HPC && DEBUG
273 printf("hs_hpc_tick(%d)\n",globIx);
275 if (!hpc_ticks_inited) {
276 char* trace_filename;
281 hpc_ticks_inited = 1;
283 trace_filename = (char *) malloc(strlen(prog_name) + 6);
284 sprintf(trace_filename, "%s.rix", prog_name);
285 rixFile = fopen(trace_filename,"w+");
289 fprintf(rixFile,"START %s\n",prog_name);
290 fprintf(rixFile,"[");
292 for(;tmpModule != 0;tmpModule = tmpModule->next) {
294 fprintf(rixFile,",");
298 fprintf(rixFile,"(\"%s\",%u)",
300 tmpModule->tickCount);
302 fprintf(stderr,"(tracer)%s: %u (offset=%u)\n",
304 tmpModule->tickCount,
305 tmpModule->tickOffset);
308 fprintf(rixFile,"]\n");
311 assert(rixFile != 0);
313 fprintf(rixFile,"%d\n",globIx);
316 printf("end: hs_hpc_tick\n");
321 /* This is called after all the modules have registered their local tixboxes,
322 * and does a sanity check: are we good to go?
329 printf("startupHpc\n");
332 if (hpc_inited == 0) {
339 for(;tmpModule != 0;tmpModule = tmpModule->next) {
340 if (!tmpModule->tixArr) {
341 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
343 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
351 /* Called at the end of execution, to write out the Hpc *.tix file
352 * for this exection. Safe to call, even if coverage is not used.
363 if (hpc_inited == 0) {
367 FILE *f = fopen(tixFilename,"w");
371 fprintf(f,"Tix %" PRIuWORD64 " [", magicTixNumber);
373 for(;tmpModule != 0;tmpModule = tmpModule->next) {
379 fprintf(f,"(\"%s\",%u)",
381 tmpModule->tickCount);
383 fprintf(stderr,"%s: %u (offset=%u)\n",
385 tmpModule->tickCount,
386 tmpModule->tickOffset);
393 for(;tmpModule != 0;tmpModule = tmpModule->next) {
394 if (!tmpModule->tixArr) {
395 fprintf(stderr,"warning: module %s did not register any hpc tick data\n",
399 for(i = 0;i < tmpModule->tickCount;i++) {
406 if (tmpModule->tixArr) {
407 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
418 if (hpc_ticks_inited && rixFile != 0) {