53e670ec51c006fa96cbb0ea2f201e5f77fb7f7c
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(testlib
.__file
__)):
14 fullpath
= os
.path
.join(directory
, path
)
15 if os
.path
.exists(fullpath
):
19 def compile(args
, xlen
=32):
20 """Compile a single .c file into a binary."""
21 dst
= os
.path
.splitext(args
[0])[0]
22 cc
= os
.path
.expandvars("$RISCV/bin/riscv%d-unknown-elf-gcc" % xlen
)
23 cmd
= [cc
, "-g", "-o", dst
]
25 found
= find_file(arg
)
31 result
= os
.system(cmd
)
32 assert result
== 0, "%r failed" % cmd
36 # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python/2838309#2838309
38 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
40 port
= s
.getsockname()[1]
45 def __init__(self
, cmd
, binary
=None, halted
=False, with_gdb
=True, timeout
=None,
47 """Launch spike. Return tuple of its process and the port it's running on."""
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
)]
66 logfile
= open("spike.log", "w")
67 logfile
.write("+ %s\n" % " ".join(cmd
))
68 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
78 def wait(self
, *args
, **kwargs
):
79 return self
.process
.wait(*args
, **kwargs
)
81 class Openocd(object):
82 def __init__(self
, cmd
=None, config
=None, debug
=True):
84 cmd
= shlex
.split(cmd
)
88 cmd
+= ["-f", find_file(config
)]
91 logfile
= open("openocd.log", "w")
92 self
.process
= subprocess
.Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=logfile
,
94 # TODO: Pick a random port
106 path
= os
.path
.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")
107 self
.child
= pexpect
.spawn(path
)
108 self
.child
.logfile
= file("gdb.log", "w")
110 self
.command("set confirm off")
111 self
.command("set width 0")
112 self
.command("set height 0")
114 self
.command("set print entry-values no")
117 """Wait for prompt."""
118 self
.child
.expect("\(gdb\)")
120 def command(self
, command
, timeout
=-1):
121 self
.child
.sendline(command
)
122 self
.child
.expect("\n", timeout
=timeout
)
123 self
.child
.expect("\(gdb\)", timeout
=timeout
)
124 return self
.child
.before
.strip()
126 def c(self
, wait
=True):
128 output
= self
.command("c")
129 assert "Continuing" in output
132 self
.child
.sendline("c")
133 self
.child
.expect("Continuing")
136 self
.child
.send("\003");
137 self
.child
.expect("\(gdb\)")
138 return self
.child
.before
.strip()
140 def x(self
, address
, size
='w'):
141 output
= self
.command("x/%s %s" % (size
, address
))
142 value
= int(output
.split(':')[1].strip(), 0)
146 output
= self
.command("p/x %s" % obj
)
147 value
= int(output
.split('=')[-1].strip(), 0)
151 output
= self
.command("stepi")
152 assert "Cannot" not in output
156 output
= self
.command("load")
157 assert "failed" not in output
158 assert "Transfer rate" in output
160 def b(self
, location
):
161 output
= self
.command("b %s" % location
)
162 assert "not defined" not in output
163 assert "Breakpoint" in output
166 def hbreak(self
, location
):
167 output
= self
.command("hbreak %s" % location
)
168 assert "not defined" not in output
169 assert "Hardware assisted breakpoint" in output