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