【发布时间】:2021-05-10 15:06:39
【问题描述】:
问题总结
我正在为 Python 开发一个自我 C 扩展,以提高特定代码段的性能。我想调试这个扩展,但到目前为止我还没有成功。我关注了几个链接,例如this from Nadiah 或this from Bark,但我总是遇到同样的问题:我无法在 C 代码的任何断点处停止。
我尝试过的方法
这个想法是将 Python 作为主进程运行,并将编译后的 C 代码附加到这个主进程。下面我留下一个minimal reproducible example:
Python 文件
import os
import greet
pid = os.getpid()
test=2.2
greet.greet('World')
print("hi")
如您所见,我什至检索了进程ID,以便在附加C代码时在vscode中选择该ID,如下所示:
C 代码
#include <Python.h>
static PyObject *
greet_name(PyObject *self, PyObject *args)
{
const char *name;
if (!PyArg_ParseTuple(args, "s", &name))
{
return NULL;
}
printf("Helllo %s!\n", name);
Py_RETURN_NONE;
}
static PyMethodDef GreetMethods[] = {
{"greet", greet_name, METH_VARARGS, "Greet an entity."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef greet =
{
PyModuleDef_HEAD_INIT,
"greet", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
GreetMethods
};
PyMODINIT_FUNC PyInit_greet(void)
{
return PyModule_Create(&greet);
}
我通过运行python setup.py install 使用 GCC 8.1 编译 C 代码:
设置文件
import os
from setuptools import setup, Extension
os.environ["CC"] = "g++-8.1.0"
_DEBUG = True
_DEBUG_LEVEL = 0
# extra_compile_args = sysconfig.get_config_var('CFLAGS').split()
extra_compile_args = ["-Wall", "-Wextra"]
if _DEBUG:
extra_compile_args += ["-g3", "-O0", "-DDEBUG=%s" % _DEBUG_LEVEL, "-UNDEBUG"]
else:
extra_compile_args += ["-DNDEBUG", "-O3"]
setup(
name='greet',
version='1.0',
description='Python Package with Hello World C Extension',
ext_modules=[
Extension(
'greet',
sources=['greetmodule.c'],
py_limited_api=True,
extra_compile_args=extra_compile_args)
],
)
我什至指定O0 选项来包含所有调试符号。
启动 JSON 文件
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "venv/Scripts/python",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
// "miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
我遵循的调试步骤
- 在python文件中添加断点,运行启动配置“Python: Current File”并等待到达断点。
- 运行“(gdb) Attach”启动配置,选择路径包含“/.vscode/”的python解释器。在这种情况下,在 Windows 中,不会像在 Linux 中那样提示我输入用户密码。
- 在 C++ 文件中设置断点
- python 调试器当前在断点处停止。从调试器“(gdb) Attach”切换回另一个调试器“Python: Current File”,然后按 F5(继续)。
在这最后一步中,vscode 应该自动在 python 和 c++ 代码之间的两个调试器之间跳转,但我无法实现这种行为。
我可以单独调试 Python 和 C 程序,但不能一起调试。
【问题讨论】:
-
你在这个链接中遇到什么问题 [nadiah.org/2020/03/01/…
-
嗨@Divyessh,您传递的链接似乎已损坏...我想您指的是我在帖子中提供的第一个链接。一切都很好,直到最后一步(第 5 步)。不同之处在于,在 Windows 中,系统不会提示我输入超级用户密码。然后,即使 (gdb) 附加任务正在运行,我也无法在 C 代码中设置的任何断点处停止。该链接无论如何都是基于 Linux 的,因此在 Windows 中调试时可能会有所不同。
-
我遇到了类似的问题,我也尝试了您尝试过的大多数解决方案。您是否尝试过解决 github 上的 vscode 问题?
-
嗨@axel_ande。是的,我两周前在 VsCode C++ 扩展 (github.com/microsoft/vscode-cpptools/issues/6974) 的 github 中打开了一个问题。看看有没有人解决。
标签: python c++ c debugging visual-studio-code