c++ - 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
在 Qt Creator 中以 RELEASE 模式运行可以正常工作
按照评论中的建议从终端运行工作正常
g++ -pthread -o testThread main.cpp
./testThread
从终端运行 gdb 工作正常
$ gdb ./testThread
GNU gdb (Ubuntu 9.2-0ubuntu2) 9.2
版权所有 (C) 2020 Free Software Foundation, Inc.
从 ./testThread 读取符号...
(gdb) r
启动程序:testThread
[启用使用 libthread_db 进行线程调试]
使用主机 libthread_db 库“/lib/x86_64-linux-gnu/libthread_db.so.1”。
[新线程 0x7ffff7a51640 (LWP 5946)]
[线程 0x7ffff7a51640 (LWP 5946) 已退出]
都好!
【下1(进程5942)正常退出】
(gdb)
更新 2
似乎这个问题与 Qt Creator 有关。有两种解决方法。
将此添加到“调试助手自定义”中:
处理 SIGSTOP pass nostop noprint
- 或者在项目的运行配置中禁用复选框“在终端中运行”
解决方案
推荐阅读
- javascript - 添加到 pdf 自定义 html 而不将其添加到 DOM
- mysql - 将 Sequelize 连接到 MySQL 生成访问被拒绝错误
- python - dask 性能随着传递给 dask.compute 且 scheduler='processes' 的延迟对象数量的增加而降低
- jenkins - 如何让詹金斯在 git 中寻找拉取请求,而不是在詹金斯中触发 git 工作?
- java - 在 Spring 应用程序中设置查询超时连接的问题
- apache-spark - 修改 Spark DataFrame 结构
- crystal-reports - 如果满足条件,则返回先前的公式结果
- xcode - Xcode:模板不显示
- sql - 没有明确列出列名的 SQL 联合 NULL 列
- go - 除了结构字段标签之外的其他元数据