From patchwork Sat Apr 13 21:39:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Deslandes X-Patchwork-Id: 13628797 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wfhigh4-smtp.messagingengine.com (wfhigh4-smtp.messagingengine.com [64.147.123.155]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF80323AD; Sat, 13 Apr 2024 21:39:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.155 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713044354; cv=none; b=e/Q4duJwNmLkdzodjO2YTeWxdqMJhpICWzbkKt/NXt6PDrhcE67wlN9gAn13LKVBNSKPvNn6ovDTQ8kIXNfhs0t0gzbQsJl8CXrEGGaV6tQHrpYdSVoxJ08fscTqa45t5Py5aPj//4ofK3980ITTGPUj4nZZLer7lIg8d1uYPkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713044354; c=relaxed/simple; bh=HmOzFHNly2hJix0cNa5XXicHRCBr5Q2VGfkGUEaU+8E=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=UC2uycRFa1hy2plxZ2BRSeIFF7WVrPyDviuoLANADDkQwaNpB/owqAf2HxJiRTgU+jG7yddLg8RiNT89K37lnjnXJPh8qEH/7KeKTzUbH9XnYeXhk/VauHgF8Vl07lkJEwzWMK3XwFXK3lG0JeyYr0syLtkYIMMqh9XjEYbJ2yk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=naccy.de; spf=pass smtp.mailfrom=naccy.de; dkim=pass (2048-bit key) header.d=naccy.de header.i=@naccy.de header.b=YP7Zf++E; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=JEDbRJR4; arc=none smtp.client-ip=64.147.123.155 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=naccy.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=naccy.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=naccy.de header.i=@naccy.de header.b="YP7Zf++E"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="JEDbRJR4" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.west.internal (Postfix) with ESMTP id 6FEF3180010B; Sat, 13 Apr 2024 17:39:10 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 13 Apr 2024 17:39:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=naccy.de; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to; s=fm1; t=1713044349; x=1713130749; bh=qimSkpcSGfdMhl5p572yB CeqpY7AKr5gRT2/6lHtvBQ=; b=YP7Zf++EPje/7vy+gilI9NgkNwFd5+RDxRDX9 b1XLYE72FAOcuAxfa/gryVn4+aLTPTc1407JD+pxVkb/AhZMOS/G/J4wdbRhiam0 9jpjXQ7vDwSQRzRUETqSMGajif308NX9mzBLvEF7BnFh4gnNPOHnTZ3nqRlqJcua Hkui5qVOJgnyMWHcWEp1Pu+fvOF43gBd/btJrhEbAImPThxg3j/FfiXlr0AJL6Vg Nv++zPK+k62SlU/Q4JlRDNlo8QmQhgVR6zuirvVgt93y9Ijq7HxuxnXZGuEN2Fx4 ggZ7gPkNeBhX562sbhQZr/S2a4kUWjnQ6+rT2745+sG6H0qPQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1713044349; x=1713130749; bh=qimSkpcSGfdMhl5p572yBCeqpY7A Kr5gRT2/6lHtvBQ=; b=JEDbRJR4ZuVVHyqFczmI3ptR2M58oIRKFtlwb10UU/8I GzR6x+GmPFViUK7IbWrJECbBxNBOP8XOq4/NsCB1twRLlqJ0aVvvcCjd/mQlPp/L gyMHwmSSpwrtpG2I5fUIc1BmFoRt1Y4QkytrymElG2KTsRJXfm4IJfSY1Rj0n3V5 Sq5Jd35jr47otepHYhnAaBtE1g/AFjR1OP/aP4oe5dnNG7HCWF+qvxtNecw0eQYv 2y7b/6UfPjgrRgejK3WgXigtSD1RyqkN0jPxdjTu77oNc3MPQ4l/KVFJpmsA0c94 jgx/wxvBa2EXx66XmIC8Ave6idsBGLgJzJO20InVcQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeijedgtdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgggfestdekredtredttdenucfhrhhomhepsfhuvghnthhi nhcuffgvshhlrghnuggvshcuoehquggvsehnrggttgihrdguvgeqnecuggftrfgrthhtvg hrnhephfffieeivdeiheegleejtdeuieejlefgffejfeehhefhheffieefgeduhfehiefh necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepqhguvg esnhgrtggthidruggv X-ME-Proxy: Feedback-ID: i14194934:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 13 Apr 2024 17:39:07 -0400 (EDT) From: Quentin Deslandes To: bpf@vger.kernel.org, Quentin Deslandes Cc: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [RFC PATCH bpf-next] libbpf: print character arrays as strings if possible Date: Sat, 13 Apr 2024 23:39:04 +0200 Message-ID: <20240413213904.146261-1-qde@naccy.de> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 --- tools/lib/bpf/btf.h | 3 ++- tools/lib/bpf/btf_dump.c | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) -- 2.44.0 diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 8e6880d91c84..cf190900d483 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -284,9 +284,10 @@ struct btf_dump_type_data_opts { bool compact; /* no newlines/indentation */ bool skip_names; /* skip member/type names */ bool emit_zeroes; /* show 0-valued fields */ + bool print_strings; /* print char arrays as string */ size_t :0; }; -#define btf_dump_type_data_opts__last_field emit_zeroes +#define btf_dump_type_data_opts__last_field print_strings LIBBPF_API int btf_dump__dump_type_data(struct btf_dump *d, __u32 id, diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 4d9f30bf7f01..d396a10f37b2 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -67,6 +67,7 @@ struct btf_dump_data { bool compact; bool skip_names; bool emit_zeroes; + bool print_strings; __u8 indent_lvl; /* base indent level */ char indent_str[BTF_DATA_INDENT_STR_LEN]; /* below are used during iteration */ @@ -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; + } + + 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); + return 0; + } + } } /* note that we increment depth before calling btf_dump_print() below; @@ -2533,6 +2555,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id, d->typed_dump->compact = OPTS_GET(opts, compact, false); d->typed_dump->skip_names = OPTS_GET(opts, skip_names, false); d->typed_dump->emit_zeroes = OPTS_GET(opts, emit_zeroes, false); + d->typed_dump->print_strings = OPTS_GET(opts, print_strings, false); ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);