我正在尝试使用Linux中的ptrace。我正尝试使用/proc/pid/mem接口写入被跟踪进程的内存。
我用于完成此任务的函数是:
void write_proc(pid_t child, unsigned long int addr) {
char mem_file_name[100];
char buf[10]="hope";
int mem_fd;
memset( (void*)mem_file_name, 0, 100);
memset( (void *)buf, 0, 10);
sprintf(mem_file_name, "/proc/%d/mem", child);
mem_fd = open(mem_file_name, O_RDONLY);
lseek(mem_fd, addr , SEEK_SET);
if (write(mem_fd, buf, 5) < 0 )
perror("Writing");
return;
}
但是我总是会遇到错误:写入:坏的文件描述符。
使用这种方法编写跟踪进程是否可行?
ptrace
的用例是什么? - Basile Starynkevitch