Test OpenOCD step and resume.
[riscv-tests.git] / debug / testlib.py
index 76989806b8ff57278c341c0776a3bb754dd4384e..a38b99b0ea1d4cffe7a918476eb2a787941d0350 100644 (file)
@@ -108,7 +108,7 @@ class VcsSim(object):
             if match:
                 done = True
                 self.port = int(match.group(1))
-                print "Using port %d for JTAG VPI" % self.port
+                os.environ['JTAG_VPI_PORT'] = str(self.port)
 
     def __del__(self):
         try:
@@ -132,19 +132,15 @@ class Openocd(object):
 
         # Assign port
         self.port = unused_port()
-        print "Using port %d for gdb server" % self.port
         # This command needs to come before any config scripts on the command
         # line, since they are executed in order.
         cmd[1:1] = ["--command", "gdb_port %d" % self.port]
 
-        env = os.environ.copy()
-        env['JTAG_VPI_PORT'] = str(otherProcess.port)
-
         logfile = open(Openocd.logname, "w")
         logfile.write("+ %s\n" % " ".join(cmd))
         logfile.flush()
         self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE,
-                stdout=logfile, stderr=logfile, env=env)
+                stdout=logfile, stderr=logfile)
 
         # Wait for OpenOCD to have made it through riscv_examine(). When using
         # OpenOCD to communicate with a simulator this may take a long time,
@@ -171,14 +167,29 @@ class Openocd(object):
 
 class OpenocdCli(object):
     def __init__(self, port=4444):
-        self.child = pexpect.spawn("sh -c 'telnet localhost %d | tee openocd-cli.log'" % port)
+        self.child = pexpect.spawn(
+                "sh -c 'telnet localhost %d | tee openocd-cli.log'" % port)
         self.child.expect("> ")
 
     def command(self, cmd):
         self.child.sendline(cmd)
+        self.child.expect(cmd)
         self.child.expect("\n")
         self.child.expect("> ")
-        return self.child.before.strip()
+        return self.child.before.strip("\t\r\n \0")
+
+    def reg(self, reg=''):
+        output = self.command("reg %s" % reg)
+        matches = re.findall(r"(\w+) \(/\d+\): (0x[0-9A-F]+)", output)
+        values = {r: int(v, 0) for r, v in matches}
+        if reg:
+            return values[reg]
+        return values
+
+    def load_image(self, image):
+        output = self.command("load_image %s" % image)
+        if 'invalid ELF file, only 32bits files are supported' in output:
+            raise TestNotApplicable(output)
 
 class CannotAccess(Exception):
     def __init__(self, address):
@@ -375,6 +386,8 @@ class BaseTest(object):
         try:
             self.setup()
             result = self.test()    # pylint: disable=no-member
+        except TestNotApplicable:
+            result = "not_applicable"
         except Exception as e: # pylint: disable=broad-except
             if isinstance(e, TestFailed):
                 result = "fail"
@@ -406,6 +419,11 @@ class TestFailed(Exception):
         Exception.__init__(self)
         self.message = message
 
+class TestNotApplicable(Exception):
+    def __init__(self, message):
+        Exception.__init__(self)
+        self.message = message
+
 def assertEqual(a, b):
     if a != b:
         raise TestFailed("%r != %r" % (a, b))