eric7.DebugClients.Python.debug_monitor

Module implementing a debugger / call tracer based on sys.monitoring.

Since Python 3.12 there is a new interface to implement debugger / call tracer with much better performance compared to sys.set_trace.

If raising ImportError, the old debugger will be used.

Global Attributes

Code
Frame
_recursion_limit
events
monitoring

Classes

DebugBase Class implementing base class of the debugger.

Functions

printerr Module function used for debugging the debug client.
setRecursionLimit Module function to set the recursion limit.


DebugBase

Class implementing base class of the debugger.

Provides methods for the 'owning' client to call to step etc.

Derived from

None

Class Attributes

code_has_breakpoints
filename_cache
files_to_skip
lib
paths_to_skip
pollTimerEnabled
profile_active
step_over_frames
step_single_threads
timer_thread
trace_active

Class Methods

None

Methods

DebugBase Constructor of DebugBase.
__disassemble Private method to generate a disassembly of the given code object.
__event_poll_timer Private method to check every second for new commands.
__extractSystemExitMessage Private method to get the SystemExit code and message.
__extract_stack Private member to return a list of stack frames.
__fix_frame_filename Private method used to fixup the filename for a given frame.
__monitor_exception Private method to handle exception events.
__monitor_line Private method to handle line events.
__monitor_py_return Private method to handle return events.
__monitor_py_start Private method to handle event when entering the next Python frame.
__profile_c_return Private method to profile the return from C-code frame.
__profile_call Private method to profile the next function / method call.
__profile_py_return Private method to profile the return from current Python frame.
__sendCallTrace Private method to send a call/return trace.
__set_stepinstr Private method to stop before the next instruction.
__skip_file Private method to filter out debugger files.
_has_breakpoint Protected method to check if there is a breakpoint inside the code object.
bootstrap Public method to bootstrap a thread.
getFrame Public method to return the frame "frmnr" down the stack.
getFrameLocals Public method to return the locals dictionary of the current frame or a frame below.
getStack Public method to get the stack.
go Public method to resume the thread.
move_instruction_pointer Public method to move the instruction pointer to another line.
reset_debug_information Public method to reset the cached debug information.
reset_profile Public method to disable call trace profiling.
reset_trace Public method to disable debugging.
run Public method to start a given command under debugger control.
setRecursionDepth Public method to determine the current recursion depth.
set_profile Public method to enable call trace profiling.
set_quit Public method to quit debugging.
set_trace Public method to enable debugging.
set_until Public method to stop when the line with the lineno greater than the current one is reached or when returning from current frame.
step Public method to perform a step operation in this thread.
stepOut Public method to perform a step out of the current call.
storeFrameLocals Public method to store the locals into the frame, so an access to frame.f_locals returns the last data.
tracePythonLibs Public method to update the settings to trace into Python libraries.
user_exception Public method to report an exception to the debug server and wait for user.
user_line Public method to handle the user interaction of a particular line.

Static Methods

None

DebugBase (Constructor)

DebugBase(dbgClient=None)

Constructor of DebugBase.

dbgClient (DebugClient)
the owning client

DebugBase.__disassemble

__disassemble(frame)

Private method to generate a disassembly of the given code object.

frame (code)
frame object to be disassembled
Return:
dictionary containing the disassembly information
Return Type:
dict

DebugBase.__event_poll_timer

__event_poll_timer()

Private method to check every second for new commands.

DebugBase.__extractSystemExitMessage

__extractSystemExitMessage(excinfo)

Private method to get the SystemExit code and message.

excinfo (tuple(Exception, excval object, traceback frame object))
details about the SystemExit exception
Return:
SystemExit code and message
Return Type:
int, str

DebugBase.__extract_stack

__extract_stack(exctb)

Private member to return a list of stack frames.

exctb (traceback)
exception traceback
Return:
list of stack frames
Return Type:
list of frame

DebugBase.__fix_frame_filename

__fix_frame_filename(frame)

Private method used to fixup the filename for a given frame.

The logic employed here is that if a module was loaded from a .pyc file, then the correct .py to operate with should be in the same path as the .pyc. The reason this logic is needed is that when a .pyc file is generated, the filename embedded and thus what is readable in the code object of the frame object is the fully qualified filepath when the pyc is generated. If files are moved from machine to machine this can break debugging as the .pyc will refer to the .py on the original machine. Another case might be sharing code over a network... This logic deals with that.

