esp_app_trace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ESP_APP_TRACE_H_
  7. #define ESP_APP_TRACE_H_
  8. #include <stdarg.h>
  9. #include "esp_err.h"
  10. #include "esp_app_trace_util.h" // ESP_APPTRACE_TMO_INFINITE
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * Application trace data destinations bits.
  16. */
  17. typedef enum {
  18. ESP_APPTRACE_DEST_JTAG = 1, ///< JTAG destination
  19. ESP_APPTRACE_DEST_TRAX = ESP_APPTRACE_DEST_JTAG, ///< xxx_TRAX name is obsolete, use more common xxx_JTAG
  20. ESP_APPTRACE_DEST_UART, ///< UART destination
  21. ESP_APPTRACE_DEST_MAX = ESP_APPTRACE_DEST_UART+1,
  22. ESP_APPTRACE_DEST_NUM
  23. } esp_apptrace_dest_t;
  24. /**
  25. * @brief Initializes application tracing module.
  26. *
  27. * @note Should be called before any esp_apptrace_xxx call.
  28. *
  29. * @return ESP_OK on success, otherwise see esp_err_t
  30. */
  31. esp_err_t esp_apptrace_init(void);
  32. /**
  33. * @brief Configures down buffer.
  34. * @note Needs to be called before attempting to receive any data using esp_apptrace_down_buffer_get and esp_apptrace_read.
  35. * This function does not protect internal data by lock.
  36. *
  37. * @param buf Address of buffer to use for down channel (host to target) data.
  38. * @param size Size of the buffer.
  39. */
  40. void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size);
  41. /**
  42. * @brief Allocates buffer for trace data.
  43. * Once the data in the buffer is ready to be sent, esp_apptrace_buffer_put must be called to indicate it.
  44. *
  45. * @param dest Indicates HW interface to send data.
  46. * @param size Size of data to write to trace buffer.
  47. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  48. *
  49. * @return non-NULL on success, otherwise NULL.
  50. */
  51. uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t tmo);
  52. /**
  53. * @brief Indicates that the data in the buffer is ready to be sent.
  54. * This function is a counterpart of and must be preceded by esp_apptrace_buffer_get.
  55. *
  56. * @param dest Indicates HW interface to send data. Should be identical to the same parameter in call to esp_apptrace_buffer_get.
  57. * @param ptr Address of trace buffer to release. Should be the value returned by call to esp_apptrace_buffer_get.
  58. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  59. *
  60. * @return ESP_OK on success, otherwise see esp_err_t
  61. */
  62. esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t tmo);
  63. /**
  64. * @brief Writes data to trace buffer.
  65. *
  66. * @param dest Indicates HW interface to send data.
  67. * @param data Address of data to write to trace buffer.
  68. * @param size Size of data to write to trace buffer.
  69. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  70. *
  71. * @return ESP_OK on success, otherwise see esp_err_t
  72. */
  73. esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t tmo);
  74. /**
  75. * @brief vprintf-like function to send log messages to host via specified HW interface.
  76. *
  77. * @param dest Indicates HW interface to send data.
  78. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  79. * @param fmt Address of format string.
  80. * @param ap List of arguments.
  81. *
  82. * @return Number of bytes written.
  83. */
  84. int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t tmo, const char *fmt, va_list ap);
  85. /**
  86. * @brief vprintf-like function to send log messages to host.
  87. *
  88. * @param fmt Address of format string.
  89. * @param ap List of arguments.
  90. *
  91. * @return Number of bytes written.
  92. */
  93. int esp_apptrace_vprintf(const char *fmt, va_list ap);
  94. /**
  95. * @brief Flushes remaining data in trace buffer to host.
  96. *
  97. * @param dest Indicates HW interface to flush data on.
  98. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  99. *
  100. * @return ESP_OK on success, otherwise see esp_err_t
  101. */
  102. esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t tmo);
  103. /**
  104. * @brief Flushes remaining data in trace buffer to host without locking internal data.
  105. * This is a special version of esp_apptrace_flush which should be called from panic handler.
  106. *
  107. * @param dest Indicates HW interface to flush data on.
  108. * @param min_sz Threshold for flushing data. If current filling level is above this value, data will be flushed. TRAX destinations only.
  109. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  110. *
  111. * @return ESP_OK on success, otherwise see esp_err_t
  112. */
  113. esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t tmo);
  114. /**
  115. * @brief Reads host data from trace buffer.
  116. *
  117. * @param dest Indicates HW interface to read the data on.
  118. * @param data Address of buffer to put data from trace buffer.
  119. * @param size Pointer to store size of read data. Before call to this function pointed memory must hold requested size of data
  120. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  121. *
  122. * @return ESP_OK on success, otherwise see esp_err_t
  123. */
  124. esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *data, uint32_t *size, uint32_t tmo);
  125. /**
  126. * @brief Retrieves incoming data buffer if any.
  127. * Once data in the buffer is processed, esp_apptrace_down_buffer_put must be called to indicate it.
  128. *
  129. * @param dest Indicates HW interface to receive data.
  130. * @param size Address to store size of available data in down buffer. Must be initialized with requested value.
  131. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  132. *
  133. * @return non-NULL on success, otherwise NULL.
  134. */
  135. uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t tmo);
  136. /**
  137. * @brief Indicates that the data in the down buffer is processed.
  138. * This function is a counterpart of and must be preceded by esp_apptrace_down_buffer_get.
  139. *
  140. * @param dest Indicates HW interface to receive data. Should be identical to the same parameter in call to esp_apptrace_down_buffer_get.
  141. * @param ptr Address of trace buffer to release. Should be the value returned by call to esp_apptrace_down_buffer_get.
  142. * @param tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.
  143. *
  144. * @return ESP_OK on success, otherwise see esp_err_t
  145. */
  146. esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t tmo);
  147. /**
  148. * @brief Checks whether host is connected.
  149. *
  150. * @param dest Indicates HW interface to use.
  151. *
  152. * @return true if host is connected, otherwise false
  153. */
  154. bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest);
  155. /**
  156. * @brief Opens file on host.
  157. * This function has the same semantic as 'fopen' except for the first argument.
  158. *
  159. * @param dest Indicates HW interface to use.
  160. * @param path Path to file.
  161. * @param mode Mode string. See fopen for details.
  162. *
  163. * @return non zero file handle on success, otherwise 0
  164. */
  165. void *esp_apptrace_fopen(esp_apptrace_dest_t dest, const char *path, const char *mode);
  166. /**
  167. * @brief Closes file on host.
  168. * This function has the same semantic as 'fclose' except for the first argument.
  169. *
  170. * @param dest Indicates HW interface to use.
  171. * @param stream File handle returned by esp_apptrace_fopen.
  172. *
  173. * @return Zero on success, otherwise non-zero. See fclose for details.
  174. */
  175. int esp_apptrace_fclose(esp_apptrace_dest_t dest, void *stream);
  176. /**
  177. * @brief Writes to file on host.
  178. * This function has the same semantic as 'fwrite' except for the first argument.
  179. *
  180. * @param dest Indicates HW interface to use.
  181. * @param ptr Address of data to write.
  182. * @param size Size of an item.
  183. * @param nmemb Number of items to write.
  184. * @param stream File handle returned by esp_apptrace_fopen.
  185. *
  186. * @return Number of written items. See fwrite for details.
  187. */
  188. size_t esp_apptrace_fwrite(esp_apptrace_dest_t dest, const void *ptr, size_t size, size_t nmemb, void *stream);
  189. /**
  190. * @brief Read file on host.
  191. * This function has the same semantic as 'fread' except for the first argument.
  192. *
  193. * @param dest Indicates HW interface to use.
  194. * @param ptr Address to store read data.
  195. * @param size Size of an item.
  196. * @param nmemb Number of items to read.
  197. * @param stream File handle returned by esp_apptrace_fopen.
  198. *
  199. * @return Number of read items. See fread for details.
  200. */
  201. size_t esp_apptrace_fread(esp_apptrace_dest_t dest, void *ptr, size_t size, size_t nmemb, void *stream);
  202. /**
  203. * @brief Set position indicator in file on host.
  204. * This function has the same semantic as 'fseek' except for the first argument.
  205. *
  206. * @param dest Indicates HW interface to use.
  207. * @param stream File handle returned by esp_apptrace_fopen.
  208. * @param offset Offset. See fseek for details.
  209. * @param whence Position in file. See fseek for details.
  210. *
  211. * @return Zero on success, otherwise non-zero. See fseek for details.
  212. */
  213. int esp_apptrace_fseek(esp_apptrace_dest_t dest, void *stream, long offset, int whence);
  214. /**
  215. * @brief Get current position indicator for file on host.
  216. * This function has the same semantic as 'ftell' except for the first argument.
  217. *
  218. * @param dest Indicates HW interface to use.
  219. * @param stream File handle returned by esp_apptrace_fopen.
  220. *
  221. * @return Current position in file. See ftell for details.
  222. */
  223. int esp_apptrace_ftell(esp_apptrace_dest_t dest, void *stream);
  224. /**
  225. * @brief Indicates to the host that all file operations are complete.
  226. * This function should be called after all file operations are finished and
  227. * indicate to the host that it can perform cleanup operations (close open files etc.).
  228. *
  229. * @param dest Indicates HW interface to use.
  230. *
  231. * @return ESP_OK on success, otherwise see esp_err_t
  232. */
  233. int esp_apptrace_fstop(esp_apptrace_dest_t dest);
  234. /**
  235. * @brief Test end-of-file indicator on a stream.
  236. * This function has the same semantic as 'feof' except for the first argument.
  237. *
  238. * @param dest Indicates HW interface to use.
  239. * @param stream File handle returned by esp_apptrace_fopen.
  240. *
  241. * @return Non-Zero if end-of-file indicator is set for stream. See feof for details.
  242. */
  243. int esp_apptrace_feof(esp_apptrace_dest_t dest, void *stream);
  244. /**
  245. * @brief Triggers gcov info dump.
  246. * This function waits for the host to connect to target before dumping data.
  247. */
  248. void esp_gcov_dump(void);
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif