debuggerengine.cpp 58.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31 32

#include "debuggerengine.h"

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

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

hjk's avatar
hjk committed
55
#include <coreplugin/editormanager/editormanager.h>
56
#include <coreplugin/editormanager/ieditor.h>
hjk's avatar
hjk committed
57
#include <coreplugin/icore.h>
58
#include <coreplugin/idocument.h>
59
#include <coreplugin/messagebox.h>
60 61
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
hjk's avatar
hjk committed
62

hjk's avatar
hjk committed
63
#include <projectexplorer/projectexplorer.h>
64 65
#include <projectexplorer/taskhub.h>

66
#include <texteditor/texteditor.h>
67

68
#include <utils/fileinprojectfinder.h>
hjk's avatar
hjk committed
69 70 71
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
72

73
#include <qmljs/consolemanagerinterface.h>
74

75 76 77
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
78
#include <QDir>
79

hjk's avatar
hjk committed
80
using namespace Core;
81
using namespace Debugger::Internal;
hjk's avatar
hjk committed
82 83
using namespace ProjectExplorer;
using namespace TextEditor;
84

85 86 87 88
enum { debug = 0 };

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

90 91 92 93
//#define WITH_BENCHMARK
#ifdef WITH_BENCHMARK
#include <valgrind/callgrind.h>
#endif
94

95 96
///////////////////////////////////////////////////////////////////////
//
97
// DebuggerRunParameters
98 99 100
//
///////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
101 102 103
// VariableManager Prefix
const char PrefixDebugExecutable[]  = "DebuggedExecutable";

104 105
namespace Debugger {

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

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

hjk's avatar
hjk committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141
namespace Internal {

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

142

143 144 145 146 147 148
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

149 150 151 152 153 154 155 156 157
// transitions:
//   None->Requested
//   Requested->Succeeded
//   Requested->Failed
//   Requested->Cancelled
enum RemoteSetupState { RemoteSetupNone, RemoteSetupRequested,
                        RemoteSetupSucceeded, RemoteSetupFailed,
                        RemoteSetupCancelled };

158 159 160 161 162 163
struct TypeInfo
{
    TypeInfo(uint s = 0) : size(s) {}
    uint size;
};

164
class DebuggerEnginePrivate : public QObject
165
{
166 167
    Q_OBJECT

168
public:
169
    DebuggerEnginePrivate(DebuggerEngine *engine, const DebuggerRunParameters &sp)
170
      : m_engine(engine),
171
        m_masterEngine(0),
172
        m_runControl(0),
173
        m_runParameters(sp),
174
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
175
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
176
        m_targetState(DebuggerNotReady),
177
        m_remoteSetupState(RemoteSetupNone),
178
        m_inferiorPid(0),
179
        m_modulesHandler(engine),
180
        m_registerHandler(),
181 182
        m_sourceFilesHandler(),
        m_stackHandler(),
hjk's avatar
hjk committed
183
        m_threadsHandler(),
184
        m_watchHandler(engine),
185
        m_disassemblerAgent(engine),
186
        m_memoryAgent(engine),
187
        m_isStateDebugging(false)
188
    {
hjk's avatar
hjk committed
189 190 191 192
        connect(&m_locationTimer, &QTimer::timeout,
                this, &DebuggerEnginePrivate::resetLocation);
        connect(action(IntelFlavor), &Utils::SavedAction::valueChanged,
                this, &DebuggerEnginePrivate::reloadDisassembly);
193 194 195
        connect(action(OperateNativeMixed), &QAction::triggered,
                engine, &DebuggerEngine::reloadFullStack);

hjk's avatar
hjk committed
196
        Utils::globalMacroExpander()->registerFileVariables(PrefixDebugExecutable,
197
            tr("Debugged executable"),
198
            [this]() { return m_runParameters.executable; });
199
    }
200

201
public slots:
202
    void doSetupEngine();
hjk's avatar
hjk committed
203 204
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
205 206
    void doShutdownEngine();
    void doShutdownInferior();
207
    void doInterruptInferior();
hjk's avatar
hjk committed
208
    void doFinishDebugger();
hjk's avatar
hjk committed
209

210 211 212 213 214
    void reloadDisassembly()
    {
        m_disassemblerAgent.reload();
    }

215 216 217 218 219 220 221 222 223 224 225 226 227 228
    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()));
    }

