Fix/add to instant trigger tests.
[riscv-tests.git] / debug / gdbserver.py
index 8a6e87439a124f9835ba8b81f8cea43ee505276f..b2a7c167f4b52e1534e1997d10a5ed1442de75d1 100755 (executable)
@@ -1,15 +1,16 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 
 import os
 import sys
 import argparse
-import testlib
 import unittest
 import tempfile
 import time
 import random
 import binascii
 
+import testlib
+
 
 MSTATUS_UIE = 0x00000001
 MSTATUS_SIE = 0x00000002
@@ -41,16 +42,18 @@ def gdb(
     if parsed.gdb:
         gdb = testlib.Gdb(parsed.gdb)
     else:
-        gdb =  testlib.Gdb()
+        gdb = testlib.Gdb()
 
-    if (binary):
-        gdb.command("file %s" % self.binary)
-    if (target):
+    if binary:
+        gdb.command("file %s" % binary)
+    if target:
         gdb.command("set arch riscv:rv%d" % target.xlen)
         gdb.command("set remotetimeout %d" % target.timeout_sec)
-    if (port):
+    if port:
         gdb.command("target extended-remote localhost:%d" % port)
 
+    gdb.p("$priv=3")
+
     return gdb
 
         
@@ -224,9 +227,8 @@ class DebugTest(DeleteServer):
     def test_function_call(self):
         self.gdb.b("main:start")
         self.gdb.c()
-        text = "Howdy, Earth!"
-        gdb_length = self.gdb.p('strlen("%s")' % text)
-        self.assertEqual(gdb_length, len(text))
+        self.assertEqual(self.gdb.p('fib(6)'), 8)
+        self.assertEqual(self.gdb.p('fib(7)'), 13)
         self.exit()
 
     def test_change_string(self):
@@ -238,6 +240,8 @@ class DebugTest(DeleteServer):
 
     def test_turbostep(self):
         """Single step a bunch of times."""
+        self.gdb.b("main:start")
+        self.gdb.c()
         self.gdb.command("p i=0");
         last_pc = None
         advances = 0
@@ -374,6 +378,77 @@ class StepTest(DeleteServer):
             pc = self.gdb.p("$pc")
             self.assertEqual("%x" % pc, "%x" % (expected + main))
 
+class TriggerTest(DeleteServer):
+    def setUp(self):
+        self.binary = target.compile("programs/trigger.S")
+        self.server = target.server()
+        self.gdb = gdb(target, self.server.port, self.binary)
+        self.gdb.load()
+        self.gdb.b("_exit")
+        self.gdb.b("main")
+        self.gdb.c()
+
+    def exit(self):
+        output = self.gdb.c()
+        self.assertIn("Breakpoint", output)
+        self.assertIn("_exit", output)
+
+    def test_execute_instant(self):
+        """Test an execute breakpoint on the first instruction executed out of
+        debug mode."""
+        main = self.gdb.p("$pc")
+        self.gdb.command("hbreak *0x%x" % (main + 4))
+        self.gdb.c()
+        self.assertEqual(self.gdb.p("$pc"), main+4)
+
+    def test_load_address(self):
+        self.gdb.command("rwatch *((&data)+1)");
+        output = self.gdb.c()
+        self.assertIn("read_loop", output)
+        self.assertEqual(self.gdb.p("$a0"),
+                self.gdb.p("(&data)+1"))
+        self.exit()
+
+    def test_load_address_instant(self):
+        """Test a load address breakpoint on the first instruction executed out
+        of debug mode."""
+        self.gdb.command("b just_before_read_loop")
+        self.gdb.c()
+        read_loop = self.gdb.p("&read_loop")
+        self.gdb.command("rwatch data");
+        self.gdb.c()
+        # Accept hitting the breakpoint before or after the load instruction.
+        self.assertIn(self.gdb.p("$pc"), [read_loop, read_loop + 4])
+        self.assertEqual(self.gdb.p("$a0"), self.gdb.p("&data"))
+
+    def test_store_address(self):
+        self.gdb.command("watch *((&data)+3)");
+        output = self.gdb.c()
+        self.assertIn("write_loop", output)
+        self.assertEqual(self.gdb.p("$a0"),
+                self.gdb.p("(&data)+3"))
+        self.exit()
+
+    def test_store_address_instant(self):
+        """Test a store address breakpoint on the first instruction executed out
+        of debug mode."""
+        self.gdb.command("b just_before_write_loop")
+        self.gdb.c()
+        write_loop = self.gdb.p("&write_loop")
+        self.gdb.command("watch data");
+        self.gdb.c()
+        # Accept hitting the breakpoint before or after the store instruction.
+        self.assertIn(self.gdb.p("$pc"), [write_loop, write_loop + 4])
+        self.assertEqual(self.gdb.p("$a0"), self.gdb.p("&data"))
+
+    def test_dmode(self):
+        self.gdb.command("hbreak handle_trap")
+        self.gdb.p("$pc=write_valid")
+        output = self.gdb.c()
+        self.assertIn("handle_trap", output)
+        self.assertIn("mcause=2", output)
+        self.assertIn("mepc=%d" % self.gdb.p("&write_invalid_illegal"), output)
+
 class RegsTest(DeleteServer):
     def setUp(self):
         self.binary = target.compile("programs/regs.S")
@@ -389,8 +464,8 @@ class RegsTest(DeleteServer):
 
         self.gdb.p("$pc=write_regs")
         for i, r in enumerate(regs):
-            self.gdb.command("p $%s=%d" % (r, (0xdeadbeef<<i)+17))
-        self.gdb.command("p $x1=data")
+            self.gdb.p("$%s=%d" % (r, (0xdeadbeef<<i)+17))
+        self.gdb.p("$x1=data")
         self.gdb.command("b all_done")
         output = self.gdb.c()
         self.assertIn("Breakpoint ", output)
@@ -527,9 +602,10 @@ class Target(object):
         raise NotImplementedError
 
     def compile(self, *sources):
-        binary_name = "%s_%s" % (
+        binary_name = "%s_%s-%d" % (
                 self.name,
-                os.path.basename(os.path.splitext(sources[0])[0]))
+                os.path.basename(os.path.splitext(sources[0])[0]),
+                self.xlen)
         if parsed.isolate:
             self.temporary_binary = tempfile.NamedTemporaryFile(
                     prefix=binary_name + "_")
@@ -548,7 +624,7 @@ class SpikeTarget(Target):
     directory = "spike"
     ram = 0x80010000
     ram_size = 5 * 1024 * 1024
-    instruction_hardware_breakpoint_count = 0
+    instruction_hardware_breakpoint_count = 4
     reset_vector = 0x1000
 
 class Spike64Target(SpikeTarget):
@@ -644,16 +720,28 @@ def main():
             help="The command to use to start the debug server.")
     parser.add_argument("--gdb",
             help="The command to use to start gdb.")
+
+    xlen_group = parser.add_mutually_exclusive_group()
+    xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
+            help="Force the target to be 32-bit.")
+    xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
+            help="Force the target to be 64-bit.")
+
     parser.add_argument("--isolate", action="store_true",
             help="Try to run in such a way that multiple instances can run at "
             "the same time. This may make it harder to debug a failure if it "
             "does occur.")
+
     parser.add_argument("unittest", nargs="*")
     global parsed
     parsed = parser.parse_args()
 
     global target
     target = parsed.target()
+
+    if parsed.xlen:
+        target.xlen = parsed.xlen
+
     unittest.main(argv=[sys.argv[0]] + parsed.unittest)
 
 # TROUBLESHOOTING TIPS