9 # Note that gdb comes with its own testsuite. I was unable to figure out how to
10 # run that testsuite against the spike simulator.
13 for directory
in (os
.getcwd(), os
.path
.dirname(__file__
)):
14 fullpath
= os
.path
.join(directory
, path
)
15 if os
.path
.exists(fullpath
):
19 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
20 cc
= os
.path
.expandvars("$RISCV/bin/riscv%d-unknown-elf-gcc" % xlen
)
23 found
= find_file(arg
)
29 result
= os
.system(cmd
)
30 assert result
== 0, "%r failed" % cmd
33 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
35 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
37 port
= s
.getsockname()[1]
44 def __init__(self
, cmd
, binary
=None, halted
=False, with_gdb
=True,
45 timeout
=None, xlen
=64):
46 """Launch spike. Return tuple of its process and the port it's running
49 cmd
= shlex
.split(cmd
)
53 cmd
+= ["--isa", "RV32"]
56 cmd
= ["timeout", str(timeout
)] + cmd
61 self
.port
= unused_port()
62 cmd
+= ['--gdb-port', str(self
.port
)]
67 logfile
= open(self
.logname
, "w")
68 logfile
.write("+ %s\n" % " ".join(cmd
))
70 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
71 stdout
=logfile
, stderr
=logfile
)
80 def wait(self
, *args
, **kwargs
):
81 return self
.process
.wait(*args
, **kwargs
)
84 def __init__(self
, simv
=None, debug
=False):
86 cmd
= shlex
.split(simv
)
89 cmd
+= ["+jtag_vpi_enable"]
91 cmd
[0] = cmd
[0] + "-debug"
92 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
93 logfile
= open("simv.log", "w")
94 logfile
.write("+ %s\n" % " ".join(cmd
))
96 listenfile
= open("simv.log", "r")
98 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
99 stdout
=logfile
, stderr
=logfile
)
102 line
= listenfile
.readline()
105 if "Listening on port 5555" in line
:
115 class Openocd(object):
116 logname
= "openocd.log"
118 def __init__(self
, cmd
=None, config
=None, debug
=False, otherProcess
=None):
120 # keep handles to other processes -- don't let them be
121 # garbage collected yet.
123 self
.otherProcess
= otherProcess
125 cmd
= shlex
.split(cmd
)
129 cmd
+= ["-f", find_file(config
)]
132 logfile
= open(Openocd
.logname
, "w")
133 logfile
.write("+ %s\n" % " ".join(cmd
))
135 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
136 stdout
=logfile
, stderr
=logfile
)
137 # TODO: Pick a random port
140 # Wait for OpenOCD to have made it through riscv_examine(). When using
141 # OpenOCD to communicate with a simulator this may take a long time,
142 # and gdb will time out when trying to connect if we attempt too early.
146 log
= open(Openocd
.logname
).read()
147 if "Examined RISCV core" in log
:
149 if not self
.process
.poll() is None:
150 raise Exception("OpenOCD exited before completing riscv_examine()")
151 if not messaged
and time
.time() - start
> 1:
153 print "Waiting for OpenOCD to examine RISCV core..."
162 class CannotAccess(Exception):
163 def __init__(self
, address
):
164 Exception.__init
__(self
)
165 self
.address
= address
169 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
170 self
.child
= pexpect
.spawn(cmd
)
171 self
.child
.logfile
= open("gdb.log", "w")
172 self
.child
.logfile
.write("+ %s\n" % cmd
)
174 self
.command("set confirm off")
175 self
.command("set width 0")
176 self
.command("set height 0")
178 self
.command("set print entry-values no")
181 """Wait for prompt."""
182 self
.child
.expect(r
"\(gdb\)")
184 def command(self
, command
, timeout
=-1):
185 self
.child
.sendline(command
)
186 self
.child
.expect("\n", timeout
=timeout
)
187 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
188 return self
.child
.before
.strip()
190 def c(self
, wait
=True):
192 output
= self
.command("c")
193 assert "Continuing" in output
196 self
.child
.sendline("c")
197 self
.child
.expect("Continuing")
200 self
.child
.send("\003")
201 self
.child
.expect(r
"\(gdb\)", timeout
=60)
202 return self
.child
.before
.strip()
204 def x(self
, address
, size
='w'):
205 output
= self
.command("x/%s %s" % (size
, address
))
206 value
= int(output
.split(':')[1].strip(), 0)
210 output
= self
.command("p/x %s" % obj
)
211 m
= re
.search("Cannot access memory at address (0x[0-9a-f]+)", output
)
213 raise CannotAccess(int(m
.group(1), 0))
214 value
= int(output
.split('=')[-1].strip(), 0)
217 def p_string(self
, obj
):
218 output
= self
.command("p %s" % obj
)
219 value
= shlex
.split(output
.split('=')[-1].strip())[1]
223 output
= self
.command("stepi")
227 output
= self
.command("load", timeout
=60)
228 assert "failed" not in output
229 assert "Transfer rate" in output
231 def b(self
, location
):
232 output
= self
.command("b %s" % location
)
233 assert "not defined" not in output
234 assert "Breakpoint" in output
237 def hbreak(self
, location
):
238 output
= self
.command("hbreak %s" % location
)
239 assert "not defined" not in output
240 assert "Hardware assisted breakpoint" in output