• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 PLUMgrid
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import ctypes as ct
16
17lib = ct.CDLL("libbcc.so.0", use_errno=True)
18
19# keep in sync with bpf_common.h
20lib.bpf_module_create_b.restype = ct.c_void_p
21lib.bpf_module_create_b.argtypes = [ct.c_char_p, ct.c_char_p, ct.c_uint]
22lib.bpf_module_create_c.restype = ct.c_void_p
23lib.bpf_module_create_c.argtypes = [ct.c_char_p, ct.c_uint,
24        ct.POINTER(ct.c_char_p), ct.c_int]
25lib.bpf_module_create_c_from_string.restype = ct.c_void_p
26lib.bpf_module_create_c_from_string.argtypes = [ct.c_char_p, ct.c_uint,
27        ct.POINTER(ct.c_char_p), ct.c_int]
28lib.bpf_module_destroy.restype = None
29lib.bpf_module_destroy.argtypes = [ct.c_void_p]
30lib.bpf_module_license.restype = ct.c_char_p
31lib.bpf_module_license.argtypes = [ct.c_void_p]
32lib.bpf_module_kern_version.restype = ct.c_uint
33lib.bpf_module_kern_version.argtypes = [ct.c_void_p]
34lib.bpf_num_functions.restype = ct.c_ulonglong
35lib.bpf_num_functions.argtypes = [ct.c_void_p]
36lib.bpf_function_name.restype = ct.c_char_p
37lib.bpf_function_name.argtypes = [ct.c_void_p, ct.c_ulonglong]
38lib.bpf_function_start.restype = ct.c_void_p
39lib.bpf_function_start.argtypes = [ct.c_void_p, ct.c_char_p]
40lib.bpf_function_size.restype = ct.c_size_t
41lib.bpf_function_size.argtypes = [ct.c_void_p, ct.c_char_p]
42lib.bpf_table_id.restype = ct.c_ulonglong
43lib.bpf_table_id.argtypes = [ct.c_void_p, ct.c_char_p]
44lib.bpf_table_fd.restype = ct.c_int
45lib.bpf_table_fd.argtypes = [ct.c_void_p, ct.c_char_p]
46lib.bpf_table_type_id.restype = ct.c_int
47lib.bpf_table_type_id.argtypes = [ct.c_void_p, ct.c_ulonglong]
48lib.bpf_table_max_entries_id.restype = ct.c_ulonglong
49lib.bpf_table_max_entries_id.argtypes = [ct.c_void_p, ct.c_ulonglong]
50lib.bpf_table_flags_id.restype = ct.c_int
51lib.bpf_table_flags_id.argtypes = [ct.c_void_p, ct.c_ulonglong]
52lib.bpf_table_key_desc.restype = ct.c_char_p
53lib.bpf_table_key_desc.argtypes = [ct.c_void_p, ct.c_char_p]
54lib.bpf_table_leaf_desc.restype = ct.c_char_p
55lib.bpf_table_leaf_desc.argtypes = [ct.c_void_p, ct.c_char_p]
56lib.bpf_table_key_snprintf.restype = ct.c_int
57lib.bpf_table_key_snprintf.argtypes = [ct.c_void_p, ct.c_ulonglong,
58        ct.c_char_p, ct.c_ulonglong, ct.c_void_p]
59lib.bpf_table_leaf_snprintf.restype = ct.c_int
60lib.bpf_table_leaf_snprintf.argtypes = [ct.c_void_p, ct.c_ulonglong,
61        ct.c_char_p, ct.c_ulonglong, ct.c_void_p]
62lib.bpf_table_key_sscanf.restype = ct.c_int
63lib.bpf_table_key_sscanf.argtypes = [ct.c_void_p, ct.c_ulonglong,
64        ct.c_char_p, ct.c_void_p]
65lib.bpf_table_leaf_sscanf.restype = ct.c_int
66lib.bpf_table_leaf_sscanf.argtypes = [ct.c_void_p, ct.c_ulonglong,
67        ct.c_char_p, ct.c_void_p]
68
69# keep in sync with libbpf.h
70lib.bpf_get_next_key.restype = ct.c_int
71lib.bpf_get_next_key.argtypes = [ct.c_int, ct.c_void_p, ct.c_void_p]
72lib.bpf_get_first_key.restype = ct.c_int
73lib.bpf_get_first_key.argtypes = [ct.c_int, ct.c_void_p, ct.c_uint]
74lib.bpf_lookup_elem.restype = ct.c_int
75lib.bpf_lookup_elem.argtypes = [ct.c_int, ct.c_void_p, ct.c_void_p]
76lib.bpf_update_elem.restype = ct.c_int
77lib.bpf_update_elem.argtypes = [ct.c_int, ct.c_void_p, ct.c_void_p,
78        ct.c_ulonglong]
79lib.bpf_delete_elem.restype = ct.c_int
80lib.bpf_delete_elem.argtypes = [ct.c_int, ct.c_void_p]
81lib.bpf_open_raw_sock.restype = ct.c_int
82lib.bpf_open_raw_sock.argtypes = [ct.c_char_p]
83lib.bpf_attach_socket.restype = ct.c_int
84lib.bpf_attach_socket.argtypes = [ct.c_int, ct.c_int]
85lib.bpf_prog_load.restype = ct.c_int
86lib.bpf_prog_load.argtypes = [ct.c_int, ct.c_char_p, ct.c_void_p,
87        ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_int, ct.c_char_p, ct.c_uint]
88_RAW_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_void_p, ct.c_int)
89_LOST_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_ulonglong)
90lib.bpf_attach_kprobe.restype = ct.c_int
91lib.bpf_attach_kprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p]
92lib.bpf_detach_kprobe.restype = ct.c_int
93lib.bpf_detach_kprobe.argtypes = [ct.c_char_p]
94lib.bpf_attach_uprobe.restype = ct.c_int
95lib.bpf_attach_uprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p,
96        ct.c_ulonglong, ct.c_int]
97lib.bpf_detach_uprobe.restype = ct.c_int
98lib.bpf_detach_uprobe.argtypes = [ct.c_char_p]
99lib.bpf_attach_tracepoint.restype = ct.c_int
100lib.bpf_attach_tracepoint.argtypes = [ct.c_int, ct.c_char_p, ct.c_char_p]
101lib.bpf_detach_tracepoint.restype = ct.c_int
102lib.bpf_detach_tracepoint.argtypes = [ct.c_char_p, ct.c_char_p]
103lib.bpf_attach_raw_tracepoint.restype = ct.c_int
104lib.bpf_attach_raw_tracepoint.argtypes = [ct.c_int, ct.c_char_p]
105lib.bpf_open_perf_buffer.restype = ct.c_void_p
106lib.bpf_open_perf_buffer.argtypes = [_RAW_CB_TYPE, _LOST_CB_TYPE, ct.py_object, ct.c_int, ct.c_int, ct.c_int]
107lib.bpf_open_perf_event.restype = ct.c_int
108lib.bpf_open_perf_event.argtypes = [ct.c_uint, ct.c_ulonglong, ct.c_int, ct.c_int]
109lib.perf_reader_poll.restype = ct.c_int
110lib.perf_reader_poll.argtypes = [ct.c_int, ct.POINTER(ct.c_void_p), ct.c_int]
111lib.perf_reader_free.restype = None
112lib.perf_reader_free.argtypes = [ct.c_void_p]
113lib.perf_reader_fd.restype = int
114lib.perf_reader_fd.argtypes = [ct.c_void_p]
115
116lib.bpf_attach_xdp.restype = ct.c_int
117lib.bpf_attach_xdp.argtypes = [ct.c_char_p, ct.c_int, ct.c_uint]
118
119lib.bpf_attach_perf_event.restype = ct.c_int
120lib.bpf_attach_perf_event.argtype = [ct.c_int, ct.c_uint, ct.c_uint, ct.c_ulonglong, ct.c_ulonglong,
121        ct.c_int, ct.c_int, ct.c_int]
122
123lib.bpf_close_perf_event_fd.restype = ct.c_int
124lib.bpf_close_perf_event_fd.argtype = [ct.c_int]
125
126# bcc symbol helpers
127class bcc_symbol(ct.Structure):
128    _fields_ = [
129            ('name', ct.c_char_p),
130            ('demangle_name', ct.c_char_p),
131            ('module', ct.POINTER(ct.c_char)),
132            ('offset', ct.c_ulonglong),
133        ]
134
135class bcc_symbol_option(ct.Structure):
136    _fields_ = [
137            ('use_debug_file', ct.c_int),
138            ('check_debug_file_crc', ct.c_int),
139            ('use_symbol_type', ct.c_uint),
140        ]
141
142lib.bcc_procutils_which_so.restype = ct.POINTER(ct.c_char)
143lib.bcc_procutils_which_so.argtypes = [ct.c_char_p, ct.c_int]
144lib.bcc_procutils_free.restype = None
145lib.bcc_procutils_free.argtypes = [ct.c_void_p]
146lib.bcc_procutils_language.restype = ct.POINTER(ct.c_char)
147lib.bcc_procutils_language.argtypes = [ct.c_int]
148
149lib.bcc_resolve_symname.restype = ct.c_int
150lib.bcc_resolve_symname.argtypes = [
151    ct.c_char_p, ct.c_char_p, ct.c_ulonglong, ct.c_int, ct.POINTER(bcc_symbol_option), ct.POINTER(bcc_symbol)]
152
153_SYM_CB_TYPE = ct.CFUNCTYPE(ct.c_int, ct.c_char_p, ct.c_ulonglong)
154lib.bcc_foreach_function_symbol.restype = ct.c_int
155lib.bcc_foreach_function_symbol.argtypes = [ct.c_char_p, _SYM_CB_TYPE]
156
157lib.bcc_symcache_new.restype = ct.c_void_p
158lib.bcc_symcache_new.argtypes = [ct.c_int, ct.POINTER(bcc_symbol_option)]
159
160lib.bcc_free_symcache.restype = ct.c_void_p
161lib.bcc_free_symcache.argtypes = [ct.c_void_p, ct.c_int]
162
163lib.bcc_symbol_free_demangle_name.restype = ct.c_void_p
164lib.bcc_symbol_free_demangle_name.argtypes = [ct.POINTER(bcc_symbol)]
165
166lib.bcc_symcache_resolve.restype = ct.c_int
167lib.bcc_symcache_resolve.argtypes = [ct.c_void_p, ct.c_ulonglong, ct.POINTER(bcc_symbol)]
168
169lib.bcc_symcache_resolve_no_demangle.restype = ct.c_int
170lib.bcc_symcache_resolve_no_demangle.argtypes = [ct.c_void_p, ct.c_ulonglong, ct.POINTER(bcc_symbol)]
171
172lib.bcc_symcache_resolve_name.restype = ct.c_int
173lib.bcc_symcache_resolve_name.argtypes = [
174    ct.c_void_p, ct.c_char_p, ct.c_char_p, ct.POINTER(ct.c_ulonglong)]
175
176lib.bcc_symcache_refresh.restype = None
177lib.bcc_symcache_refresh.argtypes = [ct.c_void_p]
178
179lib.bcc_usdt_new_frompid.restype = ct.c_void_p
180lib.bcc_usdt_new_frompid.argtypes = [ct.c_int, ct.c_char_p]
181
182lib.bcc_usdt_new_frompath.restype = ct.c_void_p
183lib.bcc_usdt_new_frompath.argtypes = [ct.c_char_p]
184
185lib.bcc_usdt_close.restype = None
186lib.bcc_usdt_close.argtypes = [ct.c_void_p]
187
188lib.bcc_usdt_enable_probe.restype = ct.c_int
189lib.bcc_usdt_enable_probe.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_char_p]
190
191lib.bcc_usdt_genargs.restype = ct.c_char_p
192lib.bcc_usdt_genargs.argtypes = [ct.POINTER(ct.c_void_p), ct.c_int]
193
194lib.bcc_usdt_get_probe_argctype.restype = ct.c_char_p
195lib.bcc_usdt_get_probe_argctype.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_int]
196
197class bcc_usdt(ct.Structure):
198    _fields_ = [
199            ('provider', ct.c_char_p),
200            ('name', ct.c_char_p),
201            ('bin_path', ct.c_char_p),
202            ('semaphore', ct.c_ulonglong),
203            ('num_locations', ct.c_int),
204            ('num_arguments', ct.c_int),
205        ]
206
207class bcc_usdt_location(ct.Structure):
208    _fields_ = [
209            ('address', ct.c_ulonglong),
210            ('bin_path', ct.c_char_p),
211        ]
212
213class BCC_USDT_ARGUMENT_FLAGS(object):
214    NONE = 0x0
215    CONSTANT = 0x1
216    DEREF_OFFSET = 0x2
217    DEREF_IDENT = 0x4
218    BASE_REGISTER_NAME = 0x8
219    INDEX_REGISTER_NAME = 0x10
220    SCALE = 0x20
221
222class bcc_usdt_argument(ct.Structure):
223    _fields_ = [
224            ('size', ct.c_int),
225            ('valid', ct.c_int),
226            ('constant', ct.c_int),
227            ('deref_offset', ct.c_int),
228            ('deref_ident', ct.c_char_p),
229            ('base_register_name', ct.c_char_p),
230            ('index_register_name', ct.c_char_p),
231            ('scale', ct.c_int)
232        ]
233
234_USDT_CB = ct.CFUNCTYPE(None, ct.POINTER(bcc_usdt))
235
236lib.bcc_usdt_foreach.restype = None
237lib.bcc_usdt_foreach.argtypes = [ct.c_void_p, _USDT_CB]
238
239lib.bcc_usdt_get_location.restype = ct.c_int
240lib.bcc_usdt_get_location.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_char_p, ct.c_int,
241                                      ct.POINTER(bcc_usdt_location)]
242
243lib.bcc_usdt_get_argument.restype = ct.c_int
244lib.bcc_usdt_get_argument.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_char_p, ct.c_int,
245                                      ct.c_int, ct.POINTER(bcc_usdt_argument)]
246
247_USDT_PROBE_CB = ct.CFUNCTYPE(None, ct.c_char_p, ct.c_char_p,
248                              ct.c_ulonglong, ct.c_int)
249
250lib.bcc_usdt_foreach_uprobe.restype = None
251lib.bcc_usdt_foreach_uprobe.argtypes = [ct.c_void_p, _USDT_PROBE_CB]
252