远程调试gdb多进程

发布于 2024-12-07 16:39:58 字数 3176 浏览 0 评论 0原文

我无法调试远程调试会话的子进程。我发现了类似的问题 如何调试入口点GDB 中的 fork-exec 进程?

我遵循相同的过程,尽管是针对远程目标。远程目标是否支持 follow-fork-mode child

以下是我的示例代码..

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <sys/types.h>
      4 #include <unistd.h>
      5
      6 int spawn (void)
      7 {
      8         pid_t child_pid;
      9         /* Duplicate this process. */
     10         child_pid = fork ();
     11         if (child_pid != 0)
     12                 /* This is the parent process. */
     13                 return child_pid;
     14         else {
     15                 /* Now execute PROGRAM, searching for it in the path. */
     16                 while(1)
     17                 {
     18                         static int i = 0;
     19                         if(i==0)         /* break here for child */
     20                         {
     21                                 printf("I am child\n");
     22                         }
     23                         else if(i==3)
     24                         {
     25                                 return 1;
     26                         }
     27                         else
     28                         {
     29                                 i = 0/0;
     30                         }
     31                         i++;
     32                 }
     33         }
     34         return 0;
     35 }
     36 int main ()
     37 {
     38         /* Spawn a child process running the .ls. command. Ignore the
     39            returned child process ID. */
     40         printf("Hello World..!!\n");
     41         spawn ();
     42         printf ("Bbye World..!!\n");
     43         return 0;
     44 }

使用 gdb 运行它,我可以在 child 中设置断点.. 一切都很好。!!

sh-3.2# gdb fork
(gdb) set follow-fork-mode child
(gdb) set detach-on-fork off
(gdb) b 19
Breakpoint 1 at 0x80483d0: file fork-exec.c, line 19.
(gdb) c
The program is not being run.
(gdb) start
Breakpoint 2 at 0x8048437: file fork-exec.c, line 40.
Starting program: fork
main () at fork-exec.c:40
40              printf("Hello World..!!\n");
(gdb) c
Continuing.
Hello World..!!
[Switching to process 10649]

Breakpoint 1, spawn () at fork-exec.c:19
19                              if(i==0)         /* break here for child */
(gdb)

但是,如果我尝试通过 gdbserver 捕获子进程,断点就会丢失。

sh-3.2# gdbserver :1234 fork &
[5] 10686
sh-3.2# Process fork created; pid = 10689
Listening on port 1234

作为目标远程运行

sh-3.2# gdb fork
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
Remote debugging from host 127.0.0.1
[New Thread 10689]
0x00bd2810 in _start () from /lib/ld-linux.so.2
(gdb) break 19
Breakpoint 1 at 0x80483d0: file fork-exec.c, line 19.
(gdb) c
Continuing.
Hello World..!!
Bbye World..!!

Child exited with retcode = 0

Program exited normally.

Child exited with status 0
GDBserver exiting

在嵌入式世界中调试子进程的过程是什么。我知道我可以进行进程附加,但我想从子进程的一开始就进行调试。

I am unable to debug a child process of a remote debugging session. I found a similar question How to debug the entry-point of fork-exec process in GDB?

I am following the same procedure, although for a remote target. Is follow-fork-mode child supported for remote targets ?

Following is my sample code..

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <sys/types.h>
      4 #include <unistd.h>
      5
      6 int spawn (void)
      7 {
      8         pid_t child_pid;
      9         /* Duplicate this process. */
     10         child_pid = fork ();
     11         if (child_pid != 0)
     12                 /* This is the parent process. */
     13                 return child_pid;
     14         else {
     15                 /* Now execute PROGRAM, searching for it in the path. */
     16                 while(1)
     17                 {
     18                         static int i = 0;
     19                         if(i==0)         /* break here for child */
     20                         {
     21                                 printf("I am child\n");
     22                         }
     23                         else if(i==3)
     24                         {
     25                                 return 1;
     26                         }
     27                         else
     28                         {
     29                                 i = 0/0;
     30                         }
     31                         i++;
     32                 }
     33         }
     34         return 0;
     35 }
     36 int main ()
     37 {
     38         /* Spawn a child process running the .ls. command. Ignore the
     39            returned child process ID. */
     40         printf("Hello World..!!\n");
     41         spawn ();
     42         printf ("Bbye World..!!\n");
     43         return 0;
     44 }

Running it with gdb, I can set set break point in child.. all fine here.!!

sh-3.2# gdb fork
(gdb) set follow-fork-mode child
(gdb) set detach-on-fork off
(gdb) b 19
Breakpoint 1 at 0x80483d0: file fork-exec.c, line 19.
(gdb) c
The program is not being run.
(gdb) start
Breakpoint 2 at 0x8048437: file fork-exec.c, line 40.
Starting program: fork
main () at fork-exec.c:40
40              printf("Hello World..!!\n");
(gdb) c
Continuing.
Hello World..!!
[Switching to process 10649]

Breakpoint 1, spawn () at fork-exec.c:19
19                              if(i==0)         /* break here for child */
(gdb)

However if I try to catch child via gdbserver break point is lost..

sh-3.2# gdbserver :1234 fork &
[5] 10686
sh-3.2# Process fork created; pid = 10689
Listening on port 1234

Run as target remote

sh-3.2# gdb fork
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
Remote debugging from host 127.0.0.1
[New Thread 10689]
0x00bd2810 in _start () from /lib/ld-linux.so.2
(gdb) break 19
Breakpoint 1 at 0x80483d0: file fork-exec.c, line 19.
(gdb) c
Continuing.
Hello World..!!
Bbye World..!!

Child exited with retcode = 0

Program exited normally.

Child exited with status 0
GDBserver exiting

What is the procedure to debug child process in embedded world. I know I can do a process attach, but I want to debug from the very beginning of the child process..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-12-14 16:39:58

它被称为跟随分叉。不,gdbserver 不支持它。

It is called follow-fork. No, it is not supported in gdbserver.

木格 2024-12-14 16:39:58

作为一种(肮脏的!)解决方法,您可以在 fork() 之后添加一个 sleep() 调用,并延迟足够长的时间,以便您获取子 PID,将其附加到 gdbserver 的另一个实例并使用 gdb 连接到它。

As a (dirty!) workaround, you can just add a sleep() call right after the fork() with a delay long enough for you to get the child PID, attach it with another instance of gdbserver and connect to it with gdb.

夜灵血窟げ 2024-12-14 16:39:58

根据此错误报告,它应该与现代 gdb 版本一起使用。

It should work with a modern gdb version, according to this bug-report.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文