首页 > 解决方案 > C++ 应用程序在创建 std::thread 时立即在 clone.S 中的某处崩溃

问题描述

为什么/sysdeps/unix/sysv/linux/x86_64/clone.S在 Qt Creator 中运行这个自动生成的最小(no-qt)C++ 应用程序时在 Ubuntu 20.10 上(反汇编)“ test %rax,%rax ”处因 SIGSTOP 而崩溃4.14.2 在调试模式下:

#include <iostream>
#include <thread>

void foo()
{
}

int main()
{
    std::thread t(foo); // Crash happens after stepping over this line

    t.join();

    std::cout << "All good!" << std::endl;

    return 0;
}

调用栈

1 clone clone.S         78  0x7f28abaef215 
2 ??    allocatestack.c 313 0x7f28abbc94b0 
3 ??                        0x7f28ab881640 
4 ??                                       

allocatestack.c 的反汇编程序停在:

pthread_create.c:
0x7f28abbc94b0                  f3 0f 1e fa                    endbr64

clone.S 的反汇编程序在“test %rax,%rax”处停止:

        50 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891e0                  f3 0f 1e fa           endbr64
        52 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891e4  <+    4>        48 c7 c0 ea ff ff ff  mov    $0xffffffffffffffea,%rax
        53 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891eb  <+   11>        48 85 ff              test   %rdi,%rdi
        54 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891ee  <+   14>        74 3d                 je     0x7f2f1638922d <clone+77>
        55 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f0  <+   16>        48 85 f6              test   %rsi,%rsi
        56 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f3  <+   19>        74 38                 je     0x7f2f1638922d <clone+77>
        59 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f5  <+   21>        48 83 ee 10           sub    $0x10,%rsi
        60 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f9  <+   25>        48 89 4e 08           mov    %rcx,0x8(%rsi)
        64 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891fd  <+   29>        48 89 3e              mov    %rdi,(%rsi)
        67 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389200  <+   32>        48 89 d7              mov    %rdx,%rdi
        68 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389203  <+   35>        4c 89 c2              mov    %r8,%rdx
        69 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389206  <+   38>        4d 89 c8              mov    %r9,%r8
        70 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389209  <+   41>        4c 8b 54 24 08        mov    0x8(%rsp),%r10
        71 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f1638920e  <+   46>        b8 38 00 00 00        mov    $0x38,%eax
        76 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389213  <+   51>        0f 05                 syscall
        78 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389215  <+   53>        48 85 c0              test   %rax,%rax

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(testThread LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# These 3 lines were added by me to the auto generated CMakeLists.txt
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE) # Ensures that -pthread is added to the compile flags
find_package(Threads REQUIRED)

add_executable(testThread main.cpp)

target_link_libraries(testThread Threads::Threads)

CMake 输出

Running /usr/bin/cmake -S /home/myhome/testThread -B /home/myhome/testThread/build-debug '-GCodeBlocks - Ninja' -DCMAKE_BUILD_TYPE:String=Debug -DQT_QMAKE_EXECUTABLE:STRING= -DCMAKE_PREFIX_PATH:STRING= -DCMAKE_C_COMPILER:STRING=/usr/bin/gcc -DCMAKE_CXX_COMPILER:STRING=/usr/bin/g++ in /home/myhome/testThread/build-debug.
-- The CXX compiler identification is GNU 10.2.0
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    QT_QMAKE_EXECUTABLE


-- Build files have been written to: /home/myhome/testThread/build-debug
Elapsed time: 00:00.

CMake 构建输出

17:24:26: Running steps for project testThread...
17:24:26: Starting: "/usr/bin/cmake" --build . --target all --verbose
[1/2 3.9/sec] /usr/bin/g++    -g   -pthread -std=gnu++11 -MD -MT CMakeFiles/testThread.dir/main.cpp.o -MF CMakeFiles/testThread.dir/main.cpp.o.d -o CMakeFiles/testThread.dir/main.cpp.o -c ../main.cpp
[2/2 6.9/sec] : && /usr/bin/g++  -g   CMakeFiles/testThread.dir/main.cpp.o  -o testThread  -pthread && :
17:24:26: The process "/usr/bin/cmake" exited normally.
17:24:26: Elapsed time: 00:00.

g++ 版本

~$ g++ --version
g++ (Ubuntu 10.2.0-13ubuntu1) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.

到目前为止,我尝试的是在不使用 ccache 和空构建文件夹的情况下创建这个最小示例,但问题仍然存在。我没主意了。

非常感谢帮助!

更新 1

更新 2

似乎这个问题与 Qt Creator 有关。有两种解决方法。

  1. 将此添加到“调试助手自定义”中:

    处理 SIGSTOP pass nostop noprint

在此处输入图像描述

  1. 或者在项目的运行配置中禁用复选框“在终端中运行”

标签: c++multithreadingcrashpthreadsqt-creator

解决方案


推荐阅读