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
))
133 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
,
134 stdout
=logfile
, stderr
=logfile
)
135 # TODO: Pick a random port
147 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
148 self
.child
= pexpect
.spawn(cmd
)
149 self
.child
.logfile
= open("gdb.log", "w")
150 self
.child
.logfile
.write("+ %s\n" % cmd
)
152 self
.command("set confirm off")
153 self
.command("set width 0")
154 self
.command("set height 0")
156 self
.command("set print entry-values no")
159 """Wait for prompt."""
160 self
.child
.expect(r
"\(gdb\)")
162 def command(self
, command
, timeout
=-1):
163 self
.child
.sendline(command
)
164 self
.child
.expect("\n", timeout
=timeout
)
165 self
.child
.expect(r
"\(gdb\)", timeout
=timeout
)
166 return self
.child
.before
.strip()
168 def c(self
, wait
=True):
170 output
= self
.command("c")
171 assert "Continuing" in output
174 self
.child
.sendline("c")
175 self
.child
.expect("Continuing")
178 self
.child
.send("\003")
179 self
.child
.expect(r
"\(gdb\)", timeout
=60)
180 return self
.child
.before
.strip()
182 def x(self
, address
, size
='w'):
183 output
= self
.command("x/%s %s" % (size
, address
))
184 value
= int(output
.split(':')[1].strip(), 0)
188 output
= self
.command("p/x %s" % obj
)
189 value
= int(output
.split('=')[-1].strip(), 0)
192 def p_string(self
, obj
):
193 output
= self
.command("p %s" % obj
)
194 value
= shlex
.split(output
.split('=')[-1].strip())[1]
198 output
= self
.command("stepi")
199 assert "Cannot" not in output
203 output
= self
.command("load", timeout
=60)
204 assert "failed" not in output
205 assert "Transfer rate" in output
207 def b(self
, location
):
208 output
= self
.command("b %s" % location
)
209 assert "not defined" not in output
210 assert "Breakpoint" in output
213 def hbreak(self
, location
):
214 output
= self
.command("hbreak %s" % location
)
215 assert "not defined" not in output
216 assert "Hardware assisted breakpoint" in output