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