[PATCH v2 2/2] sandbox: add cpuinfo command

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Nov 25 07:15:22 PST 2024


We have stack dump support in sandbox via AddressSanitizer, if it's
compiled in. To make it easier to test proper operation, let's a cpuinfo
command like we already do on ARM and give it the same -s option.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
v1 -> v2:
  - Have cpuinfo print uname when called without command line arguments
---
 arch/sandbox/Kconfig                          |  7 +++
 arch/sandbox/board/Makefile                   |  1 +
 arch/sandbox/board/cpuinfo.c                  | 53 +++++++++++++++++++
 .../sandbox/mach-sandbox/include/mach/linux.h |  1 +
 arch/sandbox/os/common.c                      |  5 ++
 5 files changed, 67 insertions(+)
 create mode 100644 arch/sandbox/board/cpuinfo.c

diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index 52915490ca98..caff3a138fea 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -58,6 +58,13 @@ config SANDBOX_REEXEC
 	  The normal reset handler hangs barebox. On Linux, barebox
 	  instead can exec itself to simulate a reset.
 
+config CMD_SANDBOX_CPUINFO
+	bool "cpuinfo command"
+	depends on COMMAND_SUPPORT
+	default y
+	help
+	  Say yes here to get a dummy cpuinfo command.
+
 config SDL
 	bool
 
diff --git a/arch/sandbox/board/Makefile b/arch/sandbox/board/Makefile
index 8e6e1d2d8843..029bfe7ff163 100644
--- a/arch/sandbox/board/Makefile
+++ b/arch/sandbox/board/Makefile
@@ -10,6 +10,7 @@ obj-y += dtb.o
 obj-y += power.o
 obj-y += dev-random.o
 obj-y += watchdog.o
+obj-$(CONFIG_CMD_SANDBOX_CPUINFO) += cpuinfo.o
 obj-$(CONFIG_LED) += led.o
 
 extra-y += barebox.lds
diff --git a/arch/sandbox/board/cpuinfo.c b/arch/sandbox/board/cpuinfo.c
new file mode 100644
index 000000000000..589135102cd3
--- /dev/null
+++ b/arch/sandbox/board/cpuinfo.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <common.h>
+#include <getopt.h>
+#include <command.h>
+#include <complete.h>
+#include <mach/linux.h>
+
+static int do_cpuinfo(int argc, char *argv[])
+{
+	int opt, fd, ret = -ENOENT;
+	char buf[256] = "Unknown host system";
+
+	while ((opt = getopt(argc, argv, "s")) > 0) {
+		switch (opt) {
+		case 's':
+			if (!IS_ENABLED(CONFIG_ARCH_HAS_STACK_DUMP))
+				return -ENOSYS;
+
+			dump_stack();
+			return 0;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	fd = linux_open("/proc/version", false);
+	if (fd >= 0) {
+		ret = linux_read(fd, buf, sizeof(buf));
+		linux_close(fd);
+
+		if (ret > 0)
+			printf("%.*s\n", ret, buf);
+	} else {
+		printf("Unknown host system\n");
+	}
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(cpuinfo)
+BAREBOX_CMD_HELP_TEXT("Shows misc info about CPU")
+BAREBOX_CMD_HELP_OPT ("-s", "print call stack info (if supported)")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(cpuinfo)
+	.cmd            = do_cpuinfo,
+	BAREBOX_CMD_DESC("show info about CPU")
+	BAREBOX_CMD_OPTS("[-s]")
+	BAREBOX_CMD_GROUP(CMD_GRP_INFO)
+	BAREBOX_CMD_COMPLETE(empty_complete)
+	BAREBOX_CMD_HELP(cmd_cpuinfo_help)
+BAREBOX_CMD_END
diff --git a/arch/sandbox/mach-sandbox/include/mach/linux.h b/arch/sandbox/mach-sandbox/include/mach/linux.h
index f4d91f08de8e..e09780f09278 100644
--- a/arch/sandbox/mach-sandbox/include/mach/linux.h
+++ b/arch/sandbox/mach-sandbox/include/mach/linux.h
@@ -15,6 +15,7 @@ int linux_register_device(const char *name, void *start, void *end);
 int tap_alloc(const char *dev);
 uint64_t linux_get_time(void);
 int linux_open(const char *filename, int readwrite);
+int linux_close(int fd);
 char *linux_get_stickypage_path(void);
 int linux_open_hostfile(struct hf_info *hf);
 int linux_read(int fd, void *buf, size_t count);
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 14018b2a63b0..d8bb345614ac 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -185,6 +185,11 @@ int linux_open(const char *filename, int readwrite)
 	return open(filename, (readwrite ? O_RDWR : O_RDONLY) | O_CLOEXEC);
 }
 
+int linux_close(int fd)
+{
+	return close(fd);
+}
+
 int linux_read(int fd, void *buf, size_t count)
 {
 	ssize_t ret;
-- 
2.39.5




More information about the barebox mailing list