chtrace.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
  3. This file is part of ChibiOS.
  4. ChibiOS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. ChibiOS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file chtrace.h
  17. * @brief Tracer macros and structures.
  18. *
  19. * @addtogroup trace
  20. * @{
  21. */
  22. #ifndef CHTRACE_H
  23. #define CHTRACE_H
  24. /*===========================================================================*/
  25. /* Module constants. */
  26. /*===========================================================================*/
  27. /**
  28. * @name Trace record types
  29. * @{
  30. */
  31. #define CH_TRACE_TYPE_UNUSED 0U
  32. #define CH_TRACE_TYPE_SWITCH 1U
  33. #define CH_TRACE_TYPE_ISR_ENTER 2U
  34. #define CH_TRACE_TYPE_ISR_LEAVE 3U
  35. #define CH_TRACE_TYPE_HALT 4U
  36. #define CH_TRACE_TYPE_USER 5U
  37. /** @} */
  38. /**
  39. * @name Events to trace
  40. * @{
  41. */
  42. #define CH_DBG_TRACE_MASK_DISABLED 255U
  43. #define CH_DBG_TRACE_MASK_NONE 0U
  44. #define CH_DBG_TRACE_MASK_SWITCH 1U
  45. #define CH_DBG_TRACE_MASK_ISR 2U
  46. #define CH_DBG_TRACE_MASK_HALT 4U
  47. #define CH_DBG_TRACE_MASK_USER 8U
  48. #define CH_DBG_TRACE_MASK_SLOW (CH_DBG_TRACE_MASK_SWITCH | \
  49. CH_DBG_TRACE_MASK_HALT | \
  50. CH_DBG_TRACE_MASK_USER)
  51. #define CH_DBG_TRACE_MASK_ALL (CH_DBG_TRACE_MASK_SWITCH | \
  52. CH_DBG_TRACE_MASK_ISR | \
  53. CH_DBG_TRACE_MASK_HALT | \
  54. CH_DBG_TRACE_MASK_USER)
  55. /** @} */
  56. /*===========================================================================*/
  57. /* Module pre-compile time settings. */
  58. /*===========================================================================*/
  59. /**
  60. * @name Debug related settings
  61. * @{
  62. */
  63. /**
  64. * @brief Trace buffer entries.
  65. */
  66. #if !defined(CH_DBG_TRACE_MASK) || defined(__DOXYGEN__)
  67. #define CH_DBG_TRACE_MASK CH_DBG_TRACE_MASK_DISABLED
  68. #endif
  69. /**
  70. * @brief Trace buffer entries.
  71. * @note The trace buffer is only allocated if @p CH_DBG_TRACE_MASK is
  72. * different from @p CH_DBG_TRACE_MASK_DISABLED.
  73. */
  74. #if !defined(CH_DBG_TRACE_BUFFER_SIZE) || defined(__DOXYGEN__)
  75. #define CH_DBG_TRACE_BUFFER_SIZE 128
  76. #endif
  77. /** @} */
  78. /*===========================================================================*/
  79. /* Derived constants and error checks. */
  80. /*===========================================================================*/
  81. /*===========================================================================*/
  82. /* Module data structures and types. */
  83. /*===========================================================================*/
  84. #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
  85. /*lint -save -e46 [6.1] An uint32_t is required.*/
  86. /**
  87. * @brief Trace buffer record.
  88. */
  89. typedef struct {
  90. /**
  91. * @brief Record type.
  92. */
  93. uint32_t type:3;
  94. /**
  95. * @brief Switched out thread state.
  96. */
  97. uint32_t state:5;
  98. /**
  99. * @brief Accurate time stamp.
  100. * @note This field only available if the post supports
  101. * @p PORT_SUPPORTS_RT else it is set to zero.
  102. */
  103. uint32_t rtstamp:24;
  104. /**
  105. * @brief System time stamp of the switch event.
  106. */
  107. systime_t time;
  108. union {
  109. /**
  110. * @brief Structure representing a context switch.
  111. */
  112. struct {
  113. /**
  114. * @brief Switched in thread.
  115. */
  116. thread_t *ntp;
  117. /**
  118. * @brief Object where going to sleep.
  119. */
  120. void *wtobjp;
  121. } sw;
  122. /**
  123. * @brief Structure representing an ISR enter.
  124. */
  125. struct {
  126. /**
  127. * @brief ISR function name taken using @p __func__.
  128. */
  129. const char *name;
  130. } isr;
  131. /**
  132. * @brief Structure representing an halt.
  133. */
  134. struct {
  135. /**
  136. * @brief Halt error string.
  137. */
  138. const char *reason;
  139. } halt;
  140. /**
  141. * @brief User trace structure.
  142. */
  143. struct {
  144. /**
  145. * @brief Trace user parameter 1.
  146. */
  147. void *up1;
  148. /**
  149. * @brief Trace user parameter 2.
  150. */
  151. void *up2;
  152. } user;
  153. } u;
  154. } ch_trace_event_t;
  155. /*lint -restore*/
  156. /**
  157. * @brief Trace buffer header.
  158. */
  159. typedef struct {
  160. /**
  161. * @brief Suspended trace sources mask.
  162. */
  163. uint16_t suspended;
  164. /**
  165. * @brief Trace buffer size (entries).
  166. */
  167. uint16_t size;
  168. /**
  169. * @brief Pointer to the buffer front.
  170. */
  171. ch_trace_event_t *ptr;
  172. /**
  173. * @brief Ring buffer.
  174. */
  175. ch_trace_event_t buffer[CH_DBG_TRACE_BUFFER_SIZE];
  176. } ch_trace_buffer_t;
  177. #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
  178. /*===========================================================================*/
  179. /* Module macros. */
  180. /*===========================================================================*/
  181. /* When a trace feature is disabled the associated functions are replaced by
  182. an empty macro. Note that the macros can be externally redefined in
  183. order to interface 3rd parties tracing tools.*/
  184. #if CH_DBG_TRACE_MASK == CH_DBG_TRACE_MASK_DISABLED
  185. #if !defined(_trace_init)
  186. #define _trace_init()
  187. #endif
  188. #if !defined(_trace_switch)
  189. #define _trace_switch(ntp, otp)
  190. #endif
  191. #if !defined(_trace_isr_enter)
  192. #define _trace_isr_enter(isr)
  193. #endif
  194. #if !defined(_trace_isr_leave)
  195. #define _trace_isr_leave(isr)
  196. #endif
  197. #if !defined(_trace_halt)
  198. #define _trace_halt(reason)
  199. #endif
  200. #if !defined(chDbgWriteTraceI)
  201. #define chDbgWriteTraceI(up1, up2)
  202. #endif
  203. #if !defined(chDbgWriteTrace)
  204. #define chDbgWriteTrace(up1, up2)
  205. #endif
  206. #endif /* CH_DBG_TRACE_MASK == CH_DBG_TRACE_MASK_DISABLED */
  207. /*===========================================================================*/
  208. /* External declarations. */
  209. /*===========================================================================*/
  210. #ifdef __cplusplus
  211. extern "C" {
  212. #endif
  213. #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
  214. void _trace_init(void);
  215. void _trace_switch(thread_t *ntp, thread_t *otp);
  216. void _trace_isr_enter(const char *isr);
  217. void _trace_isr_leave(const char *isr);
  218. void _trace_halt(const char *reason);
  219. void chDbgWriteTraceI(void *up1, void *up2);
  220. void chDbgWriteTrace(void *up1, void *up2);
  221. void chDbgSuspendTraceI(uint16_t mask);
  222. void chDbgSuspendTrace(uint16_t mask);
  223. void chDbgResumeTraceI(uint16_t mask);
  224. void chDbgResumeTrace(uint16_t mask);
  225. #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. /*===========================================================================*/
  230. /* Module inline functions. */
  231. /*===========================================================================*/
  232. #endif /* CHTRACE_H */
  233. /** @} */