[2] tests: detect if debugger is attached

Submitted by Marek Chalupa on Nov. 19, 2014, 11:20 a.m.

Details

Message ID 1416396031-18594-1-git-send-email-mchqwerty@gmail.com
State Superseded
Headers show

Not browsing as part of any series.

Commit Message

Marek Chalupa Nov. 19, 2014, 11:20 a.m.
Copy function from libinput/test/litest.c is_debugger_detached()
and use it in our test-runner. If debugger is attached, turn off
leak checks and timeouts automatically.

Revision of libinput: 028513a0a723e97941c39c4aeb17433198723913

Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
---
 tests/test-runner.c | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/tests/test-runner.c b/tests/test-runner.c
index 1373225..63b25ed 100644
--- a/tests/test-runner.c
+++ b/tests/test-runner.c
@@ -33,6 +33,7 @@ 
 #include <dlfcn.h>
 #include <errno.h>
 #include <limits.h>
+#include <sys/ptrace.h>
 
 #include "test-runner.h"
 
@@ -224,6 +225,39 @@  rmdir_xdg_runtime_dir(void)
 		perror("Cleaning XDG_RUNTIME_DIR");
 }
 
+
+/* this function is taken from libinput/test/litest.c
+ * (rev 028513a0a723e97941c39)
+ */
+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[])
 {
 	const struct test *t;
@@ -237,8 +271,13 @@  int main(int argc, char *argv[])
 	sys_malloc = dlsym(RTLD_NEXT, "malloc");
 	sys_free = dlsym(RTLD_NEXT, "free");
 
-	leak_check_enabled = !getenv("WAYLAND_TESTS_NO_LEAK_CHECK");
-	timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
+	if (is_debugger_attached()) {
+		leak_check_enabled = 0;
+		timeouts_enabled = 0;
+	} else {
+		leak_check_enabled = !getenv("WAYLAND_TESTS_NO_LEAK_CHECK");
+		timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
+	}
 
 	if (argc == 2 && strcmp(argv[1], "--help") == 0)
 		usage(argv[0], EXIT_SUCCESS);

Comments

On Wed, 19 Nov 2014 12:20:31 +0100
Marek Chalupa <mchqwerty@gmail.com> wrote:

> Copy function from libinput/test/litest.c is_debugger_detached()
> and use it in our test-runner. If debugger is attached, turn off
> leak checks and timeouts automatically.
> 
> Revision of libinput: 028513a0a723e97941c39c4aeb17433198723913
> 
> Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
> ---
>  tests/test-runner.c | 43 +++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 41 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/test-runner.c b/tests/test-runner.c
> index 1373225..63b25ed 100644
> --- a/tests/test-runner.c
> +++ b/tests/test-runner.c
> @@ -33,6 +33,7 @@
>  #include <dlfcn.h>
>  #include <errno.h>
>  #include <limits.h>
> +#include <sys/ptrace.h>
>  
>  #include "test-runner.h"
>  
> @@ -224,6 +225,39 @@ rmdir_xdg_runtime_dir(void)
>  		perror("Cleaning XDG_RUNTIME_DIR");
>  }
>  
> +
> +/* this function is taken from libinput/test/litest.c
> + * (rev 028513a0a723e97941c39)
> + */
> +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[])
>  {
>  	const struct test *t;
> @@ -237,8 +271,13 @@ int main(int argc, char *argv[])
>  	sys_malloc = dlsym(RTLD_NEXT, "malloc");
>  	sys_free = dlsym(RTLD_NEXT, "free");
>  
> -	leak_check_enabled = !getenv("WAYLAND_TESTS_NO_LEAK_CHECK");
> -	timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
> +	if (is_debugger_attached()) {
> +		leak_check_enabled = 0;
> +		timeouts_enabled = 0;
> +	} else {
> +		leak_check_enabled = !getenv("WAYLAND_TESTS_NO_LEAK_CHECK");
> +		timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
> +	}
>  
>  	if (argc == 2 && strcmp(argv[1], "--help") == 0)
>  		usage(argv[0], EXIT_SUCCESS);

Hi,

I'm a little wary of this kind of magic. You can't force-enable these
settings under a debugger now, but I'm not sure anyone would want to.

Would need a trivial rebase.

Libinput seems use the debugger detection for preventing test forking,
and we have our own way for that by running a single sub-test.

Do we care enough to have this patch?


Thanks,
pq
Hi,

Here it is used to suppress the timeout, which just kills the client during
debugging if you forget to turn it off.
Since it is just a way how to workaround forgetting of humans (read: me xD)
I think that I don't care about this patch much.
If no-one else want this, I'm OK with rejecting this patch.

Thanks,
Marek


On 28 November 2014 at 12:54, Pekka Paalanen <ppaalanen@gmail.com> wrote:

> On Wed, 19 Nov 2014 12:20:31 +0100
> Marek Chalupa <mchqwerty@gmail.com> wrote:
>
> > Copy function from libinput/test/litest.c is_debugger_detached()
> > and use it in our test-runner. If debugger is attached, turn off
> > leak checks and timeouts automatically.
> >
> > Revision of libinput: 028513a0a723e97941c39c4aeb17433198723913
> >
> > Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
> > ---
> >  tests/test-runner.c | 43 +++++++++++++++++++++++++++++++++++++++++--
> >  1 file changed, 41 insertions(+), 2 deletions(-)
> >
> > diff --git a/tests/test-runner.c b/tests/test-runner.c
> > index 1373225..63b25ed 100644
> > --- a/tests/test-runner.c
> > +++ b/tests/test-runner.c
> > @@ -33,6 +33,7 @@
> >  #include <dlfcn.h>
> >  #include <errno.h>
> >  #include <limits.h>
> > +#include <sys/ptrace.h>
> >
> >  #include "test-runner.h"
> >
> > @@ -224,6 +225,39 @@ rmdir_xdg_runtime_dir(void)
> >               perror("Cleaning XDG_RUNTIME_DIR");
> >  }
> >
> > +
> > +/* this function is taken from libinput/test/litest.c
> > + * (rev 028513a0a723e97941c39)
> > + */
> > +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[])
> >  {
> >       const struct test *t;
> > @@ -237,8 +271,13 @@ int main(int argc, char *argv[])
> >       sys_malloc = dlsym(RTLD_NEXT, "malloc");
> >       sys_free = dlsym(RTLD_NEXT, "free");
> >
> > -     leak_check_enabled = !getenv("WAYLAND_TESTS_NO_LEAK_CHECK");
> > -     timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
> > +     if (is_debugger_attached()) {
> > +             leak_check_enabled = 0;
> > +             timeouts_enabled = 0;
> > +     } else {
> > +             leak_check_enabled =
> !getenv("WAYLAND_TESTS_NO_LEAK_CHECK");
> > +             timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
> > +     }
> >
> >       if (argc == 2 && strcmp(argv[1], "--help") == 0)
> >               usage(argv[0], EXIT_SUCCESS);
>
> Hi,
>
> I'm a little wary of this kind of magic. You can't force-enable these
> settings under a debugger now, but I'm not sure anyone would want to.
>
> Would need a trivial rebase.
>
> Libinput seems use the debugger detection for preventing test forking,
> and we have our own way for that by running a single sub-test.
>
> Do we care enough to have this patch?
>
>
> Thanks,
> pq
>
On Fri, 28 Nov 2014 13:09:59 +0100
Marek Chalupa <mchqwerty@gmail.com> wrote:

> Hi,
> 
> Here it is used to suppress the timeout, which just kills the client during
> debugging if you forget to turn it off.
> Since it is just a way how to workaround forgetting of humans (read: me xD)
> I think that I don't care about this patch much.
> If no-one else want this, I'm OK with rejecting this patch.

If you care to rebase it, I'll push it. :-)

Thanks,
pq