Debian Codegraph @

test/commands_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2020 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 import argparse
17 import sys
18 import traceback
19 import logging
20 import tempfile
21 import platform
22
23 from pyocd.core.helpers import ConnectHelper
24 from pyocd.probe.pydapaccess import DAPAccess
25 from pyocd.utility.mask import round_up_div
26 from pyocd.utility import conversion
27 from pyocd.core.memory_map import MemoryType
28 from pyocd.commands.execution_context import CommandExecutionContext
29 from pyocd.commands import commands
30 from test_util import (
31 Test,
32 TestResult,
33 get_session_options,
34 get_target_test_params,
35 binary_to_hex_file,
36 binary_to_elf_file,
37 get_test_binary_path,
38 )
39
40 class CommandsTestResult(TestResult):
41 def __init__(self):
42 super(CommandsTestResult, self).__init__(None, None, None)
43 self.name = "commands"
44
45 class CommandsTest(Test):
46 def __init__(self):
47 super(CommandsTest, self).__init__("Commands Test", commands_test)
48
49 def run(self, board):
50 try:
51 result = self.test_function(board.unique_id)
52 except Exception as e:
53 result = CommandsTestResult()
54 result.passed = False
55 print("Exception %s when testing board %s" % (e, board.unique_id))
56 traceback.print_exc(file=sys.stdout)
57 result.board = board
58 result.test = self
59 return result
60
61 def fix_windows_path(path: str) -> str:
62 """Double backslashes in paths on Windows."""
63 if platform.system() == "Windows":
64 return path.replace('\\', '\\\\')
65 else:
66 return path
67
68 def commands_test(board_id):
69 with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session:
70 board = session.board
71 target = session.target
72 target_type = board.target_type
73
74 test_params = get_target_test_params(session)
75 session.probe.set_clock(test_params['test_clock'])
76
77 memory_map = board.target.get_memory_map()
78 boot_region = memory_map.get_boot_memory()
79 ram_region = memory_map.get_default_region_of_type(MemoryType.RAM)
80 ram_base = ram_region.start
81 boot_start_addr = boot_region.start
82 boot_end_addr = boot_region.end
83 boot_blocksize = boot_region.blocksize
84 binary_file = get_test_binary_path(board.test_binary)
85
86 # Generate an Intel hex file from the binary test file.
87 temp_test_hex_name = binary_to_hex_file(binary_file, boot_region.start)
88
89 temp_bin_file = tempfile.mktemp('.bin')
90
91 with open(binary_file, "rb") as f:
92 test_data = list(bytearray(f.read()))
93 test_data_length = len(test_data)
94 test_file_sectors = round_up_div(test_data_length, boot_blocksize)
95 boot_first_free_block_addr = boot_start_addr + test_file_sectors * boot_blocksize
96 reset_handler_addr = conversion.byte_list_to_u32le_list(test_data[4:8])[0]
97
98 test_pass_count = 0
99 test_count = 0
100 failed_commands = []
101 result = CommandsTestResult()
102
103 context = CommandExecutionContext()
104 context.attach_session(session)
105
106 COMMANDS_TO_TEST = [
107 "status",
108 "reset",
109 "reset halt",
110 "reg",
111 "reg general",
112 "reg all",
113 "reg r0",
114 "wreg r0 0x12345678",
115 "d pc",
116 "d --center pc 32",
117 "read64 0x%08x" % ((boot_start_addr + boot_blocksize) & ~7),
118 "read32 0x%08x" % ((boot_start_addr + boot_blocksize) & ~3),
119 "read16 0x%08x" % ((boot_start_addr + boot_blocksize) & ~1),
120 "read8 0x%08x" % (boot_start_addr + boot_blocksize),
121 "rd 0x%08x 16" % ram_base,
122 "rw 0x%08x 16" % ram_base,
123 "rh 0x%08x 16" % ram_base,
124 "rb 0x%08x 16" % ram_base,
125 "write64 0x%08x 0x1122334455667788 0xaabbccddeeff0011" % ram_base,
126 "write32 0x%08x 0x11223344 0x55667788" % ram_base,
127 "write16 0x%08x 0xabcd" % (ram_base + 8),
128 "write8 0x%08x 0 1 2 3 4 5 6" % (ram_base + 10),
129 "savemem 0x%08x 128 '%s'" % (boot_start_addr, fix_windows_path(temp_bin_file)),
130 "loadmem 0x%08x '%s'" % (ram_base, fix_windows_path(temp_bin_file)),
131 "loadmem 0x%08x '%s'" % (boot_start_addr, fix_windows_path(binary_file)),
132 "load '%s'" % fix_windows_path(temp_test_hex_name),
133 "load '%s' 0x%08x" % (fix_windows_path(binary_file), boot_start_addr),
134 "compare 0x%08x '%s'" % (ram_base, fix_windows_path(temp_bin_file)),
135 "compare 0x%08x 32 '%s'" % (ram_base, fix_windows_path(temp_bin_file)),
136 "fill 0x%08x 128 0xa5" % ram_base,
137 "fill 16 0x%08x 64 0x55aa" % (ram_base + 64),
138 "find 0x%08x 128 0xaa 0x55" % ram_base, # find that will pass
139 "find 0x%08x 128 0xff" % ram_base, # find that will fail
140 "find -n 0x%08x 128 0xff" % ram_base, # inverted find that will now pass
141 "erase 0x%08x" % (boot_first_free_block_addr),
142 "erase 0x%08x 1" % (boot_first_free_block_addr + boot_blocksize),
143 "go",
144 "halt",
145 "step",
146 "s 4",
147 "continue",
148 "h",
149 "break 0x%08x" % reset_handler_addr,
150 "lsbreak",
151 "rmbreak 0x%08x" % reset_handler_addr,
152 "watch 0x%08x" % ram_base,
153 "lswatch",
154 "rmwatch 0x%08x" % ram_base,
155 "watch 0x%08x rw 2" % ram_base,
156 "rmwatch 0x%08x" % ram_base,
157 "core",
158 "core 0",
159 "readdp 0", # read DPIDR
160 "writedp 0 0x1e", # write ABORT to clear error flags
161 "readap 0xfc", # read IDR
162 "readap 0 0xfc", # read IDR of AP#0
163 "writeap 0x4 0", # set TAR to 0
164 "writeap 0 0x4 0", # set TAR to 0 on AP#0
165 "gdbserver start",
166 "gdbserver status",
167 "gdbserver stop",
168 "probeserver start",
169 "probeserver status",
170 "probeserver stop",
171 "show probe-uid",
172 "show target",
173 "show cores",
174 "show map",
175 "show peripherals",
176 "show fault",
177 "show nreset",
178 "set nreset 1",
179 "show option reset_type",
180 "set option reset_type=sw",
181 "show mem-ap",
182 "set mem-ap 0",
183 "show hnonsec",
184 "set hnonsec 0",
185 "show hprot",
186 "set hprot 0x3", # set default hprot: data, priv
187 "show graph",
188 "show locked",
189 "show register-groups",
190 "show vector-catch",
191 "set vector-catch all",
192 "show step-into-interrupts",
193 "set step-into-interrupts 1",
194 "set log info",
195 "set frequency %d" % test_params['test_clock'],
196
197 # Semicolon-separated commands.
198 'rw 0x%08x ; rw 0x%08x' % (ram_base, ram_base + 4),
199 'rb 0x%08x;rb 0x%08x' % (ram_base, ram_base + 1),
200 'rb 0x%08x; rb 0x%08x' % (ram_base, ram_base + 1),
201
202 # Python and system commands.
203 '$2+ 2',
204 '$ target',
205 '!echo hello',
206 '!echo hi ; echo there', # semicolon in a sytem command (because semicolon separation is not supported for Python/system command lines)
207 ' $ " ".join(["hello", "there"])',
208 ' ! echo "yo dude" ',
209
210 # Commands not tested:
211 # "list",
212 # "erase", # chip erase
213 # "unlock",
214 # "exit",
215 # "initdp",
216 # "makeap",
217 # "reinit",
218 # "where",
219 # "symbol",
220 ]
221
222 # For now we just verify that the commands run without raising an exception.
223 print("\n------ Testing commands ------")
224
225 def test_command(cmd, print_result=True):
226 try:
227 print("\nTEST: %s" % cmd)
228 context.process_command_line(cmd)
229 except Exception:
230 if print_result:
231 print("TEST FAILED")
232 failed_commands.append(cmd)
233 traceback.print_exc(file=sys.stdout)
234 return False
235 else:
236 if print_result:
237 print("TEST PASSED")
238 return True
239
240 for cmd in COMMANDS_TO_TEST:
241 if test_command(cmd):
242 test_pass_count += 1
243 test_count += 1
244
245 #
246 # Special command tests.
247 #
248 print("\n------ Additional command tests ------")
249
250 # set option
251 session.options['auto_unlock'] = False
252 if test_command('set option auto_unlock=on', print_result=False):
253 if session.options['auto_unlock'] is True:
254 test_pass_count += 1
255 print("TEST PASSED")
256 else:
257 print("TEST FAILED")
258
259 test_count += 1
260
261 print("\n\nTest Summary:")
262 print("Pass count %i of %i tests" % (test_pass_count, test_count))
263 if failed_commands:
264 for c in failed_commands:
265 print(" - '" + c + "'")
266 if test_pass_count == test_count:
267 print("COMMANDS TEST SCRIPT PASSED")
268 else:
269 print("COMMANDS TEST SCRIPT FAILED")
270
271 target.reset()
272
273 result.passed = test_count == test_pass_count
274 return result
275
276 if __name__ == "__main__":
277 parser = argparse.ArgumentParser(description='pyOCD commands test')
278 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
279 parser.add_argument('-u', '--uid', help='Debug probe unique ID')
280 parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
281 args = parser.parse_args()
282 level = logging.DEBUG if args.debug else logging.INFO
283 logging.basicConfig(level=level)
284 DAPAccess.set_args(args.daparg)
285 commands_test(args.uid)