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