简体   繁体   中英

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.

(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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM