public TargetMemoryArea[] GetMemoryMaps()
{
// We cannot use System.IO to read this file because it is not
// seekable. Actually, the file is seekable, but it contains
// "holes" and each line starts on a new 4096 bytes block.
// So if you just read the first line from the file, the current
// file position will be rounded up to the next 4096 bytes
// boundary - it'll be different from what System.IO thinks is
// the current file position and System.IO will try to "fix" this
// by seeking back.
string mapfile = String.Format ("/proc/{0}/maps", child_pid);
string contents = GetFileContents (mapfile);
if (contents == null)
return null;
ArrayList list = new ArrayList ();
using (StringReader reader = new StringReader (contents)) {
do {
string l = reader.ReadLine ();
if (l == null)
break;
bool is64bit;
if (l [8] == '-')
is64bit = false;
else if (l [16] == '-')
is64bit = true;
else
throw new InternalError ();
string sstart = is64bit ? l.Substring (0,16) : l.Substring (0,8);
string send = is64bit ? l.Substring (17,16) : l.Substring (9,8);
string sflags = is64bit ? l.Substring (34,4) : l.Substring (18,4);
long start = Int64.Parse (sstart, NumberStyles.HexNumber);
long end = Int64.Parse (send, NumberStyles.HexNumber);
string name;
if (is64bit)
name = (l.Length > 73) ? l.Substring (73) : "";
else
name = (l.Length > 49) ? l.Substring (49) : "";
name = name.TrimStart (' ').TrimEnd (' ');
if (name == "")
name = null;
TargetMemoryFlags flags = 0;
if (sflags [1] != 'w')
flags |= TargetMemoryFlags.ReadOnly;
TargetMemoryArea area = new TargetMemoryArea (
new TargetAddress (AddressDomain, start),
new TargetAddress (AddressDomain, end),
flags, name);
list.Add (area);
} while (true);
}
TargetMemoryArea[] maps = new TargetMemoryArea [list.Count];
list.CopyTo (maps, 0);
return maps;
}