12 # Note that gdb comes with its own testsuite. I was unable to figure out how to
13 # run that testsuite against the spike simulator.
16 for directory
in (os
.getcwd(), os
.path
.dirname(__file__
)):
17 fullpath
= os
.path
.join(directory
, path
)
18 if os
.path
.exists(fullpath
):
22 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
23 cc
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
26 cmd
.append("-march=rv32imac")
27 cmd
.append("-mabi=ilp32")
29 cmd
.append("-march=rv64imac")
30 cmd
.append("-mabi=lp64")
32 found
= find_file(arg
)
37 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
,
38 stderr
=subprocess
.PIPE
)
39 stdout
, stderr
= process
.communicate()
40 if process
.returncode
:
42 header("Compile failed")
43 print "+", " ".join(cmd
)
47 raise Exception("Compile failed!")
50 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
52 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
54 port
= s
.getsockname()[1]
59 logname
= "spike-%d.log" % os
.getpid()
61 def __init__(self
, target
, halted
=False, timeout
=None, with_jtag_gdb
=True):
62 """Launch spike. Return tuple of its process and the port it's running
65 cmd
= shlex
.split(target
.sim_cmd
)
67 spike
= os
.path
.expandvars("$RISCV/bin/spike")
70 cmd
+= ["--isa", "RV32G"]
72 cmd
+= ["--isa", "RV64G"]
73 cmd
+= ["-m0x%x:0x%x" % (target
.ram
, target
.ram_size
)]
76 cmd
= ["timeout", str(timeout
)] + cmd
81 cmd
+= ['--rbb-port', '0']
82 os
.environ
['REMOTE_BITBANG_HOST'] = 'localhost'
83 self
.infinite_loop
= target
.compile(
84 "programs/checksum.c", "programs/tiny-malloc.c",
85 "programs/infinite_loop.c", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
86 cmd
.append(self
.infinite_loop
)
87 logfile
= open(self
.logname
, "w")
88 logfile
.write("+ %s\n" % " ".join(cmd
))
90 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
91 stdout
=logfile
, stderr
=logfile
)
96 m
= re
.search(r
"Listening for remote bitbang connection on "
97 r
"port (\d+).", open(self
.logname
).read())
99 self
.port
= int(m
.group(1))
100 os
.environ
['REMOTE_BITBANG_PORT'] = m
.group(1)
103 assert self
.port
, "Didn't get spike message about bitbang " \
113 def wait(self
, *args
, **kwargs
):
114 return self
.process
.wait(*args
, **kwargs
)
116 class VcsSim(object):
117 def __init__(self
, sim_cmd
=None, debug
=False):
119 cmd
= shlex
.split(sim_cmd
)
122 cmd
+= ["+jtag_vpi_enable"]
124 cmd
[0] = cmd
[0] + "-debug"
125 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
126 logfile
= open("simv.log", "w")
127 logfile
.write("+ %s\n" % " ".join(cmd
))
129 listenfile
= open("simv.log", "r")
130 listenfile
.seek(0, 2)
131 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
132 stdout
=logfile
, stderr
=logfile
)
135 # Fail if VCS exits early
136 exit_code
= self
.process
.poll()
137 if exit_code
is not None:
138 raise RuntimeError('VCS simulator exited early with status %d'
141 line
= listenfile
.readline()
144 match
= re
.match(r
"^Listening on port (\d+)$", line
)
147 self
.port
= int(match
.group(1))
148 os
.environ
['JTAG_VPI_PORT'] = str(self
.port
)
157 class Openocd(object):
158 logfile
= tempfile
.NamedTemporaryFile(prefix
='openocd', suffix
='.log')
159 logname
= logfile
.name
161 def __init__(self
, server_cmd
=None, config
=None, debug
=False):
163 cmd
= shlex
.split(server_cmd
)
165 openocd
= os
.path
.expandvars("$RISCV/bin/openocd")
170 # This command needs to come before any config scripts on the command
171 # line, since they are executed in order.
173 # Tell OpenOCD to bind gdb to an unused, ephemeral port.
176 # Disable tcl and telnet servers, since they are unused and because
177 # the port numbers will conflict if multiple OpenOCD processes are
178 # running on the same server.
182 "telnet_port disabled",
186 f
= find_file(config
)
188 print "Unable to read file " + config
195 logfile
= open(Openocd
.logname
, "w")
196 logfile
.write("+ %s\n" % " ".join(cmd
))
198 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
199 stdout
=logfile
, stderr
=logfile
)
201 # Wait for OpenOCD to have made it through riscv_examine(). When using
202 # OpenOCD to communicate with a simulator this may take a long time,
203 # and gdb will time out when trying to connect if we attempt too early.
207 log
= open(Openocd
.logname
).read()
208 if "Ready for Remote Connections" in log
:
210 if not self
.process
.poll() is None:
212 "OpenOCD exited before completing riscv_examine()")
213 if not messaged
and time
.time() - start
> 1:
215 print "Waiting for OpenOCD to examine RISCV core..."
216 if time
.time() - start
> 60:
217 raise Exception("ERROR: Timed out waiting for OpenOCD to "
218 "examine RISCV core")
220 self
.port
= self
._get
_gdb
_server
_port
()
222 def _get_gdb_server_port(self
):
223 """Get port that OpenOCD's gdb server is listening on."""
225 PORT_REGEX
= re
.compile(r
'(?P<port>\d+) \(LISTEN\)')
226 for _
in range(MAX_ATTEMPTS
):
227 with
open(os
.devnull
, 'w') as devnull
:
229 output
= subprocess
.check_output([
231 '-a', # Take the AND of the following selectors
232 '-p{}'.format(self
.process
.pid
), # Filter on PID
233 '-iTCP', # Filter only TCP sockets
235 except subprocess
.CalledProcessError
:
237 matches
= list(PORT_REGEX
.finditer(output
))
238 matches
= [m
for m
in matches
239 if m
.group('port') not in ('6666', '4444')]
243 "OpenOCD listening on multiple ports. Cannot uniquely "
244 "identify gdb server port.")
247 return int(match
.group('port'))
249 raise Exception("Timed out waiting for gdb server to obtain port.")
258 class OpenocdCli(object):
259 def __init__(self
, port
=4444):
260 self
.child
= pexpect
.spawn(
261 "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port
)
262 self
.child
.expect("> ")
264 def command(self
, cmd
):
265 self
.child
.sendline(cmd
)
266 self
.child
.expect(cmd
)
267 self
.child
.expect("\n")
268 self
.child
.expect("> ")
269 return self
.child
.before
.strip("\t\r\n \0")
271 def reg(self
, reg
=''):
272 output
= self
.command("reg %s" % reg
)
273 matches
= re
.findall(r
"(\w+) \(/\d+\): (0x[0-9A-F]+)", output
)
274 values
= {r
: int(v
, 0) for r
, v
in matches
}
279 def load_image(self
, image
):
280 output
= self
.command("load_image %s" % image
)
281 if 'invalid ELF file, only 32bits files are supported' in output
:
282 raise TestNotApplicable(output
)
284 class CannotAccess(Exception):
285 def __init__(self
, address
):
286 Exception.__init
__(self
)
287 self
.address
= address
290 logfile
= tempfile
.NamedTemporaryFile(prefix
="gdb", suffix
=".log")
291 logname
= logfile
.name
294 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
295 self
.child
= pexpect
.spawn(cmd
)
296 self
.child
.logfile
= open(self
.logname
, "w")
297 self
.child
.logfile
.write("+ %s\n" % cmd
)
299 self
.command("set confirm off")
300 self
.command("set width 0")
301 self
.command("set height 0")
303 self
.command("set print entry-values no")
306 """Wait for prompt."""
307 self
.child
.expect(r
"\(gdb\)")
309 def command(self
, command
, timeout
=6000):
310 self
.child
.sendline(command
)
311 self
.child
.expect("\n", timeout
=timeout
)
312 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
313 return self
.child
.before
.strip()
315 def c(self
, wait
=True, timeout
=-1):
317 output
= self
.command("c", timeout
=timeout
)
318 assert "Continuing" in output
321 self
.child
.sendline("c")
322 self
.child
.expect("Continuing")
325 self
.child
.send("\003")
326 self
.child
.expect(r
"\(gdb\)", timeout
=6000)
327 return self
.child
.before
.strip()
329 def x(self
, address
, size
='w'):
330 output
= self
.command("x/%s %s" % (size
, address
))
331 value
= int(output
.split(':')[1].strip(), 0)
334 def p_raw(self
, obj
):
335 output
= self
.command("p %s" % obj
)
336 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
338 raise CannotAccess(int(m
.group(1), 0))
339 return output
.split('=')[-1].strip()
342 output
= self
.command("p/x %s" % obj
)
343 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
345 raise CannotAccess(int(m
.group(1), 0))
346 value
= int(output
.split('=')[-1].strip(), 0)
349 def p_string(self
, obj
):
350 output
= self
.command("p %s" % obj
)
351 value
= shlex
.split(output
.split('=')[-1].strip())[1]
355 output
= self
.command("stepi")
359 output
= self
.command("load", timeout
=6000)
360 assert "failed" not in output
361 assert "Transfer rate" in output
363 def b(self
, location
):
364 output
= self
.command("b %s" % location
)
365 assert "not defined" not in output
366 assert "Breakpoint" in output
369 def hbreak(self
, location
):
370 output
= self
.command("hbreak %s" % location
)
371 assert "not defined" not in output
372 assert "Hardware assisted breakpoint" in output
375 def run_all_tests(module
, target
, parsed
):
376 if not os
.path
.exists(parsed
.logs
):
377 os
.makedirs(parsed
.logs
)
379 overall_start
= time
.time()
381 global gdb_cmd
# pylint: disable=global-statement
386 target
.misa
= int(parsed
.misaval
, 16)
387 print "Assuming $MISA value of 0x%x. Skipping ExamineTarget." % \
390 todo
.append(("ExamineTarget", ExamineTarget
))
392 for name
in dir(module
):
393 definition
= getattr(module
, name
)
394 if type(definition
) == type and hasattr(definition
, 'test') and \
395 (not parsed
.test
or any(test
in name
for test
in parsed
.test
)):
396 todo
.append((name
, definition
))
398 results
, count
= run_tests(parsed
, target
, todo
)
400 header("ran %d tests in %.0fs" % (count
, time
.time() - overall_start
),
403 return print_results(results
)
405 good_results
= set(('pass', 'not_applicable'))
406 def run_tests(parsed
, target
, todo
):
410 for name
, definition
in todo
:
411 instance
= definition(target
)
412 log_name
= os
.path
.join(parsed
.logs
, "%s-%s-%s.log" %
413 (time
.strftime("%Y%m%d-%H%M%S"), type(target
).__name
__, name
))
414 log_fd
= open(log_name
, 'w')
415 print "Running", name
, "...",
417 log_fd
.write("Test: %s\n" % name
)
418 log_fd
.write("Target: %s\n" % type(target
).__name
__)
420 real_stdout
= sys
.stdout
423 result
= instance
.run()
425 sys
.stdout
= real_stdout
426 print "%s in %.2fs" % (result
, time
.time() - start
)
428 log_fd
.write("Result: %s\n" % result
)
429 log_fd
.write("Time elapsed: %.2fs\n" % (time
.time() - start
))
430 results
.setdefault(result
, []).append(name
)
432 if result
not in good_results
and parsed
.fail_fast
:
435 return results
, count
437 def print_results(results
):
439 for key
, value
in results
.iteritems():
440 print "%d tests returned %s" % (len(value
), key
)
441 if key
not in good_results
:
448 def add_test_run_options(parser
):
450 parser
.add_argument("--logs", default
="logs",
451 help="Store logs in the specified directory.")
452 parser
.add_argument("--fail-fast", "-f", action
="store_true",
453 help="Exit as soon as any test fails.")
454 parser
.add_argument("test", nargs
='*',
455 help="Run only tests that are named here.")
456 parser
.add_argument("--gdb",
457 help="The command to use to start gdb.")
458 parser
.add_argument("--misaval",
459 help="Don't run ExamineTarget, just assume the misa value which is "
462 def header(title
, dash
='-', length
=78):
464 dashes
= dash
* (length
- 4 - len(title
))
465 before
= dashes
[:len(dashes
)/2]
466 after
= dashes
[len(dashes
)/2:]
467 print "%s[ %s ]%s" % (before
, title
, after
)
473 lines
= open(path
, "r").readlines()
478 class BaseTest(object):
481 def __init__(self
, target
):
484 self
.target_process
= None
489 def early_applicable(self
):
490 """Return a false value if the test has determined it cannot run
491 without ever needing to talk to the target or server."""
492 # pylint: disable=no-self-use
499 compile_args
= getattr(self
, 'compile_args', None)
501 if compile_args
not in BaseTest
.compiled
:
502 # pylint: disable=star-args
503 BaseTest
.compiled
[compile_args
] = \
504 self
.target
.compile(*compile_args
)
505 self
.binary
= BaseTest
.compiled
.get(compile_args
)
507 def classSetup(self
):
509 self
.target_process
= self
.target
.target()
510 self
.server
= self
.target
.server()
511 self
.logs
.append(self
.server
.logname
)
513 def classTeardown(self
):
515 del self
.target_process
519 If compile_args is set, compile a program and set self.binary.
523 Then call test() and return the result, displaying relevant information
524 if an exception is raised.
529 if not self
.early_applicable():
530 return "not_applicable"
532 self
.start
= time
.time()
538 result
= self
.test() # pylint: disable=no-member
539 except TestNotApplicable
:
540 result
= "not_applicable"
541 except Exception as e
: # pylint: disable=broad-except
542 if isinstance(e
, TestFailed
):
546 if isinstance(e
, TestFailed
):
550 traceback
.print_exc(file=sys
.stdout
)
554 for log
in self
.logs
:
556 header("End of logs")
564 class GdbTest(BaseTest
):
565 def __init__(self
, target
):
566 BaseTest
.__init
__(self
, target
)
569 def classSetup(self
):
570 BaseTest
.classSetup(self
)
573 self
.gdb
= Gdb(gdb_cmd
)
577 self
.logs
.append(self
.gdb
.logname
)
580 self
.gdb
.command("file %s" % self
.binary
)
582 self
.gdb
.command("set arch riscv:rv%d" % self
.target
.xlen
)
583 self
.gdb
.command("set remotetimeout %d" % self
.target
.timeout_sec
)
586 "target extended-remote localhost:%d" % self
.server
.port
)
587 # Force gdb to discover threads now, otherwise it might interrupt
588 # us at some point when it decides by itself to check.
589 self
.gdb
.command("info threads")
591 # FIXME: OpenOCD doesn't handle PRIV now
592 #self.gdb.p("$priv=3")
594 def classTeardown(self
):
596 BaseTest
.classTeardown(self
)
598 class ExamineTarget(GdbTest
):
600 self
.target
.misa
= self
.gdb
.p("$misa")
603 if (self
.target
.misa
>> 30) == 1:
605 elif (self
.target
.misa
>> 62) == 2:
607 elif (self
.target
.misa
>> 126) == 3:
610 raise TestFailed("Couldn't determine XLEN from $misa (0x%x)" %
614 if self
.target
.misa
& (1<<i
):
615 txt
+= chr(i
+ ord('A'))
618 class TestFailed(Exception):
619 def __init__(self
, message
):
620 Exception.__init
__(self
)
621 self
.message
= message
623 class TestNotApplicable(Exception):
624 def __init__(self
, message
):
625 Exception.__init
__(self
)
626 self
.message
= message
628 def assertEqual(a
, b
):
630 raise TestFailed("%r != %r" % (a
, b
))
632 def assertNotEqual(a
, b
):
634 raise TestFailed("%r == %r" % (a
, b
))
638 raise TestFailed("%r not in %r" % (a
, b
))
640 def assertNotIn(a
, b
):
642 raise TestFailed("%r in %r" % (a
, b
))
644 def assertGreater(a
, b
):
646 raise TestFailed("%r not greater than %r" % (a
, b
))
648 def assertLess(a
, b
):
650 raise TestFailed("%r not less than %r" % (a
, b
))
654 raise TestFailed("%r is not True" % a
)
656 def assertRegexpMatches(text
, regexp
):
657 if not re
.search(regexp
, text
):
658 raise TestFailed("can't find %r in %r" % (regexp
, text
))