frame (frame object)
the frame object
Return:
fixed up file name
Return Type:
str

DebugBase.__monitor_exception

__monitor_exception(code: CodeType, _instruction_offset: int, exception: BaseException)

Private method to handle exception events.

Every exception is handled here. It's not possible to disable it.

Because user_exception() requires a triple like returned by sys.exc_info() but sys.exc_info() is returning None at this point, an artificial exc_info is created here.

code (CodeType)
current code object
_instruction_offset (int)
where the call is performed next
exception (subclass of BaseException)
the exception itself

DebugBase.__monitor_line

__monitor_line(code: CodeType, line_number: int)

Private method to handle line events.

When stepping through the code, for each line we have to wait for the user. Otherwise we check, if current line has a breakpoint. If so, wait for user else just continue because in one of those code lines should be a breakpoint. Watchpoints are handled the same as breakpoints.

code (CodeType)
current code object
line_number (int)
current line number
Return:
monitoring.DISABLE if further calls from this code object should be suppressed
Return Type:
None | monitoring.DISABLE

DebugBase.__monitor_py_return

__monitor_py_return(_code: CodeType, _instruction_offset: int, _retval: object)

Private method to handle return events.

When leaving a Python frame this event is triggered if PY_RETURN is enabled. This is used to activate line events when leaving a frame with the command step over, because the step over command want to stop at the current frame.

_code (CodeType)
current code object
_instruction_offset (int)
where the call is performed next
_retval (object)
first return value of the function / method or None

DebugBase.__monitor_py_start

__monitor_py_start(code: CodeType, _instruction_offset: int)

Private method to handle event when entering the next Python frame.

Check if this code object could be of interest, e.g. is there a breakpoint or are we stepped in. In those cases enable line events to get __monitor_line called, otherwise just return monitoring.DISABLE and we don't visit this code object till reenabled by calling monitoring.restart_events().

code (CodeType)
current code object
_instruction_offset (int)
where the call is performed next
Return:
monitoring.DISABLE if further calls from this code object should be suppressed
Return Type:
None | monitoring.DISABLE

DebugBase.__profile_c_return

__profile_c_return(code: CodeType, _instruction_offset: int, callable_object: object, _arg0: object, )

Private method to profile the return from C-code frame. # NOTE: Maybe unused...

code (CodeType)
current code object
_instruction_offset (int)
where the call is performed next
callable_object (object)
function / method which will be called
_arg0 (object)
First argument of the call
Return:
monitoring.DISABLE if further calls from this code object should be suppressed
Return Type:
None | monitoring.DISABLE

DebugBase.__profile_call

__profile_call(code: CodeType, _instruction_offset: int, callable_object: object, _arg0: object, )

Private method to profile the next function / method call.

code (CodeType)
current code object
_instruction_offset (int)
where the call is performed next
callable_object (object)
function / method which will be called
_arg0 (object)
First argument of the call
Return:
monitoring.DISABLE if further calls from this code object should be suppressed
Return Type:
None | monitoring.DISABLE
Raises RuntimeError:
when maximum recursion depth exceeded

DebugBase.__profile_py_return

__profile_py_return(code: CodeType, _instruction_offset: int, _retval: object)

Private method to profile the return from current Python frame.

code (CodeType)
current code object
_instruction_offset (int)
offset of the return statement
_retval (object)
value of the return statement
Return:
monitoring.DISABLE if further calls from this code object should be suppressed
Return Type:
None | monitoring.DISABLE

DebugBase.__sendCallTrace

__sendCallTrace(event, from_frame, to_frame)

Private method to send a call/return trace.

event (str)
trace event
from_frame (frame object)
originating frame
to_frame (frame object)
destination frame

DebugBase.__set_stepinstr

__set_stepinstr()

Private method to stop before the next instruction.

DebugBase.__skip_file

__skip_file(co_filename)

Private method to filter out debugger files.

Tracing is turned off for files that are part of the debugger that are called from the application being debugged.

co_filename (co_filename str)
the frame's filename
Return:
flag indicating whether the debugger should skip this file
Return Type:
bool

DebugBase._has_breakpoint

_has_breakpoint(code: CodeType)

Protected method to check if there is a breakpoint inside the code object.

code (CodeType)
current code object
Return:
Flag if breakpoint in code object found
Return Type:
bool

DebugBase.bootstrap

bootstrap(target, args, kwargs)

Public method to bootstrap a thread.

It wraps the call to the user function to enable tracing before hand.

target (function pointer)
function which is called in the new created thread
args (tuple)
arguments to pass to target
kwargs (dict)
keyword arguments to pass to target

DebugBase.getFrame

getFrame(frmnr=0)

Public method to return the frame "frmnr" down the stack.

frmnr (int)
distance of frames down the stack. 0 is the current frame
Return:
the current frame
Return Type:
frame object

DebugBase.getFrameLocals

getFrameLocals(frmnr=0)

Public method to return the locals dictionary of the current frame or a frame below.

frmnr (int)
distance of frame to get locals dictionary of. 0 is the current frame
Return:
locals dictionary of the frame
Return Type:
dict

DebugBase.getStack

getStack(frame=None, frame_list=None)

Public method to get the stack.

frame (frame object or list)
frame object to inspect
frame_list (list or None)
where to store the frame information
Return:
list of lists with file name, line number, function name and function arguments
Return Type:
list of list of [str, int, str, str]

DebugBase.go

go(special=events.NO_EVENTS)

Public method to resume the thread.

It resumes the thread stopping only at breakpoints or exceptions.

special (bool | events)
flag indicating a special continue operation

DebugBase.move_instruction_pointer

move_instruction_pointer(lineno)

Public method to move the instruction pointer to another line.

lineno (int)
new line number

DebugBase.reset_debug_information

reset_debug_information()

Public method to reset the cached debug information.

This is needed to enable newly set breakpoints or watches, because the debugger might stepped already through some of those code objects and disabled further processing for speed optimization.

DebugBase.reset_profile

reset_profile()

Public method to disable call trace profiling.

DebugBase.reset_trace

reset_trace()

Public method to disable debugging.

DebugBase.run

run(cmd, globalsDict=None, localsDict=None, debug=True, closeSession=True)

Public method to start a given command under debugger control.

cmd (str or CodeType)
command / code to execute under debugger control
globalsDict (dict)
dictionary of global variables for cmd
localsDict (dict)
dictionary of local variables for cmd
debug (bool)
flag if command should run under debugger control
closeSession (bool)
flag indicating to close the debugger session at exit
Return:
exit code of the program
Return Type:
int

DebugBase.setRecursionDepth

setRecursionDepth(frame)

Public method to determine the current recursion depth.

frame (frame object)
The current stack frame.

DebugBase.set_profile

set_profile()

Public method to enable call trace profiling.

DebugBase.set_quit

set_quit()

Public method to quit debugging.

Disables the trace functions and resets all frame pointer.

DebugBase.set_trace

set_trace(start_line_trace=True)

Public method to enable debugging.

start_line_trace (bool)
start debugging with the next line

DebugBase.set_until

set_until(lineno)

Public method to stop when the line with the lineno greater than the current one is reached or when returning from current frame.

The name "until" is borrowed from gdb.

lineno (int)
line number to continue to

DebugBase.step

step(step_into)

Public method to perform a step operation in this thread.

step_into (bool)
If it is True, then the step is a step into, otherwise it is a step over.

DebugBase.stepOut

stepOut()

Public method to perform a step out of the current call.

DebugBase.storeFrameLocals

storeFrameLocals(frmnr=0)

Public method to store the locals into the frame, so an access to frame.f_locals returns the last data.

frmnr (int)
distance of frame to store locals dictionary to. 0 is the current frame

DebugBase.tracePythonLibs

tracePythonLibs(enable)

Public method to update the settings to trace into Python libraries.

enable (bool)
flag to debug into Python libraries

DebugBase.user_exception

user_exception(excinfo, unhandled=False)

Public method to report an exception to the debug server and wait for user.

excinfo (tuple(Exception, excval object, traceback frame object))
details about the exception
unhandled (bool)
flag indicating an uncaught exception

DebugBase.user_line

user_line(thread_id, frame)

Public method to handle the user interaction of a particular line.

thread_id (int)
the current tread id
frame (frame object)
reference to the frame object
Up


printerr

printerr(s)

Module function used for debugging the debug client.

s (str)
data to be printed
Up


setRecursionLimit

setRecursionLimit(limit)

Module function to set the recursion limit.

limit (int)
recursion limit
Up