0bdb433a1f36301e7896bb2e36d774fb05d56749
14 # Note that gdb comes with its own testsuite. I was unable to figure out how to
15 # run that testsuite against the spike simulator.
18 for directory
in (os
.getcwd(), os
.path
.dirname(__file__
)):
19 fullpath
= os
.path
.join(directory
, path
)
20 relpath
= os
.path
.relpath(fullpath
)
21 if len(relpath
) >= len(fullpath
):
23 if os
.path
.exists(relpath
):
27 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
28 cc
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
31 cmd
.append("-march=rv32imac")
32 cmd
.append("-mabi=ilp32")
34 cmd
.append("-march=rv64imac")
35 cmd
.append("-mabi=lp64")
37 found
= find_file(arg
)
43 print "+", " ".join(cmd
)
44 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
,
45 stderr
=subprocess
.PIPE
)
46 stdout
, stderr
= process
.communicate()
47 if process
.returncode
:
51 raise Exception("Compile failed!")
54 logname
= "spike-%d.log" % os
.getpid()
56 def __init__(self
, target
, halted
=False, timeout
=None, with_jtag_gdb
=True):
57 """Launch spike. Return tuple of its process and the port it's running
66 cmd
= self
.command(target
, harts
, halted
, timeout
, with_jtag_gdb
)
67 self
.infinite_loop
= target
.compile(harts
[0],
68 "programs/checksum.c", "programs/tiny-malloc.c",
69 "programs/infinite_loop.S", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
70 cmd
.append(self
.infinite_loop
)
71 logfile
= open(self
.logname
, "w")
72 logfile
.write("+ %s\n" % " ".join(cmd
))
74 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
75 stdout
=logfile
, stderr
=logfile
)
80 m
= re
.search(r
"Listening for remote bitbang connection on "
81 r
"port (\d+).", open(self
.logname
).read())
83 self
.port
= int(m
.group(1))
84 os
.environ
['REMOTE_BITBANG_PORT'] = m
.group(1)
88 print_log(self
.logname
)
89 raise Exception("Didn't get spike message about bitbang "
92 def command(self
, target
, harts
, halted
, timeout
, with_jtag_gdb
):
93 # pylint: disable=no-self-use
95 cmd
= shlex
.split(target
.sim_cmd
)
97 spike
= os
.path
.expandvars("$RISCV/bin/spike")
100 cmd
+= ["-p%d" % len(harts
)]
102 assert len(set(t
.xlen
for t
in harts
)) == 1, \
103 "All spike harts must have the same XLEN"
105 if harts
[0].xlen
== 32:
106 cmd
+= ["--isa", "RV32G"]
108 cmd
+= ["--isa", "RV64G"]
110 assert len(set(t
.ram
for t
in harts
)) == 1, \
111 "All spike harts must have the same RAM layout"
112 assert len(set(t
.ram_size
for t
in harts
)) == 1, \
113 "All spike harts must have the same RAM layout"
114 cmd
+= ["-m0x%x:0x%x" % (harts
[0].ram
, harts
[0].ram_size
)]
117 cmd
= ["timeout", str(timeout
)] + cmd
122 cmd
+= ['--rbb-port', '0']
123 os
.environ
['REMOTE_BITBANG_HOST'] = 'localhost'
135 def wait(self
, *args
, **kwargs
):
136 return self
.process
.wait(*args
, **kwargs
)
138 class VcsSim(object):
141 def __init__(self
, sim_cmd
=None, debug
=False):
143 cmd
= shlex
.split(sim_cmd
)
146 cmd
+= ["+jtag_vpi_enable"]
148 cmd
[0] = cmd
[0] + "-debug"
149 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
150 logfile
= open(self
.logname
, "w")
151 logfile
.write("+ %s\n" % " ".join(cmd
))
153 listenfile
= open(self
.logname
, "r")
154 listenfile
.seek(0, 2)
155 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
156 stdout
=logfile
, stderr
=logfile
)
159 # Fail if VCS exits early
160 exit_code
= self
.process
.poll()
161 if exit_code
is not None:
162 raise RuntimeError('VCS simulator exited early with status %d'
165 line
= listenfile
.readline()
168 match
= re
.match(r
"^Listening on port (\d+)$", line
)
171 self
.port
= int(match
.group(1))
172 os
.environ
['JTAG_VPI_PORT'] = str(self
.port
)
181 class Openocd(object):
182 logfile
= tempfile
.NamedTemporaryFile(prefix
='openocd', suffix
='.log')
183 logname
= logfile
.name
184 print "OpenOCD Temporary Log File: %s" % logname
186 def __init__(self
, server_cmd
=None, config
=None, debug
=False, timeout
=60):
187 self
.timeout
= timeout
190 cmd
= shlex
.split(server_cmd
)
192 openocd
= os
.path
.expandvars("$RISCV/bin/openocd")
197 # This command needs to come before any config scripts on the command
198 # line, since they are executed in order.
200 # Tell OpenOCD to bind gdb to an unused, ephemeral port.
203 # Disable tcl and telnet servers, since they are unused and because
204 # the port numbers will conflict if multiple OpenOCD processes are
205 # running on the same server.
209 "telnet_port disabled",
213 f
= find_file(config
)
215 print "Unable to read file " + config
222 logfile
= open(Openocd
.logname
, "w")
223 logfile
.write("+ %s\n" % " ".join(cmd
))
228 self
.process
= self
.start(cmd
, logfile
)
230 def start(self
, cmd
, logfile
):
231 process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
232 stdout
=logfile
, stderr
=logfile
)
235 # Wait for OpenOCD to have made it through riscv_examine(). When
236 # using OpenOCD to communicate with a simulator this may take a
237 # long time, and gdb will time out when trying to connect if we
242 log
= open(Openocd
.logname
).read()
243 m
= re
.search(r
"Listening on port (\d+) for gdb connections",
247 self
.port
= int(m
.group(1))
248 self
.ports
.append(int(m
.group(1)))
250 if "telnet server disabled" in log
:
253 if not process
.poll() is None:
255 "OpenOCD exited before completing riscv_examine()")
256 if not messaged
and time
.time() - start
> 1:
258 print "Waiting for OpenOCD to start..."
259 if (time
.time() - start
) > self
.timeout
:
260 raise Exception("ERROR: Timed out waiting for OpenOCD to "
264 header("OpenOCD log")
265 sys
.stdout
.write(log
)
272 except (OSError, AttributeError):
275 class OpenocdCli(object):
276 def __init__(self
, port
=4444):
277 self
.child
= pexpect
.spawn(
278 "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port
)
279 self
.child
.expect("> ")
281 def command(self
, cmd
):
282 self
.child
.sendline(cmd
)
283 self
.child
.expect(cmd
)
284 self
.child
.expect("\n")
285 self
.child
.expect("> ")
286 return self
.child
.before
.strip("\t\r\n \0")
288 def reg(self
, reg
=''):
289 output
= self
.command("reg %s" % reg
)
290 matches
= re
.findall(r
"(\w+) \(/\d+\): (0x[0-9A-F]+)", output
)
291 values
= {r
: int(v
, 0) for r
, v
in matches
}
296 def load_image(self
, image
):
297 output
= self
.command("load_image %s" % image
)
298 if 'invalid ELF file, only 32bits files are supported' in output
:
299 raise TestNotApplicable(output
)
301 class CannotAccess(Exception):
302 def __init__(self
, address
):
303 Exception.__init
__(self
)
304 self
.address
= address
306 Thread
= collections
.namedtuple('Thread', ('id', 'target_id', 'name',
310 logfile
= tempfile
.NamedTemporaryFile(prefix
="gdb", suffix
=".log")
311 logname
= logfile
.name
312 print "GDB Temporary Log File: %s" % logname
315 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
316 self
.child
= pexpect
.spawn(cmd
)
317 self
.child
.logfile
= open(self
.logname
, "w")
318 self
.child
.logfile
.write("+ %s\n" % cmd
)
320 self
.command("set confirm off")
321 self
.command("set width 0")
322 self
.command("set height 0")
324 self
.command("set print entry-values no")
326 def select_hart(self
, hart
):
327 output
= self
.command("thread %d" % (hart
.index
+ 1))
328 assert "Unknown" not in output
331 """Wait for prompt."""
332 self
.child
.expect(r
"\(gdb\)")
334 def command(self
, command
, timeout
=6000):
335 """timeout is in seconds"""
336 self
.child
.sendline(command
)
337 self
.child
.expect("\n", timeout
=timeout
)
338 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
339 return self
.child
.before
.strip()
341 def c(self
, wait
=True, timeout
=-1, async=False):
347 output
= self
.command("c%s" % async, timeout
=timeout
)
348 assert "Continuing" in output
351 self
.child
.sendline("c%s" % async)
352 self
.child
.expect("Continuing")
355 self
.child
.send("\003")
356 self
.child
.expect(r
"\(gdb\)", timeout
=6000)
357 return self
.child
.before
.strip()
359 def x(self
, address
, size
='w'):
360 output
= self
.command("x/%s %s" % (size
, address
))
361 value
= int(output
.split(':')[1].strip(), 0)
364 def p_raw(self
, obj
):
365 output
= self
.command("p %s" % obj
)
366 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
368 raise CannotAccess(int(m
.group(1), 0))
369 return output
.split('=')[-1].strip()
371 def parse_string(self
, text
):
373 if text
.startswith("{") and text
.endswith("}"):
375 return [self
.parse_string(t
) for t
in inner
.split(", ")]
376 elif text
.startswith('"') and text
.endswith('"'):
381 def p(self
, obj
, fmt
="/x"):
382 output
= self
.command("p%s %s" % (fmt
, obj
))
383 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
385 raise CannotAccess(int(m
.group(1), 0))
386 rhs
= output
.split('=')[-1]
387 return self
.parse_string(rhs
)
389 def p_string(self
, obj
):
390 output
= self
.command("p %s" % obj
)
391 value
= shlex
.split(output
.split('=')[-1].strip())[1]
395 output
= self
.command("stepi", timeout
=60)
399 output
= self
.command("load", timeout
=6000)
400 assert "failed" not in output
401 assert "Transfer rate" in output
403 def b(self
, location
):
404 output
= self
.command("b %s" % location
)
405 assert "not defined" not in output
406 assert "Breakpoint" in output
409 def hbreak(self
, location
):
410 output
= self
.command("hbreak %s" % location
)
411 assert "not defined" not in output
412 assert "Hardware assisted breakpoint" in output
416 output
= self
.command("info threads")
418 for line
in output
.splitlines():
420 r
"[\s\*]*(\d+)\s*Thread (\d+)\s*\(Name: ([^\)]+)\s*(.*)",
423 threads
.append(Thread(*m
.groups()))
425 threads
.append(Thread('1', '1', 'Default', '???'))
428 def thread(self
, thread
):
429 return self
.command("thread %s" % thread
.id)
431 def run_all_tests(module
, target
, parsed
):
432 if not os
.path
.exists(parsed
.logs
):
433 os
.makedirs(parsed
.logs
)
435 overall_start
= time
.time()
437 global gdb_cmd
# pylint: disable=global-statement
441 for hart
in target
.harts
:
443 hart
.misa
= int(parsed
.misaval
, 16)
444 print "Using $misa from command line: 0x%x" % hart
.misa
446 print "Using $misa from hart definition: 0x%x" % hart
.misa
448 todo
.append(("ExamineTarget", ExamineTarget
, hart
))
450 for name
in dir(module
):
451 definition
= getattr(module
, name
)
452 if type(definition
) == type and hasattr(definition
, 'test') and \
453 (not parsed
.test
or any(test
in name
for test
in parsed
.test
)):
454 todo
.append((name
, definition
, None))
456 results
, count
= run_tests(parsed
, target
, todo
)
458 header("ran %d tests in %.0fs" % (count
, time
.time() - overall_start
),
461 return print_results(results
)
463 good_results
= set(('pass', 'not_applicable'))
464 def run_tests(parsed
, target
, todo
):
468 for name
, definition
, hart
in todo
:
469 log_name
= os
.path
.join(parsed
.logs
, "%s-%s-%s.log" %
470 (time
.strftime("%Y%m%d-%H%M%S"), type(target
).__name
__, name
))
471 log_fd
= open(log_name
, 'w')
472 print "Running %s > %s ..." % (name
, log_name
),
473 instance
= definition(target
, hart
)
475 log_fd
.write("Test: %s\n" % name
)
476 log_fd
.write("Target: %s\n" % type(target
).__name
__)
478 real_stdout
= sys
.stdout
481 result
= instance
.run()
482 log_fd
.write("Result: %s\n" % result
)
484 sys
.stdout
= real_stdout
485 log_fd
.write("Time elapsed: %.2fs\n" % (time
.time() - start
))
486 print "%s in %.2fs" % (result
, time
.time() - start
)
487 if result
not in good_results
and parsed
.print_failures
:
488 sys
.stdout
.write(open(log_name
).read())
490 results
.setdefault(result
, []).append((name
, log_name
))
492 if result
not in good_results
and parsed
.fail_fast
:
495 return results
, count
497 def print_results(results
):
499 for key
, value
in results
.iteritems():
500 print "%d tests returned %s" % (len(value
), key
)
501 if key
not in good_results
:
503 for name
, log_name
in value
:
504 print " %s > %s" % (name
, log_name
)
508 def add_test_run_options(parser
):
509 parser
.add_argument("--logs", default
="logs",
510 help="Store logs in the specified directory.")
511 parser
.add_argument("--fail-fast", "-f", action
="store_true",
512 help="Exit as soon as any test fails.")
513 parser
.add_argument("--print-failures", action
="store_true",
514 help="When a test fails, print the log file to stdout.")
515 parser
.add_argument("test", nargs
='*',
516 help="Run only tests that are named here.")
517 parser
.add_argument("--gdb",
518 help="The command to use to start gdb.")
519 parser
.add_argument("--misaval",
520 help="Don't run ExamineTarget, just assume the misa value which is "
523 def header(title
, dash
='-', length
=78):
525 dashes
= dash
* (length
- 4 - len(title
))
526 before
= dashes
[:len(dashes
)/2]
527 after
= dashes
[len(dashes
)/2:]
528 print "%s[ %s ]%s" % (before
, title
, after
)
534 for l
in open(path
, "r"):
538 class BaseTest(object):
541 def __init__(self
, target
, hart
=None):
546 self
.hart
= random
.choice(target
.harts
)
547 self
.hart
= target
.harts
[-1] #<<<
549 self
.target_process
= None
554 def early_applicable(self
):
555 """Return a false value if the test has determined it cannot run
556 without ever needing to talk to the target or server."""
557 # pylint: disable=no-self-use
564 compile_args
= getattr(self
, 'compile_args', None)
566 if compile_args
not in BaseTest
.compiled
:
567 # pylint: disable=star-args
568 BaseTest
.compiled
[compile_args
] = \
569 self
.target
.compile(self
.hart
, *compile_args
)
570 self
.binary
= BaseTest
.compiled
.get(compile_args
)
572 def classSetup(self
):
574 self
.target_process
= self
.target
.create()
575 if self
.target_process
:
576 self
.logs
.append(self
.target_process
.logname
)
578 self
.server
= self
.target
.server()
579 self
.logs
.append(self
.server
.logname
)
581 for log
in self
.logs
:
585 def classTeardown(self
):
587 del self
.target_process
589 def postMortem(self
):
594 If compile_args is set, compile a program and set self.binary.
598 Then call test() and return the result, displaying relevant information
599 if an exception is raised.
604 if not self
.early_applicable():
605 return "not_applicable"
607 self
.start
= time
.time()
612 result
= self
.test() # pylint: disable=no-member
613 except TestNotApplicable
:
614 result
= "not_applicable"
615 except Exception as e
: # pylint: disable=broad-except
616 if isinstance(e
, TestFailed
):
620 if isinstance(e
, TestFailed
):
624 traceback
.print_exc(file=sys
.stdout
)
629 for log
in self
.logs
:
631 header("End of logs")
639 class GdbTest(BaseTest
):
640 def __init__(self
, target
, hart
=None):
641 BaseTest
.__init
__(self
, target
, hart
=hart
)
644 def classSetup(self
):
645 BaseTest
.classSetup(self
)
648 self
.gdb
= Gdb(gdb_cmd
)
652 self
.logs
.append(self
.gdb
.logname
)
655 self
.gdb
.command("file %s" % self
.binary
)
657 self
.gdb
.command("set arch riscv:rv%d" % self
.hart
.xlen
)
658 self
.gdb
.command("set remotetimeout %d" % self
.target
.timeout_sec
)
661 "target extended-remote localhost:%d" % self
.server
.port
)
662 self
.gdb
.select_hart(self
.hart
)
664 for cmd
in self
.target
.gdb_setup
:
665 self
.gdb
.command(cmd
)
667 # FIXME: OpenOCD doesn't handle PRIV now
668 #self.gdb.p("$priv=3")
670 def postMortem(self
):
674 self
.gdb
.command("info registers all", timeout
=10)
676 def classTeardown(self
):
678 BaseTest
.classTeardown(self
)
680 class GdbSingleHartTest(GdbTest
):
681 def classSetup(self
):
682 GdbTest
.classSetup(self
)
684 for hart
in self
.target
.harts
:
685 # Park all harts that we're not using in a safe place.
686 if hart
!= self
.hart
:
687 self
.gdb
.select_hart(hart
)
688 self
.gdb
.p("$pc=loop_forever")
689 self
.gdb
.select_hart(self
.hart
)
691 class ExamineTarget(GdbTest
):
693 self
.hart
.misa
= self
.gdb
.p("$misa")
696 if (self
.hart
.misa
>> 30) == 1:
698 elif (self
.hart
.misa
>> 62) == 2:
700 elif (self
.hart
.misa
>> 126) == 3:
703 raise TestFailed("Couldn't determine XLEN from $misa (0x%x)" %
707 if self
.hart
.misa
& (1<<i
):
708 txt
+= chr(i
+ ord('A'))
711 class TestFailed(Exception):
712 def __init__(self
, message
):
713 Exception.__init
__(self
)
714 self
.message
= message
716 class TestNotApplicable(Exception):
717 def __init__(self
, message
):
718 Exception.__init
__(self
)
719 self
.message
= message
721 def assertEqual(a
, b
):
723 raise TestFailed("%r != %r" % (a
, b
))
725 def assertNotEqual(a
, b
):
727 raise TestFailed("%r == %r" % (a
, b
))
731 raise TestFailed("%r not in %r" % (a
, b
))
733 def assertNotIn(a
, b
):
735 raise TestFailed("%r in %r" % (a
, b
))
737 def assertGreater(a
, b
):
739 raise TestFailed("%r not greater than %r" % (a
, b
))
741 def assertLess(a
, b
):
743 raise TestFailed("%r not less than %r" % (a
, b
))
747 raise TestFailed("%r is not True" % a
)
749 def assertRegexpMatches(text
, regexp
):
750 if not re
.search(regexp
, text
):
751 raise TestFailed("can't find %r in %r" % (regexp
, text
))