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