3c5f2a224a
Add encoding of DWARF's DW_TAG_subprogram_type into BTF's BTF_KIND_FUNC (plus corresponding BTF_KIND_FUNC_PROTO). Only exported functions are converted for now. This allows to capture all the exported kernel functions, same subset that's exposed through /proc/kallsyms. Committer testing: Before: $ readelf -SW vmlinux | grep BTF [78] .BTF PROGBITS 0000000000000000 26a27da9 1e5543 00 0 0 1 $ After: $ pahole -J vmlinux $ readelf -SW vmlinux | grep BTF [78] .BTF PROGBITS 0000000000000000 26a27da9 2d5f47 00 0 0 1 $ >>> 0x2d5f47 - 0x1e5543 985604 The kernel has a lot of functions! :-) $ pahole -VJ vmlinux > /tmp/pahole-btf-encoding-verbose-output.txt $ grep -w FUNC /tmp/pahole-btf-encoding-verbose-output.txt | wc -l 22871 [acme@quaco pahole]$ grep -w FUNC /tmp/pahole-btf-encoding-verbose-output.txt | tail [4511543] FUNC copy_from_user_nmi type_id=4511542 [4512934] FUNC memcpy_page_flushcache type_id=4512933 [4512936] FUNC __memcpy_flushcache type_id=4512935 [4512938] FUNC __copy_user_flushcache type_id=4512937 [4512940] FUNC arch_wb_cache_pmem type_id=4512939 [4512942] FUNC mcsafe_handle_tail type_id=4512941 [4512944] FUNC copy_user_handle_tail type_id=4512943 [4512946] FUNC clear_user type_id=4512945 [4512948] FUNC __clear_user type_id=4512947 [4512950] FUNC memcpy type_id=4512949 $ grep -w FUNC_PROTO /tmp/pahole-btf-encoding-verbose-output.txt | tail [4512902] FUNC_PROTO (anon) return=4511725 args=(4512097 (anon), 4511544 (anon)) [4512933] FUNC_PROTO (anon) return=0 args=(4511598 to, 4511725 page, 4511610 offset, 4511610 len) [4512935] FUNC_PROTO (anon) return=0 args=(4511638 _dst, 4511759 _src, 4511610 size) [4512937] FUNC_PROTO (anon) return=4511585 args=(4511638 dst, 4511759 src, 4511552 size) [4512939] FUNC_PROTO (anon) return=0 args=(4511638 addr, 4511610 size) [4512941] FUNC_PROTO (anon) return=4511544 args=(4511598 to, 4511598 from, 4511552 len) [4512943] FUNC_PROTO (anon) return=4511544 args=(4511598 to, 4511598 from, 4511552 len) [4512945] FUNC_PROTO (anon) return=4511544 args=(4511638 to, 4511544 n) [4512947] FUNC_PROTO (anon) return=4511544 args=(4511638 addr, 4511544 size) [4512949] FUNC_PROTO (anon) return=4511638 args=(4511638 p, 4511759 q, 4511591 size) $ grep -w FUNC_PROTO /tmp/pahole-btf-encoding-verbose-output.txt |grep 4511542 [4511542] FUNC_PROTO (anon) return=4510159 args=(4510254 to, 4510374 from, 4510159 n) $ With a little change to pdwtags to see DW_TAG_subroutine_type, which is what BTF's KIND_FUNC_PROTO maps to, we see some of those last prototypes: [acme@quaco pahole]$ pdwtags -F btf vmlinux | grep '()(' | tail void ()(struct insn * insn); /* size: 45404744 */ int ()(struct insn * insn); /* size: 4 */ void ()(struct insn * insn, const void * kaddr, int buf_len, int x86_64); /* size: 45405032 */ long unsigned int ()(const char * purpose); /* size: 8 */ void ()(char * to, struct page * page, size_t offset, size_t len); /* size: 45405864 */ void ()(void * _dst, const void * _src, size_t size); /* size: 45406200 */ long int ()(void * dst, const void * src, unsigned int size); /* size: 8 */ long unsigned int ()(char * to, char * from, unsigned int len); /* size: 8 */ long unsigned int ()(void * to, long unsigned int n); /* size: 8 */ long unsigned int ()(void * addr, long unsigned int size); /* size: 8 */ [acme@quaco pahole]$ I.e.: [4512941] FUNC_PROTO (anon) return=4511544 args=(4511598 to, 4511598 from, 4511552 len) gets decoded by pdwtags as: long unsigned int ()(char * to, char * from, unsigned int len); /* size: 8 */ $ grep '\[\(4511544\|4511598\|4511550\|4511552\)\]' /tmp/pahole-btf-encoding-verbose-output.txt [4511544] INT long unsigned int size=8 bit_offset=0 nr_bits=64 encoding=(none) [4511550] INT char size=1 bit_offset=0 nr_bits=8 encoding=(none) [4511552] INT unsigned int size=4 bit_offset=0 nr_bits=32 encoding=(none) [4511598] PTR (anon) type_id=4511550 $ Signed-off-by: Andrii Nakryiko <andriin@fb.com> Tested-by: Alexei Starovoitov <ast@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Cc: Alexei Starovoitov <ast@fb.com> Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> Cc: Yonghong Song <yhs@fb.com> Cc: dwarves@vger.kernel.org Cc: kernel-team@fb.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
249 lines
6.3 KiB
C
249 lines
6.3 KiB
C
/*
|
|
SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
Copyright (C) 2019 Facebook
|
|
|
|
Derived from ctf_encoder.c, which is:
|
|
|
|
Copyright (C) Arnaldo Carvalho de Melo <acme@redhat.com>
|
|
Copyright (C) Red Hat Inc
|
|
*/
|
|
|
|
#include "dwarves.h"
|
|
#include "libbtf.h"
|
|
#include "lib/bpf/include/uapi/linux/btf.h"
|
|
#include "hash.h"
|
|
#include "elf_symtab.h"
|
|
#include "btf_encoder.h"
|
|
|
|
#include <inttypes.h>
|
|
|
|
static int tag__check_id_drift(const struct tag *tag,
|
|
uint32_t core_id, uint32_t btf_type_id,
|
|
uint32_t type_id_off)
|
|
{
|
|
if (btf_type_id != (core_id + type_id_off)) {
|
|
fprintf(stderr,
|
|
"%s: %s id drift, core_id: %u, btf_type_id: %u, type_id_off: %u\n",
|
|
__func__, dwarf_tag_name(tag->tag),
|
|
core_id, btf_type_id, type_id_off);
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int32_t structure_type__encode(struct btf_elf *btfe, struct tag *tag, uint32_t type_id_off)
|
|
{
|
|
struct type *type = tag__type(tag);
|
|
struct class_member *pos;
|
|
bool kind_flag = false;
|
|
int32_t type_id;
|
|
uint8_t kind;
|
|
|
|
kind = (tag->tag == DW_TAG_union_type) ?
|
|
BTF_KIND_UNION : BTF_KIND_STRUCT;
|
|
|
|
/* Although no_bitfield_type_recode has been set true
|
|
* in pahole.c if BTF encoding is requested, we still check
|
|
* the value here. So if no_bitfield_type_recode is set
|
|
* to false for whatever reason, we do not accidentally
|
|
* set kind_flag incorrectly.
|
|
*/
|
|
if (no_bitfield_type_recode) {
|
|
/* kind_flag only set where there is a bitfield
|
|
* in the struct.
|
|
*/
|
|
type__for_each_data_member(type, pos) {
|
|
if (pos->bitfield_size) {
|
|
kind_flag = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
type_id = btf_elf__add_struct(btfe, kind, type->namespace.name, kind_flag, type->size, type->nr_members);
|
|
if (type_id < 0)
|
|
return type_id;
|
|
|
|
type__for_each_data_member(type, pos) {
|
|
/*
|
|
* dwarf_loader uses DWARF's recommended bit offset addressing
|
|
* scheme, which conforms to BTF requirement, so no conversion
|
|
* is required.
|
|
*/
|
|
if (btf_elf__add_member(btfe, pos->name, type_id_off + pos->tag.type, kind_flag, pos->bitfield_size, pos->bit_offset))
|
|
return -1;
|
|
}
|
|
|
|
return type_id;
|
|
}
|
|
|
|
static uint32_t array_type__nelems(struct tag *tag)
|
|
{
|
|
int i;
|
|
uint32_t nelem = 1;
|
|
struct array_type *array = tag__array_type(tag);
|
|
|
|
for (i = array->dimensions - 1; i >= 0; --i)
|
|
nelem *= array->nr_entries[i];
|
|
|
|
return nelem;
|
|
}
|
|
|
|
static int32_t enumeration_type__encode(struct btf_elf *btfe, struct tag *tag)
|
|
{
|
|
struct type *etype = tag__type(tag);
|
|
struct enumerator *pos;
|
|
int32_t type_id;
|
|
|
|
type_id = btf_elf__add_enum(btfe, etype->namespace.name, etype->size, etype->nr_members);
|
|
if (type_id < 0)
|
|
return type_id;
|
|
|
|
type__for_each_enumerator(etype, pos)
|
|
if (btf_elf__add_enum_val(btfe, pos->name, pos->value))
|
|
return -1;
|
|
|
|
return type_id;
|
|
}
|
|
|
|
static int tag__encode_btf(struct tag *tag, uint32_t core_id, struct btf_elf *btfe,
|
|
uint32_t array_index_id, uint32_t type_id_off)
|
|
{
|
|
/* single out type 0 as it represents special type "void" */
|
|
uint32_t ref_type_id = tag->type == 0 ? 0 : type_id_off + tag->type;
|
|
|
|
switch (tag->tag) {
|
|
case DW_TAG_base_type:
|
|
return btf_elf__add_base_type(btfe, tag__base_type(tag));
|
|
case DW_TAG_const_type:
|
|
return btf_elf__add_ref_type(btfe, BTF_KIND_CONST, ref_type_id, 0, false);
|
|
case DW_TAG_pointer_type:
|
|
return btf_elf__add_ref_type(btfe, BTF_KIND_PTR, ref_type_id, 0, false);
|
|
case DW_TAG_restrict_type:
|
|
return btf_elf__add_ref_type(btfe, BTF_KIND_RESTRICT, ref_type_id, 0, false);
|
|
case DW_TAG_volatile_type:
|
|
return btf_elf__add_ref_type(btfe, BTF_KIND_VOLATILE, ref_type_id, 0, false);
|
|
case DW_TAG_typedef:
|
|
return btf_elf__add_ref_type(btfe, BTF_KIND_TYPEDEF, ref_type_id, tag__namespace(tag)->name, false);
|
|
case DW_TAG_structure_type:
|
|
case DW_TAG_union_type:
|
|
case DW_TAG_class_type:
|
|
if (tag__type(tag)->declaration)
|
|
return btf_elf__add_ref_type(btfe, BTF_KIND_FWD, 0, tag__namespace(tag)->name, tag->tag == DW_TAG_union_type);
|
|
else
|
|
return structure_type__encode(btfe, tag, type_id_off);
|
|
case DW_TAG_array_type:
|
|
/* TODO: Encode one dimension at a time. */
|
|
return btf_elf__add_array(btfe, ref_type_id, array_index_id, array_type__nelems(tag));
|
|
case DW_TAG_enumeration_type:
|
|
return enumeration_type__encode(btfe, tag);
|
|
case DW_TAG_subroutine_type:
|
|
return btf_elf__add_func_proto(btfe, tag__ftype(tag), type_id_off);
|
|
default:
|
|
fprintf(stderr, "Unsupported DW_TAG_%s(0x%x)\n",
|
|
dwarf_tag_name(tag->tag), tag->tag);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* FIXME: Its in the DWARF loader, we have to find a better handoff
|
|
* mechanizm...
|
|
*/
|
|
extern struct strings *strings;
|
|
|
|
static struct btf_elf *btfe;
|
|
static uint32_t array_index_id;
|
|
|
|
int btf_encoder__encode()
|
|
{
|
|
int err;
|
|
|
|
err = btf_elf__encode(btfe, 0);
|
|
btf_elf__delete(btfe);
|
|
btfe = NULL;
|
|
|
|
return err;
|
|
}
|
|
|
|
int cu__encode_btf(struct cu *cu, int verbose)
|
|
{
|
|
bool add_index_type = false;
|
|
uint32_t type_id_off;
|
|
uint32_t core_id;
|
|
struct function *fn;
|
|
struct tag *pos;
|
|
int err = 0;
|
|
|
|
if (btfe && strcmp(btfe->filename, cu->filename)) {
|
|
err = btf_encoder__encode();
|
|
if (err)
|
|
goto out;
|
|
|
|
/* Finished one file, add one empty line */
|
|
if (verbose)
|
|
printf("\n");
|
|
}
|
|
|
|
if (!btfe) {
|
|
btfe = btf_elf__new(cu->filename, cu->elf);
|
|
if (!btfe)
|
|
return -1;
|
|
btf_elf__set_strings(btfe, &strings->gb);
|
|
|
|
/* cu__find_base_type_by_name() takes "type_id_t *id" */
|
|
type_id_t id;
|
|
if (!cu__find_base_type_by_name(cu, "int", &id)) {
|
|
add_index_type = true;
|
|
id = cu->types_table.nr_entries;
|
|
}
|
|
array_index_id = id;
|
|
|
|
if (verbose)
|
|
printf("File %s:\n", btfe->filename);
|
|
}
|
|
|
|
btf_elf__verbose = verbose;
|
|
type_id_off = btfe->type_index;
|
|
|
|
cu__for_each_type(cu, core_id, pos) {
|
|
int32_t btf_type_id = tag__encode_btf(pos, core_id, btfe, array_index_id, type_id_off);
|
|
|
|
if (btf_type_id < 0 ||
|
|
tag__check_id_drift(pos, core_id, btf_type_id, type_id_off)) {
|
|
err = -1;
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
if (add_index_type) {
|
|
struct base_type bt = {};
|
|
|
|
bt.name = 0;
|
|
bt.bit_size = 32;
|
|
btf_elf__add_base_type(btfe, &bt);
|
|
}
|
|
|
|
cu__for_each_function(cu, core_id, fn) {
|
|
int btf_fnproto_id, btf_fn_id;
|
|
|
|
if (fn->declaration || !fn->external)
|
|
continue;
|
|
|
|
btf_fnproto_id = btf_elf__add_func_proto(btfe, &fn->proto, type_id_off);
|
|
btf_fn_id = btf_elf__add_ref_type(btfe, BTF_KIND_FUNC, btf_fnproto_id, fn->name, false);
|
|
if (btf_fnproto_id < 0 || btf_fn_id < 0) {
|
|
err = -1;
|
|
printf("error: failed to encode function '%s'\n", function__name(fn, cu));
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
out:
|
|
if (err)
|
|
btf_elf__delete(btfe);
|
|
return err;
|
|
}
|