From fa046601a54ddf2137048f11594ed7d72ede995a Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sat, 28 Feb 2015 20:50:10 -0800 Subject: Add exception handling mechanism and raise exception on low-level comms failure. --- src/exception.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/exception.c (limited to 'src/exception.c') diff --git a/src/exception.c b/src/exception.c new file mode 100644 index 0000000..33e3869 --- /dev/null +++ b/src/exception.c @@ -0,0 +1,39 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2015 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "general.h" +#include "exception.h" + +struct exception *innermost_exception; + +void raise_exception(uint32_t type, const char *msg) +{ + struct exception *e; + for (e = innermost_exception; e; e = e->outer) { + if (e->mask & type) { + e->type = type; + e->msg = msg; + innermost_exception = e->outer; + longjmp(e->jmpbuf, type); + } + } + PLATFORM_FATAL_ERROR(type); +} + -- cgit v1.2.3 From 5ab8564ff67583ac8df977bb6efca047b12e0aed Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 22 Mar 2015 14:05:12 -0700 Subject: Clean up handling of lost targets using new exceptions mechanism. --- src/cortexm.c | 17 +++++++++++++++-- src/exception.c | 2 +- src/gdb_main.c | 6 ++++++ src/main.c | 16 ++++++++++++++-- src/platforms/stm32/timing.c | 1 + 5 files changed, 37 insertions(+), 5 deletions(-) (limited to 'src/exception.c') diff --git a/src/cortexm.c b/src/cortexm.c index f20f282..f6a0ea3 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -37,6 +37,7 @@ #include "command.h" #include "gdb_packet.h" #include "cortexm.h" +#include "morse.h" #include @@ -57,6 +58,7 @@ const struct command_s cortexm_cmd_list[] = { #define SIGINT 2 #define SIGTRAP 5 #define SIGSEGV 11 +#define SIGLOST 29 static int cortexm_regs_read(struct target_s *target, void *data); static int cortexm_regs_write(struct target_s *target, const void *data); @@ -487,12 +489,23 @@ cortexm_halt_wait(struct target_s *target) uint32_t dhcsr = 0; volatile struct exception e; - TRY_CATCH (e, EXCEPTION_TIMEOUT) { + TRY_CATCH (e, EXCEPTION_ALL) { /* If this times out because the target is in WFI then * the target is still running. */ dhcsr = target_mem_read32(target, CORTEXM_DHCSR); } - if (e.type || !(dhcsr & CORTEXM_DHCSR_S_HALT)) + switch (e.type) { + case EXCEPTION_ERROR: + /* Oh crap, there's no recovery from this... */ + target_list_free(); + morse("TARGET LOST.", 1); + return SIGLOST; + case EXCEPTION_TIMEOUT: + /* Timeout isn't a problem, target could be in WFI */ + return 0; + } + + if (!(dhcsr & CORTEXM_DHCSR_S_HALT)) return 0; /* We've halted. Let's find out why. */ diff --git a/src/exception.c b/src/exception.c index 33e3869..3d43f99 100644 --- a/src/exception.c +++ b/src/exception.c @@ -34,6 +34,6 @@ void raise_exception(uint32_t type, const char *msg) longjmp(e->jmpbuf, type); } } - PLATFORM_FATAL_ERROR(type); + abort(); } diff --git a/src/gdb_main.c b/src/gdb_main.c index 7bb788b..750dfe2 100644 --- a/src/gdb_main.c +++ b/src/gdb_main.c @@ -158,6 +158,12 @@ gdb_main(void) if (sig < 0) break; + /* Target disappeared */ + if (cur_target == NULL) { + gdb_putpacket_f("X%02X", sig); + break; + } + /* Report reason for halt */ if(target_check_hw_wp(cur_target, &watch_addr)) { /* Watchpoint hit */ diff --git a/src/main.c b/src/main.c index 60db15c..d1b3c86 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,9 @@ #include "jtagtap.h" #include "jtag_scan.h" #include "target.h" +#include "exception.h" +#include "gdb_packet.h" +#include "morse.h" int main(int argc, char **argv) @@ -39,9 +42,18 @@ main(int argc, char **argv) (void) argv; platform_init(); #endif - PLATFORM_SET_FATAL_ERROR_RECOVERY(); - gdb_main(); + while (true) { + volatile struct exception e; + TRY_CATCH(e, EXCEPTION_ALL) { + gdb_main(); + } + if (e.type == EXCEPTION_ERROR) { + gdb_putpacketz("EFF"); + target_list_free(); + morse("TARGET LOST.", 1); + } + } /* Should never get here */ return 0; diff --git a/src/platforms/stm32/timing.c b/src/platforms/stm32/timing.c index cac22ca..55a217a 100644 --- a/src/platforms/stm32/timing.c +++ b/src/platforms/stm32/timing.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ #include "general.h" +#include "morse.h" #include #include -- cgit v1.2.3