| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
import os |
| 27 |
import json |
| 28 |
import sys |
| 29 |
from subprocess import ( |
| 30 |
Popen, |
| 31 |
STDOUT, |
| 32 |
PIPE, |
| 33 |
) |
| 34 |
import argparse |
| 35 |
import logging |
| 36 |
import traceback |
| 37 |
import threading |
| 38 |
|
| 39 |
from pyocd.__main__ import PyOCDTool |
| 40 |
from pyocd.core.helpers import ConnectHelper |
| 41 |
from pyocd.core.memory_map import MemoryType |
| 42 |
from pyocd.core.session import Session |
| 43 |
from pyocd.flash.file_programmer import FileProgrammer |
| 44 |
from test_util import ( |
| 45 |
Test, |
| 46 |
TestResult, |
| 47 |
get_session_options, |
| 48 |
get_target_test_params, |
| 49 |
binary_to_elf_file, |
| 50 |
get_env_file_name, |
| 51 |
get_test_binary_path, |
| 52 |
TEST_DIR, |
| 53 |
TEST_OUTPUT_DIR, |
| 54 |
ensure_output_dir, |
| 55 |
wait_with_deadline, |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
LOG = logging.getLogger(__name__) |
| 62 |
|
| 63 |
PYTHON_GDB = "arm-none-eabi-gdb-py" |
| 64 |
TEST_TIMEOUT_SECONDS = 60.0 * 5 |
| 65 |
SERVER_EXIT_TIMEOUT = 10.0 |
| 66 |
|
| 67 |
GDB_SCRIPT_PATH = os.path.join(TEST_DIR, "gdb_test_script.py") |
| 68 |
|
| 69 |
class GdbTestResult(TestResult): |
| 70 |
def __init__(self): |
| 71 |
super(self.__class__, self).__init__(None, None, None) |
| 72 |
self.name = "gdbserver" |
| 73 |
|
| 74 |
|
| 75 |
class GdbTest(Test): |
| 76 |
def __init__(self): |
| 77 |
super(self.__class__, self).__init__("Gdb Test", test_gdb) |
| 78 |
|
| 79 |
def run(self, board): |
| 80 |
try: |
| 81 |
result = self.test_function(board.unique_id, self.n) |
| 82 |
except Exception as e: |
| 83 |
result = GdbTestResult() |
| 84 |
result.passed = False |
| 85 |
print("Exception %s when testing board %s" % |
| 86 |
(e, board.unique_id)) |
| 87 |
traceback.print_exc(file=sys.stdout) |
| 88 |
result.board = board |
| 89 |
result.test = self |
| 90 |
return result |
| 91 |
|
| 92 |
TEST_RESULT_KEYS = [ |
| 93 |
"breakpoint_count", |
| 94 |
"watchpoint_count", |
| 95 |
"step_time_si", |
| 96 |
"step_time_s", |
| 97 |
"step_time_n", |
| 98 |
"fail_count", |
| 99 |
] |
| 100 |
|
| 101 |
|
| 102 |
def test_gdb(board_id=None, n=0): |
| 103 |
temp_test_elf_name = None |
| 104 |
result = GdbTestResult() |
| 105 |
with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session: |
| 106 |
board = session.board |
| 107 |
memory_map = board.target.get_memory_map() |
| 108 |
ram_region = memory_map.get_default_region_of_type(MemoryType.RAM) |
| 109 |
rom_region = memory_map.get_boot_memory() |
| 110 |
target_type = board.target_type |
| 111 |
binary_file = get_test_binary_path(board.test_binary) |
| 112 |
if board_id is None: |
| 113 |
board_id = board.unique_id |
| 114 |
target_test_params = get_target_test_params(session) |
| 115 |
test_port = 3333 + n |
| 116 |
telnet_port = 4444 + n |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
fpb = session.target.selected_core.fpb |
| 121 |
assert fpb is not None |
| 122 |
ignore_hw_bkpt_result = int(fpb.revision == 1 and ram_region.start >= 0x20000000) |
| 123 |
|
| 124 |
|
| 125 |
board.target.reset_and_halt() |
| 126 |
FileProgrammer(session).program(binary_file, base_address=rom_region.start) |
| 127 |
|
| 128 |
|
| 129 |
temp_test_elf_name = binary_to_elf_file(binary_file, rom_region.start) |
| 130 |
|
| 131 |
|
| 132 |
test_params = { |
| 133 |
"test_port" : test_port, |
| 134 |
"rom_start" : rom_region.start, |
| 135 |
"rom_length" : rom_region.length, |
| 136 |
"ram_start" : ram_region.start, |
| 137 |
"ram_length" : ram_region.length, |
| 138 |
"invalid_start" : 0x3E000000, |
| 139 |
"invalid_length" : 0x1000, |
| 140 |
"expect_error_on_invalid_access" : target_test_params['error_on_invalid_access'], |
| 141 |
"ignore_hw_bkpt_result" : ignore_hw_bkpt_result, |
| 142 |
"test_elf" : temp_test_elf_name, |
| 143 |
} |
| 144 |
test_param_filename = os.path.join(TEST_OUTPUT_DIR, "gdb_test_params%s_%d.txt" % (get_env_file_name(), n)) |
| 145 |
with open(test_param_filename, "w") as f: |
| 146 |
f.write(json.dumps(test_params)) |
| 147 |
|
| 148 |
|
| 149 |
test_result_filename = os.path.join(TEST_OUTPUT_DIR, "gdb_test_results%s_%d.txt" % (get_env_file_name(), n)) |
| 150 |
if os.path.exists(test_result_filename): |
| 151 |
os.remove(test_result_filename) |
| 152 |
|
| 153 |
|
| 154 |
gdb_args = [PYTHON_GDB, "--nh", "-ex", "set $testn=%d" % n, "--command=%s" % GDB_SCRIPT_PATH] |
| 155 |
gdb_output_filename = os.path.join(TEST_OUTPUT_DIR, "gdb_output%s_%s_%d.txt" % (get_env_file_name(), board.target_type, n)) |
| 156 |
with open(gdb_output_filename, "w") as f: |
| 157 |
LOG.info('Starting gdb (stdout -> %s): %s', gdb_output_filename, ' '.join(gdb_args)) |
| 158 |
gdb_program = Popen(gdb_args, stdin=PIPE, stdout=f, stderr=STDOUT) |
| 159 |
server_args = ['gdbserver', |
| 160 |
'--port=%i' % test_port, |
| 161 |
"--telnet-port=%i" % telnet_port, |
| 162 |
"--frequency=%i" % target_test_params['test_clock'], |
| 163 |
"--uid=%s" % board_id, |
| 164 |
] |
| 165 |
server = PyOCDTool() |
| 166 |
server._setup_logging = lambda: None |
| 167 |
LOG.info('Starting gdbserver: %s', ' '.join(server_args)) |
| 168 |
server_thread = threading.Thread(target=server.run, args=[server_args]) |
| 169 |
server_thread.daemon = True |
| 170 |
server_thread.start() |
| 171 |
|
| 172 |
LOG.info('Waiting for gdb to finish...') |
| 173 |
did_complete = wait_with_deadline(gdb_program, TEST_TIMEOUT_SECONDS) |
| 174 |
if not did_complete: |
| 175 |
LOG.error("Test timed out!") |
| 176 |
|
| 177 |
LOG.info('Waiting for server to finish...') |
| 178 |
server_thread.join(timeout=SERVER_EXIT_TIMEOUT) |
| 179 |
if server_thread.is_alive(): |
| 180 |
LOG.error('Server is still running! Stopping now... and failing test') |
| 181 |
did_complete = False |
| 182 |
session = Session.get_current() |
| 183 |
LOG.info(f"gdbserver session: {session}") |
| 184 |
LOG.info(f"gdbservers: {session.gdbservers}") |
| 185 |
for g in session.gdbservers.values(): |
| 186 |
g.stop() |
| 187 |
|
| 188 |
|
| 189 |
server_thread.join(timeout=SERVER_EXIT_TIMEOUT) |
| 190 |
if server_thread.is_alive(): |
| 191 |
|
| 192 |
|
| 193 |
LOG.error("Server thread is still alive after stopping gdbservers!") |
| 194 |
|
| 195 |
try: |
| 196 |
with open(gdb_output_filename, 'r') as f: |
| 197 |
LOG.debug('Gdb output:\n%s', f.read()) |
| 198 |
except IOError: |
| 199 |
pass |
| 200 |
|
| 201 |
|
| 202 |
result.passed = False |
| 203 |
if did_complete: |
| 204 |
try: |
| 205 |
with open(test_result_filename, "r") as f: |
| 206 |
test_result = json.loads(f.read()) |
| 207 |
|
| 208 |
|
| 209 |
if set(TEST_RESULT_KEYS).issubset(test_result): |
| 210 |
print("----------------Test Results----------------") |
| 211 |
print("HW breakpoint count: %s" % test_result["breakpoint_count"]) |
| 212 |
print("Watchpoint count: %s" % test_result["watchpoint_count"]) |
| 213 |
print("Average instruction step time: %s" % |
| 214 |
test_result["step_time_si"]) |
| 215 |
print("Average single step time: %s" % test_result["step_time_s"]) |
| 216 |
print("Average over step time: %s" % test_result["step_time_n"]) |
| 217 |
print("Failure count: %i" % test_result["fail_count"]) |
| 218 |
result.passed = test_result["fail_count"] == 0 |
| 219 |
except IOError as err: |
| 220 |
LOG.error("Error reading test results: %s", err, exc_info=True) |
| 221 |
|
| 222 |
if result.passed: |
| 223 |
print("GDB TEST PASSED") |
| 224 |
else: |
| 225 |
print("GDB TEST FAILED") |
| 226 |
|
| 227 |
|
| 228 |
try: |
| 229 |
if temp_test_elf_name and os.path.exists(temp_test_elf_name): |
| 230 |
os.remove(temp_test_elf_name) |
| 231 |
os.remove(test_result_filename) |
| 232 |
os.remove(test_param_filename) |
| 233 |
except IOError as err: |
| 234 |
pass |
| 235 |
|
| 236 |
return result |
| 237 |
|
| 238 |
if __name__ == "__main__": |
| 239 |
parser = argparse.ArgumentParser(description='pyOCD gdb test') |
| 240 |
parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging') |
| 241 |
args = parser.parse_args() |
| 242 |
level = logging.DEBUG if args.debug else logging.INFO |
| 243 |
logging.basicConfig(level=level) |
| 244 |
ensure_output_dir() |
| 245 |
test_gdb() |