debuggerengine.cpp 55.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
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
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 39

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

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

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

59 60
#include <texteditor/itexteditor.h>

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

65
#include <qmljs/consolemanagerinterface.h>
66

67 68 69
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
70

71
#include <QMessageBox>
72

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

78 79 80 81
enum { debug = 0 };

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

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

88 89 90 91 92 93
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

94 95
namespace Debugger {

Friedemann Kleint's avatar
Friedemann Kleint committed
96 97 98 99 100 101 102 103 104
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;
105
    m_from = frame.from;
Friedemann Kleint's avatar
Friedemann Kleint committed
106 107
}

108 109
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
110 111
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
112 113 114 115 116 117 118
}

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

131

132 133 134 135 136 137
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

138 139 140 141 142 143 144 145 146
// transitions:
//   None->Requested
//   Requested->Succeeded
//   Requested->Failed
//   Requested->Cancelled
enum RemoteSetupState { RemoteSetupNone, RemoteSetupRequested,
                        RemoteSetupSucceeded, RemoteSetupFailed,
                        RemoteSetupCancelled };

147
class DebuggerEnginePrivate : public QObject
148
{
149 150
    Q_OBJECT

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

176
public slots:
177
    void doSetupEngine();
hjk's avatar
hjk committed
178 179
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
180 181
    void doShutdownEngine();
    void doShutdownInferior();
182
    void doInterruptInferior();
hjk's avatar
hjk committed
183
    void doFinishDebugger();
hjk's avatar
hjk committed
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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()));
    }

199 200
    void queueRunEngine()
    {
hjk's avatar
hjk committed
201 202 203 204 205
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

206 207
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
208 209 210 211 212
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

213 214
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
215 216 217 218 219
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

220 221
    void queueFinishDebugger()
    {
222 223 224
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
225 226 227 228 229
        resetLocation();
        if (isMasterEngine()) {
            m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
            QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
        }
hjk's avatar
hjk committed
230 231
    }

232 233
    void raiseApplication()
    {
234 235
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
236 237
    }

238
    void scheduleResetLocation()
239
    {
240
        m_stackHandler.scheduleResetLocation();
241
        m_watchHandler.scheduleResetLocation();
242
        m_threadsHandler.scheduleResetLocation();
243
        m_disassemblerAgent.scheduleResetLocation();
244 245 246 247
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

248
    void resetLocation()
249 250 251
    {
        m_locationTimer.stop();
        m_locationMark.reset();
252
        m_stackHandler.resetLocation();
253
        m_watchHandler.resetLocation();
254
        m_threadsHandler.resetLocation();
255
        m_disassemblerAgent.resetLocation();
256 257
    }

258 259
    TaskHub *taskHub();

260
public:
hjk's avatar
hjk committed
261
    DebuggerState state() const { return m_state; }
262
    RemoteSetupState remoteSetupState() const { return m_remoteSetupState; }
263 264 265
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
266
    void setRemoteSetupState(RemoteSetupState state);
hjk's avatar
hjk committed
267

268
    DebuggerEngine *m_engine; // Not owned.
269
    DebuggerEngine *m_masterEngine; // Not owned
270 271 272
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
hjk's avatar
hjk committed
273 274

    // The current state.
275 276
    DebuggerState m_state;

hjk's avatar
hjk committed
277 278 279 280 281 282
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

283 284 285
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

286 287 288 289 290 291 292 293
    qint64 m_inferiorPid;

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

296 297
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
298 299
    QScopedPointer<TextEditor::BaseTextMark> m_locationMark;
    QTimer m_locationTimer;
300 301

    bool m_isStateDebugging;
302

303
    Utils::FileInProjectFinder m_fileFinder;
304
    TaskHub *m_taskHub;
305 306 307 308 309 310 311 312 313
};


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

314 315
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters)
  : d(new DebuggerEnginePrivate(this, startParameters))
