Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* C standard headers */
- #include <errno.h>
- #include <inttypes.h>
- #include <setjmp.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Linux headers */
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <sys/ptrace.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <sys/user.h>
- /* Architecture-specific headers */
- #include <asm/ptrace.h>
- #include <asm/unistd.h>
- #include <signal.h>
- #include <assert.h>
- typedef void fun_moved_from_context();
- //using namespace std;
- void attachTo(pid_t pid, int id) {
- long ret = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
- printf("%i attachTo: %ld\n", id, ret);
- }
- void seizeTo(pid_t pid, int id) {
- long ret = ptrace (PTRACE_SEIZE, pid, NULL, NULL);
- //assert(ret > 0);
- printf("%i seizeTo: %ld\n", id, ret);
- }
- void detachFrom(pid_t pid, int id) {
- long ret = ptrace (PTRACE_DETACH, pid, NULL, NULL);
- printf("%i detachFrom: %ld\n", id, ret);
- }
- void setOptions(pid_t pid, int id) {
- long ret = ptrace(PTRACE_SETOPTIONS, pid, NULL, (void*) (PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC | PTRACE_O_TRACEEXIT | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK));
- printf("%i setOptions: %ld\n", id, ret);
- }
- void setVarData(pid_t pid, volatile bool* can_run, void* data, int id) {
- long ret = ptrace(PTRACE_POKEDATA, pid, (void*)can_run, (void*)data);
- printf("%i setVarData: %ld\n", id, ret);
- }
- void cont(pid_t pid, int id) {
- long ret = ptrace (PTRACE_CONT, pid, NULL, NULL);
- printf("%i cont: %ld\n", id, ret);
- }
- void interrupt(pid_t pid, int id) {
- long ret = ptrace (PTRACE_INTERRUPT, pid, NULL, NULL);
- printf("%i interrupt: %ld\n", id, ret);
- }
- void debug(int id) {
- int status;
- while (true) {
- printf("%i while\n", id);
- sleep(1);
- struct user_regs regs;
- pid_t recv = wait(&status);
- if (recv == -1) {
- printf("%i Debugger exiting\n", id);
- return 0;
- } else {
- if (WIFSTOPPED(status)) {
- int signal = WSTOPSIG(status);
- printf("%i signal: %i\n",id,signal);
- switch (signal) {
- case SIGTRAP: {
- int event_code = (status >> 8) ^ SIGTRAP;
- printf("%i event_code: %i\n",id,event_code);
- switch (event_code) {
- case PTRACE_EVENT_FORK << 8:
- printf("%i FORK EVENT.\n", id);
- cont(recv,id);
- break;
- case PTRACE_EVENT_EXIT << 8:
- printf("%i %li exited.\n", id, recv);
- return 0;
- break;
- default: {
- printf("%i recv: %i ; status: %i\n", id, recv, status);
- long ret=ptrace (PTRACE_GETREGS, recv, NULL, ®s);
- regs.uregs[15] += 2;//addr_size;
- printf("%i p: new PC: %lx\n", id, regs.uregs[15]);
- ptrace (PTRACE_SETREGS, recv, NULL, ®s);
- cont(recv,id);
- }
- }
- break;
- }
- default: {
- cont(recv,id);
- break;
- }
- }
- }
- }
- }
- }
- int main() {
- int pid;
- int me = getpid();
- printf("Hello, I am %d\n", me);
- printf("pid:");
- scanf("%d",&pid);
- if (pid == 0) {
- printf("bkpt asm\n");
- asm("bkpt");
- } else {
- attachTo(pid, me);
- printf("start waitpid\n");
- waitpid(pid, NULL, __WALL);
- printf("end waitpid\n");
- setOptions(pid, me);
- cont(pid,me);
- debug(me);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement