debuggerrunner.cpp 18.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 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"
43

Friedemann Kleint's avatar
Friedemann Kleint committed
44
45
#ifdef Q_OS_WIN
#  include "peutils.h"
46
#  include <utils/winutils.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
47
48
#endif

49
#include <projectexplorer/localapplicationrunconfiguration.h> // For LocalApplication*
50
#include <projectexplorer/environmentaspect.h> // For the environment
hjk's avatar
hjk committed
51
#include <projectexplorer/buildconfiguration.h>
con's avatar
con committed
52
#include <projectexplorer/project.h>
hjk's avatar
hjk committed
53
#include <projectexplorer/projectexplorer.h>
Tobias Hunger's avatar
Tobias Hunger committed
54
#include <projectexplorer/target.h>
55
#include <projectexplorer/taskhub.h>
con's avatar
con committed
56

hjk's avatar
hjk committed
57
#include <utils/qtcassert.h>
58
#include <utils/qtcprocess.h>
59
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
60

61
#include <QErrorMessage>
62
#include <QTcpServer>
con's avatar
con committed
63

64
using namespace Debugger::Internal;
hjk's avatar
hjk committed
65
66
using namespace ProjectExplorer;
using namespace Utils;
67

68
69
enum { debug = 0 };

70
namespace Debugger {
71
namespace Internal {
72

73
74
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp, QString *error);
DebuggerEngine *createGdbEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
75
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &sp);
76
77
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &sp, QString *error);
78
DebuggerEngine *createLldbLibEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
79
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &sp);
80

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

106
107
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
108
// DebuggerRunControlPrivate
109
110
111
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
112
113
114
class DebuggerRunControlPrivate
{
public:
115
116
    explicit DebuggerRunControlPrivate(DebuggerRunControl *parent,
                                       RunConfiguration *runConfiguration);
117

hjk's avatar
hjk committed
118
119
public:
    DebuggerRunControl *q;
120
    DebuggerEngine *m_engine;
121
    const QPointer<RunConfiguration> m_myRunConfiguration;
122
123
124
    bool m_running;
};

hjk's avatar
hjk committed
125
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
126
                                                     RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
127
128
    : q(parent)
    , m_engine(0)
129
    , m_myRunConfiguration(runConfiguration)
130
    , m_running(false)
131
132
{
}
133

hjk's avatar
hjk committed
134
135
} // namespace Internal

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

147
148
149
    if (d->m_engine) {
        DebuggerToolTipManager::instance()->registerEngine(d->m_engine);
    } else {
150
        debuggingFinished();
hjk's avatar
hjk committed
151
        Core::ICore::showWarningWithOptions(DebuggerRunControl::tr("Debugger"), errorMessage);
152
    }
153
154
}

hjk's avatar
hjk committed
155
156
157
158
159
160
161
162
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
hjk's avatar
hjk committed
163
    delete d;
hjk's avatar
hjk committed
164
165
166
167
168
169
170
171
}

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

172
QString DebuggerRunControl::displayName() const
173
{
174
175
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
176
177
}

178
179
QIcon DebuggerRunControl::icon() const
{
180
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
181
182
}

hjk's avatar
hjk committed
183
void DebuggerRunControl::setCustomEnvironment(Environment env)
184
{
185
    QTC_ASSERT(d->m_engine, return);
186
    d->m_engine->startParameters().environment = env;
187
188
}

189
void DebuggerRunControl::start()
190
{
191
    QTC_ASSERT(d->m_engine, return);
192
193
194
    // 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()) {
hjk's avatar
hjk committed
195
        appendMessage(tr("No executable specified.\n"), ErrorMessageFormat);
196
197
198
199
200
        emit started();
        emit finished();
        return;
    }

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    if (d->m_engine->startParameters().startMode == StartInternal) {
        foreach (const BreakpointModelId &id, debuggerCore()->breakHandler()->allBreakpointIds()) {
            if (d->m_engine->breakHandler()->breakpointData(id).enabled
                    && !d->m_engine->acceptsBreakpoint(id)) {

                QString warningMessage =
                        DebuggerPlugin::tr("Some breakpoints cannot be handled by the debugger "
                                           "languages currently active, and will be ignored.");

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

                QErrorMessage *msgBox = new QErrorMessage(debuggerCore()->mainWindow());
                msgBox->setAttribute(Qt::WA_DeleteOnClose);
                msgBox->showMessage(warningMessage);
                break;
            }
217
218
219
        }
    }

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

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

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

229
    if (d->m_running)
hjk's avatar
hjk committed
230
        appendMessage(tr("Debugging starts\n"), NormalMessageFormat);
231
232
}

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

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

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

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

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

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

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

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

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

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

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

308
309
310
311
312
313
314

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

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

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

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

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

#if defined(Q_OS_WIN)
    // Work around QTBUG-17529 (QtDeclarative fails with 'File name case mismatch' ...)
