debuggerrunner.cpp 7.97 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 31
#include "debuggerrunner.h"

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

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

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

46
#include <QtGui/QTextDocument>
con's avatar
con committed
47

hjk's avatar
hjk committed
48 49
namespace Debugger {
namespace Internal {
con's avatar
con committed
50

51
using ProjectExplorer::BuildConfiguration;
con's avatar
con committed
52 53
using ProjectExplorer::RunConfiguration;
using ProjectExplorer::RunControl;
54
using ProjectExplorer::LocalApplicationRunConfiguration;
con's avatar
con committed
55 56 57

////////////////////////////////////////////////////////////////////////
//
58
// DebuggerRunControlFactory
con's avatar
con committed
59 60 61
//
////////////////////////////////////////////////////////////////////////

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

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

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

79
static DebuggerStartParametersPtr localStartParameters(RunConfiguration *runConfiguration)
80
{
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    DebuggerStartParametersPtr sp(new DebuggerStartParameters());
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);

    sp->startMode = StartInternal;
    sp->executable = rc->executable();
    sp->environment = rc->environment().toStringList();
    sp->workingDirectory = rc->workingDirectory();
    sp->processArgs = rc->commandLineArguments();

    switch (sp->toolChainType) {
    case ProjectExplorer::ToolChain::UNKNOWN:
    case ProjectExplorer::ToolChain::INVALID:
        sp->toolChainType = rc->toolChainType();
        break;
    default:
        break;
    }
    if (rc->target()->project()) {
        BuildConfiguration *bc = rc->target()->activeBuildConfiguration();
        if (bc)
            sp->buildDirectory = bc->buildDirectory();
    }
    sp->useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp->dumperLibrary = rc->dumperLibrary();
    sp->dumperLibraryLocations = rc->dumperLibraryLocations();

    QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(
            rc->environment());
    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) + "/..");
        sp->qtInstallPath = fi.absoluteFilePath();
    }
    return sp;
124 125
}

126
RunControl *DebuggerRunControlFactory::create(RunConfiguration *runConfiguration,
127
                                              const QString &mode)
con's avatar
con committed
128
{
hjk's avatar
hjk committed
129
    QTC_ASSERT(mode == ProjectExplorer::Constants::DEBUGMODE, return 0);
130 131 132 133 134 135 136
    DebuggerStartParametersPtr sp = localStartParameters(runConfiguration);
    return new DebuggerRunControl(m_manager, sp, runConfiguration);
}

RunControl *DebuggerRunControlFactory::create(const DebuggerStartParametersPtr &sp)
{
    return new DebuggerRunControl(m_manager, sp, 0);
con's avatar
con committed
137 138
}

139
QWidget *DebuggerRunControlFactory::createConfigurationWidget(RunConfiguration *runConfiguration)
con's avatar
con committed
140 141
{
    // NBS TODO: Add GDB-specific configuration widget
142
    Q_UNUSED(runConfiguration)
con's avatar
con committed
143 144 145 146 147 148 149 150 151 152 153 154
    return 0;
}



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

DebuggerRunControl::DebuggerRunControl(DebuggerManager *manager,
155 156 157
        const DebuggerStartParametersPtr &startParameters,
        ProjectExplorer::RunConfiguration *runConfiguration)
    : RunControl(runConfiguration, ProjectExplorer::Constants::DEBUGMODE),
158 159 160 161 162 163 164
      m_startParameters(startParameters),
      m_manager(manager),
      m_running(false)
{
    init();

    if (m_startParameters->environment.empty())
Friedemann Kleint's avatar
Friedemann Kleint committed
165
     m_startParameters->environment = ProjectExplorer::Environment().toStringList();
166 167 168
    m_startParameters->useTerminal = false;
}

169 170 171 172 173
QString DebuggerRunControl::displayName() const
{
    return tr("Core file: \"%1\"").arg(m_startParameters->coreFile);
}

174 175 176 177 178
void DebuggerRunControl::setCustomEnvironment(ProjectExplorer::Environment env)
{
    m_startParameters->environment = env.toStringList();
}

179 180 181 182 183
void DebuggerRunControl::init()
{
    connect(m_manager, SIGNAL(debuggingFinished()),
            this, SLOT(debuggingFinished()),
            Qt::QueuedConnection);
184 185
    connect(m_manager, SIGNAL(applicationOutputAvailable(QString, bool)),
            this, SLOT(slotAddToOutputWindowInline(QString, bool)),
186
            Qt::QueuedConnection);
187 188
    connect(m_manager, SIGNAL(messageAvailable(QString, bool)),
            this, SLOT(slotMessageAvailable(QString, bool)));
189 190 191 192 193 194 195
    connect(m_manager, SIGNAL(inferiorPidChanged(qint64)),
            this, SLOT(bringApplicationToForeground(qint64)),
            Qt::QueuedConnection);
    connect(this, SIGNAL(stopRequested()),
            m_manager, SLOT(exitDebugger()));
}

196 197 198
void DebuggerRunControl::start()
{
    m_running = true;
199 200 201 202 203 204
    QString errorMessage;
    QString settingsCategory;
    QString settingsPage;
    if (m_manager->checkDebugConfiguration(startParameters()->toolChainType, &errorMessage,
                                           &settingsCategory, &settingsPage)) {
        m_manager->startNewDebugger(m_startParameters);
205
        emit started();
206
    } else {
207
        appendMessage(this, errorMessage, true);
208 209 210 211 212
        emit finished();
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger"), errorMessage,
                                                        QString(),
                                                        settingsCategory, settingsPage);
    }
con's avatar
con committed
213 214
}

215 216
void DebuggerRunControl::slotAddToOutputWindowInline(const QString &data,
                                                     bool onStdErr)
217
{
218 219 220 221 222 223
    emit addToOutputWindowInline(this, data, onStdErr);
}

void DebuggerRunControl::slotMessageAvailable(const QString &data, bool isError)
{
    emit appendMessage(this, data, isError);
con's avatar
con committed
224 225 226 227
}

void DebuggerRunControl::stop()
{
hjk's avatar
hjk committed
228 229
    m_running = false;
    emit stopRequested();
con's avatar
con committed
230 231 232 233 234 235 236 237 238 239 240 241
}

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

bool DebuggerRunControl::isRunning() const
{
    return m_running;
}
hjk's avatar
hjk committed
242 243 244

} // namespace Internal
} // namespace Debugger