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(char *filename)
78 tixFile = fopen(filename,"r");
82 tix_ch = getc(tixFile);
86 static void expect(char c) {
88 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
89 failure("parse error when reading .tix file");
91 tix_ch = getc(tixFile);
94 static void ws(void) {
95 while (tix_ch == ' ') {
96 tix_ch = getc(tixFile);
100 static char *expectString(void) {
104 while (tix_ch != '"') {
105 tmp[tmp_ix++] = tix_ch;
106 tix_ch = getc(tixFile);
110 res = malloc(tmp_ix);
115 static StgWord64 expectWord64(void) {
117 while (isdigit(tix_ch)) {
118 tmp = tmp * 10 + (tix_ch -'0');
119 tix_ch = getc(tixFile);
124 static void hpc_init(void) {
128 if (hpc_inited != 0) {
133 tixFilename = (char *) malloc(strlen(prog_name) + 6);
134 sprintf(tixFilename, "%s.tix", prog_name);
136 if (init_open(tixFilename)) {
146 while(tix_ch != ']') {
147 tmpModule = (Info *)calloc(1,sizeof(Info));
158 tmpModule -> modName = expectString();
160 tmpModule -> hashNo = (unsigned int)expectWord64();
162 tmpModule -> tickCount = (int)expectWord64();
163 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
164 tmpModule -> tickOffset = totalTixes;
165 totalTixes += tmpModule -> tickCount;
169 for(i = 0;i < tmpModule->tickCount;i++) {
170 tmpModule->tixArr[i] = expectWord64();
183 nextModule->next=tmpModule;
185 nextModule=tmpModule;
197 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
198 * This memory can be uninitized, because we will initialize it with either the contents
199 * of the tix file, or all zeros.
203 hs_hpc_module(char *modName,
207 Info *tmpModule, *lastModule;
211 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
218 for(;tmpModule != 0;tmpModule = tmpModule->next) {
219 if (!strcmp(tmpModule->modName,modName)) {
220 if (tmpModule->tickCount != modCount) {
221 failure("inconsistent number of tick boxes");
223 assert(tmpModule->tixArr != 0);
224 if (tmpModule->hashNo != modHashNo) {
225 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
226 failure("module mismatch with .tix/.mix file hash number");
227 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
231 for(i=0;i < modCount;i++) {
232 tixArr[i] = tmpModule->tixArr[i];
234 tmpModule->tixArr = tixArr;
235 return tmpModule->tickOffset;
237 lastModule = tmpModule;
239 // Did not find entry so add one on.
240 tmpModule = (Info *)calloc(1,sizeof(Info));
241 tmpModule->modName = modName;
242 tmpModule->tickCount = modCount;
243 tmpModule->hashNo = modHashNo;
245 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
247 tmpModule->tickOffset = 0;
249 tmpModule->tixArr = tixArr;
250 for(i=0;i < modCount;i++) {
258 lastModule->next=tmpModule;
261 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
266 static void breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid);
269 static StgThreadID previousTid = 0;
270 static StgWord64 rixBPCounter = 0; // The global event breakpoint counter
271 static int *tixBoxBP;
272 static HpcRixOp rixOpBack[WOP_SIZE]; // The actual op
273 static HpcRixOp rixTidBack[WOP_SIZE]; // Tid's before the op
276 hs_hpc_raise_event(StgTSO *current_tso) {
277 hs_hpc_tick(RixRaiseOp,current_tso);
281 hs_hpc_thread_finished_event(StgTSO *current_tso) {
282 hs_hpc_tick(RixThreadFinishedOp,current_tso);
285 /* Called on every tick, dynamically, sending to our
286 * external record of program execution.
290 hs_hpc_tick(int rixOp, StgTSO *current_tso) {
292 debugTrace(DEBUG_hpc,"hs_hpc_tick(%x)",rixOp);
294 if (rixFile == NULL) {
297 assert(rixCmdFile != NULL);
298 StgThreadID tid = (current_tso == 0) ? 0 : current_tso->id;
300 // now check to see if we have met a breakpoint condition
301 if (rixCounter == rixBPCounter
302 || tid != previousTid) {
303 breakPointCommand(rixOp,tid);
307 if (tixBoxBP[rixOp] == 1) { // reached a bp tixbox
308 breakPointCommand(rixOp,tid);
311 // record the special operation
312 breakPointCommand(rixOp,tid);
315 // update the history information.
317 rixOpBack[rixCounter % WOP_SIZE] = rixOp;
318 rixTidBack[rixCounter % WOP_SIZE] = tid;
321 debugTrace(DEBUG_hpc, "end: hs_hpc_tick");
325 printEvent(FILE *out,StgWord64 rixCounter,StgThreadID rixTid,HpcRixOp rixOp) {
330 "Event %" PRIuWORD64 " %u ",
332 (unsigned int)rixTid);
335 case RixThreadFinishedOp:
336 sprintf(suffixMsg,"ThreadFinished");
339 sprintf(suffixMsg,"Raise");
342 sprintf(suffixMsg,"Finished");
345 sprintf(suffixMsg,"%u",rixOp);
348 fprintf(out,"%s%s\n",prefixMsg,suffixMsg);
349 debugTrace(DEBUG_hpc,"sending %s%s",prefixMsg,suffixMsg);
353 breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid) {
355 unsigned int tmp = 0;
357 if (getpid() != debuggee_pid) {
358 // We are not the original process, to do not issue
359 // any events, and do not try to talk to the debugger.
363 debugTrace(DEBUG_hpc,"breakPointCommand %d %x",rixOp,(unsigned int)rixTid);
365 printEvent(rixFile,rixCounter,rixTid,rixOp);
368 /* From here, you can ask some basic questions.
370 * c<nat> set the (one) counter breakpoint
371 * s<nat> set the (many) tickbox breakpoint
372 * u<nat> unset the (many) tickbox breakpoint
375 * Note that you aways end up here on the first tick
376 * because the rixBPCounter starts equal to 0.
378 int c = getc(rixCmdFile);
379 while(c != 10 && c != -1) {
381 case 'c': // c1234 -- set counter breakpoint at 1234
382 c = getc(rixCmdFile);
385 tmp64 = tmp64 * 10 + (c - '0');
386 c = getc(rixCmdFile);
388 debugTrace(DEBUG_hpc,"setting countBP = %" PRIuWORD64,tmp64);
390 rixBPCounter = tmp64;
392 case 's': // s2323 -- set tick box breakpoint at 2323
393 c = getc(rixCmdFile);
396 tmp = tmp * 10 + (c - '0');
397 c = getc(rixCmdFile);
400 debugTrace(DEBUG_hpc,"seting bp for tix %d",tmp);
404 case 'u': // u2323 -- unset tick box breakpoint at 2323
405 c = getc(rixCmdFile);
408 tmp = tmp * 10 + (c - '0');
409 c = getc(rixCmdFile);
412 debugTrace(DEBUG_hpc,"unseting bp for tix %d",tmp);
416 case 'h': // h -- history of the last few (WOP_SIZE) steps
417 if (rixCounter > WOP_SIZE) {
418 tmp64 = rixCounter - WOP_SIZE;
422 for(;tmp64 < rixCounter;tmp64++) {
425 rixTidBack[tmp64 % WOP_SIZE],
426 rixOpBack[tmp64 % WOP_SIZE]);
429 c = getc(rixCmdFile);
433 debugTrace(DEBUG_hpc,"strange command from HPCRIX (%d)",c);
435 c = getc(rixCmdFile);
437 while (c != 10) { // the end of the line
438 c = getc(rixCmdFile); // to the end of the line
440 c = getc(rixCmdFile); // the first char on the next command
443 debugTrace(DEBUG_hpc,"leaving breakPointCommand");
447 /* This is called after all the modules have registered their local tixboxes,
448 * and does a sanity check: are we good to go?
455 debugTrace(DEBUG_hpc,"startupHpc");
457 if (hpc_inited == 0) {
460 // HPCRIX contains the name of the file to send our dynamic runtime output to (a named pipe).
462 hpcRix = getenv("HPCRIX");
471 if (sscanf(hpcRix,"%d:%d",&rixFD,&rixCmdFD) != 2) {
472 /* Bad format for HPCRIX.
474 debugTrace(DEBUG_hpc,"Bad HPCRIX (%s)",hpcRix);
478 debugTrace(DEBUG_hpc,"found HPCRIX pipes: %d:%d",rixFD,rixCmdFD);
480 rixFile = fdopen(rixFD,"w");
481 assert(rixFile != NULL);
483 rixCmdFile = fdopen(rixCmdFD,"r");
484 assert(rixCmdFile != NULL);
486 // If we fork a process, then we do not want ticks inside
487 // the sub-process to talk to the debugger. So we remember
488 // our pid at startup time, so we can check if we are still
489 // the original process.
491 debuggee_pid = getpid();
495 fprintf(rixFile,"Starting %s\n",prog_name);
496 fprintf(rixFile,"[");
498 for(;tmpModule != 0;tmpModule = tmpModule->next) {
500 fprintf(rixFile,",");
504 fprintf(rixFile,"(\"%s\",%u)",
506 tmpModule->tickCount);
508 tixCount += tmpModule->tickCount;
510 debugTrace(DEBUG_hpc,"(tracer)%s: %u (offset=%u) (hash=%u)\n",
512 tmpModule->tickCount,
514 tmpModule->tickOffset);
517 fprintf(rixFile,"]\n");
520 // Allocate the tixBox breakpoint array
521 // These are set to 1 if you want to
522 // stop at a specific breakpoint
523 tixBoxBP = (int *)calloc(tixCount,sizeof(int));
529 /* Called at the end of execution, to write out the Hpc *.tix file
530 * for this exection. Safe to call, even if coverage is not used.
535 int i, inner_comma, outer_comma;
537 debugTrace(DEBUG_hpc,"exitHpc");
539 if (hpc_inited == 0) {
543 FILE *f = fopen(tixFilename,"w");
549 for(;tmpModule != 0;tmpModule = tmpModule->next) {
555 fprintf(f," TixModule \"%s\" %u %u [",
558 tmpModule->tickCount);
559 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
561 tmpModule->tickCount,
563 tmpModule->tickOffset);
566 for(i = 0;i < tmpModule->tickCount;i++) {
573 if (tmpModule->tixArr) {
574 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
585 for(;tmpModule != 0;tmpModule = tmpModule->next) {
586 if (!tmpModule->tixArr) {
587 debugTrace(DEBUG_hpc,
588 "warning: module %s did not register any hpc tick data\n",
592 for(i = 0;i < tmpModule->tickCount;i++) {
599 if (tmpModule->tixArr) {
600 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
612 if (rixFile != NULL) {
613 hs_hpc_tick(RixFinishedOp,(StgThreadID)0);
616 if (rixCmdFile != NULL) {