gdb python in pyenv virtualenv
Asked Answered
L

1

8

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://mcmap.net/q/182044/-python-tracing-a-segmentation-fault to debug my C extension's segfault?

https://mcmap.net/q/1305817/-cannot-start-dbg-on-my-python-c-extension 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
Lonne answered 26/3, 2021 at 21:7 Comment(1)
You might find the solution to your problem here: #22932274Photogrammetry
C
6

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
Chetnik answered 23/8, 2023 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.