Skip to content

Commit

Permalink
pahole: Add global_var BTF feature
Browse files Browse the repository at this point in the history
So far, pahole has only encoded type information for percpu variables.

However, there are several reasons why type information for all global
variables would be useful in the kernel:

1. Runtime kernel debuggers like drgn could use the BTF to introspect
kernel data structures without needing to install heavyweight DWARF.

2. BPF programs using the "__ksym" annotation could declare the
variables using the correct type, rather than "void".

It makes sense to introduce a feature for this in pahole so that these
capabilities can be explored in the kernel.

The feature is non-default: when using "--btf-features=default", it is
disabled. It must be explicitly requested, e.g. with
"--btf-features=+global_var".

Reviewed-by: Alan Maguire <[email protected]>
Signed-off-by: Stephen Brennan <[email protected]>
Tested-by: Alan Maguire <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
brenns10 authored and acmel committed Oct 7, 2024
1 parent ff34e73 commit d580a45
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions btf_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,8 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
encoder->encode_vars = 0;
if (!conf_load->skip_encoding_btf_vars)
encoder->encode_vars |= BTF_VAR_PERCPU;
if (conf_load->encode_btf_global_vars)
encoder->encode_vars |= BTF_VAR_GLOBAL;

GElf_Ehdr ehdr;

Expand Down Expand Up @@ -2400,6 +2402,9 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
encoder->secinfo[shndx].name = secname;
encoder->secinfo[shndx].type = shdr.sh_type;

if (encoder->encode_vars & BTF_VAR_GLOBAL)
encoder->secinfo[shndx].include = true;

if (strcmp(secname, PERCPU_SECTION) == 0) {
found_percpu = true;
if (encoder->encode_vars & BTF_VAR_PERCPU)
Expand Down
1 change: 1 addition & 0 deletions btf_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct list_head;
enum btf_var_option {
BTF_VAR_NONE = 0,
BTF_VAR_PERCPU = 1,
BTF_VAR_GLOBAL = 2,
};

struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filename, struct btf *base_btf, bool verbose, struct conf_load *conf_load);
Expand Down
1 change: 1 addition & 0 deletions dwarves.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct conf_load {
bool btf_gen_optimized;
bool skip_encoding_btf_inconsistent_proto;
bool skip_encoding_btf_vars;
bool encode_btf_global_vars;
bool btf_gen_floats;
bool btf_encode_force;
bool reproducible_build;
Expand Down
7 changes: 5 additions & 2 deletions man-pages/pahole.1
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ the debugging information.

.TP
.B \-\-skip_encoding_btf_vars
Do not encode VARs in BTF.
By default, VARs are encoded only for percpu variables. When specified, this
option prevents encoding any VARs. Note that this option can be overridden
by the feature "global_var".

.TP
.B \-\-skip_encoding_btf_decl_tag
Expand Down Expand Up @@ -304,7 +306,7 @@ Encode BTF using the specified feature list, or specify 'default' for all standa
encode_force Ignore invalid symbols when encoding BTF; for example
if a symbol has an invalid name, it will be ignored
and BTF encoding will continue.
var Encode variables using BTF_KIND_VAR in BTF.
var Encode percpu variables using BTF_KIND_VAR in BTF.
float Encode floating-point types in BTF.
decl_tag Encode declaration tags using BTF_KIND_DECL_TAG.
type_tag Encode type tags using BTF_KIND_TYPE_TAG.
Expand All @@ -329,6 +331,7 @@ Supported non-standard features (not enabled for 'default')
the associated base BTF to support later relocation
of split BTF with a possibly changed base, storing
it in a .BTF.base ELF section.
global_var Encode all global variables using BTF_KIND_VAR in BTF.
.fi

So for example, specifying \-\-btf_encode=var,enum64 will result in a BTF encoding that (as well as encoding basic BTF information) will contain variables and enum64 values.
Expand Down
3 changes: 2 additions & 1 deletion pahole.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ struct btf_feature {
BTF_DEFAULT_FEATURE(decl_tag_kfuncs, btf_decl_tag_kfuncs, false),
BTF_NON_DEFAULT_FEATURE(reproducible_build, reproducible_build, false),
BTF_NON_DEFAULT_FEATURE(distilled_base, btf_gen_distilled_base, false),
BTF_NON_DEFAULT_FEATURE(global_var, encode_btf_global_vars, false),
};

#define BTF_MAX_FEATURE_STR 1024
Expand Down Expand Up @@ -1736,7 +1737,7 @@ static const struct argp_option pahole__options[] = {
{
.name = "skip_encoding_btf_vars",
.key = ARGP_skip_encoding_btf_vars,
.doc = "Do not encode VARs in BTF."
.doc = "Do not encode any VARs in BTF [if this is not specified, only percpu variables are encoded. To encode global variables too, use --encode_btf_global_vars]."
},
{
.name = "btf_encode_force",
Expand Down

0 comments on commit d580a45

Please sign in to comment.