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