bpf_load.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <libelf.h>
  7. #include <gelf.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include <linux/bpf.h>
  14. #include <linux/filter.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/netlink.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/types.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <sys/syscall.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/mman.h>
  24. #include <poll.h>
  25. #include <ctype.h>
  26. #include <assert.h>
  27. #include <bpf/bpf.h>
  28. #include "bpf_load.h"
  29. #include "perf-sys.h"
  30. #define DEBUGFS "/sys/kernel/debug/tracing/"
  31. static char license[128];
  32. static int kern_version;
  33. static bool processed_sec[128];
  34. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  35. int map_fd[MAX_MAPS];
  36. int prog_fd[MAX_PROGS];
  37. int event_fd[MAX_PROGS];
  38. int prog_cnt;
  39. int prog_array_fd = -1;
  40. struct bpf_map_data map_data[MAX_MAPS];
  41. int map_data_count = 0;
  42. static int populate_prog_array(const char *event, int prog_fd)
  43. {
  44. int ind = atoi(event), err;
  45. err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
  46. if (err < 0) {
  47. printf("failed to store prog_fd in prog_array\n");
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
  53. {
  54. bool is_socket = strncmp(event, "socket", 6) == 0;
  55. bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
  56. bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
  57. bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
  58. bool is_raw_tracepoint = strncmp(event, "raw_tracepoint/", 15) == 0;
  59. bool is_xdp = strncmp(event, "xdp", 3) == 0;
  60. bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
  61. bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
  62. bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
  63. bool is_sockops = strncmp(event, "sockops", 7) == 0;
  64. bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
  65. bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0;
  66. size_t insns_cnt = size / sizeof(struct bpf_insn);
  67. enum bpf_prog_type prog_type;
  68. char buf[256];
  69. int fd, efd, err, id;
  70. struct perf_event_attr attr = {};
  71. attr.type = PERF_TYPE_TRACEPOINT;
  72. attr.sample_type = PERF_SAMPLE_RAW;
  73. attr.sample_period = 1;
  74. attr.wakeup_events = 1;
  75. if (is_socket) {
  76. prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  77. } else if (is_kprobe || is_kretprobe) {
  78. prog_type = BPF_PROG_TYPE_KPROBE;
  79. } else if (is_tracepoint) {
  80. prog_type = BPF_PROG_TYPE_TRACEPOINT;
  81. } else if (is_raw_tracepoint) {
  82. prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT;
  83. } else if (is_xdp) {
  84. prog_type = BPF_PROG_TYPE_XDP;
  85. } else if (is_perf_event) {
  86. prog_type = BPF_PROG_TYPE_PERF_EVENT;
  87. } else if (is_cgroup_skb) {
  88. prog_type = BPF_PROG_TYPE_CGROUP_SKB;
  89. } else if (is_cgroup_sk) {
  90. prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
  91. } else if (is_sockops) {
  92. prog_type = BPF_PROG_TYPE_SOCK_OPS;
  93. } else if (is_sk_skb) {
  94. prog_type = BPF_PROG_TYPE_SK_SKB;
  95. } else if (is_sk_msg) {
  96. prog_type = BPF_PROG_TYPE_SK_MSG;
  97. } else {
  98. printf("Unknown event '%s'\n", event);
  99. return -1;
  100. }
  101. fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
  102. bpf_log_buf, BPF_LOG_BUF_SIZE);
  103. if (fd < 0) {
  104. printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
  105. return -1;
  106. }
  107. prog_fd[prog_cnt++] = fd;
  108. if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
  109. return 0;
  110. if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
  111. if (is_socket)
  112. event += 6;
  113. else
  114. event += 7;
  115. if (*event != '/')
  116. return 0;
  117. event++;
  118. if (!isdigit(*event)) {
  119. printf("invalid prog number\n");
  120. return -1;
  121. }
  122. return populate_prog_array(event, fd);
  123. }
  124. if (is_raw_tracepoint) {
  125. efd = bpf_raw_tracepoint_open(event + 15, fd);
  126. if (efd < 0) {
  127. printf("tracepoint %s %s\n", event + 15, strerror(errno));
  128. return -1;
  129. }
  130. event_fd[prog_cnt - 1] = efd;
  131. return 0;
  132. }
  133. if (is_kprobe || is_kretprobe) {
  134. bool need_normal_check = true;
  135. const char *event_prefix = "";
  136. if (is_kprobe)
  137. event += 7;
  138. else
  139. event += 10;
  140. if (*event == 0) {
  141. printf("event name cannot be empty\n");
  142. return -1;
  143. }
  144. if (isdigit(*event))
  145. return populate_prog_array(event, fd);
  146. #ifdef __x86_64__
  147. if (strncmp(event, "sys_", 4) == 0) {
  148. snprintf(buf, sizeof(buf),
  149. "echo '%c:__x64_%s __x64_%s' >> /sys/kernel/debug/tracing/kprobe_events",
  150. is_kprobe ? 'p' : 'r', event, event);
  151. err = system(buf);
  152. if (err >= 0) {
  153. need_normal_check = false;
  154. event_prefix = "__x64_";
  155. }
  156. }
  157. #endif
  158. if (need_normal_check) {
  159. snprintf(buf, sizeof(buf),
  160. "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
  161. is_kprobe ? 'p' : 'r', event, event);
  162. err = system(buf);
  163. if (err < 0) {
  164. printf("failed to create kprobe '%s' error '%s'\n",
  165. event, strerror(errno));
  166. return -1;
  167. }
  168. }
  169. strcpy(buf, DEBUGFS);
  170. strcat(buf, "events/kprobes/");
  171. strcat(buf, event_prefix);
  172. strcat(buf, event);
  173. strcat(buf, "/id");
  174. } else if (is_tracepoint) {
  175. event += 11;
  176. if (*event == 0) {
  177. printf("event name cannot be empty\n");
  178. return -1;
  179. }
  180. strcpy(buf, DEBUGFS);
  181. strcat(buf, "events/");
  182. strcat(buf, event);
  183. strcat(buf, "/id");
  184. }
  185. efd = open(buf, O_RDONLY, 0);
  186. if (efd < 0) {
  187. printf("failed to open event %s\n", event);
  188. return -1;
  189. }
  190. err = read(efd, buf, sizeof(buf));
  191. if (err < 0 || err >= sizeof(buf)) {
  192. printf("read from '%s' failed '%s'\n", event, strerror(errno));
  193. return -1;
  194. }
  195. close(efd);
  196. buf[err] = 0;
  197. id = atoi(buf);
  198. attr.config = id;
  199. efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
  200. if (efd < 0) {
  201. printf("event %d fd %d err %s\n", id, efd, strerror(errno));
  202. return -1;
  203. }
  204. event_fd[prog_cnt - 1] = efd;
  205. err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
  206. if (err < 0) {
  207. printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
  208. strerror(errno));
  209. return -1;
  210. }
  211. err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
  212. if (err < 0) {
  213. printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
  214. strerror(errno));
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. static int load_maps(struct bpf_map_data *maps, int nr_maps,
  220. fixup_map_cb fixup_map)
  221. {
  222. int i, numa_node;
  223. for (i = 0; i < nr_maps; i++) {
  224. if (fixup_map) {
  225. fixup_map(&maps[i], i);
  226. /* Allow userspace to assign map FD prior to creation */
  227. if (maps[i].fd != -1) {
  228. map_fd[i] = maps[i].fd;
  229. continue;
  230. }
  231. }
  232. numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
  233. maps[i].def.numa_node : -1;
  234. if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
  235. maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  236. int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
  237. map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
  238. maps[i].name,
  239. maps[i].def.key_size,
  240. inner_map_fd,
  241. maps[i].def.max_entries,
  242. maps[i].def.map_flags,
  243. numa_node);
  244. } else {
  245. map_fd[i] = bpf_create_map_node(maps[i].def.type,
  246. maps[i].name,
  247. maps[i].def.key_size,
  248. maps[i].def.value_size,
  249. maps[i].def.max_entries,
  250. maps[i].def.map_flags,
  251. numa_node);
  252. }
  253. if (map_fd[i] < 0) {
  254. printf("failed to create a map: %d %s\n",
  255. errno, strerror(errno));
  256. return 1;
  257. }
  258. maps[i].fd = map_fd[i];
  259. if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
  260. prog_array_fd = map_fd[i];
  261. }
  262. return 0;
  263. }
  264. static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
  265. GElf_Shdr *shdr, Elf_Data **data)
  266. {
  267. Elf_Scn *scn;
  268. scn = elf_getscn(elf, i);
  269. if (!scn)
  270. return 1;
  271. if (gelf_getshdr(scn, shdr) != shdr)
  272. return 2;
  273. *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
  274. if (!*shname || !shdr->sh_size)
  275. return 3;
  276. *data = elf_getdata(scn, 0);
  277. if (!*data || elf_getdata(scn, *data) != NULL)
  278. return 4;
  279. return 0;
  280. }
  281. static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
  282. GElf_Shdr *shdr, struct bpf_insn *insn,
  283. struct bpf_map_data *maps, int nr_maps)
  284. {
  285. int i, nrels;
  286. nrels = shdr->sh_size / shdr->sh_entsize;
  287. for (i = 0; i < nrels; i++) {
  288. GElf_Sym sym;
  289. GElf_Rel rel;
  290. unsigned int insn_idx;
  291. bool match = false;
  292. int j, map_idx;
  293. gelf_getrel(data, i, &rel);
  294. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  295. gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
  296. if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  297. printf("invalid relo for insn[%d].code 0x%x\n",
  298. insn_idx, insn[insn_idx].code);
  299. return 1;
  300. }
  301. insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  302. /* Match FD relocation against recorded map_data[] offset */
  303. for (map_idx = 0; map_idx < nr_maps; map_idx++) {
  304. if (maps[map_idx].elf_offset == sym.st_value) {
  305. match = true;
  306. break;
  307. }
  308. }
  309. if (match) {
  310. insn[insn_idx].imm = maps[map_idx].fd;
  311. } else {
  312. printf("invalid relo for insn[%d] no map_data match\n",
  313. insn_idx);
  314. return 1;
  315. }
  316. }
  317. return 0;
  318. }
  319. static int cmp_symbols(const void *l, const void *r)
  320. {
  321. const GElf_Sym *lsym = (const GElf_Sym *)l;
  322. const GElf_Sym *rsym = (const GElf_Sym *)r;
  323. if (lsym->st_value < rsym->st_value)
  324. return -1;
  325. else if (lsym->st_value > rsym->st_value)
  326. return 1;
  327. else
  328. return 0;
  329. }
  330. static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
  331. Elf *elf, Elf_Data *symbols, int strtabidx)
  332. {
  333. int map_sz_elf, map_sz_copy;
  334. bool validate_zero = false;
  335. Elf_Data *data_maps;
  336. int i, nr_maps;
  337. GElf_Sym *sym;
  338. Elf_Scn *scn;
  339. int copy_sz;
  340. if (maps_shndx < 0)
  341. return -EINVAL;
  342. if (!symbols)
  343. return -EINVAL;
  344. /* Get data for maps section via elf index */
  345. scn = elf_getscn(elf, maps_shndx);
  346. if (scn)
  347. data_maps = elf_getdata(scn, NULL);
  348. if (!scn || !data_maps) {
  349. printf("Failed to get Elf_Data from maps section %d\n",
  350. maps_shndx);
  351. return -EINVAL;
  352. }
  353. /* For each map get corrosponding symbol table entry */
  354. sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
  355. for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  356. assert(nr_maps < MAX_MAPS+1);
  357. if (!gelf_getsym(symbols, i, &sym[nr_maps]))
  358. continue;
  359. if (sym[nr_maps].st_shndx != maps_shndx)
  360. continue;
  361. /* Only increment iif maps section */
  362. nr_maps++;
  363. }
  364. /* Align to map_fd[] order, via sort on offset in sym.st_value */
  365. qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
  366. /* Keeping compatible with ELF maps section changes
  367. * ------------------------------------------------
  368. * The program size of struct bpf_load_map_def is known by loader
  369. * code, but struct stored in ELF file can be different.
  370. *
  371. * Unfortunately sym[i].st_size is zero. To calculate the
  372. * struct size stored in the ELF file, assume all struct have
  373. * the same size, and simply divide with number of map
  374. * symbols.
  375. */
  376. map_sz_elf = data_maps->d_size / nr_maps;
  377. map_sz_copy = sizeof(struct bpf_load_map_def);
  378. if (map_sz_elf < map_sz_copy) {
  379. /*
  380. * Backward compat, loading older ELF file with
  381. * smaller struct, keeping remaining bytes zero.
  382. */
  383. map_sz_copy = map_sz_elf;
  384. } else if (map_sz_elf > map_sz_copy) {
  385. /*
  386. * Forward compat, loading newer ELF file with larger
  387. * struct with unknown features. Assume zero means
  388. * feature not used. Thus, validate rest of struct
  389. * data is zero.
  390. */
  391. validate_zero = true;
  392. }
  393. /* Memcpy relevant part of ELF maps data to loader maps */
  394. for (i = 0; i < nr_maps; i++) {
  395. struct bpf_load_map_def *def;
  396. unsigned char *addr, *end;
  397. const char *map_name;
  398. size_t offset;
  399. map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
  400. maps[i].name = strdup(map_name);
  401. if (!maps[i].name) {
  402. printf("strdup(%s): %s(%d)\n", map_name,
  403. strerror(errno), errno);
  404. free(sym);
  405. return -errno;
  406. }
  407. /* Symbol value is offset into ELF maps section data area */
  408. offset = sym[i].st_value;
  409. def = (struct bpf_load_map_def *)(data_maps->d_buf + offset);
  410. maps[i].elf_offset = offset;
  411. memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def));
  412. memcpy(&maps[i].def, def, map_sz_copy);
  413. /* Verify no newer features were requested */
  414. if (validate_zero) {
  415. addr = (unsigned char*) def + map_sz_copy;
  416. end = (unsigned char*) def + map_sz_elf;
  417. for (; addr < end; addr++) {
  418. if (*addr != 0) {
  419. free(sym);
  420. return -EFBIG;
  421. }
  422. }
  423. }
  424. }
  425. free(sym);
  426. return nr_maps;
  427. }
  428. static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
  429. {
  430. int fd, i, ret, maps_shndx = -1, strtabidx = -1;
  431. Elf *elf;
  432. GElf_Ehdr ehdr;
  433. GElf_Shdr shdr, shdr_prog;
  434. Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
  435. char *shname, *shname_prog;
  436. int nr_maps = 0;
  437. /* reset global variables */
  438. kern_version = 0;
  439. memset(license, 0, sizeof(license));
  440. memset(processed_sec, 0, sizeof(processed_sec));
  441. if (elf_version(EV_CURRENT) == EV_NONE)
  442. return 1;
  443. fd = open(path, O_RDONLY, 0);
  444. if (fd < 0)
  445. return 1;
  446. elf = elf_begin(fd, ELF_C_READ, NULL);
  447. if (!elf)
  448. return 1;
  449. if (gelf_getehdr(elf, &ehdr) != &ehdr)
  450. return 1;
  451. /* clear all kprobes */
  452. i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
  453. /* scan over all elf sections to get license and map info */
  454. for (i = 1; i < ehdr.e_shnum; i++) {
  455. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  456. continue;
  457. if (0) /* helpful for llvm debugging */
  458. printf("section %d:%s data %p size %zd link %d flags %d\n",
  459. i, shname, data->d_buf, data->d_size,
  460. shdr.sh_link, (int) shdr.sh_flags);
  461. if (strcmp(shname, "license") == 0) {
  462. processed_sec[i] = true;
  463. memcpy(license, data->d_buf, data->d_size);
  464. } else if (strcmp(shname, "version") == 0) {
  465. processed_sec[i] = true;
  466. if (data->d_size != sizeof(int)) {
  467. printf("invalid size of version section %zd\n",
  468. data->d_size);
  469. return 1;
  470. }
  471. memcpy(&kern_version, data->d_buf, sizeof(int));
  472. } else if (strcmp(shname, "maps") == 0) {
  473. int j;
  474. maps_shndx = i;
  475. data_maps = data;
  476. for (j = 0; j < MAX_MAPS; j++)
  477. map_data[j].fd = -1;
  478. } else if (shdr.sh_type == SHT_SYMTAB) {
  479. strtabidx = shdr.sh_link;
  480. symbols = data;
  481. }
  482. }
  483. ret = 1;
  484. if (!symbols) {
  485. printf("missing SHT_SYMTAB section\n");
  486. goto done;
  487. }
  488. if (data_maps) {
  489. nr_maps = load_elf_maps_section(map_data, maps_shndx,
  490. elf, symbols, strtabidx);
  491. if (nr_maps < 0) {
  492. printf("Error: Failed loading ELF maps (errno:%d):%s\n",
  493. nr_maps, strerror(-nr_maps));
  494. goto done;
  495. }
  496. if (load_maps(map_data, nr_maps, fixup_map))
  497. goto done;
  498. map_data_count = nr_maps;
  499. processed_sec[maps_shndx] = true;
  500. }
  501. /* process all relo sections, and rewrite bpf insns for maps */
  502. for (i = 1; i < ehdr.e_shnum; i++) {
  503. if (processed_sec[i])
  504. continue;
  505. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  506. continue;
  507. if (shdr.sh_type == SHT_REL) {
  508. struct bpf_insn *insns;
  509. /* locate prog sec that need map fixup (relocations) */
  510. if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
  511. &shdr_prog, &data_prog))
  512. continue;
  513. if (shdr_prog.sh_type != SHT_PROGBITS ||
  514. !(shdr_prog.sh_flags & SHF_EXECINSTR))
  515. continue;
  516. insns = (struct bpf_insn *) data_prog->d_buf;
  517. processed_sec[i] = true; /* relo section */
  518. if (parse_relo_and_apply(data, symbols, &shdr, insns,
  519. map_data, nr_maps))
  520. continue;
  521. }
  522. }
  523. /* load programs */
  524. for (i = 1; i < ehdr.e_shnum; i++) {
  525. if (processed_sec[i])
  526. continue;
  527. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  528. continue;
  529. if (memcmp(shname, "kprobe/", 7) == 0 ||
  530. memcmp(shname, "kretprobe/", 10) == 0 ||
  531. memcmp(shname, "tracepoint/", 11) == 0 ||
  532. memcmp(shname, "raw_tracepoint/", 15) == 0 ||
  533. memcmp(shname, "xdp", 3) == 0 ||
  534. memcmp(shname, "perf_event", 10) == 0 ||
  535. memcmp(shname, "socket", 6) == 0 ||
  536. memcmp(shname, "cgroup/", 7) == 0 ||
  537. memcmp(shname, "sockops", 7) == 0 ||
  538. memcmp(shname, "sk_skb", 6) == 0 ||
  539. memcmp(shname, "sk_msg", 6) == 0) {
  540. ret = load_and_attach(shname, data->d_buf,
  541. data->d_size);
  542. if (ret != 0)
  543. goto done;
  544. }
  545. }
  546. done:
  547. close(fd);
  548. return ret;
  549. }
  550. int load_bpf_file(char *path)
  551. {
  552. return do_load_bpf_file(path, NULL);
  553. }
  554. int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
  555. {
  556. return do_load_bpf_file(path, fixup_map);
  557. }
  558. void read_trace_pipe(void)
  559. {
  560. int trace_fd;
  561. trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
  562. if (trace_fd < 0)
  563. return;
  564. while (1) {
  565. static char buf[4096];
  566. ssize_t sz;
  567. sz = read(trace_fd, buf, sizeof(buf));
  568. if (sz > 0) {
  569. buf[sz] = 0;
  570. puts(buf);
  571. }
  572. }
  573. }