我被困住了,可以用一双新鲜的眼睛。
我有一个相对简单的 Linux 程序,我使用 CodeSourcery 工具链为 Variscite OMAP4 开发板进行了交叉编译。该程序只是创建一个线程,等待线程完成并存在。该线程只输出几个字符串,中间有睡眠。
我的问题是线程创建后线程中的程序分段错误。回溯表明该崩溃发生在标准共享库中。GDB 可以告诉我加载的库不是加载的 .so 文件的地址,所以我不知道哪个库崩溃以及库中的偏移量。
下面是代码、构建输出、程序输出和 GDB 输出。
任何人都有一些想法可以检查以帮助找到根本原因?
我的主程序
#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;
}
我的线程.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