d6044f5eba79adeecdf507d2f43e826155b37feb
10 # Note that gdb comes with its own testsuite. I was unable to figure out how to
11 # run that testsuite against the spike simulator.
14 for directory
in (os
.getcwd(), os
.path
.dirname(testlib
.__file
__)):
15 fullpath
= os
.path
.join(directory
, path
)
16 if os
.path
.exists(fullpath
):
20 def compile(args
, xlen
=32):
21 cc
= os
.path
.expandvars("$RISCV/bin/riscv%d-unknown-elf-gcc" % xlen
)
24 found
= find_file(arg
)
30 result
= os
.system(cmd
)
31 assert result
== 0, "%r failed" % cmd
34 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
36 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
38 port
= s
.getsockname()[1]
43 def __init__(self
, cmd
, binary
=None, halted
=False, with_gdb
=True, timeout
=None,
45 """Launch spike. Return tuple of its process and the port it's running on."""
47 cmd
= shlex
.split(cmd
)
51 cmd
+= ["--isa", "RV32"]
54 cmd
= ["timeout", str(timeout
)] + cmd
59 self
.port
= unused_port()
60 cmd
+= ['--gdb-port', str(self
.port
)]
64 logfile
= open("spike.log", "w")
65 logfile
.write("+ %s\n" % " ".join(cmd
))
67 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
77 def wait(self
, *args
, **kwargs
):
78 return self
.process
.wait(*args
, **kwargs
)
81 def __init__(self
, simv
=None, debug
=False):
83 cmd
= shlex
.split(simv
)
86 cmd
+= ["+jtag_vpi_enable"]
88 cmd
[0] = cmd
[0] + "-debug"
89 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
90 logfile
= open("simv.log", "w")
91 logfile
.write("+ %s\n" % " ".join(cmd
))
93 listenfile
= open("simv.log", "r")
95 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
99 line
= listenfile
.readline()
102 if ("Listening on port 5555" in line
):
106 print "DELETE called for VcsSim"
114 class Openocd(object):
115 def __init__(self
, cmd
=None, config
=None, debug
=False, keepAlive
=None):
117 # keep handles to other processes -- don't let them be
118 # garbage collected yet.
120 self
.keepAlive
= keepAlive
122 cmd
= shlex
.split(cmd
)
126 cmd
+= ["-f", find_file(config
)]
129 logfile
= open("openocd.log", "w")
130 logfile
.write("+ %s\n" % " ".join(cmd
))
131 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
133 # TODO: Pick a random port
145 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
146 self
.child
= pexpect
.spawn(cmd
)
147 self
.child
.logfile
= file("gdb.log", "w")
148 self
.child
.logfile
.write("+ %s\n" % cmd
)
150 self
.command("set confirm off")
151 self
.command("set width 0")
152 self
.command("set height 0")
154 self
.command("set print entry-values no")
157 """Wait for prompt."""
158 self
.child
.expect("\(gdb\)")
160 def command(self
, command
, timeout
=-1):
161 self
.child
.sendline(command
)
162 self
.child
.expect("\n", timeout
=timeout
)
163 self
.child
.expect("\(gdb\)", timeout
=timeout
)
164 return self
.child
.before
.strip()
166 def c(self
, wait
=True):
168 output
= self
.command("c")
169 assert "Continuing" in output
172 self
.child
.sendline("c")
173 self
.child
.expect("Continuing")
176 self
.child
.send("\003");
177 self
.child
.expect("\(gdb\)")
178 return self
.child
.before
.strip()
180 def x(self
, address
, size
='w'):
181 output
= self
.command("x/%s %s" % (size
, address
))
182 value
= int(output
.split(':')[1].strip(), 0)
186 output
= self
.command("p/x %s" % obj
)
187 value
= int(output
.split('=')[-1].strip(), 0)
191 output
= self
.command("stepi")
192 assert "Cannot" not in output
196 output
= self
.command("load", timeout
=60)
197 assert "failed" not in output
198 assert "Transfer rate" in output
200 def b(self
, location
):
201 output
= self
.command("b %s" % location
)
202 assert "not defined" not in output
203 assert "Breakpoint" in output
206 def hbreak(self
, location
):
207 output
= self
.command("hbreak %s" % location
)
208 assert "not defined" not in output
209 assert "Hardware assisted breakpoint" in output