debuggerengine.cpp 46.2 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.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
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.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 53
#include "breakhandler.h"
#include "moduleshandler.h"
#include "registerhandler.h"
#include "snapshothandler.h"
#include "sourcefileshandler.h"
#include "stackhandler.h"
#include "threadshandler.h"
#include "watchhandler.h"

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

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

63 64 65 66 67
#include <utils/savedaction.h>
#include <utils/qtcassert.h>

#include <QtCore/QDebug>
#include <QtCore/QTimer>
68
#include <QtCore/QFutureInterface>
69

70
#include <QtGui/QMessageBox>
71

hjk's avatar
hjk committed
72
using namespace Core;
73
using namespace Debugger::Internal;
hjk's avatar
hjk committed
74 75
using namespace ProjectExplorer;
using namespace TextEditor;
76

77 78 79 80
enum { debug = 0 };

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

82

83 84 85 86 87 88
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

89 90
namespace Debugger {

Friedemann Kleint's avatar
Friedemann Kleint committed
91 92 93 94 95 96 97 98 99
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;
100
    m_from = frame.from;
Friedemann Kleint's avatar
Friedemann Kleint committed
101 102
}

103 104
QDebug operator<<(QDebug d, DebuggerState state)
{
hjk's avatar
hjk committed
105 106
    //return d << DebuggerEngine::stateName(state) << '(' << int(state) << ')';
    return d << DebuggerEngine::stateName(state);
107 108 109 110 111 112 113
}

QDebug operator<<(QDebug str, const DebuggerStartParameters &sp)
{
    QDebug nospace = str.nospace();
    nospace << "executable=" << sp.executable
            << " coreFile=" << sp.coreFile
114
            << " processArgs=" << sp.processArgs
115 116 117 118 119 120 121
            << " 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
122
            << " useServerStartScript=" << sp.useServerStartScript
123
            << " serverStartScript=" << sp.serverStartScript
124
            << " abi=" << sp.toolChainAbi.toString() << '\n';
125 126 127
    return str;
}

128

129 130 131 132 133 134
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

135
class DebuggerEnginePrivate : public QObject
136
{
137 138
    Q_OBJECT

139
public:
140 141 142
    DebuggerEnginePrivate(DebuggerEngine *engine,
            DebuggerEngine *masterEngine,
            const DebuggerStartParameters &sp)
143
      : m_engine(engine),
144
        m_masterEngine(masterEngine),
145 146 147
        m_runControl(0),
        m_startParameters(sp),
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
148
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
149
        m_targetState(DebuggerNotReady),
150
        m_inferiorPid(0),
151
        m_modulesHandler(),
152
        m_registerHandler(),
153 154
        m_sourceFilesHandler(),
        m_stackHandler(),
hjk's avatar
hjk committed
155
        m_threadsHandler(),
156
        m_watchHandler(engine),
157
        m_disassemblerAgent(engine),
158 159
        m_memoryAgent(engine),
        m_isStateDebugging(false)
160
    {
161
        connect(&m_locationTimer, SIGNAL(timeout()), SLOT(resetLocation()));
con's avatar
con committed
162 163
        if (sp.toolChainAbi.os() == ProjectExplorer::Abi::MacOS)
            m_disassemblerAgent.setTryMixed(false);
164
    }
165 166

    ~DebuggerEnginePrivate() {}
167

168
public slots:
169
    void doSetupEngine();
hjk's avatar
hjk committed
170 171
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
172 173
    void doShutdownEngine();
    void doShutdownInferior();
174
    void doInterruptInferior();
hjk's avatar
hjk committed
175
    void doFinishDebugger();
hjk's avatar
hjk committed
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190
    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()));
    }

