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

Peter Hutterer peter.hutterer at who-t.net
Sun Dec 29 01:59:02 PST 2013


On 28/12/2013 01:50 , David Herrmann wrote:
> Hi Peter
>
> On Mon, Dec 23, 2013 at 11:44 PM, Peter Hutterer
> <peter.hutterer at who-t.net> wrote:
>> 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
>
> You should also change this line then.
>
>>    *
>> 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);
>
> I guess PTRACE_ATTACH stops the tracee or what are you waiting here for?
>
>> +                       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())
>
> Can you add: && !getenv("CK_FORK")
> Allows to set CK_FORK explicitly on the command-line.

setenv("foo", "bar", 0) only overwrites foo when it isn't set, otherwise 
it'll leave it untouched (see the man page) so we're good here.

Cheers,
   Peter


>
> Thanks
> David
>
>> +               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
>>
>> _______________________________________________
>> Input-tools mailing list
>> Input-tools at lists.freedesktop.org
>> http://lists.freedesktop.org/mailman/listinfo/input-tools



More information about the Input-tools mailing list