The following recordings-related functions
(see Process Record and Replay) are available in the gdb
module:
Start a recording using the given method and format. If no format is given, the default format for the recording method is used. If no method is given, the default method will be used. Returns a
gdb.Recordobject on success. Throw an exception on failure.The following strings can be passed as method:
"full""btrace": Possible values for format:"pt","bts"or leave out for default format.
Access a currently running recording. Return a
gdb.Recordobject on success. ReturnNoneif no recording is currently active.
Stop the current recording. Throw an exception if no recording is currently active. All record objects become invalid after this call.
A gdb.Record object has the following attributes:
A method specific instruction object representing the first instruction in this recording.
A method specific instruction object representing the current instruction, that is not actually part of the recording.
The instruction representing the current replay position. If there is no replay active, this will be
None.
A gdb.Record object has the following methods:
The common gdb.Instruction class that recording method specific
instruction objects inherit from, has the following attributes:
A buffer with the raw instruction data. In Python 3, the return value is a
memoryviewobject.
Additionally gdb.RecordInstruction has the following attributes:
An integer identifying this instruction.
numbercorresponds to the numbers seen inrecord instruction-history(see Process Record and Replay).
A
gdb.Symtab_and_lineobject representing the associated symtab and line of this instruction. May beNoneif no debug information is available.
A boolean indicating whether the instruction was executed speculatively.
If an error occured during recording or decoding a recording, this error is
represented by a gdb.RecordGap object in the instruction list. It has
the following attributes:
An integer identifying this gap.
numbercorresponds to the numbers seen inrecord instruction-history(see Process Record and Replay).
A numerical representation of the reason for the gap. The value is specific to the current recording method.
A gdb.RecordFunctionSegment object has the following attributes:
An integer identifying this function segment.
numbercorresponds to the numbers seen inrecord function-call-history(see Process Record and Replay).
A
gdb.Symbolobject representing the associated symbol. May beNoneif no debug information is available.
An integer representing the function call's stack level. May be
Noneif the function call is a gap.
A list of
gdb.RecordInstructionorgdb.RecordGapobjects associated with this function call.
A
gdb.RecordFunctionSegmentobject representing the caller's function segment. If the call has not been recorded, this will be the function segment to which control returns. If neither the call nor the return have been recorded, this will beNone.
A
gdb.RecordFunctionSegmentobject representing the previous segment of this function call. May beNone.
A
gdb.RecordFunctionSegmentobject representing the next segment of this function call. May beNone.
The following example demonstrates the usage of these objects and functions to create a function that will rewind a record to the last time a function in a different file was executed. This would typically be used to track the execution of user provided callback functions in a library which typically are not visible in a back trace.
def bringback ():
rec = gdb.current_recording ()
if not rec:
return
insn = rec.instruction_history
if len (insn) == 0:
return
try:
position = insn.index (rec.replay_position)
except:
position = -1
try:
filename = insn[position].sal.symtab.fullname ()
except:
filename = None
for i in reversed (insn[:position]):
try:
current = i.sal.symtab.fullname ()
except:
current = None
if filename == current:
continue
rec.goto (i)
return
Another possible application is to write a function that counts the number of code executions in a given line range. This line range can contain parts of functions or span across several functions and is not limited to be contiguous.
def countrange (filename, linerange):
count = 0
def filter_only (file_name):
for call in gdb.current_recording ().function_call_history:
try:
if file_name in call.symbol.symtab.fullname ():
yield call
except:
pass
for c in filter_only (filename):
for i in c.instructions:
try:
if i.sal.line in linerange:
count += 1
break;
except:
pass
return count