Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 0d9af49c authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

Enable AKONADI_DEBUG_WAIT on Windows

There's no equivalent real to sending SIGSTOP to a process on Windows so
instead we check in AgentBase if AKONADI_DEBUG_WAIT is set and matches the
current executable name (without .exe) and if so we use WinAPI to wait for
a debugger to be attached to the agent process and once that happens we set
a breakpoint immediatelly.
parent 2058ae6c
......@@ -58,6 +58,12 @@
# include <malloc.h> // for dumping memory information
#endif
#ifdef Q_OS_WIN
#include <Windows.h>
#include <chrono>
#include <thread>
#endif
using namespace Akonadi;
static AgentBase *sAgentBase = nullptr;
......@@ -907,6 +913,20 @@ AgentBase::~AgentBase()
delete d_ptr;
}
void AgentBase::debugAgent(int argc, char **argv)
{
#ifdef Q_OS_WIN
if (qEnvironmentVariableIsSet("AKONADI_DEBUG_WAIT")) {
if (QByteArray(argv[0]).endsWith(qgetenv("AKONADI_DEBUG_WAIT") + ".exe")) {
while (!IsDebuggerPresent()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
DebugBreak();
}
}
#endif
}
QString AgentBase::parseArguments(int argc, char **argv)
{
Q_UNUSED(argc);
......
......@@ -464,10 +464,12 @@ public:
template <typename T>
static int init(int argc, char **argv)
{
// Disable session management
qunsetenv("SESSION_MANAGER");
QApplication app(argc, argv);
debugAgent(argc, argv);
const QString id = parseArguments(argc, argv);
T *r = new T(id);
......@@ -754,6 +756,8 @@ protected:
void setTemporaryOffline(int makeOnlineInSeconds = 300);
//@cond PRIVATE
static void debugAgent(int argc, char **argv);
AgentBasePrivate *d_ptr;
explicit AgentBase(AgentBasePrivate *d, const QString &id);
friend class ObserverV2;
......
......@@ -179,10 +179,12 @@ public:
template <typename T>
static int init(int argc, char **argv)
{
// Disable session management
qunsetenv("SESSION_MANAGER");
QApplication app(argc, argv);
debugAgent(argc, argv);
const QString id = parseArguments(argc, argv);
T *r = new T(id);
......
......@@ -238,24 +238,27 @@ void ProcessControl::start()
qPrintable(mApplication), qPrintable(mProcess.errorString()));
Q_EMIT unableToStart();
return;
}
#ifdef Q_OS_UNIX
else {
} else {
QString agentDebug = QString::fromLocal8Bit(qgetenv("AKONADI_DEBUG_WAIT"));
pid_t pid = mProcess.pid();
auto pid = mProcess.processId();
if (!agentDebug.isEmpty() && mApplication.contains(agentDebug)) {
qCDebug(AKONADICONTROL_LOG);
qCDebug(AKONADICONTROL_LOG) << "============================================================";
qCDebug(AKONADICONTROL_LOG) << "ProcessControl: Suspending process" << mApplication;
#ifdef Q_OS_UNIX
qCDebug(AKONADICONTROL_LOG) << "'gdb --pid" << pid << "' to debug";
qCDebug(AKONADICONTROL_LOG) << "'kill -SIGCONT" << pid << "' to continue";
kill(pid, SIGSTOP);
#else defined(Q_OS_WIN)
qCDebug(AKONADICONTROL_LOG) << "PID:" << pid;
qCDebug(AKONADICONTROL_LOG) << "Process is waiting for a debugger...";
// the agent process will wait for a debugger to be attached in AgentBase::debugAgent()
#endif
qCDebug(AKONADICONTROL_LOG) << "============================================================";
qCDebug(AKONADICONTROL_LOG);
kill(pid, SIGSTOP);
}
}
#endif
}
void ProcessControl::resetCrashCount()
......
......@@ -201,7 +201,7 @@ void AgentInstanceCreateJobPrivate::doStart()
if (!agentValgrind.isEmpty() && agentType.identifier().contains(agentValgrind)) {
timeout *= 15;
}
#endif
// change the timeout when debugging the agent, because we need time to start the debugger
const QString agentDebugging = QString::fromLocal8Bit(qgetenv("AKONADI_DEBUG_WAIT"));
if (!agentDebugging.isEmpty()) {
......@@ -215,7 +215,6 @@ void AgentInstanceCreateJobPrivate::doStart()
timeout = agentDebuggingTimeout.toInt();
}
}
#endif
safetyTimer->start(timeout);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment