google / pyringe

Debugger capable of attaching to and injecting code into python processes.
Other
1.63k stars 78 forks source link

Explicitly tell gdb the python executable to use #18

Closed silviot closed 10 years ago

silviot commented 10 years ago

Without this change I'm getting this on a system that has also python3 installed, when I try to attach() to a process:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../pyringe/repl.py", line 157, in Attach
    self.inferior.Reinit(pid)
  File ".../pyringe/inferior.py", line 464, in Reinit
    self.__init__(pid, auto_symfile_loading)
  File ".../pyringe/inferior.py", line 436, in __init__
    self.StartGdb()
  File ".../pyringe/inferior.py", line 483, in StartGdb
    self._gdb.Attach(self.position)
  File ".../pyringe/inferior.py", line 196, in <lambda>
    return lambda *args, **kwargs: self._Execute(name, *args, **kwargs)
  File ".../pyringe/inferior.py", line 323, in _Execute
    result_string = self._Recv(timeout)
  File ".../pyringe/inferior.py", line 408, in _Recv
    raise ProxyError(exc_text)
ProxyError: Traceback (most recent call last):
  File ".../pyringe/payload/gdb_service.py", line 34, in <module>
    import libpython
  File ".../pyringe/payload/libpython.py", line 58
    Py_TPFLAGS_HEAPTYPE = (1L << 9)
                            ^
SyntaxError: invalid syntax

Curiosly enough /usr/bin/env python gives a python 2.7, but still gdb would try to run py3k. This change does not make pyringe work for me: I still have a consistent TimeoutError. But IMO it should not hurt.

TehMillhouse commented 10 years ago

The main problem here is that gdb doesn't start python as a subprocess, but links against libpython*.so and embeds the interpreter. The --command flag doesn't allow you to choose your preferred python interpreter, so this commit doesn't help; in fact, since --command expects a file path for a file to execute (gdb will decide by itself what to do with that file), this breaks the debugger.

If you'll take a look at #16, you'll see you're not the first one to run into this problem (Ubuntu is the only distro I know of that links gdb against libpython3.*.so). You can try getting around this by attempting to use LD_PRELOAD to preload libpython2.7.so, but my own attempts to get this to work didn't prove fruitful (using Ubuntu packages on an Arch install, so YMMV).

silviot commented 10 years ago

Thanks for the explanation! I was missing quite a part of the picture.