]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
262a0e14 | 2 | * $Id$ |
30a4f2a8 | 3 | * |
4 | * DEBUG: section 0 Debug Routines | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
2b6662ba | 7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 8 | * ---------------------------------------------------------- |
30a4f2a8 | 9 | * |
2b6662ba | 10 | * Squid is the result of efforts by numerous individuals from |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
30a4f2a8 | 18 | * |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
26ac0430 | 23 | * |
30a4f2a8 | 24 | * This program is distributed in the hope that it will be useful, |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
26ac0430 | 28 | * |
30a4f2a8 | 29 | * You should have received a copy of the GNU General Public License |
30 | * along with this program; if not, write to the Free Software | |
cbdec147 | 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 32 | * |
30a4f2a8 | 33 | */ |
5b6f3fe5 | 34 | |
351e769c | 35 | #include "config.h" |
ebe96adf | 36 | #include "Debug.h" |
608391f4 | 37 | #include "SquidTime.h" |
62493678 | 38 | #include "util.h" |
c772f001 | 39 | |
62493678 AJ |
40 | /* for shutting_down flag in xassert() */ |
41 | #include "globals.h" | |
e1f7507e | 42 | |
62493678 AJ |
43 | /* cope with no squid.h */ |
44 | #ifndef MAXPATHLEN | |
45 | #define MAXPATHLEN 256 | |
46 | #endif | |
47 | ||
48 | char *Debug::debugOptions = NULL; | |
49 | int Debug::override_X = 0; | |
50 | int Debug::log_stderr = -1; | |
51 | bool Debug::log_syslog = false; | |
c772f001 | 52 | int Debug::Levels[MAX_DEBUG_SECTIONS]; |
53 | int Debug::level; | |
62493678 | 54 | char *Debug::cache_log = NULL; |
47df1aa7 | 55 | int Debug::rotateNumber = -1; |
f76d2f97 | 56 | FILE *debug_log = NULL; |
090089c4 | 57 | static char *debug_log_file = NULL; |
123abbe1 | 58 | static int Ctx_Lock = 0; |
12858c3a | 59 | static const char *debugLogTime(void); |
2d72d4fd | 60 | static void ctx_print(void); |
811ef305 | 61 | #if HAVE_SYSLOG |
5e6d4736 | 62 | #ifdef LOG_LOCAL4 |
63 | static int syslog_facility = 0; | |
64 | #endif | |
811ef305 | 65 | static void _db_print_syslog(const char *format, va_list args); |
66 | #endif | |
67 | static void _db_print_stderr(const char *format, va_list args); | |
68 | static void _db_print_file(const char *format, va_list args); | |
24382924 | 69 | |
1bc874d7 | 70 | #ifdef _SQUID_MSWIN_ |
71 | SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex; | |
72 | typedef BOOL (WINAPI * PFInitializeCriticalSectionAndSpinCount) (LPCRITICAL_SECTION, DWORD); | |
1bc874d7 | 73 | #endif |
74 | ||
b8d8561b | 75 | void |
a3d5953d | 76 | _db_print(const char *format,...) |
090089c4 | 77 | { |
62493678 | 78 | char f[BUFSIZ]; f[0]='\0'; |
cd11dce6 | 79 | va_list args1; |
cd11dce6 | 80 | va_list args2; |
81 | va_list args3; | |
8d707f3d | 82 | |
1bc874d7 | 83 | #ifdef _SQUID_MSWIN_ |
84 | /* Multiple WIN32 threads may call this simultaneously */ | |
85 | ||
26ac0430 | 86 | if (!dbg_mutex) { |
1bc874d7 | 87 | HMODULE krnl_lib = GetModuleHandle("Kernel32"); |
88 | PFInitializeCriticalSectionAndSpinCount InitializeCriticalSectionAndSpinCount = NULL; | |
89 | ||
90 | if (krnl_lib) | |
91 | InitializeCriticalSectionAndSpinCount = | |
92 | (PFInitializeCriticalSectionAndSpinCount) GetProcAddress(krnl_lib, | |
93 | "InitializeCriticalSectionAndSpinCount"); | |
94 | ||
95 | dbg_mutex = static_cast<CRITICAL_SECTION*>(xcalloc(1, sizeof(CRITICAL_SECTION))); | |
96 | ||
97 | if (InitializeCriticalSectionAndSpinCount) { | |
98 | /* let multiprocessor systems EnterCriticalSection() fast */ | |
99 | ||
100 | if (!InitializeCriticalSectionAndSpinCount(dbg_mutex, 4000)) { | |
101 | if (debug_log) { | |
102 | fprintf(debug_log, "FATAL: _db_print: can't initialize critical section\n"); | |
103 | fflush(debug_log); | |
104 | } | |
105 | ||
106 | fprintf(stderr, "FATAL: _db_print: can't initialize critical section\n"); | |
107 | abort(); | |
108 | } else | |
109 | InitializeCriticalSection(dbg_mutex); | |
110 | } | |
111 | } | |
112 | ||
113 | EnterCriticalSection(dbg_mutex); | |
380de6f3 | 114 | #endif |
62e76326 | 115 | |
8d707f3d | 116 | /* give a chance to context-based debugging to print current context */ |
380de6f3 | 117 | if (!Ctx_Lock) |
62e76326 | 118 | ctx_print(); |
119 | ||
cd11dce6 | 120 | va_start(args1, format); |
121 | va_start(args2, format); | |
122 | va_start(args3, format); | |
62e76326 | 123 | |
811ef305 | 124 | snprintf(f, BUFSIZ, "%s| %s", |
12858c3a | 125 | debugLogTime(), |
62e76326 | 126 | format); |
127 | ||
cd11dce6 | 128 | _db_print_file(f, args1); |
129 | _db_print_stderr(f, args2); | |
62e76326 | 130 | |
811ef305 | 131 | #if HAVE_SYSLOG |
cd11dce6 | 132 | _db_print_syslog(format, args3); |
133 | #endif | |
1bc874d7 | 134 | |
8d707f3d | 135 | #ifdef _SQUID_MSWIN_ |
1bc874d7 | 136 | LeaveCriticalSection(dbg_mutex); |
1bc874d7 | 137 | #endif |
62e76326 | 138 | |
cd11dce6 | 139 | va_end(args1); |
cd11dce6 | 140 | va_end(args2); |
141 | va_end(args3); | |
811ef305 | 142 | } |
74946a0f | 143 | |
811ef305 | 144 | static void |
af6a12ee AJ |
145 | _db_print_file(const char *format, va_list args) |
146 | { | |
74946a0f | 147 | if (debug_log == NULL) |
62e76326 | 148 | return; |
149 | ||
123abbe1 | 150 | /* give a chance to context-based debugging to print current context */ |
151 | if (!Ctx_Lock) | |
62e76326 | 152 | ctx_print(); |
153 | ||
811ef305 | 154 | vfprintf(debug_log, format, args); |
26ac0430 | 155 | fflush(debug_log); |
090089c4 | 156 | } |
157 | ||
811ef305 | 158 | static void |
af6a12ee AJ |
159 | _db_print_stderr(const char *format, va_list args) |
160 | { | |
62493678 | 161 | if (Debug::log_stderr < Debug::level) |
62e76326 | 162 | return; |
163 | ||
811ef305 | 164 | if (debug_log == stderr) |
62e76326 | 165 | return; |
166 | ||
811ef305 | 167 | vfprintf(stderr, format, args); |
168 | } | |
169 | ||
170 | #if HAVE_SYSLOG | |
171 | static void | |
af6a12ee AJ |
172 | _db_print_syslog(const char *format, va_list args) |
173 | { | |
811ef305 | 174 | /* level 0,1 go to syslog */ |
62e76326 | 175 | |
c772f001 | 176 | if (Debug::level > 1) |
62e76326 | 177 | return; |
178 | ||
62493678 | 179 | if (!Debug::log_syslog) |
62e76326 | 180 | return; |
181 | ||
62493678 | 182 | char tmpbuf[BUFSIZ]; |
811ef305 | 183 | tmpbuf[0] = '\0'; |
62e76326 | 184 | |
811ef305 | 185 | vsnprintf(tmpbuf, BUFSIZ, format, args); |
62e76326 | 186 | |
811ef305 | 187 | tmpbuf[BUFSIZ - 1] = '\0'; |
62e76326 | 188 | |
c772f001 | 189 | syslog(Debug::level == 0 ? LOG_WARNING : LOG_NOTICE, "%s", tmpbuf); |
811ef305 | 190 | } |
191 | #endif /* HAVE_SYSLOG */ | |
192 | ||
b8d8561b | 193 | static void |
af6a12ee AJ |
194 | debugArg(const char *arg) |
195 | { | |
c9f06944 | 196 | int s = 0; |
197 | int l = 0; | |
198 | int i; | |
62e76326 | 199 | |
62493678 AJ |
200 | if (!strncasecmp(arg, "rotate=", 7)) { |
201 | arg += 7; | |
202 | Debug::rotateNumber = atoi(arg); | |
203 | return; | |
204 | } else if (!strncasecmp(arg, "ALL", 3)) { | |
62e76326 | 205 | s = -1; |
206 | arg += 4; | |
c9f06944 | 207 | } else { |
62e76326 | 208 | s = atoi(arg); |
3d0ac046 | 209 | while (*arg && *arg++ != ','); |
c9f06944 | 210 | } |
62e76326 | 211 | |
c9f06944 | 212 | l = atoi(arg); |
b6a2f15e | 213 | assert(s >= -1); |
e879923e | 214 | |
26ac0430 | 215 | if (s >= MAX_DEBUG_SECTIONS) |
e879923e | 216 | s = MAX_DEBUG_SECTIONS-1; |
62e76326 | 217 | |
b6a2f15e | 218 | if (l < 0) |
62e76326 | 219 | l = 0; |
220 | ||
b6a2f15e | 221 | if (l > 10) |
62e76326 | 222 | l = 10; |
223 | ||
c9f06944 | 224 | if (s >= 0) { |
62e76326 | 225 | Debug::Levels[s] = l; |
226 | return; | |
c9f06944 | 227 | } |
62e76326 | 228 | |
c9f06944 | 229 | for (i = 0; i < MAX_DEBUG_SECTIONS; i++) |
62e76326 | 230 | Debug::Levels[i] = l; |
12b9e9b1 | 231 | } |
232 | ||
b8d8561b | 233 | static void |
af6a12ee AJ |
234 | debugOpenLog(const char *logfile) |
235 | { | |
ccff9601 | 236 | if (logfile == NULL) { |
62e76326 | 237 | debug_log = stderr; |
238 | return; | |
ccff9601 | 239 | } |
62e76326 | 240 | |
ccff9601 | 241 | if (debug_log_file) |
62e76326 | 242 | xfree(debug_log_file); |
243 | ||
ccff9601 | 244 | debug_log_file = xstrdup(logfile); /* keep a static copy */ |
62e76326 | 245 | |
f2cc1af5 | 246 | if (debug_log && debug_log != stderr) |
62e76326 | 247 | fclose(debug_log); |
248 | ||
ccff9601 | 249 | debug_log = fopen(logfile, "a+"); |
62e76326 | 250 | |
ccff9601 | 251 | if (!debug_log) { |
62e76326 | 252 | fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile); |
253 | perror(logfile); | |
254 | fprintf(stderr, " messages will be sent to 'stderr'.\n"); | |
255 | fflush(stderr); | |
256 | debug_log = stderr; | |
ccff9601 | 257 | } |
62e76326 | 258 | |
ec4daaa5 | 259 | #ifdef _SQUID_WIN32_ |
c4aefe96 | 260 | setmode(fileno(debug_log), O_TEXT); |
62e76326 | 261 | |
c4aefe96 | 262 | #endif |
ccff9601 | 263 | } |
264 | ||
5e6d4736 | 265 | #if HAVE_SYSLOG |
266 | #ifdef LOG_LOCAL4 | |
267 | ||
268 | static struct syslog_facility_name { | |
269 | const char *name; | |
270 | int facility; | |
271 | } | |
272 | ||
273 | syslog_facility_names[] = { | |
274 | ||
275 | #ifdef LOG_AUTH | |
26ac0430 AJ |
276 | { |
277 | "auth", LOG_AUTH | |
278 | }, | |
5e6d4736 | 279 | #endif |
280 | #ifdef LOG_AUTHPRIV | |
26ac0430 AJ |
281 | { |
282 | "authpriv", LOG_AUTHPRIV | |
283 | }, | |
5e6d4736 | 284 | #endif |
285 | #ifdef LOG_CRON | |
26ac0430 AJ |
286 | { |
287 | "cron", LOG_CRON | |
288 | }, | |
5e6d4736 | 289 | #endif |
290 | #ifdef LOG_DAEMON | |
26ac0430 AJ |
291 | { |
292 | "daemon", LOG_DAEMON | |
293 | }, | |
5e6d4736 | 294 | #endif |
295 | #ifdef LOG_FTP | |
26ac0430 AJ |
296 | { |
297 | "ftp", LOG_FTP | |
298 | }, | |
5e6d4736 | 299 | #endif |
300 | #ifdef LOG_KERN | |
26ac0430 AJ |
301 | { |
302 | "kern", LOG_KERN | |
303 | }, | |
5e6d4736 | 304 | #endif |
305 | #ifdef LOG_LPR | |
26ac0430 AJ |
306 | { |
307 | "lpr", LOG_LPR | |
308 | }, | |
5e6d4736 | 309 | #endif |
310 | #ifdef LOG_MAIL | |
26ac0430 AJ |
311 | { |
312 | "mail", LOG_MAIL | |
313 | }, | |
5e6d4736 | 314 | #endif |
315 | #ifdef LOG_NEWS | |
26ac0430 AJ |
316 | { |
317 | "news", LOG_NEWS | |
318 | }, | |
5e6d4736 | 319 | #endif |
320 | #ifdef LOG_SYSLOG | |
26ac0430 AJ |
321 | { |
322 | "syslog", LOG_SYSLOG | |
323 | }, | |
5e6d4736 | 324 | #endif |
325 | #ifdef LOG_USER | |
26ac0430 AJ |
326 | { |
327 | "user", LOG_USER | |
328 | }, | |
5e6d4736 | 329 | #endif |
330 | #ifdef LOG_UUCP | |
26ac0430 AJ |
331 | { |
332 | "uucp", LOG_UUCP | |
333 | }, | |
5e6d4736 | 334 | #endif |
335 | #ifdef LOG_LOCAL0 | |
26ac0430 AJ |
336 | { |
337 | "local0", LOG_LOCAL0 | |
338 | }, | |
5e6d4736 | 339 | #endif |
340 | #ifdef LOG_LOCAL1 | |
26ac0430 AJ |
341 | { |
342 | "local1", LOG_LOCAL1 | |
343 | }, | |
5e6d4736 | 344 | #endif |
345 | #ifdef LOG_LOCAL2 | |
26ac0430 AJ |
346 | { |
347 | "local2", LOG_LOCAL2 | |
348 | }, | |
5e6d4736 | 349 | #endif |
350 | #ifdef LOG_LOCAL3 | |
26ac0430 AJ |
351 | { |
352 | "local3", LOG_LOCAL3 | |
353 | }, | |
5e6d4736 | 354 | #endif |
355 | #ifdef LOG_LOCAL4 | |
26ac0430 AJ |
356 | { |
357 | "local4", LOG_LOCAL4 | |
358 | }, | |
5e6d4736 | 359 | #endif |
360 | #ifdef LOG_LOCAL5 | |
26ac0430 AJ |
361 | { |
362 | "local5", LOG_LOCAL5 | |
363 | }, | |
5e6d4736 | 364 | #endif |
365 | #ifdef LOG_LOCAL6 | |
26ac0430 AJ |
366 | { |
367 | "local6", LOG_LOCAL6 | |
368 | }, | |
5e6d4736 | 369 | #endif |
370 | #ifdef LOG_LOCAL7 | |
26ac0430 AJ |
371 | { |
372 | "local7", LOG_LOCAL7 | |
373 | }, | |
5e6d4736 | 374 | #endif |
26ac0430 AJ |
375 | { |
376 | NULL, 0 | |
377 | } | |
378 | }; | |
5e6d4736 | 379 | |
380 | #endif | |
381 | ||
382 | void | |
af6a12ee AJ |
383 | _db_set_syslog(const char *facility) |
384 | { | |
62493678 AJ |
385 | Debug::log_syslog = true; |
386 | ||
5e6d4736 | 387 | #ifdef LOG_LOCAL4 |
388 | #ifdef LOG_DAEMON | |
389 | ||
390 | syslog_facility = LOG_DAEMON; | |
391 | #else | |
392 | ||
393 | syslog_facility = LOG_LOCAL4; | |
62493678 | 394 | #endif /* LOG_DAEMON */ |
5e6d4736 | 395 | |
396 | if (facility) { | |
397 | ||
398 | struct syslog_facility_name *n; | |
399 | ||
400 | for (n = syslog_facility_names; n->name; n++) { | |
401 | if (strcmp(n->name, facility) == 0) { | |
402 | syslog_facility = n->facility; | |
403 | return; | |
404 | } | |
405 | } | |
406 | ||
407 | fprintf(stderr, "unknown syslog facility '%s'\n", facility); | |
408 | exit(1); | |
409 | } | |
410 | ||
411 | #else | |
412 | if (facility) | |
413 | fprintf(stderr, "syslog facility type not supported on your system\n"); | |
414 | ||
62493678 | 415 | #endif /* LOG_LOCAL4 */ |
5e6d4736 | 416 | } |
417 | ||
418 | #endif | |
419 | ||
b8d8561b | 420 | void |
af6a12ee AJ |
421 | Debug::parseOptions(char const *options) |
422 | { | |
12b9e9b1 | 423 | int i; |
424 | char *p = NULL; | |
425 | char *s = NULL; | |
090089c4 | 426 | |
62493678 | 427 | if (override_X) { |
37296f4c | 428 | debugs(0, 9, "command-line -X overrides: " << options); |
429 | return; | |
430 | } | |
431 | ||
12b9e9b1 | 432 | for (i = 0; i < MAX_DEBUG_SECTIONS; i++) |
fb6a61d1 | 433 | Debug::Levels[i] = 0; |
090089c4 | 434 | |
30a4f2a8 | 435 | if (options) { |
62e76326 | 436 | p = xstrdup(options); |
437 | ||
438 | for (s = strtok(p, w_space); s; s = strtok(NULL, w_space)) | |
439 | debugArg(s); | |
440 | ||
441 | xfree(p); | |
12b9e9b1 | 442 | } |
d9e04dc7 | 443 | } |
444 | ||
445 | void | |
af6a12ee AJ |
446 | _db_init(const char *logfile, const char *options) |
447 | { | |
d9e04dc7 | 448 | Debug::parseOptions(options); |
62e76326 | 449 | |
ccff9601 | 450 | debugOpenLog(logfile); |
090089c4 | 451 | |
30a4f2a8 | 452 | #if HAVE_SYSLOG && defined(LOG_LOCAL4) |
62e76326 | 453 | |
62493678 | 454 | if (Debug::log_syslog) |
7dbca7a4 | 455 | openlog(APP_SHORTNAME, LOG_PID | LOG_NDELAY | LOG_CONS, syslog_facility); |
62e76326 | 456 | |
db40ae20 | 457 | #endif /* HAVE_SYSLOG */ |
458 | ||
d07e0122 AJ |
459 | /* Pre-Init TZ env, see bug #2656 */ |
460 | tzset(); | |
090089c4 | 461 | } |
462 | ||
b8d8561b | 463 | void |
af6a12ee AJ |
464 | _db_rotate_log(void) |
465 | { | |
090089c4 | 466 | if (debug_log_file == NULL) |
62e76326 | 467 | return; |
468 | ||
71d95578 | 469 | #ifdef S_ISREG |
62493678 | 470 | struct stat sb; |
2edc2504 | 471 | if (stat(debug_log_file, &sb) == 0) |
62e76326 | 472 | if (S_ISREG(sb.st_mode) == 0) |
473 | return; | |
71d95578 | 474 | #endif |
090089c4 | 475 | |
62493678 AJ |
476 | char from[MAXPATHLEN]; |
477 | from[0] = '\0'; | |
478 | ||
479 | char to[MAXPATHLEN]; | |
480 | to[0] = '\0'; | |
481 | ||
1f38f50a | 482 | /* |
483 | * NOTE: we cannot use xrename here without having it in a | |
484 | * separate file -- tools.c has too many dependencies to be | |
485 | * used everywhere debug.c is used. | |
486 | */ | |
090089c4 | 487 | /* Rotate numbers 0 through N up one */ |
62493678 | 488 | for (int i = Debug::rotateNumber; i > 1;) { |
62e76326 | 489 | i--; |
490 | snprintf(from, MAXPATHLEN, "%s.%d", debug_log_file, i - 1); | |
491 | snprintf(to, MAXPATHLEN, "%s.%d", debug_log_file, i); | |
0ef0f1de | 492 | #ifdef _SQUID_MSWIN_ |
62e76326 | 493 | remove |
26ac0430 | 494 | (to); |
0ef0f1de | 495 | #endif |
62e76326 | 496 | rename(from, to); |
090089c4 | 497 | } |
62e76326 | 498 | |
499 | /* | |
500 | * You can't rename open files on Microsoft "operating systems" | |
501 | * so we close before renaming. | |
502 | */ | |
e24070c5 | 503 | #ifdef _SQUID_MSWIN_ |
504 | if (debug_log != stderr) | |
62e76326 | 505 | fclose(debug_log); |
e24070c5 | 506 | #endif |
090089c4 | 507 | /* Rotate the current log to .0 */ |
62493678 | 508 | if (Debug::rotateNumber > 0) { |
62e76326 | 509 | snprintf(to, MAXPATHLEN, "%s.%d", debug_log_file, 0); |
0ef0f1de | 510 | #ifdef _SQUID_MSWIN_ |
62e76326 | 511 | remove |
26ac0430 | 512 | (to); |
0ef0f1de | 513 | #endif |
62e76326 | 514 | rename(debug_log_file, to); |
090089c4 | 515 | } |
62e76326 | 516 | |
090089c4 | 517 | /* Close and reopen the log. It may have been renamed "manually" |
518 | * before HUP'ing us. */ | |
0a8020d1 | 519 | if (debug_log != stderr) |
62493678 | 520 | debugOpenLog(Debug::cache_log); |
090089c4 | 521 | } |
4873d3a4 | 522 | |
523 | static const char * | |
af6a12ee AJ |
524 | debugLogTime(void) |
525 | { | |
12858c3a | 526 | |
527 | time_t t = getCurrentTime(); | |
62e76326 | 528 | |
4873d3a4 | 529 | struct tm *tm; |
530 | static char buf[128]; | |
531 | static time_t last_t = 0; | |
62e76326 | 532 | |
12858c3a | 533 | if (Debug::level > 1) { |
534 | char buf2[128]; | |
535 | tm = localtime(&t); | |
536 | strftime(buf2, 127, "%Y/%m/%d %H:%M:%S", tm); | |
537 | buf2[127] = '\0'; | |
538 | snprintf(buf, 127, "%s.%03d", buf2, (int) current_time.tv_usec / 1000); | |
539 | last_t = t; | |
540 | } else if (t != last_t) { | |
62e76326 | 541 | tm = localtime(&t); |
542 | strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm); | |
543 | last_t = t; | |
4873d3a4 | 544 | } |
62e76326 | 545 | |
12858c3a | 546 | buf[127] = '\0'; |
4873d3a4 | 547 | return buf; |
548 | } | |
549 | ||
bb71ea1f | 550 | void |
af6a12ee AJ |
551 | xassert(const char *msg, const char *file, int line) |
552 | { | |
bf8fe701 | 553 | debugs(0, 0, "assertion failed: " << file << ":" << line << ": \"" << msg << "\""); |
62e76326 | 554 | |
bb71ea1f | 555 | if (!shutting_down) |
62e76326 | 556 | abort(); |
bb71ea1f | 557 | } |
558 | ||
123abbe1 | 559 | /* |
560 | * Context-based Debugging | |
26970499 | 561 | * |
562 | * Rationale | |
563 | * --------- | |
26ac0430 | 564 | * |
26970499 | 565 | * When you have a long nested processing sequence, it is often impossible |
566 | * for low level routines to know in what larger context they operate. If a | |
567 | * routine coredumps, one can restore the context using debugger trace. | |
568 | * However, in many case you do not want to coredump, but just want to report | |
569 | * a potential problem. A report maybe useless out of problem context. | |
26ac0430 | 570 | * |
26970499 | 571 | * To solve this potential problem, use the following approach: |
26ac0430 | 572 | * |
26970499 | 573 | * int |
574 | * top_level_foo(const char *url) | |
575 | * { | |
576 | * // define current context | |
577 | * // note: we stack but do not dup ctx descriptions! | |
578 | * Ctx ctx = ctx_enter(url); | |
579 | * ... | |
580 | * // go down; middle_level_bar will eventually call bottom_level_boo | |
581 | * middle_level_bar(method, protocol); | |
582 | * ... | |
583 | * // exit, clean after yourself | |
584 | * ctx_exit(ctx); | |
585 | * } | |
26ac0430 | 586 | * |
26970499 | 587 | * void |
588 | * bottom_level_boo(int status, void *data) | |
589 | * { | |
590 | * // detect exceptional condition, and simply report it, the context | |
591 | * // information will be available somewhere close in the log file | |
592 | * if (status == STRANGE_STATUS) | |
bf8fe701 | 593 | * debugs(13, 6, "DOS attack detected, data: " << data); |
26970499 | 594 | * ... |
595 | * } | |
26ac0430 | 596 | * |
26970499 | 597 | * Current implementation is extremely simple but still very handy. It has a |
598 | * negligible overhead (descriptions are not duplicated). | |
26ac0430 | 599 | * |
26970499 | 600 | * When the _first_ debug message for a given context is printed, it is |
601 | * prepended with the current context description. Context is printed with | |
602 | * the same debugging level as the original message. | |
26ac0430 | 603 | * |
26970499 | 604 | * Note that we do not print context every type you do ctx_enter(). This |
605 | * approach would produce too many useless messages. For the same reason, a | |
606 | * context description is printed at most _once_ even if you have 10 | |
607 | * debugging messages within one context. | |
26ac0430 | 608 | * |
26970499 | 609 | * Contexts can be nested, of course. You must use ctx_enter() to enter a |
610 | * context (push it onto stack). It is probably safe to exit several nested | |
611 | * contexts at _once_ by calling ctx_exit() at the top level (this will pop | |
612 | * all context till current one). However, as in any stack, you cannot start | |
613 | * in the middle. | |
26ac0430 AJ |
614 | * |
615 | * Analysis: | |
26970499 | 616 | * i) locate debugging message, |
617 | * ii) locate current context by going _upstream_ in your log file, | |
618 | * iii) hack away. | |
619 | * | |
620 | * | |
26ac0430 | 621 | * To-Do: |
26970499 | 622 | * ----- |
623 | * | |
123abbe1 | 624 | * decide if we want to dup() descriptions (adds overhead) but allows to |
625 | * add printf()-style interface | |
26970499 | 626 | * |
2ac76861 | 627 | * implementation: |
26970499 | 628 | * --------------- |
2ac76861 | 629 | * |
123abbe1 | 630 | * descriptions for contexts over CTX_MAX_LEVEL limit are ignored, you probably |
631 | * have a bug if your nesting goes that deep. | |
632 | */ | |
2ac76861 | 633 | |
123abbe1 | 634 | #define CTX_MAX_LEVEL 255 |
2ac76861 | 635 | |
636 | /* | |
637 | * produce a warning when nesting reaches this level and then double | |
638 | * the level | |
639 | */ | |
640 | static int Ctx_Warn_Level = 32; | |
123abbe1 | 641 | /* all descriptions has been printed up to this level */ |
a3f9588e | 642 | static int Ctx_Reported_Level = -1; |
123abbe1 | 643 | /* descriptions are still valid or active up to this level */ |
a3f9588e | 644 | static int Ctx_Valid_Level = -1; |
123abbe1 | 645 | /* current level, the number of nested ctx_enter() calls */ |
a3f9588e | 646 | static int Ctx_Current_Level = -1; |
123abbe1 | 647 | /* saved descriptions (stack) */ |
2ac76861 | 648 | static const char *Ctx_Descrs[CTX_MAX_LEVEL + 1]; |
b70d1c58 | 649 | /* "safe" get secription */ |
650 | static const char *ctx_get_descr(Ctx ctx); | |
123abbe1 | 651 | |
652 | ||
653 | Ctx | |
af6a12ee AJ |
654 | ctx_enter(const char *descr) |
655 | { | |
123abbe1 | 656 | Ctx_Current_Level++; |
657 | ||
658 | if (Ctx_Current_Level <= CTX_MAX_LEVEL) | |
62e76326 | 659 | Ctx_Descrs[Ctx_Current_Level] = descr; |
123abbe1 | 660 | |
661 | if (Ctx_Current_Level == Ctx_Warn_Level) { | |
bf8fe701 | 662 | debugs(0, 0, "# ctx: suspiciously deep (" << Ctx_Warn_Level << ") nesting:"); |
62e76326 | 663 | Ctx_Warn_Level *= 2; |
123abbe1 | 664 | } |
62e76326 | 665 | |
123abbe1 | 666 | return Ctx_Current_Level; |
667 | } | |
668 | ||
669 | void | |
af6a12ee AJ |
670 | ctx_exit(Ctx ctx) |
671 | { | |
123abbe1 | 672 | assert(ctx >= 0); |
2ac76861 | 673 | Ctx_Current_Level = (ctx >= 0) ? ctx - 1 : -1; |
62e76326 | 674 | |
123abbe1 | 675 | if (Ctx_Valid_Level > Ctx_Current_Level) |
62e76326 | 676 | Ctx_Valid_Level = Ctx_Current_Level; |
123abbe1 | 677 | } |
678 | ||
679 | /* | |
680 | * the idea id to print each context description at most once but provide enough | |
681 | * info for deducing the current execution stack | |
682 | */ | |
683 | static void | |
af6a12ee AJ |
684 | ctx_print(void) |
685 | { | |
123abbe1 | 686 | /* lock so _db_print will not call us recursively */ |
687 | Ctx_Lock++; | |
688 | /* ok, user saw [0,Ctx_Reported_Level] descriptions */ | |
689 | /* first inform about entries popped since user saw them */ | |
62e76326 | 690 | |
123abbe1 | 691 | if (Ctx_Valid_Level < Ctx_Reported_Level) { |
62e76326 | 692 | if (Ctx_Reported_Level != Ctx_Valid_Level + 1) |
693 | _db_print("ctx: exit levels from %2d down to %2d\n", | |
694 | Ctx_Reported_Level, Ctx_Valid_Level + 1); | |
695 | else | |
696 | _db_print("ctx: exit level %2d\n", Ctx_Reported_Level); | |
697 | ||
698 | Ctx_Reported_Level = Ctx_Valid_Level; | |
123abbe1 | 699 | } |
62e76326 | 700 | |
123abbe1 | 701 | /* report new contexts that were pushed since last report */ |
702 | while (Ctx_Reported_Level < Ctx_Current_Level) { | |
62e76326 | 703 | Ctx_Reported_Level++; |
704 | Ctx_Valid_Level++; | |
705 | _db_print("ctx: enter level %2d: '%s'\n", Ctx_Reported_Level, | |
706 | ctx_get_descr(Ctx_Reported_Level)); | |
123abbe1 | 707 | } |
62e76326 | 708 | |
123abbe1 | 709 | /* unlock */ |
710 | Ctx_Lock--; | |
711 | } | |
b70d1c58 | 712 | |
713 | /* checks for nulls and overflows */ | |
714 | static const char * | |
af6a12ee AJ |
715 | ctx_get_descr(Ctx ctx) |
716 | { | |
2ac76861 | 717 | if (ctx < 0 || ctx > CTX_MAX_LEVEL) |
62e76326 | 718 | return "<lost>"; |
719 | ||
b70d1c58 | 720 | return Ctx_Descrs[ctx] ? Ctx_Descrs[ctx] : "<null>"; |
721 | } | |
f95fe6ed | 722 | |
9eab365d | 723 | int Debug::TheDepth = 0; |
724 | ||
f95fe6ed | 725 | std::ostream & |
af6a12ee AJ |
726 | Debug::getDebugOut() |
727 | { | |
9eab365d | 728 | assert(TheDepth >= 0); |
729 | ++TheDepth; | |
730 | if (TheDepth > 1) { | |
731 | assert(CurrentDebug); | |
732 | *CurrentDebug << std::endl << "reentrant debuging " << TheDepth << "-{"; | |
733 | } else { | |
734 | assert(!CurrentDebug); | |
735 | CurrentDebug = new std::ostringstream(); | |
736 | // set default formatting flags | |
737 | CurrentDebug->setf(std::ios::fixed); | |
738 | CurrentDebug->precision(2); | |
739 | } | |
4ecaa0f0 | 740 | return *CurrentDebug; |
f95fe6ed | 741 | } |
742 | ||
743 | void | |
af6a12ee AJ |
744 | Debug::finishDebug() |
745 | { | |
9eab365d | 746 | assert(TheDepth >= 0); |
747 | assert(CurrentDebug); | |
748 | if (TheDepth > 1) { | |
749 | *CurrentDebug << "}-" << TheDepth << std::endl; | |
750 | } else { | |
751 | assert(TheDepth == 1); | |
752 | _db_print("%s\n", CurrentDebug->str().c_str()); | |
753 | delete CurrentDebug; | |
754 | CurrentDebug = NULL; | |
755 | } | |
756 | --TheDepth; | |
757 | } | |
758 | ||
759 | // Hack: replaces global ::xassert() to debug debugging assertions | |
760 | // Relies on assert macro calling xassert() without a specific scope. | |
761 | void | |
af6a12ee AJ |
762 | Debug::xassert(const char *msg, const char *file, int line) |
763 | { | |
26ac0430 | 764 | |
9eab365d | 765 | if (CurrentDebug) { |
766 | *CurrentDebug << "assertion failed: " << file << ":" << line << | |
26ac0430 | 767 | ": \"" << msg << "\""; |
9eab365d | 768 | } |
769 | abort(); | |
f95fe6ed | 770 | } |
771 | ||
bf5113eb | 772 | std::ostringstream (*Debug::CurrentDebug)(NULL); |
5b6f3fe5 | 773 | |
af28bfbd AJ |
774 | const size_t |
775 | BuildPrefixInit() | |
5b6f3fe5 | 776 | { |
cd31e896 | 777 | // XXX: This must be kept in sync with the actual debug.cc location |
af28bfbd AJ |
778 | const char *ThisFileNameTail = "src/debug.cc"; |
779 | ||
5b6f3fe5 | 780 | const char *file=__FILE__; |
cd31e896 AR |
781 | |
782 | // Disable heuristic if it does not work. | |
783 | if (!strstr(file, ThisFileNameTail)) | |
af6a12ee | 784 | return 0; |
cd31e896 | 785 | |
af28bfbd | 786 | return strlen(file)-strlen(ThisFileNameTail); |
5b6f3fe5 | 787 | } |
af28bfbd AJ |
788 | |
789 | const char* | |
790 | SkipBuildPrefix(const char* path) | |
5b6f3fe5 | 791 | { |
af28bfbd AJ |
792 | static const size_t BuildPrefixLength = BuildPrefixInit(); |
793 | ||
5b6f3fe5 FC |
794 | return path+BuildPrefixLength; |
795 | } |