【发布时间】:2014-02-24 09:05:26
【问题描述】:
我在一些 Linux 发行版(Debian、Linux Mint...)上测试了以下代码并且可以正常工作,但在 CentOS 下,即使我以 root 身份运行它也会出现错误:
#!/usr/bin/env python
import re
maps_file = open("/proc/18396/maps", 'r')
mem_file = open("/proc/18396/mem", 'r', 0)
for line in maps_file.readlines(): # for each mapped region
m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])', line)
if m.group(3) == 'r': # if this is a readable region
start = int(m.group(1), 16)
end = int(m.group(2), 16)
mem_file.seek(start) # seek to region start
chunk = mem_file.read(end - start) # read region contents
print chunk, # dump contents to standard output
maps_file.close()
mem_file.close()
脚本读取进程的内存并转储可读区域。在 CentOS 5.4 x64 下出现以下错误:
Traceback (most recent call last):
File "./mem.py", line 11, in ?
chunk = mem_file.read(end - start) # read region contents
IOError: [Errno 3] No such process
进程是活的并且可读的:
[root@localhost ~]# ps xa|grep 18396
18396 ? S 0:00 /usr/sbin/httpd
[root@localhost ~]# ls -al /proc/18396/maps && ls -al /proc/18396/mem
-r--r--r-- 1 root root 0 Jan 31 17:26 /proc/18396/maps
-rw------- 1 root root 0 Jan 31 17:26 /proc/18396/mem
有什么想法吗?我在 Python 2.4 下尝试过,Python 2.7 可以在类似 Debian 的发行版上运行,但不能在 CentOS 下运行。
【问题讨论】:
-
编辑:看来我需要 PTRACE 它。
标签: linux python-2.7 memory process