C程序在linux上转储堆栈的运行进程

时间:2014-02-11 06:31:19

标签: c linux

我正在尝试在linux fedora 20上转储正在运行的进程的堆栈(我想自己做这个没有gdb /...)

但看起来我的缓冲区(buf)的默认值永远不会被替换,即使/ proc / x / mem的read()返回正数。

我禁用了selinux

[me@localhost ~]$ cat /proc/2419/maps
00400000-00401000 r-xp 00000000 fd:02 164622                             /home/me/memo
00600000-00601000 r--p 00000000 fd:02 164622                             /home/me/memo
00601000-00602000 rw-p 00001000 fd:02 164622                             /home/me/memo
7fd7cbea2000-7fd7cc056000 r-xp 00000000 fd:02 8636                       /usr/lib64/libc-2.18.so
7fd7cc056000-7fd7cc256000 ---p 001b4000 fd:02 8636                       /usr/lib64/libc-2.18.so
7fd7cc256000-7fd7cc25a000 r--p 001b4000 fd:02 8636                       /usr/lib64/libc-2.18.so
7fd7cc25a000-7fd7cc25c000 rw-p 001b8000 fd:02 8636                       /usr/lib64/libc-2.18.so
7fd7cc25c000-7fd7cc261000 rw-p 00000000 00:00 0 
7fd7cc261000-7fd7cc281000 r-xp 00000000 fd:02 723                        /usr/lib64/ld-2.18.so
7fd7cc469000-7fd7cc46c000 rw-p 00000000 00:00 0 
7fd7cc47d000-7fd7cc480000 rw-p 00000000 00:00 0 
7fd7cc480000-7fd7cc481000 r--p 0001f000 fd:02 723                        /usr/lib64/ld-2.18.so
7fd7cc481000-7fd7cc482000 rw-p 00020000 fd:02 723                        /usr/lib64/ld-2.18.so
7fd7cc482000-7fd7cc483000 rw-p 00000000 00:00 0 
7fffc8479000-7fffc849a000 rw-p 00000000 00:00 0                          [stack]
7fffc854e000-7fffc8550000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]


[me@localhost ~]$ cat analyse.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <fcntl.h>

#define STACK_BEGIN 0x7fffc8479000
#define STACK_END 0x7fffc849a000
#define STACK_SIZE STACK_END - STACK_BEGIN

void main(int argc, char** argv)
{
    int pid = atoi(argv[1]);
    char mem_file_name[30];

    int mem_fd; 
    char buf[STACK_SIZE];

    sprintf(mem_file_name, "/proc/%d/mem", pid);
    mem_fd = open(mem_file_name, O_RDONLY);
    printf("PID=%d\n\
            NAME=%s\n\
            FD=%d\n\
            Initial value of buf : %x\n", pid, mem_file_name, mem_fd, buf);

    int ptrace_log = ptrace(PTRACE_ATTACH, pid, NULL, NULL);

    waitpid(pid, NULL, 0);
    int lseek_log = lseek(mem_fd, STACK_BEGIN, SEEK_SET);
    int rd = read(mem_fd, buf, STACK_SIZE);

    printf("in Memory at : %zd, %x (readed %d chari - ptrace ret = %d - lseek ret = %d)\n", STACK_BEGIN, buf, rd, ptrace_log, lseek_log);

    ptrace(PTRACE_DETACH, pid, NULL, NULL);
    close(mem_fd);
}

[me@localhost ~]$sudo ./analyse 2419
PID=2419
NAME=/proc/2419/mem
FD=3
Initial value of buf : da5030f0
in Memory at : 140736553521152, da5030f0 (readed 135168 chari - ptrace ret = 0 - lseek ret = -934834176)

1 个答案:

答案 0 :(得分:0)

read()不会更改buf的地址(怎么可能?),它会更改内容。如果要查看更改,则必须打印内容。打印*buf将显示缓冲区的第一个字符。

void main(int argc, char** argv)
{
    int pid = atoi(argv[1]);
    char mem_file_name[30];

    int mem_fd; 
    char buf[STACK_SIZE];

    sprintf(mem_file_name, "/proc/%d/mem", pid);
    mem_fd = open(mem_file_name, O_RDONLY);
    printf("PID=%d\n\
            NAME=%s\n\
            FD=%d\n\
            Initial value of buf : %c\n", pid, mem_file_name, mem_fd, *buf);

    int ptrace_log = ptrace(PTRACE_ATTACH, pid, NULL, NULL);

    waitpid(pid, NULL, 0);
    int lseek_log = lseek(mem_fd, STACK_BEGIN, SEEK_SET);
    int rd = read(mem_fd, buf, STACK_SIZE);

    printf("in Memory at : %zd, %c (readed %d chari - ptrace ret = %d - lseek ret = %d)\n", STACK_BEGIN, *buf, rd, ptrace_log, lseek_log);

    ptrace(PTRACE_DETACH, pid, NULL, NULL);
    close(mem_fd);
}