Write command to logfile.
[riscv-tests.git] / debug / testlib.py
index 0902983c88a41a7c03a986ca86dceea776bbef6a..0e5f35dd9a8c0b6b0d92ab45dedbbfc5a42b7805 100644 (file)
@@ -16,11 +16,11 @@ def find_file(path):
             return fullpath
     return None
 
-def compile(*args):
+def compile(args, xlen=32):
     """Compile a single .c file into a binary."""
     dst = os.path.splitext(args[0])[0]
-    cc = os.path.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
-    cmd = [cc, "-g", "-O", "-o", dst]
+    cc = os.path.expandvars("$RISCV/bin/riscv%d-unknown-elf-gcc" % xlen)
+    cmd = [cc, "-g", "-o", dst]
     for arg in args:
         found = find_file(arg)
         if found:
@@ -42,13 +42,19 @@ def unused_port():
     return port
 
 class Spike(object):
-    def __init__(self, binary, halted=False, with_gdb=True, timeout=None):
+    def __init__(self, cmd, binary=None, halted=False, with_gdb=True, timeout=None,
+            xlen=64):
         """Launch spike. Return tuple of its process and the port it's running on."""
-        cmd = []
+        if cmd:
+            cmd = shlex.split(cmd)
+        else:
+            cmd = ["spike"]
+        if (xlen == 32):
+            cmd += ["--isa", "RV32"]
+
         if timeout:
-            cmd += ["timeout", str(timeout)]
+            cmd = ["timeout", str(timeout)] + cmd
 
-        cmd += [find_file("spike")]
         if halted:
             cmd.append('-H')
         if with_gdb:
@@ -58,6 +64,7 @@ class Spike(object):
         if binary:
             cmd.append(binary)
         logfile = open("spike.log", "w")
+        logfile.write("+ %s\n" % " ".join(cmd))
         self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=logfile,
                 stderr=logfile)
 
@@ -82,6 +89,7 @@ class Openocd(object):
         if debug:
             cmd.append("-d")
         logfile = open("openocd.log", "w")
+        logfile.write("+ %s\n" % " ".join(cmd))
         self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=logfile,
                 stderr=logfile)
         # TODO: Pick a random port
@@ -95,11 +103,13 @@ class Openocd(object):
             pass
 
 class Gdb(object):
-    def __init__(self):
-        path = os.path.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")
+    def __init__(self,
+            path=os.path.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")):
         self.child = pexpect.spawn(path)
         self.child.logfile = file("gdb.log", "w")
+        self.child.logfile.write("+ %s\n" % path)
         self.wait()
+        self.command("set confirm off")
         self.command("set width 0")
         self.command("set height 0")
         # Force consistency.
@@ -117,7 +127,9 @@ class Gdb(object):
 
     def c(self, wait=True):
         if wait:
-            return self.command("c")
+            output = self.command("c")
+            assert "Continuing" in output
+            return output
         else:
             self.child.sendline("c")
             self.child.expect("Continuing")
@@ -125,6 +137,7 @@ class Gdb(object):
     def interrupt(self):
         self.child.send("\003");
         self.child.expect("\(gdb\)")
+        return self.child.before.strip()
 
     def x(self, address, size='w'):
         output = self.command("x/%s %s" % (size, address))
@@ -132,9 +145,28 @@ class Gdb(object):
         return value
 
     def p(self, obj):
-        output = self.command("p %s" % obj)
-        value = int(output.split('=')[-1].strip())
+        output = self.command("p/x %s" % obj)
+        value = int(output.split('=')[-1].strip(), 0)
         return value
 
     def stepi(self):
-        return self.command("stepi")
+        output = self.command("stepi")
+        assert "Cannot" not in output
+        return output
+
+    def load(self):
+        output = self.command("load", timeout=60)
+        assert "failed" not in  output
+        assert "Transfer rate" in output
+
+    def b(self, location):
+        output = self.command("b %s" % location)
+        assert "not defined" not in output
+        assert "Breakpoint" in output
+        return output
+
+    def hbreak(self, location):
+        output = self.command("hbreak %s" % location)
+        assert "not defined" not in output
+        assert "Hardware assisted breakpoint" in output
+        return output