Re: [PATCH v2 1/2] libbpf: add support for printing BTF character arrays as strings

From: Andrii Nakryiko
Date: Tue Jun 03 2025 - 14:39:51 EST


On Tue, Jun 3, 2025 at 8:39 AM Blake Jones <blakejones@xxxxxxxxxx> wrote:
>
> On Tue, Jun 3, 2025 at 6:18 AM Jiri Olsa <olsajiri@xxxxxxxxx> wrote:
> > > +static int btf_dump_string_data(struct btf_dump *d,
> > > + const struct btf_type *t,
> > > + __u32 id,
> > > + const void *data)
> > > +{
> > > + const struct btf_array *array = btf_array(t);
> > > + __u32 i;
> > > +
> > > + btf_dump_data_pfx(d);
> > > + btf_dump_printf(d, "\"");
> > > +
> > > + for (i = 0; i < array->nelems; i++, data++) {
> > > + char c;
> > > +
> > > + if (data >= d->typed_dump->data_end)
> > > + return -E2BIG;
> >
> > curious, is this just string array without null terminating byte?
> > should we just print " and return 0 instead of E2BIG error ?
>
> Good question. That E2BIG error would happen, for example, if we tried
> to print the array "{ 'a', 'b', 'c' }" when the type was "char[4]".

Exactly, data is truncated, we have to return E2BIG. But I think that
is checked earlier with btf_dump_type_data_check_overflow(), so we
probably don't need to do this here?

Please add tests with truncated data so we know for sure?

> I'd say your proposed behavior would be consistent with the semantic of
> ".emit_strings should display strings in an intuitively useful way",

It still should follow the overall contract, so I think E2BIG is
justified for truncated data.

But there is also a bit of a quirk. If a string is not
zero-terminated, we actually don't distinguish it in any way. Would it
make sense to detect this and still print it as an array of individual
characters? It's clearly not a valid C string at that point, so
emit_strings doesn't have to apply. WDYT? The implementation would be
simple -- find zero in an array, if found - emit everything up to that
point as string, if not - emit character array?

> and I'd be in favor of doing that (replacing "return -E2BIG" with "break").
> If others agree (specifically Andrii, who had comments about the semantics
> yesterday), I'll make that change.
>



> Blake