Whenever i try to debug a c/c++ library for python on Ubuntu 18.04 with GDB, i get an error that:
/build/glibc-2ORdQG/glibc-2.27/sysdeps/unix/sysv/linux/select.c
could not be opened
I suspect it has something to to with loading shared libraries with GDB, but i am not sure.
If i copy the glibc-sourcecode to that position i get an exception like this:
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)
I am trying to loosely reproduce the tutorial from here.
Steps i did before:
create a folder "/home/jhm/Documents/pythonClibToyExample" to work in.
create a file "myadd.cpp" with the code:
#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);
}
create a "setup.py" with the code
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="[email protected]",
ext_modules=[Extension("myadd", ["myadd.cpp"])],
)
if __name__ == "__main__":
main()
run python3 setup.py build
in a terminal
I want to create a local python package to import, so i create a folder "myadd" and copy the "myadd.cpython-36m-x86_64-linux-gnu.so" from the ./build/lib.linux-x86_64-3.6/ directory into the myadd folder
in the myadd folder create a "__init__.py" with from .myadd import *
inside.
in the myadd folder create a "myadd.py" with code
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__()
and create a "main.py" with code
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)
I used Visual Studio Code with following 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
}
]
}
set a breakpoint in the main.py before executing anything in the myadd and wanted attached gdb to the printed pid. However, as soon as i entered my sudo pw for attaching the gdb debugger ends with the above exceptions.
I tried this on different computers and also tried it using sudo gdb '/home/jhm/Documents/pythonClibToyExample/build/lib.linux-x86_64-3.6/myadd.cpython-36m-x86_64-linux-gnu.so'
and then attach &printed pid
in the terminal - but same error(With the glibc sourcecode at /build/glibc-2ORdQG/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,
What am I doing wrong here? Do i have to specify somewhere where gdb should look for the glib-c (as the root /build/... is clearly wrong)? Did i miss some critical step while compiling for debugging since running the program is no problem? I really just want to debug my compiled cpp code, not anything related to glibc.
Ok, i solved it. I had a wrong launch configuration... the program to debug is my python interpreter not the .so object.... Its even in the tutorial... but i guess i confused the config with debugging a standalone c program. -_-