0

I am definitely new to C and gdb, but not programming. I want to debug library referenced by a failing unit-test. There is a child process forked per each unit test (I assume to keep memory separated) and I not willing to change it to run in single process. Rather I'd like to tell my gdb to automatically track each child process created by the parent process my gdb runs.

What's the right way to go?

Zazaeil
  • 3,900
  • 2
  • 14
  • 31
  • https://sourceware.org/gdb/onlinedocs/gdb/Forks.html – EOF Sep 19 '20 at 21:02
  • Does this answer your question? [How do I debug the child process after fork() in gdb?](https://stackoverflow.com/questions/6199270/how-do-i-debug-the-child-process-after-fork-in-gdb) – kaylum Sep 19 '20 at 22:41

1 Answers1

1

I'd like to tell my gdb to automatically track each child process created by the parent process my gdb runs.

You can write a Python program to do that (assuming recent GDB built with embedded Python).

Often a much easier approach is to modify the failing test case like so (I am using gUnit example here, but the same technique works in general):

TEST(Foo, Bar) {
  // Start added code.
  volatile int go = 0;  // Modified from GDB.
  while (go == 0) {
    fprintf(stderr, "Run 'gdb -p %d'\n", getpid());
    sleep(1);
  }
  // End.

  EXPECT_EQ(123, Bar());  // This is the test which fails.
}

Now just run all your tests, attach GDB when the test case runs, set breakpoints as desired, set go to 1 and continue.

P.S. Most test frameworks already have a mechanism to attach debugger on failure. If you are dealing with one that doesn't, you may want to fix that.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362