Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

I had the same problem with qemu, and I finally found a very bad solution (but still a solution): parsing the process memory.

This is working here because I know that qemu is storing the remote pts in a string with a specific format and allocated on the heap. May be it can work in other situations too with a few changes and by reusing the pid from the fuser output (check other answer).

The code is adapted from herehere.

#! /usr/bin/env python import sys pid = sys.argv[1] import re maps_file = open("/proc/" + pid + "/maps", 'r') mem_file = open("/proc/" + pid + "/mem", 'r', 0) for line in maps_file.readlines(): # You may want to remove the 'heap' part to search all RAM m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r]).*\[heap\]', line) if m and m.group(3) == 'r': start = int(m.group(1), 16) end = int(m.group(2), 16) mem_file.seek(start) chunk = mem_file.read(end - start) # You may want to adapt this one to reduce false matches idx = chunk.find("/dev/pts/") if idx != -1: end = chunk.find("\0", idx) print chunk[idx:end] maps_file.close() mem_file.close() 

I had the same problem with qemu, and I finally found a very bad solution (but still a solution): parsing the process memory.

This is working here because I know that qemu is storing the remote pts in a string with a specific format and allocated on the heap. May be it can work in other situations too with a few changes and by reusing the pid from the fuser output (check other answer).

The code is adapted from here.

#! /usr/bin/env python import sys pid = sys.argv[1] import re maps_file = open("/proc/" + pid + "/maps", 'r') mem_file = open("/proc/" + pid + "/mem", 'r', 0) for line in maps_file.readlines(): # You may want to remove the 'heap' part to search all RAM m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r]).*\[heap\]', line) if m and m.group(3) == 'r': start = int(m.group(1), 16) end = int(m.group(2), 16) mem_file.seek(start) chunk = mem_file.read(end - start) # You may want to adapt this one to reduce false matches idx = chunk.find("/dev/pts/") if idx != -1: end = chunk.find("\0", idx) print chunk[idx:end] maps_file.close() mem_file.close() 

I had the same problem with qemu, and I finally found a very bad solution (but still a solution): parsing the process memory.

This is working here because I know that qemu is storing the remote pts in a string with a specific format and allocated on the heap. May be it can work in other situations too with a few changes and by reusing the pid from the fuser output (check other answer).

The code is adapted from here.

#! /usr/bin/env python import sys pid = sys.argv[1] import re maps_file = open("/proc/" + pid + "/maps", 'r') mem_file = open("/proc/" + pid + "/mem", 'r', 0) for line in maps_file.readlines(): # You may want to remove the 'heap' part to search all RAM m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r]).*\[heap\]', line) if m and m.group(3) == 'r': start = int(m.group(1), 16) end = int(m.group(2), 16) mem_file.seek(start) chunk = mem_file.read(end - start) # You may want to adapt this one to reduce false matches idx = chunk.find("/dev/pts/") if idx != -1: end = chunk.find("\0", idx) print chunk[idx:end] maps_file.close() mem_file.close() 
Source Link
calandoa
  • 333
  • 3
  • 6

I had the same problem with qemu, and I finally found a very bad solution (but still a solution): parsing the process memory.

This is working here because I know that qemu is storing the remote pts in a string with a specific format and allocated on the heap. May be it can work in other situations too with a few changes and by reusing the pid from the fuser output (check other answer).

The code is adapted from here.

#! /usr/bin/env python import sys pid = sys.argv[1] import re maps_file = open("/proc/" + pid + "/maps", 'r') mem_file = open("/proc/" + pid + "/mem", 'r', 0) for line in maps_file.readlines(): # You may want to remove the 'heap' part to search all RAM m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r]).*\[heap\]', line) if m and m.group(3) == 'r': start = int(m.group(1), 16) end = int(m.group(2), 16) mem_file.seek(start) chunk = mem_file.read(end - start) # You may want to adapt this one to reduce false matches idx = chunk.find("/dev/pts/") if idx != -1: end = chunk.find("\0", idx) print chunk[idx:end] maps_file.close() mem_file.close()