esp_app_trace.h 10 KB

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