-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdtrace.py
More file actions
executable file
·204 lines (160 loc) · 5.38 KB
/
Copy pathdtrace.py
File metadata and controls
executable file
·204 lines (160 loc) · 5.38 KB
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
#!/usr/bin/env python2.7
"""
Simple Python based DTrace consumers which executes a Hello World D script
using libdtrace and ctypes to access it.
"""
from __future__ import print_function
from ctypes import cdll, CDLL, Structure, c_void_p, c_char_p, c_uint, c_int, \
c_char, CFUNCTYPE, POINTER, byref, cast
import time
SCRIPT_COUNT = 'dtrace:::BEGIN {trace("Hello World");} \
syscall:::entry { @num[execname] = count(); }'
class dtrace_aggdesc(Structure):
"""
TODO - use offset
"""
_fields_ = [("dtagd_flags", c_int)]
class dtrace_aggdata(Structure):
"""
As defined in dtrace.h:351
"""
_fields_ = [("dtada_handle", c_void_p),
("dtada_desc", dtrace_aggdesc),
("dtada_edesc", c_void_p),
("dtada_pdesc", c_void_p),
("dtada_data", c_void_p),
("dtada_normal", c_void_p),
("dtada_size", c_int),
("dtada_delta", c_void_p),
("dtada_percpu", c_void_p),
("dtada_percpu_delta", c_void_p)]
class dtrace_bufdata(Structure):
"""
As defined in dtrace.h:310
"""
_fields_ = [("dtbda_handle", c_void_p),
("dtbda_buffered", c_char_p), # works
("dtbda_probe", c_void_p),
("dtbda_recdesc", c_void_p),
("dtbda_aggdata", c_void_p),
("dtbda_flags", c_uint)]
class dtrace_probedesc(Structure):
"""
As defined in sys/dtrace.h:884
"""
_fields_ = [("dtrace_id_t", c_uint),
("dtpd_provider", c_char),
("dtpd_mod", c_char),
("dtpd_func", c_char_p),
("dtpd_name", c_char_p)]
class dtrace_probedata(Structure):
"""
As defined in dtrace.h:186
"""
_fields_ = [("dtpda_handle", c_void_p),
("dtpda_edesc", c_void_p),
("dtpda_pdesc", dtrace_probedesc),
("dtpda_cpu", c_int), # works
("dtpda_data", c_void_p),
("dtpda_flow", c_void_p),
("dtpda_prefix", c_void_p),
("dtpda_indent", c_int)]
class dtrace_recdesc(Structure):
"""
As defined in sys/dtrace.h:931
"""
pass
def deref(addr, typ):
"""
Deref a pointer.
"""
return cast(addr, POINTER(typ)).contents
CHEW_FUNC = CFUNCTYPE(c_int,
POINTER(dtrace_probedata),
POINTER(c_void_p))
CHEWREC_FUNC = CFUNCTYPE(c_int,
POINTER(dtrace_probedata),
POINTER(dtrace_recdesc),
POINTER(c_void_p))
BUFFERED_FUNC = CFUNCTYPE(c_int,
POINTER(dtrace_bufdata),
POINTER(c_void_p))
WALK_FUNC = CFUNCTYPE(c_int,
POINTER(dtrace_aggdata),
POINTER(c_void_p))
cdll.LoadLibrary("libdtrace.so")
LIBRARY = CDLL("libdtrace.so")
def chew_func(data, arg):
"""
Callback for chew.
"""
print('+--> In chew: cpu :', c_int(data.contents.dtpda_cpu).value)
return 0
def chewrec_func(data, rec, arg):
"""
Callback for record chewing.
"""
if rec is None:
return 1
return 0
def buffered_stdout_writer(bufdata, arg):
"""
In case dtrace_work is given None as filename - this one is called.
"""
tmp = c_char_p(bufdata.contents.dtbda_buffered).value.strip()
print(' +--> In buffered_stdout_writer: ', tmp)
return 0
def walk(data, arg):
"""
Aggregate walker.
"""
# TODO: pickup the 16 and 272 from offset in desc...
tmp = data.contents.dtada_data
name = cast(tmp + 16, c_char_p).value
instance = deref(tmp + 272, c_int).value
print('+--> walking', name, instance)
return 0
def run_dtrace():
"""
Go for it...
"""
# get dtrace handle
handle = LIBRARY.dtrace_open(3, 0, byref(c_int(0)))
# options
if LIBRARY.dtrace_setopt(handle, "bufsize", "4m") != 0:
txt = LIBRARY.dtrace_errmsg(handle, LIBRARY.dtrace_errno(handle))
raise Exception(c_char_p(txt).value)
if LIBRARY.dtrace_setopt(handle, "aggsize", "4m") != 0:
txt = LIBRARY.dtrace_errmsg(handle, LIBRARY.dtrace_errno(handle))
raise Exception(c_char_p(txt).value)
# callbacks
buf_func = BUFFERED_FUNC(buffered_stdout_writer)
LIBRARY.dtrace_handle_buffered(handle, buf_func, None)
# compile
prg = LIBRARY.dtrace_program_strcompile(handle,
SCRIPT_COUNT, 3, 4, 0, None)
# run
LIBRARY.dtrace_program_exec(handle, prg, None)
LIBRARY.dtrace_go(handle)
# aggregate data for a few sec...
i = 0
chew = CHEW_FUNC(chew_func)
chew_rec = CHEWREC_FUNC(chewrec_func)
while i < 2:
LIBRARY.dtrace_sleep(handle)
LIBRARY.dtrace_work(handle, None, chew, chew_rec, None)
time.sleep(1)
i += 1
LIBRARY.dtrace_stop(handle)
walk_func = WALK_FUNC(walk)
# sorting instead of dtrace_aggregate_walk
if LIBRARY.dtrace_aggregate_walk_valsorted(handle, walk_func, None) != 0:
txt = LIBRARY.dtrace_errmsg(handle, LIBRARY.dtrace_errno(handle))
raise Exception(c_char_p(txt).value)
# Get errors if any...
txt = LIBRARY.dtrace_errmsg(handle, LIBRARY.dtrace_errno(handle))
print(c_char_p(txt).value)
# Last: close handle!
LIBRARY.dtrace_close(handle)
if __name__ == '__main__':
run_dtrace()