1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: debug.c 2336 2008-05-18 18:13:45Z sam $ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * debug.c: debugging support |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if defined HAVE_STDINT_H |
---|
22 | # include <stdint.h> |
---|
23 | #elif defined HAVE_INTTYPES_H |
---|
24 | # include <inttypes.h> |
---|
25 | #endif |
---|
26 | #include <stdio.h> |
---|
27 | #if defined HAVE_UNISTD_H |
---|
28 | # include <unistd.h> |
---|
29 | #endif |
---|
30 | #if defined HAVE_IO_H |
---|
31 | # include <io.h> |
---|
32 | #endif |
---|
33 | #include <errno.h> |
---|
34 | #include <stdarg.h> |
---|
35 | |
---|
36 | #include "debug.h" |
---|
37 | #include "libzzuf.h" |
---|
38 | |
---|
39 | extern int _zz_debugfd; |
---|
40 | |
---|
41 | /** |
---|
42 | * Helper macro to write an integer value to a given file descriptor, |
---|
43 | * either in base 10 or in hexadecimal. |
---|
44 | */ |
---|
45 | #define WRITE_INT(fd, i, base) \ |
---|
46 | do \ |
---|
47 | { \ |
---|
48 | char buf[128], *b = buf + 127; \ |
---|
49 | if(i <= 0) \ |
---|
50 | write(fd, (i = -i) ? "-" : "0", 1); /* XXX: hack here */ \ |
---|
51 | while(i) \ |
---|
52 | { \ |
---|
53 | *b-- = hex2char[i % base]; \ |
---|
54 | i /= base; \ |
---|
55 | } \ |
---|
56 | write(fd, b + 1, (int)(buf + 127 - b)); \ |
---|
57 | } while(0) |
---|
58 | |
---|
59 | /** |
---|
60 | * Format a string, printf-like, and write the resulting data to zzuf's |
---|
61 | * debug file descriptor _zz_debugfd. If the debug file descriptor is |
---|
62 | * still -1, this function does nothing. |
---|
63 | * |
---|
64 | * This function's code is roughly equivalent to the following *printf |
---|
65 | * calls, except it only uses signal-safe functions: |
---|
66 | * - fprintf(stderr, "** zzuf debug ** "); |
---|
67 | * - vfprintf(stderr, format, args); |
---|
68 | * - fprintf(stderr, "\n"); |
---|
69 | */ |
---|
70 | void _zz_debug(char const *format, ...) |
---|
71 | { |
---|
72 | static char const *hex2char = "0123456789abcdef"; |
---|
73 | char const *f; |
---|
74 | va_list args; |
---|
75 | int saved_errno; |
---|
76 | |
---|
77 | if(_zz_debugfd < 0) |
---|
78 | return; |
---|
79 | |
---|
80 | saved_errno = errno; |
---|
81 | va_start(args, format); |
---|
82 | |
---|
83 | write(_zz_debugfd, "** zzuf debug ** ", 17); |
---|
84 | for(f = format; *f; f++) |
---|
85 | { |
---|
86 | if(*f != '%') |
---|
87 | { |
---|
88 | write(_zz_debugfd, f, 1); |
---|
89 | continue; |
---|
90 | } |
---|
91 | |
---|
92 | f++; |
---|
93 | if(!*f) |
---|
94 | break; |
---|
95 | |
---|
96 | if(*f == 'c') |
---|
97 | { |
---|
98 | char i = (char)(unsigned char)va_arg(args, int); |
---|
99 | if(i >= 0x20 && i < 0x7f) |
---|
100 | write(_zz_debugfd, &i, 1); |
---|
101 | else if(i == '\n') |
---|
102 | write(_zz_debugfd, "\\n", 2); |
---|
103 | else if(i == '\t') |
---|
104 | write(_zz_debugfd, "\\t", 2); |
---|
105 | else if(i == '\r') |
---|
106 | write(_zz_debugfd, "\\r", 2); |
---|
107 | else |
---|
108 | { |
---|
109 | write(_zz_debugfd, "\\x", 2); |
---|
110 | write(_zz_debugfd, hex2char + ((i & 0xf0) >> 4), 1); |
---|
111 | write(_zz_debugfd, hex2char + (i & 0x0f), 1); |
---|
112 | } |
---|
113 | } |
---|
114 | else if(*f == 'i' || *f == 'd') |
---|
115 | { |
---|
116 | int i = va_arg(args, int); |
---|
117 | WRITE_INT(_zz_debugfd, i, 10); |
---|
118 | } |
---|
119 | else if(*f == 'x') |
---|
120 | { |
---|
121 | int i = va_arg(args, int); |
---|
122 | WRITE_INT(_zz_debugfd, i, 16); |
---|
123 | } |
---|
124 | else if(f[0] == 'l' && (f[1] == 'i' || f[1] == 'd')) |
---|
125 | { |
---|
126 | long int i = va_arg(args, long int); |
---|
127 | WRITE_INT(_zz_debugfd, i, 10); |
---|
128 | f++; |
---|
129 | } |
---|
130 | else if(f[0] == 'l' && f[1] == 'l' && (f[2] == 'i' || f[1] == 'd')) |
---|
131 | { |
---|
132 | long long int i = va_arg(args, long long int); |
---|
133 | WRITE_INT(_zz_debugfd, i, 10); |
---|
134 | f += 2; |
---|
135 | } |
---|
136 | else if(f[0] == 'g') |
---|
137 | { |
---|
138 | double g = va_arg(args, double), h = 0.0000001; |
---|
139 | int i = g; |
---|
140 | WRITE_INT(_zz_debugfd, i, 10); |
---|
141 | for(i = 0; i < 7; i++) |
---|
142 | { |
---|
143 | g = (g - (int)g) * 10; |
---|
144 | h *= 10; |
---|
145 | if(g < h) |
---|
146 | break; |
---|
147 | if(i == 0) |
---|
148 | write(_zz_debugfd, ".", 1); |
---|
149 | write(_zz_debugfd, hex2char + (int)g, 1); |
---|
150 | } |
---|
151 | } |
---|
152 | else if(f[0] == 'p') |
---|
153 | { |
---|
154 | uintptr_t i = va_arg(args, uintptr_t); |
---|
155 | if(!i) |
---|
156 | write(_zz_debugfd, "NULL", 5); |
---|
157 | else |
---|
158 | { |
---|
159 | write(_zz_debugfd, "0x", 2); |
---|
160 | WRITE_INT(_zz_debugfd, i, 16); |
---|
161 | } |
---|
162 | } |
---|
163 | else if(f[0] == 's') |
---|
164 | { |
---|
165 | char *s = va_arg(args, char *); |
---|
166 | if(!s) |
---|
167 | write(_zz_debugfd, "(nil)", 5); |
---|
168 | else |
---|
169 | { |
---|
170 | int l = 0; |
---|
171 | while(s[l]) |
---|
172 | l++; |
---|
173 | write(_zz_debugfd, s, l); |
---|
174 | } |
---|
175 | } |
---|
176 | else if(f[0] == '0' && f[1] == '2' && f[2] == 'x') |
---|
177 | { |
---|
178 | int i = va_arg(args, int); |
---|
179 | write(_zz_debugfd, hex2char + ((i & 0xf0) >> 4), 1); |
---|
180 | write(_zz_debugfd, hex2char + (i & 0x0f), 1); |
---|
181 | f += 2; |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | write(_zz_debugfd, f - 1, 2); |
---|
186 | } |
---|
187 | } |
---|
188 | write(_zz_debugfd, "\n", 1); |
---|
189 | va_end(args); |
---|
190 | errno = saved_errno; |
---|
191 | } |
---|