debuggerrunner.cpp 18 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
110
    explicit DebuggerRunControlPrivate(DebuggerRunControl *parent,
                                       RunConfiguration *runConfiguration);
111

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

hjk's avatar
hjk committed
119
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
120
                                                     RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
121
122
    : q(parent)
    , m_engine(0)
123
    , m_myRunConfiguration(runConfiguration)
124
    , m_running(false)
125
126
{
}
127

hjk's avatar
hjk committed
128
129
} // namespace Internal

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

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

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

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

166
QString DebuggerRunControl::displayName() const
167
{
168
169
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
170
171
}

172
173
QIcon DebuggerRunControl::icon() const
{
174
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
175
176
}

hjk's avatar
hjk committed
177
void DebuggerRunControl::setCustomEnvironment(Environment env)
178
{
179
    QTC_ASSERT(d->m_engine, return);
180
    d->m_engine->startParameters().environment = env;
181
182
}

183
void DebuggerRunControl::start()
184
{
185
    QTC_ASSERT(d->m_engine, return);
186
187
188
    // 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()) {
189
        appendMessage(tr("No executable specified.") + QLatin1Char('\n'), ErrorMessageFormat);
190
191
192
193
194
        emit started();
        emit finished();
        return;
    }

195
    if (d->m_engine->startParameters().startMode == StartInternal) {
196
        QStringList unhandledIds;
197
198
        foreach (const BreakpointModelId &id, debuggerCore()->breakHandler()->allBreakpointIds()) {
            if (d->m_engine->breakHandler()->breakpointData(id).enabled
199
200
201
202
203
204
205
206
207
                    && !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(", ")));
208

209
            debuggerCore()->showMessage(warningMessage, LogWarning);
210

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

221
    debuggerCore()->runControlStarted(d->m_engine);
222

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

228
    d->m_engine->startDebugger(this);
229

230
    if (d->m_running)
231
        appendMessage(tr("Debugging starts") + QLatin1Char('\n'), NormalMessageFormat);
232
233
}

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

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

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

265
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
266
{
267
    QTC_ASSERT(isRunning(), return true);
268

269
270
271
    if (optionalPrompt && !*optionalPrompt)
        return true;

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

RunControl::StopResult DebuggerRunControl::stop()
{
282
283
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
284
    return AsynchronousStop;
285
286
}

287
void DebuggerRunControl::debuggingFinished()
288
{
289
    d->m_running = false;
290
    emit finished();
291
292
}

293
bool DebuggerRunControl::isRunning() const
294
{
295
    return d->m_running;
296
297
}

298
DebuggerEngine *DebuggerRunControl::engine()
299
{
300
    QTC_CHECK(d->m_engine);
301
    return d->m_engine;
302
303
}

304
305
306
307
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
308

309
310
311
312
313
314
315

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

316
317
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
318
319
{}

320
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
321
{
322
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
323
324
325
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

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

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

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

347
    sp.executable = rc->executable();
348
349
    if (sp.executable.isEmpty())
        return sp;
350

351
352
353
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;

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

363
    DebuggerRunConfigurationAspect *debugger
364
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
365
    sp.multiProcess = debugger->useMultiProcess();
366

367
    if (debugger->useCppDebugger())
368
369
        sp.languages |= CppLanguage;

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

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

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

395
    sp.startMode = StartInternal;
396
397
398
399
400
401
    sp.displayName = rc->displayName();

    return sp;
}

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

411
    return doCreate(sp, runConfiguration, errorMessage);
412
413
}

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

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
421
        return true;
hjk's avatar
hjk committed
422
423
424
    }

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

DebuggerRunControl *DebuggerRunControlFactory::doCreate
450
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
451
{
452
453
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO);
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME);
454

hjk's avatar
hjk committed
455
456
457
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
458
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
459
460
461
462
463
464
465
            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"));
        }
466
467
    }

468
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
469
470
471
472
473
        return 0;

    return new DebuggerRunControl(rc, sp);
}

474
475
476
477
478
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

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

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

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

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