debuggerrunner.cpp 7.44 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 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 32 33
#include "debuggerrunner.h"

#include "debuggermanager.h"

hjk's avatar
hjk committed
34
#include <projectexplorer/debugginghelper.h>
con's avatar
con committed
35 36 37
#include <projectexplorer/environment.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
38
#include <projectexplorer/target.h>
39
#include <projectexplorer/buildconfiguration.h>
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

hjk's avatar
hjk committed
50 51
namespace Debugger {
namespace Internal {
con's avatar
con committed
52 53 54

using ProjectExplorer::RunConfiguration;
using ProjectExplorer::RunControl;
55
using ProjectExplorer::LocalApplicationRunConfiguration;
con's avatar
con committed
56 57 58

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

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

68
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
con's avatar
con committed
69
{
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
RunControl *DebuggerRunControlFactory::create(const DebuggerStartParametersPtr &sp)
80
{
81
    return new DebuggerRunControl(m_manager, sp);
82 83
}

84
RunControl *DebuggerRunControlFactory::create(RunConfiguration *runConfiguration,
85
                                              const QString &mode)
con's avatar
con committed
86
{
hjk's avatar
hjk committed
87
    QTC_ASSERT(mode == ProjectExplorer::Constants::DEBUGMODE, return 0);
88 89
    LocalApplicationRunConfiguration *rc = qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return 0);
90
    return new DebuggerRunControl(m_manager, rc);
con's avatar
con committed
91 92
}

93
QWidget *DebuggerRunControlFactory::configurationWidget(RunConfiguration *runConfiguration)
con's avatar
con committed
94 95
{
    // NBS TODO: Add GDB-specific configuration widget
96
    Q_UNUSED(runConfiguration)
con's avatar
con committed
97 98 99 100 101 102 103 104 105 106 107 108 109
    return 0;
}



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


DebuggerRunControl::DebuggerRunControl(DebuggerManager *manager,
110
       LocalApplicationRunConfiguration *runConfiguration)
hjk's avatar
hjk committed
111
  : RunControl(runConfiguration),
112
    m_startParameters(new DebuggerStartParameters()),
113 114
    m_manager(manager),
    m_running(false)
con's avatar
con committed
115
{
116
    init();
hjk's avatar
hjk committed
117 118 119
    if (!runConfiguration)
        return;

120 121 122 123 124 125
    m_startParameters->startMode = StartInternal;
    m_startParameters->executable = runConfiguration->executable();
    m_startParameters->environment = runConfiguration->environment().toStringList();
    m_startParameters->workingDir = runConfiguration->workingDirectory();
    m_startParameters->processArgs = runConfiguration->commandLineArguments();

hjk's avatar
hjk committed
126 127 128 129 130 131 132 133
    switch (m_startParameters->toolChainType) {
    case ProjectExplorer::ToolChain::UNKNOWN:
    case ProjectExplorer::ToolChain::INVALID:
        m_startParameters->toolChainType = runConfiguration->toolChainType();
        break;
    default:
        break;
    }
134
    if (runConfiguration->target()->project()) {
135
        m_startParameters->buildDir =
Tobias Hunger's avatar
Tobias Hunger committed
136
            runConfiguration->target()->activeBuildConfiguration()->buildDirectory();
137
    }
hjk's avatar
hjk committed
138
    m_startParameters->useTerminal =
139
        runConfiguration->runMode() == LocalApplicationRunConfiguration::Console;
hjk's avatar
hjk committed
140 141 142 143
    m_startParameters->dumperLibrary =
        runConfiguration->dumperLibrary();
    m_startParameters->dumperLibraryLocations =
        runConfiguration->dumperLibraryLocations();
hjk's avatar
hjk committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(
            runConfiguration->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) + "/..");
        m_startParameters->qtInstallPath = fi.absoluteFilePath();
    }

159 160
}

161
DebuggerRunControl::DebuggerRunControl(DebuggerManager *manager, const DebuggerStartParametersPtr &startParameters)
162
    : RunControl(0),
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      m_startParameters(startParameters),
      m_manager(manager),
      m_running(false)
{
    init();

    if (m_startParameters->environment.empty())
        m_startParameters->environment = ProjectExplorer::Environment::Environment().toStringList();
    m_startParameters->useTerminal = false;
}

void DebuggerRunControl::init()
{
    connect(m_manager, SIGNAL(debuggingFinished()),
            this, SLOT(debuggingFinished()),
            Qt::QueuedConnection);
    connect(m_manager, SIGNAL(applicationOutputAvailable(QString)),
            this, SLOT(slotAddToOutputWindowInline(QString)),
            Qt::QueuedConnection);
    connect(m_manager, SIGNAL(inferiorPidChanged(qint64)),
            this, SLOT(bringApplicationToForeground(qint64)),
            Qt::QueuedConnection);
    connect(this, SIGNAL(stopRequested()),
            m_manager, SLOT(exitDebugger()));
}

189 190 191
void DebuggerRunControl::start()
{
    m_running = true;
192 193 194 195 196 197 198 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);
    } else {
        error(this, errorMessage);
        emit finished();
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger"), errorMessage,
                                                        QString(),
                                                        settingsCategory, settingsPage);
    }
con's avatar
con committed
205 206
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
207
void DebuggerRunControl::slotAddToOutputWindowInline(const QString &data)
208
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
209
    emit addToOutputWindowInline(this, data);
con's avatar
con committed
210 211 212 213
}

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

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

bool DebuggerRunControl::isRunning() const
{
    return m_running;
}
hjk's avatar
hjk committed
228 229 230

} // namespace Internal
} // namespace Debugger