debuggerrunner.cpp 17.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

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

33
#include "debuggeractions.h"
34
#include "debuggercore.h"
35
36
#include "debuggerengine.h"
#include "debuggerkitinformation.h"
37
#include "debuggerplugin.h"
38
#include "debuggerrunconfigurationaspect.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
39
#include "debuggerstartparameters.h"
40
#include "debuggerstringutils.h"
41
#include "debuggertooltipmanager.h"
42
#include "breakhandler.h"
hjk's avatar
hjk committed
43
#include "shared/peutils.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
44

45
#include <projectexplorer/localapplicationrunconfiguration.h> // For LocalApplication*
46
#include <projectexplorer/environmentaspect.h> // For the environment
hjk's avatar
hjk committed
47
#include <projectexplorer/buildconfiguration.h>
con's avatar
con committed
48
#include <projectexplorer/project.h>
hjk's avatar
hjk committed
49
#include <projectexplorer/projectexplorer.h>
Tobias Hunger's avatar
Tobias Hunger committed
50
#include <projectexplorer/target.h>
51
#include <projectexplorer/taskhub.h>
con's avatar
con committed
52

Kai Koehne's avatar
Kai Koehne committed
53
#include <utils/checkablemessagebox.h>
Orgad Shaneh's avatar
Orgad Shaneh committed
54
#include <utils/fileutils.h>
hjk's avatar
hjk committed
55
#include <utils/qtcassert.h>
56
#include <utils/qtcprocess.h>
57
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
58

59
#include <QTcpServer>
con's avatar
con committed
60

61
using namespace Debugger::Internal;
hjk's avatar
hjk committed
62
63
using namespace ProjectExplorer;
using namespace Utils;
64

65
66
enum { debug = 0 };

67
namespace Debugger {
68
namespace Internal {
69

70
71
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp, QString *error);
DebuggerEngine *createGdbEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
72
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &sp);
73
74
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &sp, QString *error);
hjk's avatar
hjk committed
75
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &sp);
76

77
78
79
80
81
82
static const char *engineTypeName(DebuggerEngineType et)
{
    switch (et) {
    case Debugger::NoEngineType:
        break;
    case Debugger::GdbEngineType:
83
        return "Gdb engine";
84
85
86
    case Debugger::CdbEngineType:
        return "Cdb engine";
    case Debugger::PdbEngineType:
87
        return "Pdb engine";
88
89
90
91
92
    case Debugger::QmlEngineType:
        return "QML engine";
    case Debugger::QmlCppEngineType:
        return "QML C++ engine";
    case Debugger::LldbEngineType:
93
        return "LLDB command line engine";
94
95
96
97
98
99
    case Debugger::AllEngineTypes:
        break;
    }
    return "No engine";
}

100
101
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
102
// DebuggerRunControlPrivate
103
104
105
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
106
107
108
class DebuggerRunControlPrivate
{
public:
109
    explicit DebuggerRunControlPrivate(RunConfiguration *runConfiguration);
110

hjk's avatar
hjk committed
111
public:
112
    DebuggerEngine *m_engine;
113
    const QPointer<RunConfiguration> m_myRunConfiguration;
114
115
116
    bool m_running;
};

117
118
DebuggerRunControlPrivate::DebuggerRunControlPrivate(RunConfiguration *runConfiguration)
    : m_engine(0)
119
    , m_myRunConfiguration(runConfiguration)
120
    , m_running(false)
121
122
{
}
123

hjk's avatar
hjk committed
124
125
} // namespace Internal

hjk's avatar
hjk committed
126
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
hjk's avatar
hjk committed
127
                                       const DebuggerStartParameters &sp)
hjk's avatar
hjk committed
128
    : RunControl(runConfiguration, DebugRunMode),
129
      d(new DebuggerRunControlPrivate(runConfiguration))
130
{
hjk's avatar
hjk committed
131
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
132
133
134
    // Create the engine. Could arguably be moved to the factory, but
    // we still have a derived S60DebugControl. Should rarely fail, though.
    QString errorMessage;
135
    d->m_engine = DebuggerRunControlFactory::createEngine(sp.masterEngineType, sp, &errorMessage);
hjk's avatar
hjk committed
136

137
    if (d->m_engine) {
138
        DebuggerToolTipManager::registerEngine(d->m_engine);
139
    } else {
140
        debuggingFinished();
hjk's avatar
hjk committed
141
        Core::ICore::showWarningWithOptions(DebuggerRunControl::tr("Debugger"), errorMessage);
142
    }
143
144
}

hjk's avatar
hjk committed
145
146
147
148
149
150
151
152
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
hjk's avatar
hjk committed
153
    delete d;
hjk's avatar
hjk committed
154
155
156
157
158
159
160
161
}

const DebuggerStartParameters &DebuggerRunControl::startParameters() const
{
    QTC_ASSERT(d->m_engine, return *(new DebuggerStartParameters()));
    return d->m_engine->startParameters();
}

162
QString DebuggerRunControl::displayName() const
163
{
164
165
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
166
167
}

168
169
QIcon DebuggerRunControl::icon() const
{
170
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
171
172
}

hjk's avatar
hjk committed
173
void DebuggerRunControl::setCustomEnvironment(Environment env)
174
{
175
    QTC_ASSERT(d->m_engine, return);
176
    d->m_engine->startParameters().environment = env;
177
178
}

179
void DebuggerRunControl::start()
180
{
181
    QTC_ASSERT(d->m_engine, return);
182
183
184
    // User canceled input dialog asking for executable when working on library project.
    if (d->m_engine->startParameters().startMode == StartInternal
        && d->m_engine->startParameters().executable.isEmpty()) {
185
        appendMessage(tr("No executable specified.") + QLatin1Char('\n'), ErrorMessageFormat);
186
187
188
189
190
        emit started();
        emit finished();
        return;
    }

191
    if (d->m_engine->startParameters().startMode == StartInternal) {
192
        QStringList unhandledIds;
193
194
        foreach (const BreakpointModelId &id, debuggerCore()->breakHandler()->allBreakpointIds()) {
            if (d->m_engine->breakHandler()->breakpointData(id).enabled
195
196
197
198
199
200
201
202
203
                    && !d->m_engine->acceptsBreakpoint(id))
                unhandledIds.append(id.toString());
        }
        if (!unhandledIds.isEmpty()) {
            QString warningMessage =
                    DebuggerPlugin::tr("Some breakpoints cannot be handled by the debugger "
                                       "languages currently active, and will be ignored.\n"
                                       "Affected are breakpoints %1")
                    .arg(unhandledIds.join(QLatin1String(", ")));
204

205
            debuggerCore()->showMessage(warningMessage, LogWarning);
206

207
208
            static bool checked = true;
            if (checked)
209
                CheckableMessageBox::information(Core::ICore::mainWindow(),
Kai Koehne's avatar
Kai Koehne committed
210
211
212
213
                                                 tr("Debugger"),
                                                 warningMessage,
                                                 tr("&Show this message again."),
                                                 &checked, QDialogButtonBox::Ok);
214
215
216
        }
    }

217
    debuggerCore()->runControlStarted(d->m_engine);
218

219
220
    // We might get a synchronous startFailed() notification on Windows,
    // when launching the process fails. Emit a proper finished() sequence.
221
    emit started();
222
223
    d->m_running = true;

224
    d->m_engine->startDebugger(this);
225

226
    if (d->m_running)
227
        appendMessage(tr("Debugging starts") + QLatin1Char('\n'), NormalMessageFormat);
228
229
}

230
void DebuggerRunControl::startFailed()
231
{
232
    appendMessage(tr("Debugging has failed") + QLatin1Char('\n'), NormalMessageFormat);
233
    d->m_running = false;
234
    emit finished();
235
    d->m_engine->handleStartFailed();
236
237
}

238
void DebuggerRunControl::handleFinished()
239
{
240
    appendMessage(tr("Debugging has finished") + QLatin1Char('\n'), NormalMessageFormat);
241
242
243
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
244
245
}

246
void DebuggerRunControl::showMessage(const QString &msg, int channel)
247
{
248
249
    switch (channel) {
        case AppOutput:
hjk's avatar
hjk committed
250
            appendMessage(msg, StdOutFormatSameLine);
251
252
            break;
        case AppError:
hjk's avatar
hjk committed
253
            appendMessage(msg, StdErrFormatSameLine);
254
255
            break;
        case AppStuff:
hjk's avatar
hjk committed
256
            appendMessage(msg, DebugFormat);
257
258
            break;
    }
259
260
}

261
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
262
{
263
    QTC_ASSERT(isRunning(), return true);
264

265
266
267
    if (optionalPrompt && !*optionalPrompt)
        return true;

Jarek Kobus's avatar
Jarek Kobus committed
268
    const QString question = tr("A debugging session is still in progress. "
269
270
271
            "Terminating the session in the current"
            " state can leave the target in an inconsistent state."
            " Would you still like to terminate it?");
272
273
    return showPromptToStopDialog(tr("Close Debugging Session"), question,
                                  QString(), QString(), optionalPrompt);
274
275
276
277
}

RunControl::StopResult DebuggerRunControl::stop()
{
278
279
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
280
    return AsynchronousStop;
281
282
}

283
void DebuggerRunControl::debuggingFinished()
284
{
285
    d->m_running = false;
286
    emit finished();
287
288
}

289
bool DebuggerRunControl::isRunning() const
290
{
291
    return d->m_running;
292
293
}

294
DebuggerEngine *DebuggerRunControl::engine()
295
{
296
    QTC_CHECK(d->m_engine);
297
    return d->m_engine;
298
299
}

300
301
302
303
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
304

305
306
307
308
309
310
311

////////////////////////////////////////////////////////////////////////
//
// DebuggerRunControlFactory
//
////////////////////////////////////////////////////////////////////////

312
313
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
314
315
{}

316
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
317
{
318
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
319
320
321
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

322
static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration, QString *errorMessage)
323
324
325
326
327
328
{
    DebuggerStartParameters sp;
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);
329
330
    EnvironmentAspect *environment = rc->extraAspect<ProjectExplorer::EnvironmentAspect>();
    QTC_ASSERT(environment, return sp);
331
332
    if (!rc->ensureConfigured(errorMessage))
        return sp;
333

hjk's avatar
hjk committed
334
    Target *target = runConfiguration->target();
335
    Kit *kit = target ? target->kit() : KitManager::defaultKit();
336
337
    if (!fillParameters(&sp, kit, errorMessage))
        return sp;
338
    sp.environment = environment->environment();
339

Orgad Shaneh's avatar
Orgad Shaneh committed
340
341
    // Normalize to work around QTBUG-17529 (QtDeclarative fails with 'File name case mismatch'...)
    sp.workingDirectory = FileUtils::normalizePathName(rc->workingDirectory());
342

343
    sp.executable = rc->executable();
344
345
    if (sp.executable.isEmpty())
        return sp;
346

347
348
349
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;

hjk's avatar
hjk committed
350
351
    if (target) {
        if (const Project *project = target->project()) {
352
            sp.projectSourceDirectory = project->projectDirectory().toString();
hjk's avatar
hjk committed
353
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
354
                sp.projectBuildDirectory = buildConfig->buildDirectory().toString();
355
            sp.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
356
357
358
        }
    }

359
    DebuggerRunConfigurationAspect *debugger
360
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
361
    sp.multiProcess = debugger->useMultiProcess();
362

363
    if (debugger->useCppDebugger())
364
365
        sp.languages |= CppLanguage;

366
    if (debugger->useQmlDebugger()) {
367
368
369
        const ProjectExplorer::IDevice::ConstPtr device =
                DeviceKitInformation::device(runConfiguration->target()->kit());
        QTC_ASSERT(device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE, return sp);
370
        QTcpServer server;
371
372
        const bool canListen = server.listen(QHostAddress::LocalHost)
                || server.listen(QHostAddress::LocalHostIPv6);
373
        if (!canListen) {
374
            if (errorMessage)
375
                *errorMessage = DebuggerPlugin::tr("Not enough free ports for QML debugging.") + QLatin1Char(' ');
376
377
            return sp;
        }
378
        sp.qmlServerAddress = server.serverAddress().toString();
379
        sp.qmlServerPort = server.serverPort();
380
        sp.languages |= QmlLanguage;
381

382
383
        // Makes sure that all bindings go through the JavaScript engine, so that
        // breakpoints are actually hit!
384
        const QString optimizerKey = _("QML_DISABLE_OPTIMIZER");
hjk's avatar
hjk committed
385
        if (!sp.environment.hasKey(optimizerKey))
386
            sp.environment.set(optimizerKey, _("1"));
387

hjk's avatar
hjk committed
388
        QtcProcess::addArg(&sp.processArgs, QString::fromLatin1("-qmljsdebugger=port:%1,block").arg(sp.qmlServerPort));
389
390
    }

391
    sp.startMode = StartInternal;
392
393
394
395
396
397
    sp.displayName = rc->displayName();

    return sp;
}

RunControl *DebuggerRunControlFactory::create
398
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
399
{
400
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
401
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
402
403
    if (sp.startMode == NoStartMode)
        return 0;
404
    if (mode == DebugRunModeWithBreakOnMain)
405
        sp.breakOnMain = true;
hjk's avatar
hjk committed
406

407
    return doCreate(sp, runConfiguration, errorMessage);
408
409
}

410
static bool fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
411
412
{
    if (sp.masterEngineType != NoEngineType)
413
        return true;
hjk's avatar
hjk committed
414
415
416

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
417
        return true;
hjk's avatar
hjk committed
418
419
420
    }

    if (rc) {
421
        DebuggerRunConfigurationAspect *aspect
422
                = rc->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
423
        if (const Target *target = rc->target())
424
425
            if (!fillParameters(&sp, target->kit(), errorMessage))
                return false;
426
427
        const bool useCppDebugger = aspect->useCppDebugger() && (sp.languages & CppLanguage);
        const bool useQmlDebugger = aspect->useQmlDebugger() && (sp.languages & QmlLanguage);
hjk's avatar
hjk committed
428
429
430
        if (useQmlDebugger) {
            if (useCppDebugger) {
                sp.masterEngineType = QmlCppEngineType;
431
                sp.firstSlaveEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
432
433
434
435
436
                sp.secondSlaveEngineType = QmlCppEngineType;
            } else {
                sp.masterEngineType = QmlEngineType;
            }
        } else {
437
            sp.masterEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
438
        }
439
        return true;
hjk's avatar
hjk committed
440
    }
441
442
    sp.masterEngineType = sp.cppEngineType;
    return true;
hjk's avatar
hjk committed
443
444
445
}

DebuggerRunControl *DebuggerRunControlFactory::doCreate
446
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
447
{
448
449
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO);
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME);
450

hjk's avatar
hjk committed
451
452
453
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
454
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
455
456
457
458
459
460
461
            sp.debugInfoLocation = sysroot + QLatin1String("/usr/lib/debug");
        if (sp.debugSourceLocation.isEmpty()) {
            QString base = sysroot + QLatin1String("/usr/src/debug/");
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/corelib"));
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/gui"));
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/network"));
        }
462
463
    }

464
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
465
466
467
468
469
        return 0;

    return new DebuggerRunControl(rc, sp);
}

470
471
472
473
474
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

hjk's avatar
hjk committed
475
476
477
DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
478
    QString errorMessage;
479
480
481
    if (runConfiguration && !runConfiguration->ensureConfigured(&errorMessage))
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);

482
483
484
    DebuggerRunControl *rc = doCreate(sp, runConfiguration, &errorMessage);
    if (!rc) {
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);
hjk's avatar
hjk committed
485
        return 0;
486
    }
hjk's avatar
hjk committed
487
488
489
490
491
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}

492
493
DebuggerEngine *DebuggerRunControlFactory::createEngine(DebuggerEngineType et,
    const DebuggerStartParameters &sp, QString *errorMessage)
494
495
496
{
    switch (et) {
    case GdbEngineType:
497
        return createGdbEngine(sp);
498
    case CdbEngineType:
499
        return createCdbEngine(sp, errorMessage);
500
501
502
    case PdbEngineType:
        return createPdbEngine(sp);
    case QmlEngineType:
503
        return createQmlEngine(sp);
504
505
    case LldbEngineType:
        return createLldbEngine(sp);
hjk's avatar
hjk committed
506
507
    case QmlCppEngineType:
        return createQmlCppEngine(sp, errorMessage);
508
509
    default:
        break;
510
    }
511
    *errorMessage = DebuggerPlugin::tr("Unable to create a debugger engine of the type \"%1\"").
512
                    arg(_(engineTypeName(et)));
513
514
515
    return 0;
}

hjk's avatar
hjk committed
516
} // namespace Debugger