2 * (c)2006 Galois Connections, Inc.
20 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
27 static int hpc_inited = 0; // Have you started this component?
28 static FILE *tixFile; // file being read/written
29 static int tix_ch; // current char
31 static FILE *rixFile = NULL; // The tracer file/pipe (to debugger)
32 static FILE *rixCmdFile = NULL; // The tracer file/pipe (from debugger)
33 static StgWord64 rixCounter = 0; // The global event counter
34 static int debuggee_pid;
37 RixThreadFinishedOp = -1,
43 typedef struct _Info {
44 char *modName; // name of module
45 int tickCount; // number of ticks
46 int tickOffset; // offset into a single large .tix Array
47 int hashNo; // Hash number for this module's mix info
48 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
52 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
54 #define PRIuWORD64 "lu"
56 #define PRIuWORD64 "llu"
61 int totalTixes = 0; // total number of tix boxes.
63 static char *tixFilename;
65 static void failure(char *msg) {
66 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
67 fprintf(stderr,"Hpc failure: %s\n",msg);
69 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
71 fprintf(stderr,"(perhaps remove .tix file?)\n");
76 static int init_open(FILE *file) {
81 tix_ch = getc(tixFile);
85 static void expect(char c) {
87 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
88 failure("parse error when reading .tix file");
90 tix_ch = getc(tixFile);
93 static void ws(void) {
94 while (tix_ch == ' ') {
95 tix_ch = getc(tixFile);
99 static char *expectString(void) {
103 while (tix_ch != '"') {
104 tmp[tmp_ix++] = tix_ch;
105 tix_ch = getc(tixFile);
109 res = malloc(tmp_ix);
114 static StgWord64 expectWord64(void) {
116 while (isdigit(tix_ch)) {
117 tmp = tmp * 10 + (tix_ch -'0');
118 tix_ch = getc(tixFile);
138 while(tix_ch != ']') {
139 tmpModule = (Info *)calloc(1,sizeof(Info));
150 tmpModule -> modName = expectString();
152 tmpModule -> hashNo = (unsigned int)expectWord64();
154 tmpModule -> tickCount = (int)expectWord64();
155 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
156 tmpModule -> tickOffset = totalTixes;
157 totalTixes += tmpModule -> tickCount;
161 for(i = 0;i < tmpModule->tickCount;i++) {
162 tmpModule->tixArr[i] = expectWord64();
175 nextModule->next=tmpModule;
177 nextModule=tmpModule;
188 static void hpc_init(void) {
189 if (hpc_inited != 0) {
194 tixFilename = (char *) malloc(strlen(prog_name) + 6);
195 sprintf(tixFilename, "%s.tix", prog_name);
197 if (init_open(fopen(tixFilename,"r"))) {
202 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
203 * This memory can be uninitized, because we will initialize it with either the contents
204 * of the tix file, or all zeros.
208 hs_hpc_module(char *modName,
212 Info *tmpModule, *lastModule;
216 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
223 for(;tmpModule != 0;tmpModule = tmpModule->next) {
224 if (!strcmp(tmpModule->modName,modName)) {
225 if (tmpModule->tickCount != modCount) {
226 failure("inconsistent number of tick boxes");
228 assert(tmpModule->tixArr != 0);
229 if (tmpModule->hashNo != modHashNo) {
230 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
231 failure("module mismatch with .tix/.mix file hash number");
232 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
236 for(i=0;i < modCount;i++) {
237 tixArr[i] = tmpModule->tixArr[i];
239 tmpModule->tixArr = tixArr;
240 return tmpModule->tickOffset;
242 lastModule = tmpModule;
244 // Did not find entry so add one on.
245 tmpModule = (Info *)calloc(1,sizeof(Info));
246 tmpModule->modName = modName;
247 tmpModule->tickCount = modCount;
248 tmpModule->hashNo = modHashNo;
250 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
252 tmpModule->tickOffset = 0;
254 tmpModule->tixArr = tixArr;
255 for(i=0;i < modCount;i++) {
263 lastModule->next=tmpModule;
266 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
271 static void breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid);
274 static StgThreadID previousTid = 0;
275 static StgWord64 rixBPCounter = 0; // The global event breakpoint counter
276 static int *tixBoxBP;
277 static HpcRixOp rixOpBack[WOP_SIZE]; // The actual op
278 static HpcRixOp rixTidBack[WOP_SIZE]; // Tid's before the op
281 hs_hpc_raise_event(StgTSO *current_tso) {
282 hs_hpc_tick(RixRaiseOp,current_tso);
286 hs_hpc_thread_finished_event(StgTSO *current_tso) {
287 hs_hpc_tick(RixThreadFinishedOp,current_tso);
290 /* Called on every tick, dynamically, sending to our
291 * external record of program execution.
295 hs_hpc_tick(int rixOp, StgTSO *current_tso) {
297 debugTrace(DEBUG_hpc,"hs_hpc_tick(%x)",rixOp);
299 if (rixFile == NULL) {
302 assert(rixCmdFile != NULL);
303 StgThreadID tid = (current_tso == 0) ? 0 : current_tso->id;
305 // now check to see if we have met a breakpoint condition
306 if (rixCounter == rixBPCounter
307 || tid != previousTid) {
308 breakPointCommand(rixOp,tid);
312 if (tixBoxBP[rixOp] == 1) { // reached a bp tixbox
313 breakPointCommand(rixOp,tid);
316 // record the special operation
317 breakPointCommand(rixOp,tid);
320 // update the history information.
322 rixOpBack[rixCounter % WOP_SIZE] = rixOp;
323 rixTidBack[rixCounter % WOP_SIZE] = tid;
326 debugTrace(DEBUG_hpc, "end: hs_hpc_tick");
330 printEvent(FILE *out,StgWord64 rixCounter,StgThreadID rixTid,HpcRixOp rixOp) {
335 "Event %" PRIuWORD64 " %u ",
337 (unsigned int)rixTid);
340 case RixThreadFinishedOp:
341 sprintf(suffixMsg,"ThreadFinished");
344 sprintf(suffixMsg,"Raise");
347 sprintf(suffixMsg,"Finished");
350 sprintf(suffixMsg,"%u",rixOp);
353 fprintf(out,"%s%s\n",prefixMsg,suffixMsg);
354 debugTrace(DEBUG_hpc,"sending %s%s",prefixMsg,suffixMsg);
358 breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid) {
360 unsigned int tmp = 0;
362 if (getpid() != debuggee_pid) {
363 // We are not the original process, to do not issue
364 // any events, and do not try to talk to the debugger.
368 debugTrace(DEBUG_hpc,"breakPointCommand %d %x",rixOp,(unsigned int)rixTid);
370 printEvent(rixFile,rixCounter,rixTid,rixOp);
373 /* From here, you can ask some basic questions.
375 * c<nat> set the (one) counter breakpoint
376 * s<nat> set the (many) tickbox breakpoint
377 * u<nat> unset the (many) tickbox breakpoint
380 * Note that you aways end up here on the first tick
381 * because the rixBPCounter starts equal to 0.
383 int c = getc(rixCmdFile);
384 while(c != 10 && c != -1) {
386 case 'c': // c1234 -- set counter breakpoint at 1234
387 c = getc(rixCmdFile);
390 tmp64 = tmp64 * 10 + (c - '0');
391 c = getc(rixCmdFile);
393 debugTrace(DEBUG_hpc,"setting countBP = %" PRIuWORD64,tmp64);
395 rixBPCounter = tmp64;
397 case 's': // s2323 -- set tick box breakpoint at 2323
398 c = getc(rixCmdFile);
401 tmp = tmp * 10 + (c - '0');
402 c = getc(rixCmdFile);
405 debugTrace(DEBUG_hpc,"seting bp for tix %d",tmp);
409 case 'u': // u2323 -- unset tick box breakpoint at 2323
410 c = getc(rixCmdFile);
413 tmp = tmp * 10 + (c - '0');
414 c = getc(rixCmdFile);
417 debugTrace(DEBUG_hpc,"unseting bp for tix %d",tmp);
421 case 'h': // h -- history of the last few (WOP_SIZE) steps
422 if (rixCounter > WOP_SIZE) {
423 tmp64 = rixCounter - WOP_SIZE;
427 for(;tmp64 < rixCounter;tmp64++) {
430 rixTidBack[tmp64 % WOP_SIZE],
431 rixOpBack[tmp64 % WOP_SIZE]);
434 c = getc(rixCmdFile);
438 debugTrace(DEBUG_hpc,"strange command from HPCRIX (%d)",c);
440 c = getc(rixCmdFile);
442 while (c != 10) { // the end of the line
443 c = getc(rixCmdFile); // to the end of the line
445 c = getc(rixCmdFile); // the first char on the next command
448 debugTrace(DEBUG_hpc,"leaving breakPointCommand");
452 /* This is called after all the modules have registered their local tixboxes,
453 * and does a sanity check: are we good to go?
460 debugTrace(DEBUG_hpc,"startupHpc");
462 if (hpc_inited == 0) {
465 // HPCRIX contains the name of the file to send our dynamic runtime output to (a named pipe).
467 hpcRix = getenv("HPCRIX");
476 if (sscanf(hpcRix,"%d:%d",&rixFD,&rixCmdFD) != 2) {
477 /* Bad format for HPCRIX.
479 debugTrace(DEBUG_hpc,"Bad HPCRIX (%s)",hpcRix);
483 debugTrace(DEBUG_hpc,"found HPCRIX pipes: %d:%d",rixFD,rixCmdFD);
485 rixFile = fdopen(rixFD,"w");
486 assert(rixFile != NULL);
488 rixCmdFile = fdopen(rixCmdFD,"r");
489 assert(rixCmdFile != NULL);
491 // If we fork a process, then we do not want ticks inside
492 // the sub-process to talk to the debugger. So we remember
493 // our pid at startup time, so we can check if we are still
494 // the original process.
496 debuggee_pid = getpid();
500 fprintf(rixFile,"Starting %s\n",prog_name);
501 fprintf(rixFile,"[");
503 for(;tmpModule != 0;tmpModule = tmpModule->next) {
505 fprintf(rixFile,",");
509 fprintf(rixFile,"(\"%s\",%u)",
511 tmpModule->tickCount);
513 tixCount += tmpModule->tickCount;
515 debugTrace(DEBUG_hpc,"(tracer)%s: %u (offset=%u) (hash=%u)\n",
517 tmpModule->tickCount,
519 tmpModule->tickOffset);
522 fprintf(rixFile,"]\n");
525 // Allocate the tixBox breakpoint array
526 // These are set to 1 if you want to
527 // stop at a specific breakpoint
528 tixBoxBP = (int *)calloc(tixCount,sizeof(int));
537 int i, inner_comma, outer_comma;
547 for(;tmpModule != 0;tmpModule = tmpModule->next) {
553 fprintf(f," TixModule \"%s\" %u %u [",
556 tmpModule->tickCount);
557 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
559 tmpModule->tickCount,
561 tmpModule->tickOffset);
564 for(i = 0;i < tmpModule->tickCount;i++) {
571 if (tmpModule->tixArr) {
572 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
584 /* Called at the end of execution, to write out the Hpc *.tix file
585 * for this exection. Safe to call, even if coverage is not used.
589 debugTrace(DEBUG_hpc,"exitHpc");
591 if (hpc_inited == 0) {
595 FILE *f = fopen(tixFilename,"w");
598 if (rixFile != NULL) {
599 hs_hpc_tick(RixFinishedOp,(StgThreadID)0);
602 if (rixCmdFile != NULL) {
608 void hs_hpc_read(char *filename) {
609 Info *orig_modules = 0, *tmpModule, *tmpOrigModule;
612 orig_modules = modules;
614 if (init_open(fopen(filename,"r"))) {
616 // Now we copy across the arrays. O(n^2), but works
617 for(tmpModule = modules;
619 tmpModule = tmpModule->next) {
621 for(tmpOrigModule = orig_modules;
623 tmpOrigModule = tmpOrigModule->next) {
624 if (!strcmp(tmpModule->modName,tmpOrigModule->modName)) {
625 assert(tmpModule->tixArr != 0);
626 assert(tmpOrigModule->tixArr != 0);
627 assert(tmpModule->tickCount == tmpOrigModule->tickCount);
628 for(i=0;i < tmpModule->tickCount;i++) {
629 tmpOrigModule->tixArr[i] = tmpModule->tixArr[i];
631 tmpModule->tixArr = tmpOrigModule->tixArr;
639 void hs_hpc_write(char *filename) {
640 writeTix(fopen(filename,"w"));