Display the inline flags and the attached inlined instances of the kprobe multi link. For each inlined instance display its address, cookie, name of the inlined function and the function it is inlined in: # bpftool link 3: kprobe_multi prog 10 kprobe.multi inline func_cnt 1 inline_cnt 2 addr cookie func [module] ffffffff825e9110 5 bpf_fentry_test1 addr cookie func, inlined in [module] ffffffff825eb4b9 6 bpf_kprobe_multi_inline_inner, bpf_kprobe_multi_inline_test1+0x9 ffffffff825eb4c9 6 bpf_kprobe_multi_inline_inner, bpf_kprobe_multi_inline_test2+0x9 The function the instance is inlined in is the kallsyms symbol covering the instance address. The name of the inlined function is resolved from the BTF type id returned in inline_ids, because several inlined functions can have their instance on the same address. In json output the instances are displayed in "inlines" array, together with "inline", "inline_strict", "inline_cnt" and "inline_skipped" fields. Signed-off-by: Jiri Olsa --- tools/bpf/bpftool/link.c | 348 ++++++++++++++++++++++++++++++++++----- 1 file changed, 303 insertions(+), 45 deletions(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 5cffa2d283b4..19daac789a4c 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -11,6 +11,7 @@ #include #include +#include #include #include "json_writer.h" @@ -254,6 +255,12 @@ struct addr_cookie { __u64 cookie; }; +struct addr_cookie_id { + __u64 addr; + __u64 cookie; + __u32 id; +}; + static int cmp_addr_cookie(const void *A, const void *B) { const struct addr_cookie *a = A, *b = B; @@ -263,6 +270,15 @@ static int cmp_addr_cookie(const void *A, const void *B) return a->addr < b->addr ? -1 : 1; } +static int cmp_addr_cookie_id(const void *A, const void *B) +{ + const struct addr_cookie_id *a = A, *b = B; + + if (a->addr == b->addr) + return 0; + return a->addr < b->addr ? -1 : 1; +} + static struct addr_cookie * get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count) { @@ -282,6 +298,29 @@ get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count) return data; } +static struct addr_cookie_id *get_addr_cookie_id_array(struct bpf_link_info *info) +{ + __u64 *cookies = u64_to_ptr(info->kprobe_multi.inline_cookies); + __u64 *addrs = u64_to_ptr(info->kprobe_multi.inline_addrs); + __u32 *ids = u64_to_ptr(info->kprobe_multi.inline_ids); + __u32 count = info->kprobe_multi.inline_count; + struct addr_cookie_id *data; + __u32 i; + + data = calloc(count, sizeof(data[0])); + if (!data) { + p_err("mem alloc failed"); + return NULL; + } + for (i = 0; i < count; i++) { + data[i].addr = addrs[i]; + data[i].cookie = cookies[i]; + data[i].id = ids[i]; + } + qsort(data, count, sizeof(data[0]), cmp_addr_cookie_id); + return data; +} + static bool is_x86_ibt_enabled(void) { #if defined(__x86_64__) @@ -322,33 +361,156 @@ symbol_matches_target(__u64 sym_addr, __u64 target_addr, bool is_ibt_enabled) return false; } +/* Inlined instances are placed inside functions, find the one covering @addr. */ +static struct kernel_sym *find_kernel_sym_by_range(__u64 addr) +{ + __u32 lo = 0, hi = dd.sym_count, mid; + + if (!addr) + return NULL; + + while (lo < hi) { + mid = lo + (hi - lo) / 2; + if (dd.sym_mapping[mid].address <= addr) + lo = mid + 1; + else + hi = mid; + } + return lo ? &dd.sym_mapping[lo - 1] : NULL; +} + +/* + * Inline BTF used to get names of inlined functions. The vmlinux one is + * loaded once on first use, for modules we keep the last used one. + */ +static struct { + struct btf *vmlinux; + struct btf *vmlinux_inline; + struct btf *mod; + struct btf *mod_inline; + char mod_name[MODULE_MAX_NAME]; + bool vmlinux_loaded; +} inline_btf; + +static struct btf *parse_sysfs_btf(const char *name, const char *suffix, struct btf *base) +{ + char path[PATH_MAX]; + struct btf *btf; + + snprintf(path, sizeof(path), "/sys/kernel/btf/%s%s", name, suffix); + btf = btf__parse_split(path, base); + return IS_ERR_OR_NULL(btf) ? NULL : btf; +} + +/* Get inline BTF for vmlinux or for @module, NULL if it's not available. */ +static struct btf *get_inline_btf(const char *module) +{ + if (!inline_btf.vmlinux_loaded) { + inline_btf.vmlinux_loaded = true; + inline_btf.vmlinux = libbpf_find_kernel_btf(); + if (IS_ERR_OR_NULL(inline_btf.vmlinux)) + inline_btf.vmlinux = NULL; + else + inline_btf.vmlinux_inline = parse_sysfs_btf("vmlinux", ".inline", + inline_btf.vmlinux); + } + if (!inline_btf.vmlinux) + return NULL; + if (module[0] == '\0') + return inline_btf.vmlinux_inline; + + if (strcmp(inline_btf.mod_name, module)) { + btf__free(inline_btf.mod_inline); + btf__free(inline_btf.mod); + inline_btf.mod_inline = NULL; + + /* Module inline BTF is split BTF on top of the module BTF. */ + snprintf(inline_btf.mod_name, sizeof(inline_btf.mod_name), "%s", module); + inline_btf.mod = parse_sysfs_btf(module, "", inline_btf.vmlinux); + if (inline_btf.mod) + inline_btf.mod_inline = parse_sysfs_btf(module, ".inline", + inline_btf.mod); + } + return inline_btf.mod_inline; +} + +static void free_inline_btf(void) +{ + /* The caller checks errno of the last link lookup, keep it. */ + int saved_errno = errno; + + btf__free(inline_btf.mod_inline); + btf__free(inline_btf.mod); + btf__free(inline_btf.vmlinux_inline); + btf__free(inline_btf.vmlinux); + memset(&inline_btf, 0, sizeof(inline_btf)); + errno = saved_errno; +} + +/* + * Get name of the inlined function with BTF type @id, the id is from + * inline BTF of the object the inlined instance is located in, which + * we get from @sym. Returns NULL if the name is not available. + */ +static const char *inline_func_name(const struct kernel_sym *sym, __u32 id) +{ + const struct btf_type *t; + const char *name; + struct btf *btf; + + if (!sym || !id) + return NULL; + btf = get_inline_btf(sym->module); + if (!btf) + return NULL; + t = btf__type_by_id(btf, id); + if (!t) + return NULL; + name = btf__name_by_offset(btf, t->name_off); + return name && name[0] ? name : NULL; +} + static void show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) { - struct addr_cookie *data; + __u32 inline_count = info->kprobe_multi.inline_count; + __u32 count = info->kprobe_multi.count; + struct addr_cookie_id *inlines = NULL; + struct addr_cookie *data = NULL; + struct kernel_sym *sym; + const char *name; __u32 i, j = 0; bool is_ibt_enabled; jsonw_bool_field(json_wtr, "retprobe", info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN); - jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count); + jsonw_bool_field(json_wtr, "inline", + info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_INLINE); + jsonw_bool_field(json_wtr, "inline_strict", + info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_INLINE_STRICT); + jsonw_uint_field(json_wtr, "func_cnt", count); jsonw_uint_field(json_wtr, "missed", info->kprobe_multi.missed); - jsonw_name(json_wtr, "funcs"); - jsonw_start_array(json_wtr); - data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs), - u64_to_ptr(info->kprobe_multi.cookies), - info->kprobe_multi.count); - if (!data) - return; + + if (count) + data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs), + u64_to_ptr(info->kprobe_multi.cookies), + count); + if (inline_count) + inlines = get_addr_cookie_id_array(info); /* Load it once for all. */ - if (!dd.sym_count) + if ((data || inlines) && !dd.sym_count) kernel_syms_load(&dd); - if (!dd.sym_count) - goto error; + /* The array is empty if we don't have its data or symbols. */ + if (!data || !dd.sym_count) + count = 0; + if (!inlines || !dd.sym_count) + inline_count = 0; is_ibt_enabled = is_x86_ibt_enabled(); - for (i = 0; i < dd.sym_count; i++) { + jsonw_name(json_wtr, "funcs"); + jsonw_start_array(json_wtr); + for (i = 0; count && i < dd.sym_count; i++) { if (!symbol_matches_target(dd.sym_mapping[i].address, data[j].addr, is_ibt_enabled)) continue; @@ -364,12 +526,50 @@ show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) } jsonw_uint_field(json_wtr, "cookie", data[j].cookie); jsonw_end_object(json_wtr); - if (++j == info->kprobe_multi.count) + if (++j == count) break; } jsonw_end_array(json_wtr); -error: + + jsonw_uint_field(json_wtr, "inline_cnt", info->kprobe_multi.inline_count); + jsonw_uint_field(json_wtr, "inline_skipped", info->kprobe_multi.inline_skipped); + jsonw_name(json_wtr, "inlines"); + jsonw_start_array(json_wtr); + for (i = 0; i < inline_count; i++) { + sym = find_kernel_sym_by_range(inlines[i].addr); + name = inline_func_name(sym, inlines[i].id); + + jsonw_start_object(json_wtr); + jsonw_uint_field(json_wtr, "addr", (unsigned long)inlines[i].addr); + /* Name of the inlined function. */ + if (name) { + jsonw_string_field(json_wtr, "func", name); + } else { + jsonw_name(json_wtr, "func"); + jsonw_null(json_wtr); + } + /* The function it is inlined in. */ + if (sym) { + jsonw_string_field(json_wtr, "caller", sym->name); + jsonw_uint_field(json_wtr, "offset", inlines[i].addr - sym->address); + } else { + jsonw_name(json_wtr, "caller"); + jsonw_null(json_wtr); + jsonw_uint_field(json_wtr, "offset", 0); + } + /* Print null if it is vmlinux */ + if (!sym || sym->module[0] == '\0') { + jsonw_name(json_wtr, "module"); + jsonw_null(json_wtr); + } else { + jsonw_string_field(json_wtr, "module", sym->module); + } + jsonw_uint_field(json_wtr, "cookie", inlines[i].cookie); + jsonw_end_object(json_wtr); + } + jsonw_end_array(json_wtr); free(data); + free(inlines); } static __u64 *u64_to_arr(__u64 val) @@ -828,35 +1028,57 @@ void netfilter_dump_plain(const struct bpf_link_info *info) static void show_kprobe_multi_plain(struct bpf_link_info *info) { - struct addr_cookie *data; + __u32 inline_count = info->kprobe_multi.inline_count; + bool is_ibt_enabled, is_inline, is_inline_strict; + __u32 count = info->kprobe_multi.count; + struct addr_cookie_id *inlines = NULL; + struct addr_cookie *data = NULL; + struct kernel_sym *sym; + const char *name; __u32 i, j = 0; - bool is_ibt_enabled; - if (!info->kprobe_multi.count) + is_inline_strict = info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_INLINE_STRICT; + is_inline = info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_INLINE; + + /* Link with inline flag can have only inlined instances attached. */ + if (!count && !is_inline) return; if (info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN) printf("\n\tkretprobe.multi "); else printf("\n\tkprobe.multi "); - printf("func_cnt %u ", info->kprobe_multi.count); + if (is_inline_strict) + printf("inline_strict "); + else if (is_inline) + printf("inline "); + printf("func_cnt %u ", count); + if (is_inline) + printf("inline_cnt %u ", inline_count); + if (info->kprobe_multi.inline_skipped) + printf("inline_skipped %u ", info->kprobe_multi.inline_skipped); if (info->kprobe_multi.missed) printf("missed %llu ", info->kprobe_multi.missed); - data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs), - u64_to_ptr(info->kprobe_multi.cookies), - info->kprobe_multi.count); - if (!data) - return; + if (count) + data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs), + u64_to_ptr(info->kprobe_multi.cookies), + count); + if (inline_count) + inlines = get_addr_cookie_id_array(info); /* Load it once for all. */ - if (!dd.sym_count) + if ((data || inlines) && !dd.sym_count) kernel_syms_load(&dd); - if (!dd.sym_count) - goto error; + /* The array is empty if we don't have its data or symbols. */ + if (!data || !dd.sym_count) + count = 0; + if (!inlines || !dd.sym_count) + inline_count = 0; is_ibt_enabled = is_x86_ibt_enabled(); - printf("\n\t%-16s %-16s %s", "addr", "cookie", "func [module]"); - for (i = 0; i < dd.sym_count; i++) { + if (count) + printf("\n\t%-16s %-16s %s", "addr", "cookie", "func [module]"); + for (i = 0; count && i < dd.sym_count; i++) { if (!symbol_matches_target(dd.sym_mapping[i].address, data[j].addr, is_ibt_enabled)) continue; @@ -867,11 +1089,30 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info) else printf(" "); - if (++j == info->kprobe_multi.count) + if (++j == count) break; } -error: + + if (inline_count) + printf("\n\t%-16s %-16s %s", "addr", "cookie", "func, inlined in [module]"); + for (i = 0; i < inline_count; i++) { + sym = find_kernel_sym_by_range(inlines[i].addr); + + printf("\n\t%016lx %-16llx ", + (unsigned long)inlines[i].addr, inlines[i].cookie); + if (!sym) + continue; + name = inline_func_name(sym, inlines[i].id); + if (name) + printf("%s, ", name); + printf("%s+0x%lx", sym->name, + (unsigned long)inlines[i].addr - sym->address); + if (sym->module[0] != '\0') + printf(" [%s]", sym->module); + printf(" "); + } free(data); + free(inlines); } static void show_uprobe_multi_plain(struct bpf_link_info *info) @@ -1140,7 +1381,8 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) static int do_show_link(int fd) { __u64 *ref_ctr_offsets = NULL, *offsets = NULL, *cookies = NULL; - __u32 *ids = NULL; + __u64 *inline_addrs = NULL, *inline_cookies = NULL; + __u32 *ids = NULL, *inline_ids = NULL; struct bpf_link_info info; __u32 len = sizeof(info); char path_buf[PATH_MAX]; @@ -1172,24 +1414,36 @@ static int do_show_link(int fd) goto again; } if (info.type == BPF_LINK_TYPE_KPROBE_MULTI && - !info.kprobe_multi.addrs) { + !info.kprobe_multi.addrs && !info.kprobe_multi.inline_addrs) { + __u32 inline_count = info.kprobe_multi.inline_count; + count = info.kprobe_multi.count; if (count) { addrs = calloc(count, sizeof(__u64)); - if (!addrs) { - p_err("mem alloc failed"); - close(fd); - return -ENOMEM; - } - info.kprobe_multi.addrs = ptr_to_u64(addrs); cookies = calloc(count, sizeof(__u64)); - if (!cookies) { - p_err("mem alloc failed"); - free(addrs); - close(fd); - return -ENOMEM; - } + } + if (inline_count) { + inline_addrs = calloc(inline_count, sizeof(__u64)); + inline_cookies = calloc(inline_count, sizeof(__u64)); + inline_ids = calloc(inline_count, sizeof(__u32)); + } + if ((count && (!addrs || !cookies)) || + (inline_count && (!inline_addrs || !inline_cookies || !inline_ids))) { + p_err("mem alloc failed"); + free(inline_ids); + free(inline_cookies); + free(inline_addrs); + free(cookies); + free(addrs); + close(fd); + return -ENOMEM; + } + if (count || inline_count) { + info.kprobe_multi.addrs = ptr_to_u64(addrs); info.kprobe_multi.cookies = ptr_to_u64(cookies); + info.kprobe_multi.inline_addrs = ptr_to_u64(inline_addrs); + info.kprobe_multi.inline_cookies = ptr_to_u64(inline_cookies); + info.kprobe_multi.inline_ids = ptr_to_u64(inline_ids); goto again; } } @@ -1282,6 +1536,9 @@ static int do_show_link(int fd) show_link_close_plain(fd, &info); free(ref_ctr_offsets); + free(inline_ids); + free(inline_cookies); + free(inline_addrs); free(cookies); free(offsets); free(addrs); @@ -1353,6 +1610,7 @@ static int do_show(int argc, char **argv) out: if (dd.sym_count) kernel_syms_destroy(&dd); + free_inline_btf(); return errno == ENOENT ? 0 : -1; } -- 2.56.1