读取进程中某块内存区域的内容

以下python 脚本可以读取一个进程的某个内存区域的内容:

#!/usr/bin/env python

import re
import sys

def dump_memory_region_by_pid(pid, region, of):
    print "PID = %d, find region ..." % pid
    with open("/proc/%d/maps" % pid, 'r') as maps_file:
        with open("/proc/%d/mem" % pid, 'r') as mem_file:
            for line in maps_file.readlines():  # for each mapped region
                m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-rw][-rw])(.*)\[(.*)\]', line)
                if m == None:
                    continue

                if m.group(5) == region:
                    print "find region " + m.group(5)

                    start = int(m.group(1), 16)
                    end = int(m.group(2), 16)
                    if start > 0xFFFFFFFFFFFF:
                        print "start too big: " + str(start) + ", skip"
                        continue
                    print "start = " + str(start) + ", end = " + str(end)

                    mem_file.seek(start)  # seek to region start
                    chunk = mem_file.read(end - start)  # read region contents

                    with open(of, 'w') as of_file:
                        of_file.write(chunk)
                    print "dumped " + region + " to file " + of

                    break
                else:
                    print "skip region " + m.group(5)

if __name__ == '__main__':
    try:
        assert len(sys.argv) == 2, "Provide exactly 1 PID (process ID)"
        pid = int(sys.argv[1])
        dump_memory_region_by_pid(pid, "vdso", "vdso.dd")

    except (AssertionError, ValueError) as e:
        print "Please provide 1 PID as a commandline argument."
        print "You entered: %s" % ' '.join(sys.argv)
        raise e

其实通过以下命令也可以手动来dump,但我在使用的过程中遇到dd报skip的错。

1)查看内存区域的起始位置和长度,从下面的输出可以知道xxx区域的起始位置为0xffffffc000,长度是0x4000

$ cat /proc/xxx/maps |grep xxxx
ffffffc000-10000000000 r-xp 00000000 00:00 0 [xxxx]

2)使用dd读取内存区域的内容,其中的skip就是该内存区域的起始位置,count为长度,都是10进制

$ dd if=/proc/xxx/mem of=xxx.dd bs=1 skip=1099511611392 count=16384

 

 

参考资料

1 https://stackoverflow.com/questions/12977179/reading-living-process-memory-without-interrupting-it

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在子进程读取共享内存,需要先使用`shmat()`函数将共享内存区域映射到子进程的地址空间,然后再进行读取操作。 具体步骤如下: 1. 使用`shmget()`函数获取共享内存标识符。 2. 使用`shmat()`函数将共享内存区域映射到子进程的地址空间,并返回指向共享内存区域的指针。 3. 读取共享内存区域的数据。 4. 使用`shmdt()`函数将共享内存区域从子进程的地址空间解除映射。 下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> #include <sys/shm.h> #define SHM_SIZE 100 int main() { int shmid; char *shm_ptr; // 获取共享内存标识符 if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } // 映射共享内存区域到子进程的地址空间 if ((shm_ptr = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } // 读取共享内存区域的数据 printf("Shared memory content: %s\n", shm_ptr); // 解除共享内存区域的映射 if (shmdt(shm_ptr) == -1) { perror("shmdt"); exit(1); } // 删除共享内存区域 if (shmctl(shmid, IPC_RMID, NULL) == -1) { perror("shmctl"); exit(1); } return 0; } ``` 在示例代码,我们首先使用`shmget()`函数获取了一个共享内存标识符,然后使用`shmat()`函数将共享内存区域映射到子进程的地址空间,接着读取了共享内存区域的数据,最后使用`shmdt()`函数将共享内存区域从子进程的地址空间解除映射,以及使用`shmctl()`函数删除共享内存区域
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值