【问题标题】:Running a gdb command before attaching it to a process via Visual Studio Code在通过 Visual Studio Code 将其附加到进程之前运行 gdb 命令
【发布时间】:2020-05-03 01:06:13
【问题描述】:

我正在尝试使用 Visual Studio Code 作为我在 Linux 上的 IDE 逐步执行 Postgresql 代码。我正在使用附加到 launch.json 中的进程配置来实现相同的目的。以下是 launch.json 配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "attach",
            "program": "/usr/local/pgsql/bin/postgres",
            "processId": 4165,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true,
                }
            ]
        }
    ]
}

当我通过 GUI 启动调试时,它会附加到进程。但是每当我添加断点时,我都会在调试控制台上打印以下消息:

Program received signal SIGINT, Interrupt.
0x00007ff5d084e31b in epoll_wait () from /lib64/libc.so.6

并且无法添加断点。从 Postgres 开发人员文档 (link) 中可以清楚地看出,我们需要通过向 gdb 发出以下命令来绕过到达 gdb 的中断:

handle SIGUSR1 noprint pass

我认为gdb中的这个命令只能在附加进程进行调试之前执行。因此,当我通过 Visual Studio Code 上的调试控制台运行此命令时,出现以下错误:

Unable to perform this action because the process is running.

有没有办法指示 Visual Studio Code 调试,在通过 gdb 附加目标进程之前向 gdb 发出“handle SIGUSR1 noprint pass”?

【问题讨论】:

    标签: postgresql debugging visual-studio-code gdb interrupt


    【解决方案1】:

    经过更多研究,我找到了一种使用 ~/.gdbinit 文件实现此目的的方法。该文件可以包含每次运行 gdb 时都会运行的命令。我里面有以下内容:

    handle SIGUSR1 nostop noprint pass
    handle SIGINT nostop noprint pass
    

    现在发生的情况是,由于 SIGINT 被覆盖,每次 IDE 与进程断开连接时,它都会重新启动,因为它无法再正常断开连接。

    【讨论】:

      【解决方案2】:

      考虑在 launch.jsonsetupCommands 部分定义这些命令:

      {
          "version": "0.2.0",
          "configurations": [    
              {
                  "name": "debugging of local server",
                  "type": "cppdbg",
                  "request": "attach",
                  "program": "/usr/local/pgsql/bin/postgres",
                  "processId": "${command:pickProcess}",
                  "MIMode": "gdb",
                  "setupCommands": [
                      {
                          "description": "Enable pretty-printing for gdb",
                          "text": "-enable-pretty-printing",
                          "ignoreFailures": true
                      },
                      {
                          "description": "ignore SIGUSR1 signal",
                          "text": "handle SIGUSR1 nostop noprint pass"
                      }
                  ]            
              }
          ]
      }
      

      【讨论】:

        【解决方案3】:

        除了Keshav的回答。 您还可以在 ~/.gdbinit 文件中添加另一个命令:
        set auto-load safe-path /
        这将告诉编译器它可以使用工作目录中的本地 .gdbinit 文件。 现在您可以为每个项目/目录创建一个单独的 .gdbinit 并让它们独立配置,而不会弄乱全局 .gdbinit。

        【讨论】:

          猜你喜欢
          • 2011-09-20
          • 2016-06-04
          • 1970-01-01
          • 2019-07-31
          • 1970-01-01
          • 1970-01-01
          • 2021-02-19
          • 2021-05-11
          • 1970-01-01
          相关资源
          最近更新 更多