6 #include <sys/syscall.h>
7 #include <sys/ptrace.h>
13 #include <sys/select.h>
28 #define MAXTHREADS 128
35 static char libname[4096][1024];
38 struct user_regs_struct
59 #define __WCLONE 0x80000000
72 char fname[128], line[4096];
73 sprintf(fname,
"/proc/%d/maps", pid);
74 FILE *fp = fopen(fname,
"r");
78 char lastlibname[1024];
80 while (fgets(line,
sizeof(line), fp) != NULL) {
82 p = strstr(line,
"/");
86 strcpy(lastlibname, p);
89 p = strstr(line,
"[");
93 strcpy(lastlibname, p);
96 strcpy(libname[numOfSegments], lastlibname);
98 n1 = strtok(line,
"- ");
99 n2 = strtok(NULL,
"- ");
100 memorySegments[numOfSegments].start = (
void*)strtoul(n1, NULL, 16);
101 memorySegments[numOfSegments].end = (
void*)strtoul(n2, NULL, 16);
108 for (i = 0 ; i < numOfSegments ; i++)
110 std::cout <<
"Maps: " << memorySegments[i].start <<
", "
111 << memorySegments[i].end <<
", " << libname[i] << std::endl;
118 for (i = 0 ; i < numOfSegments ; i++)
120 if ((
void*)address >= memorySegments[i].start && (
void*)address <= memorySegments[i].end)
128 strcpy(
cmd, argv[1]);
129 printf(
"tracer: traced process executable file: %s:: %s %s\n",
cmd, argv[1], argv[2]);
135 while (*current !=
' ')
137 tmpnum[i] = *current;
150 while (*current !=
' ')
152 tmpnum[i] = *current;
162 void test(pid_t pid,
int wait_val)
164 printf(
"tracer for %d WIFEXITED: %d\n", pid, WIFEXITED(wait_val));
165 printf(
"tracer for %d WEXITSTATUS: %d\n", pid, WEXITSTATUS(wait_val));
166 printf(
"tracer for %d WIFSIGNALED: %d\n", pid, WIFSIGNALED(wait_val));
167 printf(
"tracer for %d WTERMSIG: %d\n", pid, WTERMSIG(wait_val));
168 printf(
"tracer for %d WIFSTOPPED: %d\n", pid, WIFSTOPPED(wait_val));
169 printf(
"tracer for %d WSTOPSIG: %d\n", pid, WSTOPSIG(wait_val));
170 printf(
"tracer for %d WIFCONTINUED: %d\n", pid, WIFCONTINUED(wait_val));
175 static int my_waitpid(pid_t pid,
int *state,
int flags)
180 ret = waitpid(pid, state, flags);
181 }
while (ret == -1 && errno == EINTR);
185 static int my_wait(pid_t pid,
int *wait_val)
188 return my_waitpid(pid, wait_val, 0);
190 return my_waitpid(pid, wait_val, __WCLONE);
197 #if defined(__linux__)
200 printf(
"tracer for %d ptrace getregs", pid);
211 if (ptrace(PT_STEP, pid, 0, 0) != 0)
213 printf(
"tracer for %d ptrace singlestep", pid);
216 assert(pid == my_wait(pid, &wait_val));
217 if (wait_val == 1407)
return 0;
218 if (WSTOPSIG(wait_val) == SIGTERM)
220 printf(
"tracer for %d terminated signal\n", pid);
223 else if (wait_val == 0)
225 printf(
"tracer for %d finished signal\n", pid);
240 if (ptrace(PT_ATTACH, pid, 0, 0) != 0)
242 printf(
"tracer for %d ptrace attach", pid);
245 assert(pid == my_wait(pid, &wait_val));
247 printf(
"tracer for %d after attach\n", pid);
251 if (ptrace(PT_CONTINUE, pid, 0, SIGUSR2) != 0)
253 printf(
"tracer for %d ptrace continue with SIGUSR2", pid);
257 assert(pid == my_wait(pid, &wait_val));
258 assert(SIGUSR2 == WSTOPSIG(wait_val));
261 printf(
"tracer for %d after memory protection\n", pid);
263 if (ptrace(PT_CONTINUE, pid, 0, 0) != 0)
265 printf(
"tracer for %d ptrace continue without SIGUSR2", pid);
272 printf(
"tracer for %d Waiting for the memory is protected\n", pid);
275 if (ptrace(PT_CONTINUE, pid, 0, 0) != 0)
277 printf(
"tracer for %d ptrace continue without SIGUSR2", pid);
281 printf(
"tracer for %d after continue\n", pid);
292 if (WSTOPSIG(wait_val) == SIGSEGV)
296 #if defined(__linux__)
299 printf(
"tracer for %d ptrace getregs", pid);
312 char convertcmd[1024];
313 memset(convertcmd, 0, 1024);
322 if (ptrace(PT_CONTINUE, pid, 0, SIGSEGV) != 0)
324 printf(
"tracer for %d ptrace continue with SIGSEGV", pid);
328 assert(pid == my_wait(pid, &wait_val));
329 assert(SIGUSR2 == WSTOPSIG(wait_val));
332 if (SIGUSR2 != WSTOPSIG(wait_val))
343 if (ptrace(PT_CONTINUE, pid, 0, SIGUSR1) != 0)
345 printf(
"tracer for %d ptrace continue with SIGUSR1\n", pid);
360 else if (WSTOPSIG(wait_val) == SIGUSR1)
364 if (ptrace(PT_DETACH, pid, 0, 0) != 0)
366 printf(
"tracer for %d ptrace detach\n", pid);
370 else if (WSTOPSIG(wait_val) == SIGUSR2)
373 if (ptrace(PT_CONTINUE, pid, 0, 0) != 0)
375 printf(
"tracer for %d ptrace continue without SIGSEGV\n", pid);
381 if (ptrace(PT_CONTINUE, pid, 0, 0) != 0)
383 printf(
"tracer for %d ptrace continue without SIGSEGV\n", pid);
393 printf(
"tracer for %d Exit code: %d\n", pid, tmp);
397 if (ptrace(PT_DETACH, pid, 0, 0) != 0)
398 printf(
"tracer for %d ptrace detach", pid);
408 pid = * ((pid_t *) pid_ptr);
420 printf(
"tracer for %d start threads[%d] = %d\n", pid, j,
threads[j]);
430 printf(
"tracer for %d join threads[%d] = %d\n", pid, j,
threads[j]);
432 printf(
"tracer for %d after join threads[%d] = %d\n", pid, j,
threads[j]);
453 select(
fildes[0] + 1, &rfds, NULL, NULL, NULL);
454 if ((len = read(
fildes[0], &code,
sizeof(code))) < 0)
break;
469 printf(
"tracer: filter stopping...\n");
480 printf(
"tracer stopping...\n");
481 unsigned long exitCode = 0;
483 printf(
"tracer: notifying filter to exit\n");
484 write(
fildes[1], &exitCode,
sizeof(exitCode));
491 int main(
int argc,
char *argv[])
G4ThreadLocal struct user_regs_struct regssinglestep
void outputAddresses(char *cmd)
int main(int argc, char **argv)
int SingleSteps(pid_t pid)
void waitForAllTracers(pid_t pid)
void processParameters(int, char *argv[])
G4ThreadLocal struct user_regs_struct regs
pthread_t tids[MAXTHREADS]
G4ThreadLocal int detectionFlag
int insertAddress(unsigned long address)
void generategdbscripts()
char * getLibName(long int address)
system("rm -rf dna.root")
printf("%d Experimental points found\n", nlines)
void startThreadTracers(pid_t pid)
pid_t threads[MAXTHREADS]
void * tracer_thread(void *pid_ptr)
pthread_mutex_t segFaultHandlingLock