debuggerplugin.cpp 47.2 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "debuggerplugin.h"

32
#include "breakhandler.h"
33
#include "debuggeractions.h"
34
#include "debuggerdialogs.h"
con's avatar
con committed
35
36
37
#include "debuggerconstants.h"
#include "debuggermanager.h"
#include "debuggerrunner.h"
38
#include "debuggerstringutils.h"
39
40
#include "debuggeruiswitcher.h"
#include "debuggermainwindow.h"
con's avatar
con committed
41

42
#include "ui_commonoptionspage.h"
hjk's avatar
hjk committed
43
#include "ui_dumperoptionpage.h"
44

45
#include <coreplugin/actionmanager/actionmanager.h>
hjk's avatar
hjk committed
46
#include <coreplugin/basemode.h>
con's avatar
con committed
47
#include <coreplugin/coreconstants.h>
48
#include <coreplugin/dialogs/ioptionspage.h>
con's avatar
con committed
49
#include <coreplugin/editormanager/editormanager.h>
hjk's avatar
hjk committed
50
#include <coreplugin/findplaceholder.h>
con's avatar
con committed
51
#include <coreplugin/icore.h>
52
#include <coreplugin/icorelistener.h>
con's avatar
con committed
53
#include <coreplugin/messagemanager.h>
hjk's avatar
hjk committed
54
#include <coreplugin/minisplitter.h>
con's avatar
con committed
55
#include <coreplugin/modemanager.h>
hjk's avatar
hjk committed
56
57
58
#include <coreplugin/navigationwidget.h>
#include <coreplugin/outputpane.h>
#include <coreplugin/rightpane.h>
con's avatar
con committed
59
#include <coreplugin/uniqueidmanager.h>
hjk's avatar
hjk committed
60

con's avatar
con committed
61
#include <cplusplus/ExpressionUnderCursor.h>
hjk's avatar
hjk committed
62

con's avatar
con committed
63
#include <cppeditor/cppeditorconstants.h>
hjk's avatar
hjk committed
64

65
66
#include <extensionsystem/pluginmanager.h>

67
#include <coreplugin/manhattanstyle.h>
68
#include <projectexplorer/projectexplorer.h>
con's avatar
con committed
69
70
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/session.h>
71
#include <projectexplorer/project.h>
hjk's avatar
hjk committed
72
73

#include <texteditor/basetexteditor.h>
con's avatar
con committed
74
75
76
#include <texteditor/basetextmark.h>
#include <texteditor/itexteditor.h>
#include <texteditor/texteditorconstants.h>
77
#include <texteditor/texteditorplugin.h>
hjk's avatar
hjk committed
78
79

#include <utils/qtcassert.h>
80
#include <utils/styledbar.h>
con's avatar
con committed
81
82
83
84
85

#include <QtCore/QDebug>
#include <QtCore/QObject>
#include <QtCore/QPoint>
#include <QtCore/QSettings>
86
#include <QtCore/QtPlugin>
87
#include <QtCore/QCoreApplication>
88
#include <QtCore/QTimer>
89
#include <QtCore/QVariant>
hjk's avatar
hjk committed
90

91
#include <QtGui/QLineEdit>
92
#include <QtGui/QDockWidget>
con's avatar
con committed
93
94
95
#include <QtGui/QPlainTextEdit>
#include <QtGui/QTextBlock>
#include <QtGui/QTextCursor>
96
#include <QtGui/QToolButton>
97
#include <QtGui/QMessageBox>
con's avatar
con committed
98

99
100
#include <climits>

con's avatar
con committed
101
using namespace Core;
102
using namespace Debugger;
hjk's avatar
hjk committed
103
104
using namespace Debugger::Constants;
using namespace Debugger::Internal;
con's avatar
con committed
105
using namespace ProjectExplorer;
hjk's avatar
hjk committed
106
using namespace TextEditor;
con's avatar
con committed
107

hjk's avatar
hjk committed
108
109
110
namespace CC = Core::Constants;
namespace PE = ProjectExplorer::Constants;

con's avatar
con committed
111
112
113
114

