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