1*1031c584SApple OSS Distributions#!/usr/bin/python 2*1031c584SApple OSS Distributions# 3*1031c584SApple OSS Distributions 4*1031c584SApple OSS Distributions#source of register info is from http://opensource.apple.com/source/gdb/gdb-962/src/gdb/arm-tdep.c 5*1031c584SApple OSS Distributionsfrom __future__ import absolute_import, print_function 6*1031c584SApple OSS Distributions 7*1031c584SApple OSS Distributionsfrom builtins import hex 8*1031c584SApple OSS Distributionsfrom builtins import range 9*1031c584SApple OSS Distributionsfrom builtins import object 10*1031c584SApple OSS Distributions 11*1031c584SApple OSS Distributionsimport struct 12*1031c584SApple OSS Distributionsimport lldb 13*1031c584SApple OSS Distributions 14*1031c584SApple OSS Distributions 15*1031c584SApple OSS Distributionsosplugin_target_obj = None 16*1031c584SApple OSS Distributions 17*1031c584SApple OSS Distributionsclass PluginValue(lldb.SBValue): 18*1031c584SApple OSS Distributions def GetChildMemberWithName(val, name): 19*1031c584SApple OSS Distributions val_type = val.GetType() 20*1031c584SApple OSS Distributions if val_type.IsPointerType() == True: 21*1031c584SApple OSS Distributions val_type = val_type.GetPointeeType() 22*1031c584SApple OSS Distributions for i in range(val_type.GetNumberOfFields()): 23*1031c584SApple OSS Distributions if name == val_type.GetFieldAtIndex(i).GetName(): 24*1031c584SApple OSS Distributions return PluginValue(val.GetChildAtIndex(i)) 25*1031c584SApple OSS Distributions return None 26*1031c584SApple OSS Distributions 27*1031c584SApple OSS Distributionsclass Armv8_RegisterSet(object): 28*1031c584SApple OSS Distributions """ register info set for armv8 64 bit architecture""" 29*1031c584SApple OSS Distributions register_info = { 'sets' : ['GPR'], 30*1031c584SApple OSS Distributions 'registers': [ 31*1031c584SApple OSS Distributions {'name': 'x0' , 'bitsize':64, 'offset': 0, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 0, 'dwarf': 0, 'alt-name':'arg1', 'generic':'arg1'}, 32*1031c584SApple OSS Distributions {'name': 'x1' , 'bitsize':64, 'offset': 8, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 1, 'dwarf': 1, 'alt-name':'arg2', 'generic':'arg2'}, 33*1031c584SApple OSS Distributions {'name': 'x2' , 'bitsize':64, 'offset': 16, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 2, 'dwarf': 2, 'alt-name':'arg3', 'generic':'arg3'}, 34*1031c584SApple OSS Distributions {'name': 'x3' , 'bitsize':64, 'offset': 24, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 3, 'dwarf': 3, 'alt-name':'arg4', 'generic':'arg4'}, 35*1031c584SApple OSS Distributions {'name': 'x4' , 'bitsize':64, 'offset': 32, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 4, 'dwarf': 4, 'alt-name':'arg5', 'generic':'arg5'}, 36*1031c584SApple OSS Distributions {'name': 'x5' , 'bitsize':64, 'offset': 40, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 5, 'dwarf': 5, 'alt-name':'arg6', 'generic':'arg6'}, 37*1031c584SApple OSS Distributions {'name': 'x6' , 'bitsize':64, 'offset': 48, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 6, 'dwarf': 6, 'alt-name':'arg7', 'generic':'arg7'}, 38*1031c584SApple OSS Distributions {'name': 'x7' , 'bitsize':64, 'offset': 56, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 7, 'dwarf': 7, 'alt-name':'arg8', 'generic':'arg8'}, 39*1031c584SApple OSS Distributions {'name': 'x8' , 'bitsize':64, 'offset': 64, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 8, 'dwarf': 8}, 40*1031c584SApple OSS Distributions {'name': 'x9' , 'bitsize':64, 'offset': 72, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 9, 'dwarf': 9}, 41*1031c584SApple OSS Distributions {'name': 'x10' , 'bitsize':64, 'offset': 80, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':10, 'dwarf':10}, 42*1031c584SApple OSS Distributions {'name': 'x11' , 'bitsize':64, 'offset': 88, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':11, 'dwarf':11}, 43*1031c584SApple OSS Distributions {'name': 'x12' , 'bitsize':64, 'offset': 96, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':12, 'dwarf':12}, 44*1031c584SApple OSS Distributions {'name': 'x13' , 'bitsize':64, 'offset':104, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':13, 'dwarf':13}, 45*1031c584SApple OSS Distributions {'name': 'x14' , 'bitsize':64, 'offset':112, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':14, 'dwarf':14}, 46*1031c584SApple OSS Distributions {'name': 'x15' , 'bitsize':64, 'offset':120, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':15, 'dwarf':15}, 47*1031c584SApple OSS Distributions {'name': 'x16' , 'bitsize':64, 'offset':128, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':16, 'dwarf':16}, 48*1031c584SApple OSS Distributions {'name': 'x17' , 'bitsize':64, 'offset':136, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':17, 'dwarf':17}, 49*1031c584SApple OSS Distributions {'name': 'x18' , 'bitsize':64, 'offset':144, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':18, 'dwarf':18}, 50*1031c584SApple OSS Distributions {'name': 'x19' , 'bitsize':64, 'offset':152, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':19, 'dwarf':19}, 51*1031c584SApple OSS Distributions {'name': 'x20' , 'bitsize':64, 'offset':160, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':20, 'dwarf':20}, 52*1031c584SApple OSS Distributions {'name': 'x21' , 'bitsize':64, 'offset':168, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':21, 'dwarf':21}, 53*1031c584SApple OSS Distributions {'name': 'x22' , 'bitsize':64, 'offset':176, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':22, 'dwarf':22}, 54*1031c584SApple OSS Distributions {'name': 'x23' , 'bitsize':64, 'offset':184, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':23, 'dwarf':23}, 55*1031c584SApple OSS Distributions {'name': 'x24' , 'bitsize':64, 'offset':192, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':24, 'dwarf':24}, 56*1031c584SApple OSS Distributions {'name': 'x25' , 'bitsize':64, 'offset':200, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':25, 'dwarf':25}, 57*1031c584SApple OSS Distributions {'name': 'x26' , 'bitsize':64, 'offset':208, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':26, 'dwarf':26}, 58*1031c584SApple OSS Distributions {'name': 'x27' , 'bitsize':64, 'offset':216, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':27, 'dwarf':27}, 59*1031c584SApple OSS Distributions {'name': 'x28' , 'bitsize':64, 'offset':224, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':28, 'dwarf':28}, 60*1031c584SApple OSS Distributions {'name': 'fp' , 'bitsize':64, 'offset':232, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':29, 'dwarf':29, 'alt-name': 'fp', 'generic':'fp'}, 61*1031c584SApple OSS Distributions {'name': 'lr' , 'bitsize':64, 'offset':240, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':30, 'dwarf':30, 'alt-name': 'lr', 'generic':'lr'}, 62*1031c584SApple OSS Distributions {'name': 'sp' , 'bitsize':64, 'offset':248, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':31, 'dwarf':31, 'alt-name': 'sp', 'generic':'sp'}, 63*1031c584SApple OSS Distributions {'name': 'pc' , 'bitsize':64, 'offset':256, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':32, 'dwarf':32, 'alt-name': 'pc', 'generic':'pc'}, 64*1031c584SApple OSS Distributions {'name': 'far' , 'bitsize':64, 'offset':264, 'encoding':'uint', 'format':'hex', 'set':0}, 65*1031c584SApple OSS Distributions {'name': 'cpsr', 'bitsize':32, 'offset':272, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':33, 'dwarf':33, 'generic':'flags'}, 66*1031c584SApple OSS Distributions {'name': 'esr' , 'bitsize':32, 'offset':276, 'encoding':'uint', 'format':'hex', 'set':0}, 67*1031c584SApple OSS Distributions ] 68*1031c584SApple OSS Distributions } 69*1031c584SApple OSS Distributions 70*1031c584SApple OSS Distributions def __init__(self): 71*1031c584SApple OSS Distributions self.switch_context_address = osplugin_target_obj.FindSymbols('Switch_context')[0].GetSymbol().GetStartAddress().GetLoadAddress(osplugin_target_obj) 72*1031c584SApple OSS Distributions self.ResetRegisterValues() 73*1031c584SApple OSS Distributions def ResetRegisterValues(self): 74*1031c584SApple OSS Distributions self.x0 = 0 75*1031c584SApple OSS Distributions self.x1 = 0 76*1031c584SApple OSS Distributions self.x2 = 0 77*1031c584SApple OSS Distributions self.x3 = 0 78*1031c584SApple OSS Distributions self.x4 = 0 79*1031c584SApple OSS Distributions self.x5 = 0 80*1031c584SApple OSS Distributions self.x6 = 0 81*1031c584SApple OSS Distributions self.x7 = 0 82*1031c584SApple OSS Distributions self.x8 = 0 83*1031c584SApple OSS Distributions self.x9 = 0 84*1031c584SApple OSS Distributions self.x10 = 0 85*1031c584SApple OSS Distributions self.x11 = 0 86*1031c584SApple OSS Distributions self.x12 = 0 87*1031c584SApple OSS Distributions self.x13 = 0 88*1031c584SApple OSS Distributions self.x14 = 0 89*1031c584SApple OSS Distributions self.x15 = 0 90*1031c584SApple OSS Distributions self.x16 = 0 91*1031c584SApple OSS Distributions self.x17 = 0 92*1031c584SApple OSS Distributions self.x18 = 0 93*1031c584SApple OSS Distributions self.x19 = 0 94*1031c584SApple OSS Distributions self.x20 = 0 95*1031c584SApple OSS Distributions self.x21 = 0 96*1031c584SApple OSS Distributions self.x22 = 0 97*1031c584SApple OSS Distributions self.x23 = 0 98*1031c584SApple OSS Distributions self.x24 = 0 99*1031c584SApple OSS Distributions self.x25 = 0 100*1031c584SApple OSS Distributions self.x26 = 0 101*1031c584SApple OSS Distributions self.x27 = 0 102*1031c584SApple OSS Distributions self.x28 = 0 103*1031c584SApple OSS Distributions self.fp = 0 104*1031c584SApple OSS Distributions self.lr = 0 105*1031c584SApple OSS Distributions self.sp = 0 106*1031c584SApple OSS Distributions self.pc = 0 107*1031c584SApple OSS Distributions self.far = 0 108*1031c584SApple OSS Distributions self.cpsr = 0 109*1031c584SApple OSS Distributions self.esr = 0 110*1031c584SApple OSS Distributions 111*1031c584SApple OSS Distributions def __str__(self): 112*1031c584SApple OSS Distributions return """ pc = """ 113*1031c584SApple OSS Distributions 114*1031c584SApple OSS Distributions def GetPackedRegisterState(self): 115*1031c584SApple OSS Distributions return struct.pack('34QII', self.x0, self.x1, self.x2, self.x3, self.x4, self.x5, 116*1031c584SApple OSS Distributions self.x6, self.x7, self.x8, self.x9, self.x10, self.x11, self.x12, self.x13, 117*1031c584SApple OSS Distributions self.x14, self.x15, self.x16, self.x17, self.x18, self.x19, self.x20, self.x21, 118*1031c584SApple OSS Distributions self.x22, self.x23, self.x24, self.x25, self.x26, self.x27, self.x28, self.fp, 119*1031c584SApple OSS Distributions self.lr, self.sp, self.pc, self.far, self.cpsr, self.esr) 120*1031c584SApple OSS Distributions 121*1031c584SApple OSS Distributions def ReadRegisterDataFromKDPSavedState(self, kdp_state, kernel_version): 122*1031c584SApple OSS Distributions """ Setup register values from KDP saved information. 123*1031c584SApple OSS Distributions """ 124*1031c584SApple OSS Distributions saved_state = kernel_version.CreateValueFromExpression(None, '(struct arm_saved_state64 *) ' + str(kdp_state.GetValueAsUnsigned())) 125*1031c584SApple OSS Distributions saved_state = saved_state.Dereference() 126*1031c584SApple OSS Distributions saved_state = PluginValue(saved_state) 127*1031c584SApple OSS Distributions self.ResetRegisterValues() 128*1031c584SApple OSS Distributions self.x0 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(0).GetValueAsUnsigned() 129*1031c584SApple OSS Distributions self.x1 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(1).GetValueAsUnsigned() 130*1031c584SApple OSS Distributions self.x2 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(2).GetValueAsUnsigned() 131*1031c584SApple OSS Distributions self.x3 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(3).GetValueAsUnsigned() 132*1031c584SApple OSS Distributions self.x4 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(4).GetValueAsUnsigned() 133*1031c584SApple OSS Distributions self.x5 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(5).GetValueAsUnsigned() 134*1031c584SApple OSS Distributions self.x6 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(6).GetValueAsUnsigned() 135*1031c584SApple OSS Distributions self.x7 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(7).GetValueAsUnsigned() 136*1031c584SApple OSS Distributions self.x8 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(8).GetValueAsUnsigned() 137*1031c584SApple OSS Distributions self.x9 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(9).GetValueAsUnsigned() 138*1031c584SApple OSS Distributions self.x10 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(10).GetValueAsUnsigned() 139*1031c584SApple OSS Distributions self.x11 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(11).GetValueAsUnsigned() 140*1031c584SApple OSS Distributions self.x12 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(12).GetValueAsUnsigned() 141*1031c584SApple OSS Distributions self.x13 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(13).GetValueAsUnsigned() 142*1031c584SApple OSS Distributions self.x14 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(14).GetValueAsUnsigned() 143*1031c584SApple OSS Distributions self.x15 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(15).GetValueAsUnsigned() 144*1031c584SApple OSS Distributions self.x16 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(16).GetValueAsUnsigned() 145*1031c584SApple OSS Distributions self.x17 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(17).GetValueAsUnsigned() 146*1031c584SApple OSS Distributions self.x18 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(18).GetValueAsUnsigned() 147*1031c584SApple OSS Distributions self.x19 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(19).GetValueAsUnsigned() 148*1031c584SApple OSS Distributions self.x20 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(20).GetValueAsUnsigned() 149*1031c584SApple OSS Distributions self.x21 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(21).GetValueAsUnsigned() 150*1031c584SApple OSS Distributions self.x22 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(22).GetValueAsUnsigned() 151*1031c584SApple OSS Distributions self.x23 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(23).GetValueAsUnsigned() 152*1031c584SApple OSS Distributions self.x24 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(24).GetValueAsUnsigned() 153*1031c584SApple OSS Distributions self.x25 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(25).GetValueAsUnsigned() 154*1031c584SApple OSS Distributions self.x26 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(26).GetValueAsUnsigned() 155*1031c584SApple OSS Distributions self.x27 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(27).GetValueAsUnsigned() 156*1031c584SApple OSS Distributions self.x28 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(28).GetValueAsUnsigned() 157*1031c584SApple OSS Distributions self.fp = saved_state.GetChildMemberWithName('fp').GetValueAsUnsigned() 158*1031c584SApple OSS Distributions self.lr = saved_state.GetChildMemberWithName('lr').GetValueAsUnsigned() 159*1031c584SApple OSS Distributions self.sp = saved_state.GetChildMemberWithName('sp').GetValueAsUnsigned() 160*1031c584SApple OSS Distributions self.pc = saved_state.GetChildMemberWithName('pc').GetValueAsUnsigned() 161*1031c584SApple OSS Distributions self.far = saved_state.GetChildMemberWithName('far').GetValueAsUnsigned() 162*1031c584SApple OSS Distributions self.cpsr = saved_state.GetChildMemberWithName('cpsr').GetValueAsUnsigned() 163*1031c584SApple OSS Distributions self.esr = saved_state.GetChildMemberWithName('esr').GetValueAsUnsigned() 164*1031c584SApple OSS Distributions return self 165*1031c584SApple OSS Distributions 166*1031c584SApple OSS Distributions def ReadRegisterDataFromKernelStack(self, kstack_saved_state_addr, kernel_version): 167*1031c584SApple OSS Distributions saved_state = kernel_version.CreateValueFromExpression(None, '(arm_kernel_saved_state_t *) '+ str(kstack_saved_state_addr)) 168*1031c584SApple OSS Distributions saved_state = saved_state.Dereference() 169*1031c584SApple OSS Distributions saved_state = PluginValue(saved_state) 170*1031c584SApple OSS Distributions self.ResetRegisterValues() 171*1031c584SApple OSS Distributions self.x19 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(0).GetValueAsUnsigned() 172*1031c584SApple OSS Distributions self.x20 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(1).GetValueAsUnsigned() 173*1031c584SApple OSS Distributions self.x21 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(2).GetValueAsUnsigned() 174*1031c584SApple OSS Distributions self.x22 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(3).GetValueAsUnsigned() 175*1031c584SApple OSS Distributions self.x23 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(4).GetValueAsUnsigned() 176*1031c584SApple OSS Distributions self.x24 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(5).GetValueAsUnsigned() 177*1031c584SApple OSS Distributions self.x25 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(6).GetValueAsUnsigned() 178*1031c584SApple OSS Distributions self.x26 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(7).GetValueAsUnsigned() 179*1031c584SApple OSS Distributions self.x27 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(8).GetValueAsUnsigned() 180*1031c584SApple OSS Distributions self.x28 = saved_state.GetChildMemberWithName('x').GetChildAtIndex(9).GetValueAsUnsigned() 181*1031c584SApple OSS Distributions self.fp = saved_state.GetChildMemberWithName('fp').GetValueAsUnsigned() 182*1031c584SApple OSS Distributions self.lr = saved_state.GetChildMemberWithName('lr').GetValueAsUnsigned() 183*1031c584SApple OSS Distributions self.sp = saved_state.GetChildMemberWithName('sp').GetValueAsUnsigned() 184*1031c584SApple OSS Distributions # pc for a blocked thread is treated to be the next instruction it would run after thread switch. 185*1031c584SApple OSS Distributions self.pc = self.switch_context_address 186*1031c584SApple OSS Distributions return self 187*1031c584SApple OSS Distributions 188*1031c584SApple OSS Distributions def ReadRegisterDataFromContinuation(self, continuation_ptr): 189*1031c584SApple OSS Distributions self.ResetRegisterValues() 190*1031c584SApple OSS Distributions self.pc = continuation_ptr 191*1031c584SApple OSS Distributions return self 192*1031c584SApple OSS Distributions 193*1031c584SApple OSS Distributions @classmethod 194*1031c584SApple OSS Distributions def GetRegisterInfo(cls, regnum): 195*1031c584SApple OSS Distributions if regnum < 0 or regnum > len(cls.register_info['registers']): 196*1031c584SApple OSS Distributions return '' 197*1031c584SApple OSS Distributions 198*1031c584SApple OSS Distributions reginfo = cls.register_info['registers'][regnum] 199*1031c584SApple OSS Distributions retval = '' 200*1031c584SApple OSS Distributions for i in list(reginfo.keys()): 201*1031c584SApple OSS Distributions v_str = str(reginfo[i]) 202*1031c584SApple OSS Distributions if i == 'set': 203*1031c584SApple OSS Distributions v_str = 'General Purpose Registers' 204*1031c584SApple OSS Distributions retval += "%s:%s;" % (str(i), v_str) 205*1031c584SApple OSS Distributions return retval 206*1031c584SApple OSS Distributions 207*1031c584SApple OSS Distributions 208*1031c584SApple OSS Distributions 209*1031c584SApple OSS Distributionsclass Armv7_RegisterSet(object): 210*1031c584SApple OSS Distributions """ register info set for armv7 32 bit architecture """ 211*1031c584SApple OSS Distributions register_info = { 'sets' : ['GPR'], 212*1031c584SApple OSS Distributions 'registers': [ 213*1031c584SApple OSS Distributions { 'name':'r0' , 'bitsize' : 32, 'offset' : 0, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 0, 'dwarf' : 0}, 214*1031c584SApple OSS Distributions { 'name':'r1' , 'bitsize' : 32, 'offset' : 4, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 1, 'dwarf' : 1}, 215*1031c584SApple OSS Distributions { 'name':'r2' , 'bitsize' : 32, 'offset' : 8, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 2, 'dwarf' : 2}, 216*1031c584SApple OSS Distributions { 'name':'r3' , 'bitsize' : 32, 'offset' : 12, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 3, 'dwarf' : 3}, 217*1031c584SApple OSS Distributions { 'name':'r4' , 'bitsize' : 32, 'offset' : 16, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 4, 'dwarf' : 4}, 218*1031c584SApple OSS Distributions { 'name':'r5' , 'bitsize' : 32, 'offset' : 20, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 5, 'dwarf' : 5}, 219*1031c584SApple OSS Distributions { 'name':'r6' , 'bitsize' : 32, 'offset' : 24, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 6, 'dwarf' : 6}, 220*1031c584SApple OSS Distributions { 'name':'r7' , 'bitsize' : 32, 'offset' : 28, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 7, 'dwarf' : 7}, 221*1031c584SApple OSS Distributions { 'name':'r8' , 'bitsize' : 32, 'offset' : 32, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 8, 'dwarf' : 8}, 222*1031c584SApple OSS Distributions { 'name':'r9' , 'bitsize' : 32, 'offset' : 36, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc': 9, 'dwarf' : 9}, 223*1031c584SApple OSS Distributions { 'name':'r10' , 'bitsize' : 32, 'offset' : 40, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':10, 'dwarf' :10}, 224*1031c584SApple OSS Distributions { 'name':'r11' , 'bitsize' : 32, 'offset' : 44, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':11, 'dwarf' :11, 'alt-name': 'fp', 'generic': 'fp'}, 225*1031c584SApple OSS Distributions { 'name':'r12' , 'bitsize' : 32, 'offset' : 48, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':12, 'dwarf' :12}, 226*1031c584SApple OSS Distributions { 'name':'sp' , 'bitsize' : 32, 'offset' : 52, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':13, 'dwarf' :13, 'generic': 'sp'}, 227*1031c584SApple OSS Distributions { 'name':'lr' , 'bitsize' : 32, 'offset' : 56, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':14, 'dwarf' :14, 'generic': 'lr'}, 228*1031c584SApple OSS Distributions { 'name':'pc' , 'bitsize' : 32, 'offset' : 60, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':15, 'dwarf' :15, 'generic': 'pc'}, 229*1031c584SApple OSS Distributions { 'name':'cpsr' , 'bitsize' : 32, 'offset' : 64, 'encoding':'uint', 'format':'hex', 'set':0, 'gcc':16, 'dwarf' :16, 'generic':'flags'}, 230*1031c584SApple OSS Distributions { 'name':'fsr' , 'bitsize' : 32, 'offset' : 68, 'encoding':'uint', 'format':'hex', 'set':0}, 231*1031c584SApple OSS Distributions { 'name':'far' , 'bitsize' : 32, 'offset' : 72, 'encoding':'uint', 'format':'hex', 'set':0} 232*1031c584SApple OSS Distributions ] 233*1031c584SApple OSS Distributions } 234*1031c584SApple OSS Distributions 235*1031c584SApple OSS Distributions def __init__(self): 236*1031c584SApple OSS Distributions self.switch_context_address = osplugin_target_obj.FindSymbols('load_reg')[0].GetSymbol().GetStartAddress().GetLoadAddress(osplugin_target_obj) + 8 237*1031c584SApple OSS Distributions self.ResetRegisterValues() 238*1031c584SApple OSS Distributions 239*1031c584SApple OSS Distributions @classmethod 240*1031c584SApple OSS Distributions def GetRegisterInfo(cls, regnum): 241*1031c584SApple OSS Distributions if regnum < 0 or regnum > len(cls.register_info['registers']): 242*1031c584SApple OSS Distributions return '' 243*1031c584SApple OSS Distributions 244*1031c584SApple OSS Distributions reginfo = cls.register_info['registers'][regnum] 245*1031c584SApple OSS Distributions retval = '' 246*1031c584SApple OSS Distributions for i in list(reginfo.keys()): 247*1031c584SApple OSS Distributions v_str = str(reginfo[i]) 248*1031c584SApple OSS Distributions if i == 'set': 249*1031c584SApple OSS Distributions v_str = 'General Purpose Registers' 250*1031c584SApple OSS Distributions retval += "%s:%s;" % (str(i), v_str) 251*1031c584SApple OSS Distributions return retval 252*1031c584SApple OSS Distributions 253*1031c584SApple OSS Distributions def ResetRegisterValues(self): 254*1031c584SApple OSS Distributions self.r0 = 0 255*1031c584SApple OSS Distributions self.r1 = 0 256*1031c584SApple OSS Distributions self.r2 = 0 257*1031c584SApple OSS Distributions self.r3 = 0 258*1031c584SApple OSS Distributions self.r4 = 0 259*1031c584SApple OSS Distributions self.r5 = 0 260*1031c584SApple OSS Distributions self.r6 = 0 261*1031c584SApple OSS Distributions self.r7 = 0 262*1031c584SApple OSS Distributions self.r8 = 0 263*1031c584SApple OSS Distributions self.r9 = 0 264*1031c584SApple OSS Distributions self.r10 = 0 265*1031c584SApple OSS Distributions self.r11 = 0 266*1031c584SApple OSS Distributions self.r12 = 0 267*1031c584SApple OSS Distributions self.sp = 0 268*1031c584SApple OSS Distributions self.lr = 0 269*1031c584SApple OSS Distributions self.pc = 0 270*1031c584SApple OSS Distributions self.cpsr = 0 271*1031c584SApple OSS Distributions self.fsr = 0 272*1031c584SApple OSS Distributions self.far = 0 273*1031c584SApple OSS Distributions 274*1031c584SApple OSS Distributions def __str__(self): 275*1031c584SApple OSS Distributions return """ 276*1031c584SApple OSS Distributions r0 = {o.r0: <#010x} 277*1031c584SApple OSS Distributions r1 = {o.r1: <#010x} 278*1031c584SApple OSS Distributions r2 = {o.r2: <#010x} 279*1031c584SApple OSS Distributions r3 = {o.r3: <#010x} 280*1031c584SApple OSS Distributions r4 = {o.r4: <#010x} 281*1031c584SApple OSS Distributions r5 = {o.r5: <#010x} 282*1031c584SApple OSS Distributions r6 = {o.r6: <#010x} 283*1031c584SApple OSS Distributions r7 = {o.r7: <#010x} 284*1031c584SApple OSS Distributions r8 = {o.r8: <#010x} 285*1031c584SApple OSS Distributions r9 = {o.r9: <#010x} 286*1031c584SApple OSS Distributions r10 = {o.r10: <#010x} 287*1031c584SApple OSS Distributions r11 = {o.r11: <#010x} 288*1031c584SApple OSS Distributions r12 = {o.r12: <#010x} 289*1031c584SApple OSS Distributions sp = {o.sp: <#010x} 290*1031c584SApple OSS Distributions lr = {o.lr: <#010x} 291*1031c584SApple OSS Distributions pc = {o.pc: <#010x} 292*1031c584SApple OSS Distributions cpsr = {o.cpsr: <#010x} 293*1031c584SApple OSS Distributions fsr = {o.fsr : <#010x} 294*1031c584SApple OSS Distributions far = {o.far : <#010x} 295*1031c584SApple OSS Distributions """.format(o=self) 296*1031c584SApple OSS Distributions 297*1031c584SApple OSS Distributions def GetPackedRegisterState(self): 298*1031c584SApple OSS Distributions return struct.pack('19I', self.r0, self.r1, self.r2, self.r3, 299*1031c584SApple OSS Distributions self.r4, self.r5, self.r6, self.r7, 300*1031c584SApple OSS Distributions self.r8, self.r9, self.r10, self.r11, 301*1031c584SApple OSS Distributions self.r12, self.sp, self.lr, self.pc, 302*1031c584SApple OSS Distributions self.cpsr, self.fsr, self.far) 303*1031c584SApple OSS Distributions 304*1031c584SApple OSS Distributions def ReadRegisterDataFromKDPSavedState(self, kdp_state, kernel_version): 305*1031c584SApple OSS Distributions saved_state = kernel_version.CreateValueFromExpression(None, '(struct arm_saved_state *) ' + str(kdp_state.GetValueAsUnsigned())) 306*1031c584SApple OSS Distributions saved_state = saved_state.Dereference() 307*1031c584SApple OSS Distributions saved_state = PluginValue(saved_state) 308*1031c584SApple OSS Distributions self.ResetRegisterValues() 309*1031c584SApple OSS Distributions self.r0 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(0).GetValueAsUnsigned() 310*1031c584SApple OSS Distributions self.r1 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(1).GetValueAsUnsigned() 311*1031c584SApple OSS Distributions self.r2 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(2).GetValueAsUnsigned() 312*1031c584SApple OSS Distributions self.r3 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(3).GetValueAsUnsigned() 313*1031c584SApple OSS Distributions self.r4 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(4).GetValueAsUnsigned() 314*1031c584SApple OSS Distributions self.r5 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(5).GetValueAsUnsigned() 315*1031c584SApple OSS Distributions self.r6 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(6).GetValueAsUnsigned() 316*1031c584SApple OSS Distributions self.r7 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(7).GetValueAsUnsigned() 317*1031c584SApple OSS Distributions self.r8 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(8).GetValueAsUnsigned() 318*1031c584SApple OSS Distributions self.r9 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(9).GetValueAsUnsigned() 319*1031c584SApple OSS Distributions self.r10 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(10).GetValueAsUnsigned() 320*1031c584SApple OSS Distributions self.r11 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(11).GetValueAsUnsigned() 321*1031c584SApple OSS Distributions self.r12 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(12).GetValueAsUnsigned() 322*1031c584SApple OSS Distributions self.sp = saved_state.GetChildMemberWithName('sp').GetValueAsUnsigned() 323*1031c584SApple OSS Distributions self.lr = saved_state.GetChildMemberWithName('lr').GetValueAsUnsigned() 324*1031c584SApple OSS Distributions self.pc = saved_state.GetChildMemberWithName('pc').GetValueAsUnsigned() 325*1031c584SApple OSS Distributions self.cpsr = saved_state.GetChildMemberWithName('cpsr').GetValueAsUnsigned() 326*1031c584SApple OSS Distributions self.fsr = saved_state.GetChildMemberWithName('fsr').GetValueAsUnsigned() 327*1031c584SApple OSS Distributions self.far = saved_state.GetChildMemberWithName('far').GetValueAsUnsigned() 328*1031c584SApple OSS Distributions return self 329*1031c584SApple OSS Distributions 330*1031c584SApple OSS Distributions def ReadRegisterDataFromKernelStack(self, kstack_saved_state_addr, kernel_version): 331*1031c584SApple OSS Distributions saved_state = kernel_version.CreateValueFromExpression(None, '(struct arm_saved_state *) '+ str(kstack_saved_state_addr)) 332*1031c584SApple OSS Distributions saved_state = saved_state.Dereference() 333*1031c584SApple OSS Distributions saved_state = PluginValue(saved_state) 334*1031c584SApple OSS Distributions self.ResetRegisterValues() 335*1031c584SApple OSS Distributions self.r0 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(0).GetValueAsUnsigned() 336*1031c584SApple OSS Distributions self.r1 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(1).GetValueAsUnsigned() 337*1031c584SApple OSS Distributions self.r2 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(2).GetValueAsUnsigned() 338*1031c584SApple OSS Distributions self.r3 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(3).GetValueAsUnsigned() 339*1031c584SApple OSS Distributions self.r4 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(4).GetValueAsUnsigned() 340*1031c584SApple OSS Distributions self.r5 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(5).GetValueAsUnsigned() 341*1031c584SApple OSS Distributions self.r6 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(6).GetValueAsUnsigned() 342*1031c584SApple OSS Distributions self.r7 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(7).GetValueAsUnsigned() 343*1031c584SApple OSS Distributions self.r8 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(8).GetValueAsUnsigned() 344*1031c584SApple OSS Distributions self.r9 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(9).GetValueAsUnsigned() 345*1031c584SApple OSS Distributions self.r10 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(10).GetValueAsUnsigned() 346*1031c584SApple OSS Distributions self.r11 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(11).GetValueAsUnsigned() 347*1031c584SApple OSS Distributions self.r12 = saved_state.GetChildMemberWithName('r').GetChildAtIndex(12).GetValueAsUnsigned() 348*1031c584SApple OSS Distributions self.sp = saved_state.GetChildMemberWithName('sp').GetValueAsUnsigned() 349*1031c584SApple OSS Distributions self.lr = saved_state.GetChildMemberWithName('lr').GetValueAsUnsigned() 350*1031c584SApple OSS Distributions # pc for a blocked thread is treated to be the next instruction it would run after thread switch. 351*1031c584SApple OSS Distributions self.pc = self.switch_context_address 352*1031c584SApple OSS Distributions self.cpsr = saved_state.GetChildMemberWithName('cpsr').GetValueAsUnsigned() 353*1031c584SApple OSS Distributions self.fsr = saved_state.GetChildMemberWithName('fsr').GetValueAsUnsigned() 354*1031c584SApple OSS Distributions self.far = saved_state.GetChildMemberWithName('far').GetValueAsUnsigned() 355*1031c584SApple OSS Distributions return self 356*1031c584SApple OSS Distributions 357*1031c584SApple OSS Distributions def ReadRegisterDataFromContinuation(self, continuation_ptr): 358*1031c584SApple OSS Distributions self.ResetRegisterValues() 359*1031c584SApple OSS Distributions self.pc = continuation_ptr 360*1031c584SApple OSS Distributions return self 361*1031c584SApple OSS Distributions 362*1031c584SApple OSS Distributions 363*1031c584SApple OSS Distributionsclass I386_RegisterSet(object): 364*1031c584SApple OSS Distributions """ register info set for i386 architecture 365*1031c584SApple OSS Distributions """ 366*1031c584SApple OSS Distributions register_info = { 'sets' : ['GPR'], 367*1031c584SApple OSS Distributions 'registers': [ 368*1031c584SApple OSS Distributions { 'name': 'eax' , 'bitsize': 32, 'offset' : 0, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 0, 'dwarf': 0}, 369*1031c584SApple OSS Distributions { 'name': 'ebx' , 'bitsize': 32, 'offset' : 4, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 3, 'dwarf': 3}, 370*1031c584SApple OSS Distributions { 'name': 'ecx' , 'bitsize': 32, 'offset' : 8, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 1, 'dwarf': 1}, 371*1031c584SApple OSS Distributions { 'name': 'edx' , 'bitsize': 32, 'offset' :12, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 2, 'dwarf': 2}, 372*1031c584SApple OSS Distributions { 'name': 'edi' , 'bitsize': 32, 'offset' :16, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 7, 'dwarf': 7}, 373*1031c584SApple OSS Distributions { 'name': 'esi' , 'bitsize': 32, 'offset' :20, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 6, 'dwarf': 6}, 374*1031c584SApple OSS Distributions { 'name': 'ebp' , 'bitsize': 32, 'offset' :24, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 4, 'dwarf': 5, 'generic': 'fp', 'alt-name': 'fp'}, 375*1031c584SApple OSS Distributions { 'name': 'esp' , 'bitsize': 32, 'offset' :28, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 5, 'dwarf': 4, 'generic': 'sp', 'alt-name': 'sp'}, 376*1031c584SApple OSS Distributions { 'name': 'ss' , 'bitsize': 32, 'offset' :32, 'encoding': 'uint' , 'format':'hex' , 'set': 0}, 377*1031c584SApple OSS Distributions { 'name': 'eflags', 'bitsize': 32, 'offset' :36, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' : 9, 'dwarf': 9, 'generic': 'flags'}, 378*1031c584SApple OSS Distributions { 'name': 'eip' , 'bitsize': 32, 'offset' :40, 'encoding': 'uint' , 'format':'hex' , 'set': 0, 'gcc' :8, 'dwarf':8, 'generic': 'pc', 'alt-name': 'pc'}, 379*1031c584SApple OSS Distributions { 'name': 'cs' , 'bitsize': 32, 'offset' :44, 'encoding': 'uint' , 'format':'hex' , 'set': 0}, 380*1031c584SApple OSS Distributions { 'name': 'ds' , 'bitsize': 32, 'offset' :48, 'encoding': 'uint' , 'format':'hex' , 'set': 0}, 381*1031c584SApple OSS Distributions { 'name': 'es' , 'bitsize': 32, 'offset' :52, 'encoding': 'uint' , 'format':'hex' , 'set': 0}, 382*1031c584SApple OSS Distributions { 'name': 'fs' , 'bitsize': 32, 'offset' :56, 'encoding': 'uint' , 'format':'hex' , 'set': 0}, 383*1031c584SApple OSS Distributions { 'name': 'gs' , 'bitsize': 32, 'offset' :60, 'encoding': 'uint' , 'format':'hex' , 'set': 0}, 384*1031c584SApple OSS Distributions ] 385*1031c584SApple OSS Distributions } 386*1031c584SApple OSS Distributions 387*1031c584SApple OSS Distributions def __init__(self): 388*1031c584SApple OSS Distributions self.ResetRegisterValues() 389*1031c584SApple OSS Distributions 390*1031c584SApple OSS Distributions @classmethod 391*1031c584SApple OSS Distributions def GetRegisterInfo(cls, regnum): 392*1031c584SApple OSS Distributions if regnum < 0 or regnum > len(cls.register_info['registers']): 393*1031c584SApple OSS Distributions return '' 394*1031c584SApple OSS Distributions 395*1031c584SApple OSS Distributions reginfo = cls.register_info['registers'][regnum] 396*1031c584SApple OSS Distributions retval = '' 397*1031c584SApple OSS Distributions for i in list(reginfo.keys()): 398*1031c584SApple OSS Distributions v_str = str(reginfo[i]) 399*1031c584SApple OSS Distributions if i == 'set': 400*1031c584SApple OSS Distributions v_str = 'General Purpose Registers' 401*1031c584SApple OSS Distributions retval += "%s:%s;" % (str(i), v_str) 402*1031c584SApple OSS Distributions return retval 403*1031c584SApple OSS Distributions 404*1031c584SApple OSS Distributions def ResetRegisterValues(self): 405*1031c584SApple OSS Distributions """ set all registers to zero """ 406*1031c584SApple OSS Distributions self.eax = 0 407*1031c584SApple OSS Distributions self.ebx = 0 408*1031c584SApple OSS Distributions self.ecx = 0 409*1031c584SApple OSS Distributions self.edx = 0 410*1031c584SApple OSS Distributions self.edi = 0 411*1031c584SApple OSS Distributions self.esi = 0 412*1031c584SApple OSS Distributions self.ebp = 0 413*1031c584SApple OSS Distributions self.esp = 0 414*1031c584SApple OSS Distributions self.ss = 0 415*1031c584SApple OSS Distributions self.eflags = 0 416*1031c584SApple OSS Distributions self.eip = 0 417*1031c584SApple OSS Distributions self.cs = 0 418*1031c584SApple OSS Distributions self.ds = 0 419*1031c584SApple OSS Distributions self.es = 0 420*1031c584SApple OSS Distributions self.fs = 0 421*1031c584SApple OSS Distributions self.gs = 0 422*1031c584SApple OSS Distributions 423*1031c584SApple OSS Distributions def __str__(self): 424*1031c584SApple OSS Distributions return """ 425*1031c584SApple OSS Distributions eax = {o.eax: #010x} 426*1031c584SApple OSS Distributions ebx = {o.ebx: #010x} 427*1031c584SApple OSS Distributions ecx = {o.ecx: #010x} 428*1031c584SApple OSS Distributions edx = {o.edx: #010x} 429*1031c584SApple OSS Distributions edi = {o.edi: #010x} 430*1031c584SApple OSS Distributions esi = {o.esi: #010x} 431*1031c584SApple OSS Distributions ebp = {o.ebp: #010x} 432*1031c584SApple OSS Distributions esp = {o.esp: #010x} 433*1031c584SApple OSS Distributions ss = {o.ss: #010x} 434*1031c584SApple OSS Distributions eflags = {o.eflags: #010x} 435*1031c584SApple OSS Distributions eip = {o.eip: #010x} 436*1031c584SApple OSS Distributions cs = {o.cs: #010x} 437*1031c584SApple OSS Distributions ds = {o.ds: #010x} 438*1031c584SApple OSS Distributions es = {o.es: #010x} 439*1031c584SApple OSS Distributions fs = {o.fs: #010x} 440*1031c584SApple OSS Distributions gs = {o.gs: #010x} 441*1031c584SApple OSS Distributions """.format(o=self) 442*1031c584SApple OSS Distributions 443*1031c584SApple OSS Distributions def GetPackedRegisterState(self): 444*1031c584SApple OSS Distributions """ get a struct.pack register data """ 445*1031c584SApple OSS Distributions return struct.pack('16I', self.eax, self.ebx, self.ecx, 446*1031c584SApple OSS Distributions self.edx, self.edi, self.esi, 447*1031c584SApple OSS Distributions self.ebp, self.esp, self.ss, 448*1031c584SApple OSS Distributions self.eflags, self.eip, self.cs, 449*1031c584SApple OSS Distributions self.ds, self.es, self.fs, self.gs 450*1031c584SApple OSS Distributions ) 451*1031c584SApple OSS Distributions 452*1031c584SApple OSS Distributions def ReadRegisterDataFromKDPSavedState(self, kdp_state, kernel_version): 453*1031c584SApple OSS Distributions """ to be implemented""" 454*1031c584SApple OSS Distributions return None 455*1031c584SApple OSS Distributions 456*1031c584SApple OSS Distributions def ReadRegisterDataFromKernelStack(self, kstack_saved_state_addr, kernel_version): 457*1031c584SApple OSS Distributions """ to be implemented """ 458*1031c584SApple OSS Distributions return None 459*1031c584SApple OSS Distributions 460*1031c584SApple OSS Distributions def ReadRegisterDataFromContinuation(self, continuation_ptr): 461*1031c584SApple OSS Distributions self.ResetRegisterValues() 462*1031c584SApple OSS Distributions self.eip = continuation_ptr 463*1031c584SApple OSS Distributions return self 464*1031c584SApple OSS Distributions 465*1031c584SApple OSS Distributions 466*1031c584SApple OSS Distributionsclass X86_64RegisterSet(object): 467*1031c584SApple OSS Distributions """ register info set for x86_64 architecture """ 468*1031c584SApple OSS Distributions register_info = { 'sets' : ['GPR'], 469*1031c584SApple OSS Distributions 'registers': [ 470*1031c584SApple OSS Distributions { 'name':'rax' , 'bitsize' : 64, 'offset' : 0, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 0, 'dwarf' : 0}, 471*1031c584SApple OSS Distributions { 'name':'rbx' , 'bitsize' : 64, 'offset' : 8, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 3, 'dwarf' : 3}, 472*1031c584SApple OSS Distributions { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 2, 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', }, 473*1031c584SApple OSS Distributions { 'name':'rdx' , 'bitsize' : 64, 'offset' : 24, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 1, 'dwarf' : 1, 'generic':'arg3', 'alt-name':'arg3', }, 474*1031c584SApple OSS Distributions { 'name':'rdi' , 'bitsize' : 64, 'offset' : 32, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 5, 'dwarf' : 5, 'generic':'arg1', 'alt-name':'arg1', }, 475*1031c584SApple OSS Distributions { 'name':'rsi' , 'bitsize' : 64, 'offset' : 40, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 4, 'dwarf' : 4, 'generic':'arg2', 'alt-name':'arg2', }, 476*1031c584SApple OSS Distributions { 'name':'rbp' , 'bitsize' : 64, 'offset' : 48, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 6, 'dwarf' : 6, 'generic':'fp' , 'alt-name':'fp', }, 477*1031c584SApple OSS Distributions { 'name':'rsp' , 'bitsize' : 64, 'offset' : 56, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 7, 'dwarf' : 7, 'generic':'sp' , 'alt-name':'sp', }, 478*1031c584SApple OSS Distributions { 'name':'r8' , 'bitsize' : 64, 'offset' : 64, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 8, 'dwarf' : 8, 'generic':'arg5', 'alt-name':'arg5', }, 479*1031c584SApple OSS Distributions { 'name':'r9' , 'bitsize' : 64, 'offset' : 72, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 9, 'dwarf' : 9, 'generic':'arg6', 'alt-name':'arg6', }, 480*1031c584SApple OSS Distributions { 'name':'r10' , 'bitsize' : 64, 'offset' : 80, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 10, 'dwarf' : 10}, 481*1031c584SApple OSS Distributions { 'name':'r11' , 'bitsize' : 64, 'offset' : 88, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 11, 'dwarf' : 11}, 482*1031c584SApple OSS Distributions { 'name':'r12' , 'bitsize' : 64, 'offset' : 96, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 12, 'dwarf' : 12}, 483*1031c584SApple OSS Distributions { 'name':'r13' , 'bitsize' : 64, 'offset' : 104, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 13, 'dwarf' : 13}, 484*1031c584SApple OSS Distributions { 'name':'r14' , 'bitsize' : 64, 'offset' : 112, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 14, 'dwarf' : 14}, 485*1031c584SApple OSS Distributions { 'name':'r15' , 'bitsize' : 64, 'offset' : 120, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 15, 'dwarf' : 15}, 486*1031c584SApple OSS Distributions { 'name':'rip' , 'bitsize' : 64, 'offset' : 128, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 16, 'dwarf' : 16, 'generic':'pc', 'alt-name':'pc' }, 487*1031c584SApple OSS Distributions { 'name':'rflags' , 'bitsize' : 64, 'offset' : 136, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'generic':'flags', 'alt-name':'flags' }, 488*1031c584SApple OSS Distributions { 'name':'cs' , 'bitsize' : 64, 'offset' : 144, 'encoding':'uint' , 'format':'hex' , 'set': 0 }, 489*1031c584SApple OSS Distributions { 'name':'fs' , 'bitsize' : 64, 'offset' : 152, 'encoding':'uint' , 'format':'hex' , 'set': 0 }, 490*1031c584SApple OSS Distributions { 'name':'gs' , 'bitsize' : 64, 'offset' : 160, 'encoding':'uint' , 'format':'hex' , 'set': 0 }, 491*1031c584SApple OSS Distributions ] 492*1031c584SApple OSS Distributions } 493*1031c584SApple OSS Distributions def __init__(self): 494*1031c584SApple OSS Distributions self.ResetRegisterValues() 495*1031c584SApple OSS Distributions 496*1031c584SApple OSS Distributions @classmethod 497*1031c584SApple OSS Distributions def GetRegisterInfo(cls, regnum): 498*1031c584SApple OSS Distributions if regnum < 0 or regnum > len(cls.register_info['registers']): 499*1031c584SApple OSS Distributions return '' 500*1031c584SApple OSS Distributions 501*1031c584SApple OSS Distributions reginfo = cls.register_info['registers'][regnum] 502*1031c584SApple OSS Distributions retval = '' 503*1031c584SApple OSS Distributions for i in list(reginfo.keys()): 504*1031c584SApple OSS Distributions v_str = str(reginfo[i]) 505*1031c584SApple OSS Distributions if i == 'set': 506*1031c584SApple OSS Distributions v_str = 'General Purpose Registers' 507*1031c584SApple OSS Distributions retval += "%s:%s;" % (str(i), v_str) 508*1031c584SApple OSS Distributions return retval 509*1031c584SApple OSS Distributions 510*1031c584SApple OSS Distributions 511*1031c584SApple OSS Distributions def ResetRegisterValues(self): 512*1031c584SApple OSS Distributions """ set all the registers to zero. """ 513*1031c584SApple OSS Distributions self.rax = 0 514*1031c584SApple OSS Distributions self.rbx = 0 515*1031c584SApple OSS Distributions self.rcx = 0 516*1031c584SApple OSS Distributions self.rdx = 0 517*1031c584SApple OSS Distributions self.rdi = 0 518*1031c584SApple OSS Distributions self.rsi = 0 519*1031c584SApple OSS Distributions self.rbp = 0 520*1031c584SApple OSS Distributions self.rsp = 0 521*1031c584SApple OSS Distributions self.r8 = 0 522*1031c584SApple OSS Distributions self.r9 = 0 523*1031c584SApple OSS Distributions self.r10 = 0 524*1031c584SApple OSS Distributions self.r11 = 0 525*1031c584SApple OSS Distributions self.r12 = 0 526*1031c584SApple OSS Distributions self.r13 = 0 527*1031c584SApple OSS Distributions self.r14 = 0 528*1031c584SApple OSS Distributions self.r15 = 0 529*1031c584SApple OSS Distributions self.rip = 0 530*1031c584SApple OSS Distributions self.rflags = 0 531*1031c584SApple OSS Distributions self.cs = 0 532*1031c584SApple OSS Distributions self.fs = 0 533*1031c584SApple OSS Distributions self.gs = 0 534*1031c584SApple OSS Distributions 535*1031c584SApple OSS Distributions def __str__(self): 536*1031c584SApple OSS Distributions return """ 537*1031c584SApple OSS Distributions rax = {o.rax: <#018x} 538*1031c584SApple OSS Distributions rbx = {o.rbx: <#018x} 539*1031c584SApple OSS Distributions rcx = {o.rcx: <#018x} 540*1031c584SApple OSS Distributions rdx = {o.rdx: <#018x} 541*1031c584SApple OSS Distributions rdi = {o.rdi: <#018x} 542*1031c584SApple OSS Distributions rsi = {o.rsi: <#018x} 543*1031c584SApple OSS Distributions rbp = {o.rbp: <#018x} 544*1031c584SApple OSS Distributions rsp = {o.rsp: <#018x} 545*1031c584SApple OSS Distributions r8 = {o.r8: <#018x} 546*1031c584SApple OSS Distributions r9 = {o.r9: <#018x} 547*1031c584SApple OSS Distributions r10 = {o.r10: <#018x} 548*1031c584SApple OSS Distributions r11 = {o.r11: <#018x} 549*1031c584SApple OSS Distributions r12 = {o.r12: <#018x} 550*1031c584SApple OSS Distributions r13 = {o.r13: <#018x} 551*1031c584SApple OSS Distributions r14 = {o.r14: <#018x} 552*1031c584SApple OSS Distributions r15 = {o.r15: <#018x} 553*1031c584SApple OSS Distributions rip = {o.rip: <#018x} 554*1031c584SApple OSS Distributions rflags = {o.rflags: <#018x} 555*1031c584SApple OSS Distributions cs = {o.cs: <#018x} 556*1031c584SApple OSS Distributions fs = {o.fs: <#018x} 557*1031c584SApple OSS Distributions gs = {o.gs: <#018x} 558*1031c584SApple OSS Distributions """.format(o=self) 559*1031c584SApple OSS Distributions 560*1031c584SApple OSS Distributions def GetPackedRegisterState(self): 561*1031c584SApple OSS Distributions """ get a struct.pack register data for passing to C constructs """ 562*1031c584SApple OSS Distributions return struct.pack('21Q', self.rax, self.rbx, self.rcx, self.rdx, self.rdi, 563*1031c584SApple OSS Distributions self.rsi, self.rbp, self.rsp, self.r8, self.r9, 564*1031c584SApple OSS Distributions self.r10, self.r11, self.r12, self.r13, self.r14, 565*1031c584SApple OSS Distributions self.r15, self.rip, self.rflags, self.cs, self.fs, self.gs) 566*1031c584SApple OSS Distributions 567*1031c584SApple OSS Distributions def ReadRegisterDataFromKDPSavedState(self, kdp_state, kernel_version): 568*1031c584SApple OSS Distributions saved_state = kernel_version.CreateValueFromExpression(None, '(struct x86_saved_state64 *) '+ str(kdp_state.GetValueAsUnsigned())) 569*1031c584SApple OSS Distributions saved_state = saved_state.Dereference() 570*1031c584SApple OSS Distributions saved_state = PluginValue(saved_state) 571*1031c584SApple OSS Distributions self.ResetRegisterValues() 572*1031c584SApple OSS Distributions self.rdi = saved_state.GetChildMemberWithName('rdi').GetValueAsUnsigned() 573*1031c584SApple OSS Distributions self.rsi = saved_state.GetChildMemberWithName('rsi').GetValueAsUnsigned() 574*1031c584SApple OSS Distributions self.rdx = saved_state.GetChildMemberWithName('rdx').GetValueAsUnsigned() 575*1031c584SApple OSS Distributions self.r10 = saved_state.GetChildMemberWithName('r10').GetValueAsUnsigned() 576*1031c584SApple OSS Distributions self.r8 = saved_state.GetChildMemberWithName('r8').GetValueAsUnsigned() 577*1031c584SApple OSS Distributions self.r9 = saved_state.GetChildMemberWithName('r9').GetValueAsUnsigned() 578*1031c584SApple OSS Distributions self.r15 = saved_state.GetChildMemberWithName('r15').GetValueAsUnsigned() 579*1031c584SApple OSS Distributions self.r14 = saved_state.GetChildMemberWithName('r14').GetValueAsUnsigned() 580*1031c584SApple OSS Distributions self.r13 = saved_state.GetChildMemberWithName('r13').GetValueAsUnsigned() 581*1031c584SApple OSS Distributions self.r12 = saved_state.GetChildMemberWithName('r12').GetValueAsUnsigned() 582*1031c584SApple OSS Distributions self.r11 = saved_state.GetChildMemberWithName('r11').GetValueAsUnsigned() 583*1031c584SApple OSS Distributions self.rbp = saved_state.GetChildMemberWithName('rbp').GetValueAsUnsigned() 584*1031c584SApple OSS Distributions self.rbx = saved_state.GetChildMemberWithName('rbx').GetValueAsUnsigned() 585*1031c584SApple OSS Distributions self.rcx = saved_state.GetChildMemberWithName('rcx').GetValueAsUnsigned() 586*1031c584SApple OSS Distributions self.rax = saved_state.GetChildMemberWithName('rax').GetValueAsUnsigned() 587*1031c584SApple OSS Distributions self.rip = saved_state.GetChildMemberWithName('isf').GetChildMemberWithName('rip').GetValueAsUnsigned() 588*1031c584SApple OSS Distributions self.rflags = saved_state.GetChildMemberWithName('isf').GetChildMemberWithName('rflags').GetValueAsUnsigned() 589*1031c584SApple OSS Distributions self.rsp = saved_state.GetChildMemberWithName('isf').GetChildMemberWithName('rsp').GetValueAsUnsigned() 590*1031c584SApple OSS Distributions return self 591*1031c584SApple OSS Distributions 592*1031c584SApple OSS Distributions def ReadRegisterDataFromKernelStack(self, kstack_saved_state_addr, kernel_version): 593*1031c584SApple OSS Distributions saved_state = kernel_version.CreateValueFromExpression(None, '(struct x86_kernel_state *) '+ str(kstack_saved_state_addr)) 594*1031c584SApple OSS Distributions saved_state = saved_state.Dereference() 595*1031c584SApple OSS Distributions saved_state = PluginValue(saved_state) 596*1031c584SApple OSS Distributions self.ResetRegisterValues() 597*1031c584SApple OSS Distributions self.rbx = saved_state.GetChildMemberWithName('k_rbx').GetValueAsUnsigned() 598*1031c584SApple OSS Distributions self.rsp = saved_state.GetChildMemberWithName('k_rsp').GetValueAsUnsigned() 599*1031c584SApple OSS Distributions self.rbp = saved_state.GetChildMemberWithName('k_rbp').GetValueAsUnsigned() 600*1031c584SApple OSS Distributions self.r12 = saved_state.GetChildMemberWithName('k_r12').GetValueAsUnsigned() 601*1031c584SApple OSS Distributions self.r13 = saved_state.GetChildMemberWithName('k_r13').GetValueAsUnsigned() 602*1031c584SApple OSS Distributions self.r14 = saved_state.GetChildMemberWithName('k_r14').GetValueAsUnsigned() 603*1031c584SApple OSS Distributions self.r15 = saved_state.GetChildMemberWithName('k_r15').GetValueAsUnsigned() 604*1031c584SApple OSS Distributions self.rip = saved_state.GetChildMemberWithName('k_rip').GetValueAsUnsigned() 605*1031c584SApple OSS Distributions return self 606*1031c584SApple OSS Distributions 607*1031c584SApple OSS Distributions def ReadRegisterDataFromContinuation(self, continuation_ptr): 608*1031c584SApple OSS Distributions self.ResetRegisterValues() 609*1031c584SApple OSS Distributions self.rip = continuation_ptr 610*1031c584SApple OSS Distributions return self 611*1031c584SApple OSS Distributions 612*1031c584SApple OSS Distributions 613*1031c584SApple OSS Distributions 614*1031c584SApple OSS Distributions 615*1031c584SApple OSS Distributionsdef IterateQueue(queue_head, element_ptr_type, element_field_name): 616*1031c584SApple OSS Distributions """ iterate over a queue in kernel of type queue_head_t. refer to osfmk/kern/queue.h 617*1031c584SApple OSS Distributions params: 618*1031c584SApple OSS Distributions queue_head - lldb.SBValue : Value object for queue_head. 619*1031c584SApple OSS Distributions element_type - lldb.SBType : a pointer type of the element 'next' points to. Typically its structs like thread, task etc.. 620*1031c584SApple OSS Distributions element_field_name - str : name of the field in target struct. 621*1031c584SApple OSS Distributions returns: 622*1031c584SApple OSS Distributions A generator does not return. It is used for iterating. 623*1031c584SApple OSS Distributions SBValue : an object thats of type (element_type) queue_head->next. Always a pointer object 624*1031c584SApple OSS Distributions """ 625*1031c584SApple OSS Distributions queue_head_addr = 0x0 626*1031c584SApple OSS Distributions if queue_head.TypeIsPointerType(): 627*1031c584SApple OSS Distributions queue_head_addr = queue_head.GetValueAsUnsigned() 628*1031c584SApple OSS Distributions else: 629*1031c584SApple OSS Distributions queue_head_addr = queue_head.GetAddress().GetLoadAddress(osplugin_target_obj) 630*1031c584SApple OSS Distributions cur_elt = queue_head.GetChildMemberWithName('next') 631*1031c584SApple OSS Distributions while True: 632*1031c584SApple OSS Distributions if not cur_elt.IsValid() or cur_elt.GetValueAsUnsigned() == 0 or cur_elt.GetValueAsUnsigned() == queue_head_addr: 633*1031c584SApple OSS Distributions break 634*1031c584SApple OSS Distributions elt = cur_elt.Cast(element_ptr_type) 635*1031c584SApple OSS Distributions yield elt 636*1031c584SApple OSS Distributions cur_elt = elt.GetChildMemberWithName(element_field_name).GetChildMemberWithName('next') 637*1031c584SApple OSS Distributions 638*1031c584SApple OSS Distributionsdef GetUniqueSessionID(process_obj): 639*1031c584SApple OSS Distributions """ Create a unique session identifier. 640*1031c584SApple OSS Distributions params: 641*1031c584SApple OSS Distributions process_obj: lldb.SBProcess object refering to connected process. 642*1031c584SApple OSS Distributions returns: 643*1031c584SApple OSS Distributions int - a unique number identified by processid and stopid. 644*1031c584SApple OSS Distributions """ 645*1031c584SApple OSS Distributions session_key_str = "" 646*1031c584SApple OSS Distributions if hasattr(process_obj, "GetUniqueID"): 647*1031c584SApple OSS Distributions session_key_str += str(process_obj.GetUniqueID()) + ":" 648*1031c584SApple OSS Distributions else: 649*1031c584SApple OSS Distributions session_key_str += "0:" 650*1031c584SApple OSS Distributions 651*1031c584SApple OSS Distributions if hasattr(process_obj, "GetStopID"): 652*1031c584SApple OSS Distributions session_key_str += str(process_obj.GetStopID()) 653*1031c584SApple OSS Distributions else: 654*1031c584SApple OSS Distributions session_key_str +="1" 655*1031c584SApple OSS Distributions 656*1031c584SApple OSS Distributions return hash(session_key_str) 657*1031c584SApple OSS Distributions 658*1031c584SApple OSS Distributions 659*1031c584SApple OSS Distributions(archX86_64, archARMv7, archI386, archARMv8) = ("x86_64", "armv7", "i386", "arm64") 660*1031c584SApple OSS Distributions 661*1031c584SApple OSS Distributionsclass OperatingSystemPlugIn(object): 662*1031c584SApple OSS Distributions """Class that provides data for an instance of a LLDB 'OperatingSystemPython' plug-in class""" 663*1031c584SApple OSS Distributions 664*1031c584SApple OSS Distributions def __init__(self, process): 665*1031c584SApple OSS Distributions '''Initialization needs a valid.SBProcess object''' 666*1031c584SApple OSS Distributions self.process = None 667*1031c584SApple OSS Distributions self.registers = None 668*1031c584SApple OSS Distributions self.threads = None 669*1031c584SApple OSS Distributions self.thread_cache = {} 670*1031c584SApple OSS Distributions self.current_session_id = 0 671*1031c584SApple OSS Distributions self.kdp_thread = None 672*1031c584SApple OSS Distributions if type(process) is lldb.SBProcess and process.IsValid(): 673*1031c584SApple OSS Distributions global osplugin_target_obj 674*1031c584SApple OSS Distributions self.process = process 675*1031c584SApple OSS Distributions self._target = process.target 676*1031c584SApple OSS Distributions osplugin_target_obj = self._target 677*1031c584SApple OSS Distributions self.current_session_id = GetUniqueSessionID(self.process) 678*1031c584SApple OSS Distributions self.version = self._target.FindGlobalVariables('version', 1).GetValueAtIndex(0) 679*1031c584SApple OSS Distributions self.kasan_tbi = self._target.FindGlobalVariables('kasan_tbi_enabled', 1).GetValueAtIndex(0) 680*1031c584SApple OSS Distributions self.kernel_stack_size = self._target.FindGlobalVariables('kernel_stack_size', 1).GetValueAtIndex(0).GetValueAsUnsigned() 681*1031c584SApple OSS Distributions self.kernel_context_size = 0 682*1031c584SApple OSS Distributions self.connected_over_kdp = False 683*1031c584SApple OSS Distributions # connected_to_debugserver signifies if we are connected to astris or other gdbserver instance 684*1031c584SApple OSS Distributions # that has the correct thread state for on core threads. For kdp and coredumps we rely on in memory 685*1031c584SApple OSS Distributions # state of threads. 686*1031c584SApple OSS Distributions self.connected_to_debugserver = True 687*1031c584SApple OSS Distributions plugin_string = self.process.GetPluginName().lower() 688*1031c584SApple OSS Distributions if plugin_string.find("kdp") >=0: 689*1031c584SApple OSS Distributions self.connected_over_kdp = True 690*1031c584SApple OSS Distributions self.connected_to_debugserver = False 691*1031c584SApple OSS Distributions #print "version", self.version, "kernel_stack_size", self.kernel_stack_size, "context_size", self.kernel_context_size 692*1031c584SApple OSS Distributions self.threads = None # Will be an dictionary containing info for each thread 693*1031c584SApple OSS Distributions triple = self.process.target.triple 694*1031c584SApple OSS Distributions arch = triple.split('-')[0].lower() 695*1031c584SApple OSS Distributions self.target_arch = "" 696*1031c584SApple OSS Distributions self.kernel_context_size = 0 697*1031c584SApple OSS Distributions if arch == archX86_64 : 698*1031c584SApple OSS Distributions self.target_arch = archX86_64 699*1031c584SApple OSS Distributions print("Target arch: x86_64") 700*1031c584SApple OSS Distributions self.register_set = X86_64RegisterSet() 701*1031c584SApple OSS Distributions self.kernel_context_size = self._target.FindFirstType('x86_kernel_state').GetByteSize() 702*1031c584SApple OSS Distributions self.kernel_thread_state_size = self._target.FindFirstType('struct thread_kernel_state').GetByteSize() 703*1031c584SApple OSS Distributions elif arch.startswith(archARMv7) : 704*1031c584SApple OSS Distributions self.target_arch = arch 705*1031c584SApple OSS Distributions print("Target arch: " + self.target_arch) 706*1031c584SApple OSS Distributions self.register_set = Armv7_RegisterSet() 707*1031c584SApple OSS Distributions elif arch.startswith(archARMv8): 708*1031c584SApple OSS Distributions self.target_arch = arch 709*1031c584SApple OSS Distributions print("Target arch: " + self.target_arch) 710*1031c584SApple OSS Distributions self.register_set = Armv8_RegisterSet() 711*1031c584SApple OSS Distributions # connection intel arm 712*1031c584SApple OSS Distributions # kdp Memory Memory 713*1031c584SApple OSS Distributions # gdb Server Server 714*1031c584SApple OSS Distributions # coredump Memory Server 715*1031c584SApple OSS Distributions if not self.connected_over_kdp : 716*1031c584SApple OSS Distributions if plugin_string.find('core') >= 0 and self.target_arch == archX86_64: 717*1031c584SApple OSS Distributions self.connected_to_debugserver = False 718*1031c584SApple OSS Distributions self.registers = self.register_set.register_info 719*1031c584SApple OSS Distributions if self.connected_to_debugserver: 720*1031c584SApple OSS Distributions print("Connected to live debugserver or arm core. Will associate on-core threads to registers reported by server.") 721*1031c584SApple OSS Distributions else: 722*1031c584SApple OSS Distributions print("Instantiating threads completely from saved state in memory.") 723*1031c584SApple OSS Distributions 724*1031c584SApple OSS Distributions def create_thread(self, tid, context): 725*1031c584SApple OSS Distributions def strip_tbi(v): 726*1031c584SApple OSS Distributions if self.kasan_tbi and v != 0: 727*1031c584SApple OSS Distributions v |= (0xFF << 56) 728*1031c584SApple OSS Distributions return v 729*1031c584SApple OSS Distributions 730*1031c584SApple OSS Distributions # Strip TBI explicitly in case create_thread() is called externally. 731*1031c584SApple OSS Distributions context = strip_tbi(context) 732*1031c584SApple OSS Distributions 733*1031c584SApple OSS Distributions # tid == deadbeef means its a custom thread which kernel does not know of. 734*1031c584SApple OSS Distributions if tid == 0xdeadbeef : 735*1031c584SApple OSS Distributions # tid manipulation should be the same as in "switchtoregs" code in lldbmacros/process.py . 736*1031c584SApple OSS Distributions tid = 0xdead0000 | (context & ~0xffff0000) 737*1031c584SApple OSS Distributions tid = tid & 0xdeadffff 738*1031c584SApple OSS Distributions thread_obj = { 'tid' : tid, 739*1031c584SApple OSS Distributions 'ptr' : context, 740*1031c584SApple OSS Distributions 'name' : 'switchtoregs' + hex(context), 741*1031c584SApple OSS Distributions 'queue' : 'None', 742*1031c584SApple OSS Distributions 'state' : 'stopped', 743*1031c584SApple OSS Distributions 'stop_reason' : 'none' 744*1031c584SApple OSS Distributions } 745*1031c584SApple OSS Distributions self.thread_cache[tid] = thread_obj 746*1031c584SApple OSS Distributions return thread_obj 747*1031c584SApple OSS Distributions 748*1031c584SApple OSS Distributions th_ptr = context 749*1031c584SApple OSS Distributions th = self.version.CreateValueFromExpression(str(th_ptr), '(struct thread *)' + str(th_ptr)) 750*1031c584SApple OSS Distributions thread_id = th.GetChildMemberWithName('thread_id').GetValueAsUnsigned() 751*1031c584SApple OSS Distributions if tid != thread_id: 752*1031c584SApple OSS Distributions print("FATAL ERROR: Creating thread from memory 0x%x with tid in mem=%d when requested tid = %d " % (context, thread_id, tid)) 753*1031c584SApple OSS Distributions return None 754*1031c584SApple OSS Distributions 755*1031c584SApple OSS Distributions wait_queue = strip_tbi(th.GetChildMemberWithName('wait_queue').GetValueAsUnsigned()) 756*1031c584SApple OSS Distributions thread_obj = { 'tid' : thread_id, 757*1031c584SApple OSS Distributions 'ptr' : th.GetValueAsUnsigned(), 758*1031c584SApple OSS Distributions 'name' : hex(th.GetValueAsUnsigned()).rstrip('L'), 759*1031c584SApple OSS Distributions 'queue' : hex(wait_queue).rstrip('L'), 760*1031c584SApple OSS Distributions 'state' : 'stopped', 761*1031c584SApple OSS Distributions 'stop_reason' : 'none' 762*1031c584SApple OSS Distributions } 763*1031c584SApple OSS Distributions 764*1031c584SApple OSS Distributions if self.current_session_id != GetUniqueSessionID(self.process): 765*1031c584SApple OSS Distributions self.thread_cache = {} 766*1031c584SApple OSS Distributions self.current_session_id = GetUniqueSessionID(self.process) 767*1031c584SApple OSS Distributions 768*1031c584SApple OSS Distributions self.thread_cache[tid] = thread_obj 769*1031c584SApple OSS Distributions return thread_obj 770*1031c584SApple OSS Distributions 771*1031c584SApple OSS Distributions def get_thread_info(self): 772*1031c584SApple OSS Distributions self.kdp_thread = None 773*1031c584SApple OSS Distributions self.kdp_state = None 774*1031c584SApple OSS Distributions if self.connected_over_kdp : 775*1031c584SApple OSS Distributions kdp = self._target.FindGlobalVariables('kdp',1).GetValueAtIndex(0) 776*1031c584SApple OSS Distributions kdp_state = kdp.GetChildMemberWithName('saved_state') 777*1031c584SApple OSS Distributions kdp_thread = self._strip_thread_tbi(kdp.GetChildMemberWithName('kdp_thread')) 778*1031c584SApple OSS Distributions if kdp_thread and kdp_thread.GetValueAsUnsigned() != 0: 779*1031c584SApple OSS Distributions self.kdp_thread = kdp_thread 780*1031c584SApple OSS Distributions self.kdp_state = kdp_state 781*1031c584SApple OSS Distributions kdp_thid = kdp_thread.GetChildMemberWithName('thread_id').GetValueAsUnsigned() 782*1031c584SApple OSS Distributions self.create_thread(kdp_thid, kdp_thread.GetValueAsUnsigned()) 783*1031c584SApple OSS Distributions self.thread_cache[kdp_thid]['core']=0 784*1031c584SApple OSS Distributions retval = [self.thread_cache[kdp_thid]] 785*1031c584SApple OSS Distributions return retval 786*1031c584SApple OSS Distributions else: 787*1031c584SApple OSS Distributions print("FATAL FAILURE: Unable to find kdp_thread state for this connection.") 788*1031c584SApple OSS Distributions return [] 789*1031c584SApple OSS Distributions 790*1031c584SApple OSS Distributions num_threads = self._target.FindGlobalVariables('threads_count',1).GetValueAtIndex(0).GetValueAsUnsigned() 791*1031c584SApple OSS Distributions #In case we are caught before threads are initialized. Fallback to threads known by astris/gdb server. 792*1031c584SApple OSS Distributions if num_threads <=0 : 793*1031c584SApple OSS Distributions return [] 794*1031c584SApple OSS Distributions 795*1031c584SApple OSS Distributions self.current_session_id = GetUniqueSessionID(self.process) 796*1031c584SApple OSS Distributions self.threads = [] 797*1031c584SApple OSS Distributions self.thread_cache = {} 798*1031c584SApple OSS Distributions self.processors = [] 799*1031c584SApple OSS Distributions try: 800*1031c584SApple OSS Distributions processor_list_val = PluginValue(self._target.FindGlobalVariables('processor_list',1).GetValueAtIndex(0)) 801*1031c584SApple OSS Distributions while processor_list_val.IsValid() and processor_list_val.GetValueAsUnsigned() !=0 : 802*1031c584SApple OSS Distributions th = self._strip_thread_tbi(processor_list_val.GetChildMemberWithName('active_thread')) 803*1031c584SApple OSS Distributions th_id = th.GetChildMemberWithName('thread_id').GetValueAsUnsigned() 804*1031c584SApple OSS Distributions cpu_id = processor_list_val.GetChildMemberWithName('cpu_id').GetValueAsUnsigned() 805*1031c584SApple OSS Distributions self.processors.append({'active_thread': th.GetValueAsUnsigned(), 'cpu_id': cpu_id}) 806*1031c584SApple OSS Distributions self.create_thread(th_id, th.GetValueAsUnsigned()) 807*1031c584SApple OSS Distributions if self.connected_to_debugserver: 808*1031c584SApple OSS Distributions self.thread_cache[th_id]['core'] = cpu_id 809*1031c584SApple OSS Distributions self.thread_cache[th_id]['queue'] = "cpu-%d" % int(cpu_id) 810*1031c584SApple OSS Distributions nth = self.thread_cache[th_id] 811*1031c584SApple OSS Distributions self.threads.append(nth) 812*1031c584SApple OSS Distributions self.thread_cache[nth['tid']] = nth 813*1031c584SApple OSS Distributions processor_list_val = processor_list_val.GetChildMemberWithName('processor_list') 814*1031c584SApple OSS Distributions except KeyboardInterrupt as ke: 815*1031c584SApple OSS Distributions print("OS Plugin Interrupted during thread loading process. \nWARNING:Thread registers and backtraces may not be accurate.") 816*1031c584SApple OSS Distributions return self.threads 817*1031c584SApple OSS Distributions 818*1031c584SApple OSS Distributions if hasattr(self.process, 'CreateOSPluginThread'): 819*1031c584SApple OSS Distributions return self.threads 820*1031c584SApple OSS Distributions 821*1031c584SApple OSS Distributions # FIXME remove legacy code 822*1031c584SApple OSS Distributions try: 823*1031c584SApple OSS Distributions thread_q_head = self._target.FindGlobalVariables('threads', 1).GetValueAtIndex(0) 824*1031c584SApple OSS Distributions thread_type = self._target.FindFirstType('thread') 825*1031c584SApple OSS Distributions thread_ptr_type = thread_type.GetPointerType() 826*1031c584SApple OSS Distributions for th in IterateQueue(thread_q_head, thread_ptr_type, 'threads'): 827*1031c584SApple OSS Distributions th = self._strip_thread_tbi(th) 828*1031c584SApple OSS Distributions th_id = th.GetChildMemberWithName('thread_id').GetValueAsUnsigned() 829*1031c584SApple OSS Distributions self.create_thread(th_id, th.GetValueAsUnsigned()) 830*1031c584SApple OSS Distributions nth = self.thread_cache[th_id] 831*1031c584SApple OSS Distributions for cputhread in self.processors: 832*1031c584SApple OSS Distributions if cputhread['active_thread'] == nth['ptr']: 833*1031c584SApple OSS Distributions nth['core'] = cputhread['cpu_id'] 834*1031c584SApple OSS Distributions self.threads.append( nth ) 835*1031c584SApple OSS Distributions except KeyboardInterrupt as ke: 836*1031c584SApple OSS Distributions print("OS Plugin Interrupted during thread loading process. \nWARNING:Thread registers and backtraces may not be accurate.") 837*1031c584SApple OSS Distributions return self.threads 838*1031c584SApple OSS Distributions # end legacy code 839*1031c584SApple OSS Distributions return self.threads 840*1031c584SApple OSS Distributions 841*1031c584SApple OSS Distributions def get_register_info(self): 842*1031c584SApple OSS Distributions if self.registers == None: 843*1031c584SApple OSS Distributions print("Register Information not found ") 844*1031c584SApple OSS Distributions return self.register_set.register_info 845*1031c584SApple OSS Distributions 846*1031c584SApple OSS Distributions def get_register_data(self, tid): 847*1031c584SApple OSS Distributions thobj = None 848*1031c584SApple OSS Distributions try: 849*1031c584SApple OSS Distributions regs = self.register_set 850*1031c584SApple OSS Distributions if self.current_session_id != GetUniqueSessionID(self.process): 851*1031c584SApple OSS Distributions self.thread_cache = {} 852*1031c584SApple OSS Distributions self.current_session_id = GetUniqueSessionID(self.process) 853*1031c584SApple OSS Distributions if tid in self.thread_cache: 854*1031c584SApple OSS Distributions 855*1031c584SApple OSS Distributions #Check if the thread is a fake one. Then create and return registers directly 856*1031c584SApple OSS Distributions if self.thread_cache[tid]['name'].find('switchtoregs') == 0: 857*1031c584SApple OSS Distributions savedstateobj = self.version.CreateValueFromExpression(None, '(uintptr_t *) ' + str(self.thread_cache[tid]['ptr'])) 858*1031c584SApple OSS Distributions regs.ReadRegisterDataFromKDPSavedState(savedstateobj, self.version) 859*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 860*1031c584SApple OSS Distributions 861*1031c584SApple OSS Distributions thobj = self.version.CreateValueFromExpression(self.thread_cache[tid]['name'], '(struct thread *)' + str(self.thread_cache[tid]['ptr'])) 862*1031c584SApple OSS Distributions 863*1031c584SApple OSS Distributions if thobj == None : 864*1031c584SApple OSS Distributions print("FATAL ERROR: Could not find thread with id %d" % tid) 865*1031c584SApple OSS Distributions regs.ResetRegisterValues() 866*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 867*1031c584SApple OSS Distributions 868*1031c584SApple OSS Distributions if self.kdp_thread and self.kdp_thread.GetValueAsUnsigned() == thobj.GetValueAsUnsigned(): 869*1031c584SApple OSS Distributions regs.ReadRegisterDataFromKDPSavedState(self.kdp_state, self.version) 870*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 871*1031c584SApple OSS Distributions if int(PluginValue(thobj).GetChildMemberWithName('kernel_stack').GetValueAsUnsigned()) != 0 : 872*1031c584SApple OSS Distributions if self.target_arch == archX86_64 : 873*1031c584SApple OSS Distributions # we do have a stack so lets get register information 874*1031c584SApple OSS Distributions saved_state_addr = PluginValue(thobj).GetChildMemberWithName('kernel_stack').GetValueAsUnsigned() + self.kernel_stack_size - self.kernel_thread_state_size 875*1031c584SApple OSS Distributions regs.ReadRegisterDataFromKernelStack(saved_state_addr, self.version) 876*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 877*1031c584SApple OSS Distributions elif self.target_arch.startswith(archARMv7) and int(PluginValue(thobj).GetChildMemberWithName('machine').GetChildMemberWithName('kstackptr').GetValueAsUnsigned()) != 0: 878*1031c584SApple OSS Distributions #we have stack on the machine.kstackptr. 879*1031c584SApple OSS Distributions saved_state_addr = PluginValue(thobj).GetChildMemberWithName('machine').GetChildMemberWithName('kstackptr').GetValueAsUnsigned() 880*1031c584SApple OSS Distributions regs.ReadRegisterDataFromKernelStack(saved_state_addr, self.version) 881*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 882*1031c584SApple OSS Distributions elif self.target_arch.startswith(archARMv8) and int(PluginValue(thobj).GetChildMemberWithName('machine').GetChildMemberWithName('kstackptr').GetValueAsUnsigned()) != 0: 883*1031c584SApple OSS Distributions saved_state_addr = PluginValue(thobj).GetChildMemberWithName('machine').GetChildMemberWithName('kstackptr').GetValueAsAddress() 884*1031c584SApple OSS Distributions arm_ctx = PluginValue(self.version.CreateValueFromExpression(None, '(struct arm_kernel_context *) ' + str(saved_state_addr))) 885*1031c584SApple OSS Distributions arm_ss_addr = arm_ctx.GetChildMemberWithName('ss').GetLoadAddress() 886*1031c584SApple OSS Distributions regs.ReadRegisterDataFromKernelStack(arm_ss_addr, self.version) 887*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 888*1031c584SApple OSS Distributions elif self.target_arch == archX86_64 or self.target_arch.startswith(archARMv7) or self.target_arch.startswith(archARMv8): 889*1031c584SApple OSS Distributions regs.ReadRegisterDataFromContinuation( PluginValue(thobj).GetChildMemberWithName('continuation').GetValueAsAddress()) 890*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 891*1031c584SApple OSS Distributions #incase we failed very miserably 892*1031c584SApple OSS Distributions except KeyboardInterrupt as ke: 893*1031c584SApple OSS Distributions print("OS Plugin Interrupted during thread register load. \nWARNING:Thread registers and backtraces may not be accurate. for tid = %d" % tid) 894*1031c584SApple OSS Distributions regs.ResetRegisterValues() 895*1031c584SApple OSS Distributions print("FATAL ERROR: Failed to get register state for thread id 0x%x " % tid) 896*1031c584SApple OSS Distributions print(thobj) 897*1031c584SApple OSS Distributions return regs.GetPackedRegisterState() 898*1031c584SApple OSS Distributions 899*1031c584SApple OSS Distributions def _strip_thread_tbi(self, th): 900*1031c584SApple OSS Distributions if not self.kasan_tbi: 901*1031c584SApple OSS Distributions return th 902*1031c584SApple OSS Distributions addr = th.GetValueAsAddress() 903*1031c584SApple OSS Distributions return self.version.CreateValueFromExpression(str(addr), '(struct thread *)' + str(addr))