6

I am working inside a pyenv-managed virtualenv

$ which python
/Users/theonlygusti/.pyenv/shims/python

/Users/theonlygusti/.pyenv/shims/python is a shell script and gdb doesn't work

"0x7ffeeb614570s": not in executable format: file format not recognized

How can I use gdb on a python script like here https://stackoverflow.com/a/2664232/3310334 to debug my C extension's segfault?

https://stackoverflow.com/a/53007303/3310334 suggests gdb -ex r --args bash python crash.py, but it doesn't work, same error

"0x7ffee0aa4530s": not in executable format: file format not recognized
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • You might find the solution to your problem here: https://stackoverflow.com/questions/22931774/how-to-use-gdb-python-debugging-extension-inside-virtualenv?rq=4 – Fichko Aug 23 '23 at 10:44

1 Answers1

5

My sense is that your problem is that you are invoking the pyenv shim file, not the python executable with gdb. gdb must be provided with the python executable file path, not the shim script file. It needs the python executable to load the symbols, etc.

Example:

> pyenv virtualenv 3.10 66824320
> pyenv local 66824320
> which python
> /Users/$USER/.pyenv/shims/python
>
> cat `which python`                                                                                                   #!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x

program="${0##*/}"

export PYENV_ROOT="/Users/$USER/.pyenv"
exec "/usr/local/opt/pyenv/bin/pyenv" exec "$program" "$@"

what you need to open is:

> pyenv which python                                                                                                   
/Users/$USER/.pyenv/versions/66824320/bin/python

to run gdb with the real python binary you need to pass the python executable file to gdb:

> gdb `pyenv which python`
GNU gdb (GDB) 13.2
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin22.4.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /Users/$USER/.pyenv/versions/66824320/bin/python...

I hope the above helps point you in the right direction.

Thus:

> gdb -ex r --args `pyenv which python` crash.py
Technophobe01
  • 8,212
  • 3
  • 32
  • 59