316
{}
317 318 319

DebuggerEngine::~DebuggerEngine()
{
320 321
    disconnect();
    delete d;
322 323
}

324 325 326 327 328 329 330 331 332 333 334
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)
335
        SN(InferiorSetupOk)
336 337 338 339 340 341 342 343
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
344
        SN(InferiorExitOk)
345 346 347 348 349 350 351 352 353 354 355 356
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

357
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
358
{
359
    showMessage(msg, StatusBar, timeout);
360
}
361

362 363 364 365 366 367 368 369 370 371 372 373
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));
}

374 375 376 377 378
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

379 380
ModulesHandler *DebuggerEngine::modulesHandler() const
{
381 382 383
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
384 385 386 387
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
388 389 390
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
391 392 393 394
}

StackHandler *DebuggerEngine::stackHandler() const
{
Aurindam Jana's avatar
Aurindam Jana committed
395 396 397
    return d->m_masterEngine
        ? d->m_masterEngine->stackHandler()
        : &d->m_stackHandler;
398 399 400 401
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
402 403 404
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
405 406 407 408
}

WatchHandler *DebuggerEngine::watchHandler() const
{
409 410 411
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
412 413 414 415
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
416 417 418
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
419 420 421 422
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
423
    QAbstractItemModel *model = modulesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
424 425 426
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ModulesModel"));
    return model;
427 428 429 430
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
431
    QAbstractItemModel *model = registerHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
432 433 434
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("RegisterModel"));
    return model;
435 436 437 438
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
439
    QAbstractItemModel *model = stackHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
440 441 442
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("StackModel"));
    return model;
443 444 445 446
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
447
    QAbstractItemModel *model = threadsHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
448 449 450
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ThreadsModel"));
    return model;
451 452 453 454
}

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

QAbstractItemModel *DebuggerEngine::watchersModel() const
{
hjk's avatar
hjk committed
460
    return watchHandler()->model();
461 462 463 464
}

QAbstractItemModel *DebuggerEngine::returnModel() const
{
hjk's avatar
hjk committed
465
    return watchHandler()->model();
466 467
}

468 469
QAbstractItemModel *DebuggerEngine::inspectorModel() const
{
hjk's avatar
hjk committed
470
    return watchHandler()->model();
471 472
}

473 474
QAbstractItemModel *DebuggerEngine::toolTipsModel() const
{
hjk's avatar
hjk committed
475 476 477 478 479 480
    return watchHandler()->model();
}

QAbstractItemModel *DebuggerEngine::watchModel() const
{
    return watchHandler()->model();
481 482
}

483 484
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
485
    QAbstractItemModel *model = sourceFilesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
486 487 488
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("SourceFilesModel"));
    return model;
489 490
}

491
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
492 493 494 495 496 497
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
498 499 500 501 502 503 504
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

505 506 507 508 509 510 511 512
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
{
513 514 515 516
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
517 518
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
519
    QmlJS::ConsoleManagerInterface *consoleManager = QmlJS::ConsoleManagerInterface::instance();
520
    if (channel == ConsoleOutput && consoleManager)
521
        consoleManager->printToConsolePane(QmlJS::ConsoleItem::UndefinedType, msg);
522

523
    debuggerCore()->showMessage(msg, channel, timeout);
524
    if (d->m_runControl)
525
        d->m_runControl->showMessage(msg, channel);
526
    else
527
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
528 529 530 531
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
532 533
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
534

535
    d->m_progress.setProgressRange(0, 1000);
hjk's avatar
hjk committed
536
    Core::FutureProgress *fp = Core::ICore::progressManager()
537 538
        ->addTask(d->m_progress.future(),
        tr("Launching"), _("Debugger.Launcher"));
con's avatar
con committed
539
    fp->setKeepOnFinish(Core::FutureProgress::HideOnFinish);
540 541
    d->m_progress.reportStarted();

542 543 544 545
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;
546 547
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
548

549 550
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
551

hjk's avatar
hjk committed
552
    debuggerCore()->action(OperateByInstruction)
553
        ->setEnabled(hasCapability(DisassemblerCapability));
554

555 556
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
557 558
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
559
    d->m_progress.setProgressValue(200);
560
    d->queueSetupEngine();
561 562 563 564
}

