【问题标题】:Why gdb is not working for this simple hello world program?为什么 gdb 不适用于这个简单的 hello world 程序?
【发布时间】:2017-09-07 04:16:42
【问题描述】:

代码(m1.cpp):

#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
   cout << "running m1" << endl;

   return 0;
}

GDB 版本:GNU gdb (GDB) 7.6.2

使用:g++ -g m1.cpp

命令行历史:

(gdb) b main
Breakpoint 1 at 0x40087b: file m1.cpp, line 6.
(gdb) r
Starting program: .../a.out

Program received signal SIGSEGV, Segmentation fault.
0x00002aaaaaac16a0 in strcmp () from /lib64/ld-linux-x86-64.so.2
(gdb) c
Continuing.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb)

当我在不设置任何断点的情况下运行时,它运行时不会出错。

根据要求:

(gdb) bt
#0  strcmp () from /lib64/ld-linux-x86-64.so.2
#1  in check_match.12104 () from /lib64/ld-linux-x86-64.so.2
#2  in do_lookup_x () from /lib64/ld-linux-x86-64.so.2
#3  in _dl_lookup_symbol_x () from /lib64/ld-linux-x86-64.so.2
#4  in _dl_relocate_object () from /lib64/ld-linux-x86-64.so.2
#5  in dl_main () from /lib64/ld-linux-x86-64.so.2
#6  in _dl_sysdep_start () from /lib64/ld-linux-x86-64.so.2
#7  in _dl_start () from /lib64/ld-linux-x86-64.so.2
#8  in _start () from /lib64/ld-linux-x86-64.so.2
#9  in ?? ()

【问题讨论】:

  • 无法使用 GDB 7.12.0 重现。
  • 你是说我需要更新 GDB 吗?我所处的环境被许多其他程序员使用,所以我真的很好奇为什么它只会发生在我身上。
  • 你是如何编译你的代码的?你是如何安装 gdb 的?
  • 请张贴bt 输出。
  • 程序在 GDB 之外运行是否正常?

标签: c++ linux segmentation-fault gdb


【解决方案1】:

我能够复制 OP 观察到的行为(使用相同的编译并获得相同的回溯)。该行为在一系列 GDB 和 GCC 中持续存在。我注意到当我取消设置 SHELL 时症状消失了。在我的正常环境中,我使用 tcsh(版本 1.15.00)。如果设置了 SHELL,那么(我相信)gdb 使用 tcsh 启动。如果我取消设置 SHELL,gdb 会使用 sh 启动。这足以让我取得进步。对于 tcsh 中体现该问题的不同之处,我没有清晰的解释,但如果其他人有相同的行为,它可能会更清楚地说明这个问题。

【讨论】:

    【解决方案2】:

    我在我的 GNU gdb 版本 7.11.1 中检查了这一点。它在里面工作得很好。 我首先编译了相同的程序并使用:

    g++ -g m1.cpp
    

    然后,在 gdb 中运行可执行文件,如下所示:

    gdb -q ./a.out

    并且做了你提到的同样的事情。它工作得很好。 更新您的 gdb,然后再次检查并告知。

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多