| 1 |
/* debug.c |
| 2 |
* |
| 3 |
* This file proides debug support. |
| 4 |
* |
| 5 |
* File begun on 2008-01-22 by RGerhards |
| 6 |
* |
| 7 |
* Some functions are controlled by environment variables: |
| 8 |
* |
| 9 |
* RSYSLOG_DEBUGLOG if set, a debug log file is written to that location |
| 10 |
* RSYSLOG_DEBUG specific debug options |
| 11 |
* |
| 12 |
* For details, visit doc/debug.html |
| 13 |
* |
| 14 |
* Copyright 2008-2018 Rainer Gerhards and Adiscon GmbH. |
| 15 |
* |
| 16 |
* This file is part of the rsyslog runtime library. |
| 17 |
* |
| 18 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 |
* you may not use this file except in compliance with the License. |
| 20 |
* You may obtain a copy of the License at |
| 21 |
* |
| 22 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 23 |
* -or- |
| 24 |
* see COPYING.ASL20 in the source distribution |
| 25 |
* |
| 26 |
* Unless required by applicable law or agreed to in writing, software |
| 27 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 28 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 |
* See the License for the specific language governing permissions and |
| 30 |
* limitations under the License. |
| 31 |
*/ |
| 32 |
#include "config.h" /* autotools! */ |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdarg.h> |
| 36 |
#include <string.h> |
| 37 |
#include <signal.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <pthread.h> |
| 40 |
#include <ctype.h> |
| 41 |
#include <assert.h> |
| 42 |
#include <unistd.h> |
| 43 |
#include <fcntl.h> |
| 44 |
#include <sys/stat.h> |
| 45 |
#ifdef HAVE_SYS_SYSCALL_H |
| 46 |
#include <sys/syscall.h> |
| 47 |
#endif |
| 48 |
#if _POSIX_TIMERS <= 0 |
| 49 |
#include <sys/time.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#include "rsyslog.h" |
| 53 |
#include "debug.h" |
| 54 |
#include "cfsysline.h" |
| 55 |
#include "obj.h" |
| 56 |
|
| 57 |
|
| 58 |
/* static data (some time to be replaced) */ |
| 59 |
DEFobjCurrIf(obj) int Debug = DEBUG_OFF; /* debug flag - read-only after startup */ |
| 60 |
int debugging_on = 0; /* read-only, except on sig USR1 */ |
| 61 |
int dbgTimeoutToStderr = 0; |
| 62 |
static int bPrintTime = 1; /* print a timestamp together with debug message */ |
| 63 |
static int bOutputTidToStderr = 0; /* output TID to stderr on thread creation */ |
| 64 |
char *pszAltDbgFileName = NULL; /* if set, debug output is *also* sent to here */ |
| 65 |
int altdbg = -1; /* and the handle for alternate debug output */ |
| 66 |
int stddbg = 1; /* the handle for regular debug output, set to stdout if not forking, -1 otherwise */ |
| 67 |
static uint64_t dummy_errcount = 0; /* just to avoid some static analyzer complaints */ |
| 68 |
static pthread_key_t keyThrdName; |
| 69 |
|
| 70 |
|
| 71 |
/* output the current thread ID to "relevant" places |
| 72 |
* (what "relevant" means is determinded by various ways) |
| 73 |
*/ |
| 74 |
void dbgOutputTID(char *name __attribute__((unused))) { |
| 75 |
#if defined(HAVE_SYSCALL) && defined(HAVE_SYS_gettid) |
| 76 |
if (bOutputTidToStderr) fprintf(stderr, "thread tid %u, name '%s'\n", (unsigned)syscall(SYS_gettid), name); |
| 77 |
DBGPRINTF("thread created, tid %u, name '%s'\n", (unsigned)syscall(SYS_gettid), name); |
| 78 |
#endif |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/* build a string with the thread name. If none is set, the thread ID is |
| 83 |
* used instead. Caller must provide buffer space. |
| 84 |
*/ |
| 85 |
static void ATTR_NONNULL() dbgGetThrdName(char *const pszBuf, const size_t lenBuf, const pthread_t thrdID) { |
| 86 |
assert(pszBuf != NULL); |
| 87 |
|
| 88 |
const char *const thrdName = pthread_getspecific(keyThrdName); |
| 89 |
if (thrdName == NULL) { |
| 90 |
snprintf(pszBuf, lenBuf, "%lx", (long)thrdID); |
| 91 |
} else { |
| 92 |
snprintf(pszBuf, lenBuf, "%-15s", thrdName); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/* set a name for the current thread */ |
| 98 |
void ATTR_NONNULL() dbgSetThrdName(const uchar *const pszName) { |
| 99 |
(void)pthread_setspecific(keyThrdName, strdup((char *)pszName)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/* destructor for thread name (called by pthreads!) */ |
| 104 |
static void dbgThrdNameDestruct(void *arg) { |
| 105 |
free(arg); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/* write the debug message. This is a helper to dbgprintf and dbgoprint which |
| 110 |
* contains common code. added 2008-09-26 rgerhards |
| 111 |
* Note: We need to split the function due to the bad nature of POSIX |
| 112 |
* cancel cleanup handlers. |
| 113 |
*/ |
| 114 |
static void DBGL_UNUSED dbgprint(obj_t *pObj, char *pszMsg, const char *pszFileName, const size_t lenMsg) { |
| 115 |
uchar *pszObjName = NULL; |
| 116 |
static pthread_t ptLastThrdID = 0; |
| 117 |
static int bWasNL = 0; |
| 118 |
char pszThrdName[64]; /* 64 is to be on the safe side, anything over 20 is bad... */ |
| 119 |
char pszWriteBuf[32 * 1024]; |
| 120 |
size_t lenCopy; |
| 121 |
size_t offsWriteBuf = 0; |
| 122 |
size_t lenWriteBuf; |
| 123 |
struct timespec t; |
| 124 |
#if _POSIX_TIMERS <= 0 |
| 125 |
struct timeval tv; |
| 126 |
#endif |
| 127 |
|
| 128 |
if (pObj != NULL) { |
| 129 |
pszObjName = obj.GetName(pObj); |
| 130 |
} |
| 131 |
|
| 132 |
/* The bWasNL handler does not really work. It works if no thread |
| 133 |
* switching occurs during non-NL messages. Else, things are messed |
| 134 |
* up. Anyhow, it works well enough to provide useful help during |
| 135 |
* getting this up and running. It is questionable if the extra effort |
| 136 |
* is worth fixing it, giving the limited appliability. -- rgerhards, 2005-10-25 |
| 137 |
* I have decided that it is not worth fixing it - especially as it works |
| 138 |
* pretty well. -- rgerhards, 2007-06-15 |
| 139 |
*/ |
| 140 |
if (ptLastThrdID != pthread_self()) { |
| 141 |
if (!bWasNL) { |
| 142 |
pszWriteBuf[0] = '\n'; |
| 143 |
offsWriteBuf = 1; |
| 144 |
bWasNL = 1; |
| 145 |
} |
| 146 |
ptLastThrdID = pthread_self(); |
| 147 |
} |
| 148 |
|
| 149 |
dbgGetThrdName(pszThrdName, sizeof(pszThrdName), ptLastThrdID); |
| 150 |
|
| 151 |
if (bWasNL) { |
| 152 |
if (bPrintTime) { |
| 153 |
#if _POSIX_TIMERS > 0 |
| 154 |
/* this is the "regular" code */ |
| 155 |
clock_gettime(CLOCK_REALTIME, &t); |
| 156 |
#else |
| 157 |
gettimeofday(&tv, NULL); |
| 158 |
t.tv_sec = tv.tv_sec; |
| 159 |
t.tv_nsec = tv.tv_usec * 1000; |
| 160 |
#endif |
| 161 |
lenWriteBuf = snprintf(pszWriteBuf + offsWriteBuf, sizeof(pszWriteBuf) - offsWriteBuf, |
| 162 |
"%4.4ld.%9.9ld:", (long)(t.tv_sec % 10000), t.tv_nsec); |
| 163 |
offsWriteBuf += lenWriteBuf; |
| 164 |
} |
| 165 |
|
| 166 |
lenWriteBuf = snprintf(pszWriteBuf + offsWriteBuf, sizeof(pszWriteBuf) - offsWriteBuf, "%s: ", pszThrdName); |
| 167 |
offsWriteBuf += lenWriteBuf; |
| 168 |
/* print object name header if we have an object */ |
| 169 |
if (pszObjName != NULL) { |
| 170 |
lenWriteBuf = snprintf(pszWriteBuf + offsWriteBuf, sizeof(pszWriteBuf) - offsWriteBuf, "%s: ", pszObjName); |
| 171 |
offsWriteBuf += lenWriteBuf; |
| 172 |
} |
| 173 |
lenWriteBuf = snprintf(pszWriteBuf + offsWriteBuf, sizeof(pszWriteBuf) - offsWriteBuf, "%s: ", pszFileName); |
| 174 |
offsWriteBuf += lenWriteBuf; |
| 175 |
} |
| 176 |
if (lenMsg > sizeof(pszWriteBuf) - offsWriteBuf) |
| 177 |
lenCopy = sizeof(pszWriteBuf) - offsWriteBuf; |
| 178 |
else |
| 179 |
lenCopy = lenMsg; |
| 180 |
memcpy(pszWriteBuf + offsWriteBuf, pszMsg, lenCopy); |
| 181 |
offsWriteBuf += lenCopy; |
| 182 |
/* the write is included in an "if" just to silence compiler |
| 183 |
* warnings. Here, we really don't care if the write fails, we |
| 184 |
* have no good response to that in any case... -- rgerhards, 2012-11-28 |
| 185 |
*/ |
| 186 |
if (stddbg != -1) { |
| 187 |
if (write(stddbg, pszWriteBuf, offsWriteBuf)) { |
| 188 |
++dummy_errcount; |
| 189 |
} |
| 190 |
} |
| 191 |
if (altdbg != -1) { |
| 192 |
if (write(altdbg, pszWriteBuf, offsWriteBuf)) { |
| 193 |
++dummy_errcount; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
bWasNL = (pszMsg[lenMsg - 1] == '\n') ? 1 : 0; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
static int DBGL_UNUSED checkDbgFile(const char *srcname) { |
| 202 |
if (glblDbgFilesNum == 0) { |
| 203 |
return 1; |
| 204 |
} |
| 205 |
if (glblDbgWhitelist) { |
| 206 |
if (bsearch(srcname, glblDbgFiles, glblDbgFilesNum, sizeof(char *), bs_arrcmp_glblDbgFiles) == NULL) { |
| 207 |
return 0; |
| 208 |
} else { |
| 209 |
return 1; |
| 210 |
} |
| 211 |
} else { |
| 212 |
if (bsearch(srcname, glblDbgFiles, glblDbgFilesNum, sizeof(char *), bs_arrcmp_glblDbgFiles) != NULL) { |
| 213 |
return 0; |
| 214 |
} else { |
| 215 |
return 1; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
/* print some debug output when an object is given |
| 220 |
* This is mostly a copy of dbgprintf, but I do not know how to combine it |
| 221 |
* into a single function as we have variable arguments and I don't know how to call |
| 222 |
* from one vararg function into another. I don't dig in this, it is OK for the |
| 223 |
* time being. -- rgerhards, 2008-01-29 |
| 224 |
*/ |
| 225 |
#ifndef DEBUGLESS |
| 226 |
void r_dbgoprint(const char *srcname, obj_t *pObj, const char *fmt, ...) { |
| 227 |
va_list ap; |
| 228 |
char pszWriteBuf[32 * 1024]; |
| 229 |
size_t lenWriteBuf; |
| 230 |
|
| 231 |
if (!(Debug && debugging_on)) return; |
| 232 |
|
| 233 |
if (!checkDbgFile(srcname)) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
va_start(ap, fmt); |
| 238 |
lenWriteBuf = vsnprintf(pszWriteBuf, sizeof(pszWriteBuf), fmt, ap); |
| 239 |
va_end(ap); |
| 240 |
if (lenWriteBuf >= sizeof(pszWriteBuf)) { |
| 241 |
/* prevent buffer overrruns and garbagge display */ |
| 242 |
pszWriteBuf[sizeof(pszWriteBuf) - 5] = '.'; |
| 243 |
pszWriteBuf[sizeof(pszWriteBuf) - 4] = '.'; |
| 244 |
pszWriteBuf[sizeof(pszWriteBuf) - 3] = '.'; |
| 245 |
pszWriteBuf[sizeof(pszWriteBuf) - 2] = '\n'; |
| 246 |
pszWriteBuf[sizeof(pszWriteBuf) - 1] = '\0'; |
| 247 |
lenWriteBuf = sizeof(pszWriteBuf); |
| 248 |
} |
| 249 |
dbgprint(pObj, pszWriteBuf, srcname, lenWriteBuf); |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
/* print some debug output when no object is given |
| 254 |
* WARNING: duplicate code, see dbgoprint above! |
| 255 |
*/ |
| 256 |
#ifndef DEBUGLESS |
| 257 |
void r_dbgprintf(const char *srcname, const char *fmt, ...) { |
| 258 |
va_list ap; |
| 259 |
char pszWriteBuf[32 * 1024]; |
| 260 |
size_t lenWriteBuf; |
| 261 |
|
| 262 |
if (!(Debug && debugging_on)) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
if (!checkDbgFile(srcname)) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
va_start(ap, fmt); |
| 271 |
lenWriteBuf = vsnprintf(pszWriteBuf, sizeof(pszWriteBuf), fmt, ap); |
| 272 |
va_end(ap); |
| 273 |
if (lenWriteBuf >= sizeof(pszWriteBuf)) { |
| 274 |
/* prevent buffer overrruns and garbagge display */ |
| 275 |
pszWriteBuf[sizeof(pszWriteBuf) - 5] = '.'; |
| 276 |
pszWriteBuf[sizeof(pszWriteBuf) - 4] = '.'; |
| 277 |
pszWriteBuf[sizeof(pszWriteBuf) - 3] = '.'; |
| 278 |
pszWriteBuf[sizeof(pszWriteBuf) - 2] = '\n'; |
| 279 |
pszWriteBuf[sizeof(pszWriteBuf) - 1] = '\0'; |
| 280 |
lenWriteBuf = sizeof(pszWriteBuf); |
| 281 |
} |
| 282 |
dbgprint(NULL, pszWriteBuf, srcname, lenWriteBuf); |
| 283 |
} |
| 284 |
#endif |
| 285 |
|
| 286 |
/* support system to set debug options at runtime */ |
| 287 |
|
| 288 |
|
| 289 |
/* parse a param/value pair from the current location of the |
| 290 |
* option string. Returns 1 if an option was found, 0 |
| 291 |
* otherwise. 0 means there are NO MORE options to be |
| 292 |
* processed. -- rgerhards, 2008-02-28 |
| 293 |
*/ |
| 294 |
static int ATTR_NONNULL() dbgGetRTOptNamVal(const uchar **const ppszOpt, uchar **const ppOptName) { |
| 295 |
assert(ppszOpt != NULL); |
| 296 |
assert(*ppszOpt != NULL); |
| 297 |
int bRet = 0; |
| 298 |
const uchar *p = *ppszOpt; |
| 299 |
size_t i; |
| 300 |
static uchar optname[128] = ""; |
| 301 |
|
| 302 |
/* skip whitespace */ |
| 303 |
while (*p && isspace(*p)) ++p; |
| 304 |
|
| 305 |
/* name: up until whitespace */ |
| 306 |
i = 0; |
| 307 |
while (i < (sizeof(optname) - 1) && *p && !isspace(*p)) { |
| 308 |
optname[i++] = *p++; |
| 309 |
} |
| 310 |
|
| 311 |
if (i > 0) { |
| 312 |
bRet = 1; |
| 313 |
optname[i] = '\0'; |
| 314 |
} |
| 315 |
|
| 316 |
/* done */ |
| 317 |
*ppszOpt = p; |
| 318 |
*ppOptName = optname; |
| 319 |
return bRet; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
/* report fd used for debug log. This is needed in case of |
| 324 |
* auto-backgrounding, where the debug log shall not be closed. |
| 325 |
*/ |
| 326 |
int dbgGetDbglogFd(void) { |
| 327 |
return altdbg; |
| 328 |
} |
| 329 |
|
| 330 |
/* read in the runtime options |
| 331 |
* rgerhards, 2008-02-28 |
| 332 |
*/ |
| 333 |
static void dbgGetRuntimeOptions(void) { |
| 334 |
const uchar *pszOpts; |
| 335 |
uchar *optname; |
| 336 |
|
| 337 |
/* set some defaults */ |
| 338 |
if ((pszOpts = (uchar *)getenv("RSYSLOG_DEBUG")) != NULL) { |
| 339 |
/* we have options set, so let's process them */ |
| 340 |
while (dbgGetRTOptNamVal(&pszOpts, &optname)) { |
| 341 |
if (!strcasecmp((char *)optname, "help")) { |
| 342 |
fprintf(stderr, "rsyslogd " VERSION |
| 343 |
" runtime debug support - help requested, " |
| 344 |
"rsyslog terminates\n\nenvironment variables:\n" |
| 345 |
"additional logfile: export RSYSLOG_DEBUGFILE=\"/path/to/file\"\n" |
| 346 |
"to set: export RSYSLOG_DEBUG=\"cmd cmd cmd\"\n\n" |
| 347 |
"Commands are (all case-insensitive):\n" |
| 348 |
"help (this list, terminates rsyslogd)\n" |
| 349 |
"NoLogTimestamp\n" |
| 350 |
"Nostdout\n" |
| 351 |
"OutputTidToStderr\n" |
| 352 |
"DebugOnDemand - enables debugging on USR1, but does not turn on output\n" |
| 353 |
"\nSee debug.html in your doc set or https://www.rsyslog.com for details\n"); |
| 354 |
exit(1); |
| 355 |
} else if (!strcasecmp((char *)optname, "debug")) { |
| 356 |
/* this is earlier in the process than the -d option, as such it |
| 357 |
* allows us to spit out debug messages from the very beginning. |
| 358 |
*/ |
| 359 |
Debug = DEBUG_FULL; |
| 360 |
debugging_on = 1; |
| 361 |
} else if (!strcasecmp((char *)optname, "debugondemand")) { |
| 362 |
/* Enables debugging, but turns off debug output */ |
| 363 |
Debug = DEBUG_ONDEMAND; |
| 364 |
debugging_on = 1; |
| 365 |
dbgprintf( |
| 366 |
"Note: debug on demand turned on via configuration file, " |
| 367 |
"use USR1 signal to activate.\n"); |
| 368 |
debugging_on = 0; |
| 369 |
} else if (!strcasecmp((char *)optname, "nologtimestamp")) { |
| 370 |
bPrintTime = 0; |
| 371 |
} else if (!strcasecmp((char *)optname, "nostdout")) { |
| 372 |
stddbg = -1; |
| 373 |
} else if (!strcasecmp((char *)optname, "outputtidtostderr")) { |
| 374 |
bOutputTidToStderr = 1; |
| 375 |
} else { |
| 376 |
fprintf(stderr, |
| 377 |
"rsyslogd " VERSION |
| 378 |
" error: invalid debug option '%s' " |
| 379 |
"- ignored\n", |
| 380 |
optname); |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
void dbgSetDebugLevel(int level) { |
| 388 |
Debug = level; |
| 389 |
debugging_on = (level == DEBUG_FULL) ? 1 : 0; |
| 390 |
} |
| 391 |
|
| 392 |
void dbgSetDebugFile(uchar *fn) { |
| 393 |
if (altdbg != -1) { |
| 394 |
dbgprintf("switching to debug file %s\n", fn); |
| 395 |
close(altdbg); |
| 396 |
} |
| 397 |
if ((altdbg = open((char *)fn, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY | O_CLOEXEC, S_IRUSR | S_IWUSR)) == -1) { |
| 398 |
fprintf(stderr, "alternate debug file could not be opened, ignoring. Error: %s\n", strerror(errno)); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/* end support system to set debug options at runtime */ |
| 403 |
|
| 404 |
rsRetVal dbgClassInit(void) { |
| 405 |
rsRetVal iRet; /* do not use DEFiRet, as this makes calls into the debug system! */ |
| 406 |
|
| 407 |
|
| 408 |
(void)pthread_key_create(&keyThrdName, dbgThrdNameDestruct); |
| 409 |
|
| 410 |
/* while we try not to use any of the real rsyslog code (to avoid infinite loops), we |
| 411 |
* need to have the ability to query object names. Thus, we need to obtain a pointer to |
| 412 |
* the object interface. -- rgerhards, 2008-02-29 |
| 413 |
*/ |
| 414 |
CHKiRet(objGetObjInterface(&obj)); /* this provides the root pointer for all other queries */ |
| 415 |
|
| 416 |
const char *dbgto2stderr; |
| 417 |
dbgto2stderr = getenv("RSYSLOG_DEBUG_TIMEOUTS_TO_STDERR"); |
| 418 |
dbgTimeoutToStderr = (dbgto2stderr != NULL && !strcmp(dbgto2stderr, "on")) ? 1 : 0; |
| 419 |
if (dbgTimeoutToStderr) { |
| 420 |
fprintf(stderr, "rsyslogd: NOTE: RSYSLOG_DEBUG_TIMEOUTS_TO_STDERR activated\n"); |
| 421 |
} |
| 422 |
dbgGetRuntimeOptions(); /* init debug system from environment */ |
| 423 |
pszAltDbgFileName = getenv("RSYSLOG_DEBUGLOG"); |
| 424 |
|
| 425 |
if (pszAltDbgFileName != NULL) { |
| 426 |
/* we have a secondary file, so let's open it) */ |
| 427 |
if ((altdbg = open(pszAltDbgFileName, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY | O_CLOEXEC, |
| 428 |
S_IRUSR | S_IWUSR)) == -1) { |
| 429 |
fprintf(stderr, "alternate debug file could not be opened, ignoring. Error: %s\n", strerror(errno)); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
dbgSetThrdName((uchar *)"main thread"); |
| 434 |
|
| 435 |
finalize_it: |
| 436 |
return (iRet); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
rsRetVal dbgClassExit(void) { |
| 441 |
pthread_key_delete(keyThrdName); |
| 442 |
if (altdbg != -1) close(altdbg); |
| 443 |
return RS_RET_OK; |
| 444 |
} |