bdb --- 偵錯器框架

原始碼:Lib/bdb.py


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

有定義以下例外:

exception bdb.BdbQuit

Bdb 類別所引發的例外,用來退出偵錯器。

bdb 模組也定義了兩個類別:

class bdb.Breakpoint(self, file, line, temporary=False, 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 bpbynumber and by (file, line) pairs through bplist. The former points to a single instance of class Breakpoint. The latter points to a list of such instances since there may be more than one breakpoint per line.

When creating a breakpoint, its associated file name should be in canonical form. If a funcname is defined, a breakpoint hit will be counted when the first line of that function is executed. A conditional breakpoint always counts a hit.

Breakpoint 實例有以下方法:

deleteMe()

Delete the breakpoint from the list associated to a file/line. If it is the last breakpoint in that position, it also deletes the entry for the file/line.

enable()

Mark the breakpoint as enabled.

disable()

Mark the breakpoint as disabled.

bpformat()

Return a string with all the information about the breakpoint, nicely formatted:

  • Breakpoint number.

  • Temporary status (del or keep).

  • File/line position.

  • Break condition.

  • Number of times to ignore.

  • Number of times hit.

在 3.2 版被加入.

bpprint(out=None)

Print the output of bpformat() to the file out, or if it is None, to standard output.

Breakpoint 實例有以下屬性:

file

Breakpoint 的檔案名稱。

line

Line number of the Breakpoint within file.

temporary

True if a Breakpoint at (file, line) is temporary.

cond

Condition for evaluating a Breakpoint at (file, line).

funcname

Function name that defines whether a Breakpoint is hit upon entering the function.

enabled

Breakpoint 有被啟用則為 True

bpbynumber

Numeric index for a single instance of a Breakpoint.

bplist

Dictionary of Breakpoint instances indexed by (file, line) tuples.

ignore

Number of times to ignore a Breakpoint.

hits

Count of the number of times a Breakpoint has been hit.

class bdb.Bdb(skip=None, backend='settrace')

The Bdb class acts as a generic Python debugger base class.

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

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

The backend argument specifies the backend to use for Bdb. It can be either 'settrace' or 'monitoring'. 'settrace' uses sys.settrace() which has the best backward compatibility. The 'monitoring' backend uses the new sys.monitoring that was introduced in Python 3.12, which can be much more efficient because it can disable unused events. We are trying to keep the exact interfaces for both backends, but there are some differences. The debugger developers are encouraged to use the 'monitoring' backend to achieve better performance.

在 3.1 版的變更: 新增 skip 引數。

在 3.14 版的變更: 新增 backend 參數。

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

canonic(filename)

Return canonical form of filename.

For real file names, the canonical form is an operating-system-dependent, case-normalized absolute path. A filename with angle brackets, such as "<stdin>" generated in interactive mode, is returned unchanged.

start_trace(self)

Start tracing. For 'settrace' backend, this method is equivalent to sys.settrace(self.trace_dispatch)

在 3.14 版被加入.

stop_trace(self)

Stop tracing. For 'settrace' backend, this method is equivalent to sys.settrace(None)

在 3.14 版被加入.

reset()

Set the botframe, stopframe, returnframe and quitting attributes with values ready to start debugging.

trace_dispatch(frame, event, arg)

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

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

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

  • "call": A function is about to be called, or another code block entered.

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

  • "exception": An exception has occurred.

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

  • "c_return": A C function has returned.

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

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

The arg parameter depends on the previous event.

See the documentation for sys.settrace() for more information on the trace function. For more information on code and frame objects, refer to 標準型別階層.

dispatch_line(frame)

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

dispatch_call(frame, arg)

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

dispatch_return(