【问题标题】:C++ gdb breakpooint in second thread causing core dump第二个线程中的 C++ gdb 断点导致核心转储
【发布时间】:2022-11-11 03:37:51
【问题描述】:

我有一个 C++ 程序,我试图在一个新的 docker 实例中调试,当我尝试在创建的线程(但不是启动线程)中命中断点时,导致程序崩溃。该程序的另一个版本在单独的计算机上运行(不同的 g++、gdb 版本)可以工作。我创建了一个测试应用程序来复制问题。

我正在使用 ubuntu docker 映像(这对我来说是新的)。我已经启动了一个 docker 容器:

docker run -it -v "/home/test/":"/home/test" -w "/home/test" ubuntu

我已在 /home/test/ 文件夹中的 VS Code 中附加到此容器。然后我安装了 g++ 和 gdb:apt-get update 然后apt-get install g++ gdb。 这将安装版本 g++: 4:11.2.0-1ubuntu1 gdb: 12.0.90-0ubuntu1

然后我使用以下代码创建一个main.cpp:

#include <iostream>
#include <chrono>
#include <thread>

void thread_runner()
{
    while (1) {
        std::cout << "Background Thread" << std::endl;                //Second Breakpoint
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}

int main()
{
    std::cout << "Test App" << std::endl;                              // First Breakpoint
    std::thread ioc_thread = std::thread(thread_runner);                    // Create a separate (background) thread to run the io_context on
    ioc_thread.join();
}

我在 2 std::cout ... 行上设置了断点。 我创建了一个 tasks.json 文件来编译它,它看起来像:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
    ],
    "version": "2.0.0"
}

然后我运行命令“运行和调试”并使用该任务进行编译。我在“测试应用程序”处击中了第一个断点,但它在击中“后台线程”之前崩溃了,终端输出是:

Test App
Aborted (core dumped)
[1] + Aborted (core dumped)      "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-mv2ltsxy.3ok" 1>"/tmp/Microsoft-MIEngine-Out-0zvvty3q.t43"

如果我删除第二个断点,应用程序每秒都会运行打印“后台线程”。

为冗长的描述道歉。我不确定我哪里出错了,所以我希望包括所有内容。另一台使用 g++ 9.3 和 gdb 8.1 似乎可以工作的计算机(使用我的完整原始程序)。

【问题讨论】:

  • 我在这里看到的唯一错误是您在编译时没有将-pthread 标志传递给GCC。添加后会发生什么?
  • @JonathanS。所以它使用给定版本的 GCC 编译得很好,但是使用 ubuntu:bionic 执行相同的过程(使用旧版本)需要我添加 -pthread 来编译。我会尝试添加它。
  • @JonathanS。在传递给 g++ 的参数中添加“-pthread”无效。

标签: c++ multithreading gdb


【解决方案1】:

我有一个类似的问题,当线程中存在断点时,我的程序会进行核心转储。我发现我的 gdb (12.0.9) 实例不起作用,installing 12.1 from launchpad 解决了我的问题。

找到解决方案here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多