16 print_log_names
= False
17 real_stdout
= sys
.stdout
19 # Note that gdb comes with its own testsuite. I was unable to figure out how to
20 # run that testsuite against the spike simulator.
23 for directory
in (os
.getcwd(), os
.path
.dirname(__file__
)):
24 fullpath
= os
.path
.join(directory
, path
)
25 relpath
= os
.path
.relpath(fullpath
)
26 if len(relpath
) >= len(fullpath
):
28 if os
.path
.exists(relpath
):
32 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
33 cc
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
36 cmd
.append("-march=rv32imac")
37 cmd
.append("-mabi=ilp32")
39 cmd
.append("-march=rv64imac")
40 cmd
.append("-mabi=lp64")
42 found
= find_file(arg
)
48 print "+", " ".join(cmd
)
49 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
,
50 stderr
=subprocess
.PIPE
)
51 stdout
, stderr
= process
.communicate()
52 if process
.returncode
:
56 raise Exception("Compile failed!")
59 def __init__(self
, target
, halted
=False, timeout
=None, with_jtag_gdb
=True,
60 isa
=None, progbufsize
=None):
61 """Launch spike. Return tuple of its process and the port it's running
65 self
.progbufsize
= progbufsize
72 cmd
= self
.command(target
, harts
, halted
, timeout
, with_jtag_gdb
)
73 self
.infinite_loop
= target
.compile(harts
[0],
74 "programs/checksum.c", "programs/tiny-malloc.c",
75 "programs/infinite_loop.S", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
76 cmd
.append(self
.infinite_loop
)
77 self
.logfile
= tempfile
.NamedTemporaryFile(prefix
="spike-",
79 self
.logname
= self
.logfile
.name
81 real_stdout
.write("Temporary spike log: %s\n" % self
.logname
)
82 self
.logfile
.write("+ %s\n" % " ".join(cmd
))
84 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
85 stdout
=self
.logfile
, stderr
=self
.logfile
)
90 m
= re
.search(r
"Listening for remote bitbang connection on "
91 r
"port (\d+).", open(self
.logname
).read())
93 self
.port
= int(m
.group(1))
94 os
.environ
['REMOTE_BITBANG_PORT'] = m
.group(1)
98 print_log(self
.logname
)
99 raise Exception("Didn't get spike message about bitbang "
102 def command(self
, target
, harts
, halted
, timeout
, with_jtag_gdb
):
103 # pylint: disable=no-self-use
105 cmd
= shlex
.split(target
.sim_cmd
)
107 spike
= os
.path
.expandvars("$RISCV/bin/spike")
110 cmd
+= ["-p%d" % len(harts
)]
112 assert len(set(t
.xlen
for t
in harts
)) == 1, \
113 "All spike harts must have the same XLEN"
118 isa
= "RV%dG" % harts
[0].xlen
120 cmd
+= ["--isa", isa
]
121 cmd
+= ["--debug-auth"]
123 if not self
.progbufsize
is None:
124 cmd
+= ["--progsize", str(self
.progbufsize
)]
125 cmd
+= ["--debug-sba", "32"]
127 assert len(set(t
.ram
for t
in harts
)) == 1, \
128 "All spike harts must have the same RAM layout"
129 assert len(set(t
.ram_size
for t
in harts
)) == 1, \
130 "All spike harts must have the same RAM layout"
131 cmd
+= ["-m0x%x:0x%x" % (harts
[0].ram
, harts
[0].ram_size
)]
134 cmd
= ["timeout", str(timeout
)] + cmd
139 cmd
+= ['--rbb-port', '0']
140 os
.environ
['REMOTE_BITBANG_HOST'] = 'localhost'
152 def wait(self
, *args
, **kwargs
):
153 return self
.process
.wait(*args
, **kwargs
)
155 class VcsSim(object):
156 logfile
= tempfile
.NamedTemporaryFile(prefix
='simv', suffix
='.log')
157 logname
= logfile
.name
159 def __init__(self
, sim_cmd
=None, debug
=False, timeout
=300):
161 cmd
= shlex
.split(sim_cmd
)
164 cmd
+= ["+jtag_vpi_enable"]
166 cmd
[0] = cmd
[0] + "-debug"
167 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
169 logfile
= open(self
.logname
, "w")
171 real_stdout
.write("Temporary VCS log: %s\n" % self
.logname
)
172 logfile
.write("+ %s\n" % " ".join(cmd
))
175 listenfile
= open(self
.logname
, "r")
176 listenfile
.seek(0, 2)
177 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
178 stdout
=logfile
, stderr
=logfile
)
182 # Fail if VCS exits early
183 exit_code
= self
.process
.poll()
184 if exit_code
is not None:
185 raise RuntimeError('VCS simulator exited early with status %d'
188 line
= listenfile
.readline()
191 match
= re
.match(r
"^Listening on port (\d+)$", line
)
194 self
.port
= int(match
.group(1))
195 os
.environ
['JTAG_VPI_PORT'] = str(self
.port
)
197 if (time
.time() - start
) > timeout
:
198 raise Exception("Timed out waiting for VCS to listen for JTAG "
208 class Openocd(object):
209 logfile
= tempfile
.NamedTemporaryFile(prefix
='openocd', suffix
='.log')
210 logname
= logfile
.name
212 def __init__(self
, server_cmd
=None, config
=None, debug
=False, timeout
=60):
213 self
.timeout
= timeout
216 cmd
= shlex
.split(server_cmd
)
218 openocd
= os
.path
.expandvars("$RISCV/bin/openocd")
223 # This command needs to come before any config scripts on the command
224 # line, since they are executed in order.
226 # Tell OpenOCD to bind gdb to an unused, ephemeral port.
229 # Disable tcl and telnet servers, since they are unused and because
230 # the port numbers will conflict if multiple OpenOCD processes are
231 # running on the same server.
235 "telnet_port disabled",
239 f
= find_file(config
)
241 print "Unable to read file " + config
248 logfile
= open(Openocd
.logname
, "w")
250 real_stdout
.write("Temporary OpenOCD log: %s\n" % Openocd
.logname
)
251 env_entries
= ("REMOTE_BITBANG_HOST", "REMOTE_BITBANG_PORT")
252 env_entries
= [key
for key
in env_entries
if key
in os
.environ
]
253 logfile
.write("+ %s%s\n" % (
254 "".join("%s=%s " % (key
, os
.environ
[key
]) for key
in env_entries
),
255 " ".join(map(pipes
.quote
, cmd
))))
259 self
.process
= self
.start(cmd
, logfile
)
261 def start(self
, cmd
, logfile
):
262 process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
263 stdout
=logfile
, stderr
=logfile
)
266 # Wait for OpenOCD to have made it through riscv_examine(). When
267 # using OpenOCD to communicate with a simulator this may take a
268 # long time, and gdb will time out when trying to connect if we
272 fd
= open(Openocd
.logname
, "r")
276 if not process
.poll() is None:
277 raise Exception("OpenOCD exited early.")
281 m
= re
.search(r
"Listening on port (\d+) for gdb connections",
284 self
.gdb_ports
.append(int(m
.group(1)))
286 if "telnet server disabled" in line
:
289 if not messaged
and time
.time() - start
> 1:
291 print "Waiting for OpenOCD to start..."
292 if (time
.time() - start
) > self
.timeout
:
293 raise Exception("Timed out waiting for OpenOCD to "
297 print_log(Openocd
.logname
)
304 except (OSError, AttributeError):
307 class OpenocdCli(object):
308 def __init__(self
, port
=4444):
309 self
.child
= pexpect
.spawn(
310 "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port
)
311 self
.child
.expect("> ")
313 def command(self
, cmd
):
314 self
.child
.sendline(cmd
)
315 self
.child
.expect(cmd
)
316 self
.child
.expect("\n")
317 self
.child
.expect("> ")
318 return self
.child
.before
.strip("\t\r\n \0")
320 def reg(self
, reg
=''):
321 output
= self
.command("reg %s" % reg
)
322 matches
= re
.findall(r
"(\w+) \(/\d+\): (0x[0-9A-F]+)", output
)
323 values
= {r
: int(v
, 0) for r
, v
in matches
}
328 def load_image(self
, image
):
329 output
= self
.command("load_image %s" % image
)
330 if 'invalid ELF file, only 32bits files are supported' in output
:
331 raise TestNotApplicable(output
)
333 class CannotAccess(Exception):
334 def __init__(self
, address
):
335 Exception.__init
__(self
)
336 self
.address
= address
338 class CouldNotFetch(Exception):
339 def __init__(self
, regname
, explanation
):
340 Exception.__init
__(self
)
341 self
.regname
= regname
342 self
.explanation
= explanation
344 Thread
= collections
.namedtuple('Thread', ('id', 'description', 'target_id',
348 """A single gdb class which can interact with one or more gdb instances."""
350 # pylint: disable=too-many-public-methods
351 # pylint: disable=too-many-instance-attributes
353 def __init__(self
, ports
,
354 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb"),
355 timeout
=60, binary
=None):
360 self
.timeout
= timeout
369 logfile
= tempfile
.NamedTemporaryFile(prefix
="gdb@%d-" % port
,
371 self
.logfiles
.append(logfile
)
373 real_stdout
.write("Temporary gdb log: %s\n" % logfile
.name
)
374 child
= pexpect
.spawn(cmd
)
375 child
.logfile
= logfile
376 child
.logfile
.write("+ %s\n" % cmd
)
377 self
.children
.append(child
)
378 self
.active_child
= self
.children
[0]
381 for port
, child
in zip(self
.ports
, self
.children
):
382 self
.select_child(child
)
384 self
.command("set confirm off")
385 self
.command("set width 0")
386 self
.command("set height 0")
388 self
.command("set print entry-values no")
389 self
.command("set remotetimeout %d" % self
.timeout
)
390 self
.command("target extended-remote localhost:%d" % port
)
392 self
.command("file %s" % self
.binary
)
393 threads
= self
.threads()
397 m
= re
.search(r
"Hart (\d+)", t
.name
)
399 hartid
= int(m
.group(1))
402 hartid
= max(self
.harts
) + 1
405 # solo: True iff this is the only thread on this child
406 self
.harts
[hartid
] = {'child': child
,
408 'solo': len(threads
) == 1}
411 for child
in self
.children
:
414 def one_hart_per_gdb(self
):
415 return all(h
['solo'] for h
in self
.harts
.itervalues())
418 return [logfile
.name
for logfile
in self
.logfiles
]
420 def select_child(self
, child
):
421 self
.active_child
= child
423 def select_hart(self
, hart
):
424 h
= self
.harts
[hart
.id]
425 self
.select_child(h
['child'])
427 output
= self
.command("thread %s" % h
['thread'].id, timeout
=10)
428 assert "Unknown" not in output
430 def push_state(self
):
432 'active_child': self
.active_child
436 state
= self
.stack
.pop()
437 self
.active_child
= state
['active_child']
440 """Wait for prompt."""
441 self
.active_child
.expect(r
"\(gdb\)")
443 def command(self
, command
, timeout
=6000):
444 """timeout is in seconds"""
445 self
.active_child
.sendline(command
)
446 self
.active_child
.expect("\n", timeout
=timeout
)
447 self
.active_child
.expect(r
"\(gdb\)", timeout
=timeout
)
448 return self
.active_child
.before
.strip()
450 def global_command(self
, command
):
451 """Execute this command on every gdb that we control."""
452 with
PrivateState(self
):
453 for child
in self
.children
:
454 self
.select_child(child
)
455 self
.command(command
)
457 def c(self
, wait
=True, timeout
=-1, async=False):
460 In RTOS mode, gdb will resume all harts.
461 In multi-gdb mode, this command will just go to the current gdb, so
462 will only resume one hart.
469 output
= self
.command("c%s" % async, timeout
=timeout
)
470 assert "Continuing" in output
473 self
.active_child
.sendline("c%s" % async)
474 self
.active_child
.expect("Continuing")
480 This function works fine when using multiple gdb sessions, but the
481 caller must be careful when using it nonetheless. gdb's behavior is to
482 not set breakpoints until just before the hart is resumed, and then
483 clears them as soon as the hart halts. That means that you can't set
484 one software breakpoint, and expect multiple harts to hit it. It's
485 possible that the first hart completes set/run/halt/clear before the
486 second hart even gets to resume, so it will never hit the breakpoint.
488 with
PrivateState(self
):
489 for child
in self
.children
:
491 child
.expect("Continuing")
493 # Now wait for them all to halt
494 for child
in self
.children
:
495 child
.expect(r
"\(gdb\)")
498 self
.active_child
.send("\003")
499 self
.active_child
.expect(r
"\(gdb\)", timeout
=6000)
500 return self
.active_child
.before
.strip()
502 def x(self
, address
, size
='w'):
503 output
= self
.command("x/%s %s" % (size
, address
))
504 value
= int(output
.split(':')[1].strip(), 0)
507 def p_raw(self
, obj
):
508 output
= self
.command("p %s" % obj
)
509 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
511 raise CannotAccess(int(m
.group(1), 0))
512 return output
.split('=')[-1].strip()
514 def parse_string(self
, text
):
516 if text
.startswith("{") and text
.endswith("}"):
518 return [self
.parse_string(t
) for t
in inner
.split(", ")]
519 elif text
.startswith('"') and text
.endswith('"'):
524 def p(self
, obj
, fmt
="/x"):
525 output
= self
.command("p%s %s" % (fmt
, obj
))
526 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
528 raise CannotAccess(int(m
.group(1), 0))
529 m
= re
.search(r
"Could not fetch register \"(\w
+)\"; (.*)$
", output)
531 raise CouldNotFetch(m.group(1), m.group(2))
532 rhs = output.split('=')[-1]
533 return self.parse_string(rhs)
535 def p_string(self, obj):
536 output = self.command("p
%s" % obj)
537 value = shlex.split(output.split('=')[-1].strip())[1]
541 output = self.command("stepi
", timeout=60)
545 output = self.command("load
", timeout=6000)
546 assert "failed
" not in output
547 assert "Transfer rate
" in output
549 def b(self, location):
550 output = self.command("b
%s" % location)
551 assert "not defined
" not in output
552 assert "Breakpoint
" in output
555 def hbreak(self, location):
556 output = self.command("hbreak
%s" % location)
557 assert "not defined
" not in output
558 assert "Hardware assisted breakpoint
" in output
562 output = self.command("info threads
")
564 for line in output.splitlines():
567 r"(Remote target|
Thread (\d
+)\s
*\
(Name
: ([^\
)]+))"
570 threads.append(Thread(*m.groups()))
573 #>>> threads.append(Thread('1', '1', 'Default', '???'))
576 def thread(self, thread):
577 return self.command("thread
%s" % thread.id)
580 return self.command("where
1")
582 class PrivateState(object):
583 def __init__(self, gdb):
587 self.gdb.push_state()
589 def __exit__(self, _type, _value, _traceback):
592 def run_all_tests(module, target, parsed):
593 if not os.path.exists(parsed.logs):
594 os.makedirs(parsed.logs)
596 overall_start = time.time()
598 global gdb_cmd # pylint: disable=global-statement
602 examine_added = False
603 for hart in target.harts:
605 hart.misa = int(parsed.misaval, 16)
606 print "Using $misa
from command line
: 0x
%x" % hart.misa
608 print "Using $misa
from hart definition
: 0x
%x" % hart.misa
609 elif not examine_added:
610 todo.append(("ExamineTarget
", ExamineTarget, None))
613 for name in dir(module):
614 definition = getattr(module, name)
615 if isinstance(definition, type) and hasattr(definition, 'test') and \
616 (not parsed.test or any(test in name for test in parsed.test)):
617 todo.append((name, definition, None))
619 results, count = run_tests(parsed, target, todo)
621 header("ran
%d tests
in %.0fs
" % (count, time.time() - overall_start),
624 return print_results(results)
626 good_results = set(('pass', 'not_applicable'))
627 def run_tests(parsed, target, todo):
631 for name, definition, hart in todo:
632 log_name = os.path.join(parsed.logs, "%s-%s-%s.log
" %
633 (time.strftime("%Y
%m
%d-%H
%M
%S
"), type(target).__name__, name))
634 log_fd = open(log_name, 'w')
635 print "[%s] Starting
> %s" % (name, log_name)
636 instance = definition(target, hart)
638 log_fd.write("Test
: %s\n" % name)
639 log_fd.write("Target
: %s\n" % type(target).__name__)
641 global real_stdout # pylint: disable=global-statement
642 real_stdout = sys.stdout
645 result = instance.run()
646 log_fd.write("Result
: %s\n" % result)
647 log_fd.write("Logfile
: %s\n" % log_name)
649 sys.stdout = real_stdout
650 log_fd.write("Time elapsed
: %.2fs
\n" % (time.time() - start))
652 print "[%s] %s in %.2fs
" % (name, result, time.time() - start)
653 if result not in good_results and parsed.print_failures:
654 sys.stdout.write(open(log_name).read())
656 results.setdefault(result, []).append((name, log_name))
658 if result not in good_results and parsed.fail_fast:
661 return results, count
663 def print_results(results):
665 for key, value in results.iteritems():
666 print "%d tests returned
%s" % (len(value), key)
667 if key not in good_results:
669 for name, log_name in value:
670 print " %s > %s" % (name, log_name)
674 def add_test_run_options(parser):
675 parser.add_argument("--logs
", default="logs
",
676 help="Store logs
in the specified directory
.")
677 parser.add_argument("--fail
-fast
", "-f
", action="store_true
",
678 help="Exit
as soon
as any test fails
.")
679 parser.add_argument("--print-failures
", action="store_true
",
680 help="When a test fails
, print the log
file to stdout
.")
681 parser.add_argument("--print-log
-names
", "--pln
", action="store_true
",
682 help="Print names of temporary log files
as soon
as they are
"
684 parser.add_argument("test
", nargs='*',
685 help="Run only tests that are named here
.")
686 parser.add_argument("--gdb
",
687 help="The command to use to start gdb
.")
688 parser.add_argument("--misaval
",
689 help="Don
't run ExamineTarget, just assume the misa value which is "
692 def header(title, dash='-', length=78):
694 dashes = dash * (length - 4 - len(title))
695 before = dashes[:len(dashes)/2]
696 after = dashes[len(dashes)/2:]
697 print "%s[ %s ]%s" % (before, title, after)
703 for l in open(path, "r"):
707 class BaseTest(object):
710 def __init__(self, target, hart=None):
715 self.hart = random.choice(target.harts)
716 self.hart = target.harts[-1] #<<<
718 self.target_process = None
723 def early_applicable(self):
724 """Return a false value if the test has determined it cannot run
725 without ever needing to talk to the target or server."""
726 # pylint: disable=no-self-use
733 compile_args = getattr(self, 'compile_args
', None)
735 if compile_args not in BaseTest.compiled:
736 BaseTest.compiled[compile_args] = \
737 self.target.compile(self.hart, *compile_args)
738 self.binary = BaseTest.compiled.get(compile_args)
740 def classSetup(self):
742 self.target_process = self.target.create()
743 if self.target_process:
744 self.logs.append(self.target_process.logname)
746 self.server = self.target.server()
747 self.logs.append(self.server.logname)
749 for log in self.logs:
753 def classTeardown(self):
755 del self.target_process
757 def postMortem(self):
762 If compile_args is set, compile a program and set self.binary.
766 Then call test() and return the result, displaying relevant information
767 if an exception is raised.
772 if not self.early_applicable():
773 return "not_applicable"
775 self.start = time.time()
780 result = self.test() # pylint: disable=no-member
781 except TestNotApplicable:
782 result = "not_applicable"
783 except Exception as e: # pylint: disable=broad-except
784 if isinstance(e, TestFailed):
788 if isinstance(e, TestFailed):
792 traceback.print_exc(file=sys.stdout)
795 except Exception as e: # pylint: disable=broad-except
796 header("postMortem Exception")
798 traceback.print_exc(file=sys.stdout)
802 for log in self.logs:
804 header("End of logs")
812 class GdbTest(BaseTest):
813 def __init__(self, target, hart=None):
814 BaseTest.__init__(self, target, hart=hart)
817 def classSetup(self):
818 BaseTest.classSetup(self)
821 self.gdb = Gdb(self.server.gdb_ports, gdb_cmd,
822 timeout=self.target.timeout_sec, binary=self.binary)
824 self.gdb = Gdb(self.server.gdb_ports,
825 timeout=self.target.timeout_sec, binary=self.binary)
827 self.logs += self.gdb.lognames()
830 self.gdb.global_command("set remotetimeout %d" %
831 self.target.timeout_sec)
833 for cmd in self.target.gdb_setup:
834 self.gdb.command(cmd)
836 self.gdb.select_hart(self.hart)
838 # FIXME: OpenOCD doesn't handle PRIV now
839 #self.gdb.p("$priv=3")
841 def postMortem(self
):
845 self
.gdb
.command("disassemble")
846 self
.gdb
.command("info registers all", timeout
=10)
848 def classTeardown(self
):
850 BaseTest
.classTeardown(self
)
852 class GdbSingleHartTest(GdbTest
):
853 def classSetup(self
):
854 GdbTest
.classSetup(self
)
856 for hart
in self
.target
.harts
:
857 # Park all harts that we're not using in a safe place.
858 if hart
!= self
.hart
:
859 self
.gdb
.select_hart(hart
)
860 self
.gdb
.p("$pc=loop_forever")
861 self
.gdb
.select_hart(self
.hart
)
863 class ExamineTarget(GdbTest
):
865 for hart
in self
.target
.harts
:
866 self
.gdb
.select_hart(hart
)
868 hart
.misa
= self
.gdb
.p("$misa")
872 if ((hart
.misa
& 0xFFFFFFFF) >> 30) == 1:
874 elif ((hart
.misa
& 0xFFFFFFFFFFFFFFFF) >> 62) == 2:
876 elif ((hart
.misa
& 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) >> 126) == 3:
879 raise TestFailed("Couldn't determine XLEN from $misa (0x%x)" %
882 if misa_xlen
!= hart
.xlen
:
883 raise TestFailed("MISA reported XLEN of %d but we were "\
884 "expecting XLEN of %d\n" % (misa_xlen
, hart
.xlen
))
886 txt
+= ("%d" % misa_xlen
)
889 if hart
.misa
& (1<<i
):
890 txt
+= chr(i
+ ord('A'))
893 class TestFailed(Exception):
894 def __init__(self
, message
):
895 Exception.__init
__(self
)
896 self
.message
= message
898 class TestNotApplicable(Exception):
899 def __init__(self
, message
):
900 Exception.__init
__(self
)
901 self
.message
= message
903 def assertEqual(a
, b
):
905 raise TestFailed("%r != %r" % (a
, b
))
907 def assertNotEqual(a
, b
):
909 raise TestFailed("%r == %r" % (a
, b
))
913 raise TestFailed("%r not in %r" % (a
, b
))
915 def assertNotIn(a
, b
):
917 raise TestFailed("%r in %r" % (a
, b
))
919 def assertGreater(a
, b
):
921 raise TestFailed("%r not greater than %r" % (a
, b
))
923 def assertLess(a
, b
):
925 raise TestFailed("%r not less than %r" % (a
, b
))
929 raise TestFailed("%r is not True" % a
)
931 def assertRegexpMatches(text
, regexp
):
932 if not re
.search(regexp
, text
):
933 raise TestFailed("can't find %r in %r" % (regexp
, text
))