2 * Copyright (C) 2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
44 #include <sys/types.h>
52 #include "xlated_dumper.h"
54 static const char * const prog_type_name[] = {
55 [BPF_PROG_TYPE_UNSPEC] = "unspec",
56 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
57 [BPF_PROG_TYPE_KPROBE] = "kprobe",
58 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
59 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
60 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
61 [BPF_PROG_TYPE_XDP] = "xdp",
62 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
63 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
64 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
65 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
66 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
67 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
68 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
69 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
70 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
71 [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
72 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
73 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
74 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
77 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
79 struct timespec real_time_ts, boot_time_ts;
80 time_t wallclock_secs;
85 if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
86 clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
87 perror("Can't read clocks");
88 snprintf(buf, size, "%llu", nsecs / 1000000000);
92 wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
93 (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
97 if (!localtime_r(&wallclock_secs, &load_tm)) {
98 snprintf(buf, size, "%llu", nsecs / 1000000000);
103 strftime(buf, size, "%s", &load_tm);
105 strftime(buf, size, "%FT%T%z", &load_tm);
108 static int prog_fd_by_tag(unsigned char *tag)
110 struct bpf_prog_info info = {};
111 __u32 len = sizeof(info);
117 err = bpf_prog_get_next_id(id, &id);
119 p_err("%s", strerror(errno));
123 fd = bpf_prog_get_fd_by_id(id);
125 p_err("can't get prog by id (%u): %s",
126 id, strerror(errno));
130 err = bpf_obj_get_info_by_fd(fd, &info, &len);
132 p_err("can't get prog info (%u): %s",
133 id, strerror(errno));
138 if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
145 int prog_parse_fd(int *argc, char ***argv)
149 if (is_prefix(**argv, "id")) {
155 id = strtoul(**argv, &endptr, 0);
157 p_err("can't parse %s as ID", **argv);
162 fd = bpf_prog_get_fd_by_id(id);
164 p_err("get by id (%u): %s", id, strerror(errno));
166 } else if (is_prefix(**argv, "tag")) {
167 unsigned char tag[BPF_TAG_SIZE];
171 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
172 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
174 p_err("can't parse tag");
179 return prog_fd_by_tag(tag);
180 } else if (is_prefix(**argv, "pinned")) {
188 return open_obj_pinned_any(path, BPF_OBJ_PROG);
191 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
195 static void show_prog_maps(int fd, u32 num_maps)
197 struct bpf_prog_info info = {};
198 __u32 len = sizeof(info);
199 __u32 map_ids[num_maps];
203 info.nr_map_ids = num_maps;
204 info.map_ids = ptr_to_u64(map_ids);
206 err = bpf_obj_get_info_by_fd(fd, &info, &len);
207 if (err || !info.nr_map_ids)
211 jsonw_name(json_wtr, "map_ids");
212 jsonw_start_array(json_wtr);
213 for (i = 0; i < info.nr_map_ids; i++)
214 jsonw_uint(json_wtr, map_ids[i]);
215 jsonw_end_array(json_wtr);
218 for (i = 0; i < info.nr_map_ids; i++)
219 printf("%u%s", map_ids[i],
220 i == info.nr_map_ids - 1 ? "" : ",");
224 static void print_prog_json(struct bpf_prog_info *info, int fd)
228 jsonw_start_object(json_wtr);
229 jsonw_uint_field(json_wtr, "id", info->id);
230 if (info->type < ARRAY_SIZE(prog_type_name))
231 jsonw_string_field(json_wtr, "type",
232 prog_type_name[info->type]);
234 jsonw_uint_field(json_wtr, "type", info->type);
237 jsonw_string_field(json_wtr, "name", info->name);
239 jsonw_name(json_wtr, "tag");
240 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
241 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
242 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
244 jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
246 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
248 if (info->load_time) {
251 print_boot_time(info->load_time, buf, sizeof(buf));
253 /* Piggy back on load_time, since 0 uid is a valid one */
254 jsonw_name(json_wtr, "loaded_at");
255 jsonw_printf(json_wtr, "%s", buf);
256 jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
259 jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
261 if (info->jited_prog_len) {
262 jsonw_bool_field(json_wtr, "jited", true);
263 jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
265 jsonw_bool_field(json_wtr, "jited", false);
268 memlock = get_fdinfo(fd, "memlock");
270 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
273 if (info->nr_map_ids)
274 show_prog_maps(fd, info->nr_map_ids);
276 if (!hash_empty(prog_table.table)) {
277 struct pinned_obj *obj;
279 jsonw_name(json_wtr, "pinned");
280 jsonw_start_array(json_wtr);
281 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
282 if (obj->id == info->id)
283 jsonw_string(json_wtr, obj->path);
285 jsonw_end_array(json_wtr);
288 jsonw_end_object(json_wtr);
291 static void print_prog_plain(struct bpf_prog_info *info, int fd)
295 printf("%u: ", info->id);
296 if (info->type < ARRAY_SIZE(prog_type_name))
297 printf("%s ", prog_type_name[info->type]);
299 printf("type %u ", info->type);
302 printf("name %s ", info->name);
305 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
306 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
307 printf("%s", info->gpl_compatible ? " gpl" : "");
310 if (info->load_time) {
313 print_boot_time(info->load_time, buf, sizeof(buf));
315 /* Piggy back on load_time, since 0 uid is a valid one */
316 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
319 printf("\txlated %uB", info->xlated_prog_len);
321 if (info->jited_prog_len)
322 printf(" jited %uB", info->jited_prog_len);
324 printf(" not jited");
326 memlock = get_fdinfo(fd, "memlock");
328 printf(" memlock %sB", memlock);
331 if (info->nr_map_ids)
332 show_prog_maps(fd, info->nr_map_ids);
334 if (!hash_empty(prog_table.table)) {
335 struct pinned_obj *obj;
338 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
339 if (obj->id == info->id)
340 printf("\tpinned %s\n", obj->path);
347 static int show_prog(int fd)
349 struct bpf_prog_info info = {};
350 __u32 len = sizeof(info);
353 err = bpf_obj_get_info_by_fd(fd, &info, &len);
355 p_err("can't get prog info: %s", strerror(errno));
360 print_prog_json(&info, fd);
362 print_prog_plain(&info, fd);
367 static int do_show(int argc, char **argv)
374 build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
377 fd = prog_parse_fd(&argc, &argv);
381 return show_prog(fd);
388 jsonw_start_array(json_wtr);
390 err = bpf_prog_get_next_id(id, &id);
392 if (errno == ENOENT) {
396 p_err("can't get next program: %s%s", strerror(errno),
397 errno == EINVAL ? " -- kernel too old?" : "");
402 fd = bpf_prog_get_fd_by_id(id);
406 p_err("can't get prog by id (%u): %s",
407 id, strerror(errno));
419 jsonw_end_array(json_wtr);
424 static int do_dump(int argc, char **argv)
426 unsigned long *func_ksyms = NULL;
427 struct bpf_prog_info info = {};
428 unsigned int *func_lens = NULL;
429 unsigned int nr_func_ksyms;
430 unsigned int nr_func_lens;
431 struct dump_data dd = {};
432 __u32 len = sizeof(info);
433 unsigned int buf_size;
434 char *filepath = NULL;
435 bool opcodes = false;
444 if (is_prefix(*argv, "jited")) {
445 member_len = &info.jited_prog_len;
446 member_ptr = &info.jited_prog_insns;
447 } else if (is_prefix(*argv, "xlated")) {
448 member_len = &info.xlated_prog_len;
449 member_ptr = &info.xlated_prog_insns;
451 p_err("expected 'xlated' or 'jited', got: %s", *argv);
459 fd = prog_parse_fd(&argc, &argv);
463 if (is_prefix(*argv, "file")) {
466 p_err("expected file path");
472 } else if (is_prefix(*argv, "opcodes")) {
475 } else if (is_prefix(*argv, "visual")) {
485 err = bpf_obj_get_info_by_fd(fd, &info, &len);
487 p_err("can't get prog info: %s", strerror(errno));
492 p_info("no instructions returned");
497 buf_size = *member_len;
499 buf = malloc(buf_size);
501 p_err("mem alloc failed");
506 nr_func_ksyms = info.nr_jited_ksyms;
508 func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
510 p_err("mem alloc failed");
516 nr_func_lens = info.nr_jited_func_lens;
518 func_lens = malloc(nr_func_lens * sizeof(__u32));
520 p_err("mem alloc failed");
526 memset(&info, 0, sizeof(info));
528 *member_ptr = ptr_to_u64(buf);
529 *member_len = buf_size;
530 info.jited_ksyms = ptr_to_u64(func_ksyms);
531 info.nr_jited_ksyms = nr_func_ksyms;
532 info.jited_func_lens = ptr_to_u64(func_lens);
533 info.nr_jited_func_lens = nr_func_lens;
535 err = bpf_obj_get_info_by_fd(fd, &info, &len);
538 p_err("can't get prog info: %s", strerror(errno));
542 if (*member_len > buf_size) {
543 p_err("too many instructions returned");
547 if (info.nr_jited_ksyms > nr_func_ksyms) {
548 p_err("too many addresses returned");
552 if (info.nr_jited_func_lens > nr_func_lens) {
553 p_err("too many values returned");
557 if ((member_len == &info.jited_prog_len &&
558 info.jited_prog_insns == 0) ||
559 (member_len == &info.xlated_prog_len &&
560 info.xlated_prog_insns == 0)) {
561 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
566 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
568 p_err("can't open file %s: %s", filepath,
573 n = write(fd, buf, *member_len);
575 if (n != *member_len) {
576 p_err("error writing output file: %s",
577 n < 0 ? strerror(errno) : "short write");
582 jsonw_null(json_wtr);
583 } else if (member_len == &info.jited_prog_len) {
584 const char *name = NULL;
587 name = ifindex_to_bfd_name_ns(info.ifindex,
594 if (info.nr_jited_func_lens && info.jited_func_lens) {
595 struct kernel_sym *sym = NULL;
596 char sym_name[SYM_MAX_NAME];
597 unsigned char *img = buf;
602 if (info.nr_jited_ksyms) {
603 kernel_syms_load(&dd);
604 ksyms = (__u64 *) info.jited_ksyms;
608 jsonw_start_array(json_wtr);
610 lens = (__u32 *) info.jited_func_lens;
611 for (i = 0; i < info.nr_jited_func_lens; i++) {
613 sym = kernel_syms_search(&dd, ksyms[i]);
615 sprintf(sym_name, "%s", sym->name);
617 sprintf(sym_name, "0x%016llx", ksyms[i]);
619 strcpy(sym_name, "unknown");
623 jsonw_start_object(json_wtr);
624 jsonw_name(json_wtr, "name");
625 jsonw_string(json_wtr, sym_name);
626 jsonw_name(json_wtr, "insns");
628 printf("%s:\n", sym_name);
631 disasm_print_insn(img, lens[i], opcodes, name);
635 jsonw_end_object(json_wtr);
641 jsonw_end_array(json_wtr);
643 disasm_print_insn(buf, *member_len, opcodes, name);
647 jsonw_null(json_wtr);
649 dump_xlated_cfg(buf, *member_len);
651 kernel_syms_load(&dd);
652 dd.nr_jited_ksyms = info.nr_jited_ksyms;
653 dd.jited_ksyms = (__u64 *) info.jited_ksyms;
656 dump_xlated_json(&dd, buf, *member_len, opcodes);
658 dump_xlated_plain(&dd, buf, *member_len, opcodes);
659 kernel_syms_destroy(&dd);
674 static int do_pin(int argc, char **argv)
678 err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
679 if (!err && json_output)
680 jsonw_null(json_wtr);
684 static int do_load(int argc, char **argv)
686 struct bpf_object *obj;
692 if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
693 p_err("failed to load program");
697 if (do_pin_fd(prog_fd, argv[1]))
701 jsonw_null(json_wtr);
703 bpf_object__close(obj);
708 bpf_object__close(obj);
712 static int do_help(int argc, char **argv)
715 jsonw_null(json_wtr);
720 "Usage: %s %s { show | list } [PROG]\n"
721 " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
722 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
723 " %s %s pin PROG FILE\n"
724 " %s %s load OBJ FILE\n"
727 " " HELP_SPEC_PROGRAM "\n"
728 " " HELP_SPEC_OPTIONS "\n"
730 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
731 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
736 static const struct cmd cmds[] = {
746 int do_prog(int argc, char **argv)
748 return cmd_select(cmds, argc, argv, do_help);