Search code examples
pythonlinuxgdbpython-c-api

Executable under GDB invokes different embedded Python


I have code that embeds Python. That embedded Python uses NumPy and hence, I need to explicitly load libpython, to make NumPy work.

The driving code is in C++ (tests in Google Test). There is a bug somewhere, and I try to use gdb for debugging. However, something strange happens as embedded versions are different, when I simply run the executable and when I run the executable under gdb.

I find the path to libpython by instantiating sysconfig module under the initialized embedded Python and then using sysconfig.get_config_var("LIBDIR").

I log the found path to the libpython hen I simply run the executable:

Path to libpython is /home/dima/.conda/envs/um02-open-interfaces/lib

When I run the same executable under gdb:

Path to libpython is /home/linuxbrew/.linuxbrew/opt/[email protected]/lib

How to stop gdb from changing environment?


Solution

  • How to stop gdb from changing environment?

    GDB doesn't change the environment on its own. However, it does invoke your $SHELL, and if you have ~/.bashrc or similar file changing the environment, than these changes will affect any process invoked by the GDB run command.

    You can confirm whether that is happening by running $SHELL -c /path/to/your/program. If it prints the wrong path to libpython, this is likely the root cause.

    The fix is to make .bashrc change the environment only for interactive shells.

    You could also disable the intermediate $SHELL using GDB set startup-with-shell off (documentation).