未在辅助线程中捕获异常,导致segfault

问题描述 投票:1回答:1

我交叉编译基于MIPS的平台。以下代码导致段错误,回溯导致__cxa_throw:

#include <cstdio>
#include <exception>
#include <thread>

#include <execinfo.h>
#include <signal.h>
#include <unistd.h>

void handler(int sig) {
  fprintf(stderr, "Error: signal %d:\n", sig);
  void *array[10];
  size_t size;

  // get void*'s for all entries on the stack
  size = backtrace(array, 10);

  fprintf(stderr, "Error: signal %d:\n", sig);
  // print out all the frames to stderr
  backtrace_symbols_fd(array, size, STDERR_FILENO);
  exit(1);
}

void deco()
{
    puts("Secondary thread started");
    for (int i = 0; i < 100; ++i)
    {
        try {
            puts("throwing");
            throw std::runtime_error("test exception");
        }
        catch (std::exception & e)
        {
            puts("catched");
            puts(e.what());
        }
        puts("going on");
    }
    puts("Secondary thread ending");
}


int main()
{
    signal(SIGSEGV, handler);
    std::thread t2(deco);
    t2.join();
    return 0;
}

我的makefile:

all: build

build: 
    /opt/mips/bin/mips-linux-gnu-g++  \
          -std=c++11 -O3 -mips32r2 -mtune=xburst -mmxu2 -mhard-float -mel -static -pthread -g -rdynamic \
          -Wl,--no-export-dynamic -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--as-needed \
          cv_test.cpp -o j_test \
          -lm -latomic -lrt
    cp ./j_test /srv/tftp/j_test

clean:
    rm -rf *.o j_test

[编译器是一些基因的定制构建:mips-linux-gnu-g ++(基因的r3.2.1-gcc520 2017.12-15)5.2.0]

如果deco()在主线程而不是辅助线程中运行,则不会发生崩溃。有什么线索吗?可能是众所周知的gcc标准库问题?有解决方法吗?

c++ exception gcc thread-safety mips
1个回答
0
投票

静态链接出了点问题。

© www.soinside.com 2019 - 2024. All rights reserved.