2

When using pdb to debug a curses application, the interactive debugger is useless, since curses messes up the terminal screen. Debugging post mortem works though, but that is a bit limited.

So what we probably need is having the debugger work in a terminal separately from the debuggee (the application that is being debugged). Some alternatives which apply remote debugging (such as xpdb) appear either not to work with python 3.3 or give weird errors for other reasons.

So how can I use pdb in a different terminal, or in another proper way?

chtenb
  • 14,924
  • 14
  • 78
  • 116
  • Relatex: [How to effectively debug a multi-threaded curses client-server application with gdb?](http://stackoverflow.com/questions/16304389/how-to-effectively-debug-a-multi-threaded-curses-client-server-application-with). The principles should be the same. Also you could use the `pdb` module instead and instantiate a [`Pdb`](http://docs.python.org/2/library/pdb.html#pdb.Pdb) with different `stdin`/`stdout` files. – Bakuriu Dec 24 '13 at 12:55
  • 1
    `pdb` appears not to have a `tty` command like `gdb` does. – chtenb Dec 24 '13 at 13:05
  • Changing stdin/stdout to a separate terminal seems to work for `set_trace()`. However, several things don't work properly, such as completion, command history, etc. – chtenb Dec 24 '13 at 14:05
  • Probably this is a missing feature. You should try to open a ticket on python's issues tracker, and maybe also in [`pdb-clone`](http://code.google.com/p/pdb-clone/) issues tracker. – Bakuriu Dec 24 '13 at 14:13
  • http://bugs.python.org/issue20061 – chtenb Dec 24 '13 at 15:10

2 Answers2

0

Use some debugger's functionalities for attach to a running process. For instance you can try:

gdb python <pid>

See how here Python Wiki DebuggingWithGdb.

being the pid of the process you want to debug. Also there is WinPdb that allows you to connect to a remote or local process. WinPdb is well documented and I think is your best option.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • For me winpdb doesn't work for some reason. When I run `python3.3 /usr/local/lib/python3.3/dist-packages/rpdb2.py --debuggee script.py` it gives an error: File "/usr/local/lib/python3.3/dist-packages/rpdb2.py", line 13682, in __getsignal handler = g_signal_handlers.get(signum, g_signal_getsignal(signum)) ValueError: signal number out of range – chtenb Dec 24 '13 at 16:08
  • http://stackoverflow.com/questions/20764082/cant-get-winpdb-working-with-python-3-3 – chtenb Dec 24 '13 at 16:39
0

I've found that this bit of advice from the Python documentation helps:

A common problem when debugging a curses application is to get your terminal messed up when the application dies without restoring the terminal to its previous state. In Python this commonly happens when your code is buggy and raises an uncaught exception. Keys are no longer echoed to the screen when you type them, for example, which makes using the shell difficult. In Python you can avoid these complications and make debugging much easier by importing the module curses.wrapper. It supplies a wrapper() function that takes a callable. It does the initializations described above, and also initializes colors if color support is present. It then runs your provided callable and finally deinitializes appropriately. The callable is called inside a try-catch clause which catches exceptions, performs curses deinitialization, and then passes the exception upwards. Thus, your terminal won’t be left in a funny state on exception.

Please see here for info.

thunderblaster
  • 918
  • 11
  • 27
ChrisF
  • 47
  • 3
  • 11
  • 1
    I already do that - you should always do that when writing a curses application. The usage of `curses.wrapper` is also the reason that post mortem debugging works as pointed out in the OP. However, debugging a running curses application on-the-fly, by using `set_trace` for instance, doesn't work, since the curses application has not been shut down. – chtenb Mar 07 '14 at 22:23
  • 1
    `curses.wrapper()` is for recovering from a crash with the window settings restored. OP wants to use the pdb debugger with a curses app. – Edward Falk Dec 16 '22 at 06:39