winIDEA SDK
Loading...
Searching...
No Matches
set_bp_on_first_available_line.py
# This script is licensed under BSD License, see file LICENSE.txt, or search for `License` in the SDK online help.
#
# (c) TASKING Germany GmbH, 2023
"""
This script tries to set breakpoint on the requested line, but if this
line does not generate any code, then it tries to find the first line,
where breakpoint can be set.
"""
import math
import isystem.connect as ic
winidea_id = ''
def initTarget(isRunUntilMain):
"""
Initializes target - downloads only if target is not in stopped state.
Parameters:
isRunUntilMain - if true, target is reset, then runUntilFunction('main')
is executed.
Return: ic.ConnectionMgr(), ic.CDebugFacade(cmgr)
"""
print('isystem.connect version: ' + ic.getModuleVersion())
cmgr = ic.ConnectionMgr()
cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
debugCtrl = ic.CDebugFacade(cmgr)
# perform download only if target is not in proper state
cpuStatus = debugCtrl.getCPUStatus()
if not cpuStatus.isStopped():
debugCtrl.download()
if isRunUntilMain:
debugCtrl.reset()
debugCtrl.runUntilFunction("main")
debugCtrl.waitUntilStopped()
return cmgr, debugCtrl
def findLine(addrCtrl, functionName, searchString):
lineDesc = ic.CLineDescription(ic.CLineDescription.E_RESOURCE_FUNCTION,
functionName,
0, # start at the beginning of a function
True, # perform search
0, # no limit
ic.CLineDescription.E_SEARCH_ANY,
ic.CLineDescription.E_MATCH_PLAIN,
searchString,
0,
ic.CLineDescription.ELocalHost)
lineLocation = addrCtrl.getSourceLocation(lineDesc)
return lineLocation
def setAdaptiveBPInFile(debugCtrl, fileName, startLine, maxOffset):
"""
This function tries to find the first line, where breakpoint can be set.
Parameters:
debugCtrl - instance of CDebugFacade
fileName - name of source file to set breakpoint in. Make sure that it also
includes path, if necessary. See Symbol browser in winIDEA,
tab Modules, to find out correct path.
startLine - the first line where we'd like to set a breakpoint
maxOffset - maximum number of lines, where to search for line which
generated object code. If positive, following lines are searched,
if negative, preceding lines are searched. Should not be 0.
"""
isBPSet = False
# search for the first line in range, which generated some code
for line in range(startLine, startLine + maxOffset, int(math.copysign(1, maxOffset))):
addresses = debugCtrl.getAddressOfSourceLine(fileName, line)
if addresses.size() > 0:
debugCtrl.setBP(line, fileName)
isBPSet = True
break;
if not isBPSet:
raise Exception("Can not set breakpoint at '{}', line {}, maxOffset = {}!"
.format(fileName, startLine, maxOffset))
def setAdaptiveBPInFunction(debugCtrl, addrCtrl, functionName, searchString, maxOffset):
"""
This function tries to find the first line, where breakpoint can be set.
Parameters:
debugCtrl - instance of CDebugFacade
addrCtrl - instance of CAddressController
functionName - name of function to set breakpoint in.
searchString - string in line where we want to set a breakpoint
maxOffset - maximum number of lines, where to search for line which
generated object code. If positive, following lines are searched,
if negative, preceding lines are searched. Should not be 0.
"""
lineLocation = findLine(addrCtrl, functionName, searchString)
print('file: ', lineLocation.getFileName())
print('line: ', lineLocation.getLineNumber())
setAdaptiveBPInFile(debugCtrl,
lineLocation.getFileName(),
lineLocation.getLineNumber(),
maxOffset)
if __name__ == '__main__':
cmgr, debugCtrl = initTarget(False)
addrCtrl = ic.CAddressController(cmgr)
setAdaptiveBPInFile(debugCtrl, 'src\\utils.cpp', 271, -50)
setAdaptiveBPInFunction(debugCtrl, addrCtrl, 'fibonacci', '}', 10)
setAdaptiveBPInFunction(debugCtrl, addrCtrl, 'trim', 'for', -10)
setAdaptiveBPInFunction(debugCtrl, addrCtrl, 'split', 'if (strstr(input, delimiter) == NULL)', 10)