首页 > 解决方案 > 在 Windows 终端中使用 GDB 有困难。GDB 在运行程序后自行退出,无需等待我的进一步命令

问题描述

我在底部包含了我的源代码。我还在下面复制粘贴我的终端和行号,我用粗体添加了自己以试图让自己清楚。我希望 (gdb) 提示符在第 10 行保持活动状态,但事实并非如此。它一直运行到第 14 行,如下所示。即终端本身在第 10 行给出退出命令,并一直运行直到在第 14 行停止。如何让它在第 10 行停止?换句话说,我如何阻止终端向(gdb)发出“退出”命令?

终端复制粘贴在下面

PS C:\Users\arunmozhi\Documents\Visual_Studio_C_Projects\Test> gdb a.exe
GNU gdb (GDB) Cygwin 7.9.1-1
Copyright (C) 2015 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 "i686-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"... 
Reading symbols from a.exe...done.
(gdb) run
Starting program: /cygdrive/c/Users/arunmozhi/Documents/Visual_Studio_C_Projects/Test/a.exe
**Line 1:**[New Thread 2136.0xdbc]
**Line 2:**[New Thread 2136.0x2958]
**Line 3:**[New Thread 2136.0xf20]
**Line 4:**[New Thread 2136.0x2a5c]
**Line 5:**[New Thread 2136.0x13f4]
**Line 6:**[New Thread 2136.0x109c]
**Line 7:** Program received signal SIGSEGV, Segmentation fault.
**Line 8:** 0x004011d4 in main () at test.c:8
**Line 9:** 8 sum+=data[i];
**Line 10:** (gdb) quit
**Line 11:** A debugging session is active.
**Line 12:** Inferior 1 [process 2136] will be killed.
**Line 13:** Quit anyway? (y or n) EOF [assumed Y]
**Line 14:** PS C:\Users\arunmozhi\Documents\Visual_Studio_C_Projects\Test>

源代码如下

#include<stdio.h>
int main()
{
    const int data[5]={1,2,3,4,5};
    int i=0, sum=0;
    for ( i=0; i>=0; i++)//incorrect code intentionally written to learn debugging with gdb
    {
        sum+=data[i];
    }
    printf("Sum = %d.\n",sum);
    return 0;
}

标签: cgdb

解决方案


设法弄清楚如何解决这个问题。它所需要的只是将 cygwin gdb 包从https://cygwin.com/install.html更新到当前版本。我的操作系统是 Windows 10 32 位,所以我使用了 setup-x86.exe。更新包用了不到 5 分钟的时间,它就像一种享受,但要更新包需要 5 天的大脑麻木研究。不起作用的旧版本是 7.9.7-1 (2015)。新版本是 9.2-1 (2020)。现在(gdb)在运行命令后不会自行退出,它等待我告诉它该做什么。我能够传递诸如等命令listprint能够得到预期的响应。修复问题后,我正在复制粘贴下面的终端。

Microsoft Windows [Version 10.0.19041.685]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\arunmozhi>cd documents
C:\Users\arunmozhi\Documents>cd visual_studio_c_projects
C:\Users\arunmozhi\Documents\Visual_Studio_C_Projects>cd test
C:\Users\arunmozhi\Documents\Visual_Studio_C_Projects\Test>ls
Test.c  a.exe
C:\Users\arunmozhi\Documents\Visual_Studio_C_Projects\Test>gdb a.exe
GNU gdb (GDB) (Cygwin 9.2-1) 9.2
Copyright (C) 2020 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 "i686-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.exe...
(gdb) run
Starting program: 
/cygdrive/c/Users/arunmozhi/Documents/Visual_Studio_C_Projects/Test/a.exe
[New Thread 10188.0xe98]
[New Thread 10188.0x195c]
[New Thread 10188.0x2c8c]
[New Thread 10188.0x14c0]
[New Thread 10188.0x3eac]

Thread 1 "a" received signal SIGSEGV, Segmentation fault.
0x004011d4 in main () at test.c:8
8               sum+=data[i];
(gdb) list 5
1       #include<stdio.h>
2       int main()
3       {
4           const int data[5]={1,2,3,4,5};
5           int i=0, sum=0;
6           for ( i=0; i>=0; i++)
7           {
8               sum+=data[i];
9           }
10          printf("Sum = %d.\n",sum);
(gdb)
11          return 0;
12      }
(gdb) print i
$1 = 209155
(gdb) print sum
$2 = -532459950
(gdb) set var i=2
(gdb) print i
$3 = 2
(gdb)

推荐阅读