sys_debug.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef __R_DEBUG_H__
  30. #define __R_DEBUG_H__
  31. #include <errno.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <stdbool.h>
  36. #define R_DEBUG_ON
  37. /*
  38. * @brief Debug level
  39. */
  40. #define DBG_LEVEL_MASK (0x0F)
  41. #define R_LEVEL_EMERG 0
  42. #define R_LEVEL_ALERT 1
  43. #define R_LEVEL_CRIT 2
  44. #define R_LEVEL_ERROR 3
  45. #define R_LEVEL_WARNING 4
  46. #define R_LEVEL_NOTICE 5
  47. #define R_LEVEL_INFO 6
  48. #define R_LEVEL_DEBUG 7
  49. #define R_LEVEL_ALL 0x0F
  50. /*
  51. * No expanded condition
  52. */
  53. #define NOEXPAND 1
  54. /*
  55. * module ON/OFF
  56. */
  57. #define DBG_ON (1 << 4)
  58. #define DBG_OFF (0)
  59. /*
  60. * Always show message
  61. */
  62. #define MOD_DBG_ALW_ON (DBG_ON | R_LEVEL_ALL)
  63. /************************************************************
  64. * R_DEBUG INTERFACE
  65. ************************************************************/
  66. #ifdef R_DEBUG_ON
  67. #define R_DEBUG_PRINT(msg, arg...) printf(msg, ##arg)
  68. #define R_DEBUG_ABORT() \
  69. do { \
  70. printf("system aborted!"); \
  71. sys_abort(); \
  72. } while (0)
  73. /*
  74. * @brief The realization of showing debug messages.
  75. * @param module: Contained a module On/Off and a module debug level.
  76. * @param dlevel: Debug message showed level for seal like MDEBUG.
  77. * @param expand: Expanded condition if level param and module ON/OFF are not
  78. * enough for developer.
  79. * @param msg: The debug message.
  80. * @param arg: Arguement shown in debug message which like printf arguement.
  81. * @retval None
  82. */
  83. #define _R_DEBUG(module, dlevel, expand, msg, arg...) \
  84. do { \
  85. if ( \
  86. ((module) & DBG_ON) && \
  87. (((module) & DBG_LEVEL_MASK) >= dlevel) && \
  88. (expand)) { \
  89. R_DEBUG_PRINT(msg, ##arg); \
  90. } \
  91. } while(0)
  92. /*
  93. * @brief The realization of showing debug messages and it can't be turn off by
  94. * module ON/OFF.
  95. * @param module: Contained a module On/Off and a module debug level.
  96. * @param dlevel: Debug message showed level for seal.
  97. * @param expand: Expanded condition if level param is not enough for developer.
  98. * @param msg: The debug message.
  99. * @param arg: Arguement shown in debug message which like printf arguement.
  100. * @retval None
  101. */
  102. #define _R_INFO(module, dlevel, expand, msg, arg...) \
  103. do { \
  104. if ( \
  105. (((int16_t)(module) & DBG_LEVEL_MASK) >= dlevel) && \
  106. (expand)) { \
  107. R_DEBUG_PRINT(msg, ##arg); \
  108. } \
  109. } while(0)
  110. /*
  111. * @brief The realization of assert debug messages shown the assert position,
  112. * for example: "[Assert] At module_debug.h line 112 fun _MASSERT: **"
  113. * @param module: Contained a module On/Off and a module debug level.
  114. * @param dlevel: Debug message showed level for seal.
  115. * @param msg: The debug message.
  116. * @param arg: Arguement shown in debug message which like printf arguement.
  117. * @retval None
  118. */
  119. #define _R_ASSERT(assert, module, dlevel, msg, arg...) \
  120. _R_DEBUG(module, dlevel, !(assert), \
  121. "[Assert] At %s line %d fun %s: " msg, \
  122. __FILE__, __LINE__, __func__, ##arg)
  123. /*
  124. * @brief The realization of assert debug messages shown the assert position,
  125. * and abort. for example: "[Assert] At module_debug.h line 112 fun
  126. * _MASSERT: ***"
  127. * @param module: Contained a module On/Off and a module debug level.
  128. * @param dlevel: Debug message showed level for seal.
  129. * @param msg: The debug message.
  130. * @param arg: Arguement shown in debug message which like printf arguement.
  131. * @retval None
  132. */
  133. #define _R_ASSERT_ABORT(assert, module, dlevel, msg, arg...) \
  134. do { \
  135. if ((((int16_t)(module) & DBG_LEVEL_MASK) >= dlevel) && !(assert)) { \
  136. R_DEBUG_PRINT("[Assert] At %s line %d fun %s: " msg, \
  137. __FILE__, __LINE__, __func__, ##arg); \
  138. R_DEBUG_ABORT(); \
  139. } \
  140. } while(0)
  141. /*
  142. * @brief A level debug message
  143. * @param module: Contained a module On/Off and a module debug level.
  144. * @param expand: Expanded condition if level param and module ON/OFF are not
  145. * enough for developer.
  146. * @param msg: The debug message.
  147. * @param arg: Arguement shown in debug message which like printf arguement.
  148. * @retval None
  149. */
  150. #define R_ERROR(module, expand, msg, arg...) \
  151. _R_DEBUG(module, R_LEVEL_ERROR, expand, msg, ##arg)
  152. #define R_ALERT(module, expand, msg, arg...) \
  153. _R_DEBUG(module, R_LEVEL_ALERT, expand, msg, ##arg)
  154. #define R_CRIT(module, expand, msg, arg...) \
  155. _R_DEBUG(module, R_LEVEL_CRIT, expand, msg, ##arg)
  156. #define R_EMERG(module, expand, msg, arg...) \
  157. _R_DEBUG(module, R_LEVEL_EMERG, R_xpand, msg, ##arg)
  158. #define R_WARN(module, expand, msg, arg...) \
  159. _R_DEBUG(module, R_LEVEL_WARNING, expand, msg, ##arg)
  160. #define R_NOTICE(module, expand, msg, arg...) \
  161. _R_DEBUG(module, R_LEVEL_NOTICE, expand, msg, ##arg)
  162. #define R_INFO(module, expand, msg, arg...) \
  163. _R_DEBUG(module, R_LEVEL_INFO, expand, msg, ##arg)
  164. #define R_DEBUG(module, expand, msg, arg...) \
  165. _R_DEBUG(module, R_LEVEL_DEBUG, expand, msg, ##arg)
  166. /*
  167. * @brief Assert a full debug message with position(file, line, etc.) without level.
  168. * for example: "[Assert] At module_debug.h line 112 fun _MASSERT: ***"
  169. * @param assert: Debug condition
  170. * @param module: Contained a module On/Off at least.
  171. * @param msg: The debug message.
  172. * @param arg: Arguement shown in debug message which like printf arguement.
  173. * @retval None
  174. */
  175. #define R_ASSERT(assert, module, msg, arg...) \
  176. _R_ASSERT(assert, module, R_LEVEL_ALL, msg, ##arg)
  177. #define R_ASSERT_ABORT(assert, module, msg, arg...) \
  178. _R_ASSERT_ABORT(assert, module, R_LEVEL_ALL, msg, ##arg)
  179. /*
  180. * @brief Assert a full debug message with position(file, line, etc.) and
  181. * error number without level. for example:
  182. * "[Assert] At module_debug.h line 112 fun _MASSERT: condition p != NULL is fault. errno is 115."
  183. * @param condition: It will assert a message if condition is fault.
  184. * @retval Nuon
  185. */
  186. #ifndef assert
  187. #define assert(condition) R_ASSERT(condition, MOD_DBG_ALW_ON, "condition %s is fault. errno is %d.\n", #condition, r_thread_errno)
  188. #endif
  189. /*
  190. // THIS REALIZATION DO NOT SEAL
  191. #define R_ASSERT(assert, module, msg, arg...) \
  192. _R_ASSERT(assert, module, R_LEVEL_ALL, "[%s]" msg, #module, ##arg)
  193. #define R_ASSERT_ABORT(assert, module, msg, arg...) \
  194. _R_ASSERT_ABORT(assert, module, R_LEVEL_ALL, "[%s]" msg, #module, ##arg)
  195. */
  196. /*
  197. * @brief notify the function entry and exit/return in the debug level
  198. * @param module: Contained a module On/Off at least.
  199. * @param mname: module name in string
  200. * @param ret: return value
  201. * @retval None
  202. */
  203. #define R_ENTRY(module, mname) \
  204. R_DEBUG(module, NOEXPAND, mname "entry %s().\n", __func__)
  205. #define R_RET(module, mname, ret) \
  206. R_DEBUG(module, NOEXPAND, mname "exit %s() with return %d.\n", __func__, ret)
  207. #define R_RET_NOVAL(module, mname) \
  208. R_DEBUG(module, NOEXPAND, mname "exit %s().\n", __func__)
  209. #else /* MDEBUG_ON */
  210. #define R_DEBUG_PRINT(msg, arg...)
  211. #define R_DEBUG_ABORT()
  212. #define _R_DEBUG(module, dlevel, expand, msg, arg...)
  213. #define _R_INFO(module, dlevel, expand, msg, arg...)
  214. #define _R_ASSERT(assert, module, dlevel, msg, arg...)
  215. #define _R_ASSERT_ABORT(assert, module, dlevel, msg, arg...)
  216. #define R_ERROR(module, expand, msg, arg...)
  217. #define R_ALERT(module, expand, msg, arg...)
  218. #define R_CRIT(module, expand, msg, arg...)
  219. #define R_EMERG(module, expand, msg, arg...)
  220. #define R_WARN(module, expand, msg, arg...)
  221. #define R_NOTICE(module, expand, msg, arg...)
  222. #define R_INFO(module, expand, msg, arg...)
  223. #define R_DEBUG(module, expand, msg, arg...)
  224. #define R_ASSERT(assert, module, msg, arg...)
  225. #define R_ASSERT_ABORT(assert, module, msg, arg...)
  226. #ifndef assert
  227. #define assert(condition)
  228. #endif
  229. #define R_ENTRY(module, mname)
  230. #define R_RET(module, mname, ret)
  231. #define R_RET_NOVAL(module, mname)
  232. #endif /* R_DEBUG_ON */
  233. #define ROM_DUMP_MASK (1 << 0)
  234. #define ROM_DBG_MASK (1 << 1)
  235. #define ROM_INF_MASK (1 << 2)
  236. #define ROM_WRN_MASK (1 << 3)
  237. #define ROM_ERR_MASK (1 << 4)
  238. #define ROM_ANY_MASK (1 << 5)
  239. #define ROM_ABORT_MASK (1 << 6)
  240. #define ROM_TOTAL_MASKS (ROM_DUMP_MASK | ROM_DBG_MASK | ROM_INF_MASK | \
  241. ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK | \
  242. ROM_ABORT_MASK)
  243. enum {
  244. DUMP_PREFIX_NONE,
  245. DUMP_PREFIX_ADDRESS,
  246. DUMP_PREFIX_OFFSET
  247. };
  248. extern int hex_to_bin(char ch);
  249. extern int hex2bin(unsigned char *dst, const char *src, size_t count);
  250. extern void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
  251. int groupsize, char *linebuf, size_t linebuflen,
  252. bool ascii);
  253. extern void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
  254. int groupsize, char *linebuf, size_t linebuflen,
  255. bool ascii);
  256. extern void print_hex_dump(const char *prefix_str, int prefix_type,
  257. int rowsize, int groupsize,
  258. const void *buf, size_t len, bool ascii);
  259. extern void print_hex_dump_words(const void *addr, unsigned int len);
  260. /*
  261. //$ Example of r_debug from mqtt $
  262. #define MQTT_MODULE (DBG_ON | R_LEVEL_DEBUG)
  263. #ifdef MOTT_ASSERT_ON
  264. #define MQTT_ASSERT(assert, msg, arg...) R_ALERT(MOD_DBG_ALW_ON, (assert), "[MQTT assert] "msg, ##arg)
  265. #else
  266. #define MQTT_ASSERT(assert, msg, arg...)
  267. #endif
  268. #ifdef MQTT_DBG_ON
  269. #define MQTT_INFO(msg, arg...) R_INFO(MQTT_MODULE, NOEXPAND, "[MQTT info] " msg, ##arg)
  270. #define MQTT_WARN(msg, arg...) R_WARN(MQTT_MODULE, NOEXPAND, "[MQTT warning] " msg, ##arg)
  271. #define MQTT_DEBUG(msg, arg...) R_DEBUG(MQTT_MODULE, NOEXPAND, "[MQTT debug] " msg, ##arg)
  272. #define MQTT_ENTRY() R_ENTRY(MQTT_MODULE, "[MQTT entry] ")
  273. #define MQTT_EXIT(ret) R_RET(MQTT_MODULE, "[MQTT return] ", ret)
  274. #else
  275. #define MQTT_INFO(msg, arg...)
  276. #define MQTT_WARN(msg, arg...)
  277. #define MQTT_DEBUG(msg, arg...)
  278. #define MQTT_ENTRY()
  279. #define MQTT_EXIT(ret)
  280. #endif
  281. */
  282. extern void print_hex_dump_bytes(const void *addr, unsigned int len);
  283. extern void print_hex_dump_words(const void *addr, unsigned int len);
  284. #endif /* __R_DEBUG_H__ */