void DebuggerEngine::resetLocation()
{
565 566
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
567 568
}

569
void DebuggerEngine::gotoLocation(const Location &loc)
570
{
Aurindam Jana's avatar
Aurindam Jana committed
571 572
     d->resetLocation();

Aurindam Jana's avatar
Aurindam Jana committed
573 574
    if ((hasCapability(OperateByInstructionCapability) &&
            debuggerCore()->boolSetting(OperateByInstruction)) || !loc.hasDebugInfo()) {
575 576 577
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
578 579 580 581 582
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;


583 584
    const QString file = loc.fileName();
    const int line = loc.lineNumber();
hjk's avatar
hjk committed
585
    QList<IEditor *> editors = EditorManager::instance()->editorsForFileName(file);
586
    IEditor *editor = 0;
hjk's avatar
hjk committed
587
    if (editors.isEmpty()) {
hjk's avatar
hjk committed
588
        editor = EditorManager::openEditor(file, Core::Id(),
589
            EditorManager::IgnoreNavigationHistory);
590 591
        QTC_ASSERT(editor, return); // Unreadable file?
        editor->setProperty(Constants::OPENED_BY_DEBUGGER, true);
592 593
    } else {
        editor = editors.back();
hjk's avatar
hjk committed
594
    }
595

596
    ITextEditor *texteditor = qobject_cast<ITextEditor *>(editor);
hjk's avatar
hjk committed
597 598 599
    if (texteditor)
        texteditor->gotoLine(line, 0);

600
    if (loc.needsMarker()) {
Daniel Teske's avatar
Daniel Teske committed
601
        d->m_locationMark.reset(new TextEditor::BaseTextMark(file, line));
602 603
        d->m_locationMark->setIcon(debuggerCore()->locationMarkIcon());
        d->m_locationMark->setPriority(TextEditor::ITextMark::HighPriority);
604
        d->m_locationMark->init();
605
    }
606 607

    // FIXME: Breaks with split views.
608
    if (!d->m_memoryAgent.hasVisibleEditor() || loc.needsRaise())
hjk's avatar
hjk committed
609
        EditorManager::activateEditor(editor);
610
    //qDebug() << "MEMORY: " << d->m_memoryAgent.hasVisibleEditor();
611 612
}

613 614 615
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
616
    showMessage(QLatin1String("HANDLE RUNCONTROL START FAILED"));
617
    d->m_runControl = 0;
618
    d->m_progress.setProgressValue(900);
619 620
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
621 622
}

623 624
// Called from RunControl.
void DebuggerEngine::handleFinished()
625
{
626
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
627
    d->m_runControl = 0;
628 629
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    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
651
DebuggerState DebuggerEngine::lastGoodState() const
652
{
hjk's avatar
hjk committed
653 654 655 656 657 658 659
    return d->m_lastGoodState;
}

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

hjk's avatar
hjk committed
661 662 663
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
664
    case DebuggerNotReady:
665
        return to == EngineSetupRequested;
666

hjk's avatar
hjk committed
667
    case EngineSetupRequested:
668 669
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
670 671 672
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
673
    case EngineSetupOk:
hjk's avatar
hjk committed
674
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
675

hjk's avatar
hjk committed
676
    case InferiorSetupRequested:
677
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
678
    case InferiorSetupFailed:
hjk's avatar
hjk committed
679
        return to == EngineShutdownRequested;
680 681
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
682 683

    case EngineRunRequested:
hjk's avatar
hjk committed
684
        return to == EngineRunFailed
685
            || to == InferiorRunRequested
hjk's avatar
hjk committed
686 687 688
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
689
    case EngineRunFailed:
690
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
691 692 693 694 695 696

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
697 698 699
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
hjk's avatar
hjk committed
700 701 702 703 704

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
705
            || to == InferiorStopOk || InferiorExitOk;
706
    case InferiorStopFailed:
hjk's avatar
hjk committed
707
        return to == EngineShutdownRequested;
708

709 710 711
    case InferiorExitOk:
        return to == InferiorShutdownOk;

712
    case InferiorUnrunnable:
hjk's avatar
hjk committed
713 714 715 716 717
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
718
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
719
        return to == EngineShutdownRequested;
720

hjk's avatar
hjk committed
721
    case EngineShutdownRequested:
722
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
723 724 725 726 727 728
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
729
        return to == EngineSetupRequested; // Happens on restart.
730 731
    }

