Re: [PATCH v2 05/19] gendwarfksyms: Expand base_type

From: Petr Pavlu
Date: Wed Aug 28 2024 - 08:46:38 EST


On 8/15/24 19:39, Sami Tolvanen wrote:
> Start making gendwarfksyms more useful by adding support for
> expanding DW_TAG_base_type types and basic DWARF attributes.
>
> Example:
>
> $ echo loops_per_jiffy | \
> scripts/gendwarfksyms/gendwarfksyms --debug vmlinux.o
> ...
> gendwarfksyms: process_exported_symbols: loops_per_jiffy
> variable base_type unsigned long byte_size(8) encoding(7);
> ...
>
> Signed-off-by: Sami Tolvanen <samitolvanen@xxxxxxxxxx>
> ---
> scripts/gendwarfksyms/dwarf.c | 121 +++++++++++++++++++++++++++++++++-
> 1 file changed, 120 insertions(+), 1 deletion(-)
>
> diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c
> index 956b30224316..a37c9049d18e 100644
> --- a/scripts/gendwarfksyms/dwarf.c
> +++ b/scripts/gendwarfksyms/dwarf.c
> @@ -5,6 +5,17 @@
>
> #include "gendwarfksyms.h"
>
> +#define DEFINE_GET_ATTR(attr, type) \
> + static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \
> + type *value) \
> + { \
> + Dwarf_Attribute da; \
> + return dwarf_attr(die, id, &da) && \
> + !dwarf_form##attr(&da, value); \
> + }
> +
> +DEFINE_GET_ATTR(udata, Dwarf_Word)
> +
> static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value)
> {
> Dwarf_Attribute da;
> @@ -60,6 +71,74 @@ static int process(struct state *state, const char *s)
> return 0;
> }
>
> +#define MAX_FMT_BUFFER_SIZE 128
> +
> +static int process_fmt(struct state *state, const char *fmt, ...)

Nit: The state parameter is unused by a number of these process_*()
functions, including the leaf process(). I suggest removing it so it
doesn't need to be passed around unnecessarily.

> +{
> + char buf[MAX_FMT_BUFFER_SIZE];
> + va_list args;
> + int res;
> +
> + va_start(args, fmt);
> +
> + res = checkp(vsnprintf(buf, sizeof(buf), fmt, args));
> + if (res >= MAX_FMT_BUFFER_SIZE - 1) {

This check looks off by one, though on the safe side:
res >= sizeof(buf)

> + error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE");
> + res = -1;
> + } else {
> + res = check(process(state, buf));
> + }
> +
> + va_end(args);
> + return res;
> +}
> +
> +/* Process a fully qualified name from DWARF scopes */
> +static int process_fqn(struct state *state, Dwarf_Die *die)
> +{
> + Dwarf_Die *scopes = NULL;
> + const char *name;
> + int res;
> + int i;
> +
> + res = checkp(dwarf_getscopes_die(die, &scopes));
> + if (!res) {
> + name = get_name(die);
> + name = name ?: "<unnamed>";
> + return check(process(state, name));
> + }
> +
> + for (i = res - 1; i >= 0; i--) {
> + if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
> + continue;
> +
> + name = get_name(&scopes[i]);
> + name = name ?: "<unnamed>";
> + check(process(state, name));
> + if (i > 0)
> + check(process(state, "::"));

Failed check(process()) calls here return immediately and so would leak
scopes. However, I see this is fixed in the following patch
"gendwarfksyms: Add a cache for processed DIEs" so it's ok.

> + }
> +
> + free(scopes);
> + return 0;
> +}
> +
> +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \
> + static int process_##attribute##_attr(struct state *state, \
> + Dwarf_Die *die) \
> + { \
> + Dwarf_Word value; \
> + if (get_udata_attr(die, DW_AT_##attribute, &value)) \
> + check(process_fmt(state, \
> + " " #attribute "(%" PRIu64 ")", \
> + value)); \
> + return 0; \
> + }
> +
> +DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment)
> +DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size)
> +DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding)
> +
> bool match_all(Dwarf_Die *die)
> {
> return true;
> @@ -81,6 +160,44 @@ int process_die_container(struct state *state, Dwarf_Die *die,
> return 0;
> }
>
> +static int process_type(struct state *state, Dwarf_Die *die);
> +
> +static int process_type_attr(struct state *state, Dwarf_Die *die)
> +{
> + Dwarf_Die type;
> +
> + if (get_ref_die_attr(die, DW_AT_type, &type))
> + return check(process_type(state, &type));
> +
> + /* Compilers can omit DW_AT_type -- print out 'void' to clarify */
> + return check(process(state, "base_type void"));
> +}
> +
> +static int process_base_type(struct state *state, Dwarf_Die *die)
> +{
> + check(process(state, "base_type "));
> + check(process_fqn(state, die));
> + check(process_byte_size_attr(state, die));
> + check(process_encoding_attr(state, die));
> + return check(process_alignment_attr(state, die));
> +}
> +
> +static int process_type(struct state *state, Dwarf_Die *die)
> +{
> + int tag = dwarf_tag(die);
> +
> + switch (tag) {
> + case DW_TAG_base_type:
> + check(process_base_type(state, die));
> + break;
> + default:
> + debug("unimplemented type: %x", tag);
> + break;
> + }
> +
> + return 0;
> +}
> +
> /*
> * Exported symbol processing
> */
> @@ -91,7 +208,9 @@ static int process_subprogram(struct state *state, Dwarf_Die *die)
>
> static int process_variable(struct state *state, Dwarf_Die *die)
> {
> - return check(process(state, "variable;\n"));
> + check(process(state, "variable "));
> + check(process_type_attr(state, die));
> + return check(process(state, ";\n"));
> }
>
> static int process_symbol_ptr(struct state *state, Dwarf_Die *die)

--
Thanks,
Petr