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