Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDB: How to inspect local variables of functions in nested stack frames?

Context:

I'm running some python code thru PDB (Python debugger). When I set and subsequently hit a breakpoint, I can inspect the local variables using:

(Pdb) locals()

This prints out a nice dict of name, value pairs of the local variables in the current scope in which I'm paused. Perfect!

I can also see a stack trace using the PDB where command which results in something like this:

  /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py(400)run()
-> exec cmd in globals, locals
  <string>(1)<module>()
  .../main.py(116)<module>()
-> run()
  .../main.py(104)run()
-> res = quicksort(res)
> .../main.py(68)quicksort()
-> if len(v) <= 1:

In this example output, I'm paused in the quicksort() function which was called by the run() function.

So far, so good.

Question:

If I can inspect the quicksort() function's local variables with a call to locals(), how can I similarly inspect the local variables of the run() function?

In other words, how can I inspect the local variables of a function which is nested in the call stack?

Important clarification: I DON'T want to continue or step into run() to inspect its local variables. I want to inspect (from my current, paused perspective) the local variables in the run() stack frame currently nested in the call stack.

like image 605
Todd Ditchendorf Avatar asked May 06 '14 04:05

Todd Ditchendorf


1 Answers

(i)pdb offer commands up and down, allowing you to travel via call stack, this way you can visit higher levels of your call and inspect local variables there.

You can jump up or down the call stack using u(p) or d(own). You can also specify a number of frames to by giving it as an argument to u(p) or d(own).

like image 117
Jan Vlcinsky Avatar answered Nov 10 '22 10:11

Jan Vlcinsky