namespace Debugger {
namespace Constants {

115
116
const char * const M_DEBUG_START_DEBUGGING = "QtCreator.Menu.Debug.StartDebugging";

con's avatar
con committed
117
118
const char * const STARTEXTERNAL        = "Debugger.StartExternal";
const char * const ATTACHEXTERNAL       = "Debugger.AttachExternal";
119
const char * const ATTACHCORE           = "Debugger.AttachCore";
120
const char * const ATTACHREMOTE         = "Debugger.AttachRemote";
121
const char * const DETACH               = "Debugger.Detach";
con's avatar
con committed
122

123
124
const char * const RUN_TO_LINE1         = "Debugger.RunToLine1";
const char * const RUN_TO_LINE2         = "Debugger.RunToLine2";
con's avatar
con committed
125
const char * const RUN_TO_FUNCTION      = "Debugger.RunToFunction";
126
127
const char * const JUMP_TO_LINE1        = "Debugger.JumpToLine1";
const char * const JUMP_TO_LINE2        = "Debugger.JumpToLine2";
128
const char * const RETURN_FROM_FUNCTION = "Debugger.ReturnFromFunction";
129
const char * const SNAPSHOT             = "Debugger.Snapshot";
con's avatar
con committed
130
131
132
const char * const TOGGLE_BREAK         = "Debugger.ToggleBreak";
const char * const BREAK_BY_FUNCTION    = "Debugger.BreakByFunction";
const char * const BREAK_AT_MAIN        = "Debugger.BreakAtMain";
133
134
const char * const ADD_TO_WATCH1        = "Debugger.AddToWatch1";
const char * const ADD_TO_WATCH2        = "Debugger.AddToWatch2";
135
const char * const OPERATE_BY_INSTRUCTION  = "Debugger.OperateByInstruction";
con's avatar
con committed
136

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
137
#ifdef Q_WS_MAC
con's avatar
con committed
138
139
140
141
142
const char * const INTERRUPT_KEY            = "Shift+F5";
const char * const RESET_KEY                = "Ctrl+Shift+F5";
const char * const STEP_KEY                 = "F7";
const char * const STEPOUT_KEY              = "Shift+F7";
const char * const NEXT_KEY                 = "F6";
143
const char * const REVERSE_KEY              = "";
con's avatar
con committed
144
145
146
147
148
149
150
const char * const RUN_TO_LINE_KEY          = "Shift+F8";
const char * const RUN_TO_FUNCTION_KEY      = "Ctrl+F6";
const char * const JUMP_TO_LINE_KEY         = "Alt+D,Alt+L";
const char * const TOGGLE_BREAK_KEY         = "F8";
const char * const BREAK_BY_FUNCTION_KEY    = "Alt+D,Alt+F";
const char * const BREAK_AT_MAIN_KEY        = "Alt+D,Alt+M";
const char * const ADD_TO_WATCH_KEY         = "Alt+D,Alt+W";
151
const char * const SNAPSHOT_KEY             = "Alt+D,Alt+S";
con's avatar
con committed
152
153
154
155
156
157
#else
const char * const INTERRUPT_KEY            = "Shift+F5";
const char * const RESET_KEY                = "Ctrl+Shift+F5";
const char * const STEP_KEY                 = "F11";
const char * const STEPOUT_KEY              = "Shift+F11";
const char * const NEXT_KEY                 = "F10";
158
const char * const REVERSE_KEY              = "F12";
con's avatar
con committed
159
160
161
162
163
164
165
const char * const RUN_TO_LINE_KEY          = "";
const char * const RUN_TO_FUNCTION_KEY      = "";
const char * const JUMP_TO_LINE_KEY         = "";
const char * const TOGGLE_BREAK_KEY         = "F9";
const char * const BREAK_BY_FUNCTION_KEY    = "";
const char * const BREAK_AT_MAIN_KEY        = "";
const char * const ADD_TO_WATCH_KEY         = "Ctrl+Alt+Q";
166
const char * const SNAPSHOT_KEY             = "Alt+D,Alt+S";
con's avatar
con committed
167
168
169
170
171
172
#endif

} // namespace Constants
} // namespace Debugger


173
174
175
176
177
static ProjectExplorer::SessionManager *sessionManager()
{
    return ProjectExplorer::ProjectExplorerPlugin::instance()->session();
}

178
179
180
181
182
static QSettings *settings()
{
    return ICore::instance()->settings();
}

183
184
185
186
187
188
189
static QToolButton *toolButton(QAction *action)
{
    QToolButton *button = new QToolButton;
    button->setDefaultAction(action);
    return button;
}

