Skip to content

Instantly share code, notes, and snippets.

@5kg
Last active December 16, 2015 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5kg/5451894 to your computer and use it in GitHub Desktop.
Save 5kg/5451894 to your computer and use it in GitHub Desktop.
all: mutator mutatee
mutator: mutator.o
g++ mutator.o -L/usr/lib/dyninst -ldyninstAPI -o mutator
mutatee: mutatee.o
gcc mutatee.o -std=gnu99 -g -o mutatee -lrt
mutatee.o: mutatee.c
gcc mutatee.c -std=gnu99 -g -c -o mutatee.o
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#define tic() do { struct timespec ts_start, ts_end; clock_gettime(CLOCK_MONOTONIC, &ts_start)
#define toc() clock_gettime(CLOCK_MONOTONIC, &ts_end); \
printf("%lfs\n", (ts_end.tv_sec - ts_start.tv_sec) + (double)(ts_end.tv_nsec - ts_start.tv_nsec)/1e9); } \
while (0)
void foo() {
sleep(1);
printf("I am foo()\n");
return;
}
void bar() {
printf("I am bar()\n");
return;
}
int main()
{
printf("Hello World!\n");
tic();
for (int i = 0; i < 10; ++i)
foo();
toc();
return 0;
}
#include <dyninst/BPatch.h>
#include <dyninst/BPatch_point.h>
#include <dyninst/BPatch_function.h>
#include <vector>
int main (int argc, const char* argv[]) {
BPatch bpatch;
BPatch_process *proc = bpatch.processCreate(argv[1], argv + 2);
//bpatch.setTrampRecursive(true);
//bpatch.setSaveFPR(false);
//bpatch.setInstrStackFrames(false);
//BPatch_process *proc = bpatch.processAttach(argv[1], atoi(argv[2]));
BPatch_image *image = proc->getImage();
std::vector<BPatch_function *> foo_fns, bar_fns;
image->findFunction("foo", foo_fns);
image->findFunction("bar", bar_fns);
std::vector<BPatch_snippet*> args;
BPatch_funcCallExpr call_bar(*bar_fns[0], args);
proc->insertSnippet(call_bar, (foo_fns[0]->findPoint(BPatch_entry))[0]);
proc->continueExecution();
//proc->detach(true);
while (!proc->isTerminated()) {
bpatch.waitForStatusChange();
}
return 0;
}
Hello World!
I am bar()
I am foo()
I am bar()
I am foo()
I am bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment