bdb — 调试器框架

源代码: Lib/bdb.py


The bdb 模块处理基本调试器功能,像:设置断点 (或凭借调试器管理执行)。

定义了以下异常:

exception bdb. BdbQuit

异常被引发通过 Bdb 类为离开调试器。

The bdb 模块还定义了 2 个类:

class bdb. Breakpoint ( self , file , line , temporary=0 , cond=None , funcname=None )

此类实现临时断点、忽略计数、禁用和 (重新) 启用及条件。

是通过数字索引断点透过列表称为 bpbynumber 和通过 (file, line) 对透过 bplist 。前者指向单实例化的类 Breakpoint 。后者指向这种实例的列表,由于每行可能有多个断点。

When creating a breakpoint, its associated filename 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 ( )

从文件/行关联列表删除断点。若它是该位置的最后一个断点,还会删除文件/行条目。

enable ( )

将断点标记为启用。

disable ( )

将断点标记为禁用。

bpformat ( )

返回具有断点所有有关信息的字符串,好的格式:

  • 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 )

打印输出的 bpformat() 到文件 out ,或若它为 None ,到标准输出。

class bdb. Bdb ( skip=None )

The Bdb 类充当一般 Python 调试器基类。

此类负责跟踪设施细节;派生类应实现用户交互。标准调试器类 ( pdb.Pdb ) 是范例。

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.

3.1 版新增: The skip 自变量。

下列方法对于 Bdb 通常不需要覆写。

canonic ( filename )

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

reset ( )

设置 botframe , stopframe , returnframe and quitting 属性采用准备开始调试的值。

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 参数从属先前事件。

见文档编制为 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() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 Bdb.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() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 Bdb.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 ( frame , arg )

若调试器应在此函数返回时停止,援引 user_return() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 Bdb.quitting flag is set (which can be set from user_return() ). Return a reference to the trace_dispatch() method for further tracing in that scope.

dispatch_exception ( frame , arg )

若调试器应在此异常处停止,援引 user_exception() 方法 (应在子类中覆写)。引发 BdbQuit 异常若 Bdb.quitting flag is set (which can be set from user_exception() ). Return a reference to the trace_dispatch() method for further tracing in that scope.

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

stop_here ( frame )

This method checks if the frame is somewhere below botframe in the call stack. botframe is the frame in which debugging started.

break_here ( frame )

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

break_anywhere ( frame )

This method checks if there is a breakpoint in the filename of the current frame.

派生类应该覆写这些方法,以获得对调试器操作的控制。

user_call ( frame , argument_list )

This method is called from dispatch_call() when there is the possibility that a break might be necessary anywhere inside the called function.

user_line ( frame )

This method is called from dispatch_line() stop_here() or break_here() 产生 True .

user_return ( frame , return_value )

This method is called from dispatch_return() stop_here() 产生 True .

user_exception ( frame , exc_info )

This method is called from dispatch_exception() stop_here() 产生 True .

do_clear ( arg )

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

此方法必须由派生类实现。

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

set_step ( )

停止在 1 行代码后。

set_next ( frame )

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

set_return ( frame )

Stop when returning from the given frame.

set_until ( frame )

Stop when the line with the line no greater than the current one is reached or when returning from current frame.

set_trace ( [ frame ] )

Start debugging from frame 。若 frame is not specified, debugging starts 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 ( )

设置 quitting 属性为 True 。这引发 BdbQuit in the next call to one of the dispatch_*() 方法。

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

set_break ( filename , lineno , temporary=0 , cond , funcname )

设置新断点。若 lineno line doesn’t exist for the filename passed as argument, return an error message. The filename should be in canonical form, as described in the canonic() 方法。

clear_break ( filename , lineno )

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

clear_bpbynumber ( arg )

Delete the breakpoint which has the index arg Breakpoint.bpbynumber 。若 arg is not numeric or out of range, return an error message.

clear_all_file_breaks ( filename )

删除所有断点在 filename . If none were set, an error message is returned.

clear_all_breaks ( )

Delete all existing breakpoints.

get_bpbynumber ( 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, if the given breakpoint never existed or has been deleted, a ValueError 被引发。

3.2 版新增。

get_break ( filename , lineno )

Check if there is a breakpoint for lineno of filename .

get_breaks ( filename , lineno )

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

get_file_breaks ( filename )

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

get_all_breaks ( )

返回设置的所有断点。

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

get_stack ( f , t )

Get a list of records for a frame and all higher (calling) and lower frames, and the size of the higher part.

format_stack_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 following two methods can be called by clients to use a debugger to debug a 语句 , given as a string.

run ( cmd , globals=None , locals=None )

调试执行语句凭借 exec() 函数。 globals 默认为 __main__.__dict__ , locals 默认为 globals .

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

Debug an expression executed via the eval() 函数。 globals and locals 拥有相同含义如在 run() .

runctx ( cmd , globals , locals )

为向后兼容。调用 run() 方法。

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 one in the frame also passed as argument. If the breakpoint was set via function name, we have to check we are in the right frame (the right function) and if we 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 ok to delete a temporary breakpoint. Return (None, None) if there is no matching breakpoint.

bdb. set_trace ( )

Start debugging with a Bdb instance from caller’s frame.

上一话题

审计事件表

下一话题

faulthandler — 转储 Python 回溯

本页