Chinaunix

标题: 真心请教一个gdb调试子进程的问题 [打印本页]

作者: nevastill    时间: 2012-04-25 18:09
标题: 真心请教一个gdb调试子进程的问题
代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

void test(pid_t pid)
{
  while(1) {
  sleep(1);
  }
}

void test1(pid_t pid)
{
  while(1) {
  sleep(1);
  }
}

int main()
{
  pid_t pid = 0;
  int ret = 0;

  pid = fork();
  if (pid == 0) {
  //setsid();
  test1(pid);
  } else if (pid > 0)
  {
  printf("This is the parent process! my id=%d ,child process id = %d\n", getpid(),pid);
  test(pid);
  } else {
  printf("fork error\n");
  }

  return 1;
}

gdb对子进程进行调试:
aven@dolphin-j:~> ./ctrl_c_test &
[1] 6537
This is the parent process! my id=6537 ,child process id = 6538
aven@dolphin-j:~> gdb attach 6538
GNU gdb (GDB) SUSE (7.0-0.4.16)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-suse-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
attach: No such file or directory.
Attaching to process 6538
Reading symbols from /home/aven/ctrl_c_test...done.
Reading symbols from /lib64/libc.so.6...Missing separate debuginfo for /lib64/libc.so.6
Try: zypper install -C "debuginfo(build-id)=a41ac0b0b7cd60bd57473303c2c3de08856d2e06"
(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
Try: zypper install -C "debuginfo(build-id)=17c088070352d83e7afc43d83756b00899fd37f0"
(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f9b618bf060 in __nanosleep_nocancel () from /lib64/libc.so.6
(gdb) c
Continuing.
^C^C^C^C^Z

gdb attach父进程则能够正常退出。ctrl+c只能用于前台的进程,我不知道此处是否是因为gdb attach的时候不能把子进程拿到前台来运行。

另外,在子进程中用setsid()切断和现在进程组(作业)的关系,或者新开一个session(窗口)来gdb调试子进程,都能够正常退出。什么原因?


作者: walleeee    时间: 2012-04-25 18:10
http://bbs.chinaunix.net/thread-3727715-1-1.html
作者: shanehan    时间: 2012-04-26 10:10
楼上你能不每次发这个吗?
作者: walleeee    时间: 2012-04-26 10:11
回复 3# shanehan


   
作者: GFree_Wind    时间: 2012-04-26 12:12
好奇二楼发的啥帖子。。。
作者: nevastill    时间: 2012-04-26 15:44
好吧,我自己顶一顶。
作者: wi1she    时间: 2012-04-26 19:26
回复 1# nevastill
你上面写的
  1. gdb attach父进程则能够正常退出。ctrl+c只能用于前台的进程
复制代码
这里的退出是什么意思?你是想从这个进程执行的状态跳转到gdb的单步调试的状态,还是直接退出你attach的进程啊。
这里ctrl+c的信号也不涉及到你那两个进程是否在前台的哦。你的ctrl+C在你调试的时候只会被GDB收到的。然后gdb自己回退到单步调试里面去。你可以在你的父子进程里面加上打印信息。然后再把SIGINT信号捕捉处理一下,你就会发现,CTRL+C在这里的作用是什么了。
   
作者: nevastill    时间: 2012-04-27 08:14
回复 7# wi1she
如你所说的话,为什么调试父进程就能够回到单步调试,而调试子进程就不行。另外我查看资料,貌似gdb是不拦截ctrl+c信号的,不知是否如此。
作者: wi1she    时间: 2012-04-27 08:37
回复 8# nevastill
为什么你哪里子进程不可以我也不知道了!在我这里两个进程都是可以的哈!正常的默认的gdb捕获这些信号是退出当前进程的执行状态方便调试的!
你这里的gdb不拦截SIGINT意思是gdb收到SIGINT,gdb本身不退出是吧!是因为他自己捕获了这个信号,处理这个信号不是退出gdb而是退出gdb下调试的进程。这些是我了解到的哈!你哪里为什么子进程退不出来我也不知道了。

   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2