[PATCH libevdev] test: detect if we're running inside gdb and disable forking

Peter Hutterer peter.hutterer at who-t.net
Mon Dec 23 14:44:21 PST 2013


The Check test framework forks by default which is annoying when running gdb.
Try to detect whether we're inside gdb by ptracing ourselves. If that works,
we're not inside a debugger. If it doesn't, then assume we're inside a
debugger and set CK_FORK to "no".

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 libevdev/libevdev.h |  5 +++--
 test/test-main.c    | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h
index c11c845..c5d12a4 100644
--- a/libevdev/libevdev.h
+++ b/libevdev/libevdev.h
@@ -177,8 +177,9 @@ extern "C" {
  *     git grep "suite_create"
  *     git grep "tcase_create"
  *
- * By default, check forks, making debugging harder. Run gdb as below to avoid
- * forking.
+ * By default, Check forks, making debugging harder. The test suite tries to detect
+ * if it is running inside gdb and disable forking. If that doesn't work for
+ * some reason, run gdb as below to avoid forking.
  *
  *     sudo CK_FORK=no CK_RUN_TEST="test case name" gdb ./test/test-libevdev
  *
diff --git a/test/test-main.c b/test/test-main.c
index 73d79c9..91128c3 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -22,6 +22,10 @@
 
 #include <config.h>
 #include <check.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
 
 extern Suite *event_name_suite(void);
 extern Suite *event_code_suite(void);
@@ -31,9 +35,42 @@ extern Suite *libevdev_has_event_test(void);
 extern Suite *libevdev_events(void);
 extern Suite *uinput_suite(void);
 
+static int
+is_debugger_attached(void)
+{
+	int status;
+	int rc;
+	int pid = fork();
+
+	if (pid == -1)
+		return 0;
+
+	if (pid == 0) {
+		int ppid = getppid();
+		if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) {
+			waitpid(ppid, NULL, 0);
+			ptrace(PTRACE_CONT, NULL, NULL);
+			ptrace(PTRACE_DETACH, ppid, NULL, NULL);
+			rc = 0;
+		} else
+			rc = 1;
+		_exit(rc);
+	} else {
+		waitpid(pid, &status, 0);
+		rc = WEXITSTATUS(status);
+	}
+
+	return rc;
+}
+
+
 int main(int argc, char **argv)
 {
 	int failed;
+
+	if (is_debugger_attached())
+		setenv("CK_FORK", "no", 0);
+
 	Suite *s = libevdev_has_event_test();
 	SRunner *sr = srunner_create(s);
 	srunner_add_suite(sr, libevdev_events());
-- 
1.8.4.2



More information about the Input-tools mailing list