debuggerengine.cpp 54.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
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
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** 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
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31 32

#include "debuggerengine.h"

33
#include "debuggerinternalconstants.h"
34
#include "debuggeractions.h"
35
#include "debuggercore.h"
36
#include "debuggerruncontrol.h"
37
#include "debuggerstringutils.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
38
#include "debuggerstartparameters.h"
39
#include "debuggertooltipmanager.h"
40 41

#include "breakhandler.h"
David Schulz's avatar
David Schulz committed
42 43
#include "disassembleragent.h"
#include "memoryagent.h"
44 45 46 47 48 49
#include "moduleshandler.h"
#include "registerhandler.h"
#include "sourcefileshandler.h"
#include "stackhandler.h"
#include "threadshandler.h"
#include "watchhandler.h"
50
#include <debugger/shared/peutils.h>
51

hjk's avatar
hjk committed
52
#include <coreplugin/editormanager/editormanager.h>
53
#include <coreplugin/editormanager/ieditor.h>
hjk's avatar
hjk committed
54
#include <coreplugin/icore.h>
55
#include <coreplugin/messagebox.h>
56 57
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
hjk's avatar
hjk committed
58

hjk's avatar
hjk committed
59
#include <projectexplorer/projectexplorer.h>
60 61
#include <projectexplorer/taskhub.h>

62
#include <texteditor/texteditor.h>
63

64
#include <utils/fileinprojectfinder.h>
hjk's avatar
hjk committed
65 66 67
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
68

69
#include <qmljs/consolemanagerinterface.h>
70

71 72 73
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
74
#include <QDir>
75

hjk's avatar
hjk committed
76
using namespace Core;
77
using namespace Debugger::Internal;
hjk's avatar
hjk committed
78 79
using namespace ProjectExplorer;
using namespace TextEditor;
80

81 82 83 84
enum { debug = 0 };

#define SDEBUG(s) if (!debug) {} else qDebug() << s;
#define XSDEBUG(s) qDebug() << s
85

86 87 88 89
//#define WITH_BENCHMARK
#ifdef WITH_BENCHMARK
#include <valgrind/callgrind.h>
#endif
90

91 92 93 94 95 96
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
97 98 99
// VariableManager Prefix
const char PrefixDebugExecutable[]  = "DebuggedExecutable";

100 101
namespace Debugger {

102 103
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
104 105
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
106 107 108 109 110 111 112
}

QDebug operator<<(QDebug str, const DebuggerStartParameters &sp)
{
    QDebug nospace = str.nospace();
    nospace << "executable=" << sp.executable
            << " coreFile=" << sp.coreFile
113
            << " processArgs=" << sp.processArgs
114 115 116 117 118 119
            << " environment=<" << sp.environment.size() << " variables>"
            << " workingDir=" << sp.workingDirectory
            << " attachPID=" << sp.attachPID
            << " useTerminal=" << sp.useTerminal
            << " remoteChannel=" << sp.remoteChannel
            << " serverStartScript=" << sp.serverStartScript
120
            << " abi=" << sp.toolChainAbi.toString() << '\n';
121 122 123
    return str;
}

hjk's avatar
hjk committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
namespace Internal {

Location::Location(const StackFrame &frame, bool marker)
{
    init();
    m_fileName = frame.file;
    m_lineNumber = frame.line;
    m_needsMarker = marker;
    m_functionName = frame.function;
    m_hasDebugInfo = frame.isUsable();
    m_address = frame.address;
    m_from = frame.from;
}

138

139 140 141 142 143 144
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

145 146 147 148 149 150 151 152 153
// transitions:
//   None->Requested
//   Requested->Succeeded
//   Requested->Failed
//   Requested->Cancelled
enum RemoteSetupState { RemoteSetupNone, RemoteSetupRequested,
                        RemoteSetupSucceeded, RemoteSetupFailed,
                        RemoteSetupCancelled };

154
class DebuggerEnginePrivate : public QObject
155
{
156 157
    Q_OBJECT

158
public:
159
    DebuggerEnginePrivate(DebuggerEngine *engine, const DebuggerStartParameters &sp)
160
      : m_engine(engine),
161
        m_masterEngine(0),
162 163 164
        m_runControl(0),
        m_startParameters(sp),
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
165
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
166
        m_targetState(DebuggerNotReady),
167
        m_remoteSetupState(RemoteSetupNone),
168
        m_inferiorPid(0),
169
        m_modulesHandler(engine),
170
        m_registerHandler(),
171 172
        m_sourceFilesHandler(),
        m_stackHandler(),
hjk's avatar
hjk committed
173
        m_threadsHandler(),
174
        m_watchHandler(engine),
175
        m_disassemblerAgent(engine),
176
        m_memoryAgent(engine),
177
        m_isStateDebugging(false)
178
    {
179
        connect(&m_locationTimer, SIGNAL(timeout()), SLOT(resetLocation()));
hjk's avatar
hjk committed
180
        connect(action(IntelFlavor), SIGNAL(valueChanged(QVariant)),
181
                SLOT(reloadDisassembly()));
182

hjk's avatar
hjk committed
183
        Utils::globalMacroExpander()->registerFileVariables(PrefixDebugExecutable,
184
            tr("Debugged executable"),
Orgad Shaneh's avatar
Orgad Shaneh committed
185
            [this]() { return m_startParameters.executable; });
186
    }
187

188
public slots:
189
    void doSetupEngine();
hjk's avatar
hjk committed
190 191
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
192 193
    void doShutdownEngine();
    void doShutdownInferior();
194
    void doInterruptInferior();
hjk's avatar
hjk committed
195
    void doFinishDebugger();
hjk's avatar
hjk committed
196

197 198 199 200 201
    void reloadDisassembly()
    {
        m_disassemblerAgent.reload();
    }

202 203 204 205 206 207 208 209 210 211 212 213 214 215
    void queueSetupEngine()
    {
        m_engine->setState(EngineSetupRequested);
        m_engine->showMessage(_("QUEUE: SETUP ENGINE"));
        QTimer::singleShot(0, this, SLOT(doSetupEngine()));
    }

    void queueSetupInferior()
    {
        m_engine->setState(InferiorSetupRequested);
        m_engine->showMessage(_("QUEUE: SETUP INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doSetupInferior()));
    }

216 217
    void queueRunEngine()
    {
hjk's avatar
hjk committed
218 219 220 221 222
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

223 224
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
225 226 227 228 229
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

230 231
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
232 233 234 235 236
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

237 238
    void queueFinishDebugger()
    {
239 240 241
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
242 243 244 245 246
        resetLocation();
        if (isMasterEngine()) {
            m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
            QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
        }
hjk's avatar
hjk committed
247 248
    }

249 250
    void raiseApplication()
    {
251 252
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
253 254
    }

255
    void scheduleResetLocation()
256
    {
257
        m_stackHandler.scheduleResetLocation();
258
        m_watchHandler.scheduleResetLocation();
259
        m_threadsHandler.scheduleResetLocation();
260
        m_disassemblerAgent.scheduleResetLocation();
261 262 263 264
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

265
    void resetLocation()
266 267 268
    {
        m_locationTimer.stop();
        m_locationMark.reset();
269
        m_stackHandler.resetLocation();
270
        m_watchHandler.resetLocation();
271
        m_threadsHandler.resetLocation();
272
        m_disassemblerAgent.resetLocation();
hjk's avatar
hjk committed
273
        DebuggerToolTipManager::resetLocation();
274 275
    }

276
public:
hjk's avatar
hjk committed
277
    DebuggerState state() const { return m_state; }
278
    RemoteSetupState remoteSetupState() const { return m_remoteSetupState; }
279 280 281
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
282
    void setRemoteSetupState(RemoteSetupState state);
hjk's avatar
hjk committed
283

284
    DebuggerEngine *m_engine; // Not owned.
285
    DebuggerEngine *m_masterEngine; // Not owned
286 287 288
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
hjk's avatar
hjk committed
289 290

    // The current state.
291 292
    DebuggerState m_state;

hjk's avatar
hjk committed
293 294 295 296 297 298
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

    // The state we are aiming for.
    DebuggerState m_targetState;

299 300 301
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

302 303 304 305 306 307 308 309
    qint64 m_inferiorPid;

    ModulesHandler m_modulesHandler;
    RegisterHandler m_registerHandler;
    SourceFilesHandler m_sourceFilesHandler;
    StackHandler m_stackHandler;
    ThreadsHandler m_threadsHandler;
    WatchHandler m_watchHandler;
310
    QFutureInterface<void> m_progress;
311

312 313
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
hjk's avatar
hjk committed
314
    QScopedPointer<TextEditor::TextMark> m_locationMark;
315
    QTimer m_locationTimer;
316 317

    bool m_isStateDebugging;
318

319
    Utils::FileInProjectFinder m_fileFinder;
320 321 322 323 324 325 326 327 328
};


//////////////////////////////////////////////////////////////////////
//
// DebuggerEngine
//
//////////////////////////////////////////////////////////////////////

329 330
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters)
  : d(new DebuggerEnginePrivate(this, startParameters))
331
{}
332 333 334

DebuggerEngine::~DebuggerEngine()
{
335 336
    disconnect();
    delete d;
337 338
}

339 340 341 342 343 344 345 346 347 348 349
const char *DebuggerEngine::stateName(int s)
{
#    define SN(x) case x: return #x;
    switch (s) {
        SN(DebuggerNotReady)
        SN(EngineSetupRequested)
        SN(EngineSetupOk)
        SN(EngineSetupFailed)
        SN(EngineRunFailed)
        SN(InferiorSetupRequested)
        SN(InferiorSetupFailed)
350
        SN(InferiorSetupOk)
351 352 353 354 355 356 357 358
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
359
        SN(InferiorExitOk)
360 361 362 363 364 365 366 367 368 369 370 371
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

372
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
373
{
374
    showMessage(msg, StatusBar, timeout);
375
}
376

377 378 379 380 381 382 383 384 385 386 387 388
void DebuggerEngine::frameUp()
{
    int currentIndex = stackHandler()->currentIndex();
    activateFrame(qMin(currentIndex + 1, stackHandler()->stackSize() - 1));
}

void DebuggerEngine::frameDown()
{
    int currentIndex = stackHandler()->currentIndex();
    activateFrame(qMax(currentIndex - 1, 0));
}

389 390 391 392 393
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

394 395
ModulesHandler *DebuggerEngine::modulesHandler() const
{
396 397 398
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
399 400 401 402
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
403 404 405
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
406 407 408 409
}

StackHandler *DebuggerEngine::stackHandler() const
{
Aurindam Jana's avatar
Aurindam Jana committed
410 411 412
    return d->m_masterEngine
        ? d->m_masterEngine->stackHandler()
        : &d->m_stackHandler;
413 414 415 416
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
417 418 419
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
420 421 422 423
}

WatchHandler *DebuggerEngine::watchHandler() const
{
424 425 426
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
427 428 429 430
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
431 432 433
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
434 435 436 437
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
438
   return modulesHandler()->model();
439 440 441 442
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
443
    return registerHandler()->model();
444 445 446 447
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
448
    return stackHandler()->model();
449 450 451 452
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
453
    return threadsHandler()->model();
454 455
}

hjk's avatar
hjk committed
456 457 458
QAbstractItemModel *DebuggerEngine::watchModel() const
{
    return watchHandler()->model();
459 460
}

461 462
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
463
    return sourceFilesHandler()->model();
464 465
}

466
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
467 468 469 470 471 472
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
473 474 475 476 477 478 479
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

hjk's avatar
hjk committed
480
void DebuggerEngine::setRegisterValue(const QByteArray &name, const QString &value)
481
{
hjk's avatar
hjk committed
482
    Q_UNUSED(name);
483 484 485 486 487
    Q_UNUSED(value);
}

void DebuggerEngine::showMessage(const QString &msg, int channel, int timeout) const
{
488 489 490 491
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
492 493
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
494
    QmlJS::ConsoleManagerInterface *consoleManager = QmlJS::ConsoleManagerInterface::instance();
495
    if (channel == ConsoleOutput && consoleManager)
496
        consoleManager->printToConsolePane(QmlJS::ConsoleItem::UndefinedType, msg);
497

498
    Internal::showMessage(msg, channel, timeout);
499 500 501 502 503 504 505 506 507 508 509 510 511
    if (d->m_runControl) {
        switch (channel) {
            case AppOutput:
                d->m_runControl->appendMessage(msg, Utils::StdOutFormatSameLine);
                break;
            case AppError:
                d->m_runControl->appendMessage(msg, Utils::StdErrFormatSameLine);
                break;
            case AppStuff:
                d->m_runControl->appendMessage(msg, Utils::DebugFormat);
                break;
        }
    } else {
512
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
513
    }
514 515 516 517
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
518 519
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
520

521
    d->m_progress.setProgressRange(0, 1000);
522
    FutureProgress *fp = ProgressManager::addTask(d->m_progress.future(),
523
        tr("Launching Debugger"), "Debugger.Launcher");
524
    connect(fp, SIGNAL(canceled()), this, SLOT(quitDebugger()));
525
    fp->setKeepOnFinish(FutureProgress::HideOnFinish);
526 527
    d->m_progress.reportStarted();

528 529 530 531
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;
532 533
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
534

535 536
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
537

hjk's avatar
hjk committed
538
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
539

540 541
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
542 543
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
544
    d->m_progress.setProgressValue(200);
545
    d->queueSetupEngine();
546 547 548 549
}

void DebuggerEngine::resetLocation()
{
550 551
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
552 553
}

554
void DebuggerEngine::gotoLocation(const Location &loc)
555
{
Aurindam Jana's avatar
Aurindam Jana committed
556 557
     d->resetLocation();

Aurindam Jana's avatar
Aurindam Jana committed
558
    if ((hasCapability(OperateByInstructionCapability) &&
hjk's avatar
hjk committed
559
            boolSetting(OperateByInstruction)) || !loc.hasDebugInfo()) {
560 561 562
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
563 564 565 566 567
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;


568
    const QString file = QDir::cleanPath(loc.fileName());
569
    const int line = loc.lineNumber();
570
    bool newEditor = false;
571
    IEditor *editor = EditorManager::openEditor(file, Id(),
572 573
                                                EditorManager::IgnoreNavigationHistory, &newEditor);
    QTC_ASSERT(editor, return); // Unreadable file?
574

hjk's avatar
hjk committed
575
    editor->gotoLine(line, 0, !boolSetting(StationaryEditorWhileStepping));
576

577 578
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
579

580
    if (loc.needsMarker()) {
hjk's avatar
hjk committed
581
        d->m_locationMark.reset(new TextEditor::TextMark(file, line));
582
        d->m_locationMark->setIcon(Internal::locationMarkIcon());
hjk's avatar
hjk committed
583
        d->m_locationMark->setPriority(TextEditor::TextMark::HighPriority);
584
    }
585 586

    //qDebug() << "MEMORY: " << d->m_memoryAgent.hasVisibleEditor();
587 588
}

589 590 591
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
592
    showMessage(QLatin1String("HANDLE RUNCONTROL START FAILED"));
593
    d->m_runControl = 0;
594
    d->m_progress.setProgressValue(900);
595 596
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
597 598
}

599 600
// Called from RunControl.
void DebuggerEngine::handleFinished()
601
{
602
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
603
    d->m_runControl = 0;
604 605
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    modulesHandler()->removeAll();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    watchHandler()->cleanup();
}

const DebuggerStartParameters &DebuggerEngine::startParameters() const
{
    return d->m_startParameters;
}

DebuggerStartParameters &DebuggerEngine::startParameters()
{
    return d->m_startParameters;
}

DebuggerState DebuggerEngine::state() const
{
    return d->m_state;
}

hjk's avatar
hjk committed
627
DebuggerState DebuggerEngine::lastGoodState() const
628
{
hjk's avatar
hjk committed
629 630 631 632 633 634 635
    return d->m_lastGoodState;
}

DebuggerState DebuggerEngine::targetState() const
{
    return d->m_targetState;
}
636

hjk's avatar
hjk committed
637 638 639
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
640
    case DebuggerNotReady:
641
        return to == EngineSetupRequested;
642

hjk's avatar
hjk committed
643
    case EngineSetupRequested:
644 645
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
646 647 648
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
649
    case EngineSetupOk:
hjk's avatar
hjk committed
650
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
651

hjk's avatar
hjk committed
652
    case InferiorSetupRequested:
653
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
654
    case InferiorSetupFailed:
hjk's avatar
hjk committed
655
        return to == EngineShutdownRequested;
656 657
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
658 659

    case EngineRunRequested:
hjk's avatar
hjk committed
660
        return to == EngineRunFailed
661
            || to == InferiorRunRequested
hjk's avatar
hjk committed
662 663 664
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
665
    case EngineRunFailed:
666
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
667 668 669 670 671 672

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
673 674 675
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
hjk's avatar
hjk committed
676 677 678 679 680

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
Orgad Shaneh's avatar
Orgad Shaneh committed
681
            || to == InferiorStopOk || to == InferiorExitOk;
682
    case InferiorStopFailed:
hjk's avatar
hjk committed
683
        return to == EngineShutdownRequested;
684

685 686 687
    case InferiorExitOk:
        return to == InferiorShutdownOk;

688
    case InferiorUnrunnable:
hjk's avatar
hjk committed
689 690 691 692 693
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
694
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
695
        return to == EngineShutdownRequested;
696

hjk's avatar
hjk committed
697
    case EngineShutdownRequested:
698
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
699 700 701 702 703 704
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
705
        return to == EngineSetupRequested; // Happens on restart.
706 707
    }

708
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
709 710 711
    return false;
}

712 713
void DebuggerEngine::setupSlaveEngine()
{
714
    QTC_CHECK(state() == DebuggerNotReady);
715 716 717 718 719 720 721
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
722
    m_engine->validateExecutable(&m_startParameters);
723 724 725
    m_engine->setupEngine();
}

726
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
727
{
hjk's avatar
hjk committed
728
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
729 730 731 732 733 734 735
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());
    if (d->remoteSetupState() == RemoteSetupRequested)
        d->setRemoteSetupState(RemoteSetupCancelled);

736
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
737
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
738
    if (isMasterEngine() && runControl())
739
        runControl()->startFailed();
hjk's avatar
hjk committed
740
    setState(DebuggerFinished);
hjk's avatar
hjk committed
741 742
}

743
void DebuggerEngine::notifyEngineSetupOk()
744
{
hjk's avatar
hjk committed
745
    showMessage(_("NOTE: ENGINE SETUP OK"));
746 747 748 749
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

750
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
751
    setState(EngineSetupOk);
hjk's avatar
hjk committed
752
    showMessage(_("QUEUE: SETUP INFERIOR"));
753
    if (isMasterEngine())
754 755 756 757 758
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
759
    QTC_CHECK(state() == EngineSetupOk);
760
    d->queueSetupInferior();
761 762
}

hjk's avatar
hjk committed
763
void DebuggerEnginePrivate::doSetupInferior()
764
{
765
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
766
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
767
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
768 769 770 771 772
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
773
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
774
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
775
    showStatusMessage(tr("Setup failed."));
hjk's avatar
hjk committed
776
    setState(InferiorSetupFailed);
777 778
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
779 780 781 782
}

void DebuggerEngine::notifyInferiorSetupOk()
{
783 784 785
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
786
    aboutToNotifyInferiorSetupOk();
hjk's avatar
hjk committed
787
    showMessage(_("NOTE: INFERIOR SETUP OK"));
788 789 790 791
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
792 793
}

794 795 796
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
797
    QTC_CHECK(state() == InferiorSetupOk);
798 799 800
    d->queueRunEngine();
}

hjk's avatar
hjk committed
801 802
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
803
    m_engine->showMessage(_("CALL: RUN ENGINE"));
804
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
805
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
806 807 808
    m_engine->runEngine();
}

