11 # Note that gdb comes with its own testsuite. I was unable to figure out how to
12 # run that testsuite against the spike simulator.
15 for directory
in (os
.getcwd(), os
.path
.dirname(__file__
)):
16 fullpath
= os
.path
.join(directory
, path
)
17 if os
.path
.exists(fullpath
):
21 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
22 cc
= os
.path
.expandvars("$RISCV/bin/riscv%d-unknown-elf-gcc" % xlen
)
25 found
= find_file(arg
)
31 result
= os
.system(cmd
)
32 assert result
== 0, "%r failed" % cmd
35 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
37 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
39 port
= s
.getsockname()[1]
46 def __init__(self
, cmd
, binary
=None, halted
=False, with_gdb
=True,
47 timeout
=None, xlen
=64):
48 """Launch spike. Return tuple of its process and the port it's running
51 cmd
= shlex
.split(cmd
)
55 cmd
+= ["--isa", "RV32"]
58 cmd
= ["timeout", str(timeout
)] + cmd
63 self
.port
= unused_port()
64 cmd
+= ['--gdb-port', str(self
.port
)]
69 logfile
= open(self
.logname
, "w")
70 logfile
.write("+ %s\n" % " ".join(cmd
))
72 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
73 stdout
=logfile
, stderr
=logfile
)
82 def wait(self
, *args
, **kwargs
):
83 return self
.process
.wait(*args
, **kwargs
)
86 def __init__(self
, simv
=None, debug
=False):
88 cmd
= shlex
.split(simv
)
91 cmd
+= ["+jtag_vpi_enable"]
93 cmd
[0] = cmd
[0] + "-debug"
94 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
95 logfile
= open("simv.log", "w")
96 logfile
.write("+ %s\n" % " ".join(cmd
))
98 listenfile
= open("simv.log", "r")
100 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
101 stdout
=logfile
, stderr
=logfile
)
104 line
= listenfile
.readline()
107 match
= re
.match(r
"^Listening on port (\d+)$", line
)
110 self
.port
= int(match
.group(1))
111 os
.environ
['JTAG_VPI_PORT'] = str(self
.port
)
120 class Openocd(object):
121 logname
= "openocd.log"
123 def __init__(self
, cmd
=None, config
=None, debug
=False):
125 cmd
= shlex
.split(cmd
)
129 cmd
+= ["-f", find_file(config
)]
133 # This command needs to come before any config scripts on the command
134 # line, since they are executed in order.
135 # Tell OpenOCD to bind to an unused port.
136 cmd
[1:1] = ["--command", "gdb_port %d" % 0]
138 logfile
= open(Openocd
.logname
, "w")
139 logfile
.write("+ %s\n" % " ".join(cmd
))
141 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
142 stdout
=logfile
, stderr
=logfile
)
144 # Wait for OpenOCD to have made it through riscv_examine(). When using
145 # OpenOCD to communicate with a simulator this may take a long time,
146 # and gdb will time out when trying to connect if we attempt too early.
150 log
= open(Openocd
.logname
).read()
151 if "Examined RISCV core" in log
:
153 if not self
.process
.poll() is None:
155 "OpenOCD exited before completing riscv_examine()")
156 if not messaged
and time
.time() - start
> 1:
158 print "Waiting for OpenOCD to examine RISCV core..."
160 self
.port
= self
._get
_gdb
_server
_port
()
162 def _get_gdb_server_port(self
):
163 """Get port that OpenOCD's gdb server is listening on."""
165 PORT_REGEX
= re
.compile(r
'(?P<port>\d+) \(LISTEN\)')
166 for _
in range(MAX_ATTEMPTS
):
167 with
open(os
.devnull
, 'w') as devnull
:
169 output
= subprocess
.check_output([
171 '-a', # Take the AND of the following selectors
172 '-p{}'.format(self
.process
.pid
), # Filter on PID
173 '-iTCP', # Filter only TCP sockets
175 except subprocess
.CalledProcessError
:
177 matches
= list(PORT_REGEX
.finditer(output
))
178 matches
= [m
for m
in matches
if m
.group('port') not in ('6666', '4444')]
182 "OpenOCD listening on multiple ports. Cannot uniquely "
183 "identify gdb server port.")
186 return int(match
.group('port'))
188 raise Exception("Timed out waiting for gdb server to obtain port.")
197 class OpenocdCli(object):
198 def __init__(self
, port
=4444):
199 self
.child
= pexpect
.spawn(
200 "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port
)
201 self
.child
.expect("> ")
203 def command(self
, cmd
):
204 self
.child
.sendline(cmd
)
205 self
.child
.expect(cmd
)
206 self
.child
.expect("\n")
207 self
.child
.expect("> ")
208 return self
.child
.before
.strip("\t\r\n \0")
210 def reg(self
, reg
=''):
211 output
= self
.command("reg %s" % reg
)
212 matches
= re
.findall(r
"(\w+) \(/\d+\): (0x[0-9A-F]+)", output
)
213 values
= {r
: int(v
, 0) for r
, v
in matches
}
218 def load_image(self
, image
):
219 output
= self
.command("load_image %s" % image
)
220 if 'invalid ELF file, only 32bits files are supported' in output
:
221 raise TestNotApplicable(output
)
223 class CannotAccess(Exception):
224 def __init__(self
, address
):
225 Exception.__init
__(self
)
226 self
.address
= address
230 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
231 self
.child
= pexpect
.spawn(cmd
)
232 self
.child
.logfile
= open("gdb.log", "w")
233 self
.child
.logfile
.write("+ %s\n" % cmd
)
235 self
.command("set confirm off")
236 self
.command("set width 0")
237 self
.command("set height 0")
239 self
.command("set print entry-values no")
242 """Wait for prompt."""
243 self
.child
.expect(r
"\(gdb\)")
245 def command(self
, command
, timeout
=-1):
246 self
.child
.sendline(command
)
247 self
.child
.expect("\n", timeout
=timeout
)
248 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
249 return self
.child
.before
.strip()
251 def c(self
, wait
=True):
253 output
= self
.command("c")
254 assert "Continuing" in output
257 self
.child
.sendline("c")
258 self
.child
.expect("Continuing")
261 self
.child
.send("\003")
262 self
.child
.expect(r
"\(gdb\)", timeout
=60)
263 return self
.child
.before
.strip()
265 def x(self
, address
, size
='w'):
266 output
= self
.command("x/%s %s" % (size
, address
))
267 value
= int(output
.split(':')[1].strip(), 0)
270 def p_raw(self
, obj
):
271 output
= self
.command("p %s" % obj
)
272 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
274 raise CannotAccess(int(m
.group(1), 0))
275 return output
.split('=')[-1].strip()
278 output
= self
.command("p/x %s" % obj
)
279 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
281 raise CannotAccess(int(m
.group(1), 0))
282 value
= int(output
.split('=')[-1].strip(), 0)
285 def p_string(self
, obj
):
286 output
= self
.command("p %s" % obj
)
287 value
= shlex
.split(output
.split('=')[-1].strip())[1]
291 output
= self
.command("stepi")
295 output
= self
.command("load", timeout
=60)
296 assert "failed" not in output
297 assert "Transfer rate" in output
299 def b(self
, location
):
300 output
= self
.command("b %s" % location
)
301 assert "not defined" not in output
302 assert "Breakpoint" in output
305 def hbreak(self
, location
):
306 output
= self
.command("hbreak %s" % location
)
307 assert "not defined" not in output
308 assert "Hardware assisted breakpoint" in output
311 def run_all_tests(module
, target
, tests
, fail_fast
):
312 good_results
= set(('pass', 'not_applicable'))
318 for name
in dir(module
):
319 definition
= getattr(module
, name
)
320 if type(definition
) == type and hasattr(definition
, 'test') and \
321 (not tests
or any(test
in name
for test
in tests
)):
322 instance
= definition(target
)
323 result
= instance
.run()
324 results
.setdefault(result
, []).append(name
)
326 if result
not in good_results
and fail_fast
:
329 header("ran %d tests in %.0fs" % (count
, time
.time() - start
), dash
=':')
332 for key
, value
in results
.iteritems():
333 print "%d tests returned %s" % (len(value
), key
)
334 if key
not in good_results
:
341 def add_test_run_options(parser
):
342 parser
.add_argument("--fail-fast", "-f", action
="store_true",
343 help="Exit as soon as any test fails.")
344 parser
.add_argument("test", nargs
='*',
345 help="Run only tests that are named here.")
347 def header(title
, dash
='-'):
348 dashes
= dash
* (36 - len(title
))
349 before
= dashes
[:len(dashes
)/2]
350 after
= dashes
[len(dashes
)/2:]
351 print "%s[ %s ]%s" % (before
, title
, after
)
353 class BaseTest(object):
357 def __init__(self
, target
):
360 self
.target_process
= None
364 def early_applicable(self
):
365 """Return a false value if the test has determined it cannot run
366 without ever needing to talk to the target or server."""
367 # pylint: disable=no-self-use
374 compile_args
= getattr(self
, 'compile_args', None)
376 if compile_args
not in BaseTest
.compiled
:
377 # pylint: disable=star-args
378 BaseTest
.compiled
[compile_args
] = \
379 self
.target
.compile(*compile_args
)
380 self
.binary
= BaseTest
.compiled
.get(compile_args
)
382 def classSetup(self
):
384 self
.target_process
= self
.target
.target()
385 self
.server
= self
.target
.server()
386 self
.logs
.append(self
.server
.logname
)
388 def classTeardown(self
):
390 del self
.target_process
394 If compile_args is set, compile a program and set self.binary.
398 Then call test() and return the result, displaying relevant information
399 if an exception is raised.
402 print "Running", type(self
).__name
__, "...",
405 if not self
.early_applicable():
406 print "not_applicable"
407 return "not_applicable"
409 self
.start
= time
.time()
415 result
= self
.test() # pylint: disable=no-member
416 except TestNotApplicable
:
417 result
= "not_applicable"
418 except Exception as e
: # pylint: disable=broad-except
419 if isinstance(e
, TestFailed
):
423 print "%s in %.2fs" % (result
, time
.time() - self
.start
)
425 if isinstance(e
, TestFailed
):
429 traceback
.print_exc(file=sys
.stdout
)
430 for log
in self
.logs
:
432 print open(log
, "r").read()
441 print "%s in %.2fs" % (result
, time
.time() - self
.start
)
444 class TestFailed(Exception):
445 def __init__(self
, message
):
446 Exception.__init
__(self
)
447 self
.message
= message
449 class TestNotApplicable(Exception):
450 def __init__(self
, message
):
451 Exception.__init
__(self
)
452 self
.message
= message
454 def assertEqual(a
, b
):
456 raise TestFailed("%r != %r" % (a
, b
))
458 def assertNotEqual(a
, b
):
460 raise TestFailed("%r == %r" % (a
, b
))
464 raise TestFailed("%r not in %r" % (a
, b
))
466 def assertNotIn(a
, b
):
468 raise TestFailed("%r in %r" % (a
, b
))
470 def assertGreater(a
, b
):
472 raise TestFailed("%r not greater than %r" % (a
, b
))
474 def assertLess(a
, b
):
476 raise TestFailed("%r not less than %r" % (a
, b
))
480 raise TestFailed("%r is not True" % a
)
482 def assertRegexpMatches(text
, regexp
):
483 if not re
.search(regexp
, text
):
484 raise TestFailed("can't find %r in %r" % (regexp
, text
))