3

I'd like to attach a Python debugger to a running process. Following this comment, I tried pdb-clone but have gotten confused. Here's the script I'm attaching to:

import os
import time
from pdb_clone import pdbhandler
pdbhandler.register()

def loop(my_pid):
    print("Entering loop")
    while True:
        x = 'frog'
        time.sleep(0.5)
    print("Out of Loop")

if __name__ == '__main__':
    my_pid = os.getpid()
    print("pid = ", my_pid)
    loop(my_pid)

If I run python3 target_code_1.py in one terminal and see PID = 95439, then in a second terminal try

sudo pdb-attach --kill --pid 95439

I get an error message (which I include below).

However, suppose I simultaneously run python3 target_code_1.py in a third terminal. I can now run sudo pdb-attach --kill --pid 95439 without error, but when I print my_pid, the value is 95440. On the other hand, if I run sudo pdb-attach --kill --pid 95440 and print my_pid, the value is 95439. (In other words, it looks like pdb-attach has swapped which thread it is attaching to.) This behavior appears to be repeatable. What is going on?

For the record, the initial error message is as follows:

sudo pdb-attach --kill --pid 95440
Traceback (most recent call last):
  File "/usr/local/bin/pdb-attach", line 4, in <module>
    attach.main()
  File "/usr/local/lib/python3.7/site-packages/pdb_clone/attach.py", line 646, in main
    attach(address)
  File "/usr/local/lib/python3.7/site-packages/pdb_clone/attach.py", line 596, in attach
    for count in asock.connect_retry(address, verbose):
  File "/usr/local/lib/python3.7/site-packages/pdb_clone/attach.py", line 115, in connect_retry
    self.connect(address)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncore.py", line 342, in connect
    raise OSError(err, errorcode[err])
OSError: [Errno 22] EINVAL

FWIW, I'm running on macOS Mojave 10.14.2, Python 3.7.0, Clang 9.1.0.

(If I am solving this problem the wrong way, e.g., if there is a better Python module to use that can attach to live process, I'd be happy to use it instead.)

Bill Bradley
  • 456
  • 4
  • 16
  • How are you developing? If you use the excellent and free PyCharm IDE, then you will have no debugger problems, plus a lot more features – Mawg says reinstate Monica Jan 17 '19 at 15:00
  • 1
    I'm trying to debug on a remote machine; I figured PyCharm would be difficult to operate non-locally. Would it help me, @Mawg ? (Also, using `pdb_clone` seemed lighter-weight than installing an IDE for the production environment, but again, I may have the wrong impression.) – Bill Bradley Jan 17 '19 at 20:00
  • 1
    Hrm, [this PyCharm page reads](https://www.jetbrains.com/help/pycharm/attaching-to-local-process.html): "Note that you cannot attach to a remote process." But since I'm not familiar with PyCharm, I may be taking the quote out of context. – Bill Bradley Jan 17 '19 at 20:29
  • I think the best you can hope for is to install Pycharm on the remote machine and VNC in to run it on target. Sorry I couldn't really help – Mawg says reinstate Monica Jan 17 '19 at 20:46

0 Answers0