Implement support for attaching GDB to the Icinga process on crash

fixes #9866
This commit is contained in:
Gunnar Beutner 2015-08-08 09:40:16 +02:00 committed by Michael Friedrich
parent 04177cb736
commit 8f3396ae0d
4 changed files with 103 additions and 78 deletions

View File

@ -375,6 +375,7 @@ EnableHostChecks |**Read-write.** Whether active host checks are globally ena
EnableServiceChecks |**Read-write.** Whether active service checks are globally enabled. Defaults to true.
EnablePerfdata |**Read-write.** Whether performance data processing is globally enabled. Defaults to true.
UseVfork |**Read-write.** Whether to use vfork(). Only available on *NIX. Defaults to true.
AttachDebugger |**Read-write.** Whether to attach a debugger when Icinga 2 crashes. Defaults to false.
RunAsUser |**Read-write.** Defines the user the Icinga 2 daemon is running as. Used in the `init.conf` configuration file.
RunAsGroup |**Read-write.** Defines the group the Icinga 2 daemon is running as. Used in the `init.conf` configuration file.

View File

@ -173,6 +173,8 @@ int Main(void)
ScriptGlobal::Set("UseVfork", true);
#endif /* __APPLE__ */
ScriptGlobal::Set("AttachDebugger", false);
LogSeverity logLevel = Logger::GetConsoleLogSeverity();
Logger::SetConsoleLogSeverity(LogWarning);

View File

@ -626,9 +626,9 @@ String Application::GetCrashReportFilename(void)
}
#ifndef _WIN32
void Application::GetDebuggerBacktrace(const String& filename)
void Application::AttachDebugger(const String& filename, bool interactive)
{
#ifndef _WIN32
#ifdef __linux__
prctl(PR_SET_DUMPABLE, 1);
#endif /* __linux __ */
@ -644,6 +644,7 @@ void Application::GetDebuggerBacktrace(const String& filename)
}
if (pid == 0) {
if (!interactive) {
int fd = open(filename.CStr(), O_CREAT | O_RDWR | O_APPEND, 0600);
if (fd < 0) {
@ -664,9 +665,21 @@ void Application::GetDebuggerBacktrace(const String& filename)
close(2);
dup2(1, 2);
}
char **argv;
char *my_pid_str = strdup(my_pid.CStr());
const char *argv[] = {
if (interactive) {
const char *uargv[] = {
"gdb",
"-p",
my_pid_str,
NULL
};
argv = const_cast<char **>(uargv);
} else {
const char *uargv[] = {
"gdb",
"--batch",
"-p",
@ -679,7 +692,10 @@ void Application::GetDebuggerBacktrace(const String& filename)
"quit",
NULL
};
(void)execvp(argv[0], const_cast<char **>(argv));
argv = const_cast<char **>(uargv);
}
(void)execvp(argv[0], argv);
perror("Failed to launch GDB");
free(my_pid_str);
_exit(0);
@ -695,6 +711,9 @@ void Application::GetDebuggerBacktrace(const String& filename)
#ifdef __linux__
prctl(PR_SET_DUMPABLE, 0);
#endif /* __linux __ */
#else /* _WIN32 */
DebugBreak();
#endif /* _WIN32 */
}
/**
@ -750,6 +769,9 @@ void Application::SigAbrtHandler(int)
String fname = GetCrashReportFilename();
Utility::MkDir(Utility::DirName(fname), 0750);
bool interactive_debugger = Convert::ToBool(ScriptGlobal::Get("AttachDebugger"));
if (!interactive_debugger) {
std::ofstream ofs;
ofs.open(fname.CStr());
@ -764,16 +786,16 @@ void Application::SigAbrtHandler(int)
DisplayBugMessage(ofs);
#ifndef _WIN32
ofs << "\n";
ofs.close();
} else {
Log(LogCritical, "Application", "Icinga 2 has terminated unexpeectedly. Attaching debugger...");
}
GetDebuggerBacktrace(fname);
#else /* _WIN32 */
ofs.close();
#endif /* _WIN32 */
AttachDebugger(fname, interactive_debugger);
}
#else /* _WIN32 */
#ifdef _WIN32
/**
* Console control handler. Prepares the application for cleanly
* shutting down during the next execution of the event loop.
@ -813,6 +835,9 @@ void Application::ExceptionHandler(void)
String fname = GetCrashReportFilename();
Utility::MkDir(Utility::DirName(fname), 0750);
bool interactive_debugger = Convert::ToBool(ScriptGlobal::Get("AttachDebugger"));
if (interactive_debugger) {
std::ofstream ofs;
ofs.open(fname.CStr());
@ -838,10 +863,9 @@ void Application::ExceptionHandler(void)
DisplayBugMessage(ofs);
ofs.close();
}
#ifndef _WIN32
GetDebuggerBacktrace(fname);
#endif /* _WIN32 */
AttachDebugger(fname, interactive_debugger);
abort();
}

View File

@ -175,9 +175,7 @@ private:
static String GetCrashReportFilename(void);
#ifndef _WIN32
static void GetDebuggerBacktrace(const String& filename);
#endif /* _WIN32 */
static void AttachDebugger(const String& filename, bool interactive);
};
}