1*1031c584SApple OSS Distributions""" Python I/O subsystem backed by LLDB. """ 2*1031c584SApple OSS Distributions 3*1031c584SApple OSS Distributionsfrom __future__ import absolute_import, division, print_function 4*1031c584SApple OSS Distributions 5*1031c584SApple OSS Distributionsfrom builtins import bytes 6*1031c584SApple OSS Distributions 7*1031c584SApple OSS Distributionsimport io 8*1031c584SApple OSS Distributionsimport lldb 9*1031c584SApple OSS Distributions 10*1031c584SApple OSS Distributions 11*1031c584SApple OSS Distributionsclass SBProcessRawIO(io.RawIOBase): 12*1031c584SApple OSS Distributions """ RAW I/O implementation backed by a process memory. """ 13*1031c584SApple OSS Distributions 14*1031c584SApple OSS Distributions def __init__(self, sbprocess, address, size): 15*1031c584SApple OSS Distributions """ Create new SBProcess I/O. 16*1031c584SApple OSS Distributions 17*1031c584SApple OSS Distributions sbproces: SBProcess instance to read data from. 18*1031c584SApple OSS Distributions address: Starting memory address in process' VA. 19*1031c584SApple OSS Distributions size: Size of the memory range. 20*1031c584SApple OSS Distributions """ 21*1031c584SApple OSS Distributions super(SBProcessRawIO, self).__init__() 22*1031c584SApple OSS Distributions 23*1031c584SApple OSS Distributions self._sbprocess = sbprocess 24*1031c584SApple OSS Distributions self._start = address 25*1031c584SApple OSS Distributions self._offset = 0 26*1031c584SApple OSS Distributions self._end = address + size 27*1031c584SApple OSS Distributions 28*1031c584SApple OSS Distributions # Base I/O methods 29*1031c584SApple OSS Distributions 30*1031c584SApple OSS Distributions def readable(self): 31*1031c584SApple OSS Distributions return True 32*1031c584SApple OSS Distributions 33*1031c584SApple OSS Distributions def writable(self): 34*1031c584SApple OSS Distributions # This is a lie that allows using BufferedRandom on top of this I/O. 35*1031c584SApple OSS Distributions return True 36*1031c584SApple OSS Distributions 37*1031c584SApple OSS Distributions def seekable(self): 38*1031c584SApple OSS Distributions return True 39*1031c584SApple OSS Distributions 40*1031c584SApple OSS Distributions # Raw I/O methods 41*1031c584SApple OSS Distributions 42*1031c584SApple OSS Distributions def tell(self): 43*1031c584SApple OSS Distributions return self._offset 44*1031c584SApple OSS Distributions 45*1031c584SApple OSS Distributions def seek(self, offset, whence=0): 46*1031c584SApple OSS Distributions seekto = offset 47*1031c584SApple OSS Distributions if whence == 0: 48*1031c584SApple OSS Distributions seekto += 0 49*1031c584SApple OSS Distributions elif whence == 1: 50*1031c584SApple OSS Distributions seekto += self.tell() 51*1031c584SApple OSS Distributions elif whence == 2: 52*1031c584SApple OSS Distributions seekto += self._end - self._start 53*1031c584SApple OSS Distributions else: 54*1031c584SApple OSS Distributions raise IOError("Invalid whence argument to seek: %r" % (whence,)) 55*1031c584SApple OSS Distributions 56*1031c584SApple OSS Distributions self._offset = seekto 57*1031c584SApple OSS Distributions return seekto 58*1031c584SApple OSS Distributions 59*1031c584SApple OSS Distributions def read(self, size=-1): 60*1031c584SApple OSS Distributions if size < 0: 61*1031c584SApple OSS Distributions return self.readall() 62*1031c584SApple OSS Distributions 63*1031c584SApple OSS Distributions # Do not read past the end of the data range. 64*1031c584SApple OSS Distributions read_size = min(size, self._end - (self._start + self._offset)) 65*1031c584SApple OSS Distributions 66*1031c584SApple OSS Distributions err = lldb.SBError() 67*1031c584SApple OSS Distributions data = self._sbprocess.ReadMemory(self._start + self._offset, read_size, err) 68*1031c584SApple OSS Distributions 69*1031c584SApple OSS Distributions # EOF on failure 70*1031c584SApple OSS Distributions if not err.Success(): 71*1031c584SApple OSS Distributions return bytes() 72*1031c584SApple OSS Distributions 73*1031c584SApple OSS Distributions self._offset += len(data) 74*1031c584SApple OSS Distributions return bytes(data) 75*1031c584SApple OSS Distributions 76*1031c584SApple OSS Distributions def readall(self): 77*1031c584SApple OSS Distributions err = lldb.SBError() 78*1031c584SApple OSS Distributions data = self._sbprocess.ReadMemory(self._start, self._end - self._start, err) 79*1031c584SApple OSS Distributions 80*1031c584SApple OSS Distributions if not err.Success(): 81*1031c584SApple OSS Distributions return bytes() 82*1031c584SApple OSS Distributions 83*1031c584SApple OSS Distributions return bytes(data) 84*1031c584SApple OSS Distributions 85*1031c584SApple OSS Distributions def readinto(self, bytes): 86*1031c584SApple OSS Distributions """ Reads data into existing object. """ 87*1031c584SApple OSS Distributions data = self.read(len(bytes)) 88*1031c584SApple OSS Distributions if data: 89*1031c584SApple OSS Distributions bytes[:len(data)] = data 90*1031c584SApple OSS Distributions return len(data) 91*1031c584SApple OSS Distributions 92*1031c584SApple OSS Distributions def readlines(self, hint=-1): 93*1031c584SApple OSS Distributions raise NotImplementedError("Can't read lines yet.") 94*1031c584SApple OSS Distributions 95*1031c584SApple OSS Distributions def write(self, bytes): 96*1031c584SApple OSS Distributions raise NotImplementedError("Can't write through LLDB yet.") 97