229 230
    void queueRunEngine()
    {
hjk's avatar
hjk committed
231 232 233 234 235
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

236 237
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
238 239 240 241 242
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

243 244
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
245 246 247 248 249
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

250 251
    void queueFinishDebugger()
    {
252 253 254
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
255 256 257 258 259
        resetLocation();
        if (isMasterEngine()) {
            m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
            QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
        }
hjk's avatar
hjk committed
260 261
    }

262 263
    void raiseApplication()
    {
264 265
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
266 267
    }

268
    void scheduleResetLocation()
269
    {
270
        m_stackHandler.scheduleResetLocation();
271
        m_watchHandler.scheduleResetLocation();
272
        m_threadsHandler.scheduleResetLocation();
273
        m_disassemblerAgent.scheduleResetLocation();
274 275 276 277
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

278
    void resetLocation()
279 280 281
    {
        m_locationTimer.stop();
        m_locationMark.reset();
282
        m_stackHandler.resetLocation();
283
        m_watchHandler.resetLocation();
284
        m_threadsHandler.resetLocation();
285
        m_disassemblerAgent.resetLocation();
hjk's avatar
hjk committed
286
        DebuggerToolTipManager::resetLocation();
287 288
    }

289
public:
hjk's avatar
hjk committed
290
    DebuggerState state() const { return m_state; }
291
    RemoteSetupState remoteSetupState() const { return m_remoteSetupState; }
292 293 294
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
295
    void setRemoteSetupState(RemoteSetupState state);
hjk's avatar
hjk committed
296

297
    DebuggerEngine *m_engine; // Not owned.
298
    DebuggerEngine *m_masterEngine; // Not owned
299 300
    DebuggerRunControl *m_runControl;  // Not owned.

301
    DebuggerRunParameters m_runParameters;
hjk's avatar
hjk committed
302 303

    // The current state.
304 305
    DebuggerState m_state;

hjk's avatar
hjk committed
306 307 308 309 310 311
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

312 313 314
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

315
    Terminal m_terminal;
316 317 318 319 320 321 322 323
    qint64 m_inferiorPid;

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

326 327
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
328
    QScopedPointer<TextMark> m_locationMark;
329
    QTimer m_locationTimer;
330 331

    bool m_isStateDebugging;
332

333
    Utils::FileInProjectFinder m_fileFinder;
334 335
    QHash<QByteArray, TypeInfo> m_typeInfoCache;
    QByteArray m_qtNamespace;
336 337 338 339 340 341 342 343 344
};


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

345
DebuggerEngine::DebuggerEngine(const DebuggerRunParameters &startParameters)
346
  : d(new DebuggerEnginePrivate(this, startParameters))
347
{}
348 349 350

DebuggerEngine::~DebuggerEngine()
{
351 352
    disconnect();
    delete d;
353 354
}

355 356 357 358 359 360 361 362 363 364 365
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)
366
        SN(InferiorSetupOk)
367 368 369 370 371 372 373 374
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
375
        SN(InferiorExitOk)
376 377 378 379 380 381 382 383 384 385 386 387
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

388
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
389
{
390
    showMessage(msg, StatusBar, timeout);
391
}
392

393 394 395 396 397 398 399 400 401 402 403 404
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));
}

405 406 407 408 409
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

410 411
ModulesHandler *DebuggerEngine::modulesHandler() const
{
412 413 414
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
415 416 417 418
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
419 420 421
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
422 423 424 425
}

StackHandler *DebuggerEngine::stackHandler() const
{
Aurindam Jana's avatar
Aurindam Jana committed
426 427 428
    return d->m_masterEngine
        ? d->m_masterEngine->stackHandler()
        : &d->m_stackHandler;
429 430 431 432
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
433 434 435
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
436 437 438 439
}

WatchHandler *DebuggerEngine::watchHandler() const
{
440 441 442
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
443 444 445 446
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
447 448 449
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
450 451 452 453
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
454
   return modulesHandler()->model();
455 456 457 458
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
459
    return registerHandler()->model();
460 461 462 463
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
464
    return stackHandler()->model();
465 466 467 468
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
469
    return threadsHandler()->model();
470 471
}

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

477 478
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
479
    return sourceFilesHandler()->model();
480 481
}

482
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
483 484 485 486 487 488
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
489 490 491 492 493 494 495
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

hjk's avatar
hjk committed
496
void DebuggerEngine::setRegisterValue(const QByteArray &name, const QString &value)
497
{
hjk's avatar
hjk committed
498
    Q_UNUSED(name);
499 500 501 502 503
    Q_UNUSED(value);
}

void DebuggerEngine::showMessage(const QString &msg, int channel, int timeout) const
{
504 505 506 507
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
508 509
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
510
    QmlJS::ConsoleManagerInterface *consoleManager = QmlJS::ConsoleManagerInterface::instance();
511
    if (channel == ConsoleOutput && consoleManager)
512
        consoleManager->printToConsolePane(QmlJS::ConsoleItem::UndefinedType, msg);
513

514
    Internal::showMessage(msg, channel, timeout);
515 516 517 518 519 520 521 522 523 524 525 526 527
    if (d->m_runControl) {
        switch (channel) {
            case AppOutput:
                d->m_runControl->appendMessage(msg, Utils::StdOutFormatSameLine);
                break;
            case AppError:
                d->m_runControl->appendMessage(msg, Utils::StdErrFormatSameLine);
                break;
            case AppStuff:
                d->m_runControl->appendMessage(msg, Utils::DebugFormat);
                break;
        }
    } else {
528
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
529
    }
530 531 532 533
}

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

