tracer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <stdint.h>
  48. #include <sys/atom.h>
  49. /******************************************************************
  50. * defines
  51. ******************************************************************/
  52. #define TRACE_MODE_FIRST 0
  53. #define TRACE_MODE_OFF 0
  54. #define TRACE_MODE_CIRCULAR 1
  55. #define TRACE_MODE_ONESHOT 2
  56. #define TRACE_MODE_LAST 2
  57. #define TRACE_MODE_DEFAULT TRACE_MODE_CIRCULAR
  58. #define TRACE_SIZE_DEFAULT 500
  59. /* Event types */
  60. #define TRACE_TAG_FIRST 0
  61. #define TRACE_TAG_CRITICAL_FIRST 0
  62. #define TRACE_TAG_CRITICAL_ENTER 0
  63. #define TRACE_TAG_CRITICAL_EXIT 1
  64. #define TRACE_TAG_CRITICAL_LAST 1
  65. #define TRACE_TAG_THREAD_FIRST 2
  66. #define TRACE_TAG_THREAD_YIELD 2
  67. #define TRACE_TAG_THREAD_SETPRIO 3
  68. #define TRACE_TAG_THREAD_WAIT 4
  69. #define TRACE_TAG_THREAD_SLEEP 5
  70. #define TRACE_TAG_THREAD_LAST 5
  71. #define TRACE_TAG_INTERRUPT_FIRST 6
  72. #define TRACE_TAG_INTERRUPT_ENTER 6
  73. #define TRACE_TAG_INTERRUPT_EXIT 7
  74. #define TRACE_TAG_INTERRUPT_LAST 7
  75. #define TRACE_TAG_START 8
  76. #define TRACE_TAG_STOP 9
  77. #define TRACE_TAG_USER 10
  78. #define TRACE_TAG_LAST 10
  79. #define TRACE_MAX_USER 10
  80. #define TRACE_INT_FIRST 0
  81. #define TRACE_INT_UART0_CTS 0
  82. #define TRACE_INT_UART0_RXCOMPL 1
  83. #define TRACE_INT_UART0_TXEMPTY 2
  84. #define TRACE_INT_UART1_CTS 3
  85. #define TRACE_INT_UART1_RXCOMPL 4
  86. #define TRACE_INT_UART1_TXEMPTY 5
  87. #define TRACE_INT_TIMER0_OVERFL 6
  88. #define TRACE_INT_TIMER1_OVERFL 7
  89. #define TRACE_INT_SUART_TIMER 8
  90. #define TRACE_INT_SUART_RX 9
  91. #define TRACE_INT_LAST 9
  92. /******************************************************************
  93. * typedefs
  94. ******************************************************************/
  95. /*! \brief Item in the trace buffer
  96. */
  97. typedef struct _t_traceitem {
  98. /*! \brief Type of event
  99. */
  100. uint8_t tag;
  101. /*! \brief Additional information, depending on the type of the event
  102. */
  103. unsigned int pc;
  104. /*! \brief Upper 16 bit of microseconds clock when event occured
  105. */
  106. unsigned int time_h;
  107. /*! \brief Lower 16 bit of microseconds clock when event occured
  108. */
  109. unsigned int time_l;
  110. } t_traceitem;
  111. /******************************************************************
  112. * global variables
  113. ******************************************************************/
  114. /*! \brief Upper 16 bits of microseconds clock, incremented on timer 1 overflow interrupts
  115. */
  116. extern unsigned int micros_high;
  117. /*! \brief Trace buffer, initialized by NutTraceInit
  118. */
  119. extern t_traceitem *trace_items;
  120. /*! \brief Pointer to the current item in the trace buffer
  121. */
  122. extern t_traceitem *trace_current;
  123. /*! \brief Current index in the trace buffer
  124. */
  125. extern int trace_head;
  126. /*! \brief Size of the trace buffer
  127. */
  128. extern int trace_size;
  129. /*! \brief Flag indicating whether all items in the trace buffer contain valid information
  130. */
  131. extern char trace_isfull;
  132. /*! \brief Current state of the tracing facility
  133. */
  134. extern char trace_mode;
  135. /*! \brief Mask to individually disable tracing of specific event types
  136. */
  137. extern char trace_mask[TRACE_TAG_LAST+1];
  138. /******************************************************************
  139. * function prototypes API
  140. ******************************************************************/
  141. /*******************************************************************************
  142. * NutTraceInit
  143. ******************************************************************************/
  144. /**
  145. * Initializes the trace buffer and activates tracing.
  146. * Starts timer 1 for microsecond clock, enables interrupt on overflow
  147. *
  148. *
  149. * @param size Number of items in the trace buffer
  150. * @param mode Mode of operation
  151. * - #TRACE_MODE_CIRCULAR Trace buffer wraps around when full
  152. * - #TRACE_MODE_ONESHOT Traceing is stopped when buffer is full
  153. *
  154. * @return int Status
  155. */
  156. extern int NutTraceInit(int size, char mode);
  157. /*******************************************************************************
  158. * NutTraceStop
  159. ******************************************************************************/
  160. /**
  161. * Sets trace_mode to #TRACE_MODE_OFF and thus stop tracing
  162. */
  163. extern void NutTraceStop(void);
  164. /*******************************************************************************
  165. * NutTracePrint
  166. ******************************************************************************/
  167. /**
  168. * Prints the current contents of the trace buffer
  169. *
  170. * @param size can be used to limit the number of printed items,
  171. * if size==0, then all items of the buffer are printed
  172. */
  173. extern void NutTracePrint(int size);
  174. /*******************************************************************************
  175. * NutTraceTerminal
  176. ******************************************************************************/
  177. /**
  178. * Commands to manipulate the tracing facility
  179. *
  180. * @param arg String containing the commands
  181. * - print &lt;size&gt; calls NutPrintTrace
  182. * - oneshot restarts tracing in the oneshot mode
  183. * - circular restarts tracing in the ciruclar mode
  184. * - size &lt;size&gt; restarts tracing in the current mode, using a buffer of length &lt;size&gt;
  185. * - stop stops tracing
  186. */
  187. extern void NutTraceTerminal(char* arg);
  188. /*******************************************************************************
  189. * NutTraceGetPC
  190. ******************************************************************************/
  191. /**
  192. * Returns the program counter (PC) of the instruction following NutGetPC
  193. * The .map file of the application can be used to find the actual C Code
  194. *
  195. * WARNING: Most likely only works on AVR. Works by inspecting the stack, thus the function
  196. * breaks when local variables are introduced in NutGetPC
  197. *
  198. * @return int Program counter
  199. */
  200. extern int NutTraceGetPC(void);
  201. /*******************************************************************************
  202. * NutTraceClear
  203. ******************************************************************************/
  204. /**
  205. * Clears the trace buffer, but leaves trace_mode unaltered.
  206. */
  207. extern void NutTraceClear(void);
  208. /*******************************************************************************
  209. * NutTraceMaskPrint
  210. ******************************************************************************/
  211. /**
  212. * Prints the current state of trace_mask, which for every type of event (TRACE_TAG_XXX)
  213. * determines, whether they are inserted in the trace buffer or not.
  214. */
  215. extern void NutTraceMaskPrint(void);
  216. /*******************************************************************************
  217. * NutTraceMaskClear
  218. ******************************************************************************/
  219. /**
  220. * Disables tracing of a particular event type
  221. *
  222. * @param tag of event to disable
  223. */
  224. extern void NutTraceMaskClear(int tag);
  225. /*******************************************************************************
  226. * NutTraceMaskSet
  227. ******************************************************************************/
  228. /**
  229. * Enables tracing of a particular event type
  230. *
  231. * @param tag of event to enable
  232. */
  233. extern void NutTraceMaskSet(int tag);
  234. /*******************************************************************************
  235. * NutTraceStatusPrint
  236. ******************************************************************************/
  237. /**
  238. * Prints current status of tracing facility
  239. */
  240. extern void NutTraceStatusPrint(void);
  241. /*******************************************************************************
  242. * NutTraceRegisterUserTag
  243. ******************************************************************************/
  244. /**
  245. * Registers a user event type
  246. *
  247. * @param tag of the new event type (e.g. #define TRACE_USER_SEND 0)
  248. * @param tag_string name of the event type used when printing the trace buffer
  249. */
  250. extern int NutTraceRegisterUserTag(int tag, char* tag_string);
  251. /**
  252. * Macro to insert an event in the trace buffer
  253. *
  254. * @param TAG Type of event
  255. * @param PC Additional information, depending on the type of event
  256. */
  257. #define TRACE_ADD_ITEM(TAG,PC) \
  258. if ((trace_mode != TRACE_MODE_OFF) && \
  259. (trace_mask[TAG] == 1)) \
  260. { \
  261. asm volatile( \
  262. "in __tmp_reg__, __SREG__" "\n\t" \
  263. "push __tmp_reg__" "\n\t" \
  264. "cli" "\n\t" \
  265. ); \
  266. trace_current = &trace_items[trace_head++]; \
  267. trace_current->tag = TAG; \
  268. trace_current->pc = PC; \
  269. trace_current->time_h = micros_high; \
  270. trace_current->time_l = TCNT1; \
  271. if (trace_head >= trace_size) { \
  272. trace_isfull = 1; \
  273. trace_head = 0; \
  274. if (trace_mode == TRACE_MODE_ONESHOT) \
  275. trace_mode = TRACE_MODE_OFF; \
  276. } \
  277. asm volatile( \
  278. "pop __tmp_reg__" "\n\t" \
  279. "out __SREG__, __tmp_reg__" "\n\t" \
  280. ); \
  281. }
  282. /**
  283. * Macro to insert an event in the trace buffer,
  284. * filling the additional information field with the program counter (PC)
  285. *
  286. * @param TAG Type of event
  287. */
  288. #define TRACE_ADD_ITEM_PC(TAG) TRACE_ADD_ITEM(TAG,NutTraceGetPC())
  289. #endif