Limit spike RAM so I can run valgrind on it.
[riscv-tests.git] / debug / testlib.py
index b3f8f66a822f8529973fa5a46b89f4beba2da52c..82a731a73b7662f0765855dcb257b9ec96599416 100644 (file)
@@ -1,16 +1,17 @@
 import os.path
-import pexpect
 import shlex
 import subprocess
 import tempfile
-import testlib
 import unittest
+import time
+
+import pexpect
 
 # Note that gdb comes with its own testsuite. I was unable to figure out how to
 # run that testsuite against the spike simulator.
 
 def find_file(path):
-    for directory in (os.getcwd(), os.path.dirname(testlib.__file__)):
+    for directory in (os.getcwd(), os.path.dirname(__file__)):
         fullpath = os.path.join(directory, path)
         if os.path.exists(fullpath):
             return fullpath
@@ -57,6 +58,7 @@ class Spike(object):
         if with_gdb:
             self.port = unused_port()
             cmd += ['--gdb-port', str(self.port)]
+        cmd.append("-m32")
         cmd.append('pk')
         if binary:
             cmd.append(binary)
@@ -76,8 +78,46 @@ class Spike(object):
     def wait(self, *args, **kwargs):
         return self.process.wait(*args, **kwargs)
 
+class VcsSim(object):
+    def __init__(self, simv=None, debug=False):
+        if simv:
+            cmd = shlex.split(simv)
+        else:
+            cmd =  ["simv"]
+        cmd += ["+jtag_vpi_enable"]
+        if debug:
+            cmd[0] = cmd[0] + "-debug"
+            cmd += ["+vcdplusfile=output/gdbserver.vpd"]
+        logfile = open("simv.log", "w")
+        logfile.write("+ %s\n" % " ".join(cmd))
+        logfile.flush()
+        listenfile = open("simv.log", "r")
+        listenfile.seek(0,2)
+        self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=logfile,
+                                        stderr=logfile)
+        done = False
+        while (not done):
+            line = listenfile.readline()
+            if (not line):
+                time.sleep(1)
+            if ("Listening on port 5555" in line):
+                done = True
+            
+    def __del__(self):
+        try:
+            self.process.kill()
+            self.process.wait()
+        except OSError:
+            pass
+
+        
 class Openocd(object):
-    def __init__(self, cmd=None, config=None, debug=False):
+    def __init__(self, cmd=None, config=None, debug=False, otherProcess=None):
+
+        # keep handles to other processes -- don't let them be
+        # garbage collected yet.
+
+        self.otherProcess = otherProcess
         if cmd:
             cmd = shlex.split(cmd)
         else:
@@ -147,6 +187,11 @@ class Gdb(object):
         value = int(output.split('=')[-1].strip(), 0)
         return value
 
+    def p_string(self, obj):
+        output = self.command("p %s" % obj)
+        value = shlex.split(output.split('=')[-1].strip())[1]
+        return value
+
     def stepi(self):
         output = self.command("stepi")
         assert "Cannot" not in output