Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <libelf.h> |
| 6 | #include <gelf.h> |
| 7 | #include <errno.h> |
| 8 | #include <unistd.h> |
| 9 | #include <string.h> |
| 10 | #include <stdbool.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 12 | #include <linux/bpf.h> |
| 13 | #include <linux/filter.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 14 | #include <linux/perf_event.h> |
| 15 | #include <sys/syscall.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <poll.h> |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 20 | #include "libbpf.h" |
| 21 | #include "bpf_helpers.h" |
| 22 | #include "bpf_load.h" |
| 23 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 24 | #define DEBUGFS "/sys/kernel/debug/tracing/" |
| 25 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 26 | static char license[128]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 27 | static int kern_version; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 28 | static bool processed_sec[128]; |
| 29 | int map_fd[MAX_MAPS]; |
| 30 | int prog_fd[MAX_PROGS]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 31 | int event_fd[MAX_PROGS]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 32 | int prog_cnt; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 33 | int prog_array_fd = -1; |
| 34 | |
| 35 | static int populate_prog_array(const char *event, int prog_fd) |
| 36 | { |
| 37 | int ind = atoi(event), err; |
| 38 | |
| 39 | err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY); |
| 40 | if (err < 0) { |
| 41 | printf("failed to store prog_fd in prog_array\n"); |
| 42 | return -1; |
| 43 | } |
| 44 | return 0; |
| 45 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 46 | |
| 47 | static int load_and_attach(const char *event, struct bpf_insn *prog, int size) |
| 48 | { |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 49 | bool is_socket = strncmp(event, "socket", 6) == 0; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 50 | bool is_kprobe = strncmp(event, "kprobe/", 7) == 0; |
| 51 | bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 52 | bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 53 | enum bpf_prog_type prog_type; |
| 54 | char buf[256]; |
| 55 | int fd, efd, err, id; |
| 56 | struct perf_event_attr attr = {}; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 57 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 58 | attr.type = PERF_TYPE_TRACEPOINT; |
| 59 | attr.sample_type = PERF_SAMPLE_RAW; |
| 60 | attr.sample_period = 1; |
| 61 | attr.wakeup_events = 1; |
| 62 | |
| 63 | if (is_socket) { |
| 64 | prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 65 | } else if (is_kprobe || is_kretprobe) { |
| 66 | prog_type = BPF_PROG_TYPE_KPROBE; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 67 | } else if (is_tracepoint) { |
| 68 | prog_type = BPF_PROG_TYPE_TRACEPOINT; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 69 | } else { |
| 70 | printf("Unknown event '%s'\n", event); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 71 | return -1; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 72 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 73 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 74 | fd = bpf_prog_load(prog_type, prog, size, license, kern_version); |
| 75 | if (fd < 0) { |
| 76 | printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | prog_fd[prog_cnt++] = fd; |
| 81 | |
| 82 | if (is_socket) { |
| 83 | event += 6; |
| 84 | if (*event != '/') |
| 85 | return 0; |
| 86 | event++; |
| 87 | if (!isdigit(*event)) { |
| 88 | printf("invalid prog number\n"); |
| 89 | return -1; |
| 90 | } |
| 91 | return populate_prog_array(event, fd); |
| 92 | } |
| 93 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 94 | if (is_kprobe || is_kretprobe) { |
| 95 | if (is_kprobe) |
| 96 | event += 7; |
| 97 | else |
| 98 | event += 10; |
| 99 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 100 | if (*event == 0) { |
| 101 | printf("event name cannot be empty\n"); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | if (isdigit(*event)) |
| 106 | return populate_prog_array(event, fd); |
| 107 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 108 | snprintf(buf, sizeof(buf), |
| 109 | "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events", |
| 110 | is_kprobe ? 'p' : 'r', event, event); |
| 111 | err = system(buf); |
| 112 | if (err < 0) { |
| 113 | printf("failed to create kprobe '%s' error '%s'\n", |
| 114 | event, strerror(errno)); |
| 115 | return -1; |
| 116 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 117 | |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 118 | strcpy(buf, DEBUGFS); |
| 119 | strcat(buf, "events/kprobes/"); |
| 120 | strcat(buf, event); |
| 121 | strcat(buf, "/id"); |
| 122 | } else if (is_tracepoint) { |
| 123 | event += 11; |
| 124 | |
| 125 | if (*event == 0) { |
| 126 | printf("event name cannot be empty\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | strcpy(buf, DEBUGFS); |
| 130 | strcat(buf, "events/"); |
| 131 | strcat(buf, event); |
| 132 | strcat(buf, "/id"); |
| 133 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 134 | |
| 135 | efd = open(buf, O_RDONLY, 0); |
| 136 | if (efd < 0) { |
| 137 | printf("failed to open event %s\n", event); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | err = read(efd, buf, sizeof(buf)); |
| 142 | if (err < 0 || err >= sizeof(buf)) { |
| 143 | printf("read from '%s' failed '%s'\n", event, strerror(errno)); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | close(efd); |
| 148 | |
| 149 | buf[err] = 0; |
| 150 | id = atoi(buf); |
| 151 | attr.config = id; |
| 152 | |
| 153 | efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); |
| 154 | if (efd < 0) { |
| 155 | printf("event %d fd %d err %s\n", id, efd, strerror(errno)); |
| 156 | return -1; |
| 157 | } |
| 158 | event_fd[prog_cnt - 1] = efd; |
| 159 | ioctl(efd, PERF_EVENT_IOC_ENABLE, 0); |
| 160 | ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd); |
| 161 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int load_maps(struct bpf_map_def *maps, int len) |
| 166 | { |
| 167 | int i; |
| 168 | |
| 169 | for (i = 0; i < len / sizeof(struct bpf_map_def); i++) { |
| 170 | |
| 171 | map_fd[i] = bpf_create_map(maps[i].type, |
| 172 | maps[i].key_size, |
| 173 | maps[i].value_size, |
Alexei Starovoitov | 89b9760 | 2016-03-07 21:57:20 -0800 | [diff] [blame] | 174 | maps[i].max_entries, |
| 175 | maps[i].map_flags); |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 176 | if (map_fd[i] < 0) { |
| 177 | printf("failed to create a map: %d %s\n", |
| 178 | errno, strerror(errno)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 179 | return 1; |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 180 | } |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 181 | |
| 182 | if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY) |
| 183 | prog_array_fd = map_fd[i]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname, |
| 189 | GElf_Shdr *shdr, Elf_Data **data) |
| 190 | { |
| 191 | Elf_Scn *scn; |
| 192 | |
| 193 | scn = elf_getscn(elf, i); |
| 194 | if (!scn) |
| 195 | return 1; |
| 196 | |
| 197 | if (gelf_getshdr(scn, shdr) != shdr) |
| 198 | return 2; |
| 199 | |
| 200 | *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); |
| 201 | if (!*shname || !shdr->sh_size) |
| 202 | return 3; |
| 203 | |
| 204 | *data = elf_getdata(scn, 0); |
| 205 | if (!*data || elf_getdata(scn, *data) != NULL) |
| 206 | return 4; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols, |
| 212 | GElf_Shdr *shdr, struct bpf_insn *insn) |
| 213 | { |
| 214 | int i, nrels; |
| 215 | |
| 216 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 217 | |
| 218 | for (i = 0; i < nrels; i++) { |
| 219 | GElf_Sym sym; |
| 220 | GElf_Rel rel; |
| 221 | unsigned int insn_idx; |
| 222 | |
| 223 | gelf_getrel(data, i, &rel); |
| 224 | |
| 225 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 226 | |
| 227 | gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym); |
| 228 | |
| 229 | if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 230 | printf("invalid relo for insn[%d].code 0x%x\n", |
| 231 | insn_idx, insn[insn_idx].code); |
| 232 | return 1; |
| 233 | } |
| 234 | insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
| 235 | insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)]; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int load_bpf_file(char *path) |
| 242 | { |
| 243 | int fd, i; |
| 244 | Elf *elf; |
| 245 | GElf_Ehdr ehdr; |
| 246 | GElf_Shdr shdr, shdr_prog; |
| 247 | Elf_Data *data, *data_prog, *symbols = NULL; |
| 248 | char *shname, *shname_prog; |
| 249 | |
| 250 | if (elf_version(EV_CURRENT) == EV_NONE) |
| 251 | return 1; |
| 252 | |
| 253 | fd = open(path, O_RDONLY, 0); |
| 254 | if (fd < 0) |
| 255 | return 1; |
| 256 | |
| 257 | elf = elf_begin(fd, ELF_C_READ, NULL); |
| 258 | |
| 259 | if (!elf) |
| 260 | return 1; |
| 261 | |
| 262 | if (gelf_getehdr(elf, &ehdr) != &ehdr) |
| 263 | return 1; |
| 264 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 265 | /* clear all kprobes */ |
| 266 | i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events"); |
| 267 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 268 | /* scan over all elf sections to get license and map info */ |
| 269 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 270 | |
| 271 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 272 | continue; |
| 273 | |
| 274 | if (0) /* helpful for llvm debugging */ |
| 275 | printf("section %d:%s data %p size %zd link %d flags %d\n", |
| 276 | i, shname, data->d_buf, data->d_size, |
| 277 | shdr.sh_link, (int) shdr.sh_flags); |
| 278 | |
| 279 | if (strcmp(shname, "license") == 0) { |
| 280 | processed_sec[i] = true; |
| 281 | memcpy(license, data->d_buf, data->d_size); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 282 | } else if (strcmp(shname, "version") == 0) { |
| 283 | processed_sec[i] = true; |
| 284 | if (data->d_size != sizeof(int)) { |
| 285 | printf("invalid size of version section %zd\n", |
| 286 | data->d_size); |
| 287 | return 1; |
| 288 | } |
| 289 | memcpy(&kern_version, data->d_buf, sizeof(int)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 290 | } else if (strcmp(shname, "maps") == 0) { |
| 291 | processed_sec[i] = true; |
| 292 | if (load_maps(data->d_buf, data->d_size)) |
| 293 | return 1; |
| 294 | } else if (shdr.sh_type == SHT_SYMTAB) { |
| 295 | symbols = data; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* load programs that need map fixup (relocations) */ |
| 300 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 301 | |
| 302 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 303 | continue; |
| 304 | if (shdr.sh_type == SHT_REL) { |
| 305 | struct bpf_insn *insns; |
| 306 | |
| 307 | if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog, |
| 308 | &shdr_prog, &data_prog)) |
| 309 | continue; |
| 310 | |
| 311 | insns = (struct bpf_insn *) data_prog->d_buf; |
| 312 | |
| 313 | processed_sec[shdr.sh_info] = true; |
| 314 | processed_sec[i] = true; |
| 315 | |
| 316 | if (parse_relo_and_apply(data, symbols, &shdr, insns)) |
| 317 | continue; |
| 318 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 319 | if (memcmp(shname_prog, "kprobe/", 7) == 0 || |
| 320 | memcmp(shname_prog, "kretprobe/", 10) == 0 || |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 321 | memcmp(shname_prog, "tracepoint/", 11) == 0 || |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 322 | memcmp(shname_prog, "socket", 6) == 0) |
| 323 | load_and_attach(shname_prog, insns, data_prog->d_size); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* load programs that don't use maps */ |
| 328 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 329 | |
| 330 | if (processed_sec[i]) |
| 331 | continue; |
| 332 | |
| 333 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 334 | continue; |
| 335 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 336 | if (memcmp(shname, "kprobe/", 7) == 0 || |
| 337 | memcmp(shname, "kretprobe/", 10) == 0 || |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 338 | memcmp(shname, "tracepoint/", 11) == 0 || |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 339 | memcmp(shname, "socket", 6) == 0) |
| 340 | load_and_attach(shname, data->d_buf, data->d_size); |
| 341 | } |
| 342 | |
| 343 | close(fd); |
| 344 | return 0; |
| 345 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 346 | |
| 347 | void read_trace_pipe(void) |
| 348 | { |
| 349 | int trace_fd; |
| 350 | |
| 351 | trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0); |
| 352 | if (trace_fd < 0) |
| 353 | return; |
| 354 | |
| 355 | while (1) { |
| 356 | static char buf[4096]; |
| 357 | ssize_t sz; |
| 358 | |
| 359 | sz = read(trace_fd, buf, sizeof(buf)); |
| 360 | if (sz > 0) { |
| 361 | buf[sz] = 0; |
| 362 | puts(buf); |
| 363 | } |
| 364 | } |
| 365 | } |
Alexei Starovoitov | 3622e7e | 2016-03-07 21:57:19 -0800 | [diff] [blame] | 366 | |
| 367 | #define MAX_SYMS 300000 |
| 368 | static struct ksym syms[MAX_SYMS]; |
| 369 | static int sym_cnt; |
| 370 | |
| 371 | static int ksym_cmp(const void *p1, const void *p2) |
| 372 | { |
| 373 | return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; |
| 374 | } |
| 375 | |
| 376 | int load_kallsyms(void) |
| 377 | { |
| 378 | FILE *f = fopen("/proc/kallsyms", "r"); |
| 379 | char func[256], buf[256]; |
| 380 | char symbol; |
| 381 | void *addr; |
| 382 | int i = 0; |
| 383 | |
| 384 | if (!f) |
| 385 | return -ENOENT; |
| 386 | |
| 387 | while (!feof(f)) { |
| 388 | if (!fgets(buf, sizeof(buf), f)) |
| 389 | break; |
| 390 | if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) |
| 391 | break; |
| 392 | if (!addr) |
| 393 | continue; |
| 394 | syms[i].addr = (long) addr; |
| 395 | syms[i].name = strdup(func); |
| 396 | i++; |
| 397 | } |
| 398 | sym_cnt = i; |
| 399 | qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | struct ksym *ksym_search(long key) |
| 404 | { |
| 405 | int start = 0, end = sym_cnt; |
| 406 | int result; |
| 407 | |
| 408 | while (start < end) { |
| 409 | size_t mid = start + (end - start) / 2; |
| 410 | |
| 411 | result = key - syms[mid].addr; |
| 412 | if (result < 0) |
| 413 | end = mid; |
| 414 | else if (result > 0) |
| 415 | start = mid + 1; |
| 416 | else |
| 417 | return &syms[mid]; |
| 418 | } |
| 419 | |
| 420 | if (start >= 1 && syms[start - 1].addr < key && |
| 421 | key < syms[start].addr) |
| 422 | /* valid ksym */ |
| 423 | return &syms[start - 1]; |
| 424 | |
| 425 | /* out of range. return _stext */ |
| 426 | return &syms[0]; |
| 427 | } |