Separate and clean up break handling.

Ctrl+C handling is done in ctrlc.c, and a break condition now persists
until the interactive reader begins to process the next command. The
condition is cleared by calling ctrlc_clear().
This commit is contained in:
Daniel Beer 2012-10-09 09:55:48 +13:00
parent 0fcd0ae124
commit 18cf2a561f
12 changed files with 213 additions and 121 deletions

View File

@ -63,9 +63,9 @@ else
BINARY = mspdebug
ifneq ($(filter $(UNAME_S),FreeBSD OpenBSD),)
OS_LIBS =
OS_LIBS = -lpthread
else
OS_LIBS = -ldl
OS_LIBS = -lpthread -ldl
endif
endif
@ -115,6 +115,7 @@ OBJ=\
util/dynload.o \
util/demangle.o \
util/powerbuf.o \
util/ctrlc.o \
transport/cp210x.o \
transport/cdc_acm.o \
transport/ftdi.o \

View File

@ -36,6 +36,7 @@
#include "fet_db.h"
#include "output.h"
#include "opdb.h"
#include "ctrlc.h"
#include "fet_olimex_db.h"
#include "devicelist.h"
@ -622,7 +623,6 @@ int fet_ctl(device_t dev_base, device_ctl_t action)
return -1;
}
ctrlc_reset();
return 0;
case DEVICE_CTL_HALT:

View File

@ -26,6 +26,7 @@
#include "gdb_proto.h"
#include "opdb.h"
#include "util.h"
#include "ctrlc.h"
struct gdb_client {
struct device base;
@ -348,7 +349,6 @@ static device_status_t gdbc_poll(device_t dev_base)
if (!dev->is_running)
return DEVICE_STATUS_HALTED;
ctrlc_reset();
len = gdb_peek(&dev->gdb, 50);
if (ctrlc_check())
return DEVICE_STATUS_INTR;

View File

@ -23,6 +23,7 @@
#include "sport.h"
#include "output.h"
#include "goodfet.h"
#include "ctrlc.h"
/* GoodFET protocol definitions */
#define APP_JTAG430 0x11
@ -577,7 +578,6 @@ static device_status_t goodfet_poll(device_t dev_base)
{
(void)dev_base;
ctrlc_reset();
if (delay_ms(100) < 0 || ctrlc_check())
return DEVICE_STATUS_INTR;

View File

@ -26,6 +26,7 @@
#include "output.h"
#include "sim.h"
#include "simio_cpu.h"
#include "ctrlc.h"
#define MEM_SIZE 65536
#define MEM_IO_END 0x200
@ -722,7 +723,6 @@ static device_status_t sim_poll(device_t dev_base)
if (!dev->running)
return DEVICE_STATUS_HALTED;
ctrlc_reset();
dev->watchpoint_hit = 0;
while (count > 0) {
int i;

View File

@ -25,6 +25,7 @@
#include "tilib.h"
#include "tilib_defs.h"
#include "threads.h"
#include "ctrlc.h"
#if defined(__Windows__) || defined(__CYGWIN__)
static const char tilib_filename[] = "MSP430.DLL";
@ -476,7 +477,6 @@ static device_status_t tilib_poll(device_t dev_base)
{
struct tilib_device *dev = (struct tilib_device *)dev_base;
ctrlc_reset();
if ((delay_ms(50) < 0) || ctrlc_check())
return DEVICE_STATUS_INTR;

View File

@ -38,6 +38,7 @@
#include "reader.h"
#include "output.h"
#include "simio.h"
#include "ctrlc.h"
#include "sim.h"
#include "bsl.h"

View File

@ -35,6 +35,7 @@
#include "reader.h"
#include "opdb.h"
#include "aliasdb.h"
#include "ctrlc.h"
#define MAX_READER_LINE 1024
@ -209,6 +210,7 @@ void reader_loop(void)
memcpy(tmpbuf, repeat_buf, sizeof(tmpbuf));
}
ctrlc_clear();
do_command(buf, 1);
if (want_exit)

139
util/ctrlc.c Normal file
View File

@ -0,0 +1,139 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009-2012 Daniel Beer
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ctrlc.h"
#ifdef __Windows__
#include <windows.h>
static int ctrlc_flag;
static HANDLE ctrlc_event;
static CRITICAL_SECTION ctrlc_cs;
static WINAPI BOOL ctrlc_handler(DWORD event)
{
if ((event == CTRL_C_EVENT) || (event == CTRL_BREAK_EVENT)) {
ctrlc_raise();
return TRUE;
}
return FALSE;
}
void ctrlc_init(void)
{
ctrlc_event = CreateEvent(0, TRUE, FALSE, NULL);
InitializeCriticalSection(&ctrlc_cs);
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
}
void ctrlc_exit(void)
{
SetConsoleCtrlHandler(NULL, TRUE);
DeleteCriticalSection(&ctrlc_cs);
CloseHandle(ctrlc_event);
}
int ctrlc_check(void)
{
int cc;
EnterCriticalSection(&ctrlc_cs);
cc = ctrlc_flag;
LeaveCriticalSection(&ctrlc_cs);
return cc;
}
void ctrlc_clear(void)
{
EnterCriticalSection(&ctrlc_cs);
ctrlc_flag = 0;
ResetEvent(ctrlc_event);
LeaveCriticalSection(&ctrlc_cs);
}
void ctrlc_raise(void)
{
EnterCriticalSection(&ctrlc_cs);
ctrlc_flag = 1;
SetEvent(ctrlc_event);
LeaveCriticalSection(&ctrlc_cs);
}
HANDLE ctrlc_win32_event(void)
{
return ctrlc_event;
}
#else /* __Windows__ */
#include <pthread.h>
#include <signal.h>
static volatile int ctrlc_flag;
static pthread_t ctrlc_thread;
static void sigint_handler(int signum)
{
(void)signum;
ctrlc_flag = 1;
}
void ctrlc_init(void)
{
#ifndef __CYGWIN__
static const struct sigaction siga = {
.sa_handler = sigint_handler,
.sa_flags = 0
};
#endif
ctrlc_thread = pthread_self();
#ifdef __CYGWIN__
signal(SIGINT, sigint_handler);
#else
sigaction(SIGINT, &siga, NULL);
#endif
}
void ctrlc_exit(void)
{
signal(SIGINT, SIG_DFL);
}
void ctrlc_clear(void)
{
ctrlc_flag = 0;
}
void ctrlc_raise(void)
{
pthread_kill(ctrlc_thread, SIGINT);
}
int ctrlc_check(void)
{
#ifdef __CYGWIN__
/* Cygwin's signal emulation seems to require the process to
* block.
*/
usleep(1);
#endif
return ctrlc_flag;
}
#endif

63
util/ctrlc.h Normal file
View File

@ -0,0 +1,63 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009-2012 Daniel Beer
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CTRLC_H_
#define CTRLC_H_
/* The Ctrl+C subsystem provides a mechanism for interrupting IO
* operations in a controlled way. Relevant signals are captured (SIGINT
* on Linux and console events on Windows) and the event is reported to
* the system.
*
* The Ctrl+C state has the semantics of an event variable: it can be
* either set, reset or checked via the interface provided.
*/
/* Set up Ctrl+C handling and register all necessary handlers. */
void ctrlc_init(void);
void ctrlc_exit(void);
/* Check the state of the Ctrl+C event variable. This function returns
* non-zero if the event is raised.
*/
int ctrlc_check(void);
/* Manually reset the Ctrl+C event. This should be done before starting
* the processing of a command.
*/
void ctrlc_clear(void);
/* Manually raise a Ctrl+C event. This function is safe to call from any
* thread.
*/
void ctrlc_raise(void);
#ifdef __Windows__
/* On Unix systems, Ctrl+C generates a signal which will also interrupt
* any IO operation currently in progress, after which the event will be
* checked by the initiator of the operation.
*
* Under Windows, we don't have this facility, so we expose a kernel
* object which becomes signalled when the Ctrl+C event is raised.
* Implementations of Windows IO operations should allow operations to
* be interrupted by the signalling of this object.
*/
HANDLE ctrlc_win32_event(void);
#endif
#endif

View File

@ -22,7 +22,6 @@
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <time.h>
@ -33,107 +32,6 @@
#include "util.h"
#include "output.h"
#ifdef __Windows__
static int ctrlc_flag;
static HANDLE ctrlc_event;
static CRITICAL_SECTION ctrlc_cs;
static WINAPI BOOL ctrlc_handler(DWORD event)
{
if ((event == CTRL_C_EVENT) || (event == CTRL_BREAK_EVENT)) {
EnterCriticalSection(&ctrlc_cs);
ctrlc_flag = 1;
LeaveCriticalSection(&ctrlc_cs);
SetEvent(ctrlc_event);
return TRUE;
}
return FALSE;
}
void ctrlc_init(void)
{
ctrlc_event = CreateEvent(0, TRUE, FALSE, NULL);
InitializeCriticalSection(&ctrlc_cs);
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
}
void ctrlc_exit(void)
{
SetConsoleCtrlHandler(NULL, TRUE);
DeleteCriticalSection(&ctrlc_cs);
CloseHandle(ctrlc_event);
}
int ctrlc_check(void)
{
int cc;
EnterCriticalSection(&ctrlc_cs);
cc = ctrlc_flag;
LeaveCriticalSection(&ctrlc_cs);
return cc;
}
void ctrlc_reset(void)
{
EnterCriticalSection(&ctrlc_cs);
ctrlc_flag = 0;
LeaveCriticalSection(&ctrlc_cs);
ResetEvent(ctrlc_event);
}
HANDLE ctrlc_win32_event(void)
{
return ctrlc_event;
}
#else /* __Windows__ */
static volatile int ctrlc_flag;
static void sigint_handler(int signum)
{
(void)signum;
ctrlc_flag = 1;
}
void ctrlc_init(void)
{
#ifdef __CYGWIN__
signal(SIGINT, sigint_handler);
#else
static const struct sigaction siga = {
.sa_handler = sigint_handler,
.sa_flags = 0
};
sigaction(SIGINT, &siga, NULL);
#endif
}
void ctrlc_exit(void)
{
signal(SIGINT, SIG_DFL);
}
void ctrlc_reset(void)
{
ctrlc_flag = 0;
}
int ctrlc_check(void)
{
#ifdef __CYGWIN__
/* Cygwin's signal emulation seems to require the process to
* block.
*/
delay_ms(1);
#endif
return ctrlc_flag;
}
#endif
char *get_arg(char **text)
{
char *start;

View File

@ -22,10 +22,6 @@
#include <stdint.h>
#include <ctype.h>
#ifdef __Windows__
#include <windows.h>
#endif
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
#define LE_BYTE(b, x) ((int)((uint8_t *)(b))[x])
@ -38,12 +34,6 @@ typedef uint32_t address_t;
/* Retrive a string describing the last system error */
const char *last_error(void);
/* Check and catch ^C from the user */
void ctrlc_init(void);
void ctrlc_exit(void);
void ctrlc_reset(void);
int ctrlc_check(void);
/* Retrieve the next word from a pointer to the rest of a command
* argument buffer. Returns NULL if no more words.
*/
@ -62,8 +52,6 @@ int hexval(int c);
#ifdef __Windows__
char *strsep(char **strp, const char *delim);
HANDLE ctrlc_win32_event(void);
#endif
/* Expand `~' in path names. Caller must free the returned ptr */