I want to get instruction from running process and change it using ptrace. When variable instr (contains current instruction - PTRACE_PEEKDATA) is unsigned everything works, but when I change it to long int there is an error (memory dump). ptrace(PTRACE_PEEKDATA, ...) returns long int so this shouldn't be a problem. I work on Ubuntu.
Where I made a mistake? I'm new to it so this propably will be something stupid.
My code:
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/types.h>
#include <stdlib.h>
#include <wait.h>
int main()
{
int status;
char *pid_char;
pid_t PID;
struct user_regs_struct reg; /* register */
long int instr;
unsigned changedInstr;
printf("Tracee PID: ");
scanf("%s", pid_char);
PID = atoi(pid_char);
printf("\n");
/* PTRACE STARTS */
ptrace(PTRACE_ATTACH, PID, NULL, NULL);
waitpid(PID, &status, 0);
ptrace(PTRACE_GETREGS, PID, NULL, ®);
instr = ptrace(PTRACE_PEEKDATA, PID, reg.rip, NULL);
printf("Current Instruction: %llx\n", instr);
scanf("%u", &changedInstr);
ptrace(PTRACE_POKEDATA, PID, reg.rip, &changedInstr);
ptrace(PTRACE_DETACH, PID, NULL, NULL);
return 0;
}