[PATCH 016/112] commands: kallsyms: add command-line interface

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jan 3 10:11:36 PST 2024


When working on kallsyms code, it's useful to be able to resolve symbols
and decode addresses on the fly. Add a simple command to facilitate
this. There should be no need to enable this except for development.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/Kconfig    |  9 ++++++++
 commands/Makefile   |  1 +
 commands/kallsyms.c | 50 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
 create mode 100644 commands/kallsyms.c

diff --git a/commands/Kconfig b/commands/Kconfig
index a6806f198ec4..d27c20478fcf 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -2178,6 +2178,15 @@ config CMD_FIRMWARELOAD
 	  Provides the "firmwareload" command which deals with devices which need
 	  firmware to work. It is also used to upload firmware to FPGA devices.
 
+config CMD_KALLSYMS
+	depends on KALLSYMS
+	bool
+	prompt "kallsyms"
+	help
+	  query kallsyms table
+
+	  Usage: kallsyns [SYMBOL | ADDRESS]
+
 config CMD_KEYSTORE
 	depends on CRYPTO_KEYSTORE
 	bool
diff --git a/commands/Makefile b/commands/Makefile
index 4924755500e3..c8cb21a7db88 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -106,6 +106,7 @@ obj-$(CONFIG_CMD_READLINK)	+= readlink.o
 obj-$(CONFIG_CMD_LET)		+= let.o
 obj-$(CONFIG_CMD_LN)		+= ln.o
 obj-$(CONFIG_CMD_CLK)		+= clk.o
+obj-$(CONFIG_CMD_KALLSYMS)	+= kallsyms.o
 obj-$(CONFIG_CMD_KEYSTORE)	+= keystore.o
 obj-$(CONFIG_CMD_TFTP)		+= tftp.o
 obj-$(CONFIG_CMD_FILETYPE)	+= filetype.o
diff --git a/commands/kallsyms.c b/commands/kallsyms.c
new file mode 100644
index 000000000000..163cd99c1079
--- /dev/null
+++ b/commands/kallsyms.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * kallsyms.c - translate address to symbols
+ */
+
+#include <common.h>
+#include <kallsyms.h>
+#include <command.h>
+#include <malloc.h>
+#include <complete.h>
+#include <getopt.h>
+#include <string.h>
+
+static int do_kallsyms(int argc, char *argv[])
+{
+	unsigned long addr;
+
+	if (argc != 2)
+		return COMMAND_ERROR_USAGE;
+
+	if (kstrtoul(argv[1], 16, &addr) == 0) {
+		char sym[KSYM_SYMBOL_LEN];
+
+		sprint_symbol(sym, addr);
+
+		printf("%s\n", sym);
+		return 0;
+	}
+
+	if ((addr = kallsyms_lookup_name(argv[1]))) {
+		printf("0x%08lx\n", addr);
+		return 0;
+	}
+
+	return COMMAND_ERROR;
+}
+
+BAREBOX_CMD_HELP_START(kallsyms)
+BAREBOX_CMD_HELP_TEXT("Lookup address or symbol using kallsyms table")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(kallsyms)
+	.cmd	= do_kallsyms,
+	BAREBOX_CMD_DESC("query kallsyms table")
+	BAREBOX_CMD_OPTS("[SYMBOL | ADDRESS]")
+	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+	BAREBOX_CMD_COMPLETE(empty_complete)
+	BAREBOX_CMD_HELP(cmd_kallsyms_help)
+BAREBOX_CMD_END
-- 
2.39.2




More information about the barebox mailing list