190
191
192
193
194
195
///////////////////////////////////////////////////////////////////////
//
// DebugMode
//
///////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
196
197
198
199
200
201
202
203
204
205
206
207
208
namespace Debugger {
namespace Internal {

class DebugMode : public Core::BaseMode
{
    Q_OBJECT

public:
    DebugMode(QObject *parent = 0);
    ~DebugMode();
};

DebugMode::DebugMode(QObject *parent)
hjk's avatar
hjk committed
209
  : BaseMode(parent)
hjk's avatar
hjk committed
210
{
211
    setDisplayName(tr("Debug"));
hjk's avatar
hjk committed
212
    setId(MODE_DEBUG);
hjk's avatar
hjk committed
213
    setIcon(QIcon(":/fancyactionbar/images/mode_Debug.png"));
hjk's avatar
hjk committed
214
    setPriority(P_MODE_DEBUG);
hjk's avatar
hjk committed
215
216
217
218
219
220
221
222
}

DebugMode::~DebugMode()
{
    // Make sure the editor manager does not get deleted
    EditorManager::instance()->setParent(0);
}

223
224
225
226
227
228
///////////////////////////////////////////////////////////////////////
//
// DebuggerListener: Close the debugging session if running.
//
///////////////////////////////////////////////////////////////////////

229
230
class DebuggerListener : public Core::ICoreListener
{
231
232
    Q_OBJECT
public:
hjk's avatar
hjk committed
233
    DebuggerListener() {}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    virtual bool coreAboutToClose();
};

bool DebuggerListener::coreAboutToClose()
{
    DebuggerManager *mgr = DebuggerManager::instance();
    if (!mgr)
        return true;
    // Ask to terminate the session.
    bool cleanTermination = false;
    switch (mgr->state()) {
    case DebuggerNotReady:
        return true;
    case AdapterStarted:     // Most importantly, terminating a running
    case AdapterStartFailed: // debuggee can cause problems.
    case InferiorUnrunnable:
    case InferiorStartFailed:
    case InferiorStopped:
    case InferiorShutDown:
        cleanTermination = true;
        break;
    default:
        break;
    }
hjk's avatar
hjk committed
258

259
    const QString question = cleanTermination ?
hjk's avatar
hjk committed
260
261
262
263
        tr("A debugging session is still in progress.\n"
           "Would you like to terminate it?") :
        tr("A debugging session is still in progress. "
           "Terminating the session in the current"
264
           " state (%1) can leave the target in an inconsistent state."
265
           " Would you still like to terminate it?")
hjk's avatar
hjk committed
266
267
268
269
270
271
272
        .arg(_(DebuggerManager::stateName(mgr->state())));

    QMessageBox::StandardButton answer =
        QMessageBox::question(DebuggerUISwitcher::instance()->mainWindow(),
            tr("Close Debugging Session"), question,
            QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);

273
    if (answer != QMessageBox::Yes)
274
        return false;
hjk's avatar
hjk committed
275

276
    mgr->exitDebugger();
277
    QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
278
279
280
    return true;
}

281
282
283
} // namespace Internal
} // namespace Debugger

hjk's avatar
hjk committed
284

con's avatar
con committed
285
286
287
288
289
290
///////////////////////////////////////////////////////////////////////
//
// LocationMark
//
///////////////////////////////////////////////////////////////////////

291
292
293
namespace Debugger {
namespace Internal {

294
// Used in "real" editors
295
class LocationMark : public TextEditor::BaseTextMark
con's avatar
con committed
296
297
298
299
300
301
{
    Q_OBJECT

public:
    LocationMark(const QString &fileName, int linenumber)
        : BaseTextMark(fileName, linenumber)
302
    {}
con's avatar
con committed
303

304
    QIcon icon() const { return DebuggerManager::instance()->locationMarkIcon(); }
con's avatar
con committed
305
306
    void updateLineNumber(int /*lineNumber*/) {}
    void updateBlock(const QTextBlock & /*block*/) {}
307
    void removedFromEditor() {}
con's avatar
con committed
308
309
};

310
311
312
313
314
315
} // namespace Internal
} // namespace Debugger


///////////////////////////////////////////////////////////////////////
//
316
// CommonOptionsPage
317
318
319
320
321
322
//
///////////////////////////////////////////////////////////////////////

