From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 8/9] vsprintf: add %ps format specifier for symbols without offset
Date: Tue, 27 May 2025 22:13:58 +0200 [thread overview]
Message-ID: <20250527201359.889550-8-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250527201359.889550-1-a.fatoum@pengutronix.de>
%pS always prints an offset, which can look confusing outside of debug
output. Add a %ps variant like Linux also has.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
commands/kallsyms.c | 2 +-
common/kallsyms.c | 6 +++++-
include/kallsyms.h | 4 +++-
lib/vsprintf.c | 14 ++++++++++----
4 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/commands/kallsyms.c b/commands/kallsyms.c
index 163cd99c1079..121d245500f9 100644
--- a/commands/kallsyms.c
+++ b/commands/kallsyms.c
@@ -22,7 +22,7 @@ static int do_kallsyms(int argc, char *argv[])
if (kstrtoul(argv[1], 16, &addr) == 0) {
char sym[KSYM_SYMBOL_LEN];
- sprint_symbol(sym, addr);
+ sprint_symbol(sym, addr, true);
printf("%s\n", sym);
return 0;
diff --git a/common/kallsyms.c b/common/kallsyms.c
index 3c5904f8a833..61eae2794537 100644
--- a/common/kallsyms.c
+++ b/common/kallsyms.c
@@ -189,7 +189,7 @@ const char *kallsyms_lookup(unsigned long addr,
}
/* Look up a kernel symbol and return it in a text buffer. */
-int sprint_symbol(char *buffer, unsigned long address)
+int sprint_symbol(char *buffer, unsigned long address, bool with_offset)
{
char *modname;
const char *name;
@@ -205,12 +205,16 @@ int sprint_symbol(char *buffer, unsigned long address)
len = strlen(buffer);
buffer += len;
+ if (!with_offset)
+ goto out;
+
if (modname)
len += sprintf(buffer, "+%#lx/%#lx [%s]",
offset, size, modname);
else
len += sprintf(buffer, "+%#lx/%#lx", offset, size);
+out:
return len;
}
EXPORT_SYMBOL_GPL(sprint_symbol);
diff --git a/include/kallsyms.h b/include/kallsyms.h
index f61efc9e0c42..c93287302bde 100644
--- a/include/kallsyms.h
+++ b/include/kallsyms.h
@@ -2,6 +2,8 @@
#ifndef __KALLSYMS_H
#define __KALLSYMS_H
+#include <linux/types.h>
+
#define KSYM_NAME_LEN 128
#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s]") + (KSYM_NAME_LEN - 1) + \
2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + 1)
@@ -12,6 +14,6 @@ const char *kallsyms_lookup(unsigned long addr,
char **modname, char *namebuf);
/* Look up a kernel symbol and return it in a text buffer. */
-int sprint_symbol(char *buffer, unsigned long address);
+int sprint_symbol(char *buffer, unsigned long address, bool with_offset);
#endif /* __KALLSYMS_H */
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 32429dd76c23..ba87f6c210d2 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -254,12 +254,12 @@ static char *raw_pointer(char *buf, const char *end, const void *ptr, int field_
#if IN_PROPER
static char *symbol_string(char *buf, const char *end, const void *ptr, int field_width,
- int precision, int flags)
+ int precision, int flags, bool with_offset)
{
unsigned long value = (unsigned long) ptr;
#ifdef CONFIG_KALLSYMS
char sym[KSYM_SYMBOL_LEN];
- sprint_symbol(sym, value);
+ sprint_symbol(sym, value, with_offset);
return string(buf, end, sym, field_width, precision, flags);
#else
field_width = 2*sizeof(void *);
@@ -484,7 +484,8 @@ char *device_node_string(char *buf, const char *end, const struct device_node *n
*
* Right now we handle following Linux-compatible format specifiers:
*
- * - 'S' For symbolic direct pointers
+ * - 'S' For symbolic direct pointers (or function descriptors) with offset
+ * - 's' For symbolic direct pointers (or function descriptors) without offset
* - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
* Options for %pU are:
@@ -523,9 +524,14 @@ char *device_node_string(char *buf, const char *end, const struct device_node *n
static char *pointer(const char *fmt, char *buf, const char *end, const void *ptr,
int field_width, int precision, int flags)
{
+ bool with_offset = false;
+
switch (*fmt) {
case 'S':
- return symbol_string(buf, end, ptr, field_width, precision, flags);
+ with_offset = true;
+ fallthrough;
+ case 's':
+ return symbol_string(buf, end, ptr, field_width, precision, flags, with_offset);
case 'U':
if (IS_ENABLED(CONFIG_PRINTF_UUID))
return uuid_string(buf, end, ptr, field_width, precision, flags, fmt);
--
2.39.5
next prev parent reply other threads:[~2025-05-27 20:14 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-27 20:13 [PATCH 1/9] lib: stackprot: fix type for %pS Ahmad Fatoum
2025-05-27 20:13 ` [PATCH 2/9] fastboot: use correct format specifier for size_t Ahmad Fatoum
2025-05-27 20:13 ` [PATCH 3/9] Makefile: don't warn over zero-size format string Ahmad Fatoum
2025-05-27 20:13 ` [PATCH 4/9] scripts: define __printf attribute macro Ahmad Fatoum
2025-05-27 20:13 ` [PATCH 5/9] treewide: specify __printf attribute directly on static definition Ahmad Fatoum
2025-05-27 20:13 ` [PATCH 6/9] treewide: replace attribute with shorter __printf macro Ahmad Fatoum
2025-05-27 20:13 ` [PATCH 7/9] treewide: add missing __printf attributes Ahmad Fatoum
2025-05-27 20:13 ` Ahmad Fatoum [this message]
2025-05-27 20:13 ` [PATCH 9/9] lib: random: print get_crypto_bytes caller when no HWRNG is registered Ahmad Fatoum
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=20250527201359.889550-8-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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