13 # Note that gdb comes with its own testsuite. I was unable to figure out how to
14 # run that testsuite against the spike simulator.
17 for directory
in (os
.getcwd(), os
.path
.dirname(__file__
)):
18 fullpath
= os
.path
.join(directory
, path
)
19 if os
.path
.exists(fullpath
):
23 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
24 cc
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
27 cmd
.append("-march=rv32imac")
28 cmd
.append("-mabi=ilp32")
30 cmd
.append("-march=rv64imac")
31 cmd
.append("-mabi=lp64")
33 found
= find_file(arg
)
38 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
,
39 stderr
=subprocess
.PIPE
)
40 stdout
, stderr
= process
.communicate()
41 if process
.returncode
:
43 header("Compile failed")
44 print "+", " ".join(cmd
)
48 raise Exception("Compile failed!")
51 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
53 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
55 port
= s
.getsockname()[1]
60 logname
= "spike-%d.log" % os
.getpid()
62 def __init__(self
, target
, halted
=False, timeout
=None, with_jtag_gdb
=True):
63 """Launch spike. Return tuple of its process and the port it's running
66 cmd
= shlex
.split(target
.sim_cmd
)
68 spike
= os
.path
.expandvars("$RISCV/bin/spike")
71 cmd
+= ["--isa", "RV32G"]
73 cmd
+= ["--isa", "RV64G"]
74 cmd
+= ["-m0x%x:0x%x" % (target
.ram
, target
.ram_size
)]
77 cmd
= ["timeout", str(timeout
)] + cmd
82 cmd
+= ['--rbb-port', '0']
83 os
.environ
['REMOTE_BITBANG_HOST'] = 'localhost'
84 self
.infinite_loop
= target
.compile(
85 "programs/checksum.c", "programs/tiny-malloc.c",
86 "programs/infinite_loop.S", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
87 cmd
.append(self
.infinite_loop
)
88 logfile
= open(self
.logname
, "w")
89 logfile
.write("+ %s\n" % " ".join(cmd
))
91 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
92 stdout
=logfile
, stderr
=logfile
)
97 m
= re
.search(r
"Listening for remote bitbang connection on "
98 r
"port (\d+).", open(self
.logname
).read())
100 self
.port
= int(m
.group(1))
101 os
.environ
['REMOTE_BITBANG_PORT'] = m
.group(1)
104 assert self
.port
, "Didn't get spike message about bitbang " \
114 def wait(self
, *args
, **kwargs
):
115 return self
.process
.wait(*args
, **kwargs
)
117 class VcsSim(object):
118 def __init__(self
, sim_cmd
=None, debug
=False):
120 cmd
= shlex
.split(sim_cmd
)
123 cmd
+= ["+jtag_vpi_enable"]
125 cmd
[0] = cmd
[0] + "-debug"
126 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
127 logfile
= open("simv.log", "w")
128 logfile
.write("+ %s\n" % " ".join(cmd
))
130 listenfile
= open("simv.log", "r")
131 listenfile
.seek(0, 2)
132 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
133 stdout
=logfile
, stderr
=logfile
)
136 # Fail if VCS exits early
137 exit_code
= self
.process
.poll()
138 if exit_code
is not None:
139 raise RuntimeError('VCS simulator exited early with status %d'
142 line
= listenfile
.readline()
145 match
= re
.match(r
"^Listening on port (\d+)$", line
)
148 self
.port
= int(match
.group(1))
149 os
.environ
['JTAG_VPI_PORT'] = str(self
.port
)
158 class Openocd(object):
159 logfile
= tempfile
.NamedTemporaryFile(prefix
='openocd', suffix
='.log')
160 logname
= logfile
.name
162 def __init__(self
, server_cmd
=None, config
=None, debug
=False):
164 cmd
= shlex
.split(server_cmd
)
166 openocd
= os
.path
.expandvars("$RISCV/bin/openocd")
171 # This command needs to come before any config scripts on the command
172 # line, since they are executed in order.
174 # Tell OpenOCD to bind gdb to an unused, ephemeral port.
177 # Disable tcl and telnet servers, since they are unused and because
178 # the port numbers will conflict if multiple OpenOCD processes are
179 # running on the same server.
183 "telnet_port disabled",
187 f
= find_file(config
)
189 print "Unable to read file " + config
196 logfile
= open(Openocd
.logname
, "w")
197 logfile
.write("+ %s\n" % " ".join(cmd
))
199 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
200 stdout
=logfile
, stderr
=logfile
)
202 # Wait for OpenOCD to have made it through riscv_examine(). When using
203 # OpenOCD to communicate with a simulator this may take a long time,
204 # and gdb will time out when trying to connect if we attempt too early.
208 log
= open(Openocd
.logname
).read()
209 if "Ready for Remote Connections" in log
:
211 if not self
.process
.poll() is None:
212 header("OpenOCD log")
213 sys
.stdout
.write(log
)
215 "OpenOCD exited before completing riscv_examine()")
216 if not messaged
and time
.time() - start
> 1:
218 print "Waiting for OpenOCD to examine RISCV core..."
219 if time
.time() - start
> 60:
220 raise Exception("ERROR: Timed out waiting for OpenOCD to "
221 "examine RISCV core")
224 self
.port
= self
._get
_gdb
_server
_port
()
226 header("OpenOCD log")
227 sys
.stdout
.write(log
)
230 def _get_gdb_server_port(self
):
231 """Get port that OpenOCD's gdb server is listening on."""
233 PORT_REGEX
= re
.compile(r
'(?P<port>\d+) \(LISTEN\)')
234 for _
in range(MAX_ATTEMPTS
):
235 with
open(os
.devnull
, 'w') as devnull
:
237 output
= subprocess
.check_output([
239 '-a', # Take the AND of the following selectors
240 '-p{}'.format(self
.process
.pid
), # Filter on PID
241 '-iTCP', # Filter only TCP sockets
243 except subprocess
.CalledProcessError
:
245 matches
= list(PORT_REGEX
.finditer(output
))
246 matches
= [m
for m
in matches
247 if m
.group('port') not in ('6666', '4444')]
251 "OpenOCD listening on multiple ports. Cannot uniquely "
252 "identify gdb server port.")
255 return int(match
.group('port'))
257 raise Exception("Timed out waiting for gdb server to obtain port.")
266 class OpenocdCli(object):
267 def __init__(self
, port
=4444):
268 self
.child
= pexpect
.spawn(
269 "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port
)
270 self
.child
.expect("> ")
272 def command(self
, cmd
):
273 self
.child
.sendline(cmd
)
274 self
.child
.expect(cmd
)
275 self
.child
.expect("\n")
276 self
.child
.expect("> ")
277 return self
.child
.before
.strip("\t\r\n \0")
279 def reg(self
, reg
=''):
280 output
= self
.command("reg %s" % reg
)
281 matches
= re
.findall(r
"(\w+) \(/\d+\): (0x[0-9A-F]+)", output
)
282 values
= {r
: int(v
, 0) for r
, v
in matches
}
287 def load_image(self
, image
):
288 output
= self
.command("load_image %s" % image
)
289 if 'invalid ELF file, only 32bits files are supported' in output
:
290 raise TestNotApplicable(output
)
292 class CannotAccess(Exception):
293 def __init__(self
, address
):
294 Exception.__init
__(self
)
295 self
.address
= address
298 logfile
= tempfile
.NamedTemporaryFile(prefix
="gdb", suffix
=".log")
299 logname
= logfile
.name
302 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
303 self
.child
= pexpect
.spawn(cmd
)
304 self
.child
.logfile
= open(self
.logname
, "w")
305 self
.child
.logfile
.write("+ %s\n" % cmd
)
307 self
.command("set confirm off")
308 self
.command("set width 0")
309 self
.command("set height 0")
311 self
.command("set print entry-values no")
314 """Wait for prompt."""
315 self
.child
.expect(r
"\(gdb\)")
317 def command(self
, command
, timeout
=6000):
318 self
.child
.sendline(command
)
319 self
.child
.expect("\n", timeout
=timeout
)
320 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
321 return self
.child
.before
.strip()
323 def c(self
, wait
=True, timeout
=-1):
325 output
= self
.command("c", timeout
=timeout
)
326 assert "Continuing" in output
329 self
.child
.sendline("c")
330 self
.child
.expect("Continuing")
333 self
.child
.send("\003")
334 self
.child
.expect(r
"\(gdb\)", timeout
=6000)
335 return self
.child
.before
.strip()
337 def x(self
, address
, size
='w'):
338 output
= self
.command("x/%s %s" % (size
, address
))
339 value
= int(output
.split(':')[1].strip(), 0)
342 def p_raw(self
, obj
):
343 output
= self
.command("p %s" % obj
)
344 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
346 raise CannotAccess(int(m
.group(1), 0))
347 return output
.split('=')[-1].strip()
350 output
= self
.command("p/x %s" % obj
)
351 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
353 raise CannotAccess(int(m
.group(1), 0))
354 value
= int(output
.split('=')[-1].strip(), 0)
357 def p_string(self
, obj
):
358 output
= self
.command("p %s" % obj
)
359 value
= shlex
.split(output
.split('=')[-1].strip())[1]
363 output
= self
.command("stepi")
367 output
= self
.command("load", timeout
=6000)
368 assert "failed" not in output
369 assert "Transfer rate" in output
371 def b(self
, location
):
372 output
= self
.command("b %s" % location
)
373 assert "not defined" not in output
374 assert "Breakpoint" in output
377 def hbreak(self
, location
):
378 output
= self
.command("hbreak %s" % location
)
379 assert "not defined" not in output
380 assert "Hardware assisted breakpoint" in output
383 def run_all_tests(module
, target
, parsed
):
384 if not os
.path
.exists(parsed
.logs
):
385 os
.makedirs(parsed
.logs
)
387 overall_start
= time
.time()
389 global gdb_cmd
# pylint: disable=global-statement
394 target
.misa
= int(parsed
.misaval
, 16)
395 print "Assuming $MISA value of 0x%x. Skipping ExamineTarget." % \
398 todo
.append(("ExamineTarget", ExamineTarget
))
400 for name
in dir(module
):
401 definition
= getattr(module
, name
)
402 if type(definition
) == type and hasattr(definition
, 'test') and \
403 (not parsed
.test
or any(test
in name
for test
in parsed
.test
)):
404 todo
.append((name
, definition
))
406 results
, count
= run_tests(parsed
, target
, todo
)
408 header("ran %d tests in %.0fs" % (count
, time
.time() - overall_start
),
411 return print_results(results
)
413 good_results
= set(('pass', 'not_applicable'))
414 def run_tests(parsed
, target
, todo
):
418 for name
, definition
in todo
:
419 instance
= definition(target
)
420 log_name
= os
.path
.join(parsed
.logs
, "%s-%s-%s.log" %
421 (time
.strftime("%Y%m%d-%H%M%S"), type(target
).__name
__, name
))
422 log_fd
= open(log_name
, 'w')
423 print "Running", name
, "...",
425 log_fd
.write("Test: %s\n" % name
)
426 log_fd
.write("Target: %s\n" % type(target
).__name
__)
428 real_stdout
= sys
.stdout
431 result
= instance
.run()
432 log_fd
.write("Result: %s\n" % result
)
434 sys
.stdout
= real_stdout
435 log_fd
.write("Time elapsed: %.2fs\n" % (time
.time() - start
))
436 print "%s in %.2fs" % (result
, time
.time() - start
)
438 results
.setdefault(result
, []).append(name
)
440 if result
not in good_results
and parsed
.fail_fast
:
443 return results
, count
445 def print_results(results
):
447 for key
, value
in results
.iteritems():
448 print "%d tests returned %s" % (len(value
), key
)
449 if key
not in good_results
:
456 def add_test_run_options(parser
):
458 parser
.add_argument("--logs", default
="logs",
459 help="Store logs in the specified directory.")
460 parser
.add_argument("--fail-fast", "-f", action
="store_true",
461 help="Exit as soon as any test fails.")
462 parser
.add_argument("test", nargs
='*',
463 help="Run only tests that are named here.")
464 parser
.add_argument("--gdb",
465 help="The command to use to start gdb.")
466 parser
.add_argument("--misaval",
467 help="Don't run ExamineTarget, just assume the misa value which is "
470 def header(title
, dash
='-', length
=78):
472 dashes
= dash
* (length
- 4 - len(title
))
473 before
= dashes
[:len(dashes
)/2]
474 after
= dashes
[len(dashes
)/2:]
475 print "%s[ %s ]%s" % (before
, title
, after
)
481 lines
= open(path
, "r").readlines()
486 class BaseTest(object):
489 def __init__(self
, target
):
492 self
.target_process
= None
497 def early_applicable(self
):
498 """Return a false value if the test has determined it cannot run
499 without ever needing to talk to the target or server."""
500 # pylint: disable=no-self-use
507 compile_args
= getattr(self
, 'compile_args', None)
509 if compile_args
not in BaseTest
.compiled
:
510 # pylint: disable=star-args
511 BaseTest
.compiled
[compile_args
] = \
512 self
.target
.compile(*compile_args
)
513 self
.binary
= BaseTest
.compiled
.get(compile_args
)
515 def classSetup(self
):
517 self
.target_process
= self
.target
.target()
518 self
.server
= self
.target
.server()
519 self
.logs
.append(self
.server
.logname
)
521 def classTeardown(self
):
523 del self
.target_process
527 If compile_args is set, compile a program and set self.binary.
531 Then call test() and return the result, displaying relevant information
532 if an exception is raised.
537 if not self
.early_applicable():
538 return "not_applicable"
540 self
.start
= time
.time()
546 result
= self
.test() # pylint: disable=no-member
547 except TestNotApplicable
:
548 result
= "not_applicable"
549 except Exception as e
: # pylint: disable=broad-except
550 if isinstance(e
, TestFailed
):
554 if isinstance(e
, TestFailed
):
558 traceback
.print_exc(file=sys
.stdout
)
562 for log
in self
.logs
:
564 header("End of logs")
572 class GdbTest(BaseTest
):
573 def __init__(self
, target
):
574 BaseTest
.__init
__(self
, target
)
577 def classSetup(self
):
578 BaseTest
.classSetup(self
)
581 self
.gdb
= Gdb(gdb_cmd
)
585 self
.logs
.append(self
.gdb
.logname
)
588 self
.gdb
.command("file %s" % self
.binary
)
590 self
.gdb
.command("set arch riscv:rv%d" % self
.target
.xlen
)
591 self
.gdb
.command("set remotetimeout %d" % self
.target
.timeout_sec
)
594 "target extended-remote localhost:%d" % self
.server
.port
)
595 # Select a random thread.
596 # TODO: Allow a command line option to force a specific thread.
597 output
= self
.gdb
.command("info threads")
598 threads
= re
.findall(r
"Thread (\d+)", output
)
600 thread
= random
.choice(threads
)
601 self
.gdb
.command("thread %s" % thread
)
603 # FIXME: OpenOCD doesn't handle PRIV now
604 #self.gdb.p("$priv=3")
606 def classTeardown(self
):
608 BaseTest
.classTeardown(self
)
610 class ExamineTarget(GdbTest
):
612 self
.target
.misa
= self
.gdb
.p("$misa")
615 if (self
.target
.misa
>> 30) == 1:
617 elif (self
.target
.misa
>> 62) == 2:
619 elif (self
.target
.misa
>> 126) == 3:
622 raise TestFailed("Couldn't determine XLEN from $misa (0x%x)" %
626 if self
.target
.misa
& (1<<i
):
627 txt
+= chr(i
+ ord('A'))
630 class TestFailed(Exception):
631 def __init__(self
, message
):
632 Exception.__init
__(self
)
633 self
.message
= message
635 class TestNotApplicable(Exception):
636 def __init__(self
, message
):
637 Exception.__init
__(self
)
638 self
.message
= message
640 def assertEqual(a
, b
):
642 raise TestFailed("%r != %r" % (a
, b
))
644 def assertNotEqual(a
, b
):
646 raise TestFailed("%r == %r" % (a
, b
))
650 raise TestFailed("%r not in %r" % (a
, b
))
652 def assertNotIn(a
, b
):
654 raise TestFailed("%r in %r" % (a
, b
))
656 def assertGreater(a
, b
):
658 raise TestFailed("%r not greater than %r" % (a
, b
))
660 def assertLess(a
, b
):
662 raise TestFailed("%r not less than %r" % (a
, b
))
666 raise TestFailed("%r is not True" % a
)
668 def assertRegexpMatches(text
, regexp
):
669 if not re
.search(regexp
, text
):
670 raise TestFailed("can't find %r in %r" % (regexp
, text
))