debuggerengine.cpp 61.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25 26 27

#include "debuggerengine.h"

28
#include "debuggerinternalconstants.h"
29
#include "debuggeractions.h"
30
#include "debuggercore.h"
31
#include "debuggericons.h"
32
#include "debuggerruncontrol.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
33
#include "debuggerstartparameters.h"
34
#include "debuggertooltipmanager.h"
35 36

#include "breakhandler.h"
David Schulz's avatar
David Schulz committed
37
#include "disassembleragent.h"
38
#include "logwindow.h"
David Schulz's avatar
David Schulz committed
39
#include "memoryagent.h"
40 41 42
#include "moduleshandler.h"
#include "registerhandler.h"
#include "sourcefileshandler.h"
hjk's avatar
hjk committed
43
#include "sourceutils.h"
44
#include "stackhandler.h"
45
#include "terminal.h"
46 47
#include "threadshandler.h"
#include "watchhandler.h"
hjk's avatar
hjk committed
48 49
#include "debugger/shared/peutils.h"
#include "console/console.h"
50

hjk's avatar
hjk committed
51
#include <coreplugin/editormanager/editormanager.h>
52
#include <coreplugin/editormanager/ieditor.h>
hjk's avatar
hjk committed
53
#include <coreplugin/icore.h>
54
#include <coreplugin/idocument.h>
55
#include <coreplugin/messagebox.h>
56 57
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
hjk's avatar
hjk committed
58

hjk's avatar
hjk committed
59
#include <projectexplorer/projectexplorer.h>
60 61
#include <projectexplorer/taskhub.h>

62
#include <texteditor/texteditor.h>
63

64
#include <utils/fileinprojectfinder.h>
hjk's avatar
hjk committed
65 66 67
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
68

69 70 71
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
72
#include <QDir>
73

74 75 76 77 78
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

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

84 85 86 87
//#define WITH_BENCHMARK
#ifdef WITH_BENCHMARK
#include <valgrind/callgrind.h>
#endif
88

hjk's avatar
hjk committed
89 90 91
// VariableManager Prefix
const char PrefixDebugExecutable[]  = "DebuggedExecutable";

92 93
namespace Debugger {

94 95
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
96 97
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
98 99
}

100
QDebug operator<<(QDebug str, const DebuggerRunParameters &sp)
101 102
{
    QDebug nospace = str.nospace();
103
    nospace << "executable=" << sp.inferior.executable
104
            << " coreFile=" << sp.coreFile
105 106
            << " processArgs=" << sp.inferior.commandLineArguments
            << " inferior environment=<" << sp.inferior.environment.size() << " variables>"
107
            << " debugger environment=<" << sp.debuggerEnvironment.size() << " variables>"
108
            << " workingDir=" << sp.inferior.workingDirectory
109 110 111 112
            << " attachPID=" << sp.attachPID
            << " useTerminal=" << sp.useTerminal
            << " remoteChannel=" << sp.remoteChannel
            << " serverStartScript=" << sp.serverStartScript
113
            << " abi=" << sp.toolChainAbi.toString() << '\n';
114 115 116
    return str;
}

hjk's avatar
hjk committed
117 118 119 120 121 122 123 124 125 126 127
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;
128
    m_from = frame.module;
hjk's avatar
hjk committed
129 130
}

131

hjk's avatar
hjk committed
132 133
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
134
{
135
    setIcon(Icons::LOCATION.icon());
hjk's avatar
hjk committed
136 137
    setPriority(TextMark::HighPriority);
}
hjk's avatar
hjk committed
138

hjk's avatar
hjk committed
139 140 141 142
bool LocationMark::isDraggable() const
{
    return m_engine->hasCapability(JumpToLineCapability);
}
hjk's avatar
hjk committed
143

hjk's avatar
hjk committed
144 145 146
void LocationMark::dragToLine(int line)
{
    if (m_engine) {
147 148 149 150 151
        if (BaseTextEditor *textEditor = BaseTextEditor::currentTextEditor()) {
            ContextData location = getLocationContext(textEditor->textDocument(), line);
            if (location.isValid())
                m_engine->executeJumpToLine(location);
        }
hjk's avatar
hjk committed
152
    }
hjk's avatar
hjk committed
153
}
hjk's avatar
hjk committed
154

155 156 157 158 159 160
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

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

