| |
Deep Debugging
Submitted by: cduncan
Last Edited: 2004-10-14
Category: Product Development
|
| Average rating is:
0.0 out of 5
|
(0 ratings) |
|
Description:
Sometimes debugging exceptions raised deep in the bowels of Zope or any other framework can be
frustrating. Setting breakpoints is a hit-or-miss affair and especially difficult to do if the
exception occurs within code executed many times during the same request. This can be even worse in
recursive code.
What you really want is to be able to drop into the debugger at the exact place the error occurs, even
though you might not even know exactly where (or when) that will be. Then you can navigate the stack
frame to figure out exactly what is going on at the time of the error. Now this is already possible if
you envoke zope from the python command line and use the debug method with the "pm" argument. However
this can be tedious and must be done manually and I sometimes find that the environment is different
enough or the error is not reliably reproducable and an alternative is needed.
Here is some code that you can use to envoke pdb, the built-in Python command-line debugger at
precisely the point where any exception is raised in anything in the try clause of the try: except:
statmement.
|
Source (Text):
try:
..Call the thing you wish to debug..
except:
import sys, pdb
t, v, tb = sys.exc_info()
pdb.post_mortem(tb)
|
Explanation:
In order for this to work properly, you need to run Zope in "Debug" mode (the -D option to z2.py) and
send the standard output to the controlling terminal. This is the default behavior of the start
script that ships with Zope.
Once an exception occurs, you will see the debugger prompt in your terminal. From there you can examine
the environment at the time of the error and move up and down through the stack frames to see
what is going on at each level involved. For info on how to use pdb,
see: http://www.python.org/doc/current/lib/module-pdb.html
Sometimes it is useful to leave this code in your product and make it conditional on some enviroment
variable you set. Then you can just set the variable, restart Zope and dive into the debugger.
|
Comments:
No Comments
|
|