debuggerengine.cpp 45.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 7 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15 16 17 18 19 20 21 22 23 24
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31 32 33 34 35 36
**
**************************************************************************/

#include "debuggerengine.h"

#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

hjk's avatar
hjk committed
60
#include <projectexplorer/toolchain.h>
61
#include <projectexplorer/toolchaintype.h>
62

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

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

#include <QtCore/QDebug>
#include <QtCore/QTimer>
71
#include <QtCore/QFutureInterface>
72

73
#include <QtGui/QMessageBox>
74

hjk's avatar
hjk committed
75
using namespace Core;
76
using namespace Debugger::Internal;
hjk's avatar
hjk committed
77 78
using namespace ProjectExplorer;
using namespace TextEditor;
79

80 81 82 83
enum { debug = 0 };

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

85

86 87 88 89 90 91
///////////////////////////////////////////////////////////////////////
//
// DebuggerStartParameters
//
///////////////////////////////////////////////////////////////////////

92 93
namespace Debugger {

Friedemann Kleint's avatar
Friedemann Kleint committed
94 95 96 97 98 99 100 101 102 103 104
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;
}

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

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

130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
///////////////////////////////////////////////////////////////////////
//
// LocationMark
//
///////////////////////////////////////////////////////////////////////

// Used in "real" editors
class LocationMark : public TextEditor::BaseTextMark
{
public:
    LocationMark(const QString &fileName, int linenumber)
        : BaseTextMark(fileName, linenumber)
    {}

    QIcon icon() const { return debuggerCore()->locationMarkIcon(); }
    void updateLineNumber(int /*lineNumber*/) {}
    void updateBlock(const QTextBlock & /*block*/) {}
    void removedFromEditor() {}
};



153 154 155 156 157 158
//////////////////////////////////////////////////////////////////////
//
// DebuggerEnginePrivate
//
//////////////////////////////////////////////////////////////////////

159
class DebuggerEnginePrivate : public QObject
160
{
161 162
    Q_OBJECT

163
public:
164 165 166
    DebuggerEnginePrivate(DebuggerEngine *engine,
            DebuggerEngine *masterEngine,
            const DebuggerStartParameters &sp)
167
      : m_engine(engine),
168
        m_masterEngine(masterEngine),
169 170 171
        m_runControl(0),
        m_startParameters(sp),
        m_state(DebuggerNotReady),
hjk's avatar
hjk committed
172
        m_lastGoodState(DebuggerNotReady),
Friedemann Kleint's avatar
Friedemann Kleint committed
173
        m_targetState(DebuggerNotReady),
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 182
        m_memoryAgent(engine),
        m_isStateDebugging(false)
183
    {
184
        connect(&m_locationTimer, SIGNAL(timeout()), SLOT(resetLocation()));
185
    }
186 187

    ~DebuggerEnginePrivate() {}
188

189
public slots:
190
    void doSetupEngine();
hjk's avatar
hjk committed
191 192
    void doSetupInferior();
    void doRunEngine();
hjk's avatar
hjk committed
193 194
    void doShutdownEngine();
    void doShutdownInferior();
195
    void doInterruptInferior();
hjk's avatar
hjk committed
196
    void doFinishDebugger();
hjk's avatar
hjk committed
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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()));
    }

212 213
    void queueRunEngine()
    {
hjk's avatar
hjk committed
214 215 216 217 218
        m_engine->setState(EngineRunRequested);
        m_engine->showMessage(_("QUEUE: RUN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doRunEngine()));
    }

219 220
    void queueShutdownEngine()
    {
hjk's avatar
hjk committed
221 222 223 224 225
        m_engine->setState(EngineShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN ENGINE"));
        QTimer::singleShot(0, this, SLOT(doShutdownEngine()));
    }

226 227
    void queueShutdownInferior()
    {
hjk's avatar
hjk committed
228 229 230 231 232
        m_engine->setState(InferiorShutdownRequested);
        m_engine->showMessage(_("QUEUE: SHUTDOWN INFERIOR"));
        QTimer::singleShot(0, this, SLOT(doShutdownInferior()));
    }

233 234
    void queueFinishDebugger()
    {
235 236 237 238
        QTC_ASSERT(state() == EngineShutdownOk
            || state() == EngineShutdownFailed, qDebug() << state());
        m_engine->setState(DebuggerFinished);
        m_engine->showMessage(_("QUEUE: FINISH DEBUGGER"));
hjk's avatar
hjk committed
239 240 241
        QTimer::singleShot(0, this, SLOT(doFinishDebugger()));
    }

242 243
    void raiseApplication()
    {
244 245
        QTC_ASSERT(runControl(), return);
        runControl()->bringApplicationToForeground(m_inferiorPid);
246 247
    }

248
    void scheduleResetLocation()
249
    {
250
        m_stackHandler.scheduleResetLocation();
251 252 253 254
        m_locationTimer.setSingleShot(true);
        m_locationTimer.start(80);
    }

255
    void resetLocation()
256 257 258
    {
        m_locationTimer.stop();
        m_locationMark.reset();
259 260
        m_stackHandler.resetLocation();
        m_disassemblerAgent.resetLocation();
261 262
    }

263
public:
hjk's avatar
hjk committed
264
    DebuggerState state() const { return m_state; }
265 266 267
    bool isMasterEngine() const { return m_engine->isMasterEngine(); }
    DebuggerRunControl *runControl() const
        { return m_masterEngine ? m_masterEngine->runControl() : m_runControl; }
hjk's avatar
hjk committed
268

269
    DebuggerEngine *m_engine; // Not owned.
270
    DebuggerEngine *m_masterEngine; // Not owned
271 272 273
    DebuggerRunControl *m_runControl;  // Not owned.

    DebuggerStartParameters m_startParameters;
hjk's avatar
hjk committed
274 275

    // The current state.
276 277
    DebuggerState m_state;

hjk's avatar
hjk committed
278 279 280 281 282 283
    // The state we had before something unexpected happend.
    DebuggerState m_lastGoodState;

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

284 285 286 287 288 289 290 291
    qint64 m_inferiorPid;

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

294 295
    DisassemblerAgent m_disassemblerAgent;
    MemoryAgent m_memoryAgent;
296 297
    QScopedPointer<TextEditor::BaseTextMark> m_locationMark;
    QTimer m_locationTimer;
298 299

    bool m_isStateDebugging;
300 301 302 303 304 305 306 307 308
};


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

309 310 311
DebuggerEngine::DebuggerEngine(const DebuggerStartParameters &startParameters,
        DebuggerEngine *parentEngine)
  : d(new DebuggerEnginePrivate(this, parentEngine, startParameters))
312 313 314 315 316
{
}

DebuggerEngine::~DebuggerEngine()
{
317 318
    disconnect();
    delete d;
319 320
}

321 322 323 324 325 326 327 328 329 330 331
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)
332
        SN(InferiorSetupOk)
333 334 335 336 337 338 339 340
        SN(EngineRunRequested)
        SN(InferiorRunRequested)
        SN(InferiorRunOk)
        SN(InferiorRunFailed)
        SN(InferiorUnrunnable)
        SN(InferiorStopRequested)
        SN(InferiorStopOk)
        SN(InferiorStopFailed)
341
        SN(InferiorExitOk)
342 343 344 345 346 347 348 349 350 351 352 353
        SN(InferiorShutdownRequested)
        SN(InferiorShutdownOk)
        SN(InferiorShutdownFailed)
        SN(EngineShutdownRequested)
        SN(EngineShutdownOk)
        SN(EngineShutdownFailed)
        SN(DebuggerFinished)
    }
    return "<unknown>";
#    undef SN
}

354
void DebuggerEngine::showStatusMessage(const QString &msg, int timeout) const
355
{
356
    showMessage(msg, StatusBar, timeout);
357
}
358

359

360 361 362 363 364 365 366 367 368 369 370 371 372 373
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
{
374 375 376
    return d->m_masterEngine
        ? d->m_masterEngine->modulesHandler()
        : &d->m_modulesHandler;
377 378 379 380
}

RegisterHandler *DebuggerEngine::registerHandler() const
{
381 382 383
    return d->m_masterEngine
        ? d->m_masterEngine->registerHandler()
        : &d->m_registerHandler;
384 385 386 387
}

StackHandler *DebuggerEngine::stackHandler() const
{
hjk's avatar
hjk committed
388 389 390 391
    //return d->m_masterEngine
    //    ? d->m_masterEngine->stackHandler()
    //    : &d->m_stackHandler;
    return &d->m_stackHandler;
392 393 394 395
}

ThreadsHandler *DebuggerEngine::threadsHandler() const
{
396 397 398
    return d->m_masterEngine
        ? d->m_masterEngine->threadsHandler()
        : &d->m_threadsHandler;
399 400 401 402
}

WatchHandler *DebuggerEngine::watchHandler() const
{
403 404 405
    return d->m_masterEngine
        ? d->m_masterEngine->watchHandler()
        : &d->m_watchHandler;
406 407 408 409
}

SourceFilesHandler *DebuggerEngine::sourceFilesHandler() const
{
410 411 412
    return d->m_masterEngine
        ? d->m_masterEngine->sourceFilesHandler()
        : &d->m_sourceFilesHandler;
413 414 415 416
}

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

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

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

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

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

QAbstractItemModel *DebuggerEngine::watchersModel() const
{
457
    QAbstractItemModel *model = watchHandler()->model(WatchersWatch);
Friedemann Kleint's avatar
Friedemann Kleint committed
458 459 460
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("WatchersModel"));
    return model;
461 462 463 464
}

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

471 472 473 474 475 476 477 478
QAbstractItemModel *DebuggerEngine::toolTipsModel() const
{
    QAbstractItemModel *model = watchHandler()->model(TooltipsWatch);
    if (model->objectName().isEmpty()) // Make debugging easier.
        model->setObjectName(objectName() + QLatin1String("TooltipsModel"));
    return model;
}

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

487
void DebuggerEngine::fetchMemory(MemoryAgent *, QObject *,
488 489 490 491 492 493 494 495 496 497 498 499 500 501
        quint64 addr, quint64 length)
{
    Q_UNUSED(addr);
    Q_UNUSED(length);
}

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
{
502 503 504 505
    if (d->m_masterEngine) {
        d->m_masterEngine->showMessage(msg, channel, timeout);
        return;
    }
506 507
    //if (msg.size() && msg.at(0).isUpper() && msg.at(1).isUpper())
    //    qDebug() << qPrintable(msg) << "IN STATE" << state();
508
    debuggerCore()->showMessage(msg, channel, timeout);
509 510 511 512 513
    if (d->m_runControl) {
        d->m_runControl->showMessage(msg, channel);
    } else {
        qWarning("Warning: %s (no active run control)", qPrintable(msg));
    }
514 515 516 517
}

void DebuggerEngine::startDebugger(DebuggerRunControl *runControl)
{
518 519
    QTC_ASSERT(runControl, notifyEngineSetupFailed(); return);
    QTC_ASSERT(!d->m_runControl, notifyEngineSetupFailed(); return);
520

521 522 523 524 525 526 527
    d->m_progress.setProgressRange(0, 1000);
    Core::FutureProgress *fp = Core::ICore::instance()->progressManager()
        ->addTask(d->m_progress.future(),
        tr("Launching"), _("Debugger.Launcher"));
    fp->setKeepOnFinish(Core::FutureProgress::DontKeepOnFinish);
    d->m_progress.reportStarted();

528 529 530 531 532
    d->m_runControl = runControl;

    d->m_inferiorPid = d->m_startParameters.attachPID > 0
        ? d->m_startParameters.attachPID : 0;

533 534
    if (!d->m_startParameters.environment.size())
        d->m_startParameters.environment = Utils::Environment();
535 536

    const unsigned engineCapabilities = debuggerCapabilities();
hjk's avatar
hjk committed
537
    debuggerCore()->action(OperateByInstruction)
538 539
        ->setEnabled(engineCapabilities & DisassemblerCapability);

540 541
    QTC_ASSERT(state() == DebuggerNotReady || state() == DebuggerFinished,
         qDebug() << state());
542 543
    d->m_lastGoodState = DebuggerNotReady;
    d->m_targetState = DebuggerNotReady;
544
    d->m_progress.setProgressValue(200);
545
    d->queueSetupEngine();
546 547 548 549
}

void DebuggerEngine::resetLocation()
{
550 551
    // Do it after some delay to avoid flicker.
    d->scheduleResetLocation();
552 553
}

554
void DebuggerEngine::gotoLocation(const Location &loc)
555
{
556 557 558 559 560
    if (debuggerCore()->boolSetting(OperateByInstruction) || !loc.hasDebugInfo()) {
        d->m_disassemblerAgent.setTryMixed(true);
        d->m_disassemblerAgent.setLocation(loc);
        return;
    }
561 562 563 564
    // CDB might hit on breakpoints while shutting down.
    //if (m_shuttingDown)
    //    return;

565
    d->resetLocation();
566

567 568
    const QString file = loc.fileName();
    const int line = loc.lineNumber();
hjk's avatar
hjk committed
569 570
    EditorManager *editorManager = EditorManager::instance();
    QList<IEditor *> editors = editorManager->editorsForFileName(file);
571
    IEditor *editor = 0;
hjk's avatar
hjk committed
572
    if (editors.isEmpty()) {
573 574 575 576 577 578 579 580
        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
581
    }
582
    ITextEditor *texteditor = qobject_cast<ITextEditor *>(editor);
hjk's avatar
hjk committed
583 584 585
    if (texteditor)
        texteditor->gotoLine(line, 0);

586
    if (loc.needsMarker())
587
        d->m_locationMark.reset(new LocationMark(file, line));
588 589

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

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

605 606
// Called from RunControl.
void DebuggerEngine::handleFinished()
607
{
608
    showMessage("HANDLE RUNCONTROL FINISHED");
609
    d->m_runControl = 0;
610 611 612 613
    modulesHandler()->removeAll();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    watchHandler()->cleanup();
614
    d->m_progress.setProgressValue(1000);
615
    d->m_progress.reportFinished();
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
}

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
640
    return debuggerCore()->boolSetting(UseDebuggingHelpers);
641 642 643 644
}

QStringList DebuggerEngine::qtDumperLibraryLocations() const
{
hjk's avatar
hjk committed
645
    if (debuggerCore()->action(UseCustomDebuggingHelperLocation)->value().toBool()) {
646
        const QString customLocation =
hjk's avatar
hjk committed
647
            debuggerCore()->action(CustomDebuggingHelperLocation)->value().toString();
648 649 650 651 652 653 654 655 656
        const QString location =
            tr("%1 (explicitly set in the Debugger Options)").arg(customLocation);
        return QStringList(location);
    }
    return d->m_startParameters.dumperLibraryLocations;
}

void DebuggerEngine::showQtDumperLibraryWarning(const QString &details)
{
657
    debuggerCore()->showQtDumperLibraryWarning(details);
658 659 660 661
}

QString DebuggerEngine::qtDumperLibraryName() const
{
hjk's avatar
hjk committed
662 663
    if (debuggerCore()->action(UseCustomDebuggingHelperLocation)->value().toBool())
        return debuggerCore()->action(CustomDebuggingHelperLocation)->value().toString();
664 665 666 667 668 669 670 671
    return startParameters().dumperLibrary;
}

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

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

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

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

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

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

    case EngineRunRequested:
hjk's avatar
hjk committed
705 706 707 708
        return to == EngineRunFailed
            || 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 718 719
        return to == InferiorStopRequested
            || to == InferiorStopOk // A spontaneous stop.
            || to == InferiorExitOk;
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 || InferiorExitOk;
726
    case InferiorStopFailed:
hjk's avatar
hjk committed
727
        return to == EngineShutdownRequested;
728

729 730 731
    case InferiorExitOk:
        return to == InferiorShutdownOk;

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

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

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

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

756 757 758 759 760 761 762 763 764 765 766 767 768
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();
}

769
void DebuggerEngine::notifyEngineSetupFailed()
hjk's avatar
hjk committed
770
{
hjk's avatar
hjk committed
771
    showMessage(_("NOTE: ENGINE SETUP FAILED"));
772
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
773
    setState(EngineSetupFailed);
hjk's avatar
hjk committed
774
    if (isMasterEngine() && runControl())
775
        runControl()->startFailed();
hjk's avatar
hjk committed
776
    setState(DebuggerFinished);
hjk's avatar
hjk committed
777 778
}

779
void DebuggerEngine::notifyEngineSetupOk()
780
{
hjk's avatar
hjk committed
781
    showMessage(_("NOTE: ENGINE SETUP OK"));
782
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << this << state());
783
    setState(EngineSetupOk);
hjk's avatar
hjk committed
784
    showMessage(_("QUEUE: SETUP INFERIOR"));
785
    if (isMasterEngine())
786 787 788 789 790 791 792
        d->queueSetupInferior();
}

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

hjk's avatar
hjk committed
795
void DebuggerEnginePrivate::doSetupInferior()
796
{
797
    m_engine->showMessage(_("CALL: SETUP INFERIOR"));
798
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << m_engine << state());
799
    m_progress.setProgressValue(250);
hjk's avatar
hjk committed
800 801 802 803 804
    m_engine->setupInferior();
}

void DebuggerEngine::notifyInferiorSetupFailed()
{
hjk's avatar
hjk committed
805
    showMessage(_("NOTE: INFERIOR SETUP FAILED"));
806
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
hjk's avatar
hjk committed
807
    setState(InferiorSetupFailed);
808 809
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
810 811 812 813
}

void DebuggerEngine::notifyInferiorSetupOk()
{
hjk's avatar
hjk committed
814
    showMessage(_("NOTE: INFERIOR SETUP OK"));
815 816 817 818
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << this << state());
    setState(InferiorSetupOk);
    if (isMasterEngine())
        d->queueRunEngine();
hjk's avatar
hjk committed
819 820
}

821 822 823 824 825 826 827
void DebuggerEngine::runSlaveEngine()
{
    QTC_ASSERT(isSlaveEngine(), return);
    QTC_ASSERT(state() == InferiorSetupOk, /**/);
    d->queueRunEngine();
}

hjk's avatar
hjk committed
828 829
void DebuggerEnginePrivate::doRunEngine()
{
hjk's avatar
hjk committed
830
    m_engine->showMessage(_("CALL: RUN ENGINE"));
831
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << m_engine << state());
832
    m_progress.setProgressValue(300);
hjk's avatar
hjk committed
833 834 835
    m_engine->runEngine();
}

hjk's avatar
hjk committed
836 837
void DebuggerEngine::notifyInferiorUnrunnable()
{
hjk's avatar
hjk committed
838
    showMessage(_("NOTE: INFERIOR UNRUNNABLE"));
839 840
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
841
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
842 843 844 845 846
    setState(InferiorUnrunnable);
}

void DebuggerEngine::notifyEngineRunFailed()
{
hjk's avatar
hjk committed
847
    showMessage(_("NOTE: ENGINE RUN FAILED"));
848
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
849 850 851
    d->m_progress.setProgressValue(900);
    d->m_progress.reportCanceled();
    d->m_progress.reportFinished();
hjk's avatar
hjk committed
852
    setState(EngineRunFailed);
853 854
    if (isMasterEngine())
        d->queueShutdownEngine();
hjk's avatar
hjk committed
855 856 857 858
}

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

void DebuggerEngine::notifyEngineRunAndInferiorStopOk()
{
hjk's avatar
hjk committed
868
    showMessage(_("NOTE: ENGINE RUN AND INFERIOR STOP OK"));
869 870
    d->m_progress.setProgressValue(1000);
    d->m_progress.reportFinished();
871
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
872
    setState(InferiorStopOk);
hjk's avatar
hjk committed
873 874 875 876
}

void DebuggerEngine::notifyInferiorRunRequested()
{
hjk's avatar
hjk committed
877
    showMessage(_("NOTE: INFERIOR RUN REQUESTED"));
878
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << this << state());
hjk's avatar
hjk committed
879 880 881 882 883
    setState(InferiorRunRequested);
}

void DebuggerEngine::notifyInferiorRunOk()
{
hjk's avatar
hjk committed
884
    showMessage(_("NOTE: INFERIOR RUN OK"));
885
    QTC_ASSERT(state() == InferiorRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
886 887 888 889 890
    setState(InferiorRunOk);
}

void DebuggerEngine::notifyInferiorRunFailed()
{
hjk's avatar
hjk committed
891
    showMessage(_("NOTE: INFERIOR RUN FAILED"));
892
    QTC_ASSERT(state() == InferiorRunRequested, qDebug() << this << state());
hjk's avatar
hjk committed
893 894
    setState(InferiorRunFailed);
    setState(InferiorStopOk);
hjk's avatar
hjk committed
895 896
    if (isDying())
        d->queueShutdownInferior();
hjk's avatar
hjk committed
897 898 899 900
}

void DebuggerEngine::notifyInferiorStopOk()
{
hjk's avatar
hjk committed
901 902
    showMessage(_("NOTE: INFERIOR STOP OK"));
    // Ignore spurious notifications after we are set to die.
hjk's avatar
hjk committed
903
    if (isDying()) {
hjk's avatar
hjk committed
904 905 906 907 908 909 910 911 912 913 914 915 916
        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
917
    }
918
    QTC_ASSERT(state() == InferiorStopRequested, qDebug() << this << state());
hjk's avatar
hjk committed
919
    setState(InferiorStopOk);
hjk's avatar
hjk committed
920 921
}

hjk's avatar
hjk committed
922
void DebuggerEngine::notifyInferiorSpontaneousStop()
923
{
hjk's avatar
hjk committed
924
    showMessage(_("NOTE: INFERIOR SPONTANEOUES STOP"));
925
    QTC_ASSERT(state() == InferiorRunOk, qDebug() << this << state());
926
    setState(InferiorStopOk);