hjk's avatar
hjk committed
346
    sp.workingDirectory = normalizePathName(sp.workingDirectory);
347
348
#endif

349
    sp.executable = rc->executable();
350
351
    if (sp.executable.isEmpty())
        return sp;
352

353
354
355
356
357
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();

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

367
    DebuggerRunConfigurationAspect *debugger
368
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
369
    sp.multiProcess = debugger->useMultiProcess();
370

371
    if (debugger->useCppDebugger())
372
373
        sp.languages |= CppLanguage;

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

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

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

399
400
    sp.startMode = StartInternal;

401
402
403
404
405
406
407
408
409
410
    // FIXME: If it's not yet build this will be empty and not filled
    // when rebuild as the runConfiguration is not stored and therefore
    // cannot be used to retrieve the dumper location.
    //qDebug() << "DUMPER: " << sp.dumperLibrary << sp.dumperLibraryLocations;
    sp.displayName = rc->displayName();

    return sp;
}

RunControl *DebuggerRunControlFactory::create
411
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
412
{
413
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
414
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
415
416
    if (sp.startMode == NoStartMode)
        return 0;
417
    if (mode == DebugRunModeWithBreakOnMain)
418
        sp.breakOnMain = true;
hjk's avatar
hjk committed
419

420
    return doCreate(sp, runConfiguration, errorMessage);
421
422
}

423
static bool fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
424
425
{
    if (sp.masterEngineType != NoEngineType)
426
        return true;
hjk's avatar
hjk committed
427
428
429

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
430
        return true;
hjk's avatar
hjk committed
431
432
433
    }

    if (rc) {
434
        DebuggerRunConfigurationAspect *aspect
435
                = rc->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
436
        if (const Target *target = rc->target())
437
438
            if (!fillParameters(&sp, target->kit(), errorMessage))
                return false;
439
440
        const bool useCppDebugger = aspect->useCppDebugger() && (sp.languages & CppLanguage);
        const bool useQmlDebugger = aspect->useQmlDebugger() && (sp.languages & QmlLanguage);
hjk's avatar
hjk committed
441
442
443
        if (useQmlDebugger) {
            if (useCppDebugger) {
                sp.masterEngineType = QmlCppEngineType;
444
                sp.firstSlaveEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
445
446
447
448
449
                sp.secondSlaveEngineType = QmlCppEngineType;
            } else {
                sp.masterEngineType = QmlEngineType;
            }
        } else {
450
            sp.masterEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
451
        }
452
        return true;
hjk's avatar
hjk committed
453
    }
454
455
    sp.masterEngineType = sp.cppEngineType;
    return true;
hjk's avatar
hjk committed
456
457
458
}

DebuggerRunControl *DebuggerRunControlFactory::doCreate
459
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
460
{
hjk's avatar
hjk committed
461
    TaskHub *th = ProjectExplorerPlugin::taskHub();
462
463
464
    th->clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO);
    th->clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_TEST);
    th->clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME);
465

hjk's avatar
hjk committed
466
467
468
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
469
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
470
471
472
473
474
475
476
            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"));
        }
477
478
    }

479
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
480
481
482
483
484
        return 0;

    return new DebuggerRunControl(rc, sp);
}

485
486
487
488
489
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

hjk's avatar
hjk committed
490
491
492
DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
493
    QString errorMessage;
494
495
496
    if (runConfiguration && !runConfiguration->ensureConfigured(&errorMessage))
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);

497
498
499
    DebuggerRunControl *rc = doCreate(sp, runConfiguration, &errorMessage);
    if (!rc) {
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);
hjk's avatar
hjk committed
500
        return 0;
501
    }
hjk's avatar
hjk committed
502
503
504
505
506
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}

507
508
DebuggerEngine *DebuggerRunControlFactory::createEngine(DebuggerEngineType et,
    const DebuggerStartParameters &sp, QString *errorMessage)
509
510
511
{
    switch (et) {
    case GdbEngineType:
512
        return createGdbEngine(sp);
513
    case CdbEngineType:
514
        return createCdbEngine(sp, errorMessage);
515
516
517
    case PdbEngineType:
        return createPdbEngine(sp);
    case QmlEngineType:
518
        return createQmlEngine(sp);
519
520
    case LldbEngineType:
        return createLldbEngine(sp);
521
522
    case LldbLibEngineType:
        return createLldbLibEngine(sp);
hjk's avatar
hjk committed
523
524
    case QmlCppEngineType:
        return createQmlCppEngine(sp, errorMessage);
525
526
    default:
        break;
527
    }
hjk's avatar
hjk committed
528
    *errorMessage = DebuggerPlugin::tr("Unable to create a debugger engine of the type '%1'").
529
                    arg(_(engineTypeName(et)));
530
531
532
    return 0;
}

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