app_trace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_log.h"
  8. #include "esp_app_trace.h"
  9. #include "esp_app_trace_port.h"
  10. #define ESP_APPTRACE_MAX_VPRINTF_ARGS 256
  11. #define ESP_APPTRACE_HOST_BUF_SIZE 256
  12. #define ESP_APPTRACE_PRINT_LOCK 0
  13. const static char *TAG = "esp_apptrace";
  14. /** tracing module internal data */
  15. typedef struct {
  16. esp_apptrace_hw_t * hw;
  17. void * hw_data;
  18. } esp_apptrace_channel_t;
  19. static esp_apptrace_channel_t s_trace_channels[ESP_APPTRACE_DEST_NUM];
  20. static bool s_inited;
  21. esp_err_t esp_apptrace_init(void)
  22. {
  23. int res;
  24. esp_apptrace_hw_t *hw = NULL;
  25. void *hw_data = NULL;
  26. // 'esp_apptrace_init()' is called on every core, so ensure to do main initialization only once
  27. if (cpu_hal_get_core_id() == 0) {
  28. memset(&s_trace_channels, 0, sizeof(s_trace_channels));
  29. hw = esp_apptrace_jtag_hw_get(&hw_data);
  30. ESP_APPTRACE_LOGD("HW interface %p", hw);
  31. if (hw != NULL) {
  32. s_trace_channels[ESP_APPTRACE_DEST_JTAG].hw = hw;
  33. s_trace_channels[ESP_APPTRACE_DEST_JTAG].hw_data = hw_data;
  34. }
  35. hw = esp_apptrace_uart_hw_get(0, &hw_data);
  36. if (hw != NULL) {
  37. s_trace_channels[ESP_APPTRACE_DEST_UART0].hw = hw;
  38. s_trace_channels[ESP_APPTRACE_DEST_UART0].hw_data = hw_data;
  39. }
  40. s_inited = true;
  41. }
  42. // esp_apptrace_init() is called on every core, so initialize trace channel on every core
  43. for (int i = 0; i < sizeof(s_trace_channels)/sizeof(s_trace_channels[0]); i++) {
  44. esp_apptrace_channel_t *ch = &s_trace_channels[i];
  45. if (ch->hw) {
  46. res = ch->hw->init(ch->hw_data);
  47. if (res != ESP_OK) {
  48. ESP_APPTRACE_LOGE("Failed to init trace channel HW interface (%d)!", res);
  49. return res;
  50. }
  51. }
  52. }
  53. return ESP_OK;
  54. }
  55. void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)
  56. {
  57. esp_apptrace_channel_t *ch;
  58. if (!s_inited) {
  59. return;
  60. }
  61. // currently down buffer is supported for JTAG interface only
  62. // TODO: one more argument should be added to this function to specify HW inteface: JTAG, UART0 etc
  63. ch = &s_trace_channels[ESP_APPTRACE_DEST_JTAG];
  64. if (ch->hw == NULL) {
  65. ESP_APPTRACE_LOGE("Trace destination not supported!");
  66. return;
  67. }
  68. if (ch->hw->down_buffer_config == NULL) {
  69. return;
  70. }
  71. ch->hw->down_buffer_config(ch->hw_data, buf, size);
  72. }
  73. uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t user_tmo)
  74. {
  75. esp_apptrace_tmo_t tmo;
  76. esp_apptrace_channel_t *ch;
  77. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  78. if (dest >= ESP_APPTRACE_DEST_MAX) {
  79. return NULL;
  80. }
  81. if (size == NULL || *size == 0) {
  82. return NULL;
  83. }
  84. if (!s_inited) {
  85. return NULL;
  86. }
  87. ch = &s_trace_channels[dest];
  88. if (ch->hw == NULL) {
  89. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  90. return NULL;
  91. }
  92. if (ch->hw->get_down_buffer == NULL) {
  93. return NULL;
  94. }
  95. esp_apptrace_tmo_init(&tmo, user_tmo);
  96. return ch->hw->get_down_buffer(ch->hw_data, size, &tmo);
  97. }
  98. esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  99. {
  100. esp_apptrace_tmo_t tmo;
  101. esp_apptrace_channel_t *ch;
  102. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  103. if (dest >= ESP_APPTRACE_DEST_MAX) {
  104. return ESP_ERR_INVALID_ARG;
  105. }
  106. if (ptr == NULL) {
  107. return ESP_ERR_INVALID_ARG;
  108. }
  109. if (!s_inited) {
  110. return ESP_ERR_INVALID_STATE;
  111. }
  112. ch = &s_trace_channels[dest];
  113. if (ch->hw == NULL) {
  114. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  115. return ESP_ERR_NOT_SUPPORTED;
  116. }
  117. if (ch->hw->get_down_buffer == NULL) {
  118. return ESP_ERR_NOT_SUPPORTED;
  119. }
  120. esp_apptrace_tmo_init(&tmo, user_tmo);
  121. return ch->hw->put_down_buffer(ch->hw_data, ptr, &tmo);
  122. }
  123. esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *buf, uint32_t *size, uint32_t user_tmo)
  124. {
  125. int res = ESP_OK;
  126. esp_apptrace_tmo_t tmo;
  127. esp_apptrace_channel_t *ch;
  128. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  129. if (dest >= ESP_APPTRACE_DEST_MAX) {
  130. return ESP_ERR_INVALID_ARG;
  131. }
  132. if (buf == NULL || size == NULL || *size == 0) {
  133. return ESP_ERR_INVALID_ARG;
  134. }
  135. if (!s_inited) {
  136. return ESP_ERR_INVALID_STATE;
  137. }
  138. ch = &s_trace_channels[dest];
  139. if (ch->hw == NULL) {
  140. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  141. return ESP_ERR_NOT_SUPPORTED;
  142. }
  143. if (ch->hw->get_down_buffer == NULL || ch->hw->put_down_buffer == NULL) {
  144. return ESP_ERR_NOT_SUPPORTED;
  145. }
  146. //TODO: callback system
  147. esp_apptrace_tmo_init(&tmo, user_tmo);
  148. uint32_t act_sz = *size;
  149. *size = 0;
  150. uint8_t * ptr = ch->hw->get_down_buffer(ch->hw_data, &act_sz, &tmo);
  151. if (ptr && act_sz > 0) {
  152. ESP_APPTRACE_LOGD("Read %d bytes from host", act_sz);
  153. memcpy(buf, ptr, act_sz);
  154. res = ch->hw->put_down_buffer(ch->hw_data, ptr, &tmo);
  155. *size = act_sz;
  156. } else {
  157. res = ESP_ERR_TIMEOUT;
  158. }
  159. return res;
  160. }
  161. uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t user_tmo)
  162. {
  163. esp_apptrace_tmo_t tmo;
  164. esp_apptrace_channel_t *ch;
  165. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  166. if (dest >= ESP_APPTRACE_DEST_MAX) {
  167. return NULL;
  168. }
  169. if (size == 0) {
  170. return NULL;
  171. }
  172. if (!s_inited) {
  173. return NULL;
  174. }
  175. ch = &s_trace_channels[dest];
  176. if (ch->hw == NULL) {
  177. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  178. return NULL;
  179. }
  180. if (ch->hw->get_up_buffer == NULL) {
  181. return NULL;
  182. }
  183. esp_apptrace_tmo_init(&tmo, user_tmo);
  184. return ch->hw->get_up_buffer(ch->hw_data, size, &tmo);
  185. }
  186. esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  187. {
  188. esp_apptrace_tmo_t tmo;
  189. esp_apptrace_channel_t *ch;
  190. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  191. if (dest >= ESP_APPTRACE_DEST_MAX) {
  192. return ESP_ERR_INVALID_ARG;
  193. }
  194. if (ptr == NULL) {
  195. return ESP_ERR_INVALID_ARG;
  196. }
  197. if (!s_inited) {
  198. return ESP_ERR_INVALID_STATE;
  199. }
  200. ch = &s_trace_channels[dest];
  201. if (ch->hw == NULL) {
  202. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  203. return ESP_ERR_NOT_SUPPORTED;
  204. }
  205. if (ch->hw->put_up_buffer == NULL) {
  206. return ESP_ERR_NOT_SUPPORTED;
  207. }
  208. esp_apptrace_tmo_init(&tmo, user_tmo);
  209. return ch->hw->put_up_buffer(ch->hw_data, ptr, &tmo);
  210. }
  211. esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t user_tmo)
  212. {
  213. uint8_t *ptr = NULL;
  214. esp_apptrace_tmo_t tmo;
  215. esp_apptrace_channel_t *ch;
  216. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  217. if (dest >= ESP_APPTRACE_DEST_MAX) {
  218. return ESP_ERR_INVALID_ARG;
  219. }
  220. if (data == NULL || size == 0) {
  221. return ESP_ERR_INVALID_ARG;
  222. }
  223. if (!s_inited) {
  224. return ESP_ERR_INVALID_STATE;
  225. }
  226. ch = &s_trace_channels[dest];
  227. if (ch->hw == NULL) {
  228. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  229. return ESP_ERR_NOT_SUPPORTED;
  230. }
  231. if (ch->hw->get_up_buffer == NULL || ch->hw->put_up_buffer == NULL) {
  232. return ESP_ERR_NOT_SUPPORTED;
  233. }
  234. esp_apptrace_tmo_init(&tmo, user_tmo);
  235. ptr = ch->hw->get_up_buffer(ch->hw_data, size, &tmo);
  236. if (ptr == NULL) {
  237. return ESP_ERR_NO_MEM;
  238. }
  239. // actually can be suspended here by higher prio tasks/ISRs
  240. //TODO: use own memcpy with dead trace calls kick-off algo and tmo expiration check
  241. memcpy(ptr, data, size);
  242. // now indicate that this buffer is ready to be sent off to host
  243. return ch->hw->put_up_buffer(ch->hw_data, ptr, &tmo);
  244. }
  245. int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t user_tmo, const char *fmt, va_list ap)
  246. {
  247. uint16_t nargs = 0;
  248. uint8_t *pout, *p = (uint8_t *)fmt;
  249. esp_apptrace_tmo_t tmo;
  250. esp_apptrace_channel_t *ch;
  251. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  252. if (dest >= ESP_APPTRACE_DEST_MAX) {
  253. return -1;
  254. }
  255. if (fmt == NULL) {
  256. return -1;
  257. }
  258. if (!s_inited) {
  259. return -1;
  260. }
  261. ch = &s_trace_channels[dest];
  262. if (ch->hw == NULL) {
  263. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  264. return -1;
  265. }
  266. if (ch->hw->get_up_buffer == NULL || ch->hw->put_up_buffer == NULL) {
  267. return -1;
  268. }
  269. esp_apptrace_tmo_init(&tmo, user_tmo);
  270. ESP_APPTRACE_LOGD("fmt %x", fmt);
  271. while ((p = (uint8_t *)strchr((char *)p, '%')) && nargs < ESP_APPTRACE_MAX_VPRINTF_ARGS) {
  272. p++;
  273. if (*p != '%' && *p != 0) {
  274. nargs++;
  275. }
  276. }
  277. ESP_APPTRACE_LOGD("nargs = %d", nargs);
  278. if (p) {
  279. ESP_APPTRACE_LOGE("Failed to store all printf args!");
  280. }
  281. pout = ch->hw->get_up_buffer(ch->hw_data, 1 + sizeof(char *) + nargs * sizeof(uint32_t), &tmo);
  282. if (pout == NULL) {
  283. ESP_APPTRACE_LOGE("Failed to get buffer!");
  284. return -1;
  285. }
  286. p = pout;
  287. *pout = nargs;
  288. pout++;
  289. *(const char **)pout = fmt;
  290. pout += sizeof(char *);
  291. while (nargs-- > 0) {
  292. uint32_t arg = va_arg(ap, uint32_t);
  293. *(uint32_t *)pout = arg;
  294. pout += sizeof(uint32_t);
  295. ESP_APPTRACE_LOGD("arg %x", arg);
  296. }
  297. int ret = ch->hw->put_up_buffer(ch->hw_data, p, &tmo);
  298. if (ret != ESP_OK) {
  299. ESP_APPTRACE_LOGE("Failed to put printf buf (%d)!", ret);
  300. return -1;
  301. }
  302. return (pout - p);
  303. }
  304. int esp_apptrace_vprintf(const char *fmt, va_list ap)
  305. {
  306. return esp_apptrace_vprintf_to(ESP_APPTRACE_DEST_JTAG, 0, fmt, ap);
  307. }
  308. esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t usr_tmo)
  309. {
  310. esp_apptrace_tmo_t tmo;
  311. esp_apptrace_channel_t *ch;
  312. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  313. if (dest >= ESP_APPTRACE_DEST_MAX) {
  314. return ESP_ERR_INVALID_ARG;
  315. }
  316. if (!s_inited) {
  317. return ESP_ERR_INVALID_STATE;
  318. }
  319. ch = &s_trace_channels[dest];
  320. if (ch->hw == NULL) {
  321. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  322. return ESP_ERR_NOT_SUPPORTED;
  323. }
  324. if (ch->hw->flush_up_buffer_nolock == NULL) {
  325. return ESP_ERR_NOT_SUPPORTED;
  326. }
  327. esp_apptrace_tmo_init(&tmo, usr_tmo);
  328. return ch->hw->flush_up_buffer_nolock(ch->hw_data, min_sz, &tmo);
  329. }
  330. esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t usr_tmo)
  331. {
  332. esp_apptrace_tmo_t tmo;
  333. esp_apptrace_channel_t *ch;
  334. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  335. if (dest >= ESP_APPTRACE_DEST_MAX) {
  336. return ESP_ERR_INVALID_ARG;
  337. }
  338. if (!s_inited) {
  339. return ESP_ERR_INVALID_STATE;
  340. }
  341. ch = &s_trace_channels[dest];
  342. if (ch->hw == NULL) {
  343. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  344. return ESP_ERR_NOT_SUPPORTED;
  345. }
  346. if (ch->hw->flush_up_buffer == NULL) {
  347. return ESP_ERR_NOT_SUPPORTED;
  348. }
  349. esp_apptrace_tmo_init(&tmo, usr_tmo);
  350. return ch->hw->flush_up_buffer(ch->hw_data, &tmo);
  351. }
  352. bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)
  353. {
  354. esp_apptrace_channel_t *ch;
  355. ESP_APPTRACE_LOGV("%s(): enter", __func__);
  356. if (dest >= ESP_APPTRACE_DEST_MAX) {
  357. return false;
  358. }
  359. if (!s_inited) {
  360. return false;
  361. }
  362. ch = &s_trace_channels[dest];
  363. if (ch->hw == NULL) {
  364. ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest);
  365. return false;
  366. }
  367. if (ch->hw->host_is_connected == NULL) {
  368. return false;
  369. }
  370. return ch->hw->host_is_connected(ch->hw_data);
  371. }