debuggerengine.cpp 58.6 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30 31 32 33 34
**
**************************************************************************/

#include "debuggerengine.h"

35
#include "debuggerinternalconstants.h"
36
#include "debuggeractions.h"
37
#include "debuggercore.h"
38
#include "debuggerplugin.h"
39
#include "debuggerrunner.h"
40
#include "debuggerstringutils.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
41
#include "debuggerstartparameters.h"
42

43 44
#include "memoryagent.h"
#include "disassembleragent.h"
45 46 47 48 49 50 51 52
#include "breakhandler.h"
#include "moduleshandler.h"
#include "registerhandler.h"
#include "snapshothandler.h"
#include "sourcefileshandler.h"
#include "stackhandler.h"
#include "threadshandler.h"
#include "watchhandler.h"
53
#include "qtmessageloghandler.h"
54

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

#include <texteditor/itexteditor.h>
62
#include <texteditor/basetextmark.h>
hjk's avatar
hjk committed
63

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

67 68
#include <utils/savedaction.h>
#include <utils/qtcassert.h>
69
#include <utils/fileinprojectfinder.h>
70

71 72 73 74 75
#include <QDebug>
#include <QTimer>
#include <QFile>
#include <QFileInfo>
#include <QFutureInterface>
76

77
#include <QMessageBox>
78

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
enum { debug = 0 };

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

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

94 95 96 97 98 99
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

100 101
namespace Debugger {

Friedemann Kleint's avatar
Friedemann Kleint committed
102 103 104 105 106 107 108 109 110
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;
111
    m_from = frame.from;
Friedemann Kleint's avatar
Friedemann Kleint committed
112 113
}

114 115
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
116 117
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
118 119 120 121 122 123 124
}

QDebug operator<<(QDebug str, const DebuggerStartParameters &sp)
{
    QDebug nospace = str.nospace();
    nospace << "executable=" << sp.executable
            << " coreFile=" << sp.coreFile
125
            << " processArgs=" << sp.processArgs
126 127 128 129 130 131 132
            << " environment=<" << sp.environment.size() << " variables>"
            << " workingDir=" << sp.workingDirectory
            << " attachPID=" << sp.attachPID
            << " useTerminal=" << sp.useTerminal
            << " remoteChannel=" << sp.remoteChannel
            << " remoteArchitecture=" << sp.remoteArchitecture
            << " symbolFileName=" << sp.symbolFileName
hjk's avatar
hjk committed
133
            << " useServerStartScript=" << sp.useServerStartScript
134
            << " serverStartScript=" << sp.serverStartScript
135
            << " abi=" << sp.toolChainAbi.toString() << '\n';
136 137 138
    return str;
}

139

140 141 142 143 144 145
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

146 147 148 149 150 151 152 153 154
// transitions:
//   None->Requested
//   Requested->Succeeded
//   Requested->Failed
//   Requested->Cancelled
enum RemoteSetupState { RemoteSetupNone, RemoteSetupRequested,
                        RemoteSetupSucceeded, RemoteSetupFailed,
                        RemoteSetupCancelled };

155
class DebuggerEnginePrivate : public QObject
156
{
157 158
    Q_OBJECT

159
public:
160 161
    DebuggerEnginePrivate(DebuggerEngine *engine,
            DebuggerEngine *masterEngine,
162
            DebuggerLanguages languages,
163
            const DebuggerStartParameters &sp)
164
      : m_engine(engine),
165
        m_masterEngine(masterEngine),
166 167
        m_runControl(0),
        m_startParameters(sp),
168
        m_languages(languages),
169
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
170
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
171
        m_targetState(DebuggerNotReady),
172
        m_remoteSetupState(RemoteSetupNone),
173
        m_inferiorPid(0),
174
        m_modulesHandler(),
175
        m_registerHandler(),
176 177
        m_sourceFilesHandler(),
        m_stackHandler(),
hjk's avatar
hjk committed
178
        m_threadsHandler(),
179
        m_watchHandler(engine),
180
        m_disassemblerAgent(engine),
181
        m_memoryAgent(engine),
182
        m_isStateDebugging(false),
183
        m_testsPossible(true),
184
        m_testsRunning(false),
185
        m_taskHub(0)
186
    {
187
        connect(&m_locationTimer, SIGNAL(timeout()), SLOT(resetLocation()));
188
        if (sp.toolChainAbi.os() == Abi::MacOS)
con's avatar
con committed
189
            m_disassemblerAgent.setTryMixed(false);
190
    }
191 192

    ~DebuggerEnginePrivate() {}
193

194
public slots:
195
    void doSetupEngine();
hjk's avatar
hjk committed
196 197
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
198 199
    void doShutdownEngine();
    void doShutdownInferior();
200
    void doInterruptInferior();
hjk's avatar
hjk committed
201
    void doFinishDebugger();
hjk's avatar
hjk committed
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216
    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()));
    }

