每当我尝试在Ubuntu18.04上用GDB调试python的c/c++库时,我都会遇到一个错误:
/build/glibc-2ORdQG/glibc-2.27/sysdeps/unix/sysv/linux/select.c
无法打开
我怀疑这与用GDB加载共享库有关,但我不确定
如果我将glibc源代码复制到该位置,会出现如下异常:
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文件夹中,创建一个“uuu init_uuuuu.py”,其中包含from .myadd import *
在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中的任何内容之前,在main.py中设置一个断点,并希望将gdb附加到打印的pid。然而,当我输入用于附加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
进行了尝试,但出现了相同的错误(glibc源代码位于/build/glibc-2oordqg/glibc-2.27/)
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(因为root/build/…显然是错误的)?因为运行程序没有问题,所以在编译调试时我是否错过了一些关键步骤?我真的只想调试我编译的cpp代码,而不是任何与glibc相关的东西
好的,我解决了。我有一个错误的启动配置。。。要调试的程序是我的python解释器,而不是.so对象。。。。它甚至在教程中。。。但是我想我把配置和调试一个独立的c程序搞混了
相关问题 更多 >
编程相关推荐