Debian Codegraph @

test/probeserver_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2021 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
17 # Note
18 # To run this script GNU Tools ARM Embedded must be installed,
19 # along with python for the same architecture. The program
20 # "arm-none-eabi-gdb-py.exe" requires python for the same
21 # architecture (x86 or 64) to work correctly. Also, on windows
22 # the GNU Tools ARM Embedded bin directory needs to be added to
23 # your path.
24
25 import os
26 import json
27 import sys
28 from subprocess import (
29 Popen,
30 STDOUT,
31 PIPE,
32 check_output,
33 )
34 import argparse
35 import logging
36 import traceback
37 import threading
38 from time import sleep
39
40 from pyocd.__main__ import PyOCDTool
41 from pyocd.core.helpers import ConnectHelper
42 from pyocd.utility.compatibility import to_str_safe
43 from pyocd.core.memory_map import MemoryType
44 from pyocd.flash.file_programmer import FileProgrammer
45 from pyocd.utility.timeout import Timeout
46 from test_util import (
47 Test,
48 TestResult,
49 get_session_options,
50 get_target_test_params,
51 binary_to_elf_file,
52 get_env_file_name,
53 get_test_binary_path,
54 TEST_DIR,
55 TEST_OUTPUT_DIR,
56 ensure_output_dir,
57 wait_with_deadline,
58 )
59
60 LOG = logging.getLogger(__name__)
61
62 TEST_TIMEOUT_SECONDS = 60.0 * 5
63
64 class TestError(Exception):
65 pass
66
67 class ProbeserverTestResult(TestResult):
68 def __init__(self):
69 super(self.__class__, self).__init__(None, None, None)
70 self.name = "probeserver"
71
72 class ProbeserverTest(Test):
73 def __init__(self):
74 super(self.__class__, self).__init__("Probeserver Test", test_probeserver)
75
76 def run(self, board):
77 try:
78 result = self.test_function(board.unique_id, self.n)
79 except Exception as e:
80 result = ProbeserverTestResult()
81 result.passed = False
82 print("Exception %s when testing board %s" %
83 (e, board.unique_id))
84 traceback.print_exc(file=sys.stdout)
85 result.board = board
86 result.test = self
87 return result
88
89 def test_probeserver(board_id=None, n=0):
90 test_port = 5555 + n
91 temp_test_elf_name = None
92 result = ProbeserverTestResult()
93 print("Connecting to identify target")
94 with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session:
95 board = session.board
96 target_test_params = get_target_test_params(session)
97 binary_file = get_test_binary_path(board.test_binary)
98 if board_id is None:
99 board_id = board.unique_id
100 target_type = board.target_type
101
102 # Run the test. We can't kill the server thread, so
103 LOG.info('Starting server on port %d', test_port)
104 server_args = ['pyocd', 'server',
105 '-v',
106 '--port=%i' % test_port,
107 "--uid=%s" % board_id,
108 ]
109 server_program = Popen(server_args, stdout=PIPE, stderr=STDOUT)
110
111 try:
112 # Read server output waiting for it to report that the server is running.
113 with Timeout(TEST_TIMEOUT_SECONDS) as time_out:
114 while time_out.check():
115 ln = server_program.stdout.readline().decode('ascii')
116 print("Server:", ln, end='')
117 if "Serving debug probe" in ln:
118 break
119 if ln == '':
120 raise TestError("no more output from server")
121 else:
122 raise TestError("server failed to start")
123
124 server_thread = threading.Thread(target=wait_with_deadline, args=[server_program, TEST_TIMEOUT_SECONDS])
125 server_thread.daemon = True
126 server_thread.start()
127
128 # Start client in a thread.
129 client_args = ['flash',
130 "--frequency=%i" % target_test_params['test_clock'],
131 "--uid=remote:localhost:%d" % test_port,
132 "--target=%s" % target_type,
133 binary_file
134 ]
135 client = PyOCDTool()
136 client._setup_logging = lambda: None # Disable logging setup so we don't have duplicate log output.
137 LOG.info('Starting client: %s', ' '.join(client_args))
138 client_thread = threading.Thread(target=client.run, args=[client_args])
139 client_thread.daemon = True
140 client_thread.start()
141
142 LOG.info('Waiting for client to finish...')
143 client_thread.join(timeout=TEST_TIMEOUT_SECONDS)
144 did_complete = not client_thread.is_alive()
145 if not did_complete:
146 LOG.error("Test timed out!")
147 LOG.info("killing probe server process")
148 server_program.kill()
149 except TestError as err:
150 LOG.info("test failed: %s", err)
151 did_complete = False
152 if server_program.returncode is None:
153 server_program.kill()
154
155 # Read back the result
156 result.passed = did_complete
157
158 if result.passed:
159 print("PROBESERVER TEST PASSED")
160 else:
161 print("PROBESERVER TEST FAILED")
162
163 return result
164
165 if __name__ == "__main__":
166 parser = argparse.ArgumentParser(description='pyOCD probeserver test')
167 parser.add_argument('-u', '--uid', help='Debug probe unique ID')
168 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
169 args = parser.parse_args()
170 level = logging.DEBUG if args.debug else logging.INFO
171 logging.basicConfig(level=level)
172 ensure_output_dir()
173 test_probeserver(args.uid)