C++ 使用Visual Studio代码(MacOS)调试Pybind11扩展

C++ 使用Visual Studio代码(MacOS)调试Pybind11扩展,c++,debugging,visual-studio-code,pybind11,python-extensions,C++,Debugging,Visual Studio Code,Pybind11,Python Extensions,我最近一直在使用pybind11,现在我已经掌握了它的窍门,我对它感到非常兴奋。这是一件很棒的作品。执行pybind11的工具拼图的最后一块是调试部分。我已经使用以下指南对lldb进行了命令行调试: 我花了一些时间尝试使用VisualStudio代码进行调试,但效果有限。第一个问题是,为了设置附加配置,需要指定python可执行文件(而不是进程id)。如果您有多个活动的python进程,我不知道这应该如何工作,这种情况经常发生 撇开这一点不谈,我设置了一个启动配置,指向ipython可执行文件

我最近一直在使用pybind11,现在我已经掌握了它的窍门,我对它感到非常兴奋。这是一件很棒的作品。执行pybind11的工具拼图的最后一块是调试部分。我已经使用以下指南对lldb进行了命令行调试:

我花了一些时间尝试使用VisualStudio代码进行调试,但效果有限。第一个问题是,为了设置附加配置,需要指定python可执行文件(而不是进程id)。如果您有多个活动的python进程,我不知道这应该如何工作,这种情况经常发生

撇开这一点不谈,我设置了一个启动配置,指向ipython可执行文件,这是最方便使用的。当我尝试开始调试时,我得到以下结果:

有人能解释一下吗

如果我将可执行文件更改为纯Python,我将在调试控制台中获得以下内容:

Could not initialize Python interpreter - only native expressions will be available.
Launching: /Users/andy/anaconda3/envs/SciPy37/bin/python
但是如果转到终端窗口,我可以成功地输入Python表达式,并触发代码中设置的断点。好极了但还有一个问题。当我的一个扩展需要加载另一个动态库时,它找不到它

>>> import block_test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/andy/Dropbox (Personal)/Developer/AudioDev/GenericDSP/Common/SciPy/BlockTest/build/block_test.cpython-37m-darwin.so, 2): Symbol not found: _vDSP_vsmul
  Referenced from: /Users/andy/Dropbox (Personal)/Developer/AudioDev/GenericDSP/Common/SciPy/BlockTest/build/block_test.cpython-37m-darwin.so
  Expected in: flat namespace
 in /Users/andy/Dropbox (Personal)/Developer/AudioDev/GenericDSP/Common/SciPy/BlockTest/build/block_test.cpython-37m-darwin.so
导入块测试 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 导入错误:dlopen(/Users/andy/Dropbox(Personal)/Developer/AudioDev/GenericDSP/Common/SciPy/BlockTest/build/block_test.cpython-37m-darwin.so,2):未找到符号:_vDSP_vsmul 引用自:/Users/andy/Dropbox(Personal)/Developer/AudioDev/GenericDSP/Common/SciPy/BlockTest/build/block_test.cpython-37m-darwin.so 应为:平面命名空间 在/Users/andy/Dropbox(Personal)/Developer/AudioDev/GenericDSP/Common/SciPy/BlockTest/build/block_test.cpython-37m-darwin.so中 这是有道理的,因为_vDSP_vsmul是Apple DSP加速库的一部分。但我不明白的是,当我使用本文开头提到的命令行调试技术时,我没有这个问题。显然,这与dylibs的发现方式有一定的关系,但为什么这与命令行的情况不同呢


在这些问题上有任何帮助都会很好。在Visual Studio代码中使用这些调试功能是Python和C++之间绝对惊人的互操作性所缺少的部分。

我找到了如何连接到进程调试,以便在iPython和Mac OS Catalina上使用Visual Studio代码。launch.json中的以下代码将起作用:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug",
      "program": "path_to/bin/python",
      "args": [],
      "cwd": "${workspaceFolder}/build"
    },
    { 
      "name": "Debug Attach",
      "type": "lldb",
      "request": "attach",
      "program": "path_to/bin/python",
      "processId": "${command:pickProcess}",
      "MIMode": "gdb"
  },
  ]
}
首先,在尝试开始调试之前,在终端窗口中,启动IPython,以便有一个进程要连接


