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