debuggerengine.cpp 55.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 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
****************************************************************************/
29 30 31

#include "debuggerengine.h"

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

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

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

hjk's avatar
hjk committed
57
#include <projectexplorer/projectexplorer.h>
58 59
#include <projectexplorer/taskhub.h>

60
#include <texteditor/basetexteditor.h>
61

62 63
#include <utils/savedaction.h>
#include <utils/qtcassert.h>
64
#include <utils/fileinprojectfinder.h>
65

66
#include <qmljs/consolemanagerinterface.h>
67

68 69 70
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
71
#include <QDir>
72

73
#include <QMessageBox>
74

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

80 81 82 83
enum { debug = 0 };

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

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

90 91 92 93 94 95
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

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

99 100
namespace Debugger {

Friedemann Kleint's avatar
Friedemann Kleint committed
101 102 103 104 105 106 107 108 109
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;
110
    m_from = frame.from;
Friedemann Kleint's avatar
Friedemann Kleint committed
111 112
}

113 114
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
115 116
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
117 118 119 120 121 122 123
}

QDebug operator<<(QDebug str, const DebuggerStartParameters &sp)
{
    QDebug nospace = str.nospace();
    nospace << "executable=" << sp.executable
            << " coreFile=" << sp.coreFile
124
            << " processArgs=" << sp.processArgs
125 126 127 128 129 130
            << " environment=<" << sp.environment.size() << " variables>"
            << " workingDir=" << sp.workingDirectory
            << " attachPID=" << sp.attachPID
            << " useTerminal=" << sp.useTerminal
            << " remoteChannel=" << sp.remoteChannel
            << " serverStartScript=" << sp.serverStartScript
131
            << " abi=" << sp.toolChainAbi.toString() << '\n';
132 133 134
    return str;
}

135

136 137 138 139 140 141
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

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

151
class DebuggerEnginePrivate : public QObject
152
{
153 154
    Q_OBJECT

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

hjk's avatar
hjk committed
180
        VariableManager::registerFileVariables(PrefixDebugExecutable,
181
            tr("Debugged executable"),
Orgad Shaneh's avatar
Orgad Shaneh committed
182
            [this]() { return m_startParameters.executable; });
183
    }
184

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

194 195 196 197 198
    void reloadDisassembly()
    {
        m_disassemblerAgent.reload();
    }

199 200 201 202 203 204 205 206 207 208 209 210 211 212
    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()));
    }

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

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

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

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

246 247
    void raiseApplication()
    {
248 249
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
250 251
    }

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

262
    void resetLocation()
263 264 265
    {
        m_locationTimer.stop();
        m_locationMark.reset();
266
        m_stackHandler.resetLocation();
267
        m_watchHandler.resetLocation();
268
        m_threadsHandler.resetLocation();
269
        m_disassemblerAgent.resetLocation();
270 271
    }

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

280
    DebuggerEngine *m_engine; // Not owned.
281
    DebuggerEngine *m_masterEngine; // Not owned
282 283 284
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
hjk's avatar
hjk committed
285 286

    // The current state.
287 288
    DebuggerState m_state;

hjk's avatar
hjk committed
289 290 291 292 293 294
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

295 296 297
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

298 299 300 301 302 303 304 305
    qint64 m_inferiorPid;

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

308 309
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
hjk's avatar
hjk committed
310
    QScopedPointer<TextEditor::TextMark> m_locationMark;
311
    QTimer m_locationTimer;
312 313

    bool m_isStateDebugging;
314

315
    Utils::FileInProjectFinder m_fileFinder;
316

317 318 319 320 321 322 323 324 325
};


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

326 327
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters)
  : d(new DebuggerEnginePrivate(this, startParameters))
328
{}
329 330 331

DebuggerEngine::~DebuggerEngine()
{
332 333
    disconnect();
    delete d;
334 335
}

336 337 338 339 340 341 342 343 344 345 346
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)
347
        SN(InferiorSetupOk)
348 349 350 351 352 353 354 355
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
356
        SN(InferiorExitOk)
357 358 359 360 361 362 363 364 365 366 367 368
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

369
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
370
{
371
    showMessage(msg, StatusBar, timeout);
372
}
373

374 375 376 377 378 379 380 381 382 383 384 385
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));
}

386 387 388 389 390
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

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

RegisterHandler *DebuggerEngine::registerHandler() const
{
400 401 402
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
403 404 405 406
}

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

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
414 415 416
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
417 418 419 420
}

WatchHandler *DebuggerEngine::watchHandler() const
{
421 422 423
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
424 425 426 427
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
428 429 430
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
431 432 433 434
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
435
   return modulesHandler()->model();
436 437 438 439
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
440
    return registerHandler()->model();
441 442 443 444
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
445
    return stackHandler()->model();
446 447 448 449
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
450
    return threadsHandler()->model();
451 452
}

hjk's avatar
hjk committed
453 454 455
QAbstractItemModel *DebuggerEngine::watchModel() const
{
    return watchHandler()->model();
456 457
}

458 459
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
460
    return sourceFilesHandler()->model();
461 462
}

463
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
464 465 466 467 468 469
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

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

477 478 479 480 481 482 483 484
void DebuggerEngine::setRegisterValue(int regnr, const QString &value)
{
    Q_UNUSED(regnr);
    Q_UNUSED(value);
}

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

495
    debuggerCore()->showMessage(msg, channel, timeout);
496
    if (d->m_runControl)
497
        d->m_runControl->showMessage(msg, channel);
498
    else
499
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
500 501 502 503
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
504 505
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
506

507
    d->m_progress.setProgressRange(0, 1000);
508
    FutureProgress *fp = ProgressManager::addTask(d->m_progress.future(),
509
        tr("Launching Debugger"), "Debugger.Launcher");
510
    connect(fp, SIGNAL(canceled()), this, SLOT(quitDebugger()));
511
    fp->setKeepOnFinish(FutureProgress::HideOnFinish);
512 513
    d->m_progress.reportStarted();

514 515 516 517
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;
518 519
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
520

521 522
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
523

hjk's avatar
hjk committed
524
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
525

526 527
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
528 529
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
530
    d->m_progress.setProgressValue(200);
531
    d->queueSetupEngine();
532 533 534 535
}

void DebuggerEngine::resetLocation()
{
536 537
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
538 539
}

540
void DebuggerEngine::gotoLocation(const Location &loc)
541
{
Aurindam Jana's avatar
Aurindam Jana committed
542 543
     d->resetLocation();

Aurindam Jana's avatar
Aurindam Jana committed
544
    if ((hasCapability(OperateByInstructionCapability) &&
hjk's avatar
hjk committed
545
            boolSetting(OperateByInstruction)) || !loc.hasDebugInfo()) {
546 547 548
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
549 550 551 552 553
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;


554
    const QString file = QDir::cleanPath(loc.fileName());
555
    const int line = loc.lineNumber();
556
    bool newEditor = false;
557
    IEditor *editor = EditorManager::openEditor(file, Id(),
558 559
                                                EditorManager::IgnoreNavigationHistory, &newEditor);
    QTC_ASSERT(editor, return); // Unreadable file?
560

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

563 564
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
565

566
    if (loc.needsMarker()) {
hjk's avatar
hjk committed
567
        d->m_locationMark.reset(new TextEditor::TextMark(file, line));
568
        d->m_locationMark->setIcon(debuggerCore()->locationMarkIcon());
hjk's avatar
hjk committed
569
        d->m_locationMark->setPriority(TextEditor::TextMark::HighPriority);
570
    }
571 572

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

575 576 577
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
578
    showMessage(QLatin1String("HANDLE RUNCONTROL START FAILED"));
579
    d->m_runControl = 0;
580
    d->m_progress.setProgressValue(900);
581 582
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
583 584
}

585 586
// Called from RunControl.
void DebuggerEngine::handleFinished()
587
{
588
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
589
    d->m_runControl = 0;
590 591
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    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
613
DebuggerState DebuggerEngine::lastGoodState() const
614
{
hjk's avatar
hjk committed
615 616 617 618 619 620 621
    return d->m_lastGoodState;
}

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

hjk's avatar
hjk committed
623 624 625
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
626
    case DebuggerNotReady:
627
        return to == EngineSetupRequested;
628

hjk's avatar
hjk committed
629
    case EngineSetupRequested:
630 631
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
632 633 634
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
635
    case EngineSetupOk:
hjk's avatar
hjk committed
636
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
637

hjk's avatar
hjk committed
638
    case InferiorSetupRequested:
639
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
640
    case InferiorSetupFailed:
hjk's avatar
hjk committed
641
        return to == EngineShutdownRequested;
642 643
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
644 645

    case EngineRunRequested:
hjk's avatar
hjk committed
646
        return to == EngineRunFailed
647
            || to == InferiorRunRequested
hjk's avatar
hjk committed
648 649 650
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
651
    case EngineRunFailed:
652
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
653 654 655 656 657 658

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
659 660 661
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
hjk's avatar
hjk committed
662 663 664 665 666

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
Orgad Shaneh's avatar
Orgad Shaneh committed
667
            || to == InferiorStopOk || to == InferiorExitOk;
668
    case InferiorStopFailed:
hjk's avatar
hjk committed
669
        return to == EngineShutdownRequested;
670

671 672 673
    case InferiorExitOk:
        return to == InferiorShutdownOk;

674
    case InferiorUnrunnable:
hjk's avatar
hjk committed
675 676 677 678 679
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
680
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
681
        return to == EngineShutdownRequested;
682

hjk's avatar
hjk committed
683
    case EngineShutdownRequested:
684
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
685 686 687 688 689 690
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
691
        return to == EngineSetupRequested; // Happens on restart.
692 693
    }

694
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
695 696 697
    return false;
}

