From a7ad3ace523d19be103e03f6244e0e797354fe0f Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 12 Nov 2007 15:02:23 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@87 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- src/chdebug.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ src/chevents.c | 11 ++++---- src/chinit.c | 1 + src/chmsg.c | 10 ++++--- src/chschd.c | 6 ++++ src/chsem.c | 13 +++++---- src/chthreads.c | 2 ++ src/include/ch.h | 4 +++ src/include/debug.h | 70 +++++++++++++++++++++++++++++++++++++++++++++ src/include/threads.h | 4 +++ src/templates/chconf.h | 6 ++++ src/templates/chcore.h | 5 ++++ src/templates/chtypes.h | 1 + 13 files changed, 193 insertions(+), 15 deletions(-) create mode 100644 src/chdebug.c create mode 100644 src/include/debug.h (limited to 'src') diff --git a/src/chdebug.c b/src/chdebug.c new file mode 100644 index 000000000..d08f91fd1 --- /dev/null +++ b/src/chdebug.c @@ -0,0 +1,75 @@ +/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT 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. + + ChibiOS/RT 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 + +#ifdef CH_USE_DEBUG + +TraceBuffer dbgtb; +char *dbglastmsg; + +/** + * Debug subsystem initialization. + */ +void chDbgInit(void) { + + dbgtb.tb_size = TRACE_BUFFER_SIZE; + dbgtb.tb_ptr = &dbgtb.tb_buffer[0]; +} + +/** + * Inserts in the circular debug trace buffer a context switch record. + * @param otp the thread being switched out + * @param ntp the thread to be resumed + */ +void chDbgTrace(Thread *otp, Thread *ntp) { + + dbgtb.tb_ptr->cse_slpdata = otp->p_common; +#ifdef CH_USE_SYSTEMTIME + dbgtb.tb_ptr->cse_time = chSysGetTime(); +#else + dbgtb.tb_ptr->cse_time = 0; +#endif + dbgtb.tb_ptr->cse_state = otp->p_state; + dbgtb.tb_ptr->cse_tid = ntp->p_tid; + if (++dbgtb.tb_ptr >= &dbgtb.tb_buffer[TRACE_BUFFER_SIZE]) + dbgtb.tb_ptr = &dbgtb.tb_buffer[0]; +} + +/** + * Prints a message on the console/debugger. The latest message pointer + * is retained. + */ +void chDbgPuts(char *msg) { + + dbglastmsg = msg; + chSysPuts(msg); +} + +/** + * Prints a panic message on the console/debugger and then halts the system. + */ +void chDbgPanic(char *msg) { + + chSysPuts("PANIC: "); + chDbgPuts(msg); + chSysHalt(); +} + +#endif /* CH_USE_DEBUG */ diff --git a/src/chevents.c b/src/chevents.c index 93d93ff3d..5f7ce6aaa 100644 --- a/src/chevents.c +++ b/src/chevents.c @@ -160,10 +160,11 @@ t_eventid chEvtWait(t_eventmask ewmask, } #ifdef CH_USE_EVENTS_TIMEOUT -static void unwait(void *p) { - -// Test removed, it should never happen. -// if (((Thread *)p)->p_state == PRWTEVENT) +static void wakeup(void *p) { +#ifdef CH_USE_DEBUG + if (((Thread *)p)->p_state != PRWTEVENT) + chDbgPanic("chevents.c, wakeup()\r\n"); +#endif chSchReadyI(p)->p_rdymsg = RDY_TIMEOUT; } @@ -200,7 +201,7 @@ t_eventid chEvtWaitTimeout(t_eventmask ewmask, if ((currp->p_epending & ewmask) == 0) { VirtualTimer vt; - chVTSetI(&vt, time, unwait, currp); + chVTSetI(&vt, time, wakeup, currp); currp->p_ewmask = ewmask; chSchGoSleepS(PRWTEVENT); if (!chVTIsArmedI(&vt)) { diff --git a/src/chinit.c b/src/chinit.c index 55a69b1fc..4a25567a8 100644 --- a/src/chinit.c +++ b/src/chinit.c @@ -47,6 +47,7 @@ static Thread idlethread; void chSysInit(void) { chSchInit(); + chDbgInit(); #ifdef CH_USE_VIRTUAL_TIMERS chVTInit(); #endif diff --git a/src/chmsg.c b/src/chmsg.c index 010da86e5..5ea3f9821 100644 --- a/src/chmsg.c +++ b/src/chmsg.c @@ -80,10 +80,12 @@ t_msg chMsgSendWithEvent(Thread *tp, t_msg msg, EventSource *esp) { #endif #ifdef CH_USE_MESSAGES_TIMEOUT -static void unsend(void *p) { +static void wakeup(void *p) { -// Test removed, it should never happen. -// if (((Thread *)p)->p_state == PRSNDMSG) +#ifdef CH_USE_DEBUG + if (((Thread *)p)->p_state != PRSNDMSG) + chDbgPanic("chmsg.c, wakeup()\r\n"); +#endif chSchReadyI(dequeue(p))->p_rdymsg = RDY_TIMEOUT; } @@ -108,7 +110,7 @@ t_msg chMsgSendTimeout(Thread *tp, t_msg msg, t_time time) { chSysLock(); - chVTSetI(&vt, time, unsend, currp); + chVTSetI(&vt, time, wakeup, currp); fifo_insert(currp, &tp->p_msgqueue); if (tp->p_state == PRWTMSG) chSchReadyI(tp); diff --git a/src/chschd.c b/src/chschd.c index c95c0cd0e..c30040520 100644 --- a/src/chschd.c +++ b/src/chschd.c @@ -86,6 +86,9 @@ static void nextready(void) { (currp = fifo_remove(&rlist.r_queue))->p_state = PRCURR; preempt = CH_TIME_QUANTUM; +#ifdef CH_USE_DEBUG + chDbgTrace(otp, currp); +#endif chSysSwitchI(&otp->p_ctx, &currp->p_ctx); } @@ -125,6 +128,9 @@ void chSchWakeupS(Thread *tp, t_msg msg) { (currp = tp)->p_state = PRCURR; tp->p_rdymsg = msg; preempt = CH_TIME_QUANTUM; +#ifdef CH_USE_DEBUG + chDbgTrace(ctp, tp); +#endif chSysSwitchI(&ctp->p_ctx, &tp->p_ctx); } } diff --git a/src/chsem.c b/src/chsem.c index 091a045b5..099dc4db4 100644 --- a/src/chsem.c +++ b/src/chsem.c @@ -112,10 +112,11 @@ void chSemWaitS(Semaphore *sp) { } #ifdef CH_USE_SEMAPHORES_TIMEOUT -static void unwait(void *p) { - -// Test removed, it should never happen. -// if (((Thread *)p)->p_state == PRWTSEM) +static void wakeup(void *p) { +#ifdef CH_USE_DEBUG + if (((Thread *)p)->p_state != PRWTSEM) + chDbgPanic("chsem.c, wakeup()\r\n"); +#endif chSemFastSignalI(((Thread *)p)->p_semp); chSchReadyI(dequeue(p))->p_rdymsg = RDY_TIMEOUT; } @@ -134,7 +135,7 @@ t_msg chSemWaitTimeout(Semaphore *sp, t_time time) { if (--sp->s_cnt < 0) { VirtualTimer vt; - chVTSetI(&vt, time, unwait, currp); + chVTSetI(&vt, time, wakeup, currp); fifo_insert(currp, &sp->s_queue); currp->p_semp = sp; chSchGoSleepS(PRWTSEM); @@ -165,7 +166,7 @@ t_msg chSemWaitTimeoutS(Semaphore *sp, t_time time) { if (--sp->s_cnt < 0) { VirtualTimer vt; - chVTSetI(&vt, time, unwait, currp); + chVTSetI(&vt, time, wakeup, currp); fifo_insert(currp, &sp->s_queue); currp->p_semp = sp; chSchGoSleepS(PRWTSEM); diff --git a/src/chthreads.c b/src/chthreads.c index 56ab4969e..9729a9b59 100644 --- a/src/chthreads.c +++ b/src/chthreads.c @@ -28,7 +28,9 @@ * Initializes a thread structure. */ void _InitThread(t_prio prio, t_tmode mode, Thread *tp) { + static t_tid nextid = 0; + tp->p_tid = nextid++; tp->p_flags = mode; tp->p_prio = prio; tp->p_rdymsg = RDY_OK; diff --git a/src/include/ch.h b/src/include/ch.h index 9df1b8363..1102f4013 100644 --- a/src/include/ch.h +++ b/src/include/ch.h @@ -83,6 +83,10 @@ #include "serial.h" #endif +#ifndef _DEBUG_H_ +#include "debug.h" +#endif + /* * Common values. */ diff --git a/src/include/debug.h b/src/include/debug.h new file mode 100644 index 000000000..5a4f3ac44 --- /dev/null +++ b/src/include/debug.h @@ -0,0 +1,70 @@ +/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT 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. + + ChibiOS/RT 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 . +*/ + +/** + * @addtogroup Debug + * @{ + */ + +#ifndef _DEBUG_H_ +#define _DEBUG_H_ + +#ifdef CH_USE_DEBUG + +#define TRACE_BUFFER_SIZE 1024 + +typedef struct { + void *cse_slpdata; + t_time cse_time; + UWORD16 cse_state: 4; + UWORD16 cse_tid: 12; +} CtxSwcEvent; + +typedef struct { + t_size tb_size; + CtxSwcEvent *tb_ptr; + CtxSwcEvent tb_buffer[TRACE_BUFFER_SIZE]; +} TraceBuffer; + +extern CtxSwcEvent *dbgnext; +extern TraceBuffer dbgtb; +extern char *dbglastmsg; + +#ifdef __cplusplus +extern "C" { +#endif + void chDbgInit(void); + void chDbgTrace(Thread *otp, Thread *ntp); + void chDbgPuts(char *msg); + void chDbgPanic(char *msg); +#ifdef __cplusplus +} +#endif + +#else /* CH_USE_DEBUG */ + +#define chDbgInit() +#define chDbgPuts(msg) {} +#define chDbgPanic(msg) {} + +#endif /* CH_USE_DEBUG */ + +#endif /* _DEBUG_H_ */ + +/** @} */ diff --git a/src/include/threads.h b/src/include/threads.h index effad525c..e4cd8376e 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -41,6 +41,8 @@ struct Thread { /** The thread priority.*/ t_prio p_prio; /* End of the fields shared with the ReadyList structure. */ + /** Thread identifier. */ + t_tid p_tid; /** Current thread state.*/ t_tstate p_state; /** Mode flags.*/ @@ -68,6 +70,8 @@ struct Thread { /** Message (only while in \p PRSNDMSG state).*/ t_msg p_msg; #endif + /** Generic way to access the union.*/ + void *p_common; }; /** Machine dependent processor context.*/ Context p_ctx; diff --git a/src/templates/chconf.h b/src/templates/chconf.h index 9c5d303bd..c8214c623 100644 --- a/src/templates/chconf.h +++ b/src/templates/chconf.h @@ -157,6 +157,12 @@ */ //#define CH_CURRP_REGISTER_CACHE "reg" +/** Configuration option: Includes basic debug support to the kernel. + * @note the debug support is port-dependent, it may be not present on some + * targets. In that case stub functions will be included. + */ +#define CH_USE_DEBUG + #endif /* _CHCONF_H_ */ /** @} */ diff --git a/src/templates/chcore.h b/src/templates/chcore.h index a9bfcce63..e917ffa6f 100644 --- a/src/templates/chcore.h +++ b/src/templates/chcore.h @@ -69,6 +69,11 @@ typedef struct { */ #define chSysUnlock() +/** + * Prints a message on the system console (if any). + */ +#define chSysPuts(msg) {} + void chSysHalt(void); void chSysPause(void); void chSysSwitchI(Context *oldp, Context *newp); diff --git a/src/templates/chtypes.h b/src/templates/chtypes.h index 45f60a1a3..fdbdae7b9 100644 --- a/src/templates/chtypes.h +++ b/src/templates/chtypes.h @@ -38,6 +38,7 @@ typedef BYTE8 t_tmode; /* Thread mode flags, BYTE8 is ok. */ typedef BYTE8 t_tstate; /* Thread state, BYTE8 is ok. */ +typedef UWORD16 t_tid; /* Thread id. */ typedef ULONG32 t_prio; /* Priority, use the fastest unsigned type. */ typedef LONG32 t_msg; /* Message, use signed pointer equivalent.*/ typedef LONG32 t_eventid; /* Event Id, use fastest signed.*/ -- cgit v1.2.3