Merge remote-tracking branch 'origin/newprogram' into debug-0.13
[riscv-tests.git] / debug / testlib.py
index 9889bad15121c9a5b057e02722b373c022b0cb75..71bd60966abdab6cb5ab8298de91e49db3b67418 100644 (file)
@@ -57,7 +57,7 @@ def unused_port():
 class Spike(object):
     logname = "spike.log"
 
-    def __init__(self, cmd, binary=None, halted=False, with_gdb=True,
+    def __init__(self, cmd, binary=None, halted=False, with_jtag_gdb=True,
             timeout=None, xlen=64):
         """Launch spike. Return tuple of its process and the port it's running
         on."""
@@ -73,9 +73,9 @@ class Spike(object):
 
         if halted:
             cmd.append('-H')
-        if with_gdb:
-            self.port = unused_port()
-            cmd += ['--gdb-port', str(self.port)]
+        if with_jtag_gdb:
+            cmd += ['--rbb-port', '0']
+            os.environ['REMOTE_BITBANG_HOST'] = 'localhost'
         cmd.append("-m32")
         cmd.append('pk')
         if binary:
@@ -86,6 +86,19 @@ class Spike(object):
         self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                 stdout=logfile, stderr=logfile)
 
+        if with_jtag_gdb:
+            self.port = None
+            for _ in range(30):
+                m = re.search(r"Listening for remote bitbang connection on "
+                        r"port (\d+).", open(self.logname).read())
+                if m:
+                    self.port = int(m.group(1))
+                    os.environ['REMOTE_BITBANG_PORT'] = m.group(1)
+                    break
+                time.sleep(0.11)
+            assert self.port, "Didn't get spike message about bitbang " \
+                    "connection"
+
     def __del__(self):
         try:
             self.process.kill()
@@ -138,15 +151,11 @@ class Openocd(object):
         if cmd:
             cmd = shlex.split(cmd)
         else:
-            cmd = ["openocd"]
-        if config:
-            cmd += ["-f", find_file(config)]
-        if debug:
-            cmd.append("-d")
+            cmd = ["openocd", "-d"]
 
         # This command needs to come before any config scripts on the command
         # line, since they are executed in order.
-        cmd[1:1] = [
+        cmd += [
             # Tell OpenOCD to bind gdb to an unused, ephemeral port.
             "--command",
             "gdb_port 0",
@@ -159,6 +168,16 @@ class Openocd(object):
             "telnet_port disabled",
         ]
 
+        if config:
+            f = find_file(config)
+            if f is None:
+                print("Unable to read file " + config)
+                exit(1)
+
+            cmd += ["-f", f]
+        if debug:
+            cmd.append("-d")
+
         logfile = open(Openocd.logname, "w")
         logfile.write("+ %s\n" % " ".join(cmd))
         logfile.flush()
@@ -273,9 +292,9 @@ class Gdb(object):
         self.child.expect(r"\(gdb\)", timeout=timeout)
         return self.child.before.strip()
 
-    def c(self, wait=True):
+    def c(self, wait=True, timeout=-1):
         if wait:
-            output = self.command("c")
+            output = self.command("c", timeout=timeout)
             assert "Continuing" in output
             return output
         else: