Skip to content

Commit

Permalink
apicalls.d: hack hack hack
Browse files Browse the repository at this point in the history
Try to work around a situation when parent process is terminated before
its children are initialized.
  • Loading branch information
rodionovd committed Jul 8, 2015
1 parent f4e5c76 commit 224f470
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
31 changes: 13 additions & 18 deletions analyzer/darwin/lib/dtrace/apicalls.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ profile:::tick-1sec
dtrace:::BEGIN
{
pidresume($target);
#ifdef WAS_EXECED
/* Since (1) we have now dtrace scripts attached to both
* parent and child processes and (2) they both have *the same* PID,
* we'll get the same results from both these scripts.
* To fix this, I just stop tracing the child here. */
exit(0);
#endif
}
#ifdef WAS_EXECED
dtrace:::BEGIN
{
/* Since (1) we have now dtrace scripts attached to both
* parent and child processes and (2) they both have *the same* PID,
* we'll get the same results from both these scripts.
* To fix this, I just stop tracing the child here. */
exit(0);
}
#endif

/* TODO(rodionovd): it looks like there's a bug in Apple dtrace that keeps
* dtrace running even when its target (specified via -p) was already terminated.
* Maybe I'm doing something wrong, but here's a temporary workaround.
Expand All @@ -93,11 +91,12 @@ profile:::tick-1sec
}
#endif

/* FORKs */
/* FORK */
proc:::create
/ pid == $target /
{
tracked[args[0]->pr_pid] = 1;
system("echo %d >> \"%s.lock\"", args[0]->pr_pid, str(OUTPUT_FILE));
}
proc:::start
/ tracked[pid] == 1 /
Expand All @@ -111,10 +110,6 @@ proc:::start
/* EXEC */
proc:::exec
/ pid == $target /
{ /* exec-success probe won't fire without this */ }

proc:::exec-success
/ ppid == $target /
{
tracked[pid] = 2;
}
Expand All @@ -131,10 +126,10 @@ syscall::stat64:entry
dtrace:::END
{
#ifdef ROOT
/* It takes children scripts some time to write final stuff to the output
* file (mainly because of printf() caching). */
system("sleep 1.5");
system("sleep 0.7 && while [ $(cat \"%s.lock\" | wc -l) != \"0\" ]; do sleep 0.1; done", str(OUTPUT_FILE));
printf("## %s done ##", SCRIPT_NAME);
#else
system("sed -i \"\" \"/%d/d\" \"%s.lock\"", $target, str(OUTPUT_FILE));
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion tests/assets/test_apicalls_children.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main(int argc, char *argv[])
printf("CHILED FAILED\n");
} else {
int status;
wait(&status);
// wait(&status);
printf("Parent done\n");
}

Expand Down
2 changes: 1 addition & 1 deletion tests/assets/test_apicalls_children_root.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "CHILD FAILED\n");
} else {
int status;
wait(&status);
// wait(&status);
printf("parent done\n");
}

Expand Down

0 comments on commit 224f470

Please sign in to comment.