debuggerengine.cpp 59 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

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

98 99
namespace Debugger {

100 101
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
102 103
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
104 105
}

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

hjk's avatar
hjk committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135
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;
}

136

hjk's avatar
hjk committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
class LocationMark : public TextMark
{
public:
    LocationMark(DebuggerEngine *engine, const QString &file, int line)
        : TextMark(file, line, Constants::TEXT_MARK_CATEGORY_LOCATION), m_engine(engine)
    {}

private:
    bool isDraggable() const { return true; }

    void dragToLine(int line)
    {
        if (m_engine) {
            ContextData data;
            data.fileName = fileName();
            data.lineNumber = line;
            m_engine->executeJumpToLine(data);
        }
    }

    QPointer<DebuggerEngine> m_engine;
};

160 161 162 163 164 165
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

166 167 168 169 170 171 172 173 174
// transitions:
//   None->Requested
//   Requested->Succeeded
//   Requested->Failed
//   Requested->Cancelled
enum RemoteSetupState { RemoteSetupNone, RemoteSetupRequested,
                        RemoteSetupSucceeded, RemoteSetupFailed,
                        RemoteSetupCancelled };

175 176 177 178 179 180
struct TypeInfo
{
    TypeInfo(uint s = 0) : size(s) {}
    uint size;
};

181
class DebuggerEnginePrivate : public QObject
182
{
183 184
    Q_OBJECT

185
public:
186
    DebuggerEnginePrivate(DebuggerEngine *engine, const DebuggerRunParameters &sp)
187
      : m_engine(engine),
188
        m_masterEngine(0),
189
        m_runControl(0),
190
        m_runParameters(sp),
191
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
192
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
193
        m_targetState(DebuggerNotReady),
194
        m_remoteSetupState(RemoteSetupNone),
195
        m_inferiorPid(0),
196
        m_modulesHandler(engine),
197
        m_registerHandler(),
198 199
        m_sourceFilesHandler(),
        m_stackHandler(),
hjk's avatar
hjk committed
200
        m_threadsHandler(),
201
        m_watchHandler(engine),
202
        m_disassemblerAgent(engine),
203
        m_memoryAgent(engine),
204
        m_isStateDebugging(false)
205
    {
hjk's avatar
hjk committed
206 207 208 209
        connect(&m_locationTimer, &QTimer::timeout,
                this, &DebuggerEnginePrivate::resetLocation);
        connect(action(IntelFlavor), &Utils::SavedAction::valueChanged,
                this, &DebuggerEnginePrivate::reloadDisassembly);
210 211 212
        connect(action(OperateNativeMixed), &QAction::triggered,
                engine, &DebuggerEngine::reloadFullStack);

hjk's avatar
hjk committed
213
        Utils::globalMacroExpander()->registerFileVariables(PrefixDebugExecutable,
214
            tr("Debugged executable"),
215
            [this]() { return m_runParameters.executable; });
216
    }
217

218
public slots:
219
    void doSetupEngine();
hjk's avatar
hjk committed
220 221
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
222 223
    void doShutdownEngine();
    void doShutdownInferior();
224
    void doInterruptInferior();
hjk's avatar
hjk committed
225
    void doFinishDebugger();
hjk's avatar
hjk committed
226

227 228 229 230 231
    void reloadDisassembly()
    {
        m_disassemblerAgent.reload();
    }

232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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()));
    }

