1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: debug.c 1730 2007-02-01 18:19:03Z 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 | /* Do we want our debug() call to be safe wrt. signals? */ |
---|
22 | #define SAFE_FUNCTION |
---|
23 | |
---|
24 | #if defined HAVE_STDINT_H |
---|
25 | # include <stdint.h> |
---|
26 | #elif defined HAVE_INTTYPES_H |
---|
27 | # include <inttypes.h> |
---|
28 | #endif |
---|
29 | #include <stdio.h> |
---|
30 | #if defined HAVE_UNISTD_H |
---|
31 | # include <unistd.h> |
---|
32 | #endif |
---|
33 | #if defined HAVE_IO_H |
---|
34 | # include <io.h> |
---|
35 | #endif |
---|
36 | #include <errno.h> |
---|
37 | #include <stdarg.h> |
---|
38 | |
---|
39 | #include "debug.h" |
---|
40 | #include "libzzuf.h" |
---|
41 | |
---|
42 | extern int _zz_debugfd; |
---|
43 | |
---|
44 | #define WRITE_INT(fd, i, base) \ |
---|
45 | do \ |
---|
46 | { \ |
---|
47 | char buf[128], *b = buf + 127; \ |
---|
48 | if(i <= 0) \ |
---|
49 | write(fd, (i = -i) ? "-" : "0", 1); /* XXX: hack here */ \ |
---|
50 | while(i) \ |
---|
51 | { \ |
---|
52 | *b-- = hex2char[i % base]; \ |
---|
53 | i /= base; \ |
---|
54 | } \ |
---|
55 | write(fd, b + 1, buf + 127 - b); \ |
---|
56 | } while(0) |
---|
57 | |
---|
58 | void _zz_debug(char const *format, ...) |
---|
59 | { |
---|
60 | #ifdef SAFE_FUNCTION |
---|
61 | static char const *hex2char = "0123456789abcdef"; |
---|
62 | char const *f; |
---|
63 | #endif |
---|
64 | va_list args; |
---|
65 | int saved_errno; |
---|
66 | |
---|
67 | if(_zz_debugfd < 0) |
---|
68 | return; |
---|
69 | |
---|
70 | saved_errno = errno; |
---|
71 | va_start(args, format); |
---|
72 | #ifdef SAFE_FUNCTION |
---|
73 | write(_zz_debugfd, "** zzuf debug ** ", 17); |
---|
74 | for(f = format; *f; f++) |
---|
75 | { |
---|
76 | if(*f != '%') |
---|
77 | { |
---|
78 | write(_zz_debugfd, f, 1); |
---|
79 | continue; |
---|
80 | } |
---|
81 | |
---|
82 | f++; |
---|
83 | if(!*f) |
---|
84 | break; |
---|
85 | |
---|
86 | if(*f == 'c') |
---|
87 | { |
---|
88 | char i = (char)(unsigned char)va_arg(args, int); |
---|
89 | if(i >= 0x20 && i < 0x7f) |
---|
90 | write(_zz_debugfd, &i, 1); |
---|
91 | else if(i == '\n') |
---|
92 | write(_zz_debugfd, "\\n", 2); |
---|
93 | else if(i == '\t') |
---|
94 | write(_zz_debugfd, "\\t", 2); |
---|
95 | else if(i == '\r') |
---|
96 | write(_zz_debugfd, "\\r", 2); |
---|
97 | else |
---|
98 | { |
---|
99 | write(_zz_debugfd, "\\x", 2); |
---|
100 | write(_zz_debugfd, hex2char + ((i & 0xf0) >> 4), 1); |
---|
101 | write(_zz_debugfd, hex2char + (i & 0x0f), 1); |
---|
102 | } |
---|
103 | } |
---|
104 | else if(*f == 'i') |
---|
105 | { |
---|
106 | int i = va_arg(args, int); |
---|
107 | WRITE_INT(_zz_debugfd, i, 10); |
---|
108 | } |
---|
109 | else if(*f == 'x') |
---|
110 | { |
---|
111 | int i = va_arg(args, int); |
---|
112 | WRITE_INT(_zz_debugfd, i, 16); |
---|
113 | } |
---|
114 | else if(f[0] == 'l' && f[1] == 'i') |
---|
115 | { |
---|
116 | long int i = va_arg(args, long int); |
---|
117 | WRITE_INT(_zz_debugfd, i, 10); |
---|
118 | f++; |
---|
119 | } |
---|
120 | else if(f[0] == 'l' && f[1] == 'l' && f[2] == 'i') |
---|
121 | { |
---|
122 | long long int i = va_arg(args, long long int); |
---|
123 | WRITE_INT(_zz_debugfd, i, 10); |
---|
124 | f += 2; |
---|
125 | } |
---|
126 | else if(f[0] == 'p') |
---|
127 | { |
---|
128 | uintptr_t i = va_arg(args, uintptr_t); |
---|
129 | if(!i) |
---|
130 | write(_zz_debugfd, "NULL", 5); |
---|
131 | else |
---|
132 | { |
---|
133 | write(_zz_debugfd, "0x", 2); |
---|
134 | WRITE_INT(_zz_debugfd, i, 16); |
---|
135 | } |
---|
136 | } |
---|
137 | else if(f[0] == 's') |
---|
138 | { |
---|
139 | char *s = va_arg(args, char *); |
---|
140 | if(!s) |
---|
141 | write(_zz_debugfd, "(nil)", 5); |
---|
142 | else |
---|
143 | { |
---|
144 | int l = 0; |
---|
145 | while(s[l]) |
---|
146 | l++; |
---|
147 | write(_zz_debugfd, s, l); |
---|
148 | } |
---|
149 | } |
---|
150 | else if(f[0] == '0' && f[1] == '2' && f[2] == 'x') |
---|
151 | { |
---|
152 | int i = va_arg(args, int); |
---|
153 | write(_zz_debugfd, hex2char + ((i & 0xf0) >> 4), 1); |
---|
154 | write(_zz_debugfd, hex2char + (i & 0x0f), 1); |
---|
155 | f += 2; |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | write(_zz_debugfd, f - 1, 2); |
---|
160 | } |
---|
161 | } |
---|
162 | write(_zz_debugfd, "\n", 1); |
---|
163 | #else |
---|
164 | fprintf(stderr, "** zzuf debug ** "); |
---|
165 | vfprintf(stderr, format, args); |
---|
166 | fprintf(stderr, "\n"); |
---|
167 | #endif |
---|
168 | va_end(args); |
---|
169 | errno = saved_errno; |
---|
170 | } |
---|
171 | |
---|