c186a175a5cc9d50a8c26d7c49618fa474dd6e2f
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(__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
):
113 class Openocd(object):
114 def __init__(self
, cmd
=None, config
=None, debug
=False, otherProcess
=None):
116 # keep handles to other processes -- don't let them be
117 # garbage collected yet.
119 self
.otherProcess
= otherProcess
121 cmd
= shlex
.split(cmd
)
125 cmd
+= ["-f", find_file(config
)]
128 logfile
= open("openocd.log", "w")
129 logfile
.write("+ %s\n" % " ".join(cmd
))
130 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
132 # TODO: Pick a random port
144 cmd
=os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
145 self
.child
= pexpect
.spawn(cmd
)
146 self
.child
.logfile
= file("gdb.log", "w")
147 self
.child
.logfile
.write("+ %s\n" % cmd
)
149 self
.command("set confirm off")
150 self
.command("set width 0")
151 self
.command("set height 0")
153 self
.command("set print entry-values no")
156 """Wait for prompt."""
157 self
.child
.expect("\(gdb\)")
159 def command(self
, command
, timeout
=-1):
160 self
.child
.sendline(command
)
161 self
.child
.expect("\n", timeout
=timeout
)
162 self
.child
.expect("\(gdb\)", timeout
=timeout
)
163 return self
.child
.before
.strip()
165 def c(self
, wait
=True):
167 output
= self
.command("c")
168 assert "Continuing" in output
171 self
.child
.sendline("c")
172 self
.child
.expect("Continuing")
175 self
.child
.send("\003");
176 self
.child
.expect("\(gdb\)")
177 return self
.child
.before
.strip()
179 def x(self
, address
, size
='w'):
180 output
= self
.command("x/%s %s" % (size
, address
))
181 value
= int(output
.split(':')[1].strip(), 0)
185 output
= self
.command("p/x %s" % obj
)
186 value
= int(output
.split('=')[-1].strip(), 0)
190 output
= self
.command("stepi")
191 assert "Cannot" not in output
195 output
= self
.command("load", timeout
=60)
196 assert "failed" not in output
197 assert "Transfer rate" in output
199 def b(self
, location
):
200 output
= self
.command("b %s" % location
)
201 assert "not defined" not in output
202 assert "Breakpoint" in output
205 def hbreak(self
, location
):
206 output
= self
.command("hbreak %s" % location
)
207 assert "not defined" not in output
208 assert "Hardware assisted breakpoint" in output