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