hjk's avatar
hjk committed
809 810
void DebuggerEngine::notifyInferiorUnrunnable()
{
hjk's avatar
hjk committed
811
    showMessage(_("NOTE: INFERIOR UNRUNNABLE"));
812 813
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
814
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
815
    showStatusMessage(tr("Loading finished."));
hjk's avatar
hjk committed
816 817 818 819 820
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{
hjk's avatar
hjk committed
821
    showMessage(_("NOTE: ENGINE RUN FAILED"));
822
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
823 824 825
    d->m_progress.setProgressValue(900);
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
826
    showStatusMessage(tr("Run failed."));
hjk's avatar
hjk committed
827
    setState(EngineRunFailed);
828 829
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
830 831
}

832 833 834 835 836 837 838 839 840 841 842
void DebuggerEngine::notifyEngineRequestRemoteSetup()
{
    showMessage(_("NOTE: REQUEST REMOTE SETUP"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone, qDebug() << this
               << "remoteSetupState" << d->remoteSetupState());

    d->setRemoteSetupState(RemoteSetupRequested);
    emit requestRemoteSetup();
}

hjk's avatar
hjk committed
843 844 845 846 847
void DebuggerEngine::notifyEngineRemoteServerRunning(const QByteArray &, int /*pid*/)
{
    showMessage(_("NOTE: REMOTE SERVER RUNNING IN MULTIMODE"));
}

848
void DebuggerEngine::notifyEngineRemoteSetupFinished(const RemoteSetupResult &result)
849 850 851 852 853 854