246 247
    void queueRunEngine()
    {
hjk's avatar
hjk committed
248 249 250 251 252
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

253 254
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
255 256 257 258 259
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

260 261
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
262 263 264 265 266
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

267 268
    void queueFinishDebugger()
    {
269 270 271
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
272 273 274 275 276
        resetLocation();
        if (isMasterEngine()) {
            m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
            QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
        }
hjk's avatar
hjk committed
277 278
    }

279 280
    void raiseApplication()
    {
281 282
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
283 284
    }

285
    void scheduleResetLocation()
286
    {
287
        m_stackHandler.scheduleResetLocation();
288
        m_watchHandler.scheduleResetLocation();
289
        m_threadsHandler.scheduleResetLocation();
290
        m_disassemblerAgent.scheduleResetLocation();
291 292 293 294
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

295
    void resetLocation()
296 297 298
    {
        m_locationTimer.stop();
        m_locationMark.reset();
299
        m_stackHandler.resetLocation();
300
        m_watchHandler.resetLocation();
301
        m_threadsHandler.resetLocation();
302
        m_disassemblerAgent.resetLocation();
hjk's avatar
hjk committed
303
        DebuggerToolTipManager::resetLocation();
304 305
    }

306
public:
hjk's avatar
hjk committed
307
    DebuggerState state() const { return m_state; }
308
    RemoteSetupState remoteSetupState() const { return m_remoteSetupState; }
309 310 311
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
312
    void setRemoteSetupState(RemoteSetupState state);
hjk's avatar
hjk committed
313

314
    DebuggerEngine *m_engine; // Not owned.
315
    DebuggerEngine *m_masterEngine; // Not owned
316 317
    DebuggerRunControl *m_runControl;  // Not owned.

318
    DebuggerRunParameters m_runParameters;
hjk's avatar
hjk committed
319 320

    // The current state.
321 322
    DebuggerState m_state;

hjk's avatar
hjk committed
323 324 325 326 327 328
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

329 330 331
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

332
    Terminal m_terminal;
333 334 335 336 337 338 339 340
    qint64 m_inferiorPid;

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

343 344
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
hjk's avatar
hjk committed
345
    QScopedPointer<LocationMark> m_locationMark;
346
    QTimer m_locationTimer;
347 348

    bool m_isStateDebugging;
349

350
    Utils::FileInProjectFinder m_fileFinder;
351 352
    QHash<QByteArray, TypeInfo> m_typeInfoCache;
    QByteArray m_qtNamespace;
353 354 355 356 357 358 359 360 361
};


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

362
DebuggerEngine::DebuggerEngine(const DebuggerRunParameters &startParameters)
363
  : d(new DebuggerEnginePrivate(this, startParameters))
364
{}
365 366 367

DebuggerEngine::~DebuggerEngine()
{
368 369
    disconnect();
    delete d;
370 371
}

372 373 374 375 376 377 378 379 380 381 382
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)
383
        SN(InferiorSetupOk)
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

404
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
405
{
406
    showMessage(msg, StatusBar, timeout);
407
}
408

409 410 411 412 413 414 415 416 417 418 419 420
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));
}

421 422 423 424
void DebuggerEngine::doUpdateLocals(const UpdateParameters &)
{
}

425 426 427 428 429
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

430 431
ModulesHandler *DebuggerEngine::modulesHandler() const
{
432 433 434
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
435 436 437 438
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
439 440 441
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
442 443 444 445
}

StackHandler *DebuggerEngine::stackHandler() const
{
Aurindam Jana's avatar
Aurindam Jana committed
446 447 448
    return d->m_masterEngine
        ? d->m_masterEngine->stackHandler()
        : &d->m_stackHandler;
449 450 451 452
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
453 454 455
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
456 457 458 459
}

WatchHandler *DebuggerEngine::watchHandler() const
{
460 461 462
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
463 464 465 466
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
467 468 469
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
470 471 472 473
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
474
   return modulesHandler()->model();
475 476 477 478
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
479
    return registerHandler()->model();
480 481 482 483
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
484
    return stackHandler()->model();
485 486 487 488
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
489
    return threadsHandler()->model();
490 491
}

hjk's avatar
hjk committed
492 493 494
QAbstractItemModel *DebuggerEngine::watchModel() const
{
    return watchHandler()->model();
495 496
}

497 498
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
499
    return sourceFilesHandler()->model();
500 501
}

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

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

hjk's avatar
hjk committed
516
void DebuggerEngine::setRegisterValue(const QByteArray &name, const QString &value)
517
{
hjk's avatar
hjk committed
518
    Q_UNUSED(name);
519 520 521 522 523
    Q_UNUSED(value);
}

