8 # Note that gdb comes with its own testsuite. I was unable to figure out how to
9 # run that testsuite against the spike simulator.
12 for directory
in (os
.getcwd(), os
.path
.dirname(testlib
.__file
__)):
13 fullpath
= os
.path
.join(directory
, path
)
14 if os
.path
.exists(fullpath
):
16 raise ValueError("Couldn't find %r." % path
)
19 """Compile a single .c file into a binary."""
21 dst
= os
.path
.splitext(src
)[0]
22 cc
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
23 cmd
= "%s -g -o %s %s" % (cc
, dst
, src
)
24 result
= os
.system(cmd
)
25 assert result
== 0, "%r failed" % cmd
29 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
31 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
33 port
= s
.getsockname()[1]
37 def spike(binary
, halted
=False, with_gdb
=True, timeout
=None):
38 """Launch spike. Return tuple of its process and the port it's running on."""
41 cmd
+= ["timeout", str(timeout
)]
43 cmd
+= [find_file("spike")]
48 cmd
+= ['--gdb-port', str(port
)]
50 logfile
= open("spike.log", "w")
51 process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
60 path
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")
61 self
.child
= pexpect
.spawn(path
)
62 self
.child
.logfile
= file("gdb.log", "w")
64 self
.command("set width 0")
65 self
.command("set height 0")
68 """Wait for prompt."""
69 self
.child
.expect("\(gdb\)")
71 def command(self
, command
):
72 self
.child
.sendline(command
)
73 self
.child
.expect("\n")
74 self
.child
.expect("\(gdb\)")
75 return self
.child
.before
.strip()
77 def c(self
, wait
=True):
79 return self
.command("c")
81 self
.child
.sendline("c")
82 self
.child
.expect("Continuing")
85 self
.child
.send("\003");
86 self
.child
.expect("\(gdb\)")
88 def x(self
, address
, size
='w'):
89 output
= self
.command("x/%s %s" % (size
, address
))
90 value
= int(output
.split(':')[1].strip(), 0)
94 output
= self
.command("p %s" % obj
)
95 value
= int(output
.split('=')[-1].strip())
99 return self
.command("stepi")