debug.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. american fuzzy lop - debug / error handling macros
  3. --------------------------------------------------
  4. Written and maintained by Michal Zalewski <lcamtuf@google.com>
  5. Copyright 2013, 2014, 2015, 2016 Google Inc. All rights reserved.
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at:
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. */
  11. #ifndef _HAVE_DEBUG_H
  12. #define _HAVE_DEBUG_H
  13. #include <errno.h>
  14. #include "types.h"
  15. #include "config.h"
  16. /*******************
  17. * Terminal colors *
  18. *******************/
  19. #ifdef USE_COLOR
  20. # define cBLK "\x1b[0;30m"
  21. # define cRED "\x1b[0;31m"
  22. # define cGRN "\x1b[0;32m"
  23. # define cBRN "\x1b[0;33m"
  24. # define cBLU "\x1b[0;34m"
  25. # define cMGN "\x1b[0;35m"
  26. # define cCYA "\x1b[0;36m"
  27. # define cLGR "\x1b[0;37m"
  28. # define cGRA "\x1b[1;90m"
  29. # define cLRD "\x1b[1;91m"
  30. # define cLGN "\x1b[1;92m"
  31. # define cYEL "\x1b[1;93m"
  32. # define cLBL "\x1b[1;94m"
  33. # define cPIN "\x1b[1;95m"
  34. # define cLCY "\x1b[1;96m"
  35. # define cBRI "\x1b[1;97m"
  36. # define cRST "\x1b[0m"
  37. # define bgBLK "\x1b[40m"
  38. # define bgRED "\x1b[41m"
  39. # define bgGRN "\x1b[42m"
  40. # define bgBRN "\x1b[43m"
  41. # define bgBLU "\x1b[44m"
  42. # define bgMGN "\x1b[45m"
  43. # define bgCYA "\x1b[46m"
  44. # define bgLGR "\x1b[47m"
  45. # define bgGRA "\x1b[100m"
  46. # define bgLRD "\x1b[101m"
  47. # define bgLGN "\x1b[102m"
  48. # define bgYEL "\x1b[103m"
  49. # define bgLBL "\x1b[104m"
  50. # define bgPIN "\x1b[105m"
  51. # define bgLCY "\x1b[106m"
  52. # define bgBRI "\x1b[107m"
  53. #else
  54. # define cBLK ""
  55. # define cRED ""
  56. # define cGRN ""
  57. # define cBRN ""
  58. # define cBLU ""
  59. # define cMGN ""
  60. # define cCYA ""
  61. # define cLGR ""
  62. # define cGRA ""
  63. # define cLRD ""
  64. # define cLGN ""
  65. # define cYEL ""
  66. # define cLBL ""
  67. # define cPIN ""
  68. # define cLCY ""
  69. # define cBRI ""
  70. # define cRST ""
  71. # define bgBLK ""
  72. # define bgRED ""
  73. # define bgGRN ""
  74. # define bgBRN ""
  75. # define bgBLU ""
  76. # define bgMGN ""
  77. # define bgCYA ""
  78. # define bgLGR ""
  79. # define bgGRA ""
  80. # define bgLRD ""
  81. # define bgLGN ""
  82. # define bgYEL ""
  83. # define bgLBL ""
  84. # define bgPIN ""
  85. # define bgLCY ""
  86. # define bgBRI ""
  87. #endif /* ^USE_COLOR */
  88. /*************************
  89. * Box drawing sequences *
  90. *************************/
  91. #ifdef FANCY_BOXES
  92. # define SET_G1 "\x1b)0" /* Set G1 for box drawing */
  93. # define RESET_G1 "\x1b)B" /* Reset G1 to ASCII */
  94. # define bSTART "\x0e" /* Enter G1 drawing mode */
  95. # define bSTOP "\x0f" /* Leave G1 drawing mode */
  96. # define bH "q" /* Horizontal line */
  97. # define bV "x" /* Vertical line */
  98. # define bLT "l" /* Left top corner */
  99. # define bRT "k" /* Right top corner */
  100. # define bLB "m" /* Left bottom corner */
  101. # define bRB "j" /* Right bottom corner */
  102. # define bX "n" /* Cross */
  103. # define bVR "t" /* Vertical, branch right */
  104. # define bVL "u" /* Vertical, branch left */
  105. # define bHT "v" /* Horizontal, branch top */
  106. # define bHB "w" /* Horizontal, branch bottom */
  107. #else
  108. # define SET_G1 ""
  109. # define RESET_G1 ""
  110. # define bSTART ""
  111. # define bSTOP ""
  112. # define bH "-"
  113. # define bV "|"
  114. # define bLT "+"
  115. # define bRT "+"
  116. # define bLB "+"
  117. # define bRB "+"
  118. # define bX "+"
  119. # define bVR "+"
  120. # define bVL "+"
  121. # define bHT "+"
  122. # define bHB "+"
  123. #endif /* ^FANCY_BOXES */
  124. /***********************
  125. * Misc terminal codes *
  126. ***********************/
  127. #define TERM_HOME "\x1b[H"
  128. #define TERM_CLEAR TERM_HOME "\x1b[2J"
  129. #define cEOL "\x1b[0K"
  130. #define CURSOR_HIDE "\x1b[?25l"
  131. #define CURSOR_SHOW "\x1b[?25h"
  132. /************************
  133. * Debug & error macros *
  134. ************************/
  135. /* Just print stuff to the appropriate stream. */
  136. #ifdef MESSAGES_TO_STDOUT
  137. # define SAYF(x...) printf(x)
  138. #else
  139. # define SAYF(x...) fprintf(stderr, x)
  140. #endif /* ^MESSAGES_TO_STDOUT */
  141. /* Show a prefixed warning. */
  142. #define WARNF(x...) do { \
  143. SAYF(cYEL "[!] " cBRI "WARNING: " cRST x); \
  144. SAYF(cRST "\n"); \
  145. } while (0)
  146. /* Show a prefixed "doing something" message. */
  147. #define ACTF(x...) do { \
  148. SAYF(cLBL "[*] " cRST x); \
  149. SAYF(cRST "\n"); \
  150. } while (0)
  151. /* Show a prefixed "success" message. */
  152. #define OKF(x...) do { \
  153. SAYF(cLGN "[+] " cRST x); \
  154. SAYF(cRST "\n"); \
  155. } while (0)
  156. /* Show a prefixed fatal error message (not used in afl). */
  157. #define BADF(x...) do { \
  158. SAYF(cLRD "\n[-] " cRST x); \
  159. SAYF(cRST "\n"); \
  160. } while (0)
  161. /* Die with a verbose non-OS fatal error message. */
  162. #define FATAL(x...) do { \
  163. SAYF(bSTOP RESET_G1 CURSOR_SHOW cRST cLRD "\n[-] PROGRAM ABORT : " \
  164. cBRI x); \
  165. SAYF(cLRD "\n Location : " cRST "%s(), %s:%u\n\n", \
  166. __FUNCTION__, __FILE__, __LINE__); \
  167. exit(1); \
  168. } while (0)
  169. /* Die by calling abort() to provide a core dump. */
  170. #define ABORT(x...) do { \
  171. SAYF(bSTOP RESET_G1 CURSOR_SHOW cRST cLRD "\n[-] PROGRAM ABORT : " \
  172. cBRI x); \
  173. SAYF(cLRD "\n Stop location : " cRST "%s(), %s:%u\n\n", \
  174. __FUNCTION__, __FILE__, __LINE__); \
  175. abort(); \
  176. } while (0)
  177. /* Die while also including the output of perror(). */
  178. #define PFATAL(x...) do { \
  179. fflush(stdout); \
  180. SAYF(bSTOP RESET_G1 CURSOR_SHOW cRST cLRD "\n[-] SYSTEM ERROR : " \
  181. cBRI x); \
  182. SAYF(cLRD "\n Stop location : " cRST "%s(), %s:%u\n", \
  183. __FUNCTION__, __FILE__, __LINE__); \
  184. SAYF(cLRD " OS message : " cRST "%s\n", strerror(errno)); \
  185. exit(1); \
  186. } while (0)
  187. /* Die with FAULT() or PFAULT() depending on the value of res (used to
  188. interpret different failure modes for read(), write(), etc). */
  189. #define RPFATAL(res, x...) do { \
  190. if (res < 0) PFATAL(x); else FATAL(x); \
  191. } while (0)
  192. /* Error-checking versions of read() and write() that call RPFATAL() as
  193. appropriate. */
  194. #define ck_write(fd, buf, len, fn) do { \
  195. u32 _len = (len); \
  196. s32 _res = write(fd, buf, _len); \
  197. if (_res != _len) RPFATAL(_res, "Short write to %s", fn); \
  198. } while (0)
  199. #define ck_read(fd, buf, len, fn) do { \
  200. u32 _len = (len); \
  201. s32 _res = read(fd, buf, _len); \
  202. if (_res != _len) RPFATAL(_res, "Short read from %s", fn); \
  203. } while (0)
  204. #endif /* ! _HAVE_DEBUG_H */