prog.c 19 KB

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