debuggerengine.cpp 60.9 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 53
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
hjk's avatar
hjk committed
54

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

58 59
#include <utils/savedaction.h>
#include <utils/qtcassert.h>
60
#include <utils/fileinprojectfinder.h>
61

62
#include <qmljs/consolemanagerinterface.h>
63

64 65 66
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
67

68
#include <QMessageBox>
69

hjk's avatar
hjk committed
70
using namespace Core;
71
using namespace Debugger::Internal;
hjk's avatar
hjk committed
72 73
using namespace ProjectExplorer;
using namespace TextEditor;
74

75 76 77 78
enum { debug = 0 };

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

80 81 82 83
//#define WITH_BENCHMARK
#ifdef WITH_BENCHMARK
#include <valgrind/callgrind.h>
#endif
84

85 86 87 88 89 90
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

91 92
namespace Debugger {

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

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

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

128

129 130 131 132 133 134
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

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

144
class DebuggerEnginePrivate : public QObject
145
{
146 147
    Q_OBJECT

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

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

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

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

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

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

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

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

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

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

257 258
    TaskHub *taskHub();

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

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

    DebuggerStartParameters m_startParameters;
hjk's avatar
hjk committed
272 273

    // The current state.
274 275
    DebuggerState m_state;

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

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

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

285 286 287 288 289 290 291 292
    qint64 m_inferiorPid;

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

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

    bool m_isStateDebugging;
301

302
    Utils::FileInProjectFinder m_fileFinder;
303 304 305
    // Testing
    void handleAutoTests();
    void handleAutoTestLine(int line);
306
    void reportTestError(const QString &msg, int line);
307
    bool m_testsPossible;
308
    bool m_testsRunning;
David Schulz's avatar
David Schulz committed
309 310
    bool m_breakOnError;
    bool m_foundError;
311
    QStringList m_testContents;
312 313
    TaskHub *m_taskHub;
    QString m_testFileName;
314 315 316 317 318 319 320 321 322
};


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

323 324
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters)
  : d(new DebuggerEnginePrivate(this, startParameters))
325
{}
326 327 328

DebuggerEngine::~DebuggerEngine()
{
329 330
    disconnect();
    delete d;
331 332
}

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

366
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
367
{
368
    showMessage(msg, StatusBar, timeout);
369
}
370

371 372 373 374 375 376 377 378 379 380 381 382
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));
}

383 384 385 386 387
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

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

RegisterHandler *DebuggerEngine::registerHandler() const
{
397 398 399
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
400 401 402 403
}

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

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
411 412 413
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
414 415 416 417
}

WatchHandler *DebuggerEngine::watchHandler() const
{
418 419 420
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
421 422 423 424
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
425 426 427
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
428 429 430 431
}

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

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

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

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
456
    QAbstractItemModel *model = threadsHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
457 458 459
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ThreadsModel"));
    return model;
460 461 462 463
}

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

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

QAbstractItemModel *DebuggerEngine::returnModel() const
{
hjk's avatar
hjk committed
474
    return watchHandler()->model();
475 476
}

477 478
QAbstractItemModel *DebuggerEngine::inspectorModel() const
{
hjk's avatar
hjk committed
479
    return watchHandler()->model();
480 481
}

482 483
QAbstractItemModel *DebuggerEngine::toolTipsModel() const
{
hjk's avatar
hjk committed
484 485 486 487 488 489
    return watchHandler()->model();
}

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

492 493
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
494
    QAbstractItemModel *model = sourceFilesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
495 496 497
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("SourceFilesModel"));
    return model;
498 499
}

500
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
501 502 503 504 505 506
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
507 508 509 510 511 512 513
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

