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