tracer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2000-2004 by ETH Zurich
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.btnode.ethz.ch
  31. *
  32. */
  33. /*
  34. * sys/tracer.h
  35. *
  36. * 22.12.2004 Philipp Blum <blum@tik.ee.ethz.ch>
  37. */
  38. #ifndef _SYS_TRACER_H_
  39. #define _SYS_TRACER_H_
  40. /**
  41. \file sys/tracer.h
  42. \author Philipp Blum <blum@tik.ee.ethz.ch>
  43. \date 22.12.2004
  44. \brief Trace functions
  45. */
  46. #include <sys/types.h>
  47. #include <sys/atom.h>
  48. /******************************************************************
  49. * defines
  50. ******************************************************************/
  51. #define TRACE_MODE_FIRST 0
  52. #define TRACE_MODE_OFF 0
  53. #define TRACE_MODE_CIRCULAR 1
  54. #define TRACE_MODE_ONESHOT 2
  55. #define TRACE_MODE_LAST 2
  56. #define TRACE_MODE_DEFAULT TRACE_MODE_CIRCULAR
  57. #define TRACE_SIZE_DEFAULT 500
  58. /* Event types */
  59. #define TRACE_TAG_FIRST 0
  60. #define TRACE_TAG_CRITICAL_FIRST 0
  61. #define TRACE_TAG_CRITICAL_ENTER 0
  62. #define TRACE_TAG_CRITICAL_EXIT 1
  63. #define TRACE_TAG_CRITICAL_LAST 1
  64. #define TRACE_TAG_THREAD_FIRST 2
  65. #define TRACE_TAG_THREAD_YIELD 2
  66. #define TRACE_TAG_THREAD_SETPRIO 3
  67. #define TRACE_TAG_THREAD_WAIT 4
  68. #define TRACE_TAG_THREAD_SLEEP 5
  69. #define TRACE_TAG_THREAD_LAST 5
  70. #define TRACE_TAG_INTERRUPT_FIRST 6
  71. #define TRACE_TAG_INTERRUPT_ENTER 6
  72. #define TRACE_TAG_INTERRUPT_EXIT 7
  73. #define TRACE_TAG_INTERRUPT_LAST 7
  74. #define TRACE_TAG_START 8
  75. #define TRACE_TAG_STOP 9
  76. #define TRACE_TAG_USER 10
  77. #define TRACE_TAG_LAST 10
  78. #define TRACE_MAX_USER 10
  79. #define TRACE_INT_FIRST 0
  80. #define TRACE_INT_UART0_CTS 0
  81. #define TRACE_INT_UART0_RXCOMPL 1
  82. #define TRACE_INT_UART0_TXEMPTY 2
  83. #define TRACE_INT_UART1_CTS 3
  84. #define TRACE_INT_UART1_RXCOMPL 4
  85. #define TRACE_INT_UART1_TXEMPTY 5
  86. #define TRACE_INT_TIMER0_OVERFL 6
  87. #define TRACE_INT_TIMER1_OVERFL 7
  88. #define TRACE_INT_SUART_TIMER 8
  89. #define TRACE_INT_SUART_RX 9
  90. #define TRACE_INT_LAST 9
  91. /******************************************************************
  92. * typedefs
  93. ******************************************************************/
  94. /*! \brief Item in the trace buffer
  95. */
  96. typedef struct _t_traceitem {
  97. /*! \brief Type of event
  98. */
  99. u_char tag;
  100. /*! \brief Additional information, depending on the type of the event
  101. */
  102. u_int pc;
  103. /*! \brief Upper 16 bit of microseconds clock when event occured
  104. */
  105. u_int time_h;
  106. /*! \brief Lower 16 bit of microseconds clock when event occured
  107. */
  108. u_int time_l;
  109. } t_traceitem;
  110. /******************************************************************
  111. * global variables
  112. ******************************************************************/
  113. /*! \brief Upper 16 bits of microseconds clock, incremented on timer 1 overflow interrupts
  114. */
  115. extern u_int micros_high;
  116. /*! \brief Trace buffer, initialized by NutTraceInit
  117. */
  118. extern t_traceitem *trace_items;
  119. /*! \brief Pointer to the current item in the trace buffer
  120. */
  121. extern t_traceitem *trace_current;
  122. /*! \brief Current index in the trace buffer
  123. */
  124. extern int trace_head;
  125. /*! \brief Size of the trace buffer
  126. */
  127. extern int trace_size;
  128. /*! \brief Flag indicating whether all items in the trace buffer contain valid information
  129. */
  130. extern char trace_isfull;
  131. /*! \brief Current state of the tracing facility
  132. */
  133. extern char trace_mode;
  134. /*! \brief Mask to individually disable tracing of specific event types
  135. */
  136. extern char trace_mask[TRACE_TAG_LAST+1];
  137. /******************************************************************
  138. * function prototypes API
  139. ******************************************************************/
  140. /*******************************************************************************
  141. * NutTraceInit
  142. ******************************************************************************/
  143. /**
  144. * Initializes the trace buffer and activates tracing.
  145. * Starts timer 1 for microsecond clock, enables interrupt on overflow
  146. *
  147. *
  148. * @param size Number of items in the trace buffer
  149. * @param mode Mode of operation
  150. * - #TRACE_MODE_CIRCULAR Trace buffer wraps around when full
  151. * - #TRACE_MODE_ONESHOT Traceing is stopped when buffer is full
  152. *
  153. * @return int Status
  154. */
  155. extern int NutTraceInit(int size, char mode);
  156. /*******************************************************************************
  157. * NutTraceStop
  158. ******************************************************************************/
  159. /**
  160. * Sets trace_mode to #TRACE_MODE_OFF and thus stop tracing
  161. */
  162. extern void NutTraceStop(void);
  163. /*******************************************************************************
  164. * NutTracePrint
  165. ******************************************************************************/
  166. /**
  167. * Prints the current contents of the trace buffer
  168. *
  169. * @param size can be used to limit the number of printed items,
  170. * if size==0, then all items of the buffer are printed
  171. */
  172. extern void NutTracePrint(int size);
  173. /*******************************************************************************
  174. * NutTraceTerminal
  175. ******************************************************************************/
  176. /**
  177. * Commands to manipulate the tracing facility
  178. *
  179. * @param arg String containing the commands
  180. * - print &lt;size&gt; calls NutPrintTrace
  181. * - oneshot restarts tracing in the oneshot mode
  182. * - circular restarts tracing in the ciruclar mode
  183. * - size &lt;size&gt; restarts tracing in the current mode, using a buffer of length &lt;size&gt;
  184. * - stop stops tracing
  185. */
  186. extern void NutTraceTerminal(char* arg);
  187. /*******************************************************************************
  188. * NutTraceGetPC
  189. ******************************************************************************/
  190. /**
  191. * Returns the program counter (PC) of the instruction following NutGetPC
  192. * The .map file of the application can be used to find the actual C Code
  193. *
  194. * WARNING: Most likely only works on AVR. Works by inspecting the stack, thus the function
  195. * breaks when local variables are introduced in NutGetPC
  196. *
  197. * @return int Program counter
  198. */
  199. extern int NutTraceGetPC(void);
  200. /*******************************************************************************
  201. * NutTraceClear
  202. ******************************************************************************/
  203. /**
  204. * Clears the trace buffer, but leaves trace_mode unaltered.
  205. */
  206. extern void NutTraceClear(void);
  207. /*******************************************************************************
  208. * NutTraceMaskPrint
  209. ******************************************************************************/
  210. /**
  211. * Prints the current state of trace_mask, which for every type of event (TRACE_TAG_XXX)
  212. * determines, whether they are inserted in the trace buffer or not.
  213. */
  214. extern void NutTraceMaskPrint(void);
  215. /*******************************************************************************
  216. * NutTraceMaskClear
  217. ******************************************************************************/
  218. /**
  219. * Disables tracing of a particular event type
  220. *
  221. * @param tag of event to disable
  222. */
  223. extern void NutTraceMaskClear(int tag);
  224. /*******************************************************************************
  225. * NutTraceMaskSet
  226. ******************************************************************************/
  227. /**
  228. * Enables tracing of a particular event type
  229. *
  230. * @param tag of event to enable
  231. */
  232. extern void NutTraceMaskSet(int tag);
  233. /*******************************************************************************
  234. * NutTraceStatusPrint
  235. ******************************************************************************/
  236. /**
  237. * Prints current status of tracing facility
  238. */
  239. extern void NutTraceStatusPrint(void);
  240. /*******************************************************************************
  241. * NutTraceRegisterUserTag
  242. ******************************************************************************/
  243. /**
  244. * Registers a user event type
  245. *
  246. * @param tag of the new event type (e.g. #define TRACE_USER_SEND 0)
  247. * @param tag_string name of the event type used when printing the trace buffer
  248. */
  249. extern int NutTraceRegisterUserTag(int tag, char* tag_string);
  250. /**
  251. * Macro to insert an event in the trace buffer
  252. *
  253. * @param TAG Type of event
  254. * @param PC Additional information, depending on the type of event
  255. */
  256. #define TRACE_ADD_ITEM(TAG,PC) \
  257. if ((trace_mode != TRACE_MODE_OFF) && \
  258. (trace_mask[TAG] == 1)) \
  259. { \
  260. asm volatile( \
  261. "in __tmp_reg__, __SREG__" "\n\t" \
  262. "push __tmp_reg__" "\n\t" \
  263. "cli" "\n\t" \
  264. ); \
  265. trace_current = &trace_items[trace_head++]; \
  266. trace_current->tag = TAG; \
  267. trace_current->pc = PC; \
  268. trace_current->time_h = micros_high; \
  269. trace_current->time_l = TCNT1; \
  270. if (trace_head >= trace_size) { \
  271. trace_isfull = 1; \
  272. trace_head = 0; \
  273. if (trace_mode == TRACE_MODE_ONESHOT) \
  274. trace_mode = TRACE_MODE_OFF; \
  275. } \
  276. asm volatile( \
  277. "pop __tmp_reg__" "\n\t" \
  278. "out __SREG__, __tmp_reg__" "\n\t" \
  279. ); \
  280. }
  281. /**
  282. * Macro to insert an event in the trace buffer,
  283. * filling the additional information field with the program counter (PC)
  284. *
  285. * @param TAG Type of event
  286. */
  287. #define TRACE_ADD_ITEM_PC(TAG) TRACE_ADD_ITEM(TAG,NutTraceGetPC())
  288. #endif