blob: 42ee8892549ccd12d53795445b68a8e238a7eecf [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
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.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35
36#include <errno.h>
37#include <fcntl.h>
Jakub Kicinskic9c35992017-10-09 10:30:13 -070038#include <stdarg.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43#include <unistd.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46
47#include <bpf.h>
Roman Gushchin49a086c2017-12-13 15:18:53 +000048#include <libbpf.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070049
50#include "main.h"
Jakub Kicinskic9c35992017-10-09 10:30:13 -070051#include "disasm.h"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070052
53static const char * const prog_type_name[] = {
54 [BPF_PROG_TYPE_UNSPEC] = "unspec",
55 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
56 [BPF_PROG_TYPE_KPROBE] = "kprobe",
57 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
58 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
59 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
60 [BPF_PROG_TYPE_XDP] = "xdp",
61 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
62 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
63 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
64 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
65 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
66 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
67 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
68 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
69};
70
71static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
72{
73 struct timespec real_time_ts, boot_time_ts;
74 time_t wallclock_secs;
75 struct tm load_tm;
76
77 buf[--size] = '\0';
78
79 if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
80 clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
81 perror("Can't read clocks");
82 snprintf(buf, size, "%llu", nsecs / 1000000000);
83 return;
84 }
85
86 wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
87 nsecs / 1000000000;
88
89 if (!localtime_r(&wallclock_secs, &load_tm)) {
90 snprintf(buf, size, "%llu", nsecs / 1000000000);
91 return;
92 }
93
94 strftime(buf, size, "%b %d/%H:%M", &load_tm);
95}
96
97static int prog_fd_by_tag(unsigned char *tag)
98{
99 struct bpf_prog_info info = {};
100 __u32 len = sizeof(info);
101 unsigned int id = 0;
102 int err;
103 int fd;
104
105 while (true) {
106 err = bpf_prog_get_next_id(id, &id);
107 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700108 p_err("%s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700109 return -1;
110 }
111
112 fd = bpf_prog_get_fd_by_id(id);
113 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700114 p_err("can't get prog by id (%u): %s",
115 id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700116 return -1;
117 }
118
119 err = bpf_obj_get_info_by_fd(fd, &info, &len);
120 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700121 p_err("can't get prog info (%u): %s",
122 id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700123 close(fd);
124 return -1;
125 }
126
127 if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
128 return fd;
129
130 close(fd);
131 }
132}
133
134int prog_parse_fd(int *argc, char ***argv)
135{
136 int fd;
137
138 if (is_prefix(**argv, "id")) {
139 unsigned int id;
140 char *endptr;
141
142 NEXT_ARGP();
143
144 id = strtoul(**argv, &endptr, 0);
145 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700146 p_err("can't parse %s as ID", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700147 return -1;
148 }
149 NEXT_ARGP();
150
151 fd = bpf_prog_get_fd_by_id(id);
152 if (fd < 0)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700153 p_err("get by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700154 return fd;
155 } else if (is_prefix(**argv, "tag")) {
156 unsigned char tag[BPF_TAG_SIZE];
157
158 NEXT_ARGP();
159
160 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
161 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
162 != BPF_TAG_SIZE) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700163 p_err("can't parse tag");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700164 return -1;
165 }
166 NEXT_ARGP();
167
168 return prog_fd_by_tag(tag);
169 } else if (is_prefix(**argv, "pinned")) {
170 char *path;
171
172 NEXT_ARGP();
173
174 path = **argv;
175 NEXT_ARGP();
176
177 return open_obj_pinned_any(path, BPF_OBJ_PROG);
178 }
179
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700180 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700181 return -1;
182}
183
184static void show_prog_maps(int fd, u32 num_maps)
185{
186 struct bpf_prog_info info = {};
187 __u32 len = sizeof(info);
188 __u32 map_ids[num_maps];
189 unsigned int i;
190 int err;
191
192 info.nr_map_ids = num_maps;
193 info.map_ids = ptr_to_u64(map_ids);
194
195 err = bpf_obj_get_info_by_fd(fd, &info, &len);
196 if (err || !info.nr_map_ids)
197 return;
198
Quentin Monnet743cc662017-10-23 09:24:08 -0700199 if (json_output) {
200 jsonw_name(json_wtr, "map_ids");
201 jsonw_start_array(json_wtr);
202 for (i = 0; i < info.nr_map_ids; i++)
203 jsonw_uint(json_wtr, map_ids[i]);
204 jsonw_end_array(json_wtr);
205 } else {
206 printf(" map_ids ");
207 for (i = 0; i < info.nr_map_ids; i++)
208 printf("%u%s", map_ids[i],
209 i == info.nr_map_ids - 1 ? "" : ",");
210 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700211}
212
Quentin Monnet743cc662017-10-23 09:24:08 -0700213static void print_prog_json(struct bpf_prog_info *info, int fd)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700214{
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700215 char *memlock;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700216
Quentin Monnet743cc662017-10-23 09:24:08 -0700217 jsonw_start_object(json_wtr);
218 jsonw_uint_field(json_wtr, "id", info->id);
219 if (info->type < ARRAY_SIZE(prog_type_name))
220 jsonw_string_field(json_wtr, "type",
221 prog_type_name[info->type]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700222 else
Quentin Monnet743cc662017-10-23 09:24:08 -0700223 jsonw_uint_field(json_wtr, "type", info->type);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700224
Quentin Monnet743cc662017-10-23 09:24:08 -0700225 if (*info->name)
226 jsonw_string_field(json_wtr, "name", info->name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700227
Quentin Monnet743cc662017-10-23 09:24:08 -0700228 jsonw_name(json_wtr, "tag");
229 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
230 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
231 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700232
Quentin Monnet743cc662017-10-23 09:24:08 -0700233 if (info->load_time) {
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700234 char buf[32];
235
Quentin Monnet743cc662017-10-23 09:24:08 -0700236 print_boot_time(info->load_time, buf, sizeof(buf));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700237
238 /* Piggy back on load_time, since 0 uid is a valid one */
Quentin Monnet743cc662017-10-23 09:24:08 -0700239 jsonw_string_field(json_wtr, "loaded_at", buf);
240 jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700241 }
242
Quentin Monnet743cc662017-10-23 09:24:08 -0700243 jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700244
Quentin Monnet743cc662017-10-23 09:24:08 -0700245 if (info->jited_prog_len) {
246 jsonw_bool_field(json_wtr, "jited", true);
247 jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
248 } else {
249 jsonw_bool_field(json_wtr, "jited", false);
250 }
251
252 memlock = get_fdinfo(fd, "memlock");
253 if (memlock)
254 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
255 free(memlock);
256
257 if (info->nr_map_ids)
258 show_prog_maps(fd, info->nr_map_ids);
259
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900260 if (!hash_empty(prog_table.table)) {
261 struct pinned_obj *obj;
262
263 jsonw_name(json_wtr, "pinned");
264 jsonw_start_array(json_wtr);
265 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
266 if (obj->id == info->id)
267 jsonw_string(json_wtr, obj->path);
268 }
269 jsonw_end_array(json_wtr);
270 }
271
Quentin Monnet743cc662017-10-23 09:24:08 -0700272 jsonw_end_object(json_wtr);
273}
274
275static void print_prog_plain(struct bpf_prog_info *info, int fd)
276{
277 char *memlock;
278
279 printf("%u: ", info->id);
280 if (info->type < ARRAY_SIZE(prog_type_name))
281 printf("%s ", prog_type_name[info->type]);
282 else
283 printf("type %u ", info->type);
284
285 if (*info->name)
286 printf("name %s ", info->name);
287
288 printf("tag ");
289 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
290 printf("\n");
291
292 if (info->load_time) {
293 char buf[32];
294
295 print_boot_time(info->load_time, buf, sizeof(buf));
296
297 /* Piggy back on load_time, since 0 uid is a valid one */
298 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
299 }
300
301 printf("\txlated %uB", info->xlated_prog_len);
302
303 if (info->jited_prog_len)
304 printf(" jited %uB", info->jited_prog_len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700305 else
306 printf(" not jited");
307
308 memlock = get_fdinfo(fd, "memlock");
309 if (memlock)
310 printf(" memlock %sB", memlock);
311 free(memlock);
312
Quentin Monnet743cc662017-10-23 09:24:08 -0700313 if (info->nr_map_ids)
314 show_prog_maps(fd, info->nr_map_ids);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700315
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900316 if (!hash_empty(prog_table.table)) {
317 struct pinned_obj *obj;
318
319 printf("\n");
320 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
321 if (obj->id == info->id)
322 printf("\tpinned %s\n", obj->path);
323 }
324 }
325
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700326 printf("\n");
Quentin Monnet743cc662017-10-23 09:24:08 -0700327}
328
329static int show_prog(int fd)
330{
331 struct bpf_prog_info info = {};
332 __u32 len = sizeof(info);
333 int err;
334
335 err = bpf_obj_get_info_by_fd(fd, &info, &len);
336 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700337 p_err("can't get prog info: %s", strerror(errno));
Quentin Monnet743cc662017-10-23 09:24:08 -0700338 return -1;
339 }
340
341 if (json_output)
342 print_prog_json(&info, fd);
343 else
344 print_prog_plain(&info, fd);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700345
346 return 0;
347}
348
349static int do_show(int argc, char **argv)
Quentin Monnet743cc662017-10-23 09:24:08 -0700350{
351 __u32 id = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700352 int err;
353 int fd;
354
Prashant Bholec541b732017-11-08 13:55:49 +0900355 if (show_pinned)
356 build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900357
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700358 if (argc == 2) {
359 fd = prog_parse_fd(&argc, &argv);
360 if (fd < 0)
361 return -1;
362
363 return show_prog(fd);
364 }
365
366 if (argc)
367 return BAD_ARG();
368
Quentin Monnet743cc662017-10-23 09:24:08 -0700369 if (json_output)
370 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700371 while (true) {
372 err = bpf_prog_get_next_id(id, &id);
373 if (err) {
Quentin Monnet1739c262017-10-19 15:46:20 -0700374 if (errno == ENOENT) {
375 err = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700376 break;
Quentin Monnet1739c262017-10-19 15:46:20 -0700377 }
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700378 p_err("can't get next program: %s%s", strerror(errno),
379 errno == EINVAL ? " -- kernel too old?" : "");
Quentin Monnet743cc662017-10-23 09:24:08 -0700380 err = -1;
381 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700382 }
383
384 fd = bpf_prog_get_fd_by_id(id);
385 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700386 p_err("can't get prog by id (%u): %s",
387 id, strerror(errno));
Quentin Monnet743cc662017-10-23 09:24:08 -0700388 err = -1;
389 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700390 }
391
392 err = show_prog(fd);
393 close(fd);
394 if (err)
Quentin Monnet743cc662017-10-23 09:24:08 -0700395 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700396 }
397
Quentin Monnet743cc662017-10-23 09:24:08 -0700398 if (json_output)
399 jsonw_end_array(json_wtr);
400
401 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700402}
403
Daniel Borkmann7105e822017-12-20 13:42:57 +0100404#define SYM_MAX_NAME 256
405
406struct kernel_sym {
407 unsigned long address;
408 char name[SYM_MAX_NAME];
409};
410
411struct dump_data {
412 unsigned long address_call_base;
413 struct kernel_sym *sym_mapping;
414 __u32 sym_count;
415 char scratch_buff[SYM_MAX_NAME];
416};
417
418static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
419{
420 return ((struct kernel_sym *)sym_a)->address -
421 ((struct kernel_sym *)sym_b)->address;
422}
423
424static void kernel_syms_load(struct dump_data *dd)
425{
426 struct kernel_sym *sym;
427 char buff[256];
428 void *tmp, *address;
429 FILE *fp;
430
431 fp = fopen("/proc/kallsyms", "r");
432 if (!fp)
433 return;
434
435 while (!feof(fp)) {
436 if (!fgets(buff, sizeof(buff), fp))
437 break;
438 tmp = realloc(dd->sym_mapping,
439 (dd->sym_count + 1) *
440 sizeof(*dd->sym_mapping));
441 if (!tmp) {
442out:
443 free(dd->sym_mapping);
444 dd->sym_mapping = NULL;
445 fclose(fp);
446 return;
447 }
448 dd->sym_mapping = tmp;
449 sym = &dd->sym_mapping[dd->sym_count];
450 if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
451 continue;
452 sym->address = (unsigned long)address;
453 if (!strcmp(sym->name, "__bpf_call_base")) {
454 dd->address_call_base = sym->address;
455 /* sysctl kernel.kptr_restrict was set */
456 if (!sym->address)
457 goto out;
458 }
459 if (sym->address)
460 dd->sym_count++;
461 }
462
463 fclose(fp);
464
465 qsort(dd->sym_mapping, dd->sym_count,
466 sizeof(*dd->sym_mapping), kernel_syms_cmp);
467}
468
469static void kernel_syms_destroy(struct dump_data *dd)
470{
471 free(dd->sym_mapping);
472}
473
474static struct kernel_sym *kernel_syms_search(struct dump_data *dd,
475 unsigned long key)
476{
477 struct kernel_sym sym = {
478 .address = key,
479 };
480
481 return dd->sym_mapping ?
482 bsearch(&sym, dd->sym_mapping, dd->sym_count,
483 sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
484}
485
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700486static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
487{
488 va_list args;
489
490 va_start(args, fmt);
491 vprintf(fmt, args);
492 va_end(args);
493}
494
Daniel Borkmann7105e822017-12-20 13:42:57 +0100495static const char *print_call_pcrel(struct dump_data *dd,
496 struct kernel_sym *sym,
497 unsigned long address,
498 const struct bpf_insn *insn)
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700499{
Daniel Borkmann7105e822017-12-20 13:42:57 +0100500 if (sym)
501 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
502 "%+d#%s", insn->off, sym->name);
503 else
504 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
505 "%+d#0x%lx", insn->off, address);
506 return dd->scratch_buff;
507}
508
509static const char *print_call_helper(struct dump_data *dd,
510 struct kernel_sym *sym,
511 unsigned long address)
512{
513 if (sym)
514 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
515 "%s", sym->name);
516 else
517 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
518 "0x%lx", address);
519 return dd->scratch_buff;
520}
521
522static const char *print_call(void *private_data,
523 const struct bpf_insn *insn)
524{
525 struct dump_data *dd = private_data;
526 unsigned long address = dd->address_call_base + insn->imm;
527 struct kernel_sym *sym;
528
529 sym = kernel_syms_search(dd, address);
530 if (insn->src_reg == BPF_PSEUDO_CALL)
531 return print_call_pcrel(dd, sym, address, insn);
532 else
533 return print_call_helper(dd, sym, address);
534}
535
536static const char *print_imm(void *private_data,
537 const struct bpf_insn *insn,
538 __u64 full_imm)
539{
540 struct dump_data *dd = private_data;
541
542 if (insn->src_reg == BPF_PSEUDO_MAP_FD)
543 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
544 "map[id:%u]", insn->imm);
545 else
546 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
547 "0x%llx", (unsigned long long)full_imm);
548 return dd->scratch_buff;
549}
550
551static void dump_xlated_plain(struct dump_data *dd, void *buf,
552 unsigned int len, bool opcodes)
553{
554 const struct bpf_insn_cbs cbs = {
555 .cb_print = print_insn,
556 .cb_call = print_call,
557 .cb_imm = print_imm,
558 .private_data = dd,
559 };
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700560 struct bpf_insn *insn = buf;
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700561 bool double_insn = false;
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700562 unsigned int i;
563
564 for (i = 0; i < len / sizeof(*insn); i++) {
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700565 if (double_insn) {
566 double_insn = false;
567 continue;
568 }
569
570 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
571
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700572 printf("% 4d: ", i);
Daniel Borkmann7105e822017-12-20 13:42:57 +0100573 print_bpf_insn(&cbs, NULL, insn + i, true);
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700574
575 if (opcodes) {
576 printf(" ");
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700577 fprint_hex(stdout, insn + i, 8, " ");
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700578 if (double_insn && i < len - 1) {
579 printf(" ");
580 fprint_hex(stdout, insn + i + 1, 8, " ");
581 }
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700582 printf("\n");
583 }
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700584 }
585}
586
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700587static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
588{
589 unsigned int l = strlen(fmt);
590 char chomped_fmt[l];
591 va_list args;
592
593 va_start(args, fmt);
594 if (l > 0) {
595 strncpy(chomped_fmt, fmt, l - 1);
596 chomped_fmt[l - 1] = '\0';
597 }
598 jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
599 va_end(args);
600}
601
Daniel Borkmann7105e822017-12-20 13:42:57 +0100602static void dump_xlated_json(struct dump_data *dd, void *buf,
603 unsigned int len, bool opcodes)
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700604{
Daniel Borkmann7105e822017-12-20 13:42:57 +0100605 const struct bpf_insn_cbs cbs = {
606 .cb_print = print_insn_json,
607 .cb_call = print_call,
608 .cb_imm = print_imm,
609 .private_data = dd,
610 };
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700611 struct bpf_insn *insn = buf;
612 bool double_insn = false;
613 unsigned int i;
614
615 jsonw_start_array(json_wtr);
616 for (i = 0; i < len / sizeof(*insn); i++) {
617 if (double_insn) {
618 double_insn = false;
619 continue;
620 }
621 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
622
623 jsonw_start_object(json_wtr);
624 jsonw_name(json_wtr, "disasm");
Daniel Borkmann7105e822017-12-20 13:42:57 +0100625 print_bpf_insn(&cbs, NULL, insn + i, true);
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700626
627 if (opcodes) {
628 jsonw_name(json_wtr, "opcodes");
629 jsonw_start_object(json_wtr);
630
631 jsonw_name(json_wtr, "code");
632 jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
633
634 jsonw_name(json_wtr, "src_reg");
635 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
636
637 jsonw_name(json_wtr, "dst_reg");
638 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
639
640 jsonw_name(json_wtr, "off");
641 print_hex_data_json((uint8_t *)(&insn[i].off), 2);
642
643 jsonw_name(json_wtr, "imm");
644 if (double_insn && i < len - 1)
645 print_hex_data_json((uint8_t *)(&insn[i].imm),
646 12);
647 else
648 print_hex_data_json((uint8_t *)(&insn[i].imm),
649 4);
650 jsonw_end_object(json_wtr);
651 }
652 jsonw_end_object(json_wtr);
653 }
654 jsonw_end_array(json_wtr);
655}
656
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700657static int do_dump(int argc, char **argv)
658{
659 struct bpf_prog_info info = {};
Daniel Borkmann7105e822017-12-20 13:42:57 +0100660 struct dump_data dd = {};
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700661 __u32 len = sizeof(info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700662 unsigned int buf_size;
663 char *filepath = NULL;
664 bool opcodes = false;
665 unsigned char *buf;
666 __u32 *member_len;
667 __u64 *member_ptr;
668 ssize_t n;
669 int err;
670 int fd;
671
672 if (is_prefix(*argv, "jited")) {
673 member_len = &info.jited_prog_len;
674 member_ptr = &info.jited_prog_insns;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700675 } else if (is_prefix(*argv, "xlated")) {
676 member_len = &info.xlated_prog_len;
677 member_ptr = &info.xlated_prog_insns;
678 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700679 p_err("expected 'xlated' or 'jited', got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700680 return -1;
681 }
682 NEXT_ARG();
683
684 if (argc < 2)
685 usage();
686
687 fd = prog_parse_fd(&argc, &argv);
688 if (fd < 0)
689 return -1;
690
691 if (is_prefix(*argv, "file")) {
692 NEXT_ARG();
693 if (!argc) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700694 p_err("expected file path");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700695 return -1;
696 }
697
698 filepath = *argv;
699 NEXT_ARG();
700 } else if (is_prefix(*argv, "opcodes")) {
701 opcodes = true;
702 NEXT_ARG();
703 }
704
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700705 if (argc) {
706 usage();
707 return -1;
708 }
709
710 err = bpf_obj_get_info_by_fd(fd, &info, &len);
711 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700712 p_err("can't get prog info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700713 return -1;
714 }
715
716 if (!*member_len) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700717 p_info("no instructions returned");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700718 close(fd);
719 return 0;
720 }
721
722 buf_size = *member_len;
723
724 buf = malloc(buf_size);
725 if (!buf) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700726 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700727 close(fd);
728 return -1;
729 }
730
731 memset(&info, 0, sizeof(info));
732
733 *member_ptr = ptr_to_u64(buf);
734 *member_len = buf_size;
735
736 err = bpf_obj_get_info_by_fd(fd, &info, &len);
737 close(fd);
738 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700739 p_err("can't get prog info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700740 goto err_free;
741 }
742
743 if (*member_len > buf_size) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700744 p_err("too many instructions returned");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700745 goto err_free;
746 }
747
Daniel Borkmann7105e822017-12-20 13:42:57 +0100748 if ((member_len == &info.jited_prog_len &&
749 info.jited_prog_insns == 0) ||
750 (member_len == &info.xlated_prog_len &&
751 info.xlated_prog_insns == 0)) {
752 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
753 goto err_free;
754 }
755
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700756 if (filepath) {
757 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
758 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700759 p_err("can't open file %s: %s", filepath,
760 strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700761 goto err_free;
762 }
763
764 n = write(fd, buf, *member_len);
765 close(fd);
766 if (n != *member_len) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700767 p_err("error writing output file: %s",
768 n < 0 ? strerror(errno) : "short write");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700769 goto err_free;
770 }
771 } else {
Daniel Borkmann7105e822017-12-20 13:42:57 +0100772 if (member_len == &info.jited_prog_len) {
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700773 disasm_print_insn(buf, *member_len, opcodes);
Daniel Borkmann7105e822017-12-20 13:42:57 +0100774 } else {
775 kernel_syms_load(&dd);
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700776 if (json_output)
Daniel Borkmann7105e822017-12-20 13:42:57 +0100777 dump_xlated_json(&dd, buf, *member_len, opcodes);
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700778 else
Daniel Borkmann7105e822017-12-20 13:42:57 +0100779 dump_xlated_plain(&dd, buf, *member_len, opcodes);
780 kernel_syms_destroy(&dd);
781 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700782 }
783
784 free(buf);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700785 return 0;
786
787err_free:
788 free(buf);
789 return -1;
790}
791
792static int do_pin(int argc, char **argv)
793{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700794 int err;
795
796 err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
797 if (!err && json_output)
798 jsonw_null(json_wtr);
799 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700800}
801
Roman Gushchin49a086c2017-12-13 15:18:53 +0000802static int do_load(int argc, char **argv)
803{
804 struct bpf_object *obj;
805 int prog_fd;
806
807 if (argc != 2)
808 usage();
809
810 if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
811 p_err("failed to load program\n");
812 return -1;
813 }
814
815 if (do_pin_fd(prog_fd, argv[1])) {
816 p_err("failed to pin program\n");
817 return -1;
818 }
819
820 if (json_output)
821 jsonw_null(json_wtr);
822
823 return 0;
824}
825
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700826static int do_help(int argc, char **argv)
827{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700828 if (json_output) {
829 jsonw_null(json_wtr);
830 return 0;
831 }
832
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700833 fprintf(stderr,
834 "Usage: %s %s show [PROG]\n"
Quentin Monnet8dfbc6d2017-10-19 15:46:25 -0700835 " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
836 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700837 " %s %s pin PROG FILE\n"
Roman Gushchin49a086c2017-12-13 15:18:53 +0000838 " %s %s load OBJ FILE\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700839 " %s %s help\n"
840 "\n"
841 " " HELP_SPEC_PROGRAM "\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -0700842 " " HELP_SPEC_OPTIONS "\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700843 "",
844 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
Roman Gushchin49a086c2017-12-13 15:18:53 +0000845 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700846
847 return 0;
848}
849
850static const struct cmd cmds[] = {
851 { "show", do_show },
Quentin Monnet9f606172017-10-19 15:46:22 -0700852 { "help", do_help },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700853 { "dump", do_dump },
854 { "pin", do_pin },
Roman Gushchin49a086c2017-12-13 15:18:53 +0000855 { "load", do_load },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700856 { 0 }
857};
858
859int do_prog(int argc, char **argv)
860{
861 return cmd_select(cmds, argc, argv, do_help);
862}