From 7e5c1b420d0b332d6663a47182f9a472e400f663 Mon Sep 17 00:00:00 2001 From: Tim Newsome Date: Thu, 5 May 2016 17:40:02 -0700 Subject: [PATCH] Halt when gdb user hits ^C. --- riscv/gdbserver.cc | 9 +++++---- riscv/processor.cc | 1 - tests/gdbserver.py | 19 ++++++++++++++++++- tests/testlib.py | 11 +++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/riscv/gdbserver.cc b/riscv/gdbserver.cc index fbe0027..f3be272 100644 --- a/riscv/gdbserver.cc +++ b/riscv/gdbserver.cc @@ -309,8 +309,11 @@ class halt_op_t : public operation_t fprintf(stderr, "Internal error. Processor halted without reason.\n"); abort(); - case DCSR_CAUSE_HWBP: case DCSR_CAUSE_DEBUGINT: + gs.send_packet("S02"); // Pretend program received SIGINT. + break; + + case DCSR_CAUSE_HWBP: case DCSR_CAUSE_STEP: case DCSR_CAUSE_HALT: // There's no gdb code for this. @@ -1513,9 +1516,7 @@ void gdbserver_t::handle_packet(const std::vector &packet) void gdbserver_t::handle_interrupt() { processor_t *p = sim->get_core(0); - // TODO p->set_halted(true, HR_INTERRUPT); - send_packet("S02"); // Pretend program received SIGINT. - // TODO running = false; + add_operation(new halt_op_t(*this, true)); } void gdbserver_t::handle() diff --git a/riscv/processor.cc b/riscv/processor.cc index 4c4e3dd..d43defc 100644 --- a/riscv/processor.cc +++ b/riscv/processor.cc @@ -279,7 +279,6 @@ static bool validate_vm(int max_xlen, reg_t vm) void processor_t::set_csr(int which, reg_t val) { - fprintf(stderr, "set_csr(0x%x, 0x%lx)\n", which, val); val = zext_xlen(val); reg_t delegable_ints = MIP_SSIP | MIP_STIP | MIP_SEIP | (1 << IRQ_COP); reg_t all_ints = delegable_ints | MIP_MSIP | MIP_MTIP; diff --git a/tests/gdbserver.py b/tests/gdbserver.py index ac191d5..2979606 100755 --- a/tests/gdbserver.py +++ b/tests/gdbserver.py @@ -13,7 +13,6 @@ class DebugTest(unittest.TestCase): self.gdb = testlib.Gdb() self.gdb.command("file %s" % self.binary) self.gdb.command("target extended-remote localhost:%d" % self.port) - self.gdb.command("p i=0"); def tearDown(self): self.spike.kill() @@ -21,6 +20,7 @@ class DebugTest(unittest.TestCase): def test_turbostep(self): """Single step a bunch of times.""" + self.gdb.command("p i=0"); last_pc = None for _ in range(100): self.gdb.command("stepi") @@ -29,11 +29,13 @@ class DebugTest(unittest.TestCase): last_pc = pc def test_exit(self): + self.gdb.command("p i=0"); output = self.gdb.command("c") self.assertIn("Continuing", output) self.assertIn("Remote connection closed", output) def test_breakpoint(self): + self.gdb.command("p i=0"); self.gdb.command("b print_row") # The breakpoint should be hit exactly 10 times. for i in range(10): @@ -46,6 +48,7 @@ class DebugTest(unittest.TestCase): self.assertIn("Remote connection closed", output) def test_registers(self): + self.gdb.command("p i=0"); # Try both forms to test gdb. for cmd in ("info all-registers", "info registers all"): output = self.gdb.command(cmd) @@ -66,6 +69,20 @@ class DebugTest(unittest.TestCase): last_instret = instret self.gdb.command("stepi") + def test_interrupt(self): + """Sending gdb ^C while the program is running should cause it to halt.""" + self.gdb.c(wait=False) + time.sleep(0.1) + self.gdb.interrupt() + self.gdb.command("p i=123"); + self.gdb.c(wait=False) + time.sleep(0.1) + self.gdb.interrupt() + self.gdb.command("p i=0"); + output = self.gdb.c() + self.assertIn("Continuing", output) + self.assertIn("Remote connection closed", output) + class RegsTest(unittest.TestCase): def setUp(self): self.binary = testlib.compile("regs.s") diff --git a/tests/testlib.py b/tests/testlib.py index 4e05616..6233901 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -74,6 +74,17 @@ class Gdb(object): self.child.expect("\(gdb\)") return self.child.before.strip() + def c(self, wait=True): + if wait: + return self.command("c") + else: + self.child.sendline("c") + self.child.expect("Continuing") + + def interrupt(self): + self.child.send("\003"); + self.child.expect("\(gdb\)") + def x(self, address, size='w'): output = self.command("x/%s %s" % (size, address)) value = int(output.split(':')[1].strip(), 0) -- 2.30.2