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 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;
67 static void failure(char *msg) {
68 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
69 fprintf(stderr,"Hpc failure: %s\n",msg);
70 fprintf(stderr,"(perhaps remove .tix file?)\n");
74 static int init_open(char *filename)
76 tixFile = fopen(filename,"r");
80 tix_ch = getc(tixFile);
84 static void expect(char c) {
86 failure("parse error when reading .tix file");
88 tix_ch = getc(tixFile);
91 static void ws(void) {
92 while (tix_ch == ' ') {
93 tix_ch = getc(tixFile);
97 static char *expectString(void) {
101 while (tix_ch != '"') {
102 tmp[tmp_ix++] = tix_ch;
103 tix_ch = getc(tixFile);
107 res = malloc(tmp_ix);
112 static StgWord64 expectWord64(void) {
114 while (isdigit(tix_ch)) {
115 tmp = tmp * 10 + (tix_ch -'0');
116 tix_ch = getc(tixFile);
121 static void hpc_init(void) {
125 if (hpc_inited != 0) {
131 tixFilename = (char *) malloc(strlen(prog_name) + 6);
132 sprintf(tixFilename, "%s.tix", prog_name);
134 if (init_open(tixFilename)) {
142 magicTixNumber = expectWord64();
146 while(tix_ch != ']') {
147 tmpModule = (Info *)calloc(1,sizeof(Info));
150 tmpModule -> modName = expectString();
154 tmpModule -> tickCount = (int)expectWord64();
159 tmpModule -> tickOffset = totalTixes;
160 totalTixes += tmpModule -> tickCount;
162 tmpModule -> tixArr = 0;
167 nextModule->next=tmpModule;
169 nextModule=tmpModule;
178 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
181 for(i = 0;i < totalTixes;i++) {
186 tixBoxes[i] = expectWord64();
193 // later, we will find a binary specific
194 magicTixNumber = (StgWord64)0;
198 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
199 * This memory can be uninitized, because we will initialize it with either the contents
200 * of the tix file, or all zeros.
204 hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) {
205 Info *tmpModule, *lastModule;
209 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
216 for(;tmpModule != 0;tmpModule = tmpModule->next) {
217 if (!strcmp(tmpModule->modName,modName)) {
218 if (tmpModule->tickCount != modCount) {
219 failure("inconsistent number of tick boxes");
221 assert(tmpModule->tixArr == 0);
222 assert(tixBoxes != 0);
223 tmpModule->tixArr = tixArr;
224 for(i=0;i < modCount;i++) {
225 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
227 return tmpModule->tickOffset;
229 lastModule = tmpModule;
231 // Did not find entry so add one on.
232 tmpModule = (Info *)calloc(1,sizeof(Info));
233 tmpModule->modName = modName;
234 tmpModule->tickCount = modCount;
236 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
238 tmpModule->tickOffset = 0;
240 tmpModule->tixArr = tixArr;
241 for(i=0;i < modCount;i++) {
249 lastModule->next=tmpModule;
252 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
257 static void breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid);
260 static StgThreadID previousTid = 0;
261 static StgWord64 rixBPCounter = 0; // The global event breakpoint counter
262 static int *tixBoxBP;
263 static HpcRixOp rixOpBack[WOP_SIZE]; // The actual op
264 static HpcRixOp rixTidBack[WOP_SIZE]; // Tid's before the op
267 hs_hpc_raise_event(StgTSO *current_tso) {
268 hs_hpc_tick(RixRaiseOp,current_tso);
272 hs_hpc_thread_finished_event(StgTSO *current_tso) {
273 hs_hpc_tick(RixThreadFinishedOp,current_tso);
276 /* Called on every tick, dynamically, sending to our
277 * external record of program execution.
281 hs_hpc_tick(int rixOp, StgTSO *current_tso) {
283 debugTrace(DEBUG_hpc,"hs_hpc_tick(%x)",rixOp);
285 if (rixFile == NULL) {
288 assert(rixCmdFile != NULL);
289 StgThreadID tid = (current_tso == 0) ? 0 : current_tso->id;
291 // now check to see if we have met a breakpoint condition
292 if (rixCounter == rixBPCounter
293 || tid != previousTid) {
294 breakPointCommand(rixOp,tid);
298 if (tixBoxBP[rixOp] == 1) { // reached a bp tixbox
299 breakPointCommand(rixOp,tid);
302 // record the special operation
303 breakPointCommand(rixOp,tid);
306 // update the history information.
308 rixOpBack[rixCounter % WOP_SIZE] = rixOp;
309 rixTidBack[rixCounter % WOP_SIZE] = tid;
312 debugTrace(DEBUG_hpc, "end: hs_hpc_tick");
316 printEvent(FILE *out,StgWord64 rixCounter,StgThreadID rixTid,HpcRixOp rixOp) {
321 "Event %" PRIuWORD64 " %u ",
323 (unsigned int)rixTid);
326 case RixThreadFinishedOp:
327 sprintf(suffixMsg,"ThreadFinished");
330 sprintf(suffixMsg,"Raise");
333 sprintf(suffixMsg,"Finished");
336 sprintf(suffixMsg,"%u",rixOp);
339 fprintf(out,"%s%s\n",prefixMsg,suffixMsg);
340 debugTrace(DEBUG_hpc,"sending %s%s",prefixMsg,suffixMsg);
344 breakPointCommand(HpcRixOp rixOp, StgThreadID rixTid) {
346 unsigned int tmp = 0;
348 if (getpid() != debuggee_pid) {
349 // We are not the original process, to do not issue
350 // any events, and do not try to talk to the debugger.
354 debugTrace(DEBUG_hpc,"breakPointCommand %d %x",rixOp,(unsigned int)rixTid);
356 printEvent(rixFile,rixCounter,rixTid,rixOp);
359 /* From here, you can ask some basic questions.
361 * c<nat> set the (one) counter breakpoint
362 * s<nat> set the (many) tickbox breakpoint
363 * u<nat> unset the (many) tickbox breakpoint
366 * Note that you aways end up here on the first tick
367 * because the rixBPCounter starts equal to 0.
369 int c = getc(rixCmdFile);
370 while(c != 10 && c != -1) {
372 case 'c': // c1234 -- set counter breakpoint at 1234
373 c = getc(rixCmdFile);
376 tmp64 = tmp64 * 10 + (c - '0');
377 c = getc(rixCmdFile);
379 debugTrace(DEBUG_hpc,"setting countBP = %" PRIuWORD64,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 debugTrace(DEBUG_hpc,"seting bp for tix %d",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 debugTrace(DEBUG_hpc,"unseting bp for tix %d",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++) {
416 rixTidBack[tmp64 % WOP_SIZE],
417 rixOpBack[tmp64 % WOP_SIZE]);
420 c = getc(rixCmdFile);
424 debugTrace(DEBUG_hpc,"strange command from HPCRIX (%d)",c);
426 c = getc(rixCmdFile);
428 while (c != 10) { // the end of the line
429 c = getc(rixCmdFile); // to the end of the line
431 c = getc(rixCmdFile); // the first char on the next command
434 debugTrace(DEBUG_hpc,"leaving breakPointCommand");
438 /* This is called after all the modules have registered their local tixboxes,
439 * and does a sanity check: are we good to go?
447 debugTrace(DEBUG_hpc,"startupHpc");
449 if (hpc_inited == 0) {
456 for(;tmpModule != 0;tmpModule = tmpModule->next) {
457 totalTickCount += tmpModule->tickCount;
458 if (!tmpModule->tixArr) {
459 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
461 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
467 // HPCRIX contains the name of the file to send our dynamic runtime output to (a named pipe).
469 hpcRix = getenv("HPCRIX");
478 if (sscanf(hpcRix,"%d:%d",&rixFD,&rixCmdFD) != 2) {
479 /* Bad format for HPCRIX.
481 debugTrace(DEBUG_hpc,"Bad HPCRIX (%s)",hpcRix);
485 debugTrace(DEBUG_hpc,"found HPCRIX pipes: %d:%d",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 tixCount += tmpModule->tickCount;
517 debugTrace(DEBUG_hpc,"(tracer)%s: %u (offset=%u)\n",
519 tmpModule->tickCount,
520 tmpModule->tickOffset);
523 fprintf(rixFile,"]\n");
526 // Allocate the tixBox breakpoint array
527 // These are set to 1 if you want to
528 // stop at a specific breakpoint
529 tixBoxBP = (int *)calloc(tixCount,sizeof(int));
535 /* Called at the end of execution, to write out the Hpc *.tix file
536 * for this exection. Safe to call, even if coverage is not used.
543 debugTrace(DEBUG_hpc,"exitHpc");
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);
564 debugTrace(DEBUG_hpc,"%s: %u (offset=%u)\n",
566 tmpModule->tickCount,
567 tmpModule->tickOffset);
573 for(;tmpModule != 0;tmpModule = tmpModule->next) {
574 if (!tmpModule->tixArr) {
575 debugTrace(DEBUG_hpc,
576 "warning: module %s did not register any hpc tick data\n",
580 for(i = 0;i < tmpModule->tickCount;i++) {
587 if (tmpModule->tixArr) {
588 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
599 if (rixFile != NULL) {
600 hs_hpc_tick(RixFinishedOp,(StgThreadID)0);
603 if (rixCmdFile != NULL) {