Question Details

No question body available.

Tags

c linux debugging gdb breakpoints

Answers (2)

Accepted Answer Available
Accepted Answer
October 30, 2025 Score: 6 Rep: 5,090 Quality: Expert Completeness: 60%

It is not clear from your question what you have tried, and what isn't working. Using your hello and world examples, here's my GDB session doing what I think you want to do:

$ gdb ./hello
GDB Version: 17.1

Reading symbols from ./hello... (gdb) tbreak 5 Temporary breakpoint 1 at 0x401135: file hello.c, line 5. (gdb) run Starting program: /tmp/exec-test/hello

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffa3e8) at hello.c:5 5 system("/tmp/exec-test/world"); (gdb) set follow-fork-mode child (gdb) break main if (strcmp (basename (argv[0]), "world") == 0) Breakpoint 2 at 0x401135: file hello.c, line 5. (gdb) continue Continuing. [Attaching after process 3719461 vfork to child process 3719507] [New inferior 2 (process 3719507)] [Detaching vfork parent process 3719461 after child exec] [Inferior 1 (process 3719461) detached] process 3719507 is executing new program: /usr/bin/bash process 3719507 is executing new program: /tmp/exec-test/world

Thread 2.1 "world" hit Breakpoint 2, main (argc=1, argv=0x7fffffffa3f8) at world.c:5 5 printf("Hello World\n"); (gdb) info inferiors Num Description Connection Executable 1 /tmp/exec-test/world * 2 process 3719507 1 (native) /tmp/exec-test/world (gdb)

I use set follow-fork-mode child so GDB will follow the child process in a fork rather than the parent. The execve calls will automatically be followed within the same inferior. I think the inferior 2 comes about due to the vfork() call in the shell, but I'm not 100% sure on that.

I use break main if (strcmp (basename (argv[0]), "world") == 0) to create a breakpoint that will only be hit in the world executable. You can do just break main but you'll also hit this in the shell, but just continue to keep going and eventually you'll stop in the correct main function. The conditional breakpoint just means GDB does the check for me. Of course, this relies on basename and strcmp being linked into your executable.

What I don't know is how you'd do this in VS Code, but hopefully they provide a GDB console where you could input the above commands directly.

October 30, 2025 Score: 4 Rep: 10,014 Quality: Medium Completeness: 80%

I assume that your real use case is more complicated and you need to debug world in the environment and/or with IPC set up by hello.

I further assume that creating a simulation environment might be too much effort.

I had a similar requirement in a system where several programs were started from a main program, connected by pipes, FIFOs and databases. It was not easily possible to run the child programs separately.

I used the following approach to attach the debugger to the program world:

  • Modify the program world to wait before the interesting part and build this version.
int main(int argc, char argv)
{
    { / begin additional code /
        static volatile int debugwait = 1;
        while(debugwait);
    } / end of additional code /

printf("Hello World"); return 0; }
  • Run the program hello and wait for the worldprocess to be started.
  • Attach the debugger to the running world process.
  • Set breakpoints as necessary.
  • Modify the variable debugwait = 0 to allow the program to terminate the loop.
  • Continue or single-step the program.