bdb —- Debugger framework

Source code:Lib/bdb.py


The bdb module handles basic debugger functions, like setting breakpointsor managing execution via the debugger.

定义了以下异常:

  • exception bdb.BdbQuit
  • Exception raised by the Bdb class for quitting the debugger.

The bdb module also defines two classes:

  • class bdb.Breakpoint(self, file, line, temporary=0, cond=None, funcname=None)
  • This class implements temporary breakpoints, ignore counts, disabling and(re-)enabling, and conditionals.

Breakpoints are indexed by number through a list called bpbynumberand by (file, line) pairs through bplist. The former points to asingle instance of class Breakpoint. The latter points to a list ofsuch instances since there may be more than one breakpoint per line.

When creating a breakpoint, its associated filename should be in canonicalform. If a funcname is defined, a breakpoint hit will be counted when thefirst line of that function is executed. A conditional breakpoint alwayscounts a hit.

Breakpoint instances have the following methods:

  • deleteMe()
  • Delete the breakpoint from the list associated to a file/line. If it isthe last breakpoint in that position, it also deletes the entry for thefile/line.

  • enable()

  • Mark the breakpoint as enabled.

  • disable()

  • Mark the breakpoint as disabled.

  • bpformat()

  • Return a string with all the information about the breakpoint, nicelyformatted:

    • The breakpoint number.

    • If it is temporary or not.

    • Its file,line position.

    • The condition that causes a break.

    • If it must be ignored the next N times.

    • The breakpoint hit count.

3.2 新版功能.

  • bpprint(out=None)
  • Print the output of bpformat() to the file out, or if it isNone, to standard output.
  • class bdb.Bdb(skip=None)
  • The Bdb class acts as a generic Python debugger base class.

This class takes care of the details of the trace facility; a derived classshould implement user interaction. The standard debugger class(pdb.Pdb) is an example.

The skip argument, if given, must be an iterable of glob-stylemodule name patterns. The debugger will not step into frames thatoriginate in a module that matches one of these patterns. Whether aframe is considered to originate in a certain module is determinedby the name in the frame globals.

3.1 新版功能: The skip argument.

The following methods of Bdb normally don't need to be overridden.

  • canonic(filename)
  • Auxiliary method for getting a filename in a canonical form, that is, as acase-normalized (on case-insensitive filesystems) absolute path, strippedof surrounding angle brackets.

  • reset()

  • Set the botframe, stopframe, returnframe andquitting attributes with values ready to start debugging.

  • tracedispatch(_frame, event, arg)

  • This function is installed as the trace function of debugged frames. Itsreturn value is the new trace function (in most cases, that is, itself).

The default implementation decides how to dispatch a frame, depending onthe type of event (passed as a string) that is about to be executed.event can be one of the following:

  1. -

"line": A new line of code is going to be executed.

  1. -

"call": A function is about to be called, or another code blockentered.

  1. -

"return": A function or other code block is about to return.

  1. -

"exception": An exception has occurred.

  1. -

"c_call": A C function is about to be called.

  1. -

"c_return": A C function has returned.

  1. -

"c_exception": A C function has raised an exception.

For the Python events, specialized functions (see below) are called. Forthe C events, no action is taken.

The arg parameter depends on the previous event.

See the documentation for sys.settrace() for more information on thetrace function. For more information on code and frame objects, refer to标准类型层级结构.

  • dispatchline(_frame)
  • If the debugger should stop on the current line, invoke theuser_line() method (which should be overridden in subclasses).Raise a BdbQuit exception if the Bdb.quitting flag is set(which can be set from user_line()). Return a reference to thetrace_dispatch() method for further tracing in that scope.

  • dispatchcall(_frame, arg)

  • If the debugger should stop on this function call, invoke theuser_call() method (which should be overridden in subclasses).Raise a BdbQuit exception if the Bdb.quitting flag is set(which can be set from user_call()). Return a reference to thetrace_dispatch() method for further tracing in that scope.

  • dispatchreturn(_frame, arg)

  • If the debugger should stop on this function return, invoke theuser_return() method (which should be overridden in subclasses).Raise a BdbQuit exception if the Bdb.quitting flag is set(which can be set from user_return()). Return a reference to thetrace_dispatch() method for further tracing in that scope.

  • dispatchexception(_frame, arg)

  • If the debugger should stop at this exception, invokes theuser_exception() method (which should be overridden in subclasses).Raise a BdbQuit exception if the Bdb.quitting flag is set(which can be set from user_exception()). Return a reference to thetrace_dispatch() method for further tracing in that scope.

Normally derived classes don't override the following methods, but they mayif they want to redefine the definition of stopping and breakpoints.

  • stophere(_frame)
  • This method checks if the frame is somewhere below botframe inthe call stack. botframe is the frame in which debugging started.

  • breakhere(_frame)

  • This method checks if there is a breakpoint in the filename and linebelonging to frame or, at least, in the current function. If thebreakpoint is a temporary one, this method deletes it.

  • breakanywhere(_frame)

  • This method checks if there is a breakpoint in the filename of the currentframe.