namespace Debugger {
namespace Internal {

323
class CommonOptionsPage : public Core::IOptionsPage
324
325
326
327
{
    Q_OBJECT

public:
328
    CommonOptionsPage() {}
329
330

    // IOptionsPage
331
    QString id() const
hjk's avatar
hjk committed
332
        { return _(DEBUGGER_COMMON_SETTINGS_ID); }
333
    QString displayName() const
hjk's avatar
hjk committed
334
        { return QCoreApplication::translate("Debugger", DEBUGGER_COMMON_SETTINGS_NAME); }
335
    QString category() const
hjk's avatar
hjk committed
336
        { return _(DEBUGGER_SETTINGS_CATEGORY);  }
337
    QString displayCategory() const
hjk's avatar
hjk committed
338
        { return QCoreApplication::translate("Debugger", DEBUGGER_SETTINGS_TR_CATEGORY); }
339
340

    QWidget *createPage(QWidget *parent);
341
    void apply() { m_group.apply(settings()); }
342
    void finish() { m_group.finish(); }
343
    virtual bool matches(const QString &s) const;
344
345

private:
346
    Ui::CommonOptionsPage m_ui;
347
    Utils::SavedActionSet m_group;
348
    QString m_searchKeywords;
349
350
};

351
QWidget *CommonOptionsPage::createPage(QWidget *parent)
352
353
354
{
    QWidget *w = new QWidget(parent);
    m_ui.setupUi(w);
355
    m_group.clear();
356

357
    m_group.insert(theDebuggerAction(ListSourceFiles),
358
        m_ui.checkBoxListSourceFiles);
359
    m_group.insert(theDebuggerAction(UseAlternatingRowColors),
360
        m_ui.checkBoxUseAlternatingRowColors);
361
362
    m_group.insert(theDebuggerAction(UseMessageBoxForSignals),
        m_ui.checkBoxUseMessageBoxForSignals);
363
    m_group.insert(theDebuggerAction(SkipKnownFrames),
364
        m_ui.checkBoxSkipKnownFrames);
365
    m_group.insert(theDebuggerAction(UseToolTipsInMainEditor),
366
        m_ui.checkBoxUseToolTipsInMainEditor);
367
    m_group.insert(theDebuggerAction(AutoDerefPointers), 0);
368
369
370
371
    m_group.insert(theDebuggerAction(UseToolTipsInLocalsView), 0);
    m_group.insert(theDebuggerAction(UseToolTipsInBreakpointsView), 0);
    m_group.insert(theDebuggerAction(UseAddressInBreakpointsView), 0);
    m_group.insert(theDebuggerAction(UseAddressInStackView), 0);
372
    m_group.insert(theDebuggerAction(EnableReverseDebugging),
373
        m_ui.checkBoxEnableReverseDebugging);
374
    m_group.insert(theDebuggerAction(MaximalStackDepth),
375
        m_ui.spinBoxMaximalStackDepth);
376
    m_group.insert(theDebuggerAction(GdbWatchdogTimeout), 0);
377
378
    m_group.insert(theDebuggerAction(ShowStdNamespace), 0);
    m_group.insert(theDebuggerAction(ShowQtNamespace), 0);
379
    m_group.insert(theDebuggerAction(LogTimeStamps), 0);
380
    m_group.insert(theDebuggerAction(VerboseLog), 0);
381
    m_group.insert(theDebuggerAction(UsePreciseBreakpoints), 0);
382
383
    m_group.insert(theDebuggerAction(BreakOnThrow), 0);
    m_group.insert(theDebuggerAction(BreakOnCatch), 0);
384

385
386
387
388
389
390
391
392
393
394
395
    if (m_searchKeywords.isEmpty()) {
        QTextStream(&m_searchKeywords) << ' ' << m_ui.checkBoxListSourceFiles->text()
                << ' ' << m_ui.checkBoxUseMessageBoxForSignals->text()
                << ' ' << m_ui.checkBoxUseAlternatingRowColors->text()
                << ' ' << m_ui.checkBoxUseToolTipsInMainEditor->text()
                << ' ' << m_ui.checkBoxSkipKnownFrames->text()
                << ' ' << m_ui.checkBoxEnableReverseDebugging->text()
                << ' ' << m_ui.labelMaximalStackDepth->text();
        m_searchKeywords.remove(QLatin1Char('&'));
    }

396
397
398
    return w;
}

399
400
401
402
403
bool CommonOptionsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

404
405
406
} // namespace Internal
} // namespace Debugger

hjk's avatar
hjk committed
407

hjk's avatar
hjk committed
408
409
///////////////////////////////////////////////////////////////////////
//
410
// DebuggingHelperOptionPage
hjk's avatar
hjk committed
411
412
413
//
///////////////////////////////////////////////////////////////////////

414
415
416
static inline bool oxygenStyle()
{
    if (const ManhattanStyle *ms = qobject_cast<const ManhattanStyle *>(qApp->style()))
417
        return !qstrcmp("OxygenStyle", ms->baseStyle()->metaObject()->className());
418
419
420
    return false;
}

