debuggerengine.cpp 60.2 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"
hjk's avatar
hjk committed
54 55
#include "debugger/shared/peutils.h"
#include "console/console.h"
56

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

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

68
#include <texteditor/texteditor.h>
69

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

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

80 81 82 83 84
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

hjk's avatar
hjk committed
85
using namespace Core;
86
using namespace Debugger::Internal;
hjk's avatar
hjk committed
87 88
using namespace ProjectExplorer;
using namespace TextEditor;
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
            << " inferior environment=<" << sp.inferiorEnvironment.size() << " variables>"
            << " debugger environment=<" << sp.debuggerEnvironment.size() << " variables>"
114 115 116 117 118
            << " 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
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;
134
    m_from = frame.module;
hjk's avatar
hjk committed
135 136
}

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),
hjk's avatar
hjk committed
198
        m_registerHandler(engine),
199
        m_sourceFilesHandler(),
200
        m_stackHandler(engine),
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // The current state.
320 321
    DebuggerState m_state;

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

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

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

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

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

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

    bool m_isStateDebugging;
348

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

void DebuggerEngine::showMessage(const QString &msg, int channel, int timeout) const
{
523 524 525 526
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
527 528
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
hjk's avatar
hjk committed
529 530
    if (channel == ConsoleOutput)
        debuggerConsole()->printItem(ConsoleItem::DefaultType, 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
    if (isNativeMixedActive())
570
        d->m_runParameters.inferiorEnvironment.set(QLatin1String("QV4_FORCE_INTERPRETER"), QLatin1String("1"));
571

hjk's avatar
hjk committed
572
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
573

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

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

593
    d->queueSetupEngine();
594 595 596 597
}

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

602
void DebuggerEngine::gotoLocation(const Location &loc)
603
{
Aurindam Jana's avatar
Aurindam Jana committed
604 605
     d->resetLocation();

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

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

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

627 628
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
629

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

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

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

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

659
const DebuggerRunParameters &DebuggerEngine::runParameters() const
660
{
661
    return d->m_runParameters;
662 663
}

664
DebuggerRunParameters &DebuggerEngine::runParameters()
665
{
666
    return d->m_runParameters;
667 668 669 670 671 672 673
}

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

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

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

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

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

hjk's avatar
hjk committed
699
    case InferiorSetupRequested:
700
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
701
    case InferiorSetupFailed:
hjk's avatar
hjk committed
702
        return to == EngineShutdownRequested;
703 704
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
705 706

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

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

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
728
            || to == InferiorStopOk || to == InferiorShutdownOk;
729
    case InferiorStopFailed:
hjk's avatar
hjk committed
730
        return to == EngineShutdownRequested;
731 732

    case InferiorUnrunnable:
hjk's avatar
hjk committed
733 734 735 736 737
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
738
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
739
        return to == EngineShutdownRequested;
740

hjk's avatar
hjk committed
741
    case EngineShutdownRequested:
742
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
743 744 745 746 747 748
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
749
        return to == EngineSetupRequested; // Happens on restart.
750 751
    }

752
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
753 754 755
    return false;
}

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

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

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

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

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

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

void DebuggerEngine::setupSlaveInferior()
{
803
    QTC_CHECK(state() == EngineSetupOk);
804
    d->queueSetupInferior();
805 806
}

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

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

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