170
class DebuggerEnginePrivate : public QObject
171
{
172 173
    Q_OBJECT

174
public:
175
    DebuggerEnginePrivate(DebuggerEngine *engine, const DebuggerRunParameters &sp)
176
      : m_engine(engine),
177
        m_masterEngine(0),
178
        m_runControl(0),
179
        m_runParameters(sp),
180
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
181
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
182
        m_targetState(DebuggerNotReady),
183
        m_remoteSetupState(RemoteSetupNone),
184
        m_inferiorPid(0),
185
        m_modulesHandler(engine),
hjk's avatar
hjk committed
186
        m_registerHandler(engine),
187
        m_sourceFilesHandler(),
188
        m_stackHandler(engine),
hjk's avatar
hjk committed
189
        m_threadsHandler(),
190
        m_watchHandler(engine),
191
        m_disassemblerAgent(engine),
192
        m_memoryAgent(engine),
193
        m_isStateDebugging(false)
194
    {
hjk's avatar
hjk committed
195 196 197 198
        connect(&m_locationTimer, &QTimer::timeout,
                this, &DebuggerEnginePrivate::resetLocation);
        connect(action(IntelFlavor), &Utils::SavedAction::valueChanged,
                this, &DebuggerEnginePrivate::reloadDisassembly);
199

hjk's avatar
hjk committed
200
        Utils::globalMacroExpander()->registerFileVariables(PrefixDebugExecutable,
201
            tr("Debugged executable"),
hjk's avatar
hjk committed
202
            [this] { return m_runParameters.inferior.executable; });
203
    }
204

205
    void doSetupEngine();
hjk's avatar
hjk committed
206 207
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
208 209
    void doShutdownEngine();
    void doShutdownInferior();
210
    void doInterruptInferior();
hjk's avatar
hjk committed
211
    void doFinishDebugger();
hjk's avatar
hjk committed
212

213 214 215 216 217
    void reloadDisassembly()
    {
        m_disassemblerAgent.reload();
    }

218 219 220
    void queueSetupEngine()
    {
        m_engine->setState(EngineSetupRequested);
hjk's avatar
hjk committed
221 222
        m_engine->showMessage("QUEUE: SETUP ENGINE");
        QTimer::singleShot(0, this, &DebuggerEnginePrivate::doSetupEngine);
223 224 225 226 227
    }

    void queueSetupInferior()
    {
        m_engine->setState(InferiorSetupRequested);
hjk's avatar
hjk committed
228 229
        m_engine->showMessage("QUEUE: SETUP INFERIOR");
        QTimer::singleShot(0, this, &DebuggerEnginePrivate::doSetupInferior);
230 231
    }

232 233
    void queueRunEngine()
    {
hjk's avatar
hjk committed
234
        m_engine->setState(EngineRunRequested);
hjk's avatar
hjk committed
235 236
        m_engine->showMessage("QUEUE: RUN ENGINE");
        QTimer::singleShot(0, this, &DebuggerEnginePrivate::doRunEngine);
hjk's avatar
hjk committed
237 238
    }

239 240
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
241
        m_engine->setState(EngineShutdownRequested);
hjk's avatar
hjk committed
242 243
        m_engine->showMessage("QUEUE: SHUTDOWN ENGINE");
        QTimer::singleShot(0, this, &DebuggerEnginePrivate::doShutdownEngine);
hjk's avatar
hjk committed
244 245
    }

246 247
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
248
        m_engine->setState(InferiorShutdownRequested);
hjk's avatar
hjk committed
249 250
        m_engine->showMessage("QUEUE: SHUTDOWN INFERIOR");
        QTimer::singleShot(0, this, &DebuggerEnginePrivate::doShutdownInferior);
hjk's avatar
hjk committed
251 252
    }

253 254
    void queueFinishDebugger()
    {
255 256 257
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
258 259
        resetLocation();
        if (isMasterEngine()) {
hjk's avatar
hjk committed
260 261
            m_engine->showMessage("QUEUE: FINISH DEBUGGER");
            QTimer::singleShot(0, this, &DebuggerEnginePrivate::doFinishDebugger);
262
        }
hjk's avatar
hjk committed
263 264
    }

265 266
    void raiseApplication()
    {
267 268
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
269 270
    }

271
    void scheduleResetLocation()
272
    {
273
        m_stackHandler.scheduleResetLocation();
274
        m_watchHandler.scheduleResetLocation();
275
        m_threadsHandler.scheduleResetLocation();
276
        m_disassemblerAgent.scheduleResetLocation();
277 278 279 280
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

281
    void resetLocation()
282
    {
283
        m_lookupRequests.clear();
284 285
        m_locationTimer.stop();
        m_locationMark.reset();
286
        m_stackHandler.resetLocation();
287
        m_watchHandler.resetLocation();
288
        m_threadsHandler.resetLocation();
289
        m_disassemblerAgent.resetLocation();
hjk's avatar
hjk committed
290
        DebuggerToolTipManager::resetLocation();
291 292
    }

293
public:
hjk's avatar
hjk committed
294
    DebuggerState state() const { return m_state; }
295
    RemoteSetupState remoteSetupState() const { return m_remoteSetupState; }
296 297 298
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
299
    void setRemoteSetupState(RemoteSetupState state);
hjk's avatar
hjk committed
300

301
    DebuggerEngine *m_engine; // Not owned.
302
    DebuggerEngine *m_masterEngine; // Not owned
303 304
    DebuggerRunControl *m_runControl;  // Not owned.

305
    DebuggerRunParameters m_runParameters;
hjk's avatar
hjk committed
306 307

    // The current state.
308 309
    DebuggerState m_state;

hjk's avatar
hjk committed
310 311 312 313 314 315
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

316 317 318
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

319
    Terminal m_terminal;
320 321 322 323 324 325 326 327
    qint64 m_inferiorPid;

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

330 331
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
hjk's avatar
hjk committed
332
    QScopedPointer<LocationMark> m_locationMark;
333
    QTimer m_locationTimer;
334 335

    bool m_isStateDebugging;
336

337
    Utils::FileInProjectFinder m_fileFinder;
hjk's avatar
hjk committed
338
    QString m_qtNamespace;
339 340

    // Safety net to avoid infinite lookups.
hjk's avatar
hjk committed
341
    QSet<QString> m_lookupRequests; // FIXME: Integrate properly.
342 343 344 345 346 347 348 349 350
};


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

351
DebuggerEngine::DebuggerEngine(const DebuggerRunParameters &startParameters)
352
  : d(new DebuggerEnginePrivate(this, startParameters))
353
{}
354 355 356

DebuggerEngine::~DebuggerEngine()
{
357 358
    disconnect();
    delete d;
359 360
}

hjk's avatar
hjk committed
361
QString DebuggerEngine::stateName(int s)
362
{
hjk's avatar
hjk committed
363
#    define SN(x) case x: return QLatin1String(#x);
364 365 366 367 368 369 370 371
    switch (s) {
        SN(DebuggerNotReady)
        SN(EngineSetupRequested)
        SN(EngineSetupOk)
        SN(EngineSetupFailed)
        SN(EngineRunFailed)
        SN(InferiorSetupRequested)
        SN(InferiorSetupFailed)
372
        SN(InferiorSetupOk)
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        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)
    }
hjk's avatar
hjk committed
389
    return QLatin1String("<unknown>");
390 391 392
#    undef SN
}

393
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
394
{
395
    showMessage(msg, StatusBar, timeout);
396
}
397

398 399 400 401 402 403 404 405 406 407 408 409
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));
}

410 411 412 413
void DebuggerEngine::doUpdateLocals(const UpdateParameters &)
{
}

414 415 416 417 418
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

419 420
ModulesHandler *DebuggerEngine::modulesHandler() const
{
421 422 423
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
424 425 426 427
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
428 429 430
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
431 432 433 434
}

StackHandler *DebuggerEngine::stackHandler() const
{
Aurindam Jana's avatar
Aurindam Jana committed
435 436 437
    return d->m_masterEngine
        ? d->m_masterEngine->stackHandler()
        : &d->m_stackHandler;
438 439 440 441
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
442 443 444
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
445 446 447 448
}

WatchHandler *DebuggerEngine::watchHandler() const
{
449 450 451
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
452 453 454 455
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
456 457 458
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
459 460 461 462
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
463
   return modulesHandler()->model();
464 465 466 467
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
468
    return registerHandler()->model();
469 470 471 472
}

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

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
478
    return threadsHandler()->model();
479 480
}

hjk's avatar
hjk committed
481 482 483
QAbstractItemModel *DebuggerEngine::watchModel() const
{
    return watchHandler()->model();
484 485
}

486 487
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
488
    return sourceFilesHandler()->model();
489 490
}

491
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
492 493 494 495 496 497
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
498 499 500 501 502 503 504
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

hjk's avatar
hjk committed
505
void DebuggerEngine::setRegisterValue(const QString &name, const QString &value)
506
{
hjk's avatar
hjk committed
507
    Q_UNUSED(name);
508 509 510
    Q_UNUSED(value);
}

511 512 513 514 515 516 517 518 519 520
static Utils::OutputFormat outputFormatForChannelType(int channel)
{
    switch (channel) {
    case AppOutput: return Utils::StdOutFormatSameLine;
    case AppError: return Utils::StdErrFormatSameLine;
    case AppStuff: return Utils::DebugFormat;
    default: return Utils::NumberOfFormats;
    }
}

521 522
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
    switch (channel) {
    case AppOutput:
    case AppError:
    case AppStuff:
        if (d->m_runControl)
            d->m_runControl->appendMessage(msg, outputFormatForChannelType(channel));
        else
            qWarning("Warning: %s (no active run control)", qPrintable(msg));
        break;
    default:
        break;
544
    }
545 546 547 548
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
549 550
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
551

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

559 560
    d->m_runControl = runControl;

561 562
    d->m_inferiorPid = d->m_runParameters.attachPID > 0
        ? d->m_runParameters.attachPID : 0;
563 564
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
565

566
    if (isNativeMixedActive())
hjk's avatar
hjk committed
567
        d->m_runParameters.inferior.environment.set("QV4_FORCE_INTERPRETER", "1");
568

hjk's avatar
hjk committed
569
    action(OperateByInstruction)->setEnabled(hasCapability(DisassemblerCapability));
570

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

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

590
    d->queueSetupEngine();
591 592 593 594
}

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

599
void DebuggerEngine::gotoLocation(const Location &loc)
600
{
Aurindam Jana's avatar
Aurindam Jana committed
601 602
     d->resetLocation();

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

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

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

624 625
    if (newEditor)
        editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
626

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

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

633 634 635
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
hjk's avatar
hjk committed
636
    showMessage("HANDLE RUNCONTROL START FAILED");
637
    d->m_runControl = 0;
638
    d->m_progress.setProgressValue(900);
639 640
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
641 642
}

643 644
// Called from RunControl.
void DebuggerEngine::handleFinished()
645
{
hjk's avatar
hjk committed
646
    showMessage("HANDLE RUNCONTROL FINISHED");
647
    d->m_runControl = 0;
648 649
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
650 651 652 653 654 655
    modulesHandler()->removeAll();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    watchHandler()->cleanup();
}

656
const DebuggerRunParameters &DebuggerEngine::runParameters() const
657
{
658
    return d->m_runParameters;
659 660
}

661
DebuggerRunParameters &DebuggerEngine::runParameters()
662
{
663
    return d->m_runParameters;
664 665 666 667 668 669 670
}

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

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

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

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

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

hjk's avatar
hjk committed
696
    case InferiorSetupRequested:
697
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
698
    case InferiorSetupFailed:
hjk's avatar
hjk committed
699
        return to == EngineShutdownRequested;
700 701
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
702 703

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

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

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
725
            || to == InferiorStopOk || to == InferiorShutdownOk;
726
    case InferiorStopFailed:
hjk's avatar
hjk committed
727
        return to == EngineShutdownRequested;
728 729

    case InferiorUnrunnable:
hjk's avatar
hjk committed
730 731 732 733 734
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
735
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
736
        return to == EngineShutdownRequested;
737

hjk's avatar
hjk committed
738
    case EngineShutdownRequested:
739
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
740 741 742 743 744 745
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
746
        return to == EngineSetupRequested; // Happens on restart.
747 748
    }

749
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
750 751 752
    return false;
}

753 754
void DebuggerEngine::setupSlaveEngine()
{
755
    QTC_CHECK(state() == DebuggerNotReady);
756 757 758 759 760
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
hjk's avatar
hjk committed
761
    m_engine->showMessage("CALL: SETUP ENGINE");
762
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
763
    m_engine->validateExecutable(&m_runParameters);
764 765 766
    m_engine->setupEngine();
}

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

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

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

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

void DebuggerEngine::setupSlaveInferior()
{
800
    QTC_CHECK(state() == EngineSetupOk);
801
    d->queueSetupInferior();
802 803
}

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

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

void DebuggerEngine::notifyInferiorSetupOk()
{
824 825 826
#ifdef WITH_BENCHMARK
    CALLGRIND_START_INSTRUMENTATION;
#endif
hjk's avatar
hjk committed
827
    aboutToNotifyInferiorSetupOk();
hjk's avatar
hjk committed
828
    showMessage("NOTE: INFERIOR SETUP OK");
829 830 831 832
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
833 834
}

835 836 837
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
838
    QTC_CHECK(state() == InferiorSetupOk);
839 840 841
    d->queueRunEngine();
}

hjk's avatar
hjk committed
842 843
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
844
    m_engine->showMessage("CALL: RUN ENGINE");
845
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
846
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
847 848 849
    m_engine->runEngine();
}

hjk's avatar
hjk committed
850
void DebuggerEngine::notifyEngineRunOkAndInferiorUnrunnable()
hjk's avatar
hjk committed
851
{
hjk's avatar
hjk committed
852
    showMessage("NOTE: INFERIOR UNRUNNABLE");
853 854
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
855
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
856
    showStatusMessage(tr("Loading finished."));
hjk's avatar
hjk committed
857 858 859 860 861
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{