hjk's avatar
hjk committed
421
422
423
namespace Debugger {
namespace Internal {

424
class DebuggingHelperOptionPage : public Core::IOptionsPage
hjk's avatar
hjk committed
425
426
427
428
{
    Q_OBJECT

public:
429
    DebuggingHelperOptionPage() {}
hjk's avatar
hjk committed
430
431

    // IOptionsPage
hjk's avatar
hjk committed
432
    QString id() const { return _("B.DebuggingHelper"); }
433
    QString displayName() const { return tr("Debugging Helper"); }
hjk's avatar
hjk committed
434
435
    QString category() const { return _(DEBUGGER_SETTINGS_CATEGORY); }
    QString displayCategory() const { return QCoreApplication::translate("Debugger", DEBUGGER_SETTINGS_TR_CATEGORY); }
hjk's avatar
hjk committed
436
437

    QWidget *createPage(QWidget *parent);
438
    void apply() { m_group.apply(settings()); }
439
    void finish() { m_group.finish(); }
440
    virtual bool matches(const QString &s) const;
hjk's avatar
hjk committed
441
442

private:
443
    Ui::DebuggingHelperOptionPage m_ui;
444
    Utils::SavedActionSet m_group;
445
    QString m_searchKeywords;
hjk's avatar
hjk committed
446
447
};

448
QWidget *DebuggingHelperOptionPage::createPage(QWidget *parent)
hjk's avatar
hjk committed
449
450
451
452
{
    QWidget *w = new QWidget(parent);
    m_ui.setupUi(w);

453
    m_ui.dumperLocationChooser->setExpectedKind(Utils::PathChooser::Command);
454
    m_ui.dumperLocationChooser->setPromptDialogTitle(tr("Choose DebuggingHelper Location"));
hjk's avatar
hjk committed
455
456
    m_ui.dumperLocationChooser->setInitialBrowsePathBackup(
        Core::ICore::instance()->resourcePath() + "../../lib");
hjk's avatar
hjk committed
457

458
    m_group.clear();
459
    m_group.insert(theDebuggerAction(UseDebuggingHelpers),
460
        m_ui.debuggingHelperGroupBox);
461
    m_group.insert(theDebuggerAction(UseCustomDebuggingHelperLocation),
462
        m_ui.customLocationGroupBox);
hjk's avatar
hjk committed
463
    // Suppress Oxygen style's giving flat group boxes bold titles.
464
    if (oxygenStyle())
hjk's avatar
hjk committed
465
        m_ui.customLocationGroupBox->setStyleSheet(_("QGroupBox::title { font: ; }"));
466

467
    m_group.insert(theDebuggerAction(CustomDebuggingHelperLocation),
468
        m_ui.dumperLocationChooser);
hjk's avatar
hjk committed
469

470
471
472
    m_group.insert(theDebuggerAction(UseCodeModel),
        m_ui.checkBoxUseCodeModel);

473
#ifdef QT_DEBUG
474
475
    m_group.insert(theDebuggerAction(DebugDebuggingHelpers),
        m_ui.checkBoxDebugDebuggingHelpers);
476
477
478
#else
    m_ui.checkBoxDebugDebuggingHelpers->hide();
#endif
hjk's avatar
hjk committed
479
480
481
482

#ifndef QT_DEBUG
#if 0
    cmd = am->registerAction(m_manager->m_dumpLogAction,
hjk's avatar
hjk committed
483
        DUMP_LOG, globalcontext);
hjk's avatar
hjk committed
484
485
486
487
488
489
    //cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+D,Ctrl+L")));
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+F11")));
    mdebug->addAction(cmd);
#endif
#endif

490
491
492
493
494
495
496
497
498
    if (m_searchKeywords.isEmpty()) {
        QTextStream(&m_searchKeywords)
                << ' ' << m_ui.debuggingHelperGroupBox->title()
                << ' ' << m_ui.customLocationGroupBox->title()
                << ' ' << m_ui.dumperLocationLabel->text()
                << ' ' << m_ui.checkBoxUseCodeModel->text()
                << ' ' << m_ui.checkBoxDebugDebuggingHelpers->text();
        m_searchKeywords.remove(QLatin1Char('&'));
    }
hjk's avatar
hjk committed
499
500
501
    return w;
}

502
bool DebuggingHelperOptionPage::matches(const QString &s) const
hjk's avatar
hjk committed
503
{
504
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
hjk's avatar
hjk committed
505
506
}

hjk's avatar
hjk committed
507
508
509
} // namespace Internal
} // namespace Debugger

510

con's avatar
con committed
511
512
513
514
515
516
///////////////////////////////////////////////////////////////////////
//
// DebuggerPlugin
//
///////////////////////////////////////////////////////////////////////

517
518
519
520
521
522
523

DebuggerPlugin::AttachRemoteParameters::AttachRemoteParameters() :
    attachPid(0),
    winCrashEvent(0)
{
}

524
525
DebuggerPlugin::DebuggerPlugin()
  : m_manager(0),
526
527
528
    m_debugMode(0),
    m_locationMark(0),
    m_gdbRunningContext(0),
529
    m_cmdLineEnabledEngines(AllEngineTypes)
530
{}
con's avatar
con committed
531
532
533
534
535
536

DebuggerPlugin::~DebuggerPlugin()
{}

void DebuggerPlugin::shutdown()
{
hjk's avatar
hjk committed
537
    QTC_ASSERT(m_manager, /**/);
con's avatar
con committed
538
539
540
    if (m_manager)
        m_manager->shutdown();

hjk's avatar
hjk committed
541
    writeSettings();
542
543
544
545

    if (m_uiSwitcher)
        m_uiSwitcher->shutdown();

dt's avatar
dt committed
546
    delete DebuggerSettings::instance();
hjk's avatar
hjk committed
547

con's avatar
con committed
548
549
550
551
552
553
554
555
556
557
    removeObject(m_debugMode);

    // FIXME: when using the line below, BreakWindow etc gets deleted twice.
    // so better leak for now...
    delete m_debugMode;
    m_debugMode = 0;

    delete m_locationMark;
    m_locationMark = 0;

ck's avatar
ck committed
558
    removeObject(m_manager);
con's avatar
con committed
559
560
    delete m_manager;
    m_manager = 0;
561
562
563
564

    removeObject(m_uiSwitcher);
    delete m_uiSwitcher;
    m_uiSwitcher = 0;
con's avatar
con committed
565
566
}

567
static QString msgParameterMissing(const QString &a)
568
569
570
571
{
    return DebuggerPlugin::tr("Option '%1' is missing the parameter.").arg(a);
}

572
static QString msgInvalidNumericParameter(const QString &a, const QString &number)
573
574
575
576
577
{
    return DebuggerPlugin::tr("The parameter '%1' of option '%2' is not a number.").arg(number, a);
}

