From: Eduard Zingerman <eddyz87@gmail.com>
To: Quentin Deslandes <qde@naccy.de>, bpf@vger.kernel.org
Cc: Andrii Nakryiko <andrii@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
linux-kernel@vger.kernel.org, kernel-team@meta.com
Subject: Re: [RFC PATCH bpf-next] libbpf: print character arrays as strings if possible
Date: Wed, 24 Apr 2024 16:33:27 -0700 [thread overview]
Message-ID: <b49224dbc89bd304a5c20eef4430dfea40f4b6a5.camel@gmail.com> (raw)
In-Reply-To: <20240413213904.146261-1-qde@naccy.de>
On Sat, 2024-04-13 at 23:39 +0200, Quentin Deslandes wrote:
> Introduce the new print_strings flag in btf_dump_type_data_opts. If
> enabled, libbpf will print character arrays as strings if they meet the
> following conditions:
> - Contains a nul-termination character ('\0')
> - Contains only printable characters before the nul-termination character
>
> If print_strings is set to false (default value), the existing
> behavior remains unchanged.
>
> With print_strings=false:
> .str_array = (__u8[14])[
> 'H',
> 'e',
> 'l',
> 'l',
> 'o',
> ],
>
> With print_strings=true:
> .str_array = (__u8[14])"Hello",
>
> Signed-off-by: Quentin Deslandes <qde@naccy.de>
> ---
Hi Quentin,
Thank you for this patch, sorry for the delay reviewing it.
Could you please also add a few tests in
tools/testing/selftests/bpf/prog_tests/btf_dump.c ?
[...]
> @@ -2021,6 +2022,21 @@ static int btf_dump_var_data(struct btf_dump *d,
> return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0);
> }
>
> +static bool btf_dump_isprint_str(const char *data, unsigned int len)
> +{
> + unsigned int i;
> +
> + for (i = 0; i < len; ++i) {
> + if (data[i] == '\0')
> + return true;
> +
> + if (!isprint(data[i]))
> + return false;
Would it make sense to use isprint_l() and specify something like C locale?
> + }
> +
> + return false;
> +}
> +
> static int btf_dump_array_data(struct btf_dump *d,
> const struct btf_type *t,
> __u32 id,
> @@ -2047,8 +2063,14 @@ static int btf_dump_array_data(struct btf_dump *d,
> * char arrays, so if size is 1 and element is
> * printable as a char, we'll do that.
> */
> - if (elem_size == 1)
> + if (elem_size == 1) {
> d->typed_dump->is_array_char = true;
> + if (d->typed_dump->print_strings &&
> + btf_dump_isprint_str(data, array->nelems)) {
> + btf_dump_type_values(d, "\"%s\"", data);
Note: this would have to deal with escape sequences,
otherwise strings containing '\' would be printed incorrectly.
> + return 0;
> + }
> + }
> }
>
> /* note that we increment depth before calling btf_dump_print() below;
[...]
prev parent reply other threads:[~2024-04-24 23:33 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-13 21:39 Quentin Deslandes
2024-04-24 23:33 ` Eduard Zingerman [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b49224dbc89bd304a5c20eef4430dfea40f4b6a5.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-team@meta.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=qde@naccy.de \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®