static int do_version(int argc, char **argv) { if (json_output) { jsonw_start_object(json_wtr); jsonw_name(json_wtr, "version"); jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION); jsonw_end_object(json_wtr); } else { printf("%s v%s\n", bin_name, BPFTOOL_VERSION); } return 0; }
static void _bridge_print_timer(FILE *f, const char *attr, struct rtattr *timer) { struct timeval tv; __jiffies_to_tv(&tv, rta_getattr_u64(timer)); if (is_json_context()) { json_writer_t *jw = get_json_writer(); jsonw_name(jw, attr); jsonw_printf(jw, "%i.%.2i", (int)tv.tv_sec, (int)tv.tv_usec / 10000); } else { fprintf(f, "%s %4i.%.2i ", attr, (int)tv.tv_sec, (int)tv.tv_usec / 10000); } }
void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len, bool opcodes, bool linum) { const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo; const struct bpf_insn_cbs cbs = { .cb_print = print_insn_json, .cb_call = print_call, .cb_imm = print_imm, .private_data = dd, }; struct bpf_func_info *record; struct bpf_insn *insn = buf; struct btf *btf = dd->btf; bool double_insn = false; unsigned int nr_skip = 0; char func_sig[1024]; unsigned int i; jsonw_start_array(json_wtr); record = dd->func_info; for (i = 0; i < len / sizeof(*insn); i++) { if (double_insn) { double_insn = false; continue; } double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW); jsonw_start_object(json_wtr); if (btf && record) { if (record->insn_off == i) { btf_dumper_type_only(btf, record->type_id, func_sig, sizeof(func_sig)); if (func_sig[0] != '\0') { jsonw_name(json_wtr, "proto"); jsonw_string(json_wtr, func_sig); } record = (void *)record + dd->finfo_rec_size; } } if (prog_linfo) { const struct bpf_line_info *linfo; linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip); if (linfo) { btf_dump_linfo_json(btf, linfo, linum); nr_skip++; } } jsonw_name(json_wtr, "disasm"); print_bpf_insn(&cbs, insn + i, true); if (opcodes) { jsonw_name(json_wtr, "opcodes"); jsonw_start_object(json_wtr); jsonw_name(json_wtr, "code"); jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code); jsonw_name(json_wtr, "src_reg"); jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg); jsonw_name(json_wtr, "dst_reg"); jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg); jsonw_name(json_wtr, "off"); print_hex_data_json((uint8_t *)(&insn[i].off), 2); jsonw_name(json_wtr, "imm"); if (double_insn && i < len - 1) print_hex_data_json((uint8_t *)(&insn[i].imm), 12); else print_hex_data_json((uint8_t *)(&insn[i].imm), 4); jsonw_end_object(json_wtr); } jsonw_end_object(json_wtr); } jsonw_end_array(json_wtr); }
void jsonw_s64(json_writer_t *self, int64_t num) { jsonw_printf(self, "%"PRId64, num); }
void jsonw_int(json_writer_t *self, int num) { jsonw_printf(self, "%d", num); }
void jsonw_lluint(json_writer_t *self, unsigned long long num) { jsonw_printf(self, "%llu", num); }
void jsonw_xint(json_writer_t *self, uint64_t num) { jsonw_printf(self, "%"PRIx64, num); }
void jsonw_u64(json_writer_t *self, uint64_t num) { jsonw_printf(self, "%"PRIu64, num); }
void jsonw_uint(json_writer_t *self, unsigned int num) { jsonw_printf(self, "%u", num); }
void jsonw_hu(json_writer_t *self, unsigned short num) { jsonw_printf(self, "%hu", num); }
void jsonw_hhu(json_writer_t *self, unsigned char num) { jsonw_printf(self, "%hhu", num); }
void jsonw_float(json_writer_t *self, double num) { jsonw_printf(self, "%g", num); }
void jsonw_null(json_writer_t *self) { jsonw_printf(self, "null"); }
void jsonw_bool(json_writer_t *self, bool val) { jsonw_printf(self, "%s", val ? "true" : "false"); }
void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num) { jsonw_printf(self, fmt, num); }