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