Derived classes should override these methods to gain control over debuggeroperation.

  • usercall(_frame, argument_list)
  • This method is called from dispatch_call() when there is thepossibility that a break might be necessary anywhere inside the calledfunction.

  • userline(_frame)

  • This method is called from dispatch_line() when eitherstop_here() or break_here() yields True.

  • userreturn(_frame, return_value)

  • This method is called from dispatch_return() when stop_here()yields True.

  • userexception(_frame, exc_info)

  • This method is called from dispatch_exception() whenstop_here() yields True.

  • doclear(_arg)

  • Handle how a breakpoint must be removed when it is a temporary one.

This method must be implemented by derived classes.

Derived classes and clients can call the following methods to affect thestepping state.

  • set_step()
  • Stop after one line of code.

  • setnext(_frame)

  • Stop on the next line in or below the given frame.

  • setreturn(_frame)

  • Stop when returning from the given frame.

  • setuntil(_frame)

  • Stop when the line with the line no greater than the current one isreached or when returning from current frame.

  • settrace([_frame])

  • Start debugging from frame. If frame is not specified, debuggingstarts from caller's frame.

  • set_continue()

  • Stop only at breakpoints or when finished. If there are no breakpoints,set the system trace function to None.

  • set_quit()

  • Set the quitting attribute to True. This raises BdbQuit inthe next call to one of the dispatch_*() methods.

Derived classes and clients can call the following methods to manipulatebreakpoints. These methods return a string containing an error message ifsomething went wrong, or None if all is well.

  • setbreak(_filename, lineno, temporary=0, cond, funcname)
  • Set a new breakpoint. If the lineno line doesn't exist for thefilename passed as argument, return an error message. The _filename_should be in canonical form, as described in the canonic() method.

  • clearbreak(_filename, lineno)

  • Delete the breakpoints in filename and lineno. If none were set, anerror message is returned.

  • clearbpbynumber(_arg)

  • Delete the breakpoint which has the index arg in theBreakpoint.bpbynumber. If arg is not numeric or out of range,return an error message.

  • clearall_file_breaks(_filename)

  • Delete all breakpoints in filename. If none were set, an error messageis returned.

  • clear_all_breaks()

  • Delete all existing breakpoints.

  • getbpbynumber(_arg)

  • Return a breakpoint specified by the given number. If arg is a string,it will be converted to a number. If arg is a non-numeric string, ifthe given breakpoint never existed or has been deleted, aValueError is raised.

3.2 新版功能.

  • getbreak(_filename, lineno)
  • Check if there is a breakpoint for lineno of filename.

  • getbreaks(_filename, lineno)

  • Return all breakpoints for lineno in filename, or an empty list ifnone are set.

  • getfile_breaks(_filename)

  • Return all breakpoints in filename, or an empty list if none are set.

  • get_all_breaks()

  • Return all breakpoints that are set.

Derived classes and clients can call the following methods to get a datastructure representing a stack trace.

  • getstack(_f, t)
  • Get a list of records for a frame and all higher (calling) and lowerframes, and the size of the higher part.

  • formatstack_entry(_frame_lineno, lprefix=': ')

  • Return a string with information about a stack entry, identified by a(frame, lineno) tuple:

    • The canonical form of the filename which contains the frame.

    • The function name, or "<lambda>".

    • The input arguments.

    • The return value.

    • The line of code (if it exists).

The following two methods can be called by clients to use a debugger to debuga statement, given as a string.

  • run(cmd, globals=None, locals=None)
  • Debug a statement executed via the exec() function. globalsdefaults to main._dict, _locals defaults to globals.

  • runeval(expr, globals=None, locals=None)

  • Debug an expression executed via the eval() function. globals andlocals have the same meaning as in run().

  • runctx(cmd, globals, locals)

  • For backwards compatibility. Calls the run() method.

  • runcall(func, *args, **kwds)

  • Debug a single function call, and return its result.

Finally, the module defines the following functions:

  • bdb.checkfuncname(b, frame)
  • Check whether we should break here, depending on the way the breakpoint _b_was set.

If it was set via line number, it checks if b.line is the same as the onein the frame also passed as argument. If the breakpoint was set via functionname, we have to check we are in the right frame (the right function) and ifwe are in its first executable line.

  • bdb.effective(file, line, frame)
  • Determine if there is an effective (active) breakpoint at this line of code.Return a tuple of the breakpoint and a boolean that indicates if it is okto delete a temporary breakpoint. Return (None, None) if there is nomatching breakpoint.

  • bdb.set_trace()

  • Start debugging with a Bdb instance from caller's frame.