Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug external program run in main function in C?

I want to add a breakpoint in the line system("/path/to/world") in VS Code for debugging the /path/to/world program with gdb.

Here is the source code of /path/to/hello.c:

int main(int argc, char **argv)
{
    system("/path/to/world");
    return 0;
}

Here is the source code of /path/to/world.c:

int main(int argc, char **argv)
{
    printf("Hello World");
    return 0;
}

If using system() is not possible for debugging external program with a breakpoint in the main function, then what function or what method should be used instead?

How to debug external program in the main function in c with a breakpoint?

Update:

After adding a breakpoint in the line of "system("/path/to/world");" in VS Code and run the hello program in debug mode, after VS Code hits the line "system("/path/to/world");", and then I click the "Step Into" button, the following error will be shown:

Could not load source './stdlib/../sysdeps/posix/system.c': 'SourceRequest' not supported..
like image 990
stackbiz Avatar asked Dec 02 '25 21:12

stackbiz


1 Answers

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    <null>                                 /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.

like image 186
Andrew Avatar answered Dec 05 '25 13:12

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!