esp_app_trace.h 9.7 KB

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