debuggerlaunchers.cpp 3.46 KB
Newer Older
1
/*
2
    SPDX-FileCopyrightText: 2009 George Kiagiadakis <gkiagia@users.sourceforge.net>
3

4
    SPDX-License-Identifier: GPL-2.0-or-later
5
6
7
*/
#include "debuggerlaunchers.h"

8
#include <QCoreApplication>
9
#include <QDBusConnection>
10
11
12

#include <KShell>
#include <KProcess>
Laurent Montel's avatar
Laurent Montel committed
13
#include "drkonqi_debug.h"
14
15
16
17
18

#include "detachedprocessmonitor.h"
#include "drkonqi.h"
#include "crashedapplication.h"

19
20
#include "ptracer.h"

21
22
23
24
DefaultDebuggerLauncher::DefaultDebuggerLauncher(const Debugger & debugger, DebuggerManager *parent)
    : AbstractDebuggerLauncher(parent), m_debugger(debugger)
{
    m_monitor = new DetachedProcessMonitor(this);
25
    connect(m_monitor, &DetachedProcessMonitor::processFinished, this, &DefaultDebuggerLauncher::onProcessFinished);
26
27
28
29
}

QString DefaultDebuggerLauncher::name() const
{
30
    return m_debugger.displayName();
31
32
33
34
}

void DefaultDebuggerLauncher::start()
{
35
    if ( static_cast<DebuggerManager*>(parent())->debuggerIsRunning() ) {
Laurent Montel's avatar
Laurent Montel committed
36
        qCWarning(DRKONQI_LOG) << "Another debugger is already running";
37
38
39
40
41
42
43
44
45
        return;
    }

    QString str = m_debugger.command();
    Debugger::expandString(str, Debugger::ExpansionUsageShell);

    emit starting();
    int pid = KProcess::startDetached(KShell::splitArgs(str));
    if ( pid > 0 ) {
46
        setPtracer(pid, DrKonqi::pid());
47
48
        m_monitor->startMonitoring(pid);
    } else {
Laurent Montel's avatar
Laurent Montel committed
49
        qCWarning(DRKONQI_LOG) << "Could not start debugger:" << name();
50
51
52
53
54
55
        emit finished();
    }
}

void DefaultDebuggerLauncher::onProcessFinished()
{
56
    setPtracer(QCoreApplication::applicationPid(), DrKonqi::pid());
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    emit finished();
}

#if 0
TerminalDebuggerLauncher::TerminalDebuggerLauncher(const Debugger & debugger, DebuggerManager *parent)
    : DefaultDebuggerLauncher(debugger, parent)
{
}

void TerminalDebuggerLauncher::start()
{
    DefaultDebuggerLauncher::start(); //FIXME
}
#endif


73
74
DBusInterfaceLauncher::DBusInterfaceLauncher(const QString &name, qint64 pid, DBusInterfaceAdaptor *parent)
    : AbstractDebuggerLauncher(parent), m_name(name), m_pid(pid)
75
76
77
{
}

78
QString DBusInterfaceLauncher::name() const
79
80
81
82
{
    return m_name;
}

83
void DBusInterfaceLauncher::start()
84
85
{
    emit starting();
86
87
88

    setPtracer(m_pid, DrKonqi::pid());

89
    emit static_cast<DBusInterfaceAdaptor*>(parent())->acceptDebuggingApplication(m_name);
90
91
92
}


93
DBusInterfaceAdaptor::DBusInterfaceAdaptor(DebuggerManager *parent)
94
95
96
    : QDBusAbstractAdaptor(parent)
{
    Q_ASSERT(parent);
97

98
    if (QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.drkonqi.PID%1").arg(pid()))) {
99
100
        QDBusConnection::sessionBus().registerObject(QStringLiteral("/debugger"), parent);
    }
101
102
}

103
int DBusInterfaceAdaptor::pid()
104
105
106
107
{
    return DrKonqi::crashedApplication()->pid();
}

108
void DBusInterfaceAdaptor::registerDebuggingApplication(const QString &name, qint64 pid)
109
{
110
    if (!name.isEmpty() && !m_launchers.contains(name)) {
111
        auto launcher = new DBusInterfaceLauncher(name, pid, this);
112
113
        m_launchers.insert(name, launcher);
        static_cast<DebuggerManager*>(parent())->addDebugger(launcher, true);
114
115
116
    }
}

117
118
119
120
void DBusInterfaceAdaptor::debuggingFinished(const QString &name)
{
    auto it = m_launchers.find(name);
    if (it != m_launchers.end()) {
121
        setPtracer(QCoreApplication::applicationPid(), DrKonqi::pid());
122
123
124
        emit it.value()->finished();
    }
}
125

126
127
128
129
130
void DBusInterfaceAdaptor::debuggerClosed(const QString &name)
{
    auto it = m_launchers.find(name);
    if (it != m_launchers.end()) {
        emit it.value()->invalidated();
131
        m_launchers.erase(it);
132
133
    }
}