732
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
733 734 735
    return false;
}

736 737
void DebuggerEngine::setupSlaveEngine()
{
738
    QTC_CHECK(state() == DebuggerNotReady);
739 740 741 742 743 744 745
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
David Schulz's avatar
David Schulz committed
746
    m_engine->checkForReleaseBuild(m_startParameters);
747 748 749
    m_engine->setupEngine();
}

750
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
751
{
hjk's avatar
hjk committed
752
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
753 754 755 756 757 758 759
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());
    if (d->remoteSetupState() == RemoteSetupRequested)
        d->setRemoteSetupState(RemoteSetupCancelled);

760
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
761
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
762
    if (isMasterEngine() && runControl())
763
        runControl()->startFailed();
hjk's avatar
hjk committed
764
    setState(DebuggerFinished);
hjk's avatar
hjk committed
765 766
}

767
void DebuggerEngine::notifyEngineSetupOk()
768
{
hjk's avatar
hjk committed
769
    showMessage(_("NOTE: ENGINE SETUP OK"));
770 771 772 773
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

774
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
775
    setState(EngineSetupOk);
hjk's avatar
hjk committed
776
    showMessage(_("QUEUE: SETUP INFERIOR"));
777
    if (isMasterEngine())
778 779 780 781 782
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
783
    QTC_CHECK(state() == EngineSetupOk);
784
    d->queueSetupInferior();
785 786
}

hjk's avatar
hjk committed
787
void DebuggerEnginePrivate::doSetupInferior()
788
{
789
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
790
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
791
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
792 793 794 795 796
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
797
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
798
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
799
    showStatusMessage(tr("Setup failed."));
hjk's avatar
hjk committed
800
    setState(InferiorSetupFailed);
801 802
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
803 804 805 806
}

void DebuggerEngine::notifyInferiorSetupOk()
{
807 808 809
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
810
    showMessage(_("NOTE: INFERIOR SETUP OK"));
811 812 813 814
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
815 816
}

817 818 819
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
820
    QTC_CHECK(state() == InferiorSetupOk);
821 822 823
    d->queueRunEngine();
}

hjk's avatar
hjk committed
824 825
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
826
    m_engine->showMessage(_("CALL: RUN ENGINE"));
827
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
828
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
829 830 831
    m_engine->runEngine();
}

hjk's avatar
hjk committed
832 833
void DebuggerEngine::notifyInferiorUnrunnable()
{
hjk's avatar
hjk committed
834
    showMessage(_("NOTE: INFERIOR UNRUNNABLE"));
835 836
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
837
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
838
    showStatusMessage(tr("Loading finished."));
hjk's avatar
hjk committed
839 840 841 842 843
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{
hjk's avatar
hjk committed
844
    showMessage(_("NOTE: ENGINE RUN FAILED"));
845
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
846 847 848
    d->m_progress.setProgressValue(900);
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
849
    showStatusMessage(tr("Run failed."));
hjk's avatar
hjk committed
850
    setState(EngineRunFailed);