OLD | NEW |
(Empty) | |
| 1 /**************************************************************************** |
| 2 | (C) Copyright 2008 Novell, Inc. All Rights Reserved. |
| 3 | |
| 4 | GPLv2: This program is free software; you can redistribute it |
| 5 | and/or modify it under the terms of version 2 of the GNU General |
| 6 | Public License as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 +-------------------------------------------------------------------------*/ |
| 13 |
| 14 #include <stdlib.h> |
| 15 #include <stdio.h> |
| 16 #include <stdarg.h> |
| 17 #include <ctype.h> |
| 18 #include <debug.h> |
| 19 #include <string.h> |
| 20 |
| 21 #ifdef __WINDOWS__ |
| 22 #define strncasecmp _strnicmp |
| 23 #define strcasecmp _stricmp |
| 24 #define snprintf sprintf_s |
| 25 #endif |
| 26 |
| 27 #include <style.h> |
| 28 |
| 29 int DebugIsOn = 1; |
| 30 int AssertIsOn = 1; |
| 31 int FnDebugIsOn = 1; |
| 32 |
| 33 static FILE **stdbug = &stdout; |
| 34 |
| 35 int debugon (void) |
| 36 { |
| 37 return DebugIsOn = 1; |
| 38 } |
| 39 |
| 40 int debugoff (void) |
| 41 { |
| 42 return DebugIsOn = 0; |
| 43 } |
| 44 |
| 45 int fdebugon (void) |
| 46 { |
| 47 return FnDebugIsOn = 1; |
| 48 } |
| 49 |
| 50 int fdebugoff (void) |
| 51 { |
| 52 return FnDebugIsOn = 0; |
| 53 } |
| 54 |
| 55 void debugstderr (void) |
| 56 { |
| 57 stdbug = &stderr; |
| 58 } |
| 59 |
| 60 void debugstdout (void) |
| 61 { |
| 62 stdbug = &stdout; |
| 63 } |
| 64 |
| 65 bool debug_is_on (void) |
| 66 { |
| 67 return DebugIsOn; |
| 68 } |
| 69 |
| 70 bool debug_is_off (void) |
| 71 { |
| 72 return !DebugIsOn; |
| 73 } |
| 74 |
| 75 int debugenv (void) |
| 76 { |
| 77 char *c; |
| 78 |
| 79 c = getenv("DEBUG"); |
| 80 if (c) { |
| 81 if (strcasecmp(c, "on") == 0) { |
| 82 fprintf(*stdbug, "Debug is on\n"); |
| 83 DebugIsOn = 1; |
| 84 debugon(); |
| 85 fdebugon(); |
| 86 } else if (strcasecmp(c, "off") == 0) { |
| 87 debugoff(); |
| 88 fdebugoff(); |
| 89 } |
| 90 } |
| 91 return DebugIsOn; |
| 92 } |
| 93 |
| 94 bool prf (const char *fn) |
| 95 { |
| 96 if (FnDebugIsOn) { |
| 97 fprintf(*stdbug, "%s\n", fn); |
| 98 fflush(*stdbug); |
| 99 } |
| 100 return TRUE; |
| 101 } |
| 102 |
| 103 int prbug (const char *format, ...) |
| 104 { |
| 105 va_list args; |
| 106 int rc; |
| 107 |
| 108 va_start(args, format); |
| 109 rc = vfprintf(*stdbug, format, args); |
| 110 va_end(args); |
| 111 |
| 112 return rc; |
| 113 } |
| 114 |
| 115 int flushbug (void) |
| 116 { |
| 117 return fflush(*stdbug); |
| 118 } |
| 119 |
| 120 bool print (const char *fn, unsigned line, const char *format, ...) |
| 121 { |
| 122 va_list args; |
| 123 char buf[80]; |
| 124 char *b = buf; |
| 125 int r = sizeof(buf); |
| 126 int n; |
| 127 |
| 128 if (!DebugIsOn) return TRUE; |
| 129 |
| 130 if (line) { |
| 131 n = snprintf(b, r, "%s<%u>", fn, line); |
| 132 } else { |
| 133 n = snprintf(b, r, "%s", fn); |
| 134 } |
| 135 b += n; |
| 136 r -= n; |
| 137 |
| 138 va_start(args, format); |
| 139 vsnprintf(b, r, format, args); |
| 140 va_end(args); |
| 141 |
| 142 fprintf(*stdbug, "%s\n", buf); |
| 143 fflush(*stdbug); |
| 144 |
| 145 return TRUE; |
| 146 } |
| 147 |
| 148 bool pr (const char *fn, unsigned line, const char *label) |
| 149 { |
| 150 if (label) return print(fn, line, "%s", label); |
| 151 else return print(fn, line, ""); |
| 152 } |
| 153 |
| 154 bool prd (const char *fn, unsigned line, const char *label, s64 x) |
| 155 { |
| 156 return print(fn, line, "%s=%lld", label, x); |
| 157 } |
| 158 |
| 159 bool prp (const char *fn, unsigned line, const char *label, void *x) |
| 160 { |
| 161 return print(fn, line, "%s=%p", label, x); |
| 162 } |
| 163 |
| 164 bool prs (const char *fn, unsigned line, const char *label, const char *s) |
| 165 { |
| 166 return print(fn, line, "%s=%s", label, s); |
| 167 } |
| 168 |
| 169 bool pru (const char *fn, unsigned line, const char *label, u64 x) |
| 170 { |
| 171 return print(fn, line, "%s=%llu", label, x); |
| 172 } |
| 173 |
| 174 bool prx (const char *fn, unsigned line, const char *label, u64 x) |
| 175 { |
| 176 return print(fn, line, "%s=%llx", label, x); |
| 177 } |
| 178 |
| 179 bool prg (const char *fn, unsigned line, const char *label, double x) |
| 180 { |
| 181 return print(fn, line, "%s=%g", label, x); |
| 182 } |
| 183 |
| 184 static void pr_n_u8 (const void *mem, unint n) |
| 185 { |
| 186 const u8 *u = mem; |
| 187 unint i; |
| 188 |
| 189 prbug(" "); |
| 190 for (i = 4; i > n; i--) prbug(" "); |
| 191 |
| 192 while (i-- > 0) prbug("%.2x", u[i]); |
| 193 } |
| 194 |
| 195 bool prmem ( |
| 196 const char *label, |
| 197 const void *mem, |
| 198 unsigned int n) |
| 199 { |
| 200 enum { NLONGS = 4, |
| 201 NCHARS = NLONGS * sizeof(u32) }; |
| 202 |
| 203 const u32 *p = mem; |
| 204 const char *c = mem; |
| 205 unsigned i, j; |
| 206 unsigned q, r; |
| 207 |
| 208 if (!DebugIsOn) return TRUE; |
| 209 |
| 210 prbug("%s:\n", label); |
| 211 |
| 212 q = n / NCHARS; |
| 213 r = n % NCHARS; |
| 214 for (i = 0; i < q; i++) { |
| 215 prbug("%p:", p); |
| 216 for (j = 0; j < NLONGS; j++) { |
| 217 prbug(" %8x", *p++); |
| 218 } |
| 219 prbug(" | "); |
| 220 for (j = 0; j < NCHARS; j++, c++) { |
| 221 prbug("%c", isprint(*c) ? *c : '.'); |
| 222 } |
| 223 prbug("\n"); |
| 224 } |
| 225 flushbug(); |
| 226 if (!r) return TRUE; |
| 227 prbug("%8p:", p); |
| 228 for (j = 0; j < r / sizeof(u32); j++) { |
| 229 prbug(" %8x", *p++); |
| 230 } |
| 231 i = r % sizeof(u32); |
| 232 if (i) { |
| 233 ++j; |
| 234 pr_n_u8(p, i); |
| 235 } |
| 236 for (; j < NLONGS; j++) { |
| 237 prbug(" "); |
| 238 } |
| 239 prbug(" | "); |
| 240 for (j = 0; j < r; j++, c++) { |
| 241 prbug("%c", isprint(*c) ? *c : '.'); |
| 242 } |
| 243 prbug("\n"); |
| 244 flushbug(); |
| 245 return TRUE; |
| 246 } |
| 247 |
| 248 bool prbytes ( |
| 249 const char *label, |
| 250 const void *mem, |
| 251 unsigned int n) |
| 252 { |
| 253 enum { CHARS_PER_LINE = 16 }; |
| 254 const u8 *p = mem; |
| 255 const char *c = mem; |
| 256 unsigned i, j; |
| 257 unsigned q, r; |
| 258 |
| 259 if (!DebugIsOn) return TRUE; |
| 260 |
| 261 prbug("%s:\n", label); |
| 262 |
| 263 q = n / CHARS_PER_LINE; |
| 264 r = n % CHARS_PER_LINE; |
| 265 for (i = 0; i < q; i++) { |
| 266 prbug("%p:", p); |
| 267 for (j = 0; j < CHARS_PER_LINE; j++) { |
| 268 prbug(" %.2x", *p++); |
| 269 } |
| 270 prbug(" | "); |
| 271 for (j = 0; j < CHARS_PER_LINE; j++, c++) { |
| 272 prbug("%c", isprint(*c) ? *c : '.'); |
| 273 } |
| 274 prbug("\n"); |
| 275 } |
| 276 if (r) { |
| 277 prbug("%8p:", p); |
| 278 for (j = 0; j < r; j++) { |
| 279 prbug(" %.2x", *p++); |
| 280 } |
| 281 for (; j < CHARS_PER_LINE; j++) { |
| 282 prbug(" "); |
| 283 } |
| 284 prbug(" | "); |
| 285 for (j = 0; j < r; j++, c++) { |
| 286 prbug("%c", isprint(*c) ? *c : '.'); |
| 287 } |
| 288 prbug("\n"); |
| 289 } |
| 290 flushbug(); |
| 291 return TRUE; |
| 292 } |
| 293 |
| 294 int assertError (const char *what) |
| 295 { |
| 296 if (AssertIsOn) { |
| 297 prbug("ASSERT FAILED: %s\n", what); |
| 298 flushbug(); |
| 299 exit(2); |
| 300 } |
| 301 return 0; |
| 302 } |
| 303 |
OLD | NEW |