514 515 516 517 518 519 520 521
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
{
522 523 524 525
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
526 527
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
528
    QmlJS::ConsoleManagerInterface *consoleManager = QmlJS::ConsoleManagerInterface::instance();
529
    if (channel == ConsoleOutput && consoleManager)
530
        consoleManager->printToConsolePane(QmlJS::ConsoleItem::UndefinedType, msg);
531

532
    debuggerCore()->showMessage(msg, channel, timeout);
533
    if (d->m_runControl)
534
        d->m_runControl->showMessage(msg, channel);
535
    else
536
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
537 538 539 540
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
541 542
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
543

544
    d->m_progress.setProgressRange(0, 1000);
hjk's avatar
hjk committed
545
    Core::FutureProgress *fp = Core::ICore::progressManager()
546 547
        ->addTask(d->m_progress.future(),
        tr("Launching"), _("Debugger.Launcher"));
con's avatar
con committed
548
    fp->setKeepOnFinish(Core::FutureProgress::HideOnFinish);
549 550
    d->m_progress.reportStarted();

551 552 553 554
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;
555 556
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
557

558 559
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
560

hjk's avatar
hjk committed
561
    debuggerCore()->action(OperateByInstruction)
562
        ->setEnabled(hasCapability(DisassemblerCapability));
563

564 565
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
566 567
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
568
    d->m_progress.setProgressValue(200);
569
    d->queueSetupEngine();
570 571 572 573
}

void DebuggerEngine::resetLocation()
{
574 575
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
576 577
}

578
void DebuggerEngine::gotoLocation(const Location &loc)
579
{
Aurindam Jana's avatar
Aurindam Jana committed
580 581
     d->resetLocation();

Aurindam Jana's avatar
Aurindam Jana committed
582 583
    if ((hasCapability(OperateByInstructionCapability) &&
            debuggerCore()->boolSetting(OperateByInstruction)) || !loc.hasDebugInfo()) {
584 585 586
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
587 588 589 590 591
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;


592 593
    const QString file = loc.fileName();
    const int line = loc.lineNumber();
hjk's avatar
hjk committed
594
    QList<IEditor *> editors = EditorManager::instance()->editorsForFileName(file);
595
    IEditor *editor = 0;
hjk's avatar
hjk committed
596
    if (editors.isEmpty()) {
hjk's avatar
hjk committed
597
        editor = EditorManager::openEditor(file, Core::Id(),
598 599 600 601 602 603 604
            EditorManager::IgnoreNavigationHistory);
        if (editor) {
            editors.append(editor);
            editor->setProperty(Constants::OPENED_BY_DEBUGGER, true);
        }
    } else {
        editor = editors.back();
hjk's avatar
hjk committed
605
    }
606
    ITextEditor *texteditor = qobject_cast<ITextEditor *>(editor);
hjk's avatar
hjk committed
607 608 609
    if (texteditor)
        texteditor->gotoLine(line, 0);

610
    if (loc.needsMarker()) {
Daniel Teske's avatar
Daniel Teske committed
611
        d->m_locationMark.reset(new TextEditor::BaseTextMark(file, line));
612 613
        d->m_locationMark->setIcon(debuggerCore()->locationMarkIcon());
        d->m_locationMark->setPriority(TextEditor::ITextMark::HighPriority);
614
        d->m_locationMark->init();
615
    }
616 617

    // FIXME: Breaks with split views.
618
    if (!d->m_memoryAgent.hasVisibleEditor() || loc.needsRaise())
hjk's avatar
hjk committed
619
        EditorManager::activateEditor(editor);
620
    //qDebug() << "MEMORY: " << d->m_memoryAgent.hasVisibleEditor();
621 622
}

623 624 625
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
626
    showMessage(QLatin1String("HANDLE RUNCONTROL START FAILED"));
627
    d->m_runControl = 0;
628
    d->m_progress.setProgressValue(900);
629 630
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
631 632
}

