spenceforce / pdb-attach

A python debugger that can attach to running processes.
BSD 3-Clause "New" or "Revised" License
30 stars 1 forks source link

Defensive coding suggestion #11

Closed jwmonaco closed 3 years ago

jwmonaco commented 3 years ago

I didn't study if this is really necessary in general, but in my use case pdb_attach raises an exception and exited when the application being debugged logged an info message. The issue was "to_server" was empty. Perhaps this points to a deeper issue, but this change was sufficient to make my use case work.

Thanks for contributing this! I don't understand why this is not a build in capability.

spenceforce commented 3 years ago

Hi @jwmonaco, thanks for the pull request! It would great if you could give a little more information and a reproducible example. Are you trying to log a message using pythons logging module? Or are you just entering an empty string at the pdb prompt?

jwmonaco commented 3 years ago

Sure, here's the way I can reproduce the issue. I've attached a trivial test script to debug.

test_logging.py.txt

Once that script is running, I try to attach and then step thru execution. Eventually I get an exception (usually on 3rd log or so, but it is not consistent).

[jmonaco@work pdb-attach (main)]$ python3 -m pdb_attach 22776 50000
> /usr/lib/python3.6/selectors.py(448)select()
-> for fd, event in fd_event_list:
(Pdb) b test_logging.py:12
Breakpoint 1 at /home/jmonaco/src/pdb-attach/test_logging.py:12
(Pdb) c
> /home/jmonaco/src/pdb-attach/test_logging.py(12)do_stuff()
-> log.warn('good')
(Pdb) n
> /home/jmonaco/src/pdb-attach/test_logging.py(13)do_stuff()
-> for idx in range(10):
(Pdb) n
> /home/jmonaco/src/pdb-attach/test_logging.py(14)do_stuff()
-> log.warn(f'good{idx}')
(Pdb) n
> /home/jmonaco/src/pdb-attach/test_logging.py(13)do_stuff()
-> for idx in range(10):
(Pdb) n
> /home/jmonaco/src/pdb-attach/test_logging.py(14)do_stuff()
-> log.warn(f'good{idx}')
(Pdb) 
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/jmonaco/src/pdb-attach/pdb_attach/__main__.py", line 54, in <module>
    if to_server[-1] != "\n":
IndexError: string index out of range
spenceforce commented 3 years ago

Ok, I see the problem. This is definitely a bug in pdb-attach, but ignoring an empty string would cause a different problem since empty strings are a valid input to pdb.

Instead of ignoring the empty string, it should add a newline to the empty string. Basically just combine the check for an empty string and for a missing newline character at the end into one and add a newline if either fails.

A test case for an empty string would be useful in the end to end tests. If you're not interested in adding a test case and changing your solution, I can fix the bug.