217 218
    void queueRunEngine()
    {
hjk's avatar
hjk committed
219 220 221 222 223
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

224 225
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
226 227 228 229 230
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

231 232
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
233 234 235 236 237
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

238 239
    void queueFinishDebugger()
    {
240 241 242
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
243 244 245 246 247
        resetLocation();
        if (isMasterEngine()) {
            m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
            QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
        }
hjk's avatar
hjk committed
248 249
    }

250 251
    void raiseApplication()
    {
252 253
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
254 255
    }

256
    void scheduleResetLocation()
257
    {
258
        m_stackHandler.scheduleResetLocation();
259
        m_watchHandler.scheduleResetLocation();
260
        m_threadsHandler.scheduleResetLocation();
261
        m_disassemblerAgent.scheduleResetLocation();
262 263 264 265
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

266
    void resetLocation()
267 268 269
    {
        m_locationTimer.stop();
        m_locationMark.reset();
270
        m_stackHandler.resetLocation();
271
        m_watchHandler.resetLocation();
272
        m_threadsHandler.resetLocation();
273
        m_disassemblerAgent.resetLocation();
274 275
    }

276 277
    TaskHub *taskHub();

278
public:
hjk's avatar
hjk committed
279
    DebuggerState state() const { return m_state; }
280
    RemoteSetupState remoteSetupState() const { return m_remoteSetupState; }
281 282 283
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
284
    void setRemoteSetupState(RemoteSetupState state);
hjk's avatar
hjk committed
285

286
    DebuggerEngine *m_engine; // Not owned.
287
    DebuggerEngine *m_masterEngine; // Not owned
288 289 290
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
291
    DebuggerLanguages m_languages;
hjk's avatar
hjk committed
292 293

    // The current state.
294 295
    DebuggerState m_state;

hjk's avatar
hjk committed
296 297 298 299 300 301
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

302 303 304
    // State of RemoteSetup signal/slots.
    RemoteSetupState m_remoteSetupState;

305 306 307 308 309 310 311 312
    qint64 m_inferiorPid;

    ModulesHandler m_modulesHandler;
    RegisterHandler m_registerHandler;
    SourceFilesHandler m_sourceFilesHandler;
    StackHandler m_stackHandler;
    ThreadsHandler m_threadsHandler;
    WatchHandler m_watchHandler;
313
    QtMessageLogHandler m_qtMessageHandler;
314
    QFutureInterface<void> m_progress;
315

316 317
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
318 319
    QScopedPointer<TextEditor::BaseTextMark> m_locationMark;
    QTimer m_locationTimer;
320 321

    bool m_isStateDebugging;
322

323
    Utils::FileInProjectFinder m_fileFinder;
324 325 326
    // Testing
    void handleAutoTests();
    void handleAutoTestLine(int line);
327
    void reportTestError(const QString &msg, int line);
328
    bool m_testsPossible;
329
    bool m_testsRunning;
David Schulz's avatar
David Schulz committed
330 331
    bool m_breakOnError;
    bool m_foundError;
332
    QStringList m_testContents;
333 334
    TaskHub *m_taskHub;
    QString m_testFileName;
335 336 337 338 339 340 341 342 343
};


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

344
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters,
345
        DebuggerLanguages languages,
346
        DebuggerEngine *parentEngine)
347
  : d(new DebuggerEnginePrivate(this, parentEngine, languages, startParameters))
348
{
349
    d->m_inferiorPid = 0;
350 351 352 353
}

DebuggerEngine::~DebuggerEngine()
{
354 355
    disconnect();
    delete d;
356 357
}

358 359 360 361 362 363 364 365 366 367 368
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)
369
        SN(InferiorSetupOk)
370 371 372 373 374 375 376 377
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
378
        SN(InferiorExitOk)
379 380 381 382 383 384 385 386 387 388 389 390
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

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

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

408 409 410 411 412
void DebuggerEngine::setTargetState(DebuggerState state)
{
    d->m_targetState = state;
}

413 414
ModulesHandler *DebuggerEngine::modulesHandler() const
{
415 416 417
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
418 419 420 421
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
422 423 424
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
425 426 427 428
}

StackHandler *DebuggerEngine::stackHandler() const
{
Aurindam Jana's avatar
Aurindam Jana committed
429 430 431
    return d->m_masterEngine
        ? d->m_masterEngine->stackHandler()
        : &d->m_stackHandler;
432 433 434 435
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
436 437 438
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
439 440 441 442
}

WatchHandler *DebuggerEngine::watchHandler() const
{
443 444 445
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
446 447
}

448 449 450 451 452
QtMessageLogHandler *DebuggerEngine::qtMessageLogHandler() const
{
    return &d->m_qtMessageHandler;
}

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

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
462
    QAbstractItemModel *model = modulesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
463 464 465
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ModulesModel"));
    return model;
466 467 468 469
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
470
    QAbstractItemModel *model = registerHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
471 472 473
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("RegisterModel"));
    return model;
474 475 476 477
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
478
    QAbstractItemModel *model = stackHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
479 480 481
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("StackModel"));
    return model;
482 483 484 485
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
486
    QAbstractItemModel *model = threadsHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
487 488 489
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ThreadsModel"));
    return model;
490 491 492 493
}

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

QAbstractItemModel *DebuggerEngine::watchersModel() const
{
hjk's avatar
hjk committed
499
    return watchHandler()->model();
500 501 502 503
}

QAbstractItemModel *DebuggerEngine::returnModel() const
{
hjk's avatar
hjk committed
504
    return watchHandler()->model();
505 506
}

507 508
QAbstractItemModel *DebuggerEngine::inspectorModel() const
{
hjk's avatar
hjk committed
509
    return watchHandler()->model();
510 511
}

512 513
QAbstractItemModel *DebuggerEngine::toolTipsModel() const
{
hjk's avatar
hjk committed
514 515 516 517 518 519
    return watchHandler()->model();
}

QAbstractItemModel *DebuggerEngine::watchModel() const
{
    return watchHandler()->model();
520 521
}

522 523
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
524
    QAbstractItemModel *model = sourceFilesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
525 526 527
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("SourceFilesModel"));
    return model;
528 529
}

530 531 532 533 534 535 536 537
QAbstractItemModel *DebuggerEngine::qtMessageLogModel() const
{
    QAbstractItemModel *model = qtMessageLogHandler()->model();
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("QtMessageLogModel"));
    return model;
}

538
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
539 540 541 542 543 544
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
545 546 547 548 549 550 551
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

552 553 554 555 556 557 558 559
void DebuggerEngine::setRegisterValue(int regnr, const QString &value)
{
    Q_UNUSED(regnr);
    Q_UNUSED(value);
}

void DebuggerEngine::showMessage(const QString &msg, int channel, int timeout) const
{
560 561 562 563
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
564 565
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
566 567 568
    if (channel == QtMessageLogOutput)
        qtMessageLogHandler()->appendMessage(QtMessageLogHandler::UndefinedType, msg);

569
    debuggerCore()->showMessage(msg, channel, timeout);
570 571 572 573 574
    if (d->m_runControl) {
        d->m_runControl->showMessage(msg, channel);
    } else {
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
    }
575 576 577 578
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
579 580
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
581

582
    d->m_progress.setProgressRange(0, 1000);
hjk's avatar
hjk committed
583
    Core::FutureProgress *fp = Core::ICore::progressManager()
584 585
        ->addTask(d->m_progress.future(),
        tr("Launching"), _("Debugger.Launcher"));
con's avatar
con committed
586
    fp->setKeepOnFinish(Core::FutureProgress::HideOnFinish);
587 588
    d->m_progress.reportStarted();

589 590 591 592
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;
593 594
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
595

596 597
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
598

hjk's avatar
hjk committed
599
    debuggerCore()->action(OperateByInstruction)
600
        ->setEnabled(hasCapability(DisassemblerCapability));
601

602 603
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
604 605
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
606
    d->m_progress.setProgressValue(200);
607
    d->queueSetupEngine();
608 609 610 611
}

void DebuggerEngine::resetLocation()
{
612 613
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
614 615
}

616
void DebuggerEngine::gotoLocation(const Location &loc)
617
{
Aurindam Jana's avatar
Aurindam Jana committed
618 619
     d->resetLocation();

Aurindam Jana's avatar
Aurindam Jana committed
620 621
    if ((hasCapability(OperateByInstructionCapability) &&
            debuggerCore()->boolSetting(OperateByInstruction)) || !loc.hasDebugInfo()) {
622 623 624
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
625 626 627 628 629
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;


630 631
    const QString file = loc.fileName();
    const int line = loc.lineNumber();
hjk's avatar
hjk committed
632
    QList<IEditor *> editors = EditorManager::instance()->editorsForFileName(file);
633
    IEditor *editor = 0;
hjk's avatar
hjk committed
634
    if (editors.isEmpty()) {
hjk's avatar
hjk committed
635
        editor = EditorManager::openEditor(file, Core::Id(),
636 637 638 639 640 641 642
            EditorManager::IgnoreNavigationHistory);
        if (editor) {
            editors.append(editor);
            editor->setProperty(Constants::OPENED_BY_DEBUGGER, true);
        }
    } else {
        editor = editors.back();
hjk's avatar
hjk committed
643
    }
644
    ITextEditor *texteditor = qobject_cast<ITextEditor *>(editor);
hjk's avatar
hjk committed
645 646 647
    if (texteditor)
        texteditor->gotoLine(line, 0);

648
    if (loc.needsMarker()) {
Daniel Teske's avatar
Daniel Teske committed
649
        d->m_locationMark.reset(new TextEditor::BaseTextMark(file, line));
650 651
        d->m_locationMark->setIcon(debuggerCore()->locationMarkIcon());
        d->m_locationMark->setPriority(TextEditor::ITextMark::HighPriority);
652
        d->m_locationMark->init();
653
    }
654 655

    // FIXME: Breaks with split views.
656
    if (!d->m_memoryAgent.hasVisibleEditor() || loc.needsRaise())
hjk's avatar
hjk committed
657
        EditorManager::activateEditor(editor);
658
    //qDebug() << "MEMORY: " << d->m_memoryAgent.hasVisibleEditor();
659 660
}

661 662 663
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
664
    showMessage(QLatin1String("HANDLE RUNCONTROL START FAILED"));
665
    d->m_runControl = 0;
666
    d->m_progress.setProgressValue(900);
667 668
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
669 670
}

671 672
// Called from RunControl.
void DebuggerEngine::handleFinished()
673
{
674
    showMessage(QLatin1String("HANDLE RUNCONTROL FINISHED"));
675
    d->m_runControl = 0;
676 677
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    modulesHandler()->removeAll();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    watchHandler()->cleanup();
}

const DebuggerStartParameters &DebuggerEngine::startParameters() const
{
    return d->m_startParameters;
}

DebuggerStartParameters &DebuggerEngine::startParameters()
{
    return d->m_startParameters;
}


//////////////////////////////////////////////////////////////////////
//
// Dumpers. "Custom dumpers" are a library compiled against the current
// Qt containing functions to evaluate values of Qt classes
// (such as QString, taking pointers to their addresses).
// The library must be loaded into the debuggee.
//
//////////////////////////////////////////////////////////////////////

bool DebuggerEngine::qtDumperLibraryEnabled() const
{
hjk's avatar
hjk committed
706
    return debuggerCore()->boolSetting(UseDebuggingHelpers);
707 708 709 710 711 712 713 714 715
}

QStringList DebuggerEngine::qtDumperLibraryLocations() const
{
    return d->m_startParameters.dumperLibraryLocations;
}

void DebuggerEngine::showQtDumperLibraryWarning(const QString &details)
{
716
    debuggerCore()->showQtDumperLibraryWarning(details);
717 718 719 720 721 722 723 724 725 726 727 728
}

QString DebuggerEngine::qtDumperLibraryName() const
{
    return startParameters().dumperLibrary;
}

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

hjk's avatar
hjk committed
729
DebuggerState DebuggerEngine::lastGoodState() const
730
{
hjk's avatar
hjk committed
731 732 733 734 735 736 737
    return d->m_lastGoodState;
}

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

hjk's avatar
hjk committed
739 740 741
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
742
    case DebuggerNotReady:
743
        return to == EngineSetupRequested;
744

hjk's avatar
hjk committed
745
    case EngineSetupRequested:
746 747
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
748 749 750
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
751
    case EngineSetupOk:
hjk's avatar
hjk committed
752
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
753

hjk's avatar
hjk committed
754
    case InferiorSetupRequested:
755
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
756
    case InferiorSetupFailed:
hjk's avatar
hjk committed
757
        return to == EngineShutdownRequested;
758 759
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
760 761

    case EngineRunRequested:
hjk's avatar
hjk committed
762 763 764 765
        return to == EngineRunFailed
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
766
    case EngineRunFailed:
767
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
768 769 770 771 772 773

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
774 775 776
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
hjk's avatar
hjk committed
777 778 779 780 781

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
782
            || to == InferiorStopOk || InferiorExitOk;
783
    case InferiorStopFailed:
hjk's avatar
hjk committed
784
        return to == EngineShutdownRequested;
785

786 787 788
    case InferiorExitOk:
        return to == InferiorShutdownOk;

789
    case InferiorUnrunnable:
hjk's avatar
hjk committed
790 791 792 793 794
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
795
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
796
        return to == EngineShutdownRequested;
797

hjk's avatar
hjk committed
798
    case EngineShutdownRequested:
799
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
800 801 802 803 804 805
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
806
        return to == EngineSetupRequested; // Happens on restart.
807 808
    }

809
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
810 811 812
    return false;
}

813 814
void DebuggerEngine::setupSlaveEngine()
{
815
    QTC_CHECK(state() == DebuggerNotReady);
816 817 818 819 820 821 822 823 824 825
    d->queueSetupEngine();
}

void DebuggerEnginePrivate::doSetupEngine()
{
    m_engine->showMessage(_("CALL: SETUP ENGINE"));
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << m_engine << state());
    m_engine->setupEngine();
}

826
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
827
{
hjk's avatar
hjk committed
828
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
829 830 831 832 833 834 835
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupRequested
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());
    if (d->remoteSetupState() == RemoteSetupRequested)
        d->setRemoteSetupState(RemoteSetupCancelled);

836
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
837
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
838
    if (isMasterEngine() && runControl())
839
        runControl()->startFailed();
hjk's avatar
hjk committed
840
    setState(DebuggerFinished);
hjk's avatar
hjk committed
841 842
}

843
void DebuggerEngine::notifyEngineSetupOk()
844
{
hjk's avatar
hjk committed
845
    showMessage(_("NOTE: ENGINE SETUP OK"));
846 847 848 849
    QTC_ASSERT(d->remoteSetupState() == RemoteSetupNone
               || d->remoteSetupState() == RemoteSetupSucceeded,
               qDebug() << this << "remoteSetupState" << d->remoteSetupState());

850
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
851
    setState(EngineSetupOk);
hjk's avatar
hjk committed
852
    showMessage(_("QUEUE: SETUP INFERIOR"));
853
    if (isMasterEngine())
854 855 856 857 858
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
859
    QTC_CHECK(state() == EngineSetupOk);
860
    d->queueSetupInferior();
861 862
}

hjk's avatar
hjk committed
863
void DebuggerEnginePrivate::doSetupInferior()
864
{
865
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
866
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
867
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
868 869 870 871 872
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
873
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
874
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
875
    showStatusMessage(tr("Setup failed."));
hjk's avatar
hjk committed
876
    setState(InferiorSetupFailed);