Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to top frame in pdb (python debugger)

When pdb / ipdb is used in post-mortem mode, it drops you into an interactive shell where you can explore the environment at the stack frame of the most deeply nested code. I'm usually not interested in the the situation several levels down into some library -- I'm interested in my function because that's what really caused the problem. So, most of the time I have to press "u" several times to get back up to the level of the code I wrote.

Is there a way I can jump to the "top" to speed this up? Or even better, a shortcut to go straight to a particular stack frame?

(By the way, the stack feels a little "upside down" to me here. A function that calls another function puts the new call on the top of the stack, right? So I feel like the pdb u(p) command is actually moving you down into the stack...)

like image 334
foobarbecue Avatar asked Jul 27 '16 17:07

foobarbecue


People also ask

How do I use pdb debugger in Python?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do you go one step back in pdb?

The docs say: j(ump) lineno Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don't want to run.

What is pdb Set_trace ()?

pdb. set_trace (*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.

How do you get out of a loop in pdb?

Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.


2 Answers

You could do "up n" with an arbitrarily high value for n, like 99: https://docs.python.org/3/library/pdb.html#pdbcommand-up

PS. It was mentioned by Chris in a comment on the OP. I do re-post it as an answer to make it more visible, and with a link to the documentation.

like image 55
xrr Avatar answered Sep 21 '22 23:09

xrr


You can use a PdbExtension for this.

See https://github.com/fschulze/pytest-pdb/pull/5 where this is about to be added to pytest-pdb.

like image 30
blueyed Avatar answered Sep 21 '22 23:09

blueyed