// Parse arguments
578
579
580
581
static bool parseArgument(QStringList::const_iterator &it,
                          const QStringList::const_iterator &cend,
                          DebuggerPlugin::AttachRemoteParameters *attachRemoteParameters,
                          unsigned *enabledEngines, QString *errorMessage)
582
583
584
{
    const QString &option = *it;
    // '-debug <pid>'
hjk's avatar
hjk committed
585
    if (*it == _("-debug")) {
586
587
588
589
590
591
        ++it;
        if (it == cend) {
            *errorMessage = msgParameterMissing(*it);
            return false;
        }
        bool ok;
592
        attachRemoteParameters->attachPid = it->toULongLong(&ok);
593
        if (!ok) {
594
595
            attachRemoteParameters->attachPid = 0;
            attachRemoteParameters->attachCore = *it;
596
597
598
        }
        return true;
    }
599
600
    // -wincrashevent <event-handle>. A handle used for
    // a handshake when attaching to a crashed Windows process.
hjk's avatar
hjk committed
601
    if (*it == _("-wincrashevent")) {
602
603
604
605
606
607
        ++it;
        if (it == cend) {
            *errorMessage = msgParameterMissing(*it);
            return false;
        }
        bool ok;
608
        attachRemoteParameters->winCrashEvent = it->toULongLong(&ok);
609
610
611
612
613
614
615
        if (!ok) {
            *errorMessage = msgInvalidNumericParameter(option, *it);
            return false;
        }
        return true;
    }
    // engine disabling
hjk's avatar
hjk committed
616
    if (option == _("-disable-cdb")) {
617
        *enabledEngines &= ~Debugger::CdbEngineType;
618
619
        return true;
    }
hjk's avatar
hjk committed
620
    if (option == _("-disable-gdb")) {
621
        *enabledEngines &= ~Debugger::GdbEngineType;
622
623
        return true;
    }
hjk's avatar
hjk committed
624
    if (option == _("-disable-sdb")) {
625
        *enabledEngines &= ~Debugger::ScriptEngineType;
626
627
628
        return true;
    }

629
    *errorMessage = DebuggerPlugin::tr("Invalid debugger option: %1").arg(option);
630
631
632
    return false;
}

633
634
635
static bool parseArguments(const QStringList &args,
                           DebuggerPlugin::AttachRemoteParameters *attachRemoteParameters,
                           unsigned *enabledEngines, QString *errorMessage)
636
637
638
{
    const QStringList::const_iterator cend = args.constEnd();
    for (QStringList::const_iterator it = args.constBegin(); it != cend; ++it)
639
        if (!parseArgument(it, cend, attachRemoteParameters, enabledEngines, errorMessage))
640
641
            return false;
    if (Debugger::Constants::Internal::debug)
642
        qDebug().nospace() << args << "engines=0x"
643
644
645
            << QString::number(*enabledEngines, 16)
            << " pid" << attachRemoteParameters->attachPid
            << " core" << attachRemoteParameters->attachCore << '\n';
646
647
648
    return true;
}

649
650
651
652
653
654
655
656
657
658
659
660
661
void DebuggerPlugin::remoteCommand(const QStringList &options, const QStringList &)
{
    QString errorMessage;
    AttachRemoteParameters parameters;
    unsigned dummy = 0;
    // Did we receive a request for debugging (unless it is ourselves)?
    if (parseArguments(options, &parameters, &dummy, &errorMessage)
        && parameters.attachPid != quint64(QCoreApplication::applicationPid())) {
        m_attachRemoteParameters = parameters;
        attachCmdLine();
    }
}

