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