summaryrefslogtreecommitdiffstats
path: root/runtime/debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/debug.c')
-rw-r--r--runtime/debug.c58
1 files changed, 51 insertions, 7 deletions
diff --git a/runtime/debug.c b/runtime/debug.c
index 26da31b4..68474989 100644
--- a/runtime/debug.c
+++ b/runtime/debug.c
@@ -44,6 +44,9 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
+#ifdef HAVE_SYS_SYSCALL_H
+# include <sys/syscall.h>
+#endif
#if _POSIX_TIMERS <= 0
#include <sys/time.h>
#endif
@@ -66,6 +69,7 @@ static int bPrintMutexAction = 0; /* shall mutex calls be printed to the debug l
static int bPrintTime = 1; /* print a timestamp together with debug message */
static int bPrintAllDebugOnExit = 0;
static int bAbortTrace = 1; /* print a trace after SIGABRT or SIGSEGV */
+static int bOutputTidToStderr = 0;/* output TID to stderr on thread creation */
static char *pszAltDbgFileName = NULL; /* if set, debug output is *also* sent to here */
static int altdbg = -1; /* and the handle for alternate debug output */
int stddbg = 1; /* the handle for regular debug output, set to stdout if not forking, -1 otherwise */
@@ -293,6 +297,21 @@ static inline void dbgFuncDBRemoveMutexLock(dbgFuncDB_t *pFuncDB, pthread_mutex_
/* ------------------------- END FuncDB utility functions ------------------------- */
+/* output the current thread ID to "relevant" places
+ * (what "relevant" means is determinded by various ways)
+ */
+void
+dbgOutputTID(char* name)
+{
+# if defined(HAVE_SYSCALL) && defined(HAVE_SYS_gettid)
+ if(bOutputTidToStderr)
+ fprintf(stderr, "thread tid %u, name '%s'\n",
+ (unsigned)syscall(SYS_gettid), name);
+ DBGPRINTF("thread created, tid %u, name '%s'\n",
+ (unsigned)syscall(SYS_gettid), name);
+# endif
+}
+
/* ###########################################################################
* IMPORTANT NOTE
* Mutex instrumentation reduces the code's concurrency and thus affects its
@@ -1334,6 +1353,7 @@ dbgGetRuntimeOptions(void)
"PrintAllDebugInfoOnExit (not yet implemented)\n"
"NoLogTimestamp\n"
"Nostdoout\n"
+ "OutputTidToStderr\n"
"filetrace=file (may be provided multiple times)\n"
"DebugOnDemand - enables debugging on USR1, but does not turn on output\n"
"\nSee debug.html in your doc set or http://www.rsyslog.com for details\n");
@@ -1367,6 +1387,8 @@ dbgGetRuntimeOptions(void)
stddbg = -1;
} else if(!strcasecmp((char*)optname, "noaborttrace")) {
bAbortTrace = 0;
+ } else if(!strcasecmp((char*)optname, "outputtidtostderr")) {
+ bOutputTidToStderr = 1;
} else if(!strcasecmp((char*)optname, "filetrace")) {
if(*optval == '\0') {
fprintf(stderr, "rsyslogd " VERSION " error: logfile debug option requires filename, "
@@ -1385,10 +1407,30 @@ dbgGetRuntimeOptions(void)
}
+void
+dbgSetDebugLevel(int level)
+{
+ Debug = level;
+ debugging_on = (level == DEBUG_FULL) ? 1 : 0;
+}
+
+void
+dbgSetDebugFile(uchar *fn)
+{
+ if(altdbg != -1) {
+ dbgprintf("switching to debug file %s\n", fn);
+ close(altdbg);
+ }
+ if((altdbg = open((char*)fn, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, S_IRUSR|S_IWUSR)) == -1) {
+ fprintf(stderr, "alternate debug file could not be opened, ignoring. Error: %s\n", strerror(errno));
+ }
+}
+
/* end support system to set debug options at runtime */
rsRetVal dbgClassInit(void)
{
+ pthread_mutexattr_t mutAttr;
rsRetVal iRet; /* do not use DEFiRet, as this makes calls into the debug system! */
struct sigaction sigAct;
@@ -1396,14 +1438,16 @@ rsRetVal dbgClassInit(void)
(void) pthread_key_create(&keyCallStack, dbgCallStackDestruct); /* MUST be the first action done! */
- /* we initialize all Mutexes with code, as some platforms seem to have
- * bugs in the static initializer macros. So better be on the safe side...
- * rgerhards, 2008-03-06
+ /* the mutexes must be recursive, because it may be called from within
+ * signal handlers, which can lead to a hang if the signal interrupted dbgprintf
+ * (yes, we have really seen that situation in practice!). -- rgerhards, 2013-05-17
*/
- pthread_mutex_init(&mutFuncDBList, NULL);
- pthread_mutex_init(&mutMutLog, NULL);
- pthread_mutex_init(&mutCallStack, NULL);
- pthread_mutex_init(&mutdbgprint, NULL);
+ pthread_mutexattr_init(&mutAttr);
+ pthread_mutexattr_settype(&mutAttr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&mutFuncDBList, &mutAttr);
+ pthread_mutex_init(&mutMutLog, &mutAttr);
+ pthread_mutex_init(&mutCallStack, &mutAttr);
+ pthread_mutex_init(&mutdbgprint, &mutAttr);
/* while we try not to use any of the real rsyslog code (to avoid infinite loops), we
* need to have the ability to query object names. Thus, we need to obtain a pointer to