]>
Commit | Line | Data |
---|---|---|
38d04788 | 1 | |
30a4f2a8 | 2 | /* |
b6a2f15e | 3 | * $Id: debug.cc,v 1.75 1999/04/15 06:15:51 wessels Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 0 Debug Routines | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
42c04c16 | 8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
e25c139f | 13 | * National Laboratory for Applied Network Research and funded by the |
14 | * National Science Foundation. Squid is Copyrighted (C) 1998 by | |
15 | * Duane Wessels and the University of California San Diego. Please | |
16 | * see the COPYRIGHT file for full details. Squid incorporates | |
17 | * software developed and/or copyrighted by other sources. Please see | |
18 | * the CREDITS file for full details. | |
30a4f2a8 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
30a4f2a8 | 34 | */ |
ed43818f | 35 | |
44a47c6e | 36 | #include "squid.h" |
090089c4 | 37 | |
090089c4 | 38 | static char *debug_log_file = NULL; |
123abbe1 | 39 | static int Ctx_Lock = 0; |
4873d3a4 | 40 | static const char *debugLogTime(time_t); |
123abbe1 | 41 | static void ctx_print(); |
24382924 | 42 | |
6de2df60 | 43 | #if STDC_HEADERS |
b8d8561b | 44 | void |
a3d5953d | 45 | _db_print(const char *format,...) |
090089c4 | 46 | { |
75e5a32e | 47 | #if defined(__QNX__) |
48 | va_list eargs; | |
49 | #endif | |
090089c4 | 50 | va_list args; |
51 | #else | |
b8d8561b | 52 | void |
53 | _db_print(va_alist) | |
090089c4 | 54 | va_dcl |
55 | { | |
090089c4 | 56 | va_list args; |
0ee4272b | 57 | const char *format = NULL; |
97f61b08 | 58 | #endif |
95d659f0 | 59 | LOCAL_ARRAY(char, f, BUFSIZ); |
233794cd | 60 | #if HAVE_SYSLOG |
95d659f0 | 61 | LOCAL_ARRAY(char, tmpbuf, BUFSIZ); |
233794cd | 62 | #endif |
090089c4 | 63 | |
6de2df60 | 64 | #if STDC_HEADERS |
97f61b08 | 65 | va_start(args, format); |
75e5a32e | 66 | #if defined(__QNX__) |
67 | va_start(eargs, format); | |
68 | #endif | |
090089c4 | 69 | #else |
70 | va_start(args); | |
74946a0f | 71 | format = va_arg(args, const char *); |
72 | #endif | |
73 | ||
74 | if (debug_log == NULL) | |
75 | return; | |
123abbe1 | 76 | /* give a chance to context-based debugging to print current context */ |
77 | if (!Ctx_Lock) | |
78 | ctx_print(); | |
042461c3 | 79 | snprintf(f, BUFSIZ, "%s| %s", |
4873d3a4 | 80 | debugLogTime(squid_curtime), |
396c5745 | 81 | format); |
db40ae20 | 82 | #if HAVE_SYSLOG |
2d22efa6 | 83 | /* level 0,1 go to syslog */ |
84 | if (_db_level <= 1 && opt_syslog_enable) { | |
12b9e9b1 | 85 | tmpbuf[0] = '\0'; |
042461c3 | 86 | vsnprintf(tmpbuf, BUFSIZ, format, args); |
e82d6d21 | 87 | tmpbuf[BUFSIZ - 1] = '\0'; |
d737baa0 | 88 | syslog(_db_level == 0 ? LOG_WARNING : LOG_NOTICE, "%s", tmpbuf); |
12b9e9b1 | 89 | } |
db40ae20 | 90 | #endif /* HAVE_SYSLOG */ |
12b9e9b1 | 91 | /* write to log file */ |
75e5a32e | 92 | #if defined(__QNX__) |
93 | vfprintf(debug_log, f, eargs); | |
94 | #else | |
12b9e9b1 | 95 | vfprintf(debug_log, f, args); |
75e5a32e | 96 | #endif |
17a0a4ee | 97 | if (!Config.onoff.buffered_logs) |
12b9e9b1 | 98 | fflush(debug_log); |
4873d3a4 | 99 | if (opt_debug_stderr >= _db_level && debug_log != stderr) { |
75e5a32e | 100 | #if defined(__QNX__) |
067bea91 | 101 | vfprintf(stderr, f, eargs); |
75e5a32e | 102 | #else |
0d90407c | 103 | vfprintf(stderr, f, args); |
75e5a32e | 104 | #endif |
105 | } | |
106 | #if defined(__QNX__) | |
107 | va_end(eargs); | |
108 | #endif | |
090089c4 | 109 | va_end(args); |
110 | } | |
111 | ||
b8d8561b | 112 | static void |
0ee4272b | 113 | debugArg(const char *arg) |
12b9e9b1 | 114 | { |
c9f06944 | 115 | int s = 0; |
116 | int l = 0; | |
117 | int i; | |
c9f06944 | 118 | if (!strncasecmp(arg, "ALL", 3)) { |
119 | s = -1; | |
120 | arg += 4; | |
121 | } else { | |
122 | s = atoi(arg); | |
123 | while (*arg && *arg++ != ','); | |
124 | } | |
125 | l = atoi(arg); | |
b6a2f15e | 126 | assert(s >= -1); |
127 | assert(s < MAX_DEBUG_SECTIONS); | |
128 | if (l < 0) | |
129 | l = 0; | |
130 | if (l > 10) | |
131 | l = 10; | |
c9f06944 | 132 | if (s >= 0) { |
133 | debugLevels[s] = l; | |
134 | return; | |
135 | } | |
136 | for (i = 0; i < MAX_DEBUG_SECTIONS; i++) | |
137 | debugLevels[i] = l; | |
12b9e9b1 | 138 | } |
139 | ||
b8d8561b | 140 | static void |
0ee4272b | 141 | debugOpenLog(const char *logfile) |
ccff9601 | 142 | { |
143 | if (logfile == NULL) { | |
144 | debug_log = stderr; | |
145 | return; | |
146 | } | |
147 | if (debug_log_file) | |
5c46528b | 148 | xfree(debug_log_file); |
ccff9601 | 149 | debug_log_file = xstrdup(logfile); /* keep a static copy */ |
f2cc1af5 | 150 | if (debug_log && debug_log != stderr) |
a0bbd6c8 | 151 | fclose(debug_log); |
ccff9601 | 152 | debug_log = fopen(logfile, "a+"); |
153 | if (!debug_log) { | |
154 | fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile); | |
155 | perror(logfile); | |
156 | fprintf(stderr, " messages will be sent to 'stderr'.\n"); | |
157 | fflush(stderr); | |
158 | debug_log = stderr; | |
159 | } | |
160 | } | |
161 | ||
b8d8561b | 162 | void |
0ee4272b | 163 | _db_init(const char *logfile, const char *options) |
090089c4 | 164 | { |
12b9e9b1 | 165 | int i; |
166 | char *p = NULL; | |
167 | char *s = NULL; | |
090089c4 | 168 | |
12b9e9b1 | 169 | for (i = 0; i < MAX_DEBUG_SECTIONS; i++) |
170 | debugLevels[i] = -1; | |
090089c4 | 171 | |
30a4f2a8 | 172 | if (options) { |
173 | p = xstrdup(options); | |
174 | for (s = strtok(p, w_space); s; s = strtok(NULL, w_space)) | |
12b9e9b1 | 175 | debugArg(s); |
12b9e9b1 | 176 | xfree(p); |
177 | } | |
ccff9601 | 178 | debugOpenLog(logfile); |
090089c4 | 179 | |
30a4f2a8 | 180 | #if HAVE_SYSLOG && defined(LOG_LOCAL4) |
6e40f263 | 181 | if (opt_syslog_enable) |
b8de7ebe | 182 | openlog(appname, LOG_PID | LOG_NDELAY | LOG_CONS, LOG_LOCAL4); |
db40ae20 | 183 | #endif /* HAVE_SYSLOG */ |
184 | ||
090089c4 | 185 | } |
186 | ||
b8d8561b | 187 | void |
0673c0ba | 188 | _db_rotate_log(void) |
090089c4 | 189 | { |
190 | int i; | |
95d659f0 | 191 | LOCAL_ARRAY(char, from, MAXPATHLEN); |
192 | LOCAL_ARRAY(char, to, MAXPATHLEN); | |
88738790 | 193 | #ifdef S_ISREG |
2edc2504 | 194 | struct stat sb; |
88738790 | 195 | #endif |
090089c4 | 196 | |
197 | if (debug_log_file == NULL) | |
198 | return; | |
71d95578 | 199 | #ifdef S_ISREG |
2edc2504 | 200 | if (stat(debug_log_file, &sb) == 0) |
201 | if (S_ISREG(sb.st_mode) == 0) | |
202 | return; | |
71d95578 | 203 | #endif |
090089c4 | 204 | |
205 | /* Rotate numbers 0 through N up one */ | |
b6f794d6 | 206 | for (i = Config.Log.rotateNumber; i > 1;) { |
090089c4 | 207 | i--; |
042461c3 | 208 | snprintf(from, MAXPATHLEN, "%s.%d", debug_log_file, i - 1); |
209 | snprintf(to, MAXPATHLEN, "%s.%d", debug_log_file, i); | |
090089c4 | 210 | rename(from, to); |
211 | } | |
212 | /* Rotate the current log to .0 */ | |
b6f794d6 | 213 | if (Config.Log.rotateNumber > 0) { |
042461c3 | 214 | snprintf(to, MAXPATHLEN, "%s.%d", debug_log_file, 0); |
090089c4 | 215 | rename(debug_log_file, to); |
216 | } | |
217 | /* Close and reopen the log. It may have been renamed "manually" | |
218 | * before HUP'ing us. */ | |
0a8020d1 | 219 | if (debug_log != stderr) |
b6f794d6 | 220 | debugOpenLog(Config.Log.log); |
090089c4 | 221 | } |
4873d3a4 | 222 | |
223 | static const char * | |
224 | debugLogTime(time_t t) | |
225 | { | |
226 | struct tm *tm; | |
227 | static char buf[128]; | |
228 | static time_t last_t = 0; | |
229 | if (t != last_t) { | |
2ac76861 | 230 | tm = localtime(&t); |
231 | strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm); | |
232 | last_t = t; | |
4873d3a4 | 233 | } |
234 | return buf; | |
235 | } | |
236 | ||
bb71ea1f | 237 | void |
238 | xassert(const char *msg, const char *file, int line) | |
8a6218c6 | 239 | { |
bb71ea1f | 240 | debug(0, 0) ("assertion failed: %s:%d: \"%s\"\n", file, line, msg); |
241 | if (!shutting_down) | |
8a6218c6 | 242 | abort(); |
bb71ea1f | 243 | } |
244 | ||
123abbe1 | 245 | /* |
246 | * Context-based Debugging | |
26970499 | 247 | * |
248 | * Rationale | |
249 | * --------- | |
250 | * | |
251 | * When you have a long nested processing sequence, it is often impossible | |
252 | * for low level routines to know in what larger context they operate. If a | |
253 | * routine coredumps, one can restore the context using debugger trace. | |
254 | * However, in many case you do not want to coredump, but just want to report | |
255 | * a potential problem. A report maybe useless out of problem context. | |
256 | * | |
257 | * To solve this potential problem, use the following approach: | |
258 | * | |
259 | * int | |
260 | * top_level_foo(const char *url) | |
261 | * { | |
262 | * // define current context | |
263 | * // note: we stack but do not dup ctx descriptions! | |
264 | * Ctx ctx = ctx_enter(url); | |
265 | * ... | |
266 | * // go down; middle_level_bar will eventually call bottom_level_boo | |
267 | * middle_level_bar(method, protocol); | |
268 | * ... | |
269 | * // exit, clean after yourself | |
270 | * ctx_exit(ctx); | |
271 | * } | |
272 | * | |
273 | * void | |
274 | * bottom_level_boo(int status, void *data) | |
275 | * { | |
276 | * // detect exceptional condition, and simply report it, the context | |
277 | * // information will be available somewhere close in the log file | |
278 | * if (status == STRANGE_STATUS) | |
279 | * debug(13, 6) ("DOS attack detected, data: %p\n", data); | |
280 | * ... | |
281 | * } | |
282 | * | |
283 | * Current implementation is extremely simple but still very handy. It has a | |
284 | * negligible overhead (descriptions are not duplicated). | |
285 | * | |
286 | * When the _first_ debug message for a given context is printed, it is | |
287 | * prepended with the current context description. Context is printed with | |
288 | * the same debugging level as the original message. | |
289 | * | |
290 | * Note that we do not print context every type you do ctx_enter(). This | |
291 | * approach would produce too many useless messages. For the same reason, a | |
292 | * context description is printed at most _once_ even if you have 10 | |
293 | * debugging messages within one context. | |
294 | * | |
295 | * Contexts can be nested, of course. You must use ctx_enter() to enter a | |
296 | * context (push it onto stack). It is probably safe to exit several nested | |
297 | * contexts at _once_ by calling ctx_exit() at the top level (this will pop | |
298 | * all context till current one). However, as in any stack, you cannot start | |
299 | * in the middle. | |
300 | * | |
301 | * Analysis: | |
302 | * i) locate debugging message, | |
303 | * ii) locate current context by going _upstream_ in your log file, | |
304 | * iii) hack away. | |
305 | * | |
306 | * | |
123abbe1 | 307 | * To-Do: |
26970499 | 308 | * ----- |
309 | * | |
123abbe1 | 310 | * decide if we want to dup() descriptions (adds overhead) but allows to |
311 | * add printf()-style interface | |
26970499 | 312 | * |
2ac76861 | 313 | * implementation: |
26970499 | 314 | * --------------- |
2ac76861 | 315 | * |
123abbe1 | 316 | * descriptions for contexts over CTX_MAX_LEVEL limit are ignored, you probably |
317 | * have a bug if your nesting goes that deep. | |
318 | */ | |
2ac76861 | 319 | |
123abbe1 | 320 | #define CTX_MAX_LEVEL 255 |
2ac76861 | 321 | |
322 | /* | |
323 | * produce a warning when nesting reaches this level and then double | |
324 | * the level | |
325 | */ | |
326 | static int Ctx_Warn_Level = 32; | |
123abbe1 | 327 | /* all descriptions has been printed up to this level */ |
a3f9588e | 328 | static int Ctx_Reported_Level = -1; |
123abbe1 | 329 | /* descriptions are still valid or active up to this level */ |
a3f9588e | 330 | static int Ctx_Valid_Level = -1; |
123abbe1 | 331 | /* current level, the number of nested ctx_enter() calls */ |
a3f9588e | 332 | static int Ctx_Current_Level = -1; |
123abbe1 | 333 | /* saved descriptions (stack) */ |
2ac76861 | 334 | static const char *Ctx_Descrs[CTX_MAX_LEVEL + 1]; |
b70d1c58 | 335 | /* "safe" get secription */ |
336 | static const char *ctx_get_descr(Ctx ctx); | |
123abbe1 | 337 | |
338 | ||
339 | Ctx | |
340 | ctx_enter(const char *descr) | |
341 | { | |
342 | Ctx_Current_Level++; | |
343 | ||
344 | if (Ctx_Current_Level <= CTX_MAX_LEVEL) | |
345 | Ctx_Descrs[Ctx_Current_Level] = descr; | |
346 | ||
347 | if (Ctx_Current_Level == Ctx_Warn_Level) { | |
2ac76861 | 348 | debug(0, 0) ("# ctx: suspiciously deep (%d) nesting:\n", Ctx_Warn_Level); |
123abbe1 | 349 | Ctx_Warn_Level *= 2; |
350 | } | |
123abbe1 | 351 | return Ctx_Current_Level; |
352 | } | |
353 | ||
354 | void | |
355 | ctx_exit(Ctx ctx) | |
356 | { | |
357 | assert(ctx >= 0); | |
2ac76861 | 358 | Ctx_Current_Level = (ctx >= 0) ? ctx - 1 : -1; |
123abbe1 | 359 | if (Ctx_Valid_Level > Ctx_Current_Level) |
360 | Ctx_Valid_Level = Ctx_Current_Level; | |
361 | } | |
362 | ||
363 | /* | |
364 | * the idea id to print each context description at most once but provide enough | |
365 | * info for deducing the current execution stack | |
366 | */ | |
367 | static void | |
368 | ctx_print() | |
369 | { | |
370 | /* lock so _db_print will not call us recursively */ | |
371 | Ctx_Lock++; | |
372 | /* ok, user saw [0,Ctx_Reported_Level] descriptions */ | |
373 | /* first inform about entries popped since user saw them */ | |
374 | if (Ctx_Valid_Level < Ctx_Reported_Level) { | |
2ac76861 | 375 | if (Ctx_Reported_Level != Ctx_Valid_Level + 1) |
376 | _db_print("ctx: exit levels from %2d down to %2d\n", | |
377 | Ctx_Reported_Level, Ctx_Valid_Level + 1); | |
4a0d3a91 | 378 | else |
379 | _db_print("ctx: exit level %2d\n", Ctx_Reported_Level); | |
123abbe1 | 380 | Ctx_Reported_Level = Ctx_Valid_Level; |
381 | } | |
382 | /* report new contexts that were pushed since last report */ | |
383 | while (Ctx_Reported_Level < Ctx_Current_Level) { | |
384 | Ctx_Reported_Level++; | |
385 | Ctx_Valid_Level++; | |
2ac76861 | 386 | _db_print("ctx: enter level %2d: '%s'\n", Ctx_Reported_Level, |
b70d1c58 | 387 | ctx_get_descr(Ctx_Reported_Level)); |
123abbe1 | 388 | } |
389 | /* unlock */ | |
390 | Ctx_Lock--; | |
391 | } | |
b70d1c58 | 392 | |
393 | /* checks for nulls and overflows */ | |
394 | static const char * | |
395 | ctx_get_descr(Ctx ctx) | |
396 | { | |
2ac76861 | 397 | if (ctx < 0 || ctx > CTX_MAX_LEVEL) |
398 | return "<lost>"; | |
b70d1c58 | 399 | return Ctx_Descrs[ctx] ? Ctx_Descrs[ctx] : "<null>"; |
400 | } |