debuggerengine.cpp 59.1 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
#include "registerhandler.h"
#include "sourcefileshandler.h"
hjk's avatar
hjk committed
49
#include "sourceutils.h"
50
#include "stackhandler.h"
51
#include "terminal.h"
52 53
#include "threadshandler.h"
#include "watchhandler.h"
54
#include <debugger/shared/peutils.h>
55

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

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

67
#include <texteditor/texteditor.h>
68

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

74
#include <qmljs/consolemanagerinterface.h>
75

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

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

86 87 88 89
enum { debug = 0 };

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

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

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

99 100
namespace Debugger {

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

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

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

137

hjk's avatar
hjk committed
138 139
LocationMark::LocationMark(DebuggerEngine *engine, const QString &file, int line)
    : TextMark(file, line, Constants::TEXT_MARK_CATEGORY_LOCATION), m_engine(engine)
hjk's avatar
hjk committed
140
{
hjk's avatar
hjk committed
141 142 143
    setIcon(Internal::locationMarkIcon());
    setPriority(TextMark::HighPriority);
}
hjk's avatar
hjk committed
144

hjk's avatar
hjk committed
145 146 147 148
bool LocationMark::isDraggable() const
{
    return m_engine->hasCapability(JumpToLineCapability);
}
hjk's avatar
hjk committed
149

hjk's avatar
hjk committed
150 151 152
void LocationMark::dragToLine(int line)
{
    if (m_engine) {
153 154 155 156 157
        if (BaseTextEditor *textEditor = BaseTextEditor::currentTextEditor()) {
            ContextData location = getLocationContext(textEditor->textDocument(), line);
            if (location.isValid())
                m_engine->executeJumpToLine(location);
        }
hjk's avatar
hjk committed
158
    }
hjk's avatar
hjk committed
159
}
hjk's avatar
hjk committed
160

161 162 163 164 165 166
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

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

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

182
class DebuggerEnginePrivate : public QObject
183
{
184 185
    Q_OBJECT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // The current state.
322 323
    DebuggerState m_state;

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

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

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

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

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

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

    bool m_isStateDebugging;
350

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


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

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

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

373 374 375 376 377 378 379 380 381 382 383
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)
384
        SN(InferiorSetupOk)
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
        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
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

565 566
    d->m_runControl = runControl;

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

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

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

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

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

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

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

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

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

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

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

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

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

hjk's avatar
hjk committed
636
    if (loc.needsMarker())
hjk's avatar
hjk committed
637
        d->m_locationMark.reset(new LocationMark(this, file, line));
638 639

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    case DebuggerFinished:
755
        return to == EngineSetupRequested; // Happens on restart.
756 757
    }

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

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

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

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

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

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

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

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

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

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

void DebuggerEngine::notifyInferiorSetupOk()
{
833 834 835
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed