我正在尝试读取进程的 proc/pid/mem 堆栈(我使用 ptrace 来跟踪它)
1)我阅读 /proc/pid/maps 并将堆栈的开头和结尾保存在
unsigned int start_stack, end_stack;
2)我使用 lseek 和 read 读取堆栈地址处的内存
当我尝试阅读时遇到问题:
int mem_file = open (mem_file_name, O_RDONLY);
if(mem_file==-1)perror("open file mem_file failed");
printf("start_stack = %x, end_stack = %x \n",
start_stack, end_stack);
/*I think the problem is here, but i'm not sure*/
if(lseek(mem_file, start_stack,
SEEK_SET)==-1)perror("lseek failed");
int buf_size = (int)(end_stack-start_stack);
buf = calloc(buf_size, sizeof(char));
if(read(mem_file, buf, size_buf) == -1 )perror("read failed");
printf("buf=");
for(i=0; i<size_buf; i+=2)
printf("%02x",buf[i]);
输出是:
start stack = bffde000, end stack = bffff000
buf = 00000000000000000000000000000000000000000000
也许我错了 lseek 的偏移量?在此示例中,偏移量为 (unsigned int)bffde00,即堆栈的开头。
有什么建议么 ?谢谢