Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb python in pyenv virtualenv

Tags:

c++

python

c

pyenv

gdb

I am working inside a pyenv-managed virtualenv

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

/Users/me/.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

1 Answers

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...

Thus:

> gdb -ex r --args `pyenv which python` crash.py
like image 157
Technophobe01 Avatar answered May 27 '26 08:05

Technophobe01



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!