debuggerengine.cpp 58.9 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 153 154
void LocationMark::dragToLine(int line)
{
    if (m_engine) {
        if (BaseTextEditor *textEditor = BaseTextEditor::currentTextEditor())
            m_engine->executeJumpToLine(getLocationContext(textEditor->textDocument(), line));
hjk's avatar
hjk committed
155
    }
hjk's avatar
hjk committed
156
}
hjk's avatar
hjk committed
157

158 159 160 161 162 163
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

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

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

179
class DebuggerEnginePrivate : public QObject
180
{
181 182
    Q_OBJECT

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

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

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

225 226 227 228 229
    void reloadDisassembly()
    {
        m_disassemblerAgent.reload();
    }

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

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

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

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

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

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

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

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

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

312
    DebuggerEngine *m_engine; // Not owned.
313
    DebuggerEngine *m_masterEngine; // Not owned
314 315
    DebuggerRunControl *m_runControl;  // Not owned.

316
    DebuggerRunParameters m_runParameters;
hjk's avatar
hjk committed
317 318

    // The current state.
319 320
    DebuggerState m_state;

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

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

327 328 329
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

330
    Terminal m_terminal;
331 332 333 334 335 336 337 338
    qint64 m_inferiorPid;

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

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

    bool m_isStateDebugging;
347

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


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

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

DebuggerEngine::~DebuggerEngine()
{
366 367
    disconnect();
    delete d;
368 369
}

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

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

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

419 420 421 422
void DebuggerEngine::doUpdateLocals(const UpdateParameters &)
{
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

562 563
    d->m_runControl = runControl;

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

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

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

hjk's avatar
hjk committed
575
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
576

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

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

596
    d->queueSetupEngine();
597 598 599 600
}

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

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

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

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

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

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

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

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

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

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

662
const DebuggerRunParameters &DebuggerEngine::runParameters() const
663
{
664
    return d->m_runParameters;
665 666
}

667
DebuggerRunParameters &DebuggerEngine::runParameters()
668
{
669
    return d->m_runParameters;
670 671 672 673 674 675 676
}

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

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

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

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

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

hjk's avatar
hjk committed
702
    case InferiorSetupRequested:
703
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
704
    case InferiorSetupFailed:
hjk's avatar
hjk committed
705
        return to == EngineShutdownRequested;
706 707
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
708 709

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

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

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
731
            || to == InferiorStopOk || to == InferiorShutdownOk;
732
    case InferiorStopFailed:
hjk's avatar
hjk committed
733
        return to == EngineShutdownRequested;
734 735

    case InferiorUnrunnable:
hjk's avatar
hjk committed
736 737 738 739 740
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
741
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
742
        return to == EngineShutdownRequested;
743

hjk's avatar
hjk committed
744
    case EngineShutdownRequested:
745
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
746 747 748 749 750 751
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
752
        return to == EngineSetupRequested; // Happens on restart.
753 754
    }

755
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
756 757 758
    return false;
}

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

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

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

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

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

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

void DebuggerEngine::setupSlaveInferior()
{
806
    QTC_CHECK(state() == EngineSetupOk);
807
    d->queueSetupInferior();
808 809
}

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

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

void DebuggerEngine::notifyInferiorSetupOk()
{
830 831 832
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
833
    aboutToNotifyInferiorSetupOk();
hjk's avatar
hjk committed
834
    showMessage(_("NOTE: INFERIOR SETUP OK"));
hjk's avatar