debuggerrunner.cpp 7.38 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "debuggerrunner.h"
hjk's avatar
hjk committed
31
#include "debuggermanager.h"
con's avatar
con committed
32

hjk's avatar
hjk committed
33
#include <projectexplorer/debugginghelper.h>
con's avatar
con committed
34 35 36
#include <projectexplorer/environment.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
37
#include <projectexplorer/target.h>
38
#include <projectexplorer/buildconfiguration.h>
39
#include <projectexplorer/applicationrunconfiguration.h> // For LocalApplication*
con's avatar
con committed
40

hjk's avatar
hjk committed
41
#include <utils/qtcassert.h>
42
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
43

con's avatar
con committed
44 45 46
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
hjk's avatar
hjk committed
47

48
#include <QtGui/QTextDocument>
con's avatar
con committed
49

50 51
using namespace ProjectExplorer;

hjk's avatar
hjk committed
52
namespace Debugger {
con's avatar
con committed
53 54 55

////////////////////////////////////////////////////////////////////////
//
56
// DebuggerRunControlFactory
con's avatar
con committed
57 58 59
//
////////////////////////////////////////////////////////////////////////

60
// A factory to create DebuggerRunControls
61
DebuggerRunControlFactory::DebuggerRunControlFactory(DebuggerManager *manager)
62
    : m_manager(manager)
con's avatar
con committed
63 64
{}

65
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
con's avatar
con committed
66
{
67
//    return mode == ProjectExplorer::Constants::DEBUGMODE;
68
    return mode == ProjectExplorer::Constants::DEBUGMODE
69
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
con's avatar
con committed
70 71
}

72
QString DebuggerRunControlFactory::displayName() const
con's avatar
con committed
73
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
74
    return tr("Debug");
con's avatar
con committed
75 76
}

77
static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration)
78
{
79
    DebuggerStartParameters sp;
80 81 82 83 84
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);

85 86 87 88 89 90 91 92 93 94
    sp.startMode = StartInternal;
    sp.executable = rc->executable();
    sp.environment = rc->environment().toStringList();
    sp.workingDirectory = rc->workingDirectory();
    sp.processArgs = rc->commandLineArguments();
    sp.toolChainType = rc->toolChainType();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();
    sp.displayName = rc->displayName();
95

96 97
    // Find qtInstallPath.
    QString qmakePath = DebuggingHelperLibrary::findSystemQt(rc->environment());
98 99 100 101 102 103 104 105 106
    if (!qmakePath.isEmpty()) {
        QProcess proc;
        QStringList args;
        args.append(QLatin1String("-query"));
        args.append(QLatin1String("QT_INSTALL_HEADERS"));
        proc.start(qmakePath, args);
        proc.waitForFinished();
        QByteArray ba = proc.readAllStandardOutput().trimmed();
        QFileInfo fi(QString::fromLocal8Bit(ba) + "/..");
107
        sp.qtInstallPath = fi.absoluteFilePath();
108 109
    }
    return sp;
110 111
}

112
RunControl *DebuggerRunControlFactory::create(RunConfiguration *runConfiguration,
113
                                              const QString &mode)
con's avatar
con committed
114
{
hjk's avatar
hjk committed
115
    QTC_ASSERT(mode == ProjectExplorer::Constants::DEBUGMODE, return 0);
116
    DebuggerStartParameters sp = localStartParameters(runConfiguration);
117
    return new DebuggerRunControl(m_manager, sp);
118 119
}

120
RunControl *DebuggerRunControlFactory::create(const DebuggerStartParameters &sp)
121
{
122
    return new DebuggerRunControl(m_manager, sp);
con's avatar
con committed
123 124
}

125
QWidget *DebuggerRunControlFactory::createConfigurationWidget(RunConfiguration *runConfiguration)
con's avatar
con committed
126 127
{
    // NBS TODO: Add GDB-specific configuration widget
128
    Q_UNUSED(runConfiguration)
con's avatar
con committed
129 130 131 132 133 134 135 136 137 138 139 140
    return 0;
}



////////////////////////////////////////////////////////////////////////
//
// DebuggerRunControl
//
////////////////////////////////////////////////////////////////////////

DebuggerRunControl::DebuggerRunControl(DebuggerManager *manager,
141
        const DebuggerStartParameters &startParameters)
142
    : RunControl(0, ProjectExplorer::Constants::DEBUGMODE),
143 144 145 146
      m_startParameters(startParameters),
      m_manager(manager),
      m_running(false)
{
147 148 149 150 151 152 153 154 155 156 157 158 159
    connect(m_manager, SIGNAL(debuggingFinished()),
            this, SLOT(debuggingFinished()),
            Qt::QueuedConnection);
    connect(m_manager, SIGNAL(applicationOutputAvailable(QString, bool)),
            this, SLOT(slotAddToOutputWindowInline(QString, bool)),
            Qt::QueuedConnection);
    connect(m_manager, SIGNAL(messageAvailable(QString, bool)),
            this, SLOT(slotMessageAvailable(QString, bool)));
    connect(m_manager, SIGNAL(inferiorPidChanged(qint64)),
            this, SLOT(bringApplicationToForeground(qint64)),
            Qt::QueuedConnection);
    connect(this, SIGNAL(stopRequested()),
            m_manager, SLOT(exitDebugger()));
160

161 162 163
    if (m_startParameters.environment.empty())
        m_startParameters.environment = ProjectExplorer::Environment().toStringList();
    m_startParameters.useTerminal = false;
164 165
}

166 167
QString DebuggerRunControl::displayName() const
{
168
    return m_startParameters.displayName;
169 170
}

171 172
void DebuggerRunControl::setCustomEnvironment(ProjectExplorer::Environment env)
{
173
    m_startParameters.environment = env.toStringList();
174 175
}

176 177 178 179
void DebuggerRunControl::init()
{
}

180 181 182
void DebuggerRunControl::start()
{
    m_running = true;
183 184 185
    QString errorMessage;
    QString settingsCategory;
    QString settingsPage;
186
    if (m_manager->checkDebugConfiguration(m_startParameters.toolChainType, &errorMessage,
187
                                           &settingsCategory, &settingsPage)) {
188
        m_manager->startNewDebugger(this);
189
        emit started();
190
    } else {
191
        appendMessage(this, errorMessage, true);
192 193 194 195 196
        emit finished();
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger"), errorMessage,
                                                        QString(),
                                                        settingsCategory, settingsPage);
    }
con's avatar
con committed
197 198
}

199 200
void DebuggerRunControl::slotAddToOutputWindowInline(const QString &data,
                                                     bool onStdErr)
201
{
202 203 204 205 206 207
    emit addToOutputWindowInline(this, data, onStdErr);
}

void DebuggerRunControl::slotMessageAvailable(const QString &data, bool isError)
{
    emit appendMessage(this, data, isError);
con's avatar
con committed
208 209 210 211
}

void DebuggerRunControl::stop()
{
hjk's avatar
hjk committed
212 213
    m_running = false;
    emit stopRequested();
con's avatar
con committed
214 215 216 217 218 219 220 221 222 223 224 225
}

void DebuggerRunControl::debuggingFinished()
{
    m_running = false;
    emit finished();
}

bool DebuggerRunControl::isRunning() const
{
    return m_running;
}
hjk's avatar
hjk committed
226 227

} // namespace Debugger