1--[[
2Copyright 2016 GitHub, Inc
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15]]
16local ffi = require("ffi")
17
18ffi.cdef[[
19enum bpf_prog_type {
20  BPF_PROG_TYPE_UNSPEC,
21  BPF_PROG_TYPE_SOCKET_FILTER,
22  BPF_PROG_TYPE_KPROBE,
23  BPF_PROG_TYPE_SCHED_CLS,
24  BPF_PROG_TYPE_SCHED_ACT,
25};
26
27int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries, int map_flags);
28int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
29int bpf_lookup_elem(int fd, void *key, void *value);
30int bpf_delete_elem(int fd, void *key);
31int bpf_get_next_key(int fd, void *key, void *next_key);
32
33int bpf_prog_load(enum bpf_prog_type prog_type, const char *name,
34  const struct bpf_insn *insns, int insn_len,
35  const char *license, unsigned kern_version,
36  int log_level, char *log_buf, unsigned log_buf_size);
37int bpf_attach_socket(int sockfd, int progfd);
38
39/* create RAW socket and bind to interface 'name' */
40int bpf_open_raw_sock(const char *name);
41
42typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size);
43typedef void (*perf_reader_lost_cb)(void *cb_cookie, uint64_t lost);
44
45int bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
46                      const char *fn_name, uint64_t fn_offset);
47
48int bpf_detach_kprobe(const char *ev_name);
49
50int bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name,
51                      const char *binary_path, uint64_t offset, int pid);
52
53int bpf_detach_uprobe(const char *ev_name);
54
55void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, perf_reader_lost_cb lost_cb, void *cb_cookie, int pid, int cpu, int page_cnt);
56
57int bpf_close_perf_event_fd(int fd);
58]]
59
60ffi.cdef[[
61void * bpf_module_create_b(const char *filename, const char *proto_filename, unsigned flags);
62void * bpf_module_create_c(const char *filename, unsigned flags, const char *cflags[], int ncflags);
63void * bpf_module_create_c_from_string(const char *text, unsigned flags, const char *cflags[], int ncflags);
64void bpf_module_destroy(void *program);
65char * bpf_module_license(void *program);
66unsigned bpf_module_kern_version(void *program);
67size_t bpf_num_functions(void *program);
68const char * bpf_function_name(void *program, size_t id);
69void * bpf_function_start_id(void *program, size_t id);
70void * bpf_function_start(void *program, const char *name);
71size_t bpf_function_size_id(void *program, size_t id);
72size_t bpf_function_size(void *program, const char *name);
73size_t bpf_num_tables(void *program);
74size_t bpf_table_id(void *program, const char *table_name);
75int bpf_table_fd(void *program, const char *table_name);
76int bpf_table_fd_id(void *program, size_t id);
77int bpf_table_type(void *program, const char *table_name);
78int bpf_table_type_id(void *program, size_t id);
79size_t bpf_table_max_entries(void *program, const char *table_name);
80size_t bpf_table_max_entries_id(void *program, size_t id);
81int bpf_table_flags(void *program, const char *table_name);
82int bpf_table_flags_id(void *program, size_t id);
83const char * bpf_table_name(void *program, size_t id);
84const char * bpf_table_key_desc(void *program, const char *table_name);
85const char * bpf_table_key_desc_id(void *program, size_t id);
86const char * bpf_table_leaf_desc(void *program, const char *table_name);
87const char * bpf_table_leaf_desc_id(void *program, size_t id);
88size_t bpf_table_key_size(void *program, const char *table_name);
89size_t bpf_table_key_size_id(void *program, size_t id);
90size_t bpf_table_leaf_size(void *program, const char *table_name);
91size_t bpf_table_leaf_size_id(void *program, size_t id);
92int bpf_table_key_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *key);
93int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *leaf);
94int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key);
95int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf);
96]]
97
98ffi.cdef[[
99struct perf_reader;
100
101void perf_reader_free(void *ptr);
102int perf_reader_mmap(struct perf_reader *reader);
103int perf_reader_poll(int num_readers, struct perf_reader **readers, int timeout);
104int perf_reader_fd(struct perf_reader *reader);
105void perf_reader_set_fd(struct perf_reader *reader, int fd);
106]]
107
108ffi.cdef[[
109struct bcc_symbol {
110	const char *name;
111	const char *demangle_name;
112	const char *module;
113	uint64_t offset;
114};
115
116struct bcc_symbol_option {
117  int use_debug_file;
118  int check_debug_file_crc;
119  uint32_t use_symbol_type;
120};
121
122int bcc_resolve_symname(const char *module, const char *symname, const uint64_t addr,
123                        int pid, struct bcc_symbol_option *option,
124                        struct bcc_symbol *sym);
125void bcc_procutils_free(const char *ptr);
126void *bcc_symcache_new(int pid, struct bcc_symbol_option *option);
127void bcc_symbol_free_demangle_name(struct bcc_symbol *sym);
128int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
129void bcc_symcache_refresh(void *resolver);
130]]
131
132ffi.cdef[[
133void *bcc_usdt_new_frompid(int pid);
134void *bcc_usdt_new_frompath(const char *path);
135void bcc_usdt_close(void *usdt);
136
137int bcc_usdt_enable_probe(void *, const char *, const char *);
138char *bcc_usdt_genargs(void *);
139
140typedef void (*bcc_usdt_uprobe_cb)(const char *, const char *, uint64_t, int);
141void bcc_usdt_foreach_uprobe(void *usdt, bcc_usdt_uprobe_cb callback);
142]]
143
144if rawget(_G, "BCC_STANDALONE") then
145  return ffi.C
146else
147  return ffi.load(
148    os.getenv("LIBBCC_SO_PATH") or
149    rawget(_G, "LIBBCC_SO_PATH") or
150    "bcc")
151end
152