2 * (c)2006 Galois Connections, Inc.
19 /* 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 int totalTickCount = 0; // How many ticks have we got to work with
29 static FILE *tixFile; // file being read/written
30 static int tix_ch; // current char
31 static StgWord64 magicTixNumber; // Magic/Hash number to mark .tix files
33 static FILE *rixFile = NULL; // The tracer file/pipe (to debugger)
34 static FILE *rixCmdFile = NULL; // The tracer file/pipe (from debugger)
35 static StgWord64 rixCounter = 0; // The global event counter
36 static int debuggee_pid;
39 RixThreadFinishedOp = -1,
45 typedef struct _Info {
46 char *modName; // name of module
47 int tickCount; // number of ticks
48 int tickOffset; // offset into a single large .tix Array
49 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
53 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
55 #define PRIuWORD64 "lu"
57 #define PRIuWORD64 "llu"
62 StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file.
63 int totalTixes = 0; // total number of tix boxes.
65 static char *tixFilename;
68 static void failure(char *msg) {
69 fprintf(stderr,"Hpc failure: %s\n",msg);
70 fprintf(stderr,"(perhaps remove .tix file?)\n");
75 static int init_open(char *filename)
77 tixFile = fopen(filename,"r");
81 tix_ch = getc(tixFile);
85 static void expect(char c) {
87 fprintf(stderr,"Hpc: parse failed (%c,%c)\n",tix_ch,c);
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);
123 static void hpc_init(void) {
127 if (hpc_inited != 0) {
133 tixFilename = (char *) malloc(strlen(prog_name) + 6);
134 sprintf(tixFilename, "%s.tix", prog_name);
136 if (init_open(tixFilename)) {
144 magicTixNumber = expectWord64();
148 while(tix_ch != ']') {
149 tmpModule = (Info *)calloc(1,sizeof(Info));
152 tmpModule -> modName = expectString();
156 tmpModule -> tickCount = (int)expectWord64();
161 tmpModule -> tickOffset = totalTixes;
162 totalTixes += tmpModule -> tickCount;
164 tmpModule -> tixArr = 0;
169 nextModule->next=tmpModule;
171 nextModule=tmpModule;
180 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
183 for(i = 0;i < totalTixes;i++) {
188 tixBoxes[i] = expectWord64();
195 // later, we will find a binary specific
196 magicTixNumber = (StgWord64)0;
200 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
201 * This memory can be uninitized, because we will initialize it with either the contents
202 * of the tix file, or all zeros.
206 hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) {
207 Info *tmpModule, *lastModule;
212 fprintf(stderr,"hs_hpc_module(%s,%d)\n",modName,modCount);
220 for(;tmpModule != 0;tmpModule = tmpModule->next) {
221 if (!strcmp(tmpModule->modName,modName)) {
222 if (tmpModule->tickCount != modCount) {
223 failure("inconsistent number of tick boxes");
225 assert(tmpModule->tixArr == 0);
226 assert(tixBoxes != 0);
227 tmpModule->tixArr = tixArr;
228 for(i=0;i < modCount;i++) {
229 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
231 return tmpModule->tickOffset;
233 lastModule = tmpModule;
235 // Did not find entry so add one on.
236 tmpModule = (Info *)calloc(1,sizeof(Info));
237 tmpModule->modName = modName;
238 tmpModule->tickCount = modCount;
240 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
242 tmpModule->tickOffset = 0;
244 tmpModule->tixArr = tixArr;
245 for(i=0;i < modCount;i++) {
253 lastModule->next=tmpModule;
257 fprintf(stderr,"end: hs_hpc_module\n");
262 static void breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid);
265 static StgThreadID previousTid = 0;
266 static StgWord64 rixBPCounter = 0; // The global event breakpoint counter
267 static int *tixBoxBP;
268 static HpcRixOp rixOpBack[WOP_SIZE]; // The actual op
269 static HpcRixOp rixTidBack[WOP_SIZE]; // Tid's before the op
272 hs_hpc_raise_event(StgTSO *current_tso) {
273 hs_hpc_tick(RixRaiseOp,current_tso);
277 hs_hpc_thread_finished_event(StgTSO *current_tso) {
278 hs_hpc_tick(RixThreadFinishedOp,current_tso);
281 /* Called on every tick, dynamically, sending to our
282 * external record of program execution.
286 hs_hpc_tick(int rixOp, StgTSO *current_tso) {
288 fprintf(stderr,"hs_hpc_tick(%x)\n",rixOp);
290 if (rixFile == NULL) {
293 assert(rixCmdFile != NULL);
294 StgThreadID tid = (current_tso == 0) ? 0 : current_tso->id;
296 // now check to see if we have met a breakpoint condition
297 if (rixCounter == rixBPCounter
298 || tid != previousTid) {
299 breakPointCommand(rixOp,tid);
303 if (tixBoxBP[rixOp] == 1) { // reached a bp tixbox
304 breakPointCommand(rixOp,tid);
307 // record the special operation
308 breakPointCommand(rixOp,tid);
311 // update the history information.
313 rixOpBack[rixCounter % WOP_SIZE] = rixOp;
314 rixTidBack[rixCounter % WOP_SIZE] = tid;
318 fprintf(stderr,"end: hs_hpc_tick\n");
323 printEvent(FILE *out,StgWord64 rixCounter,StgThreadID rixTid,HpcRixOp rixOp) {
326 printEvent(stderr,rixCounter,rixTid,rixOp);
329 fprintf(out,"Event %" PRIuWORD64 " %u ",rixCounter,(unsigned int)rixTid);
331 case RixThreadFinishedOp:
332 fprintf(out,"ThreadFinished\n");
335 fprintf(out,"Raise\n");
338 fprintf(out,"Finished\n");
341 fprintf(out,"%u\n",rixOp);
346 breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid) {
348 unsigned int tmp = 0;
350 if (getpid() != debuggee_pid) {
351 // We are not the original process, to do not issue
352 // any events, and do not try to talk to the debugger.
356 printEvent(rixFile,rixCounter,rixTid,rixOp);
358 /* From here, you can ask some basic questions.
360 * c<nat> set the (one) counter breakpoint
361 * s<nat> set the (many) tickbox breakpoint
362 * u<nat> unset the (many) tickbox breakpoint
365 * Note that you aways end up here on the first tick
366 * because the rixBPCounter starts equal to 0.
368 int c = getc(rixCmdFile);
369 while(c != 10 && c != -1) {
371 case 'c': // c1234 -- set counter breakpoint at 1234
372 c = getc(rixCmdFile);
375 tmp64 = tmp64 * 10 + (c - '0');
376 c = getc(rixCmdFile);
379 fprintf(stderr,"setting countBP = %" PRIuWORD64 "\n",tmp64);
381 rixBPCounter = tmp64;
383 case 's': // s2323 -- set tick box breakpoint at 2323
384 c = getc(rixCmdFile);
387 tmp = tmp * 10 + (c - '0');
388 c = getc(rixCmdFile);
391 fprintf(stderr,"seting bp for tix %d\n",tmp);
395 case 'u': // u2323 -- unset tick box breakpoint at 2323
396 c = getc(rixCmdFile);
399 tmp = tmp * 10 + (c - '0');
400 c = getc(rixCmdFile);
403 fprintf(stderr,"unseting bp for tix %d\n",tmp);
407 case 'h': // h -- history of the last few (WOP_SIZE) steps
408 if (rixCounter > WOP_SIZE) {
409 tmp64 = rixCounter - WOP_SIZE;
413 for(;tmp64 < rixCounter;tmp64++) {
414 printEvent(rixFile,tmp64,rixTidBack[tmp64 % WOP_SIZE],rixOpBack[tmp64 % WOP_SIZE]);
417 c = getc(rixCmdFile);
421 fprintf(stderr,"strange command from HPCRIX (%d)\n",c);
423 c = getc(rixCmdFile);
425 while (c != 10) { // the end of the line
426 c = getc(rixCmdFile); // to the end of the line
428 c = getc(rixCmdFile); // the first char on the next command
431 fprintf(stderr,"re entering program\n");
435 /* This is called after all the modules have registered their local tixboxes,
436 * and does a sanity check: are we good to go?
445 fprintf(stderr,"startupHpc\n");
448 if (hpc_inited == 0) {
455 for(;tmpModule != 0;tmpModule = tmpModule->next) {
456 totalTickCount += tmpModule->tickCount;
457 if (!tmpModule->tixArr) {
458 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
460 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
466 // HPCRIX contains the name of the file to send our dynamic runtime output to (a named pipe).
468 hpcRix = getenv("HPCRIX");
476 if (sscanf(hpcRix,"%d:%d",&rixFD,&rixCmdFD) != 2) {
477 /* Bad format for HPCRIX.
479 fprintf(stderr,"Bad HPCRIX (%s)\n",hpcRix);
484 fprintf(stderr,"found HPCRIX pipes: %d:%d\n",rixFD,rixCmdFD);
487 rixFile = fdopen(rixFD,"w");
488 assert(rixFile != NULL);
490 rixCmdFile = fdopen(rixCmdFD,"r");
491 assert(rixCmdFile != NULL);
493 // If we fork a process, then we do not want ticks inside
494 // the sub-process to talk to the debugger. So we remember
495 // our pid at startup time, so we can check if we are still
496 // the original process.
498 debuggee_pid = getpid();
502 fprintf(rixFile,"Starting %s\n",prog_name);
503 fprintf(rixFile,"[");
505 for(;tmpModule != 0;tmpModule = tmpModule->next) {
507 fprintf(rixFile,",");
511 fprintf(rixFile,"(\"%s\",%u)",
513 tmpModule->tickCount);
515 fprintf(stderr,"(tracer)%s: %u (offset=%u)\n",
517 tmpModule->tickCount,
518 tmpModule->tickOffset);
521 fprintf(rixFile,"]\n");
524 // Allocate the tixBox breakpoint array
525 // These are set to 1 if you want to
526 // stop at a specific breakpoint
527 tixBoxBP = (int *)calloc(1,sizeof(int));
533 /* Called at the end of execution, to write out the Hpc *.tix file
534 * for this exection. Safe to call, even if coverage is not used.
542 fprintf(stderr,"exitHpc\n");
545 if (hpc_inited == 0) {
549 FILE *f = fopen(tixFilename,"w");
553 fprintf(f,"Tix %" PRIuWORD64 " [", magicTixNumber);
555 for(;tmpModule != 0;tmpModule = tmpModule->next) {
561 fprintf(f,"(\"%s\",%u)",
563 tmpModule->tickCount);
565 fprintf(stderr,"%s: %u (offset=%u)\n",
567 tmpModule->tickCount,
568 tmpModule->tickOffset);
575 for(;tmpModule != 0;tmpModule = tmpModule->next) {
576 if (!tmpModule->tixArr) {
577 fprintf(stderr,"warning: module %s did not register any hpc tick data\n",
581 for(i = 0;i < tmpModule->tickCount;i++) {
588 if (tmpModule->tixArr) {
589 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
600 if (rixFile != NULL) {
601 hs_hpc_tick(RixFinishedOp,(StgThreadID)0);
604 if (rixCmdFile != NULL) {