Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Linux读取进程内存-int太大,无法转换为C long_Python_Linux_Memory_Python 2.7 - Fatal编程技术网

Python Linux读取进程内存-int太大,无法转换为C long

Python Linux读取进程内存-int太大,无法转换为C long,python,linux,memory,python-2.7,Python,Linux,Memory,Python 2.7,我有一段代码,它在Linux中读取进程内存并搜索字符串,在某些发行版上可以正常工作,但在其他发行版上却出现了以下错误: maps_file = open("/proc/%s/maps"%pid, 'r') mem_file = open("/proc/%s/mem"%pid, 'r') for line in maps_file.readlines(): # for each mapped region m = re.match(r'([0-9A-Fa-f]+)', l

我有一段代码,它在Linux中读取进程内存并搜索字符串,在某些发行版上可以正常工作,但在其他发行版上却出现了以下错误:

  maps_file = open("/proc/%s/maps"%pid, 'r')
  mem_file = open("/proc/%s/mem"%pid, 'r')
  for line in maps_file.readlines():  # for each mapped region
      m = re.match(r'([0-9A-Fa-f]+)', 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
          mem_dump = open(working_dir+"/%s.bin"%pid, "ab")
          mem_dump.write(chunk,)
          mem_dump.close()
  maps_file.close()
  mem_file.close()
错误:

scan process: 491
Traceback (most recent call last):
  File "./dump.py", line 106, in <module>
    MainDump(pid)
  File "./dump.py", line 79, in MainDump
    mem_file.seek(start)  # seek to region start
OverflowError: Python int too large to convert to C long

我应该声明为浮动吗?有什么想法吗

也尝试了
long()
,结果和错误相同


编辑:我忘了说的是,我在“x64”系统上遇到的错误。

问题是您得到了地址
0xFFFFFFFF600000L
。(有符号)C long只能保存
-0x80000000000000
0x7fffffffffffff
之间的值。因此,这个地址确实“太大,无法转换为C长”

如果查看,您会发现问题很可能是由于某种原因,当在非工作发行版上配置Python时,它无法检测到存在
fseeko
off\u t
。但是,除非您想重建Python,否则这对您没有帮助

那么,你如何解决这个问题呢?有一些事情可以尝试


第一种可能是从终点而不是起点开始寻找

mem_len = os.fstat(mem_file.fileno()).st_size

if start >= 1<<63L:
    mem_file.seek(mem_len - start, os.SEEK_END)
else:
    mem_file.seek(start)
…或使用
continue
语句跳到循环的下一个迭代:

start = int(m.group(1), 16)
end = int(m.group(2), 16)
if start > sys.maxint:
    continue
mem_file.seek(start)  # seek to region start
chunk = mem_file.read(end - start)  # read region contents
# ...
如果您知道区域总是按顺序排序,则可以使用
break
而不是
continue
(因为其余区域也将超出范围)

但我认为最好的解决方案是尝试一下,然后处理错误。还有其他原因导致此
seek
read
可能会失败,例如,如果您正在查看的进程在到达某个区域之前取消映射,或者退出,而您宁愿跳过错误并继续,而不只是退出,对吗

因此:


您使用的是64位操作系统还是64位python版本?看起来起始地址超出了C实现所能处理的范围,但这表明它被编译为64位机器上的32位应用程序?发行版官方是否为x64编译。如果您不确定,您可以始终这样做:
import ctypes;打印(ctypes.sizeof(ctypes.c_long))
。如果说是
4
而不是
8
,那就是你的问题。我对“ctypes.c_long”得到了“8”好的,下一种可能性:可能值是>
1刚刚尝试了这两种方法,第一次我得到了相同的错误,对“可怕的黑客”我得到了“IOError:[Errno 0]Erro”,如果地址类似于
0xffffffffff600000L
,而不只是退出脚本,我怎么能跳到下一个进程?
mem_len = os.fstat(mem_file.fileno()).st_size

if start >= 1<<63L:
    mem_file.seek(mem_len - start, os.SEEK_END)
else:
    mem_file.seek(start)
if start >= 1<<63L:
    start -= 1<<64L
start = int(m.group(1), 16)
end = int(m.group(2), 16)
if start <= sys.maxint:
    mem_file.seek(start)  # seek to region start
    chunk = mem_file.read(end - start)  # read region contents
    # ...
start = int(m.group(1), 16)
end = int(m.group(2), 16)
if start > sys.maxint:
    continue
mem_file.seek(start)  # seek to region start
chunk = mem_file.read(end - start)  # read region contents
# ...
if m.group(3) == 'r':  # if this is a readable region
    start = int(m.group(1), 16)
    end = int(m.group(2), 16)
    try:
        mem_file.seek(start)  # seek to region start
        chunk = mem_file.read(end - start)  # read region co
    except Exception as e:
        print('Skipping region {:#018x} because of error {}'.format(start, e))
        continue
    mem_dump = open(working_dir+"/%s.bin"%pid, "ab")
    # ...