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, 92 ct.c_ulonglong] 93lib.bpf_detach_kprobe.restype = ct.c_int 94lib.bpf_detach_kprobe.argtypes = [ct.c_char_p] 95lib.bpf_attach_uprobe.restype = ct.c_int 96lib.bpf_attach_uprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p, 97 ct.c_ulonglong, ct.c_int] 98lib.bpf_detach_uprobe.restype = ct.c_int 99lib.bpf_detach_uprobe.argtypes = [ct.c_char_p] 100lib.bpf_attach_tracepoint.restype = ct.c_int 101lib.bpf_attach_tracepoint.argtypes = [ct.c_int, ct.c_char_p, ct.c_char_p] 102lib.bpf_detach_tracepoint.restype = ct.c_int 103lib.bpf_detach_tracepoint.argtypes = [ct.c_char_p, ct.c_char_p] 104lib.bpf_attach_raw_tracepoint.restype = ct.c_int 105lib.bpf_attach_raw_tracepoint.argtypes = [ct.c_int, ct.c_char_p] 106lib.bpf_open_perf_buffer.restype = ct.c_void_p 107lib.bpf_open_perf_buffer.argtypes = [_RAW_CB_TYPE, _LOST_CB_TYPE, ct.py_object, ct.c_int, ct.c_int, ct.c_int] 108lib.bpf_open_perf_event.restype = ct.c_int 109lib.bpf_open_perf_event.argtypes = [ct.c_uint, ct.c_ulonglong, ct.c_int, ct.c_int] 110lib.perf_reader_poll.restype = ct.c_int 111lib.perf_reader_poll.argtypes = [ct.c_int, ct.POINTER(ct.c_void_p), ct.c_int] 112lib.perf_reader_free.restype = None 113lib.perf_reader_free.argtypes = [ct.c_void_p] 114lib.perf_reader_fd.restype = int 115lib.perf_reader_fd.argtypes = [ct.c_void_p] 116 117lib.bpf_attach_xdp.restype = ct.c_int 118lib.bpf_attach_xdp.argtypes = [ct.c_char_p, ct.c_int, ct.c_uint] 119 120lib.bpf_attach_perf_event.restype = ct.c_int 121lib.bpf_attach_perf_event.argtype = [ct.c_int, ct.c_uint, ct.c_uint, ct.c_ulonglong, ct.c_ulonglong, 122 ct.c_int, ct.c_int, ct.c_int] 123 124lib.bpf_close_perf_event_fd.restype = ct.c_int 125lib.bpf_close_perf_event_fd.argtype = [ct.c_int] 126 127# bcc symbol helpers 128class bcc_symbol(ct.Structure): 129 _fields_ = [ 130 ('name', ct.c_char_p), 131 ('demangle_name', ct.c_char_p), 132 ('module', ct.POINTER(ct.c_char)), 133 ('offset', ct.c_ulonglong), 134 ] 135 136class bcc_symbol_option(ct.Structure): 137 _fields_ = [ 138 ('use_debug_file', ct.c_int), 139 ('check_debug_file_crc', ct.c_int), 140 ('use_symbol_type', ct.c_uint), 141 ] 142 143lib.bcc_procutils_which_so.restype = ct.POINTER(ct.c_char) 144lib.bcc_procutils_which_so.argtypes = [ct.c_char_p, ct.c_int] 145lib.bcc_procutils_free.restype = None 146lib.bcc_procutils_free.argtypes = [ct.c_void_p] 147lib.bcc_procutils_language.restype = ct.POINTER(ct.c_char) 148lib.bcc_procutils_language.argtypes = [ct.c_int] 149 150lib.bcc_resolve_symname.restype = ct.c_int 151lib.bcc_resolve_symname.argtypes = [ 152 ct.c_char_p, ct.c_char_p, ct.c_ulonglong, ct.c_int, ct.POINTER(bcc_symbol_option), ct.POINTER(bcc_symbol)] 153 154_SYM_CB_TYPE = ct.CFUNCTYPE(ct.c_int, ct.c_char_p, ct.c_ulonglong) 155lib.bcc_foreach_function_symbol.restype = ct.c_int 156lib.bcc_foreach_function_symbol.argtypes = [ct.c_char_p, _SYM_CB_TYPE] 157 158lib.bcc_symcache_new.restype = ct.c_void_p 159lib.bcc_symcache_new.argtypes = [ct.c_int, ct.POINTER(bcc_symbol_option)] 160 161lib.bcc_free_symcache.restype = ct.c_void_p 162lib.bcc_free_symcache.argtypes = [ct.c_void_p, ct.c_int] 163 164lib.bcc_symbol_free_demangle_name.restype = ct.c_void_p 165lib.bcc_symbol_free_demangle_name.argtypes = [ct.POINTER(bcc_symbol)] 166 167lib.bcc_symcache_resolve.restype = ct.c_int 168lib.bcc_symcache_resolve.argtypes = [ct.c_void_p, ct.c_ulonglong, ct.POINTER(bcc_symbol)] 169 170lib.bcc_symcache_resolve_no_demangle.restype = ct.c_int 171lib.bcc_symcache_resolve_no_demangle.argtypes = [ct.c_void_p, ct.c_ulonglong, ct.POINTER(bcc_symbol)] 172 173lib.bcc_symcache_resolve_name.restype = ct.c_int 174lib.bcc_symcache_resolve_name.argtypes = [ 175 ct.c_void_p, ct.c_char_p, ct.c_char_p, ct.POINTER(ct.c_ulonglong)] 176 177lib.bcc_symcache_refresh.restype = None 178lib.bcc_symcache_refresh.argtypes = [ct.c_void_p] 179 180lib.bcc_free_memory.restype = ct.c_int 181lib.bcc_free_memory.argtypes = None 182 183lib.bcc_usdt_new_frompid.restype = ct.c_void_p 184lib.bcc_usdt_new_frompid.argtypes = [ct.c_int, ct.c_char_p] 185 186lib.bcc_usdt_new_frompath.restype = ct.c_void_p 187lib.bcc_usdt_new_frompath.argtypes = [ct.c_char_p] 188 189lib.bcc_usdt_close.restype = None 190lib.bcc_usdt_close.argtypes = [ct.c_void_p] 191 192lib.bcc_usdt_enable_probe.restype = ct.c_int 193lib.bcc_usdt_enable_probe.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_char_p] 194 195lib.bcc_usdt_genargs.restype = ct.c_char_p 196lib.bcc_usdt_genargs.argtypes = [ct.POINTER(ct.c_void_p), ct.c_int] 197 198lib.bcc_usdt_get_probe_argctype.restype = ct.c_char_p 199lib.bcc_usdt_get_probe_argctype.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_int] 200 201class bcc_usdt(ct.Structure): 202 _fields_ = [ 203 ('provider', ct.c_char_p), 204 ('name', ct.c_char_p), 205 ('bin_path', ct.c_char_p), 206 ('semaphore', ct.c_ulonglong), 207 ('num_locations', ct.c_int), 208 ('num_arguments', ct.c_int), 209 ] 210 211class bcc_usdt_location(ct.Structure): 212 _fields_ = [ 213 ('address', ct.c_ulonglong), 214 ('bin_path', ct.c_char_p), 215 ] 216 217class BCC_USDT_ARGUMENT_FLAGS(object): 218 NONE = 0x0 219 CONSTANT = 0x1 220 DEREF_OFFSET = 0x2 221 DEREF_IDENT = 0x4 222 BASE_REGISTER_NAME = 0x8 223 INDEX_REGISTER_NAME = 0x10 224 SCALE = 0x20 225 226class bcc_usdt_argument(ct.Structure): 227 _fields_ = [ 228 ('size', ct.c_int), 229 ('valid', ct.c_int), 230 ('constant', ct.c_int), 231 ('deref_offset', ct.c_int), 232 ('deref_ident', ct.c_char_p), 233 ('base_register_name', ct.c_char_p), 234 ('index_register_name', ct.c_char_p), 235 ('scale', ct.c_int) 236 ] 237 238_USDT_CB = ct.CFUNCTYPE(None, ct.POINTER(bcc_usdt)) 239 240lib.bcc_usdt_foreach.restype = None 241lib.bcc_usdt_foreach.argtypes = [ct.c_void_p, _USDT_CB] 242 243lib.bcc_usdt_get_location.restype = ct.c_int 244lib.bcc_usdt_get_location.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_char_p, ct.c_int, 245 ct.POINTER(bcc_usdt_location)] 246 247lib.bcc_usdt_get_argument.restype = ct.c_int 248lib.bcc_usdt_get_argument.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_char_p, ct.c_int, 249 ct.c_int, ct.POINTER(bcc_usdt_argument)] 250 251_USDT_PROBE_CB = ct.CFUNCTYPE(None, ct.c_char_p, ct.c_char_p, 252 ct.c_ulonglong, ct.c_int) 253 254lib.bcc_usdt_foreach_uprobe.restype = None 255lib.bcc_usdt_foreach_uprobe.argtypes = [ct.c_void_p, _USDT_PROBE_CB] 256