void DebuggerEngine::showMessage(const QString &msg, int channel, int timeout) const
{
524 525 526 527
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
528 529
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
530
    QmlJS::ConsoleManagerInterface *consoleManager = QmlJS::ConsoleManagerInterface::instance();
531
    if (channel == ConsoleOutput && consoleManager)
532
        consoleManager->printToConsolePane(QmlJS::ConsoleItem::UndefinedType, msg);
533

534
    Internal::showMessage(msg, channel, timeout);
535 536 537 538 539 540 541 542 543 544 545 546 547
    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 {
548
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
549
    }
550 551 552 553
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
554 555
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
556

557
    d->m_progress.setProgressRange(0, 1000);
558
    FutureProgress *fp = ProgressManager::addTask(d->m_progress.future(),
559
        tr("Launching Debugger"), "Debugger.Launcher");
hjk's avatar
hjk committed
560
    connect(fp, &FutureProgress::canceled, this, &DebuggerEngine::quitDebugger);
561
    fp->setKeepOnFinish(FutureProgress::HideOnFinish);
562 563
    d->m_progress.reportStarted();

564 565
    d->m_runControl = runControl;

566 567
    d->m_inferiorPid = d->m_runParameters.attachPID > 0
        ? d->m_runParameters.attachPID : 0;
568 569
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
570

571 572
    if (!d->m_runParameters.environment.size())
        d->m_runParameters.environment = Utils::Environment();
573

574 575 576
    if (isNativeMixedActive())
        d->m_runParameters.environment.set(QLatin1String("QV4_FORCE_INTERPRETER"), QLatin1String("1"));

hjk's avatar
hjk committed
577
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
578

579 580
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
581 582
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
583
    d->m_progress.setProgressValue(200);
584 585 586 587 588 589 590 591 592 593 594 595 596 597

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

598
    d->queueSetupEngine();
599 600 601 602
}

void DebuggerEngine::resetLocation()
{
603 604
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
605 606
}

607
void DebuggerEngine::gotoLocation(const Location &loc)
608
{
Aurindam Jana's avatar
Aurindam Jana committed
609 610
     d->resetLocation();

611 612 613 614
    if (loc.canBeDisassembled()
            && ((hasCapability(OperateByInstructionCapability) && boolSetting(OperateByInstruction))
                || !loc.hasDebugInfo()) )
    {
615 616 617
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
618

619 620 621 622
    if (loc.fileName().isEmpty()) {
        showMessage(QLatin1String("CANNOT GO TO THIS LOCATION"));
        return;
    }
623
    const QString file = QDir::cleanPath(loc.fileName());
624
    const int line = loc.lineNumber();
625
    bool newEditor = false;
626
    IEditor *editor = EditorManager::openEditor(file, Id(),
627 628
                                                EditorManager::IgnoreNavigationHistory, &newEditor);
    QTC_ASSERT(editor, return); // Unreadable file?
629

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

632 633
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
634

635
    if (loc.needsMarker()) {
hjk's avatar
hjk committed
636
        d->m_locationMark.reset(new LocationMark(this, file, line));
637
        d->m_locationMark->setIcon(Internal::locationMarkIcon());
638
        d->m_locationMark->setPriority(TextMark::HighPriority);
639
    }
640 641

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

644 645 646
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
647
    showMessage(QLatin1String("HANDLE RUNCONTROL START FAILED"));
648
    d->m_runControl = 0;
649
    d->m_progress.setProgressValue(900);
650 651
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
652 653
}

654 655
// Called from RunControl.
void DebuggerEngine::handleFinished()
656
{
657
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
658
    d->m_runControl = 0;
659 660
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
661 662 663 664 665 666
    modulesHandler()->removeAll();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    watchHandler()->cleanup();
}

667
const DebuggerRunParameters &DebuggerEngine::runParameters() const
668
{
669
    return d->m_runParameters;
670 671
}

672
DebuggerRunParameters &DebuggerEngine::runParameters()
673
{
674
    return d->m_runParameters;
675 676 677 678 679 680 681
}

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

hjk's avatar
hjk committed
682
DebuggerState DebuggerEngine::lastGoodState() const
683
{
hjk's avatar
hjk committed
684 685 686 687 688 689 690
    return d->m_lastGoodState;
}

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

hjk's avatar
hjk committed
692 693 694
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
695
    case DebuggerNotReady:
696
        return to == EngineSetupRequested;
697

hjk's avatar
hjk committed
698
    case EngineSetupRequested:
699 700
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
701 702 703
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
704
    case EngineSetupOk:
hjk's avatar
hjk committed
705
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
706

hjk's avatar
hjk committed
707
    case InferiorSetupRequested:
708
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
709
    case InferiorSetupFailed:
hjk's avatar
hjk committed
710
        return to == EngineShutdownRequested;
711 712
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
713 714

    case EngineRunRequested:
hjk's avatar
hjk committed
715
        return to == EngineRunFailed
716
            || to == InferiorRunRequested
hjk's avatar
hjk committed
717 718 719
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
720
    case EngineRunFailed:
721
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
722 723 724 725 726 727

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
728
        return to == InferiorStopRequested
729 730
            || to == InferiorStopOk       // A spontaneous stop.
            || to == InferiorShutdownOk;  // A spontaneous exit.
hjk's avatar
hjk committed
731 732 733 734 735

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
736
            || to == InferiorStopOk || to == InferiorShutdownOk;
737
    case InferiorStopFailed:
hjk's avatar
hjk committed
738
        return to == EngineShutdownRequested;
739 740

    case InferiorUnrunnable:
hjk's avatar
hjk committed
741 742 743 744 745
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
746
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
747
        return to == EngineShutdownRequested;
748

hjk's avatar
hjk committed
749
    case EngineShutdownRequested:
750
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
751 752 753 754 755 756
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
757
        return to == EngineSetupRequested; // Happens on restart.
758 759
    }

