Don't read entire log into RAM just to print it.
[riscv-tests.git] / debug / testlib.py
index c41c332c8ebf26500b3639b61989a39d1f9746c8..996c188efb8aff2a0c41e063cad437322b5361d9 100644 (file)
@@ -72,6 +72,34 @@ class Spike(object):
         else:
             harts = [target]
 
+        cmd = self.command(target, harts, halted, timeout, with_jtag_gdb)
+        self.infinite_loop = target.compile(harts[0],
+                "programs/checksum.c", "programs/tiny-malloc.c",
+                "programs/infinite_loop.S", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
+        cmd.append(self.infinite_loop)
+        logfile = open(self.logname, "w")
+        logfile.write("+ %s\n" % " ".join(cmd))
+        logfile.flush()
+        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)
+            if not self.port:
+                print_log(self.logname)
+                raise Exception("Didn't get spike message about bitbang "
+                        "connection")
+
+    def command(self, target, harts, halted, timeout, with_jtag_gdb):
+        # pylint: disable=no-self-use
         if target.sim_cmd:
             cmd = shlex.split(target.sim_cmd)
         else:
@@ -102,28 +130,8 @@ class Spike(object):
         if with_jtag_gdb:
             cmd += ['--rbb-port', '0']
             os.environ['REMOTE_BITBANG_HOST'] = 'localhost'
-        self.infinite_loop = target.compile(harts[0],
-                "programs/checksum.c", "programs/tiny-malloc.c",
-                "programs/infinite_loop.S", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
-        cmd.append(self.infinite_loop)
-        logfile = open(self.logname, "w")
-        logfile.write("+ %s\n" % " ".join(cmd))
-        logfile.flush()
-        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"
+        return cmd
 
     def __del__(self):
         if self.process:
@@ -532,8 +540,7 @@ def header(title, dash='-', length=78):
 
 def print_log(path):
     header(path)
-    lines = open(path, "r").readlines()
-    for l in lines:
+    for l in open(path, "r"):
         sys.stdout.write(l)
     print
 
@@ -588,6 +595,9 @@ class BaseTest(object):
         del self.server
         del self.target_process
 
+    def postMortem(self):
+        pass
+
     def run(self):
         """
         If compile_args is set, compile a program and set self.binary.
@@ -621,6 +631,7 @@ class BaseTest(object):
                 print e.message
             header("Traceback")
             traceback.print_exc(file=sys.stdout)
+            self.postMortem()
             return result
 
         finally:
@@ -665,6 +676,12 @@ class GdbTest(BaseTest):
         # FIXME: OpenOCD doesn't handle PRIV now
         #self.gdb.p("$priv=3")
 
+    def postMortem(self):
+        if not self.gdb:
+            return
+        self.gdb.interrupt()
+        self.gdb.command("info registers all", timeout=10)
+
     def classTeardown(self):
         del self.gdb
         BaseTest.classTeardown(self)
@@ -682,21 +699,21 @@ class GdbSingleHartTest(GdbTest):
 
 class ExamineTarget(GdbTest):
     def test(self):
-        self.target.misa = self.gdb.p("$misa")
+        self.hart.misa = self.gdb.p("$misa")
 
         txt = "RV"
-        if (self.target.misa >> 30) == 1:
+        if (self.hart.misa >> 30) == 1:
             txt += "32"
-        elif (self.target.misa >> 62) == 2:
+        elif (self.hart.misa >> 62) == 2:
             txt += "64"
-        elif (self.target.misa >> 126) == 3:
+        elif (self.hart.misa >> 126) == 3:
             txt += "128"
         else:
             raise TestFailed("Couldn't determine XLEN from $misa (0x%x)" %
-                    self.target.misa)
+                    self.hart.misa)
 
         for i in range(26):
-            if self.target.misa & (1<<i):
+            if self.hart.misa & (1<<i):
                 txt += chr(i + ord('A'))
         print txt,