#! /usr/bin/env python
import re
import sys
if len(sys.argv) != 2:
  print "LEN = = " + str(len(sys.argv))
  sys.exit(1)
mypid=sys.argv[1]
mypid=str(mypid)
sys.stderr.write("PID = " + str(mypid) )
maps_file = open("/proc/"+mypid+"/maps", 'r')
mem_file = open("/proc/"+mypid+"/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
    m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r][-w])', line)
    if m.group(3) == 'rw':  # if this is a writeable region
        sys.stderr.write("\nOK : \n" + line+"\n")
        start = int(m.group(1), 16)
        if start > 281474976710655 :
                continue
        end = int(m.group(2), 16)
        sys.stderr.write( "start = " + str(start) + "\n")
        mem_file.seek(start)  # seek to region start
        chunk = mem_file.read(end - start)  # read region contents
        print chunk,  # dump contents to standard output
    else :
        sys.stderr.write("\nPASS : \n" + line+"\n")
maps_file.close()
mem_file.close()