760
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
761 762 763
    return false;
}

764 765
void DebuggerEngine::setupSlaveEngine()
{
766
    QTC_CHECK(state() == DebuggerNotReady);
767 768 769 770 771 772 773
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
774
    m_engine->validateExecutable(&m_runParameters);
775 776 777
    m_engine->setupEngine();
}

778
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
779
{
hjk's avatar
hjk committed
780
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
781 782 783 784 785 786 787
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());
    if (d->remoteSetupState() == RemoteSetupRequested)
        d->setRemoteSetupState(RemoteSetupCancelled);

788
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
789
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
790
    if (isMasterEngine() && runControl())
791
        runControl()->startFailed();
hjk's avatar
hjk committed
792
    setState(DebuggerFinished);
hjk's avatar
hjk committed
793 794
}

795
void DebuggerEngine::notifyEngineSetupOk()
796
{
hjk's avatar
hjk committed
797
    showMessage(_("NOTE: ENGINE SETUP OK"));
798 799 800 801
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

802
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
803
    setState(EngineSetupOk);
hjk's avatar
hjk committed
804
    showMessage(_("QUEUE: SETUP INFERIOR"));
805
    if (isMasterEngine())
806 807 808 809 810
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
811
    QTC_CHECK(state() == EngineSetupOk);
812
    d->queueSetupInferior();
813 814
}

hjk's avatar
hjk committed
815
void DebuggerEnginePrivate::doSetupInferior()
816
{
817
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
818
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
819
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
820 821 822 823 824
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
825
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
826
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
827
    showStatusMessage(tr("Setup failed."));
hjk's avatar
hjk committed
828
    setState(InferiorSetupFailed);
829 830
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
831 832 833 834
}

void DebuggerEngine::notifyInferiorSetupOk()
{
835 836 837
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
838
    aboutToNotifyInferiorSetupOk();
hjk's avatar
hjk committed
839
    showMessage(_("NOTE: INFERIOR SETUP OK"));
840 841 842 843
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
844 845
}

846 847 848
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);