仅在 GDB 调试期间出现分段错误

Segmentation fault only during GDB debugging

我交叉编译了使用 boost::asio 库的应用程序,并在我的目标系统上对其进行了测试。它工作正常。但是当我尝试使用 gdb 调试我的应用程序时,我在 gdb-console 中收到此消息:

Program received signal SIGSEGV, Segmentation fault.
_dl_debug_initialize (ldbase=4294967292, ns=1996288212) at dl-debug.c:55
55           if (r->r_map == NULL || ldbase != 0)

远程和本机调试以及其他几个 boost 库(但并非全部)的结果相同。搜索任何可能有用的信息后,我在此文档 (p.63) 中发现了类似的问题:http://support.garz-fricke.com/products/Santaro/Linux-Yocto/Releases/Yocto-jethro-5.1-r6859-0/GUF-Yocto-jethro-5.1-r6859-0-IMX6GUF-Manual.pdf 如文档中所述,问题可能是由 "Static instanciation in implicitly implemented C++ methods" 引起的,并且与 glibc 相关。所以我尝试用代码通过这种方法重现这个错误:

#include <iostream> 
using namespace std; 
class AClass 
{ 
public:   
  void foo()   
  {
    static int NmbOfInvokes = 0;
    NmbOfInvokes++;
    cout << NmbOfInvokes << endl;   
  } 
};
int main(void) 
{
  cout << "Hello World" << endl;

  AClass anInstance;
  anInstance.foo();
  anInstance.foo();
  return 0;
}

此程序运行正常,但在调试时失败并出现相同的 SIGSEGV 错误。

要修复它,以这种方式重写 class AClass:

class AClass
{
public:
  void foo();
};

void AClass::foo()
{
  static int NmbOfInvokes = 0;
  NmbOfInvokes++;
  cout << NmbOfInvokes << endl;
}

编译标志:

arm-poky-linux-gnueabi-g++  -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi -DHAVE_CONFIG_H -I. -I..   --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi  -g -O0  --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi -MT SegFault_Reproduce.o -MD -MP -MF .deps/SegFault_Reproduce.Tpo -c -o SegFault_Reproduce.o SegFault_Reproduce.cpp mv -f .deps/SegFault_Reproduce.Tpo .deps/SegFault_Reproduce.Po
../arm-poky-linux-gnueabi-libtool  --tag=CXX   --mode=link arm-poky-linux-gnueabi-g++  -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi  -g -O0  --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi  --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi -o SegFault_Reproduce SegFault_Reproduce.o  
arm-poky-linux-gnueabi-libtool: link: arm-poky-linux-gnueabi-g++ -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi -g -O0 --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi --sysroot=/opt/fsl-imx-x11/4.1.15-1.2.0/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi -o SegFault_Reproduce SegFault_Reproduce.o

我想在某些boost库中使用了类似的静态实例化,因为症状完全相同。

我该怎么做才能获得调试 boost 应用程序的可能性?

我使用的包版本:yocto 2.0.1, gcc 5.2.0, gdb 7.9.1, boost 1.58.

But when I try to debug my app with gdb, I get this message

这表明 GDB 中存在错误。您的版本:7.9.1 快 2 岁了。您的第一步应该是尝试更新的 GDB 版本。

"under GDB" 和 "native" 执行之间的一个区别是 GDB 禁用了 ASLR。

您可以在 运行 程序之前尝试 (gdb) set disable-randomization off。但是鉴于你描述的症状,我怀疑这与问题有什么关系。