debuggerrunner.cpp 18.4 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
#ifdef Q_OS_WIN
45
#  include "shared/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

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

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
    if (d->m_engine) {
148
        DebuggerToolTipManager::registerEngine(d->m_engine);
149
    } 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
    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);

Kai Koehne's avatar
Kai Koehne committed
212
213
214
215
216
217
218
219
                static bool checked = true;
                if (!checked)
                    break;
                CheckableMessageBox::information(debuggerCore()->mainWindow(),
                                                 tr("Debugger"),
                                                 warningMessage,
                                                 tr("&Show this message again."),
                                                 &checked, QDialogButtonBox::Ok);
220
221
                break;
            }
222
223
224
        }
    }

225
    debuggerCore()->runControlStarted(d->m_engine);
226

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

232
    d->m_engine->startDebugger(this);
233

234
    if (d->m_running)
hjk's avatar
hjk committed
235
        appendMessage(tr("Debugging starts\n"), NormalMessageFormat);
236
237
}

238
void DebuggerRunControl::startFailed()
239
{
hjk's avatar
hjk committed
240
    appendMessage(tr("Debugging has failed\n"), NormalMessageFormat);
241
    d->m_running = false;
242
    emit finished();
243
    d->m_engine->handleStartFailed();
244
245
}

246
void DebuggerRunControl::handleFinished()
247
{
hjk's avatar
hjk committed
248
    appendMessage(tr("Debugging has finished\n"), NormalMessageFormat);
249
250
251
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
252
253
}

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

269
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
270
{
271
    QTC_ASSERT(isRunning(), return true);
272

273
274
275
    if (optionalPrompt && !*optionalPrompt)
        return true;

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

RunControl::StopResult DebuggerRunControl::stop()
{
286
287
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
288
    return AsynchronousStop;
289
290
}

291
void DebuggerRunControl::debuggingFinished()
292
{
293
    d->m_running = false;
294
    emit finished();
295
296
}

297
bool DebuggerRunControl::isRunning() const
298
{
299
    return d->m_running;
300
301
}

302
DebuggerEngine *DebuggerRunControl::engine()
303
{
304
    QTC_CHECK(d->m_engine);
305
    return d->m_engine;
306
307
}

308
309
310
311
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
312

313
314
315
316
317
318
319

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

320
321
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
322
323
{}

324
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
325
{
326
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
327
328
329
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

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

hjk's avatar
hjk committed
342
    Target *target = runConfiguration->target();
343
    Kit *kit = target ? target->kit() : KitManager::defaultKit();
344
345
    if (!fillParameters(&sp, kit, errorMessage))
        return sp;
346
    sp.environment = environment->environment();
347
    sp.workingDirectory = rc->workingDirectory();
348
349
350

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

354
    sp.executable = rc->executable();
355
356
    if (sp.executable.isEmpty())
        return sp;
357

358
359
360
361
362
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();

hjk's avatar
hjk committed
363
364
    if (target) {
        if (const Project *project = target->project()) {
365
            sp.projectSourceDirectory = project->projectDirectory();
hjk's avatar
hjk committed
366
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
367
                sp.projectBuildDirectory = buildConfig->buildDirectory().toString();
368
            sp.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
369
370
371
        }
    }

372
    DebuggerRunConfigurationAspect *debugger
373
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
374
    sp.multiProcess = debugger->useMultiProcess();
375

376
    if (debugger->useCppDebugger())
377
378
        sp.languages |= CppLanguage;

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

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

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

404
405
    sp.startMode = StartInternal;

406
407
408
409
410
411
412
413
414
415
    // 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
416
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
417
{
418
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
419
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
420
421
    if (sp.startMode == NoStartMode)
        return 0;
422
    if (mode == DebugRunModeWithBreakOnMain)
423
        sp.breakOnMain = true;
hjk's avatar
hjk committed
424

425
    return doCreate(sp, runConfiguration, errorMessage);
426
427
}

428
static bool fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
429
430
{
    if (sp.masterEngineType != NoEngineType)
431
        return true;
hjk's avatar
hjk committed
432
433
434

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
435
        return true;
hjk's avatar
hjk committed
436
437
438
    }

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

DebuggerRunControl *DebuggerRunControlFactory::doCreate
464
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
465
{
466
467
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO);
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME);
468

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

482
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
483
484
485
486
487
        return 0;

    return new DebuggerRunControl(rc, sp);
}

488
489
490
491
492
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

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

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

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

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