How can I read process memory in python on Linux?

I am trying to use python and python ptrace to read the memory of an external process. I need to fully work on python, and I was trying to read and print process memory on Linux.

So, for example, I tried the following code, which continues to give me I / O errors:

proc_mem = open("/proc/%i/mem" % process.pid, "r") print proc_mem.read() proc_mem.close() 

Basically, I just want to unload the process memory many times and look for changes over time. If this is the right way to do this, then what is my problem? OR is there a better way to do this?

+7
source share
2 answers

Call shell command from python module - subprocess

 import subprocess # ps -ux | grep 1842 (Assuming 1842 is the process id. replace with process id you get) p1 = subprocess.Popen(["ps", "-ux"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["grep", "1842"], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. output = p2.communicate()[0] print output 

and analyze the output to see its memory usage

+3
source

Basically, I just want to unload the process memory many times and look for changes over time. If this is the right way to do this, then what is my problem? OR is there a better way to do this?

You may be interested in gdb reverse debugging , which writes all changes to process memory. Here is a tutorial ( google cache ).

There is also Robert O'Callahan Chronicle / Chronomancer work if you want to play with raw recording tools.

+1
source

All Articles