winIDEA SDK
set_bp_on_first_available_line.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5"""
6This script tries to set breakpoint on the requested line, but if this
7line does not generate any code, then it tries to find the first line,
8where breakpoint can be set.
9"""
10
11import math
12import isystem.connect as ic
13
14
15winidea_id = ''
16
17
18def initTarget(isRunUntilMain):
19 """
20 Initializes target - downloads only if target is not in stopped state.
21
22 Parameters:
23 isRunUntilMain - if true, target is reset, then runUntilFunction('main')
24 is executed.
25
26 Return: ic.ConnectionMgr(), ic.CDebugFacade(cmgr)
27 """
28
29 print('isystem.connect version: ' + ic.getModuleVersion())
30
31 cmgr = ic.ConnectionMgr()
32 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
33
34 debugCtrl = ic.CDebugFacade(cmgr)
35
36 # perform download only if target is not in proper state
37 cpuStatus = debugCtrl.getCPUStatus()
38
39 if not cpuStatus.isStopped():
40 debugCtrl.download()
41
42 if isRunUntilMain:
43 debugCtrl.reset()
44 debugCtrl.runUntilFunction("main")
45 debugCtrl.waitUntilStopped()
46
47 return cmgr, debugCtrl
48
49
50def findLine(addrCtrl, functionName, searchString):
51 lineDesc = ic.CLineDescription(ic.CLineDescription.E_RESOURCE_FUNCTION,
52 functionName,
53 0, # start at the beginning of a function
54 True, # perform search
55 0, # no limit
56 ic.CLineDescription.E_SEARCH_ANY,
57 ic.CLineDescription.E_MATCH_PLAIN,
58 searchString,
59 0,
60 ic.CLineDescription.ELocalHost)
61
62 lineLocation = addrCtrl.getSourceLocation(lineDesc)
63
64 return lineLocation
65
66
67def setAdaptiveBPInFile(debugCtrl, fileName, startLine, maxOffset):
68 """
69 This function tries to find the first line, where breakpoint can be set.
70
71 Parameters:
72
73 debugCtrl - instance of CDebugFacade
74
75 fileName - name of source file to set breakpoint in. Make sure that it also
76 includes path, if necessary. See Symbol browser in winIDEA,
77 tab Modules, to find out correct path.
78
79 startLine - the first line where we'd like to set a breakpoint
80
81 maxOffset - maximum number of lines, where to search for line which
82 generated object code. If positive, following lines are searched,
83 if negative, preceding lines are searched. Should not be 0.
84 """
85
86 isBPSet = False
87
88 # search for the first line in range, which generated some code
89 for line in range(startLine, startLine + maxOffset, int(math.copysign(1, maxOffset))):
90 addresses = debugCtrl.getAddressOfSourceLine(fileName, line)
91 if addresses.size() > 0:
92 debugCtrl.setBP(line, fileName)
93 isBPSet = True
94 break;
95
96 if not isBPSet:
97 raise Exception("Can not set breakpoint at '{}', line {}, maxOffset = {}!"
98 .format(fileName, startLine, maxOffset))
99
100
101def setAdaptiveBPInFunction(debugCtrl, addrCtrl, functionName, searchString, maxOffset):
102 """
103 This function tries to find the first line, where breakpoint can be set.
104
105 Parameters:
106
107 debugCtrl - instance of CDebugFacade
108
109 addrCtrl - instance of CAddressController
110
111 functionName - name of function to set breakpoint in.
112
113 searchString - string in line where we want to set a breakpoint
114
115 maxOffset - maximum number of lines, where to search for line which
116 generated object code. If positive, following lines are searched,
117 if negative, preceding lines are searched. Should not be 0.
118 """
119 lineLocation = findLine(addrCtrl, functionName, searchString)
120 print('file: ', lineLocation.getFileName())
121 print('line: ', lineLocation.getLineNumber())
122
123 setAdaptiveBPInFile(debugCtrl,
124 lineLocation.getFileName(),
125 lineLocation.getLineNumber(),
126 maxOffset)
127
128
129cmgr, debugCtrl = initTarget(False)
130addrCtrl = ic.CAddressController(cmgr)
131
132setAdaptiveBPInFile(debugCtrl, 'src\\utils.cpp', 271, -50)
133setAdaptiveBPInFunction(debugCtrl, addrCtrl, 'fibonacci', '}', 10)
134setAdaptiveBPInFunction(debugCtrl, addrCtrl, 'trim', 'for', -10)
135setAdaptiveBPInFunction(debugCtrl, addrCtrl, 'split', 'if (strstr(input, delimiter) == NULL)', 10)