698 699
void DebuggerEngine::setupSlaveEngine()
{
700
    QTC_CHECK(state() == DebuggerNotReady);
701 702 703 704 705 706 707
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
708
    m_engine->validateExecutable(&m_startParameters);
709 710 711
    m_engine->setupEngine();
}

712
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
713
{
hjk's avatar
hjk committed
714
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
715 716 717 718 719 720 721
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());
    if (d->remoteSetupState() == RemoteSetupRequested)
        d->setRemoteSetupState(RemoteSetupCancelled);

722
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
723
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
724
    if (isMasterEngine() && runControl())
725
        runControl()->startFailed();
hjk's avatar
hjk committed
726
    setState(DebuggerFinished);
hjk's avatar
hjk committed
727 728
}

729
void DebuggerEngine::notifyEngineSetupOk()
730
{
hjk's avatar
hjk committed
731
    showMessage(_("NOTE: ENGINE SETUP OK"));
732 733 734 735
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

736
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
737
    setState(EngineSetupOk);
hjk's avatar
hjk committed
738
    showMessage(_("QUEUE: SETUP INFERIOR"));
739
    if (isMasterEngine())
740 741 742 743 744
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
745
    QTC_CHECK(state() == EngineSetupOk);
746
    d->queueSetupInferior();
747 748
}

hjk's avatar
hjk committed
749
void DebuggerEnginePrivate::doSetupInferior()
750
{
751
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
752
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
753
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
754 755 756 757 758
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
759
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
760
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
761
    showStatusMessage(tr("Setup failed."));
hjk's avatar
hjk committed
762
    setState(InferiorSetupFailed);
763 764
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
765 766 767 768
}

void DebuggerEngine::notifyInferiorSetupOk()
{
769 770 771
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
772
    showMessage(_("NOTE: INFERIOR SETUP OK"));
773 774 775 776
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
777 778
}

779 780 781
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
782
    QTC_CHECK(state() == InferiorSetupOk);
783 784 785
    d->queueRunEngine();
}

hjk's avatar
hjk committed
786 787
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
788
    m_engine->showMessage(_("CALL: RUN ENGINE"));
789
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
790
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
791 792 793
    m_engine->runEngine();
}

hjk's avatar
hjk committed
794 795
void DebuggerEngine::notifyInferiorUnrunnable()
{
hjk's avatar
hjk committed
796
    showMessage(_("NOTE: INFERIOR UNRUNNABLE"));
797 798
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
799
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
800
    showStatusMessage(tr("Loading finished."));
hjk's avatar
hjk committed
801 802 803 804 805
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{
hjk's avatar
hjk committed
806
    showMessage(_("NOTE: ENGINE RUN FAILED"));
807
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
808 809 810
    d->m_progress.setProgressValue(900);
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
811
    showStatusMessage(tr("Run failed."));
hjk's avatar
hjk committed
812
    setState(EngineRunFailed);
813 814
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
815 816
}

817 818 819 820 821 822 823 824 825 826 827
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
828 829 830 831 832
void DebuggerEngine::notifyEngineRemoteServerRunning(const QByteArray &, int /*pid*/)
{
    showMessage(_("NOTE: REMOTE SERVER RUNNING IN MULTIMODE"));
}

833
void DebuggerEngine::notifyEngineRemoteSetupFinished(const RemoteSetupResult &result)
834 835 836 837 838 839 840 841 842
{
    QTC_ASSERT(state() == EngineSetupRequested
               || state() == EngineSetupFailed
               || state() == DebuggerFinished, qDebug() << this << state());

    QTC_ASSERT(d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupCancelled,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

843 844 845
    if (result.success) {
        showMessage(_("NOTE: REMOTE SETUP DONE: GDB SERVER PORT: %1  QML PORT %2")
                    .arg(result.gdbServerPort).arg(result.qmlServerPort));
846

847 848
        if (d->remoteSetupState() != RemoteSetupCancelled)
            d->setRemoteSetupState(RemoteSetupSucceeded);
hjk's avatar
hjk committed
849