prog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  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. /* Author: Jakub Kicinski <kubakici@wp.pl> */
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <bpf.h>
  45. #include <libbpf.h>
  46. #include "main.h"
  47. #include "disasm.h"
  48. static const char * const prog_type_name[] = {
  49. [BPF_PROG_TYPE_UNSPEC] = "unspec",
  50. [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
  51. [BPF_PROG_TYPE_KPROBE] = "kprobe",
  52. [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
  53. [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
  54. [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
  55. [BPF_PROG_TYPE_XDP] = "xdp",
  56. [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
  57. [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
  58. [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
  59. [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
  60. [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
  61. [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
  62. [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
  63. [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
  64. [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
  65. };
  66. static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
  67. {
  68. struct timespec real_time_ts, boot_time_ts;
  69. time_t wallclock_secs;
  70. struct tm load_tm;
  71. buf[--size] = '\0';
  72. if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
  73. clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
  74. perror("Can't read clocks");
  75. snprintf(buf, size, "%llu", nsecs / 1000000000);
  76. return;
  77. }
  78. wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
  79. nsecs / 1000000000;
  80. if (!localtime_r(&wallclock_secs, &load_tm)) {
  81. snprintf(buf, size, "%llu", nsecs / 1000000000);
  82. return;
  83. }
  84. strftime(buf, size, "%b %d/%H:%M", &load_tm);
  85. }
  86. static int prog_fd_by_tag(unsigned char *tag)
  87. {
  88. struct bpf_prog_info info = {};
  89. __u32 len = sizeof(info);
  90. unsigned int id = 0;
  91. int err;
  92. int fd;
  93. while (true) {
  94. err = bpf_prog_get_next_id(id, &id);
  95. if (err) {
  96. p_err("%s", strerror(errno));
  97. return -1;
  98. }
  99. fd = bpf_prog_get_fd_by_id(id);
  100. if (fd < 0) {
  101. p_err("can't get prog by id (%u): %s",
  102. id, strerror(errno));
  103. return -1;
  104. }
  105. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  106. if (err) {
  107. p_err("can't get prog info (%u): %s",
  108. id, strerror(errno));
  109. close(fd);
  110. return -1;
  111. }
  112. if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
  113. return fd;
  114. close(fd);
  115. }
  116. }
  117. int prog_parse_fd(int *argc, char ***argv)
  118. {
  119. int fd;
  120. if (is_prefix(**argv, "id")) {
  121. unsigned int id;
  122. char *endptr;
  123. NEXT_ARGP();
  124. id = strtoul(**argv, &endptr, 0);
  125. if (*endptr) {
  126. p_err("can't parse %s as ID", **argv);
  127. return -1;
  128. }
  129. NEXT_ARGP();
  130. fd = bpf_prog_get_fd_by_id(id);
  131. if (fd < 0)
  132. p_err("get by id (%u): %s", id, strerror(errno));
  133. return fd;
  134. } else if (is_prefix(**argv, "tag")) {
  135. unsigned char tag[BPF_TAG_SIZE];
  136. NEXT_ARGP();
  137. if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
  138. tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
  139. != BPF_TAG_SIZE) {
  140. p_err("can't parse tag");
  141. return -1;
  142. }
  143. NEXT_ARGP();
  144. return prog_fd_by_tag(tag);
  145. } else if (is_prefix(**argv, "pinned")) {
  146. char *path;
  147. NEXT_ARGP();
  148. path = **argv;
  149. NEXT_ARGP();
  150. return open_obj_pinned_any(path, BPF_OBJ_PROG);
  151. }
  152. p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
  153. return -1;
  154. }
  155. static void show_prog_maps(int fd, u32 num_maps)
  156. {
  157. struct bpf_prog_info info = {};
  158. __u32 len = sizeof(info);
  159. __u32 map_ids[num_maps];
  160. unsigned int i;
  161. int err;
  162. info.nr_map_ids = num_maps;
  163. info.map_ids = ptr_to_u64(map_ids);
  164. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  165. if (err || !info.nr_map_ids)
  166. return;
  167. if (json_output) {
  168. jsonw_name(json_wtr, "map_ids");
  169. jsonw_start_array(json_wtr);
  170. for (i = 0; i < info.nr_map_ids; i++)
  171. jsonw_uint(json_wtr, map_ids[i]);
  172. jsonw_end_array(json_wtr);
  173. } else {
  174. printf(" map_ids ");
  175. for (i = 0; i < info.nr_map_ids; i++)
  176. printf("%u%s", map_ids[i],
  177. i == info.nr_map_ids - 1 ? "" : ",");
  178. }
  179. }
  180. static void print_prog_json(struct bpf_prog_info *info, int fd)
  181. {
  182. char *memlock;
  183. jsonw_start_object(json_wtr);
  184. jsonw_uint_field(json_wtr, "id", info->id);
  185. if (info->type < ARRAY_SIZE(prog_type_name))
  186. jsonw_string_field(json_wtr, "type",
  187. prog_type_name[info->type]);
  188. else
  189. jsonw_uint_field(json_wtr, "type", info->type);
  190. if (*info->name)
  191. jsonw_string_field(json_wtr, "name", info->name);
  192. jsonw_name(json_wtr, "tag");
  193. jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
  194. info->tag[0], info->tag[1], info->tag[2], info->tag[3],
  195. info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
  196. print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
  197. if (info->load_time) {
  198. char buf[32];
  199. print_boot_time(info->load_time, buf, sizeof(buf));
  200. /* Piggy back on load_time, since 0 uid is a valid one */
  201. jsonw_string_field(json_wtr, "loaded_at", buf);
  202. jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
  203. }
  204. jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
  205. if (info->jited_prog_len) {
  206. jsonw_bool_field(json_wtr, "jited", true);
  207. jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
  208. } else {
  209. jsonw_bool_field(json_wtr, "jited", false);
  210. }
  211. memlock = get_fdinfo(fd, "memlock");
  212. if (memlock)
  213. jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
  214. free(memlock);
  215. if (info->nr_map_ids)
  216. show_prog_maps(fd, info->nr_map_ids);
  217. if (!hash_empty(prog_table.table)) {
  218. struct pinned_obj *obj;
  219. jsonw_name(json_wtr, "pinned");
  220. jsonw_start_array(json_wtr);
  221. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  222. if (obj->id == info->id)
  223. jsonw_string(json_wtr, obj->path);
  224. }
  225. jsonw_end_array(json_wtr);
  226. }
  227. jsonw_end_object(json_wtr);
  228. }
  229. static void print_prog_plain(struct bpf_prog_info *info, int fd)
  230. {
  231. char *memlock;
  232. printf("%u: ", info->id);
  233. if (info->type < ARRAY_SIZE(prog_type_name))
  234. printf("%s ", prog_type_name[info->type]);
  235. else
  236. printf("type %u ", info->type);
  237. if (*info->name)
  238. printf("name %s ", info->name);
  239. printf("tag ");
  240. fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
  241. print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
  242. printf("\n");
  243. if (info->load_time) {
  244. char buf[32];
  245. print_boot_time(info->load_time, buf, sizeof(buf));
  246. /* Piggy back on load_time, since 0 uid is a valid one */
  247. printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
  248. }
  249. printf("\txlated %uB", info->xlated_prog_len);
  250. if (info->jited_prog_len)
  251. printf(" jited %uB", info->jited_prog_len);
  252. else
  253. printf(" not jited");
  254. memlock = get_fdinfo(fd, "memlock");
  255. if (memlock)
  256. printf(" memlock %sB", memlock);
  257. free(memlock);
  258. if (info->nr_map_ids)
  259. show_prog_maps(fd, info->nr_map_ids);
  260. if (!hash_empty(prog_table.table)) {
  261. struct pinned_obj *obj;
  262. printf("\n");
  263. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  264. if (obj->id == info->id)
  265. printf("\tpinned %s\n", obj->path);
  266. }
  267. }
  268. printf("\n");
  269. }
  270. static int show_prog(int fd)
  271. {
  272. struct bpf_prog_info info = {};
  273. __u32 len = sizeof(info);
  274. int err;
  275. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  276. if (err) {
  277. p_err("can't get prog info: %s", strerror(errno));
  278. return -1;
  279. }
  280. if (json_output)
  281. print_prog_json(&info, fd);
  282. else
  283. print_prog_plain(&info, fd);
  284. return 0;
  285. }
  286. static int do_show(int argc, char **argv)
  287. {
  288. __u32 id = 0;
  289. int err;
  290. int fd;
  291. if (show_pinned)
  292. build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
  293. if (argc == 2) {
  294. fd = prog_parse_fd(&argc, &argv);
  295. if (fd < 0)
  296. return -1;
  297. return show_prog(fd);
  298. }
  299. if (argc)
  300. return BAD_ARG();
  301. if (json_output)
  302. jsonw_start_array(json_wtr);
  303. while (true) {
  304. err = bpf_prog_get_next_id(id, &id);
  305. if (err) {
  306. if (errno == ENOENT) {
  307. err = 0;
  308. break;
  309. }
  310. p_err("can't get next program: %s%s", strerror(errno),
  311. errno == EINVAL ? " -- kernel too old?" : "");
  312. err = -1;
  313. break;
  314. }
  315. fd = bpf_prog_get_fd_by_id(id);
  316. if (fd < 0) {
  317. if (errno == ENOENT)
  318. continue;
  319. p_err("can't get prog by id (%u): %s",
  320. id, strerror(errno));
  321. err = -1;
  322. break;
  323. }
  324. err = show_prog(fd);
  325. close(fd);
  326. if (err)
  327. break;
  328. }
  329. if (json_output)
  330. jsonw_end_array(json_wtr);
  331. return err;
  332. }
  333. #define SYM_MAX_NAME 256
  334. struct kernel_sym {
  335. unsigned long address;
  336. char name[SYM_MAX_NAME];
  337. };
  338. struct dump_data {
  339. unsigned long address_call_base;
  340. struct kernel_sym *sym_mapping;
  341. __u32 sym_count;
  342. char scratch_buff[SYM_MAX_NAME];
  343. };
  344. static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
  345. {
  346. return ((struct kernel_sym *)sym_a)->address -
  347. ((struct kernel_sym *)sym_b)->address;
  348. }
  349. static void kernel_syms_load(struct dump_data *dd)
  350. {
  351. struct kernel_sym *sym;
  352. char buff[256];
  353. void *tmp, *address;
  354. FILE *fp;
  355. fp = fopen("/proc/kallsyms", "r");
  356. if (!fp)
  357. return;
  358. while (!feof(fp)) {
  359. if (!fgets(buff, sizeof(buff), fp))
  360. break;
  361. tmp = realloc(dd->sym_mapping,
  362. (dd->sym_count + 1) *
  363. sizeof(*dd->sym_mapping));
  364. if (!tmp) {
  365. out:
  366. free(dd->sym_mapping);
  367. dd->sym_mapping = NULL;
  368. fclose(fp);
  369. return;
  370. }
  371. dd->sym_mapping = tmp;
  372. sym = &dd->sym_mapping[dd->sym_count];
  373. if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
  374. continue;
  375. sym->address = (unsigned long)address;
  376. if (!strcmp(sym->name, "__bpf_call_base")) {
  377. dd->address_call_base = sym->address;
  378. /* sysctl kernel.kptr_restrict was set */
  379. if (!sym->address)
  380. goto out;
  381. }
  382. if (sym->address)
  383. dd->sym_count++;
  384. }
  385. fclose(fp);
  386. qsort(dd->sym_mapping, dd->sym_count,
  387. sizeof(*dd->sym_mapping), kernel_syms_cmp);
  388. }
  389. static void kernel_syms_destroy(struct dump_data *dd)
  390. {
  391. free(dd->sym_mapping);
  392. }
  393. static struct kernel_sym *kernel_syms_search(struct dump_data *dd,
  394. unsigned long key)
  395. {
  396. struct kernel_sym sym = {
  397. .address = key,
  398. };
  399. return dd->sym_mapping ?
  400. bsearch(&sym, dd->sym_mapping, dd->sym_count,
  401. sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
  402. }
  403. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  404. {
  405. va_list args;
  406. va_start(args, fmt);
  407. vprintf(fmt, args);
  408. va_end(args);
  409. }
  410. static const char *print_call_pcrel(struct dump_data *dd,
  411. struct kernel_sym *sym,
  412. unsigned long address,
  413. const struct bpf_insn *insn)
  414. {
  415. if (sym)
  416. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  417. "%+d#%s", insn->off, sym->name);
  418. else
  419. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  420. "%+d#0x%lx", insn->off, address);
  421. return dd->scratch_buff;
  422. }
  423. static const char *print_call_helper(struct dump_data *dd,
  424. struct kernel_sym *sym,
  425. unsigned long address)
  426. {
  427. if (sym)
  428. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  429. "%s", sym->name);
  430. else
  431. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  432. "0x%lx", address);
  433. return dd->scratch_buff;
  434. }
  435. static const char *print_call(void *private_data,
  436. const struct bpf_insn *insn)
  437. {
  438. struct dump_data *dd = private_data;
  439. unsigned long address = dd->address_call_base + insn->imm;
  440. struct kernel_sym *sym;
  441. sym = kernel_syms_search(dd, address);
  442. if (insn->src_reg == BPF_PSEUDO_CALL)
  443. return print_call_pcrel(dd, sym, address, insn);
  444. else
  445. return print_call_helper(dd, sym, address);
  446. }
  447. static const char *print_imm(void *private_data,
  448. const struct bpf_insn *insn,
  449. __u64 full_imm)
  450. {
  451. struct dump_data *dd = private_data;
  452. if (insn->src_reg == BPF_PSEUDO_MAP_FD)
  453. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  454. "map[id:%u]", insn->imm);
  455. else
  456. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  457. "0x%llx", (unsigned long long)full_imm);
  458. return dd->scratch_buff;
  459. }
  460. static void dump_xlated_plain(struct dump_data *dd, void *buf,
  461. unsigned int len, bool opcodes)
  462. {
  463. const struct bpf_insn_cbs cbs = {
  464. .cb_print = print_insn,
  465. .cb_call = print_call,
  466. .cb_imm = print_imm,
  467. .private_data = dd,
  468. };
  469. struct bpf_insn *insn = buf;
  470. bool double_insn = false;
  471. unsigned int i;
  472. for (i = 0; i < len / sizeof(*insn); i++) {
  473. if (double_insn) {
  474. double_insn = false;
  475. continue;
  476. }
  477. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  478. printf("% 4d: ", i);
  479. print_bpf_insn(&cbs, NULL, insn + i, true);
  480. if (opcodes) {
  481. printf(" ");
  482. fprint_hex(stdout, insn + i, 8, " ");
  483. if (double_insn && i < len - 1) {
  484. printf(" ");
  485. fprint_hex(stdout, insn + i + 1, 8, " ");
  486. }
  487. printf("\n");
  488. }
  489. }
  490. }
  491. static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
  492. {
  493. unsigned int l = strlen(fmt);
  494. char chomped_fmt[l];
  495. va_list args;
  496. va_start(args, fmt);
  497. if (l > 0) {
  498. strncpy(chomped_fmt, fmt, l - 1);
  499. chomped_fmt[l - 1] = '\0';
  500. }
  501. jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
  502. va_end(args);
  503. }
  504. static void dump_xlated_json(struct dump_data *dd, void *buf,
  505. unsigned int len, bool opcodes)
  506. {
  507. const struct bpf_insn_cbs cbs = {
  508. .cb_print = print_insn_json,
  509. .cb_call = print_call,
  510. .cb_imm = print_imm,
  511. .private_data = dd,
  512. };
  513. struct bpf_insn *insn = buf;
  514. bool double_insn = false;
  515. unsigned int i;
  516. jsonw_start_array(json_wtr);
  517. for (i = 0; i < len / sizeof(*insn); i++) {
  518. if (double_insn) {
  519. double_insn = false;
  520. continue;
  521. }
  522. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  523. jsonw_start_object(json_wtr);
  524. jsonw_name(json_wtr, "disasm");
  525. print_bpf_insn(&cbs, NULL, insn + i, true);
  526. if (opcodes) {
  527. jsonw_name(json_wtr, "opcodes");
  528. jsonw_start_object(json_wtr);
  529. jsonw_name(json_wtr, "code");
  530. jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
  531. jsonw_name(json_wtr, "src_reg");
  532. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
  533. jsonw_name(json_wtr, "dst_reg");
  534. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
  535. jsonw_name(json_wtr, "off");
  536. print_hex_data_json((uint8_t *)(&insn[i].off), 2);
  537. jsonw_name(json_wtr, "imm");
  538. if (double_insn && i < len - 1)
  539. print_hex_data_json((uint8_t *)(&insn[i].imm),
  540. 12);
  541. else
  542. print_hex_data_json((uint8_t *)(&insn[i].imm),
  543. 4);
  544. jsonw_end_object(json_wtr);
  545. }
  546. jsonw_end_object(json_wtr);
  547. }
  548. jsonw_end_array(json_wtr);
  549. }
  550. static int do_dump(int argc, char **argv)
  551. {
  552. struct bpf_prog_info info = {};
  553. struct dump_data dd = {};
  554. __u32 len = sizeof(info);
  555. unsigned int buf_size;
  556. char *filepath = NULL;
  557. bool opcodes = false;
  558. unsigned char *buf;
  559. __u32 *member_len;
  560. __u64 *member_ptr;
  561. ssize_t n;
  562. int err;
  563. int fd;
  564. if (is_prefix(*argv, "jited")) {
  565. member_len = &info.jited_prog_len;
  566. member_ptr = &info.jited_prog_insns;
  567. } else if (is_prefix(*argv, "xlated")) {
  568. member_len = &info.xlated_prog_len;
  569. member_ptr = &info.xlated_prog_insns;
  570. } else {
  571. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  572. return -1;
  573. }
  574. NEXT_ARG();
  575. if (argc < 2)
  576. usage();
  577. fd = prog_parse_fd(&argc, &argv);
  578. if (fd < 0)
  579. return -1;
  580. if (is_prefix(*argv, "file")) {
  581. NEXT_ARG();
  582. if (!argc) {
  583. p_err("expected file path");
  584. return -1;
  585. }
  586. filepath = *argv;
  587. NEXT_ARG();
  588. } else if (is_prefix(*argv, "opcodes")) {
  589. opcodes = true;
  590. NEXT_ARG();
  591. }
  592. if (argc) {
  593. usage();
  594. return -1;
  595. }
  596. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  597. if (err) {
  598. p_err("can't get prog info: %s", strerror(errno));
  599. return -1;
  600. }
  601. if (!*member_len) {
  602. p_info("no instructions returned");
  603. close(fd);
  604. return 0;
  605. }
  606. buf_size = *member_len;
  607. buf = malloc(buf_size);
  608. if (!buf) {
  609. p_err("mem alloc failed");
  610. close(fd);
  611. return -1;
  612. }
  613. memset(&info, 0, sizeof(info));
  614. *member_ptr = ptr_to_u64(buf);
  615. *member_len = buf_size;
  616. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  617. close(fd);
  618. if (err) {
  619. p_err("can't get prog info: %s", strerror(errno));
  620. goto err_free;
  621. }
  622. if (*member_len > buf_size) {
  623. p_err("too many instructions returned");
  624. goto err_free;
  625. }
  626. if ((member_len == &info.jited_prog_len &&
  627. info.jited_prog_insns == 0) ||
  628. (member_len == &info.xlated_prog_len &&
  629. info.xlated_prog_insns == 0)) {
  630. p_err("error retrieving insn dump: kernel.kptr_restrict set?");
  631. goto err_free;
  632. }
  633. if (filepath) {
  634. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  635. if (fd < 0) {
  636. p_err("can't open file %s: %s", filepath,
  637. strerror(errno));
  638. goto err_free;
  639. }
  640. n = write(fd, buf, *member_len);
  641. close(fd);
  642. if (n != *member_len) {
  643. p_err("error writing output file: %s",
  644. n < 0 ? strerror(errno) : "short write");
  645. goto err_free;
  646. }
  647. } else {
  648. if (member_len == &info.jited_prog_len) {
  649. const char *name = NULL;
  650. if (info.ifindex) {
  651. name = ifindex_to_bfd_name_ns(info.ifindex,
  652. info.netns_dev,
  653. info.netns_ino);
  654. if (!name)
  655. goto err_free;
  656. }
  657. disasm_print_insn(buf, *member_len, opcodes, name);
  658. } else {
  659. kernel_syms_load(&dd);
  660. if (json_output)
  661. dump_xlated_json(&dd, buf, *member_len, opcodes);
  662. else
  663. dump_xlated_plain(&dd, buf, *member_len, opcodes);
  664. kernel_syms_destroy(&dd);
  665. }
  666. }
  667. free(buf);
  668. return 0;
  669. err_free:
  670. free(buf);
  671. return -1;
  672. }
  673. static int do_pin(int argc, char **argv)
  674. {
  675. int err;
  676. err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  677. if (!err && json_output)
  678. jsonw_null(json_wtr);
  679. return err;
  680. }
  681. static int do_load(int argc, char **argv)
  682. {
  683. struct bpf_object *obj;
  684. int prog_fd;
  685. if (argc != 2)
  686. usage();
  687. if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
  688. p_err("failed to load program");
  689. return -1;
  690. }
  691. if (do_pin_fd(prog_fd, argv[1])) {
  692. p_err("failed to pin program");
  693. return -1;
  694. }
  695. if (json_output)
  696. jsonw_null(json_wtr);
  697. return 0;
  698. }
  699. static int do_help(int argc, char **argv)
  700. {
  701. if (json_output) {
  702. jsonw_null(json_wtr);
  703. return 0;
  704. }
  705. fprintf(stderr,
  706. "Usage: %s %s { show | list } [PROG]\n"
  707. " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
  708. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  709. " %s %s pin PROG FILE\n"
  710. " %s %s load OBJ FILE\n"
  711. " %s %s help\n"
  712. "\n"
  713. " " HELP_SPEC_PROGRAM "\n"
  714. " " HELP_SPEC_OPTIONS "\n"
  715. "",
  716. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  717. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
  718. return 0;
  719. }
  720. static const struct cmd cmds[] = {
  721. { "show", do_show },
  722. { "list", do_show },
  723. { "help", do_help },
  724. { "dump", do_dump },
  725. { "pin", do_pin },
  726. { "load", do_load },
  727. { 0 }
  728. };
  729. int do_prog(int argc, char **argv)
  730. {
  731. return cmd_select(cmds, argc, argv, do_help);
  732. }