191 192
    void queueRunEngine()
    {
hjk's avatar
hjk committed
193 194 195 196 197
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

198 199
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
200 201 202 203 204
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

205 206
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
207 208 209 210 211
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

212 213
    void queueFinishDebugger()
    {
214 215 216
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
217 218 219 220 221
        resetLocation();
        if (isMasterEngine()) {
            m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
            QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
        }
hjk's avatar
hjk committed
222 223
    }

224 225
    void raiseApplication()
    {
226 227
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
228 229
    }

230
    void scheduleResetLocation()
231
    {
232
        m_stackHandler.scheduleResetLocation();
233
        m_threadsHandler.scheduleResetLocation();
234
        m_disassemblerAgent.scheduleResetLocation();
235 236 237 238
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

239
    void resetLocation()
240 241 242
    {
        m_locationTimer.stop();
        m_locationMark.reset();
243
        m_stackHandler.resetLocation();
244
        m_threadsHandler.resetLocation();
245
        m_disassemblerAgent.resetLocation();
246 247
    }

248
public:
hjk's avatar
hjk committed
249
    DebuggerState state() const { return m_state; }
250 251 252
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
hjk's avatar
hjk committed
253

254
    DebuggerEngine *m_engine; // Not owned.
255
    DebuggerEngine *m_masterEngine; // Not owned
256 257 258
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
hjk's avatar
hjk committed
259 260

    // The current state.
261 262
    DebuggerState m_state;

hjk's avatar
hjk committed
263 264 265 266 267 268
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

269 270 271 272 273 274 275 276
    qint64 m_inferiorPid;

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

279 280
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
281 282
    QScopedPointer<TextEditor::BaseTextMark> m_locationMark;
    QTimer m_locationTimer;
283 284

    bool m_isStateDebugging;
285 286 287 288 289 290 291 292 293
};


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

294 295 296
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters,
        DebuggerEngine *parentEngine)
  : d(new DebuggerEnginePrivate(this, parentEngine, startParameters))
297
{
298
    d->m_inferiorPid = 0;
299 300 301 302
}

DebuggerEngine::~DebuggerEngine()
{
303 304
    disconnect();
    delete d;
305 306
}

307 308 309 310 311 312 313 314 315 316 317
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)
318
        SN(InferiorSetupOk)
319 320 321 322 323 324 325 326
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
327
        SN(InferiorExitOk)
328 329 330 331 332 333 334 335 336 337 338 339
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

340
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
341
{
342
    showMessage(msg, StatusBar, timeout);
343
}
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358
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));
}

ModulesHandler *DebuggerEngine::modulesHandler() const
{
359 360 361
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
362 363 364 365
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
366 367 368
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
369 370 371 372
}

StackHandler *DebuggerEngine::stackHandler() const
{
hjk's avatar
hjk committed
373 374 375 376
    //return d->m_masterEngine
    //    ? d->m_masterEngine->stackHandler()
    //    : &d->m_stackHandler;
    return &d->m_stackHandler;
377 378 379 380
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
381 382 383
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
384 385 386 387
}

WatchHandler *DebuggerEngine::watchHandler() const
{
388 389 390
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
391 392 393 394
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
395 396 397
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
398 399 400 401
}

QAbstractItemModel *DebuggerEngine::modulesModel() const
{
402
    QAbstractItemModel *model = modulesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
403 404 405
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ModulesModel"));
    return model;
406 407 408 409
}

QAbstractItemModel *DebuggerEngine::registerModel() const
{
410
    QAbstractItemModel *model = registerHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
411 412 413
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("RegisterModel"));
    return model;
414 415 416 417
}

QAbstractItemModel *DebuggerEngine::stackModel() const
{
418
    QAbstractItemModel *model = stackHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
419 420 421
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("StackModel"));
    return model;
422 423 424 425
}

QAbstractItemModel *DebuggerEngine::threadsModel() const
{
426
    QAbstractItemModel *model = threadsHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
427 428 429
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ThreadsModel"));
    return model;
430 431 432 433
}

QAbstractItemModel *DebuggerEngine::localsModel() const
{
434
    QAbstractItemModel *model = watchHandler()->model(LocalsWatch);
Friedemann Kleint's avatar
Friedemann Kleint committed
435 436 437
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("LocalsModel"));
    return model;
438 439 440 441
}

QAbstractItemModel *DebuggerEngine::watchersModel() const
{
442
    QAbstractItemModel *model = watchHandler()->model(WatchersWatch);
Friedemann Kleint's avatar
Friedemann Kleint committed
443 444 445
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("WatchersModel"));
    return model;
446 447 448 449
}

QAbstractItemModel *DebuggerEngine::returnModel() const
{
450
    QAbstractItemModel *model = watchHandler()->model(ReturnWatch);
Friedemann Kleint's avatar
Friedemann Kleint committed
451 452 453
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("ReturnModel"));
    return model;
454 455
}

456 457 458 459 460 461 462 463
QAbstractItemModel *DebuggerEngine::toolTipsModel() const
{
    QAbstractItemModel *model = watchHandler()->model(TooltipsWatch);
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("TooltipsModel"));
    return model;
}

464 465
QAbstractItemModel *DebuggerEngine::sourceFilesModel() const
{
466
    QAbstractItemModel *model = sourceFilesHandler()->model();
Friedemann Kleint's avatar
Friedemann Kleint committed
467 468 469
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("SourceFilesModel"));
    return model;
470 471
}

472
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
473 474 475 476 477 478
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

hjk's avatar
hjk committed
479 480 481 482 483 484 485
void DebuggerEngine::changeMemory(MemoryAgent *, QObject *,
        quint64 addr, const QByteArray &data)
{
    Q_UNUSED(addr);
    Q_UNUSED(data);
}

486 487 488 489 490 491 492 493
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
{
494 495 496 497
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
498 499
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
500
    debuggerCore()->showMessage(msg, channel, timeout);
501 502 503 504 505
    if (d->m_runControl) {
        d->m_runControl->showMessage(msg, channel);
    } else {
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
    }
506 507 508 509
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
510 511
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
512

513 514 515 516
    d->m_progress.setProgressRange(0, 1000);
    Core::FutureProgress *fp = Core::ICore::instance()->progressManager()
        ->addTask(d->m_progress.future(),
        tr("Launching"), _("Debugger.Launcher"));
con's avatar
con committed
517
    fp->setKeepOnFinish(Core::FutureProgress::HideOnFinish);
518 519
    d->m_progress.reportStarted();

520 521 522 523
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;
524 525
    if (d->m_inferiorPid)
        d->m_runControl->setApplicationProcessHandle(ProcessHandle(d->m_inferiorPid));
526

527 528
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
529 530

    const unsigned engineCapabilities = debuggerCapabilities();
hjk's avatar
hjk committed
531
    debuggerCore()->action(OperateByInstruction)
532 533
        ->setEnabled(engineCapabilities & DisassemblerCapability);

534 535
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
536 537
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
538
    d->m_progress.setProgressValue(200);
539
    d->queueSetupEngine();
540 541 542 543
}

void DebuggerEngine::resetLocation()
{
544 545
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
546 547
}

548
void DebuggerEngine::gotoLocation(const Location &loc)
549
{
550 551 552 553
    if (debuggerCore()->boolSetting(OperateByInstruction) || !loc.hasDebugInfo()) {
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
554 555 556 557
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;

558
    d->resetLocation();
559

560 561
    const QString file = loc.fileName();
    const int line = loc.lineNumber();
hjk's avatar
hjk committed
562 563
    EditorManager *editorManager = EditorManager::instance();
    QList<IEditor *> editors = editorManager->editorsForFileName(file);
564
    IEditor *editor = 0;
hjk's avatar
hjk committed
565
    if (editors.isEmpty()) {
566 567 568 569 570 571 572 573
        editor = editorManager->openEditor(file, QString(),
            EditorManager::IgnoreNavigationHistory);
        if (editor) {
            editors.append(editor);
            editor->setProperty(Constants::OPENED_BY_DEBUGGER, true);
        }
    } else {
        editor = editors.back();
hjk's avatar
hjk committed
574
    }
575
    ITextEditor *texteditor = qobject_cast<ITextEditor *>(editor);
hjk's avatar
hjk committed
576 577 578
    if (texteditor)
        texteditor->gotoLine(line, 0);

579 580 581 582 583 584
    if (loc.needsMarker()) {
        d->m_locationMark.reset(new TextEditor::BaseTextMark);
        d->m_locationMark->setLocation(file, line);
        d->m_locationMark->setIcon(debuggerCore()->locationMarkIcon());
        d->m_locationMark->setPriority(TextEditor::ITextMark::HighPriority);
    }
585 586

    // FIXME: Breaks with split views.
587
    if (!d->m_memoryAgent.hasVisibleEditor() || loc.needsRaise())
588
        editorManager->activateEditor(editor);
589
    //qDebug() << "MEMORY: " << d->m_memoryAgent.hasVisibleEditor();
590 591
}

592 593 594
// Called from RunControl.
void DebuggerEngine::handleStartFailed()
{
595
    showMessage("HANDLE RUNCONTROL START FAILED");
596
    d->m_runControl = 0;
597
    d->m_progress.setProgressValue(900);
598 599
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
600 601
}

602 603
// Called from RunControl.
void DebuggerEngine::handleFinished()
604
{
605
    showMessage("HANDLE RUNCONTROL FINISHED");
606
    d->m_runControl = 0;
607 608
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    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
637
    return debuggerCore()->boolSetting(UseDebuggingHelpers);
638 639 640 641 642 643 644 645 646
}

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

void DebuggerEngine::showQtDumperLibraryWarning(const QString &details)
{
647
    debuggerCore()->showQtDumperLibraryWarning(details);
648 649 650 651 652 653 654 655 656 657 658 659
}

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

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

hjk's avatar
hjk committed
660
DebuggerState DebuggerEngine::lastGoodState() const
661
{
hjk's avatar
hjk committed
662 663 664 665 666 667 668
    return d->m_lastGoodState;
}

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

hjk's avatar
hjk committed
670 671 672
static bool isAllowedTransition(DebuggerState from, DebuggerState to)
{
    switch (from) {
673
    case DebuggerNotReady:
674
        return to == EngineSetupRequested;
675

hjk's avatar
hjk committed
676
    case EngineSetupRequested:
677 678
        return to == EngineSetupOk || to == EngineSetupFailed;
    case EngineSetupFailed:
679 680 681
        // In is the engine's task to go into a proper "Shutdown"
        // state before calling notifyEngineSetupFailed
        return to == DebuggerFinished;
682
    case EngineSetupOk:
hjk's avatar
hjk committed
683
        return to == InferiorSetupRequested || to == EngineShutdownRequested;
684

hjk's avatar
hjk committed
685
    case InferiorSetupRequested:
686
        return to == InferiorSetupOk || to == InferiorSetupFailed;
hjk's avatar
hjk committed
687
    case InferiorSetupFailed:
hjk's avatar
hjk committed
688
        return to == EngineShutdownRequested;
689 690
    case InferiorSetupOk:
        return to == EngineRunRequested;
hjk's avatar
hjk committed
691 692

    case EngineRunRequested:
hjk's avatar
hjk committed
693 694 695 696
        return to == EngineRunFailed
            || to == InferiorRunOk
            || to == InferiorStopOk
            || to == InferiorUnrunnable;
hjk's avatar
hjk committed
697
    case EngineRunFailed:
698
        return to == EngineShutdownRequested;
hjk's avatar
hjk committed
699 700 701 702 703 704

    case InferiorRunRequested:
        return to == InferiorRunOk || to == InferiorRunFailed;
    case InferiorRunFailed:
        return to == InferiorStopOk;
    case InferiorRunOk:
705 706 707
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
hjk's avatar
hjk committed
708 709 710 711 712

    case InferiorStopRequested:
        return to == InferiorStopOk || to == InferiorStopFailed;
    case InferiorStopOk:
        return to == InferiorRunRequested || to == InferiorShutdownRequested
713
            || to == InferiorStopOk || InferiorExitOk;
714
    case InferiorStopFailed:
hjk's avatar
hjk committed
715
        return to == EngineShutdownRequested;
716

717 718 719
    case InferiorExitOk:
        return to == InferiorShutdownOk;

720
    case InferiorUnrunnable:
hjk's avatar
hjk committed
721 722 723 724 725
        return to == InferiorShutdownRequested;
    case InferiorShutdownRequested:
        return to == InferiorShutdownOk || to == InferiorShutdownFailed;
    case InferiorShutdownOk:
        return to == EngineShutdownRequested;
726
    case InferiorShutdownFailed:
hjk's avatar
hjk committed
727
        return to == EngineShutdownRequested;
728

hjk's avatar
hjk committed
729
    case EngineShutdownRequested:
730
        return to == EngineShutdownOk || to == EngineShutdownFailed;
hjk's avatar
hjk committed
731 732 733 734 735 736
    case EngineShutdownOk:
        return to == DebuggerFinished;
    case EngineShutdownFailed:
        return to == DebuggerFinished;

    case DebuggerFinished:
737
        return to == EngineSetupRequested; // Happens on restart.
738 739
    }

740
    qDebug() << "UNKNOWN DEBUGGER STATE:" << from;
741 742 743
    return false;
}

744 745 746 747 748 749 750 751 752 753 754 755 756
void DebuggerEngine::setupSlaveEngine()
{
    QTC_ASSERT(state() == DebuggerNotReady, /**/);
    d->queueSetupEngine();
}

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

757
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
758
{
hjk's avatar
hjk committed
759
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
760
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
761
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
762
    if (isMasterEngine() && runControl())
763
        runControl()->startFailed();
hjk's avatar
hjk committed
764
    setState(DebuggerFinished);
hjk's avatar
hjk committed
765 766
}

767
void DebuggerEngine::notifyEngineSetupOk()
768
{
hjk's avatar
hjk committed
769
    showMessage(_("NOTE: ENGINE SETUP OK"));
770
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
771
    setState(EngineSetupOk);
hjk's avatar
hjk committed
772
    showMessage(_("QUEUE: SETUP INFERIOR"));
773
    if (isMasterEngine())
774 775 776 777 778 779 780
        d->queueSetupInferior();
}

void DebuggerEngine::setupSlaveInferior()
{
    QTC_ASSERT(state() == EngineSetupOk, /**/);
    d->queueSetupInferior();
781 782
}

hjk's avatar
hjk committed
783
void DebuggerEnginePrivate::doSetupInferior()
784
{
785
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
786
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
787
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
788 789 790 791 792
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
793
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
794
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
hjk's avatar
hjk committed
795
    setState(InferiorSetupFailed);
796 797
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
798 799 800 801
}

void DebuggerEngine::notifyInferiorSetupOk()
{
hjk's avatar
hjk committed
802
    showMessage(_("NOTE: INFERIOR SETUP OK"));
803 804 805 806
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
807 808
}

809 810 811 812 813 814 815
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
    QTC_ASSERT(state() == InferiorSetupOk, /**/);
    d->queueRunEngine();
}

hjk's avatar
hjk committed
816 817
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
818
    m_engine->showMessage(_("CALL: RUN ENGINE"));
819
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
820
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
821 822 823
    m_engine->runEngine();
}

hjk's avatar
hjk committed
824 825
void DebuggerEngine::notifyInferiorUnrunnable()
{
hjk's avatar
hjk committed
826
    showMessage(_("NOTE: INFERIOR UNRUNNABLE"));
827 828
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
829
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
830 831 832 833 834
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{
hjk's avatar
hjk committed
835
    showMessage(_("NOTE: ENGINE RUN FAILED"));
836
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
837 838 839
    d->m_progress.setProgressValue(900);
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
hjk's avatar
hjk committed
840
    setState(EngineRunFailed);
841 842
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
843 844 845 846
}

void DebuggerEngine::notifyEngineRunAndInferiorRunOk()
{
hjk's avatar
hjk committed
847
    showMessage(_("NOTE: ENGINE RUN AND INFERIOR RUN OK"));
848 849
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
850
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
851
    setState(InferiorRunOk);
hjk's avatar
hjk committed
852 853 854 855
}

void DebuggerEngine::notifyEngineRunAndInferiorStopOk()
{
hjk's avatar
hjk committed
856
    showMessage(_("NOTE: ENGINE RUN AND INFERIOR STOP OK"));
857 858
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
859
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
860
    setState(InferiorStopOk);
hjk's avatar
hjk committed
861 862 863 864
}

void DebuggerEngine::notifyInferiorRunRequested()
{
hjk's avatar
hjk committed
865
    showMessage(_("NOTE: INFERIOR RUN REQUESTED"));
866
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << this << state());
hjk's avatar
hjk committed
867 868 869 870 871
    setState(InferiorRunRequested);
}

void DebuggerEngine::notifyInferiorRunOk()
{
hjk's avatar
hjk committed
872
    showMessage(_("NOTE: INFERIOR RUN OK"));
873
    QTC_ASSERT(state() == InferiorRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
874 875 876 877 878
    setState(InferiorRunOk);
}

void DebuggerEngine::notifyInferiorRunFailed()
{
hjk's avatar
hjk committed
879
    showMessage(_("NOTE: INFERIOR RUN FAILED"));
880
    QTC_ASSERT(state() == InferiorRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
881 882
    setState(InferiorRunFailed);
    setState(InferiorStopOk);
hjk's avatar
hjk committed
883 884
    if (isDying())
        d->queueShutdownInferior();
hjk's avatar
hjk committed
885 886 887 888
}

void DebuggerEngine::notifyInferiorStopOk()
{
hjk's avatar
hjk committed
889 890
    showMessage(_("NOTE: INFERIOR STOP OK"));
    // Ignore spurious notifications after we are set to die.
hjk's avatar
hjk committed
891
    if (isDying()) {
hjk's avatar
hjk committed
892 893 894 895 896 897 898 899 900 901 902 903 904
        showMessage(_("NOTE: ... WHILE DYING. "));
        // Forward state to "StopOk" if needed.
        if (state() == InferiorStopRequested
                || state() == InferiorRunRequested
                || state() == InferiorRunOk) {
            showMessage(_("NOTE: ... FORWARDING TO 'STOP OK'. "));
            setState(InferiorStopOk);
        }
        if (state() == InferiorStopOk || state() == InferiorStopFailed) {
            d->queueShutdownInferior();
        }
        showMessage(_("NOTE: ... IGNORING STOP MESSAGE"));
        return;
hjk's avatar
hjk committed
905
    }
906
    QTC_ASSERT(state() == InferiorStopRequested, qDebug() << this << state());
hjk's avatar
hjk committed
907
    setState(InferiorStopOk);
hjk's avatar
hjk committed
908