633 634
// Called from RunControl.
void DebuggerEngine::handleFinished()
635
{
636
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
637
    d->m_runControl = 0;
638 639
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    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
661
DebuggerState DebuggerEngine::lastGoodState() const
662
{
hjk's avatar
hjk committed
663 664 665 666 667 668 669
    return d->m_lastGoodState;
}

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

hjk's avatar
hjk committed
671 672 673
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
674
    case DebuggerNotReady:
675
        return to == EngineSetupRequested;
676

hjk's avatar
hjk committed
677
    case EngineSetupRequested:
678 679
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
680 681 682
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
683
    case EngineSetupOk:
hjk's avatar
hjk committed
684
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
685

hjk's avatar
hjk committed
686
    case InferiorSetupRequested:
687
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
688
    case InferiorSetupFailed:
hjk's avatar
hjk committed
689
        return to == EngineShutdownRequested;
690 691
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
692 693

    case EngineRunRequested:
hjk's avatar
hjk committed
694
        return to == EngineRunFailed
695
            || to == InferiorRunRequested
hjk's avatar
hjk committed
696 697 698
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
699
    case EngineRunFailed:
700
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
701 702 703 704 705 706

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
707 708 709
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
hjk's avatar
hjk committed
710 711 712 713 714

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
715
            || to == InferiorStopOk || InferiorExitOk;
716
    case InferiorStopFailed:
hjk's avatar
hjk committed
717
        return to == EngineShutdownRequested;
718

719 720 721
    case InferiorExitOk:
        return to == InferiorShutdownOk;

722
    case InferiorUnrunnable:
hjk's avatar
hjk committed
723 724 725 726 727
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
728
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
729
        return to == EngineShutdownRequested;
730

hjk's avatar
hjk committed
731
    case EngineShutdownRequested:
732
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
733 734 735 736 737 738
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
739
        return to == EngineSetupRequested; // Happens on restart.
740 741
    }

742
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
743 744 745
    return false;
}

746 747
void DebuggerEngine::setupSlaveEngine()
{
748
    QTC_CHECK(state() == DebuggerNotReady);
749 750 751 752 753 754 755
    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
756
    m_engine->checkForReleaseBuild(m_startParameters);
757 758 759
    m_engine->setupEngine();
}

760
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
761
{
hjk's avatar
hjk committed
762
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
763 764 765 766 767 768 769
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());
    if (d->remoteSetupState() == RemoteSetupRequested)
        d->setRemoteSetupState(RemoteSetupCancelled);

770
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
771
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
772
    if (isMasterEngine() && runControl())
773
        runControl()->startFailed();
hjk's avatar
hjk committed
774
    setState(DebuggerFinished);
hjk's avatar
hjk committed
775 776
}

777
void DebuggerEngine::notifyEngineSetupOk()
778
{
hjk's avatar
hjk committed
779
    showMessage(_("NOTE: ENGINE SETUP OK"));
780 781 782 783
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

784
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
785
    setState(EngineSetupOk);
hjk's avatar
hjk committed
786
    showMessage(_("QUEUE: SETUP INFERIOR"));
787
    if (isMasterEngine())
788 789 790 791 792
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
793
    QTC_CHECK(state() == EngineSetupOk);
794
    d->queueSetupInferior();
795 796
}

hjk's avatar
hjk committed
797
void DebuggerEnginePrivate::doSetupInferior()
798
{
799
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
800
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
801
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
802 803 804 805 806
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
807
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
808
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
809
    showStatusMessage(tr("Setup failed."));
hjk's avatar
hjk committed
810
    setState(InferiorSetupFailed);
811 812
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
813 814 815 816
}

void DebuggerEngine::notifyInferiorSetupOk()
{
817 818 819
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
820
    showMessage(_("NOTE: INFERIOR SETUP OK"));
821 822 823 824
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
825 826
}

827 828 829
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
830
    QTC_CHECK(state() == InferiorSetupOk);
831 832 833
    d->queueRunEngine();
}

hjk's avatar
hjk committed
834 835
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
836
    m_engine->showMessage(_("CALL: RUN ENGINE"));
837
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
838
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
839 840 841
    m_engine->runEngine();
}

hjk's avatar
hjk committed
842 843
void DebuggerEngine::notifyInferiorUnrunnable()
{
hjk's avatar
hjk committed
844
    showMessage(_("NOTE: INFERIOR UNRUNNABLE"));
845 846
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
847
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
848
    showStatusMessage(tr("Loading finished."));
hjk's avatar
hjk committed
849 850 851 852 853
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{
hjk's avatar
hjk committed
854
    showMessage(_("NOTE: ENGINE RUN FAILED"));