起初,我遇到了VSC的问题,抱怨json文件中没有“程序”条目,这在附加到进程时似乎无关紧要。事实上,它可能是完全不相关的,但你必须添加它才能使事情正常进行。我还没有尝试为程序参数输入一个虚假条目,以查看值是否重要


另一个细节——有时VSC中的断点看起来没有启用,创建它们时会得到一个未填充的圆,而不是一个红点。但不管怎样,您似乎都会遇到断点。但是,根据我以前的经验,最好在连接到进程后将测试中的包导入到iPython中,以获得最佳结果。

我找到了如何连接到进程调试,以便在iPython和Mac OS Catalina上使用Visual Studio代码。launch.json中的以下代码将起作用:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug",
      "program": "path_to/bin/python",
      "args": [],
      "cwd": "${workspaceFolder}/build"
    },
    { 
      "name": "Debug Attach",
      "type": "lldb",
      "request": "attach",
      "program": "path_to/bin/python",
      "processId": "${command:pickProcess}",
      "MIMode": "gdb"
  },
  ]
}
首先,在尝试开始调试之前,在终端窗口中,启动IPython,以便有一个进程要连接


起初,我遇到了VSC的问题,抱怨json文件中没有“程序”条目,这在附加到进程时似乎无关紧要。事实上,它可能是完全不相关的,但你必须添加它才能使事情正常进行。我还没有尝试为程序参数输入一个虚假条目,以查看值是否重要


另一个细节——有时VSC中的断点看起来没有启用,创建它们时会得到一个未填充的圆,而不是一个红点。但不管怎样,您似乎都会遇到断点。然而,根据我以前的经验,最好在附加到进程后将测试中的包导入iPython中,以获得最佳结果。

我偶然发现了这篇文章,我不必附加到已经运行的调试器,而是能够从VSCode中启动gdb:

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch 1123",
            "type": "cppdbg",
            "request": "launch",
            "program": "/usr/bin/python3",
            "args": ["${file}"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

<>从这个代码>启动.JSON<代码>安装程序,我可以通过我的C++代码,在这里设置断点并调试我的C++代码。但是,正如前一篇SO帖子的评论中提到的,我不确定在调试会话期间是否有一种方法在python和C/C++代码之间流动。如果试图在我的python文件上放置断点,我也会“在创建断点时得到一个未填充的圆圈,而不是一个红点”。

我偶然发现了这篇文章,我不必附加到已经运行的调试器,而是能够从VSCode中启动gdb:

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch 1123",
            "type": "cppdbg",
            "request": "launch",
            "program": "/usr/bin/python3",
            "args": ["${file}"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

<>从这个代码>启动.JSON<代码>安装程序,我可以通过我的C++代码,在这里设置断点并调试我的C++代码。但是,正如前一篇SO帖子的评论中提到的,我不确定在调试会话期间是否有一种方法在python和C/C++代码之间流动。如果试图在python文件上放置断点,我也会“在创建断点时得到一个未填充的圆圈,而不是一个红点”。

在macOS 10.14.5中使用LLDB实现这一点时,我遇到了一些问题。 对我有效的方法是使用以下配置为VS代码安装“vadimcn.vscode lldb”扩展:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "attach",
            "name": "Attach pid",
            "pid": "${command:pickProcess}", // use ${command:pickProcess} to pick other users' processes,
        },
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "stopOnEntry": true,
            "env": {
                "PYTHONPATH": "${workspaceFolder}/build"
            }
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "LLDB Python",
            "program": "/usr/local/bin/python3",
            "args": [
                "${file}"
            ],
            "cwd": "${workspaceFolder}",
            "stopOnEntry": false,
            "env": {
                "PYTHONPATH": "${workspaceFolder}/build"
            },
        },
    ]
}
使用lldb启动python(
lldb-python
),或者我首先启动一个python调试会话(
python:Current File
),然后我可以将其附加到(
attach-pid
)。第二个选项的好处是能够在python和Java中设置断点