python读取进程内存_Python读取Linux内存进程错误(/ proc / $pid / mem...

我已经在某些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下却没有.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值