537
    d->m_progress.setProgressRange(0, 1000);
538
    FutureProgress *fp = ProgressManager::addTask(d->m_progress.future(),
539
        tr("Launching Debugger"), "Debugger.Launcher");
hjk's avatar
hjk committed
540
    connect(fp, &FutureProgress::canceled, this, &DebuggerEngine::quitDebugger);
541
    fp->setKeepOnFinish(FutureProgress::HideOnFinish);
542 543
    d->m_progress.reportStarted();

544 545
    d->m_runControl = runControl;

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

551 552
    if (!d->m_runParameters.environment.size())
        d->m_runParameters.environment = Utils::Environment();
553

554 555 556
    if (isNativeMixedActive())
        d->m_runParameters.environment.set(QLatin1String("QV4_FORCE_INTERPRETER"), QLatin1String("1"));

hjk's avatar
hjk committed
557
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
558

559 560
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
561 562
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
563
    d->m_progress.setProgressValue(200);
564 565 566 567 568 569 570 571 572 573 574 575 576 577

    d->m_terminal.setup();
    if (d->m_terminal.isUsable()) {
        connect(&d->m_terminal, &Terminal::stdOutReady, [this, runControl](const QString &msg) {
            runControl->appendMessage(msg, Utils::StdOutFormatSameLine);
        });
        connect(&d->m_terminal, &Terminal::stdErrReady, [this, runControl](const QString &msg) {
            runControl->appendMessage(msg, Utils::StdErrFormatSameLine);
        });
        connect(&d->m_terminal, &Terminal::error, [this, runControl](const QString &msg) {
            runControl->appendMessage(msg, Utils::ErrorMessageFormat);
        });
    }

578
    d->queueSetupEngine();
579 580 581 582
}

void DebuggerEngine::resetLocation()
{
583 584
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
585 586
}

587
void DebuggerEngine::gotoLocation(const Location &loc)
588
{
Aurindam Jana's avatar
Aurindam Jana committed
589 590
     d->resetLocation();

591 592 593 594
    if (loc.canBeDisassembled()
            && ((hasCapability(OperateByInstructionCapability) && boolSetting(OperateByInstruction))
                || !loc.hasDebugInfo()) )
    {
595 596 597
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
598

599 600 601 602
    if (loc.fileName().isEmpty()) {
        showMessage(QLatin1String("CANNOT GO TO THIS LOCATION"));
        return;
    }
603
    const QString file = QDir::cleanPath(loc.fileName());
604
    const int line = loc.lineNumber();
605
    bool newEditor = false;
606
    IEditor *editor = EditorManager::openEditor(file, Id(),
607 608
                                                EditorManager::IgnoreNavigationHistory, &newEditor);
    QTC_ASSERT(editor, return); // Unreadable file?
609

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

612 613
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
614

615
    if (loc.needsMarker()) {
616
        d->m_locationMark.reset(new TextMark(file, line, Constants::TEXT_MARK_CATEGORY_LOCATION));
617
        d->m_locationMark->setIcon(Internal::locationMarkIcon());
618
        d->m_locationMark->setPriority(TextMark::HighPriority);
619
    }
620 621

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

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

634 635
// Called from RunControl.
void DebuggerEngine::handleFinished()
636
{
637
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
638
    d->m_runControl = 0;
639 640
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
641 642 643 644 645 646
    modulesHandler()->removeAll();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    watchHandler()->cleanup();
}

647
const DebuggerRunParameters &DebuggerEngine::runParameters() const
648
{
649
    return d->m_runParameters;
650 651
}

652
DebuggerRunParameters &DebuggerEngine::runParameters()
653
{
654
    return d->m_runParameters;
655 656 657 658 659 660 661
}

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

hjk's avatar
hjk committed
662
DebuggerState DebuggerEngine::lastGoodState() const
663
{
hjk's avatar
hjk committed
664 665 666 667 668 669 670
    return d->m_lastGoodState;
}

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

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

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

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

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

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

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

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

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

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

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

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

747 748
void DebuggerEngine::setupSlaveEngine()
{
749
    QTC_CHECK(state() == DebuggerNotReady);
750 751 752 753 754 755 756
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
757
    m_engine->validateExecutable(&m_runParameters);
758 759 760
    m_engine->setupEngine();
}

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

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

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

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

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

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

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

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

829 830 831
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
832