xref: /xnu-12377.81.4/tools/lldbmacros/usertaskdebugging/target.py (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributionsimport binascii
2*043036a2SApple OSS Distributionsimport logging
3*043036a2SApple OSS Distributionsimport struct
4*043036a2SApple OSS Distributions
5*043036a2SApple OSS Distributions
6*043036a2SApple OSS Distributionsclass Process(object):
7*043036a2SApple OSS Distributions    """Base interface for process being debugged. Provides basic functions for gdbserver to interact.
8*043036a2SApple OSS Distributions       Create a class object for your backing system to provide functionality
9*043036a2SApple OSS Distributions
10*043036a2SApple OSS Distributions       Here is the list of must implement functions:
11*043036a2SApple OSS Distributions        + please update hinfo['ostype'] and hinfo['vendor'] if its not in (macosx, ios)
12*043036a2SApple OSS Distributions        + please populate threads_ids_list with ids of threads.
13*043036a2SApple OSS Distributions        - getThreadStopInfo
14*043036a2SApple OSS Distributions        - getProcessInfo
15*043036a2SApple OSS Distributions        - getRegisterDataForThread
16*043036a2SApple OSS Distributions        - getRegisterInfo
17*043036a2SApple OSS Distributions        - readMemory
18*043036a2SApple OSS Distributions    """
19*043036a2SApple OSS Distributions    def __init__(self, cputype, cpusubtype, ptrsize):
20*043036a2SApple OSS Distributions        super().__init__()
21*043036a2SApple OSS Distributions        self.hinfo = {
22*043036a2SApple OSS Distributions            'cputype': cputype, 'cpusubtype': cpusubtype,
23*043036a2SApple OSS Distributions            'triple': None, 'vendor': 'apple', 'ostype': 'macosx',
24*043036a2SApple OSS Distributions            'endian': 'little', 'ptrsize': ptrsize, 'hostname': None, 'os_build': None,
25*043036a2SApple OSS Distributions            'os_kernel': None, 'os_version': None, 'watchpoint_exceptions_received': None,
26*043036a2SApple OSS Distributions            'default_packet_timeout': '10', 'distribution_id': None
27*043036a2SApple OSS Distributions        }
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions        ## if cputype is arm assume its ios
30*043036a2SApple OSS Distributions        if (cputype & 0xc) != 0xc:
31*043036a2SApple OSS Distributions            self.hinfo['ostype'] = 'ios'
32*043036a2SApple OSS Distributions        self.ptrsize = ptrsize
33*043036a2SApple OSS Distributions        self.threads = {}
34*043036a2SApple OSS Distributions        self.threads_ids_list = []
35*043036a2SApple OSS Distributions
36*043036a2SApple OSS Distributions    def getHostInfo(self):
37*043036a2SApple OSS Distributions        retval = ''
38*043036a2SApple OSS Distributions        for i in list(self.hinfo.keys()):
39*043036a2SApple OSS Distributions            if self.hinfo[i] is None:
40*043036a2SApple OSS Distributions                continue
41*043036a2SApple OSS Distributions            retval += '%s:%s;' % (str(i), str(self.hinfo[i]))
42*043036a2SApple OSS Distributions        return retval
43*043036a2SApple OSS Distributions
44*043036a2SApple OSS Distributions    def getRegisterDataForThread(self, th_id, reg_num):
45*043036a2SApple OSS Distributions        logging.critical("Not Implemented: getRegisterDataForThread")
46*043036a2SApple OSS Distributions        return ''
47*043036a2SApple OSS Distributions
48*043036a2SApple OSS Distributions    def readMemory(self, address, size):
49*043036a2SApple OSS Distributions        logging.critical("readMemory: Not Implemented: readMemory")
50*043036a2SApple OSS Distributions        #E08 means read failed
51*043036a2SApple OSS Distributions        return 'E08'
52*043036a2SApple OSS Distributions
53*043036a2SApple OSS Distributions    def writeMemory(self, address, data, size):
54*043036a2SApple OSS Distributions        """ Unimplemented. address in ptr to save data to. data is native endian stream of bytes,
55*043036a2SApple OSS Distributions        """
56*043036a2SApple OSS Distributions        return 'E09'
57*043036a2SApple OSS Distributions
58*043036a2SApple OSS Distributions    def getRegisterInfo(regnum):
59*043036a2SApple OSS Distributions        #something similar to
60*043036a2SApple OSS Distributions        #"name:x1;bitsize:64;offset:8;encoding:uint;format:hex;gcc:1;dwarf:1;set:General Purpose Registers;"
61*043036a2SApple OSS Distributions        logging.critical("getRegisterInfo: Not Implemented: getRegisterInfo")
62*043036a2SApple OSS Distributions        return 'E45'
63*043036a2SApple OSS Distributions
64*043036a2SApple OSS Distributions    def getProcessInfo(self):
65*043036a2SApple OSS Distributions        logging.critical("Not Implemented: qProcessInfo")
66*043036a2SApple OSS Distributions        return ''
67*043036a2SApple OSS Distributions
68*043036a2SApple OSS Distributions    def getFirstThreadInfo(self):
69*043036a2SApple OSS Distributions        """ describe all thread ids in the process.
70*043036a2SApple OSS Distributions        """
71*043036a2SApple OSS Distributions        thinfo_str = self.getThreadsInfo()
72*043036a2SApple OSS Distributions        if not thinfo_str:
73*043036a2SApple OSS Distributions            logging.warning('getFirstThreadInfo: Process has no threads')
74*043036a2SApple OSS Distributions            return ''
75*043036a2SApple OSS Distributions        return 'm' + thinfo_str
76*043036a2SApple OSS Distributions
77*043036a2SApple OSS Distributions    def getSubsequestThreadInfo(self):
78*043036a2SApple OSS Distributions        """ return 'l' for last because all threads are listed in getFirstThreadInfo call.
79*043036a2SApple OSS Distributions        """
80*043036a2SApple OSS Distributions        return 'l'
81*043036a2SApple OSS Distributions
82*043036a2SApple OSS Distributions    def getSharedLibInfoAddress(self):
83*043036a2SApple OSS Distributions        """ return int data of a hint where shared library is loaded.
84*043036a2SApple OSS Distributions        """
85*043036a2SApple OSS Distributions        logging.critical("Not Implemented: qShlibInfoAddr")
86*043036a2SApple OSS Distributions        raise NotImplementedError('getSharedLibInfoAddress is not Implemented')
87*043036a2SApple OSS Distributions
88*043036a2SApple OSS Distributions    def getSignalInfo(self):
89*043036a2SApple OSS Distributions        # return the signal info in required format.
90*043036a2SApple OSS Distributions        return "T02" + "threads:" + self.getThreadsInfo() + ';'
91*043036a2SApple OSS Distributions
92*043036a2SApple OSS Distributions    def getThreadsInfo(self):
93*043036a2SApple OSS Distributions        """ returns ',' separeted values of thread ids """
94*043036a2SApple OSS Distributions        retval = ''
95*043036a2SApple OSS Distributions        first = True
96*043036a2SApple OSS Distributions        for tid in self.threads_ids_list:
97*043036a2SApple OSS Distributions            if first is True:
98*043036a2SApple OSS Distributions                first = False
99*043036a2SApple OSS Distributions                retval += self.encodeThreadID(tid)
100*043036a2SApple OSS Distributions            else:
101*043036a2SApple OSS Distributions                retval += ',%s' % self.encodeThreadID(tid)
102*043036a2SApple OSS Distributions        return retval
103*043036a2SApple OSS Distributions
104*043036a2SApple OSS Distributions    def getCurrentThreadID(self):
105*043036a2SApple OSS Distributions        """ returns int thread id of the first stopped thread
106*043036a2SApple OSS Distributions            if subclass supports thread switching etc then
107*043036a2SApple OSS Distributions            make sure to re-implement this funciton
108*043036a2SApple OSS Distributions        """
109*043036a2SApple OSS Distributions        if self.threads_ids_list:
110*043036a2SApple OSS Distributions            return self.threads_ids_list[0]
111*043036a2SApple OSS Distributions        return 0
112*043036a2SApple OSS Distributions
113*043036a2SApple OSS Distributions    def getThreadStopInfo(self, th_id):
114*043036a2SApple OSS Distributions        """ returns stop signal and some thread register info.
115*043036a2SApple OSS Distributions        """
116*043036a2SApple OSS Distributions        logging.critical("getThreadStopInfo: Not Implemented. returning basic info.")
117*043036a2SApple OSS Distributions
118*043036a2SApple OSS Distributions        return 'T02thread:%s' % self.encodeThreadID(th_id)
119*043036a2SApple OSS Distributions
120*043036a2SApple OSS Distributions    def encodeRegisterData(self, intdata, bytesize=None):
121*043036a2SApple OSS Distributions        """ return an encoded string for unsigned int intdata
122*043036a2SApple OSS Distributions            based on the bytesize and endianness value
123*043036a2SApple OSS Distributions        """
124*043036a2SApple OSS Distributions        if not bytesize:
125*043036a2SApple OSS Distributions            bytesize = self.ptrsize
126*043036a2SApple OSS Distributions
127*043036a2SApple OSS Distributions        format = '<I'
128*043036a2SApple OSS Distributions        if bytesize > 4:
129*043036a2SApple OSS Distributions            format = '<Q'
130*043036a2SApple OSS Distributions        packed_data = struct.pack(format, intdata)
131*043036a2SApple OSS Distributions        return binascii.hexlify(packed_data).decode()
132*043036a2SApple OSS Distributions
133*043036a2SApple OSS Distributions    def encodePointerRegisterData(self, ptrdata):
134*043036a2SApple OSS Distributions        """ encodes pointer data based on ptrsize defined for the target """
135*043036a2SApple OSS Distributions        return self.encodeRegisterData(ptrdata, bytesize=self.ptrsize)
136*043036a2SApple OSS Distributions
137*043036a2SApple OSS Distributions    def encodeThreadID(self, intdata):
138*043036a2SApple OSS Distributions        format = '>Q'
139*043036a2SApple OSS Distributions        return binascii.hexlify(struct.pack(format, intdata)).decode()
140*043036a2SApple OSS Distributions
141*043036a2SApple OSS Distributions    def encodeByteString(self, bytestr):
142*043036a2SApple OSS Distributions        return binascii.hexlify(bytestr).decode()
143