当自动连接并退出时,GDB pid会发生变化

时间:2017-11-14 08:31:30

标签: c linux gdb

我正在尝试附加现有进程,运行一些命令并打印所需信息。但是,当我这样做时,我看到进程的PID被更改(显示killed)命令。

代码

     1  #include <iostream>
     2  #include <unistd.h>
     3  using namespace std;
     4
     5
     6  int main()
     7  {
     8      do
     9      {
    10          static int s = 100;
    11          s = s+1;
    12          sleep (3);
    13      } while(1);
    14      return 0;
    15  }
    16

GDB命令

> cat /tmp/command.txt    
set pagination off
set logging file /home/testgrp/gdb.txt
set logging on
b sample.cc:11
commands 1
    p s
end
run 1
quit

输出

root@198.18.81.198:/desktop/user1/workspace# ps -eaf | grep out
root     16724  8877  0 08:25 pts/1    00:00:00 grep --color=auto out
root@198.18.81.198:/desktop/user1/workspace# cat /home/testgrp/gdb.txt
cat: /home/testgrp/gdb.txt: No such file or directory
root@198.18.81.198:/desktop/user1/workspace# ./a.out &
[1] 16762
root@198.18.81.198:/desktop/user1/workspace# gdb --batch-silent -x=/tmp/command.txt -p 16762
[1]+  Killed                  ./a.out
root@198.18.81.198:/desktop/user1/workspace# ps -eaf | grep out
root     16805     1  0 08:25 pts/1    00:00:00 /desktop/user1/workspace/a.out 1
root     16823  8877  0 08:25 pts/1    00:00:00 grep --color=auto out
root@198.18.81.198:/desktop/user1/workspace# cat /home/testgrp/gdb.txt
Breakpoint 1 at 0x400711: file sample.cc, line 11.
$1 = 100
A debugging session is active.

    Inferior 1 [process 16805] will be detached.

Quit anyway? (y or n) [answered Y; input not from terminal]

问题

  1. 如何在不更改流程PID的情况下获取所需信息?
  2. 更重要的是,为什么pid会改变,之前的PID为killed
  3. 附录

    GDB版

    GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2aka8.0.1) 7.7.1
    Copyright (C) 2014 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-linux-gnu".
    

1 个答案:

答案 0 :(得分:4)

在你的gdb脚本的最后两行

run 1
quit

“run 1”将重启正在调试的程序,参数为“1”。默认情况下,它应该要求您确认是否重启。但是当您启动gdb时,您有一个参数“--batch-silent”。所以你的过程重启没有消息。

删除“--batch-silent”和gdb脚本的最后两行,然后就可以中断和调试。 “b sample.cc:11”将在系统函数睡眠中停止,如果您感到困惑,可以将其更改为其他行。 (我建议你在使用gdb之前阅读一些更简单的演示,这样很多cmd会让初学者感到困惑)