I have the following code tested on some Linux distros (Debian, Linux Mint...) and working, but under CentOS I get an error even I run it as 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()
The script reads the process' memory and dumps the readable region. Under CentOS 5.4 x64 I get the following error:
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
The process is alive and readable:
[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
any idea? I tried it under Python 2.4 and Python 2.7 works on Debian-like distros but not under CentOS.