debuggerengine.cpp 58.3 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 98 99 100
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

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 113 114 115 116
}

QDebug operator<<(QDebug str, const DebuggerStartParameters &sp)
{
    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 DebuggerStartParameters &sp)
170
      : m_engine(engine),
171
        m_masterEngine(0),
172 173 174
        m_runControl(0),
        m_startParameters(sp),
        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"),
Orgad Shaneh's avatar
Orgad Shaneh committed
198
            [this]() { return m_startParameters.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 301
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
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 346
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters)
  : 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 546 547
    d->m_runControl = runControl;

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

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

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

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

    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);
        });
    }

575
    d->queueSetupEngine();
576 577 578 579
}

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

584
void DebuggerEngine::gotoLocation(const Location &loc)
585
{
Aurindam Jana's avatar
Aurindam Jana committed
586 587
     d->resetLocation();

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

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

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

609 610
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
611

612
    if (loc.needsMarker()) {
613
        d->m_locationMark.reset(new TextMark(file, line));
614
        d->m_locationMark->setIcon(Internal::locationMarkIcon());
615
        d->m_locationMark->setPriority(TextMark::HighPriority);
616
    }
617 618

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

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

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

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

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

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

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

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

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

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
Orgad Shaneh's avatar
Orgad Shaneh committed
713
            || to == InferiorStopOk || to == InferiorExitOk;
714
    case InferiorStopFailed:
hjk's avatar
hjk committed
715
        return to == EngineShutdownRequested;
716

717 718 719
    case InferiorExitOk:
        return to == InferiorShutdownOk;

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

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

    case DebuggerFinished:
737
        return to == EngineSetupRequested; // Happens on restart.
738 739
    }

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

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

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
754
    m_engine->validateExecutable(&m_startParameters);
755 756 757
    m_engine->setupEngine();
}

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

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

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

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

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

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

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

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

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

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

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