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