hjk's avatar
hjk committed
662
bool DebuggerPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
663
{
hjk's avatar
hjk committed
664
    // Do not fail the whole plugin if something goes wrong here.
665
    if (!parseArguments(arguments, &m_attachRemoteParameters, &m_cmdLineEnabledEngines, errorMessage)) {
666
667
        *errorMessage = tr("Error evaluating command line arguments: %1")
            .arg(*errorMessage);
668
669
670
        qWarning("%s\n", qPrintable(*errorMessage));
        errorMessage->clear();
    }
con's avatar
con committed
671

hjk's avatar
hjk committed
672
    // Debug mode setup.
673
674
675
676
677
    m_debugMode = new DebugMode(this);
    m_uiSwitcher = new DebuggerUISwitcher(m_debugMode, this);
    ExtensionSystem::PluginManager::instance()->addObject(m_uiSwitcher);
    m_uiSwitcher->addLanguage(LANG_CPP);

678
    ICore *core = ICore::instance();
hjk's avatar
hjk committed
679
    QTC_ASSERT(core, return false);
con's avatar
con committed
680

681
682
    Core::ActionManager *am = core->actionManager();
    QTC_ASSERT(am, return false);
con's avatar
con committed
683
684

    Core::UniqueIDManager *uidm = core->uniqueIDManager();
hjk's avatar
hjk committed
685
    QTC_ASSERT(uidm, return false);
con's avatar
con committed
686
687

    QList<int> globalcontext;
hjk's avatar
hjk committed
688
    globalcontext << CC::C_GLOBAL_ID;
con's avatar
con committed
689
690

    QList<int> cppcontext;
hjk's avatar
hjk committed
691
    cppcontext << uidm->uniqueIdentifier(PE::LANG_CXX);
con's avatar
con committed
692
693
694
695
696

    QList<int> debuggercontext;
    debuggercontext << uidm->uniqueIdentifier(C_GDBDEBUGGER);

    QList<int> cppeditorcontext;
697
    cppeditorcontext << uidm->uniqueIdentifier(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
698
699
700
701
702
703

    QList<int> texteditorcontext;
    texteditorcontext << uidm->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);

    m_gdbRunningContext = uidm->uniqueIdentifier(Constants::GDBRUNNING);

704
705
706
707
708
    DebuggerManager *manager = new DebuggerManager(this);
    ExtensionSystem::PluginManager::instance()->addObject(manager);
    const QList<Core::IOptionsPage *> engineOptionPages =
        manager->initializeEngines(m_cmdLineEnabledEngines);

709
    // Register factory of DebuggerRunControl.
710
    m_debuggerRunControlFactory = new DebuggerRunControlFactory(manager);
711
712
713
    addAutoReleasedObject(m_debuggerRunControlFactory);

    QList<int> context;
hjk's avatar
hjk committed
714
715
716
    context.append(uidm->uniqueIdentifier(CC::C_EDITORMANAGER));
    context.append(uidm->uniqueIdentifier(C_GDBDEBUGGER));
    context.append(uidm->uniqueIdentifier(CC::C_NAVIGATION_PANE));
717
718
    m_debugMode->setContext(context);

719
    m_reverseToolButton = 0;
con's avatar
con committed
720

721
    // Handling of external applications.
722
723
724
725
726
727
728
729
730
731
732
733
734
735
    m_startExternalAction = new QAction(this);
    m_startExternalAction->setText(tr("Start and Debug External Application..."));
    connect(m_startExternalAction, SIGNAL(triggered()),
        this, SLOT(startExternalApplication()));

    m_attachExternalAction = new QAction(this);
    m_attachExternalAction->setText(tr("Attach to Running External Application..."));
    connect(m_attachExternalAction, SIGNAL(triggered()),
        this, SLOT(attachExternalApplication()));

    m_attachCoreAction = new QAction(this);
    m_attachCoreAction->setText(tr("Attach to Core..."));
    connect(m_attachCoreAction, SIGNAL(triggered()), this, SLOT(attachCore()));

736
737
738
739
    m_startRemoteAction = new QAction(this);
    m_startRemoteAction->setText(tr("Start and Attach to Remote Application..."));
    connect(m_startRemoteAction, SIGNAL(triggered()),
        this, SLOT(startRemoteApplication()));
740

741
    m_detachAction = new QAction(this);
742
    m_detachAction->setText(tr("Detach Debugger"));
743
    connect(m_detachAction, SIGNAL(triggered()),
744
        manager, SLOT(detachDebugger()));
745

746
    Core::Command *cmd = 0;
747
    const DebuggerManagerActions actions = manager->debuggerManagerActions();
748

749
    Core::ActionContainer *mstart =
hjk's avatar
hjk committed
750
        am->actionContainer(PE::M_DEBUG_STARTDEBUGGING);
751

752
    cmd = am->registerAction(actions.continueAction,
hjk's avatar
hjk committed
753
754
        PE::DEBUG, QList<int>() << m_gdbRunningContext);
    mstart->addAction(cmd, CC::G_DEFAULT_ONE);
755

756
    cmd = am->registerAction(m_startExternalAction,
con's avatar
con committed
757
        Constants::STARTEXTERNAL, globalcontext);
hjk's avatar
hjk committed
758
    mstart->addAction(cmd, CC::G_DEFAULT_ONE);
con's avatar
con committed
759

760
    cmd = am->registerAction(m_attachExternalAction,
con's avatar
con committed
761
        Constants::ATTACHEXTERNAL, globalcontext);
hjk's avatar
hjk committed
762
    mstart->addAction(cmd, CC::G_DEFAULT_ONE);
763

764
765
    cmd = am->registerAction(m_attachCoreAction,
        Constants::ATTACHCORE, globalcontext);
hjk's avatar
hjk committed
766
    mstart->addAction(cmd, CC::G_DEFAULT_ONE);
767

768
    cmd = am->registerAction(m_startRemoteAction,
769
        Constants::ATTACHREMOTE, globalcontext);
hjk's avatar
hjk committed
770
    mstart->addAction(cmd, CC::G_DEFAULT_ONE);
771

772
773
    cmd = am->registerAction(m_detachAction,
        Constants::DETACH, globalcontext);
hjk's avatar
hjk committed
774
    m_uiSwitcher->addMenuAction(cmd, CC::G_DEFAULT_ONE);
con's avatar
con committed
775

776
    cmd = am->registerAction(actions.stopAction,
con's avatar
con committed
777
        Constants::INTERRUPT, globalcontext);
hjk's avatar
hjk committed
778
779
    cmd->setAttribute(Command::CA_UpdateText);
    cmd->setAttribute(Command::CA_UpdateIcon);
con's avatar
con committed
780
781
    cmd->setDefaultKeySequence(QKeySequence(Constants::INTERRUPT_KEY));
    cmd->setDefaultText(tr("Stop Debugger/Interrupt Debugger"));
hjk's avatar
hjk committed
782
    m_uiSwitcher->addMenuAction(cmd, CC::G_DEFAULT_ONE);
con's avatar
con committed
783

784
    cmd = am->registerAction(actions.resetAction,
con's avatar
con committed
785
        Constants::RESET, globalcontext);
con's avatar
con committed
786
    cmd->setAttribute(Core::Command::CA_UpdateText);
787
    //cmd->setDefaultKeySequence(QKeySequence(Constants::RESET_KEY));
con's avatar
con committed
788
    cmd->setDefaultText(tr("Reset Debugger"));
hjk's avatar
hjk committed
789
    m_uiSwitcher->addMenuAction(cmd, CC::G_DEFAULT_ONE);
con's avatar
con committed
790
791
792

    QAction *sep = new QAction(this);
    sep->setSeparator(true);
hjk's avatar
hjk committed
793
    cmd = am->registerAction(sep, _("Debugger.Sep.Step"), globalcontext);
794
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
795

796
    cmd = am->registerAction(actions.nextAction,
con's avatar
con committed
797
798
        Constants::NEXT, debuggercontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::NEXT_KEY));
799
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
800

801
    cmd = am->registerAction(actions.stepAction,
con's avatar
con committed
802
803
        Constants::STEP, debuggercontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::STEP_KEY));
804
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
805

806
    cmd = am->registerAction(actions.stepOutAction,
con's avatar
con committed
807
808
        Constants::STEPOUT, debuggercontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::STEPOUT_KEY));
809
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
810

811
812
    cmd = am->registerAction(actions.runToLineAction1,
        Constants::RUN_TO_LINE1, debuggercontext);
con's avatar
con committed
813
    cmd->setDefaultKeySequence(QKeySequence(Constants::RUN_TO_LINE_KEY));
814
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
815

816
    cmd = am->registerAction(actions.runToFunctionAction,
con's avatar
con committed
817
818
        Constants::RUN_TO_FUNCTION, debuggercontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::RUN_TO_FUNCTION_KEY));
819
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
820

821
822
    cmd = am->registerAction(actions.jumpToLineAction1,
        Constants::JUMP_TO_LINE1, debuggercontext);
823
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
824

825
826
    cmd = am->registerAction(actions.returnFromFunctionAction,
        Constants::RETURN_FROM_FUNCTION, debuggercontext);
827
    m_uiSwitcher->addMenuAction(cmd);
828

829
    cmd = am->registerAction(actions.reverseDirectionAction,
830
831
        Constants::REVERSE, debuggercontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::REVERSE_KEY));
832
    m_uiSwitcher->addMenuAction(cmd);
833

con's avatar
con committed
834
835
    sep = new QAction(this);
    sep->setSeparator(true);
hjk's avatar
hjk committed
836
    cmd = am->registerAction(sep, _("Debugger.Sep.Break"), globalcontext);
837
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
838

839
840
841
    cmd = am->registerAction(actions.snapshotAction,
        Constants::SNAPSHOT, debuggercontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::SNAPSHOT_KEY));
842
    m_uiSwitcher->addMenuAction(cmd);
843

844
845
    cmd = am->registerAction(theDebuggerAction(OperateByInstruction),
        Constants::OPERATE_BY_INSTRUCTION, debuggercontext);
846
    m_uiSwitcher->addMenuAction(cmd);
847

848
    cmd = am->registerAction(actions.breakAction,
con's avatar
con committed
849
850
        Constants::TOGGLE_BREAK, cppeditorcontext);
    cmd->setDefaultKeySequence(QKeySequence(Constants::TOGGLE_BREAK_KEY));
851
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
852
853
854
855
    //mcppcontext->addAction(cmd);

    sep = new QAction(this);
    sep->setSeparator(true);
hjk's avatar
hjk committed
856
    cmd = am->registerAction(sep, _("Debugger.Sep.Watch"), globalcontext);
857
    m_uiSwitcher->addMenuAction(cmd);
con's avatar
con committed
858

859
860
    cmd = am->registerAction(actions.watchAction1,
        Constants::ADD_TO_WATCH1, cppeditorcontext);
861
    cmd->action()->setEnabled(true);
862
    //cmd->setDefaultKeySequence(QKeySequence(tr("ALT+D,ALT+W")));
863
    m_uiSwitcher->addMenuAction(cmd);
864