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
)]
134 self
.port
= unused_port()
135 # This command needs to come before any config scripts on the command
136 # line, since they are executed in order.
137 cmd
[1:1] = ["--command", "gdb_port %d" % self
.port
]
139 logfile
= open(Openocd
.logname
, "w")
140 logfile
.write("+ %s\n" % " ".join(cmd
))
142 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
143 stdout
=logfile
, stderr
=logfile
)
145 # Wait for OpenOCD to have made it through riscv_examine(). When using
146 # OpenOCD to communicate with a simulator this may take a long time,
147 # and gdb will time out when trying to connect if we attempt too early.
151 log
= open(Openocd
.logname
).read()
152 if "Examined RISCV core" in log
:
154 if not self
.process
.poll() is None:
156 "OpenOCD exited before completing riscv_examine()")
157 if not messaged
and time
.time() - start
> 1:
159 print "Waiting for OpenOCD to examine RISCV core..."
168 class OpenocdCli(object):
169 def __init__(self
, port
=4444):
170 self
.child
= pexpect
.spawn(
171 "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port
)
172 self
.child
.expect("> ")
174 def command(self
, cmd
):
175 self
.child
.sendline(cmd
)
176 self
.child
.expect(cmd
)
177 self
.child
.expect("\n")
178 self
.child
.expect("> ")
179 return self
.child
.before
.strip("\t\r\n \0")
181 def reg(self
, reg
=''):
182 output
= self
.command("reg %s" % reg
)
183 matches
= re
.findall(r
"(\w+) \(/\d+\): (0x[0-9A-F]+)", output
)
184 values
= {r
: int(v
, 0) for r
, v
in matches
}
189 def load_image(self
, image
):
190 output
= self
.command("load_image %s" % image
)
191 if 'invalid ELF file, only 32bits files are supported' in output
:
192 raise TestNotApplicable(output
)
194 class CannotAccess(Exception):
195 def __init__(self
, address
):
196 Exception.__init
__(self
)
197 self
.address
= address
201 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
202 self
.child
= pexpect
.spawn(cmd
)
203 self
.child
.logfile
= open("gdb.log", "w")
204 self
.child
.logfile
.write("+ %s\n" % cmd
)
206 self
.command("set confirm off")
207 self
.command("set width 0")
208 self
.command("set height 0")
210 self
.command("set print entry-values no")
213 """Wait for prompt."""
214 self
.child
.expect(r
"\(gdb\)")
216 def command(self
, command
, timeout
=-1):
217 self
.child
.sendline(command
)
218 self
.child
.expect("\n", timeout
=timeout
)
219 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
220 return self
.child
.before
.strip()
222 def c(self
, wait
=True):
224 output
= self
.command("c")
225 assert "Continuing" in output
228 self
.child
.sendline("c")
229 self
.child
.expect("Continuing")
232 self
.child
.send("\003")
233 self
.child
.expect(r
"\(gdb\)", timeout
=60)
234 return self
.child
.before
.strip()
236 def x(self
, address
, size
='w'):
237 output
= self
.command("x/%s %s" % (size
, address
))
238 value
= int(output
.split(':')[1].strip(), 0)
242 output
= self
.command("p/x %s" % obj
)
243 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
245 raise CannotAccess(int(m
.group(1), 0))
246 value
= int(output
.split('=')[-1].strip(), 0)
249 def p_string(self
, obj
):
250 output
= self
.command("p %s" % obj
)
251 value
= shlex
.split(output
.split('=')[-1].strip())[1]
255 output
= self
.command("stepi")
259 output
= self
.command("load", timeout
=60)
260 assert "failed" not in output
261 assert "Transfer rate" in output
263 def b(self
, location
):
264 output
= self
.command("b %s" % location
)
265 assert "not defined" not in output
266 assert "Breakpoint" in output
269 def hbreak(self
, location
):
270 output
= self
.command("hbreak %s" % location
)
271 assert "not defined" not in output
272 assert "Hardware assisted breakpoint" in output
275 def run_all_tests(module
, target
, tests
, fail_fast
):
276 good_results
= set(('pass', 'not_applicable'))
282 for name
in dir(module
):
283 definition
= getattr(module
, name
)
284 if type(definition
) == type and hasattr(definition
, 'test') and \
285 (not tests
or any(test
in name
for test
in tests
)):
286 instance
= definition(target
)
287 result
= instance
.run()
288 results
.setdefault(result
, []).append(name
)
290 if result
not in good_results
and fail_fast
:
293 header("ran %d tests in %.0fs" % (count
, time
.time() - start
), dash
=':')
296 for key
, value
in results
.iteritems():
297 print "%d tests returned %s" % (len(value
), key
)
298 if key
not in good_results
:
305 def add_test_run_options(parser
):
306 parser
.add_argument("--fail-fast", "-f", action
="store_true",
307 help="Exit as soon as any test fails.")
308 parser
.add_argument("test", nargs
='*',
309 help="Run only tests that are named here.")
311 def header(title
, dash
='-'):
312 dashes
= dash
* (36 - len(title
))
313 before
= dashes
[:len(dashes
)/2]
314 after
= dashes
[len(dashes
)/2:]
315 print "%s[ %s ]%s" % (before
, title
, after
)
317 class BaseTest(object):
321 def __init__(self
, target
):
324 self
.target_process
= None
328 def early_applicable(self
):
329 """Return a false value if the test has determined it cannot run
330 without ever needing to talk to the target or server."""
331 # pylint: disable=no-self-use
338 compile_args
= getattr(self
, 'compile_args', None)
340 if compile_args
not in BaseTest
.compiled
:
342 # pylint: disable=star-args
343 BaseTest
.compiled
[compile_args
] = \
344 self
.target
.compile(*compile_args
)
345 except Exception: # pylint: disable=broad-except
346 print "exception while compiling in %.2fs" % (
347 time
.time() - self
.start
)
350 traceback
.print_exc(file=sys
.stdout
)
353 self
.binary
= BaseTest
.compiled
.get(compile_args
)
355 def classSetup(self
):
357 self
.target_process
= self
.target
.target()
358 self
.server
= self
.target
.server()
359 self
.logs
.append(self
.server
.logname
)
361 def classTeardown(self
):
363 del self
.target_process
367 If compile_args is set, compile a program and set self.binary.
371 Then call test() and return the result, displaying relevant information
372 if an exception is raised.
375 print "Running", type(self
).__name
__, "...",
378 if not self
.early_applicable():
379 print "not_applicable"
380 return "not_applicable"
382 self
.start
= time
.time()
388 result
= self
.test() # pylint: disable=no-member
389 except TestNotApplicable
:
390 result
= "not_applicable"
391 except Exception as e
: # pylint: disable=broad-except
392 if isinstance(e
, TestFailed
):
396 print "%s in %.2fs" % (result
, time
.time() - self
.start
)
398 if isinstance(e
, TestFailed
):
402 traceback
.print_exc(file=sys
.stdout
)
403 for log
in self
.logs
:
405 print open(log
, "r").read()
414 print "%s in %.2fs" % (result
, time
.time() - self
.start
)
417 class TestFailed(Exception):
418 def __init__(self
, message
):
419 Exception.__init
__(self
)
420 self
.message
= message
422 class TestNotApplicable(Exception):
423 def __init__(self
, message
):
424 Exception.__init
__(self
)
425 self
.message
= message
427 def assertEqual(a
, b
):
429 raise TestFailed("%r != %r" % (a
, b
))
431 def assertNotEqual(a
, b
):
433 raise TestFailed("%r == %r" % (a
, b
))
437 raise TestFailed("%r not in %r" % (a
, b
))
439 def assertNotIn(a
, b
):
441 raise TestFailed("%r in %r" % (a
, b
))
443 def assertGreater(a
, b
):
445 raise TestFailed("%r not greater than %r" % (a
, b
))
449 raise TestFailed("%r is not True" % a
)
451 def assertRegexpMatches(text
, regexp
):
452 if not re
.search(regexp
, text
):
453 raise TestFailed("can't find %r in %r" % (regexp
, text
))