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