调用线程构造函数时出现Segfault



我被卡住了,可以用一双新眼睛了。

我有一个相对简单的Linux程序,我使用CodeSourcery工具链为Variscite OMAP4开发板交叉编译了该程序。程序只需创建一个线程,等待线程完成,然后就存在了。线程只输出几个字符串,中间有sleep。

我的问题是,在创建线程之后,线程中的程序分段会出错。回溯表明崩溃发生在一个标准共享库中。GDB可以告诉我加载的库,而不是加载的.so文件的地址,所以我不知道哪个库崩溃了,也不知道库的偏移量。

代码、构建输出、程序输出和GDB输出如下。

有人有什么想法可以检查以帮助找到根本原因吗?

MyMain.cpp

#include <thread>
#include <iostream>
using namespace std;
void MyThreadStart ();
int main ()
{
cout << "main started." << endl;
std::thread t2 (&MyThreadStart);
cout << "thread CTOR invokved." << endl;
t2.join();
cout << "thread join completed." << endl;
return 0;
}

MyThread.cpp

#include <iostream>
using namespace std;
#ifdef __cplusplus
extern "C" {
#include <unistd.h>
}
#endif
void MyThreadStart ()
{
cout << "thread started." << endl;
int loops = 3;
while (loops--)
{
cout << "thread going to sleep." << endl;
sleep (2);
cout << "thread woke up." << endl;
sleep (1);
}
}

使输出

16:51:39 **** Build of configuration Debug for project SegFaultTest ****
make all
Building file: ../MyMain.cpp
Invoking: ARM Sourcery Linux GCC C++ Compiler
arm-none-eabi-g++ -nostdinc -nostdinc++ -I/opt/arm/arm-none-linux-gnueabi/libc/usr/include -I/opt/arm/lib/gcc/arm-none-linux-gnueabi/4.7.2/include -I/opt/arm/arm-none-linux-gnueabi/include/c++/4.7.2 -I/opt/arm/arm-none-linux-gnueabi/include/c++/4.7.2/arm-none-linux-gnueabi -O0 -Wall -Wa,-adhlns="MyMain.o.lst" -fno-exceptions -fno-rtti -c -fmessage-length=0 -std=c++11  -mno-thumb-interwork -MMD -MP -MF"MyMain.d" -MT"MyMain.d" -mcpu=arm7 -g3 -gdwarf-2 -o "MyMain.o" "../MyMain.cpp"
Finished building: ../MyMain.cpp
Building file: ../MyThread.cpp
Invoking: ARM Sourcery Linux GCC C++ Compiler
arm-none-eabi-g++ -nostdinc -nostdinc++ -I/opt/arm/arm-none-linux-gnueabi/libc/usr/include -I/opt/arm/lib/gcc/arm-none-linux-gnueabi/4.7.2/include -I/opt/arm/arm-none-linux-gnueabi/include/c++/4.7.2 -I/opt/arm/arm-none-linux-gnueabi/include/c++/4.7.2/arm-none-linux-gnueabi -O0 -Wall -Wa,-adhlns="MyThread.o.lst" -fno-exceptions -fno-rtti -c -fmessage-length=0 -std=c++11  -mno-thumb-interwork -MMD -MP -MF"MyThread.d" -MT"MyThread.d" -mcpu=arm7 -g3 -gdwarf-2 -o "MyThread.o" "../MyThread.cpp"
Finished building: ../MyThread.cpp
Building target: SegFaultTest.elf
Invoking: ARM Sourcery Linux GCC C++ Linker
arm-none-eabi-g++ -Wl,-Map,SegFaultTest.map -mcpu=arm7 -g3 -gdwarf-2 -o "SegFaultTest.elf"  ./MyMain.o ./MyThread.o   -lpthread
Finished building target: SegFaultTest.elf

程序输出

$ ./SegFaultTest.elf
main started.
thread CTOR invokved.
Segmentation fault (core dumped)

GDB输出

info shared
From        To          Syms Read   Shared Object Library
No          /lib/arm-linux-gnueabihf/libpthread.so.0
No          /usr/lib/arm-linux-gnueabihf/libstdc++.so.6
No          /lib/arm-linux-gnueabihf/libm.so.6
No          /lib/arm-linux-gnueabihf/libgcc_s.so.1
No          /lib/arm-linux-gnueabihf/libc.so.6
No          /lib/ld-linux.so.3
bt
#0  0xb6f5e3d0 in ?? ()
#1  0xb6fb8ed2 in ?? ()
Backtrace stopped: frame did not save the PC

经过更多的调查和实验,我确定崩溃是由于我正在使用的Variscite板的stdc++库中的缺陷造成的。我在代码中启用了2011 C++标准扩展,这样我就可以使用线程类了。当调用线程构造函数时,pthread库start_thread函数会调用stdc++库中的某个unnown函数,并发生崩溃。我更改了代码,不使用2011 C++扩展,而是使用pthread调用,然后我的简单代码就工作了。我已将此缺陷告知Variscite。

相关内容

  • 没有找到相关文章

最新更新