esp_app_trace.h 10 KB

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