Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH bluez v4] tools/test-runner: Add optional mode parameter to -d
@ 2026-12-05  5:29 Zijun Hu
  2026-12-05 14:53 ` [bluez,v4] " bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Zijun Hu @ 2026-12-05  5:29 UTC (permalink / raw)
  To: Linux Bluetooth; +Cc: Marcel Holtmann, Luiz Augusto von Dentz, Zijun Hu

Issue:
When running:

  tools/test-runner -u -d -k /path/to/bzImage -- client/bluetoothctl

bluetoothd debug level logs flood the console and bury the
bluetoothctl output the user is interested in. For example, this
causes difficulty finding a specific device in the scan results.

Why:
test-runner always starts bluetoothd with "--nodetach -d": -d
enables bluetoothd's debug level logs, and --nodetach sends them to
stderr on the console, where they mix with the bluetoothctl output.

Fix by:
Make test-runner's -d take an optional integer mode parameter,
-d[mode], which controls how to start bluetoothd and defaults to 1:
- 1: "bluetoothd --nodetach -d [-f src/main.conf]", debug mode
- 2: "bluetoothd --nodetach [-f src/main.conf]", normal mode
     where errors and warnings still go to stderr on the console

Plain -d keeps the current behavior. Use -d2 for a cleaner console:

  tools/test-runner -u -d2 -k /path/to/bzImage \
      -- client/bluetoothctl

Advantages:
- Is applicable to both QEMU and UML
- Is easy to extend with new modes later if required

Assisted-by: Claude:claude-opus-5-5
---
Will post an alternative solution, which redirects bluetoothd's logs
to a host file, with title:
tools/test-runner: Add optional log path to -d for QEMU

Previous version:
https://lore.kernel.org/all/20261125-tr_arm64-v2-5-4f02989f17ba@oss.qualcomm.com

Changes since previous version:
- Improve commit title and message
- Improve example usage in doc/test-runner.rst
---
 doc/test-runner.rst | 17 +++++++++++++-
 tools/test-runner.c | 68 +++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 66 insertions(+), 19 deletions(-)

diff --git a/doc/test-runner.rst b/doc/test-runner.rst
index 7ffcff72a2a5..5a7b7c780c0b 100644
--- a/doc/test-runner.rst
+++ b/doc/test-runner.rst
@@ -12,17 +12,20 @@ it launches a virtual machine using qemu(1) with the host filesystem mounted
 read-only inside the guest.
 
 OPTIONS
 =======
 
 :-a:--auto: Find tests and run them
 :-b/--dbus: Start D-Bus system daemon
 :-s/--dbus-session: Start D-Bus session daemon
-:-d/--daemon: Start bluetoothd
+:-d/--daemon[=mode]: Start bluetoothd, mode (default 1):
+
+    | 1: --nodetach -d [-f src/main.conf]
+    | 2: --nodetach [-f src/main.conf]
 :-m/--monitor: Start btmon
 :-l/--emulator[=num]: Start btvirt
 :-A/-audio[=path]: Start audio server
 :-u/--unix[=path]: Provide serial device
 :-U/--usb=<qemu_args>: Provide USB device
 :-P/--pcie=<qemu_args>: Provide PCIe device
 :-q/--qemu=<path>: QEMU binary
 :-H/--qemu-host-cpu: Use host CPU (requires KVM support)
@@ -360,16 +363,28 @@ Running bluetoothctl with emulated controller and audio support
 Running shell with host controller using btproxy
 ------------------------------------------------
 
 .. code-block::
 
 	$ tools/btproxy -u [1]
 	$ tools/test-runner -u -d -k /pathto/bzImage -- /bin/bash [2]
 
+Running bluetoothctl with a clearer console
+-------------------------------------------
+
+To get a clearer bluetoothctl console, use ``-d2`` to start bluetoothd
+without debug level logs:
+
+.. code-block::
+
+	$ tools/btproxy -u [1]
+	$ tools/test-runner -u -d2 -k /path/to/bzImage \
+		-- client/bluetoothctl [2]
+
 Running shell with host controller USB-passthrough
 --------------------------------------------------
 
 In addition the above kernel config option the following is required:
 
 .. code-block::
 
 	CONFIG_USB=y
diff --git a/tools/test-runner.c b/tools/test-runner.c
index 1af9fa700d81..a71088ed549b 100644
--- a/tools/test-runner.c
+++ b/tools/test-runner.c
@@ -50,25 +50,26 @@
 #endif
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
 #define _cleanup_(f) __attribute__((cleanup(f)))
 
 #define CMDLINE_MAX (2048 * 10)
 #define EXTRA_OPT_MAX 64
+#define DAEMON_MODE_MAX 3
 
 static const char *own_binary;
 static char **test_argv;
 static int test_argc;
 
 static bool run_auto = false;
 static bool start_dbus = false;
 static bool start_dbus_session;
-static bool start_daemon = false;
+static unsigned int start_daemon;
 static bool start_monitor = false;
 static bool qemu_host_cpu = false;
 /* Simulated time with User Mode Linux: -1 if supported by the kernel */
 static int time_travel = -1;
 static int num_devs = 0;
 static int num_emulator = 0;
 static const char *device_path = "/tmp/bt-server-bredr";
 static const char *qemu_binary = NULL;
@@ -1578,17 +1579,17 @@ static const char *daemon_table[] = {
 	"/usr/sbin/bluetoothd",
 	"/usr/libexec/bluetooth/bluetoothd",
 	NULL
 };
 
 static pid_t start_bluetooth_daemon(const char *home)
 {
 	const char *daemon = NULL;
-	char *argv[6], *envp[2];
+	char *argv[6] = { NULL }, *envp[2];
 	pid_t pid;
 	struct stat st;
 	int i;
 
 	if (chdir(home + 5) < 0) {
 		perror("Failed to change home directory for daemon");
 		return -1;
 	}
@@ -1603,25 +1604,40 @@ static pid_t start_bluetooth_daemon(const char *home)
 
 	if (!daemon) {
 		fprintf(stderr, "Failed to locate Bluetooth daemon binary\n");
 		return -1;
 	}
 
 	printf("Using Bluetooth daemon %s\n", daemon);
 
-	argv[0] = (char *) daemon;
-	argv[1] = "--nodetach";
-	argv[2] = "-d";
-	argv[3] = NULL;
+	i = 0;
+	argv[i++] = (char *)daemon;
 
-	if (!stat("src/main.conf", &st)) {
-		argv[3] = "-f";
-		argv[4] = "src/main.conf";
-		argv[5] = NULL;
+	switch (start_daemon) {
+	case 1:
+		argv[i++] = "--nodetach";
+		argv[i++] = "-d";
+
+		if (!stat("src/main.conf", &st)) {
+			argv[i++] = "-f";
+			argv[i++] = "src/main.conf";
+		}
+		break;
+	case 2:
+		argv[i++] = "--nodetach";
+
+		if (!stat("src/main.conf", &st)) {
+			argv[i++] = "-f";
+			argv[i++] = "src/main.conf";
+		}
+		break;
+	default:
+		fprintf(stderr, "Invalid daemon mode %u\n", start_daemon);
+		return -1;
 	}
 
 	envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket";
 	envp[1] = NULL;
 
 	printf("Starting Bluetooth daemon\n");
 
 	pid = fork();
@@ -2167,20 +2183,21 @@ static void run_tests(void)
 	}
 
 	ptr = strstr(cmdline, "TESTDBUSSESSION=1");
 	if (ptr) {
 		printf("D-Bus session daemon requested\n");
 		start_dbus_session = true;
 	}
 
-	ptr = strstr(cmdline, "TESTDAEMON=1");
+	ptr = strstr(cmdline, "TESTDAEMON=");
 	if (ptr) {
-		printf("bluetoothd requested\n");
-		start_daemon = true;
+		start_daemon = strtoul(ptr + 11, NULL, 0);
+		if (start_daemon)
+			printf("bluetoothd mode %u requested\n", start_daemon);
 	}
 
 	ptr = strstr(cmdline, "TESTMONITOR=1");
 	if (ptr) {
 		printf("Monitor requested\n");
 		start_monitor = true;
 	}
 
@@ -2220,17 +2237,19 @@ static void usage(void)
 {
 	printf("test-runner - Automated test execution utility\n"
 		"Usage:\n");
 	printf("\ttest-runner [options] [--] <command> [args]\n");
 	printf("Options:\n"
 		"\t-a, --all,--auto       Find tests and run them\n"
 		"\t-b, --dbus             Start D-Bus system daemon\n"
 		"\t-s, --dbus-session     Start D-Bus session daemon\n"
-		"\t-d, --daemon           Start bluetoothd\n"
+		"\t-d, --daemon[=mode]    Start bluetoothd, mode (default 1):\n"
+		"\t                       1: --nodetach -d [-f src/main.conf]\n"
+		"\t                       2: --nodetach [-f src/main.conf]\n"
 		"\t-m, --monitor          Start btmon\n"
 		"\t-l, --emulator[=num]   Start btvirt\n"
 		"\t-A, --audio[=path]     Start audio server\n"
 		"\t-u, --unix[=path]      Provide serial device\n"
 		"\t-U, --usb <qemu_args>  Provide USB device\n"
 		"\t-P, --pcie <qemu_args> Provide PCIe device\n"
 		"\t-q, --qemu <path>      QEMU binary\n"
 		"\t-H, --qemu-host-cpu    Use host CPU (requires KVM support)\n"
@@ -2249,17 +2268,17 @@ static void usage(void)
 }
 
 static const struct option main_options[] = {
 	{ "all",     no_argument,       NULL, 'a' },
 	{ "auto",    no_argument,       NULL, 'a' },
 	{ "dbus",    no_argument,       NULL, 'b' },
 	{ "dbus-session", no_argument,  NULL, 's' },
 	{ "unix",    optional_argument, NULL, 'u' },
-	{ "daemon",  no_argument,       NULL, 'd' },
+	{ "daemon",  optional_argument, NULL, 'd' },
 	{ "emulator", no_argument,      NULL, 'l' },
 	{ "monitor", no_argument,       NULL, 'm' },
 	{ "qemu",    required_argument, NULL, 'q' },
 	{ "qemu-host-cpu", no_argument, NULL, 'H' },
 	{ "kernel",  required_argument, NULL, 'k' },
 	{ "audio",   optional_argument, NULL, 'A' },
 	{ "usb",     required_argument, NULL, 'U' },
 	{ "pcie",    required_argument, NULL, 'P' },
@@ -2290,17 +2309,17 @@ int main(int argc, char *argv[])
 			reboot(RB_AUTOBOOT);
 		return EXIT_SUCCESS;
 	}
 
 	for (;;) {
 		int opt;
 
 		opt = getopt_long(argc, argv,
-				"au::bdsl::mq:Hk:A::U:P:o:F::TRvh",
+				"au::bd::sl::mq:Hk:A::U:P:o:F::TRvh",
 				main_options, NULL);
 		if (opt < 0)
 			break;
 
 		switch (opt) {
 		case 'a':
 			run_auto = true;
 			break;
@@ -2311,18 +2330,31 @@ int main(int argc, char *argv[])
 			break;
 		case 'b':
 			start_dbus = true;
 			break;
 		case 's':
 			start_dbus_session = true;
 			break;
 		case 'd':
-			start_dbus = true;
-			start_daemon = true;
+			start_daemon = 1;
+			if (optarg) {
+				char *endptr;
+
+				start_daemon = strtoul(optarg, &endptr, 0);
+				if (start_daemon >= DAEMON_MODE_MAX ||
+				    *endptr) {
+					fprintf(stderr,
+						"Invalid daemon mode %s\n",
+						optarg);
+					return EXIT_FAILURE;
+				}
+			}
+			if (start_daemon)
+				start_dbus = true;
 			break;
 		case 'l':
 			num_emulator = optarg ? atoi(optarg) : 1;
 			break;
 		case 'm':
 			start_monitor = true;
 			break;
 		case 'q':

---
base-commit: 96e805a6b6bb052d4d2e3d73dc2b703f298b0349
change-id: 20261204-tr_mode-8a5b8f15086f

Best regards,
--  
Zijun Hu <zijun.hu@oss.qualcomm.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 14:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  5:29 [PATCH bluez v4] tools/test-runner: Add optional mode parameter to -d Zijun Hu
2026-12-05 14:53 ` [bluez,v4] " bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox