A C programming language example
The following C program contains two instrumented sections which perform a trivial floating point operation, print the results, and then launch the command interpreter to execute the ls -R / 2>&1 >/dev/null command.
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <libhpm.h>
void
do_work()
{
pid_t p, wpid;
int i, status;
float f1 = 9.7641, f2 = 2.441, f3 = 0.0;
f3 = f1 / f2;
printf("f3=%f\n", f3);
p = fork();
if (p == -1) {
perror("Mike fork error");
exit(1);
}
if (p == 0) {
i = execl("/usr/bin/sh", "sh", "-c", "ls -R / 2>&1 >/dev/null", 0);
perror("Mike execl error");
exit(2);
}
else
wpid = waitpid(p, &status, WUNTRACED | WCONTINUED);
if (wpid == -1) {
perror("Mike waitpid error");
exit(3);
}
return;
}
main(int argc, char **argv)
{
int taskID = 999;
hpmInit(taskID, "my_program");
hpmStart(1, "outer call");
do_work();
hpmStart(2, "inner call");
do_work();
hpmStop(2);
hpmStop(1);
hpmTerminate(taskID);
}