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(__file__
)):
13 fullpath
= os
.path
.join(directory
, path
)
14 if os
.path
.exists(fullpath
):
18 def compile(args
, xlen
=32): # pylint: disable=redefined-builtin
19 cc
= os
.path
.expandvars("$RISCV/bin/riscv%d-unknown-elf-gcc" % xlen
)
22 found
= find_file(arg
)
28 result
= os
.system(cmd
)
29 assert result
== 0, "%r failed" % cmd
32 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
34 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
36 port
= s
.getsockname()[1]
43 def __init__(self
, cmd
, binary
=None, halted
=False, with_gdb
=True,
44 timeout
=None, xlen
=64):
45 """Launch spike. Return tuple of its process and the port it's running
48 cmd
= shlex
.split(cmd
)
52 cmd
+= ["--isa", "RV32"]
55 cmd
= ["timeout", str(timeout
)] + cmd
60 self
.port
= unused_port()
61 cmd
+= ['--gdb-port', str(self
.port
)]
66 logfile
= open(self
.logname
, "w")
67 logfile
.write("+ %s\n" % " ".join(cmd
))
69 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
70 stdout
=logfile
, stderr
=logfile
)
79 def wait(self
, *args
, **kwargs
):
80 return self
.process
.wait(*args
, **kwargs
)
83 def __init__(self
, simv
=None, debug
=False):
85 cmd
= shlex
.split(simv
)
88 cmd
+= ["+jtag_vpi_enable"]
90 cmd
[0] = cmd
[0] + "-debug"
91 cmd
+= ["+vcdplusfile=output/gdbserver.vpd"]
92 logfile
= open("simv.log", "w")
93 logfile
.write("+ %s\n" % " ".join(cmd
))
95 listenfile
= open("simv.log", "r")
97 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
98 stdout
=logfile
, stderr
=logfile
)
101 line
= listenfile
.readline()
104 if "Listening on port 5555" in line
:
114 class Openocd(object):
115 logname
= "openocd.log"
117 def __init__(self
, cmd
=None, config
=None, debug
=False, otherProcess
=None):
119 # keep handles to other processes -- don't let them be
120 # garbage collected yet.
122 self
.otherProcess
= otherProcess
124 cmd
= shlex
.split(cmd
)
128 cmd
+= ["-f", find_file(config
)]
131 logfile
= open(Openocd
.logname
, "w")
132 logfile
.write("+ %s\n" % " ".join(cmd
))
134 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
135 stdout
=logfile
, stderr
=logfile
)
136 # TODO: Pick a random port
148 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
149 self
.child
= pexpect
.spawn(cmd
)
150 self
.child
.logfile
= open("gdb.log", "w")
151 self
.child
.logfile
.write("+ %s\n" % cmd
)
153 self
.command("set confirm off")
154 self
.command("set width 0")
155 self
.command("set height 0")
157 self
.command("set print entry-values no")
160 """Wait for prompt."""
161 self
.child
.expect(r
"\(gdb\)")
163 def command(self
, command
, timeout
=-1):
164 self
.child
.sendline(command
)
165 self
.child
.expect("\n", timeout
=timeout
)
166 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
167 return self
.child
.before
.strip()
169 def c(self
, wait
=True):
171 output
= self
.command("c")
172 assert "Continuing" in output
175 self
.child
.sendline("c")
176 self
.child
.expect("Continuing")
179 self
.child
.send("\003")
180 self
.child
.expect(r
"\(gdb\)", timeout
=60)
181 return self
.child
.before
.strip()
183 def x(self
, address
, size
='w'):
184 output
= self
.command("x/%s %s" % (size
, address
))
185 value
= int(output
.split(':')[1].strip(), 0)
189 output
= self
.command("p/x %s" % obj
)
190 value
= int(output
.split('=')[-1].strip(), 0)
193 def p_string(self
, obj
):
194 output
= self
.command("p %s" % obj
)
195 value
= shlex
.split(output
.split('=')[-1].strip())[1]
199 output
= self
.command("stepi")
200 assert "Cannot" not in output
204 output
= self
.command("load", timeout
=60)
205 assert "failed" not in output
206 assert "Transfer rate" in output
208 def b(self
, location
):
209 output
= self
.command("b %s" % location
)
210 assert "not defined" not in output
211 assert "Breakpoint" in output
214 def hbreak(self
, location
):
215 output
= self
.command("hbreak %s" % location
)
216 assert "not defined" not in output
217 assert "Hardware assisted breakpoint" in output