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