forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kevent.py
executable file
·381 lines (315 loc) · 13.6 KB
/
kevent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from xnu import *
def IterateProcKqueues(proc):
""" Iterate through all kqueues in the given process
params:
proc - the proc object
returns: nothing, this is meant to be used as a generator function
kq - yields each kqueue in the process
"""
for kqf in IterateProcKqfiles(proc):
yield kern.GetValueFromAddress(int(kqf), 'struct kqueue *')
if int(proc.p_fd.fd_wqkqueue) != 0:
yield kern.GetValueFromAddress(int(proc.p_fd.fd_wqkqueue), 'struct kqueue *')
for kqwl in IterateProcKqworkloops(proc):
yield kern.GetValueFromAddress(int(kqwl), 'struct kqueue *')
def IterateProcKqfiles(proc):
""" Iterate through all kqfiles in the given process
params:
proc - the proc object
returns: nothing, this is meant to be used as a generator function
kqf - yields each kqfile in the process
"""
filetype_KQUEUE = 5
proc_filedesc = proc.p_fd
proc_lastfile = unsigned(proc_filedesc.fd_lastfile)
proc_ofiles = proc_filedesc.fd_ofiles
queues = list()
count = 0
if unsigned(proc_ofiles) == 0:
return
while count <= proc_lastfile:
if unsigned(proc_ofiles[count]) != 0:
proc_fd_flags = proc_ofiles[count].f_flags
proc_fd_fglob = proc_ofiles[count].f_fglob
proc_fd_ftype = unsigned(proc_fd_fglob.fg_ops.fo_type)
if proc_fd_ftype == xnudefines.DTYPE_KQUEUE:
yield kern.GetValueFromAddress(int(proc_fd_fglob.fg_data), 'struct kqfile *')
count += 1
def IterateProcKqworkloops(proc):
""" Iterate through all kqworkloops in the given process
params:
proc - the proc object
returns: nothing, this is meant to be used as a generator function
kqwl - yields each kqworkloop in the process
"""
proc_filedesc = proc.p_fd
if int(proc_filedesc.fd_kqhash) == 0:
return
hash_mask = proc_filedesc.fd_kqhashmask
for i in xrange(hash_mask + 1):
for kqwl in IterateListEntry(proc_filedesc.fd_kqhash[i], 'struct kqworkloop *', 'kqwl_hashlink', list_prefix='s'):
yield kqwl
def IterateAllKqueues():
""" Iterate through all kqueues in the system
returns: nothing, this is meant to be used as a generator function
kq - yields each kqueue in the system
"""
for t in kern.tasks:
if unsigned(t.bsd_info) == 0:
continue
proc = kern.GetValueFromAddress(t.bsd_info, 'proc_t')
for kq in IterateProcKqueues(proc):
yield kq
def IterateProcKnotes(proc):
""" Iterate through all knotes in the given process
params:
proc - the proc object
returns: nothing, this is meant to be used as a generator function
kn - yields each knote in the process
"""
proc_filedesc = proc.p_fd
if int(proc.p_fd.fd_knlist) != 0:
for i in xrange(proc.p_fd.fd_knlistsize):
for kn in IterateListEntry(proc.p_fd.fd_knlist[i], 'struct knote *', 'kn_link', list_prefix='s'):
yield kn
if int(proc.p_fd.fd_knhash) != 0:
for i in xrange(proc.p_fd.fd_knhashmask + 1):
for kn in IterateListEntry(proc.p_fd.fd_knhash[i], 'struct knote *', 'kn_link', list_prefix='s'):
yield kn
def GetKnoteKqueue(kn):
""" Get the kqueue corresponding to a given knote
params:
kn - the knote object
returns: kq - the kqueue corresponding to the knote
"""
return kern.GetValueFromAddress(kn.kn_kq_packed + kern.VM_MIN_KERNEL_AND_KEXT_ADDRESS, 'struct kqueue *')
@lldb_type_summary(['knote *'])
@header('{:<20s} {:<20s} {:<10s} {:<20s} {:<20s} {:<30s} {:<10} {:<10} {:<10} {:<30s}'.format('knote', 'ident', 'kev_flags', 'kqueue', 'udata', 'filtops', 'qos_use', 'qos_req', 'qos_ovr', 'status'))
def GetKnoteSummary(kn):
""" Summarizes a knote and related information
returns: str - summary of knote
"""
format_string = '{o: <#020x} {o.kn_kevent.ident: <#020x} {o.kn_kevent.flags: <#010x} {kq_ptr: <#020x} {o.kn_kevent.udata: <#020x} {ops_str: <30s} {qos_use: <10s} {qos_req: <10s} {qos_ovr: <10s} {st_str: <30s}'
state = unsigned(kn.kn_status)
fops_str = kern.Symbolicate(kern.globals.sysfilt_ops[unsigned(kn.kn_filtid)])
return format_string.format(
o=kn,
qos_use=xnudefines.thread_qos_short_strings[int(kn.kn_qos_index)],
qos_req=xnudefines.thread_qos_short_strings[int(kn.kn_req_index)],
qos_ovr=xnudefines.thread_qos_short_strings[int(kn.kn_qos_override)],
st_str=xnudefines.GetStateString(xnudefines.kn_state_strings, state),
kq_ptr=int(GetKnoteKqueue(kn)),
ops_str=fops_str)
@lldb_command('showknote')
def ShowKnote(cmd_args=None):
""" Show information about a knote
usage: showknote <struct knote *>
"""
if not cmd_args:
raise ArgumentError('missing struct knote * argument')
kn = kern.GetValueFromAddress(cmd_args[0], 'struct knote *')
print GetKnoteSummary.header
print GetKnoteSummary(kn)
def IterateKqueueKnotes(kq):
""" Iterate through all knotes of a given kqueue
params:
kq - the kqueue to iterate the knotes of
returns: nothing, this is meant to be used as a generator function
kn - yields each knote in the kqueue
"""
proc = kq.kq_p
for kn in IterateProcKnotes(proc):
if unsigned(GetKnoteKqueue(kn)) != unsigned(addressof(kq)):
continue
yield kn
@lldb_type_summary(['struct kqrequest *'])
@header('{:<20s} {:<20s} {:<5s} {:<5s} {:<5s} {:<5s} {:s}'.format('kqrequest', 'thread', 'qos', 'ovr_qos', 'w_qos', 'sa_qos', 'state'))
def GetKqrequestSummary(kqr):
""" Summarize kqrequest information
params:
kqr - the kqrequest object
returns: str - summary of kqrequest
"""
fmt = '{kqrp: <#020x} {kqr.kqr_bound.kqrb_thread: <#020x} {qos: <5s} {ovr_qos: <5s} {w_qos: <5s} {sa_qos: <5s} {state_str:<s}'
return fmt.format(kqrp=int(kqr),
kqr=kqr,
qos=xnudefines.thread_qos_short_strings[int(kqr.kqr_qos_index)],
ovr_qos=xnudefines.thread_qos_short_strings[int(kqr.kqr_override_index)],
w_qos=xnudefines.thread_qos_short_strings[int(kqr.kqr_dsync_waiters_qos)],
sa_qos=xnudefines.thread_qos_short_strings[int(kqr.kqr_stayactive_qos)],
state_str=xnudefines.GetStateString(xnudefines.kqrequest_state_strings, kqr.kqr_state))
@lldb_command('showkqrequest')
def ShowKqrequest(cmd_args=None):
""" Display information about a kqrequest object.
usage: showkqrequest <struct kqrequest *>
"""
if len(cmd_args) < 1:
raise ArgumentError('missing struct kqrequest * argument')
kqr = kern.GetValueFromAddress(cmd_args[0], 'struct kqrequest *')
print GetKqrequestSummary.header
print GetKqrequestSummary(kqr)
print GetKnoteSummary.header
for kn in IterateTAILQ_HEAD(kqr.kqr_suppressed, 'kn_tqe'):
print GetKnoteSummary(kn)
kqueue_summary_fmt = '{ptr: <#020x} {o.kq_p: <#020x} {dyn_id: <#020x} {servicer: <#20x} {owner: <#20x} {o.kq_count: <6d} {wqs: <#020x} {kqr_state: <30s} {st_str: <10s}'
@lldb_type_summary(['struct kqueue *'])
@header('{: <20s} {: <20s} {: <20s} {: <20s} {: <20s} {: <6s} {: <20s} {: <30s} {: <10s}'.format('kqueue', 'process', 'dynamic_id', 'servicer', 'owner', '#evts', 'wqs', 'request', 'state'))
def GetKqueueSummary(kq):
""" Summarize kqueue information
params:
kq - the kqueue object
returns: str - summary of kqueue
"""
if kq.kq_state & xnudefines.KQ_WORKLOOP:
return GetKqworkloopSummary(kern.GetValueFromAddress(int(kq), 'struct kqworkloop *'))
elif kq.kq_state & xnudefines.KQ_WORKQ:
return GetKqworkqSummary(kern.GetValueFromAddress(int(kq), 'struct kqworkq *'))
else:
return GetKqfileSummary(kern.GetValueFromAddress(int(kq), 'struct kqfile *'))
@lldb_type_summary(['struct kqfile *'])
@header(GetKqueueSummary.header)
def GetKqfileSummary(kqf):
kq = kern.GetValueFromAddress(int(kqf), 'struct kqueue *')
state = int(kq.kq_state)
return kqueue_summary_fmt.format(
o=kq,
ptr=int(kq),
wqs=int(kq.kq_wqs),
kqr_state='',
dyn_id=0,
st_str=xnudefines.GetStateString(xnudefines.kq_state_strings, state),
servicer=0,
owner=0)
@lldb_command('showkqfile')
def ShowKqfile(cmd_args=None):
""" Display information about a kqfile object.
usage: showkqfile <struct kqfile *>
"""
if len(cmd_args) < 1:
raise ArgumentError('missing struct kqfile * argument')
kqf = kern.GetValueFromAddress(cmd_args[0], 'kqfile *')
print GetKqfileSummary.header
print GetKqfileSummary(kqf)
print GetKnoteSummary.header
for kn in IterateKqueueKnotes(kqf.kqf_kqueue):
print GetKnoteSummary(kn)
for kn in IterateTAILQ_HEAD(kqf.kqf_suppressed, 'kn_tqe'):
print GetKnoteSummary(kn)
@lldb_type_summary(['struct kqworkq *'])
@header(GetKqueueSummary.header)
def GetKqworkqSummary(kqwq):
""" Summarize workqueue kqueue information
params:
kqwq - the kqworkq object
returns: str - summary of workqueue kqueue
"""
return GetKqfileSummary(kern.GetValueFromAddress(int(kqwq), 'struct kqfile *'))
@lldb_command('showkqworkq')
def ShowKqworkq(cmd_args=None):
""" Display summary and knote information about a kqworkq.
usage: showkqworkq <struct kqworkq *>
"""
if len(cmd_args) < 1:
raise ArgumentError('missing struct kqworkq * argument')
kqwq = kern.GetValueFromAddress(cmd_args[0], 'struct kqworkq *')
kq = kqwq.kqwq_kqueue
print GetKqueueSummary.header
print GetKqworkqSummary(kqwq)
print GetKnoteSummary.header
for kn in IterateKqueueKnotes(kq):
print GetKnoteSummary(kn)
for i in xrange(0, xnudefines.KQWQ_NBUCKETS):
for kn in IterateTAILQ_HEAD(kq.kq_queue[i], 'kn_tqe'):
print GetKnoteSummary(kn)
@lldb_type_summary(['struct kqworkloop *'])
@header(GetKqueueSummary.header)
def GetKqworkloopSummary(kqwl):
""" Summarize workloop kqueue information
params:
kqwl - the kqworkloop object
returns: str - summary of workloop kqueue
"""
state = int(kqwl.kqwl_kqueue.kq_state)
return kqueue_summary_fmt.format(
ptr=int(kqwl),
o=kqwl.kqwl_kqueue,
wqs=int(kqwl.kqwl_kqueue.kq_wqs),
dyn_id=kqwl.kqwl_dynamicid,
kqr_state=xnudefines.GetStateString(xnudefines.kqrequest_state_strings, kqwl.kqwl_request.kqr_state),
st_str=xnudefines.GetStateString(xnudefines.kq_state_strings, state),
servicer=int(kqwl.kqwl_request.kqr_bound.kqrb_thread),
owner=int(kqwl.kqwl_owner)
)
@lldb_command('showkqworkloop')
def ShowKqworkloop(cmd_args=None):
""" Display information about a kqworkloop.
usage: showkqworkloop <struct kqworkloop *>
"""
if len(cmd_args) < 1:
raise ArgumentError('missing struct kqworkloop * argument')
kqwl = kern.GetValueFromAddress(cmd_args[0], 'struct kqworkloop *')
print GetKqworkloopSummary.header
print GetKqworkloopSummary(kqwl)
print GetKqrequestSummary.header
kqr = kern.GetValueFromAddress(unsigned(addressof(kqwl.kqwl_request)), 'struct kqrequest *')
print GetKqrequestSummary(kqr)
print GetKnoteSummary.header
for kn in IterateKqueueKnotes(kqwl.kqwl_kqueue):
print GetKnoteSummary(kn)
@lldb_command('showkqueue')
def ShowKqueue(cmd_args=None):
""" Given a struct kqueue pointer, display the summary of the kqueue
usage: showkqueue <struct kqueue *>
"""
if not cmd_args:
raise ArgumentError('missing struct kqueue * argument')
kq = kern.GetValueFromAddress(cmd_args[0], 'struct kqueue *')
if int(kq.kq_state) & xnudefines.KQ_WORKQ:
ShowKqworkq(cmd_args=[str(int(kq))])
elif int(kq.kq_state) & xnudefines.KQ_WORKLOOP:
ShowKqworkloop(cmd_args=[str(int(kq))])
else:
print GetKqueueSummary.header
print GetKqueueSummary(kq)
print GetKnoteSummary.header
for kn in IterateKqueueKnotes(kq):
print GetKnoteSummary(kn)
@lldb_command('showprocworkqkqueue')
def ShowProcWorkqKqueue(cmd_args=None):
""" Show the workqueue kqueue for a given process.
usage: showworkqkqueue <proc_t>
"""
if not cmd_args:
raise ArgumentError('missing struct proc * argument')
proc = kern.GetValueFromAddress(cmd_args[0], 'proc_t')
ShowKqworkq(cmd_args=[str(int(proc.p_fd.fd_wqkqueue))])
@lldb_command('showprockqueues')
def ShowProcKqueues(cmd_args=None):
""" Show the kqueues for a given process.
usage: showprockqueues <proc_t>
"""
if not cmd_args:
raise ArgumentError('missing struct proc * argument')
proc = kern.GetValueFromAddress(cmd_args[0], 'proc_t')
print GetKqueueSummary.header
for kq in IterateProcKqueues(proc):
print GetKqueueSummary(kq)
@lldb_command('showprocknotes')
def ShowProcKnotes(cmd_args=None):
""" Show the knotes for a given process.
usage: showprocknotes <proc_t>
"""
if not cmd_args:
raise ArgumentError('missing struct proc * argument')
proc = kern.GetValueFromAddress(cmd_args[0], 'proc_t')
print GetKnoteSummary.header
for kn in IterateProcKnotes(proc):
print GetKnoteSummary(kn)
@lldb_command('showallkqueues')
def ShowAllKqueues(cmd_args=[], cmd_options={}):
""" Display a summary of all the kqueues in the system
usage: showallkqueues
"""
print GetKqueueSummary.header
for kq in IterateAllKqueues():
print GetKqueueSummary(kq)