每当我尝试使用GDB在Ubuntu18.04上调试python的c/c++库时,我都会得到一个错误:/build/glibc-2ORdQG/glibc-2.27/sysdeps/unix/sysv/linux/select.c无法打开
我怀疑这与使用GDB加载共享库有关,但我不确定。
如果我将glibc-sourcecode代码复制到该位置,我会得到一个异常,如下所示:libc.so.6!__GI___select(int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout) (/build/glibc-2ORdQG/glibc-2.27/sysdeps/unix/sysv/linux/select.c:41) [Unknown/Just-In-Time compiled code] (Unknown Source:0)
我正在尝试从here中松散地复制教程。
我之前做过的步骤:
创建一个要在其中工作的文件夹"/home/jhm/Documents/pythonClibToyExample“。
使用以下代码创建一个文件"myadd.cpp“:
#include <Python.h>
static PyObject *method_myadd(PyObject *self, PyObject *args){
int x, y, z = -1;
/* Parse arguments */
if(!PyArg_ParseTuple(args, "ii", &x, &y)){
return NULL;
}
/* The actual bit of code I need */
z = x + y;
return PyLong_FromLong(z);
}
static PyMethodDef myaddMethods[] = {
{"myadd", method_myadd, METH_VARARGS, "Python interface for myadd C library function"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef myaddmodule = {
PyModuleDef_HEAD_INIT,
"myadd",
"Python interface for the myadd C library function",
-1,
myaddMethods
};
PyMODINIT_FUNC PyInit_myadd(void) {
return PyModule_Create(&myaddmodule);
}使用以下代码创建一个"setup.py“
from distutils.core import setup, Extension
def main():
setup(name="myadd",
version="1.0.0",
description="Python interface for the myadd C library function",
author="Nadiah",
author_email="nadiah@nadiah.org",
ext_modules=[Extension("myadd", ["myadd.cpp"])],
)
if __name__ == "__main__":
main()在终端中运行python3 setup.py build
我想创建一个本地python包来导入,所以我创建了一个文件夹" myadd“,并将./build/lib.linux-x86_64-3.6/目录中的"myadd.cpython-36m-x86_64-linux-gnu.so”复制到myadd文件夹中
在myadd文件夹中创建一个内部带有from .myadd import *的"__init__.py“。
在myadd文件夹中创建一个包含以下代码的“myadd.py
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__, 'myadd.cpython-36m-x86_64-linux-gnu.so')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()并用代码创建一个"main.py“
import glob
import os
import sys
try:
sys.path.append('/home/jhm/Documents/pythonClibToyExample')
except IndexError:
pass
import myadd
print(os.getpid())
x = myadd.myadd(5,6)
print(x)我将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": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/home/jhm/Documents/pythonClibToyExample/build/lib.linux-x86_64-3.6/myadd.cpython-36m-x86_64-linux-gnu.so",
"processId": "${command:pickProcess}",
"MIMode": "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",
"justMyCode": false
}
]
}在执行myadd中的任何内容并希望将gdb附加到打印的pid之前,在main.py中设置断点。但是,一旦我输入用于附加gdb的sudo pw,调试器就会结束,但会出现上述异常。
我在不同的计算机上进行了尝试,在终端中使用sudo gdb '/home/jhm/Documents/pythonClibToyExample/build/lib.linux-x86_64-3.6/myadd.cpython-36m-x86_64-linux-gnu.so'和attach &printed pid也进行了尝试-但出现了相同的错误(使用/build/ glibc -2ORdQG/glibc-2.27/中的glibc源码)。
Attaching to program: /home/jhm/Documents/pythonClibToyExample/build/lib.linux-x86_64-3.6/myadd.cpython-36m-x86_64-linux-gnu.so, process 7721
[New LWP 7728]
[New LWP 7730]
[New LWP 7732]
[New LWP 7733]
[New LWP 7734]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007fc5e51f313f in __GI___select (nfds=0, readfds=0x0, writefds=0x0,
exceptfds=0x0, timeout=0x7fff284935b0)
at ../sysdeps/unix/sysv/linux/select.c:41
41 return SYSCALL_CANCEL (select, nfds, readfds, writefds, exceptfds,我在这里做错了什么?我是否必须指定gdb应该在何处查找glib-c (作为根目录/build/...显然是错误的)?因为运行程序没有问题,所以在编译调试时,我是否遗漏了一些关键步骤?我真的只想调试我编译的cpp代码,而不是任何与glibc相关的东西。
发布于 2021-01-29 23:12:43
好了,我解决了。我有一个错误的启动配置...要调试的程序是我的python解释器,而不是.so对象。它甚至在教程中...但是我想我把config和调试一个独立的c程序搞混了。-_-
https://stackoverflow.com/questions/65940866
复制相似问题