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