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