debuggermanager.cpp 46.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
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
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30
31

#include "debuggermanager.h"

32
#include "debuggeractions.h"
con's avatar
con committed
33
34
35
36
37
38
39
40
41
#include "debuggerconstants.h"
#include "idebuggerengine.h"

#include "breakwindow.h"
#include "disassemblerwindow.h"
#include "debuggeroutputwindow.h"
#include "moduleswindow.h"
#include "registerwindow.h"
#include "stackwindow.h"
42
#include "sourcefileswindow.h"
con's avatar
con committed
43
44
45
46
47
48
49
50
51
52
53
54
#include "threadswindow.h"
#include "watchwindow.h"

#include "ui_breakbyfunction.h"

#include "disassemblerhandler.h"
#include "breakhandler.h"
#include "moduleshandler.h"
#include "registerhandler.h"
#include "stackhandler.h"
#include "watchhandler.h"

55
#include "debuggerdialogs.h"
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
56
57
58
#ifdef Q_OS_WIN
#  include "peutils.h"
#endif
59
60
#include <utils/qtcassert.h>

con's avatar
con committed
61
62
63
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
64
#include <QtCore/QTextStream>
con's avatar
con committed
65
#include <QtCore/QTime>
66
#include <QtCore/QTimer>
con's avatar
con committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

#include <QtGui/QAction>
#include <QtGui/QComboBox>
#include <QtGui/QDockWidget>
#include <QtGui/QErrorMessage>
#include <QtGui/QFileDialog>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMessageBox>
#include <QtGui/QPlainTextEdit>
#include <QtGui/QStatusBar>
#include <QtGui/QTextBlock>
#include <QtGui/QTextCursor>
#include <QtGui/QToolBar>
#include <QtGui/QToolButton>
#include <QtGui/QToolTip>

using namespace Debugger;
using namespace Debugger::Internal;
using namespace Debugger::Constants;

static const QString tooltipIName = "tooltip";

90
91
92
93
94
95
96
97

DebuggerSettings::DebuggerSettings()
{
    m_autoRun = false;
    m_autoQuit = false;
    m_skipKnownFrames = false;
    m_debugDumpers = false;
    m_useToolTips = false;
98
    m_useDumpers = true;
99
    m_listSourceFiles = false;
100
101
}

102
103
104
105
106
107
108
109
110
111

QString DebuggerSettings::dump()
{
    QString out;
    QTextStream ts(&out);
    ts  << "Debugger settings: "
        << "  gdbCmd: " << m_gdbCmd
        << "  gdbEnv: " << m_gdbEnv 
        << "  autoRun: " << m_autoRun
        << "  autoQuit: " << m_autoQuit
112
        << "  useCustomDumpers: " << m_useDumpers
113
114
115
116
117
118
119
120
121
122
123
124
        << "  skipKnownFrames: " << m_skipKnownFrames
        << "  debugDumpers: " << m_debugDumpers
        << "  useToolTips: " << m_useToolTips
        << "  listSourceFiles: " << m_listSourceFiles
        << "  scriptFile: " << m_scriptFile
        << "  pluginAllBreakpoints: " << m_pluginAllBreakpoints
        << "  pluginSelectedBreakpoints: " << m_pluginSelectedBreakpoints
        << "  pluginNoBreakpoints: " << m_pluginNoBreakpoints
        << "  pluginSelectedBreakpointsPattern: " << m_pluginSelectedBreakpointsPattern;
    return out;
}

con's avatar
con committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
///////////////////////////////////////////////////////////////////////
//
// BreakByFunctionDialog
//
///////////////////////////////////////////////////////////////////////

class BreakByFunctionDialog : public QDialog, Ui::BreakByFunctionDialog
{
    Q_OBJECT

public:
    explicit BreakByFunctionDialog(QWidget *parent)
      : QDialog(parent)
    {
        setupUi(this);
        connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
        connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    }
    QString functionName() const { return functionLineEdit->text(); }
};


///////////////////////////////////////////////////////////////////////
//
// DebuggerManager
//
///////////////////////////////////////////////////////////////////////

static IDebuggerEngine *gdbEngine = 0;
static IDebuggerEngine *winEngine = 0;
static IDebuggerEngine *scriptEngine = 0;

extern IDebuggerEngine *createGdbEngine(DebuggerManager *parent);
158
159
160
161
162
163
extern IDebuggerEngine *createWinEngine(DebuggerManager *)
#ifdef CDB_ENABLED
;
#else
{ return 0; }
#endif
con's avatar
con committed
164
165
extern IDebuggerEngine *createScriptEngine(DebuggerManager *parent);

Friedemann Kleint's avatar
Friedemann Kleint committed
166
167
DebuggerManager::DebuggerManager() :
    m_attachCoreAction(0)
con's avatar
con committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
    init();
}

DebuggerManager::~DebuggerManager()
{
    delete gdbEngine;
    delete winEngine;
    delete scriptEngine;
}

void DebuggerManager::init()
{
    m_status = -1;
    m_busy = false;

    m_attachedPID = 0;
185
    m_startMode = StartInternal;
con's avatar
con committed
186
187
188
189
190

    m_disassemblerHandler = 0;
    m_modulesHandler = 0;
    m_registerHandler = 0;

191
    m_statusLabel = new QLabel;
con's avatar
con committed
192
193
194
195
196
197
    m_breakWindow = new BreakWindow;
    m_disassemblerWindow = new DisassemblerWindow;
    m_modulesWindow = new ModulesWindow;
    m_outputWindow = new DebuggerOutputWindow;
    m_registerWindow = new RegisterWindow;
    m_stackWindow = new StackWindow;
198
    m_sourceFilesWindow = new SourceFilesWindow;
con's avatar
con committed
199
200
201
202
203
204
    m_threadsWindow = new ThreadsWindow;
    m_localsWindow = new WatchWindow(WatchWindow::LocalsType);
    m_watchersWindow = new WatchWindow(WatchWindow::WatchersType);
    //m_tooltipWindow = new WatchWindow(WatchWindow::TooltipType);
    //m_watchersWindow = new QTreeView;
    m_tooltipWindow = new QTreeView;
205
    m_statusTimer = new QTimer(this);
con's avatar
con committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

    m_mainWindow = new QMainWindow;
    m_mainWindow->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);
    m_mainWindow->setDocumentMode(true);

    // Stack
    m_stackHandler = new StackHandler;
    QAbstractItemView *stackView =
        qobject_cast<QAbstractItemView *>(m_stackWindow);
    stackView->setModel(m_stackHandler->stackModel());
    connect(stackView, SIGNAL(frameActivated(int)),
        this, SLOT(activateFrame(int)));

    // Threads
    m_threadsHandler = new ThreadsHandler;
    QAbstractItemView *threadsView =
        qobject_cast<QAbstractItemView *>(m_threadsWindow);
    threadsView->setModel(m_threadsHandler->threadsModel());
    connect(threadsView, SIGNAL(threadSelected(int)),
        this, SLOT(selectThread(int)));

    // Disassembler
    m_disassemblerHandler = new DisassemblerHandler;
    QAbstractItemView *disassemblerView =
        qobject_cast<QAbstractItemView *>(m_disassemblerWindow);
    disassemblerView->setModel(m_disassemblerHandler->model());

    // Breakpoints
    m_breakHandler = new BreakHandler;
    QAbstractItemView *breakView =
        qobject_cast<QAbstractItemView *>(m_breakWindow);
    breakView->setModel(m_breakHandler->model());
238
239
240
    connect(breakView, SIGNAL(breakpointActivated(int)),
        m_breakHandler, SLOT(activateBreakpoint(int)));
    connect(breakView, SIGNAL(breakpointDeleted(int)),
con's avatar
con committed
241
        m_breakHandler, SLOT(removeBreakpoint(int)));
242
243
    connect(breakView, SIGNAL(breakpointSynchronizationRequested()),
        this, SLOT(attemptBreakpointSynchronization()));
con's avatar
con committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    connect(m_breakHandler, SIGNAL(gotoLocation(QString,int,bool)),
        this, SLOT(gotoLocation(QString,int,bool)));
    connect(m_breakHandler, SIGNAL(sessionValueRequested(QString,QVariant*)),
        this, SIGNAL(sessionValueRequested(QString,QVariant*)));
    connect(m_breakHandler, SIGNAL(setSessionValueRequested(QString,QVariant)),
        this, SIGNAL(setSessionValueRequested(QString,QVariant)));

    // Modules
    QAbstractItemView *modulesView =
        qobject_cast<QAbstractItemView *>(m_modulesWindow);
    m_modulesHandler = new ModulesHandler;
    modulesView->setModel(m_modulesHandler->model());
    connect(modulesView, SIGNAL(reloadModulesRequested()),
        this, SLOT(reloadModules()));
    connect(modulesView, SIGNAL(loadSymbolsRequested(QString)),
        this, SLOT(loadSymbols(QString)));
    connect(modulesView, SIGNAL(loadAllSymbolsRequested()),
        this, SLOT(loadAllSymbols()));
262
263
    connect(modulesView, SIGNAL(fileOpenRequested(QString)),
        this, SLOT(fileOpen(QString)));
hjk's avatar
hjk committed
264
265
    connect(modulesView, SIGNAL(newDockRequested(QWidget*)),
        this, SLOT(createNewDock(QWidget*)));
con's avatar
con committed
266

267
268
269
270
271
272
273
    // Source Files
    //m_sourceFilesHandler = new SourceFilesHandler;
    QAbstractItemView *sourceFilesView =
        qobject_cast<QAbstractItemView *>(m_sourceFilesWindow);
    //sourceFileView->setModel(m_stackHandler->stackModel());
    connect(sourceFilesView, SIGNAL(reloadSourceFilesRequested()),
        this, SLOT(reloadSourceFiles()));
274
275
    connect(sourceFilesView, SIGNAL(fileOpenRequested(QString)),
        this, SLOT(fileOpen(QString)));
con's avatar
con committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

    // Registers 
    QAbstractItemView *registerView =
        qobject_cast<QAbstractItemView *>(m_registerWindow);
    m_registerHandler = new RegisterHandler;
    registerView->setModel(m_registerHandler->model());

    m_watchHandler = new WatchHandler;

    // Locals
    QTreeView *localsView = qobject_cast<QTreeView *>(m_localsWindow);
    localsView->setModel(m_watchHandler->model());
    connect(localsView, SIGNAL(requestExpandChildren(QModelIndex)),
        this, SLOT(expandChildren(QModelIndex)));
    connect(localsView, SIGNAL(requestCollapseChildren(QModelIndex)),
        this, SLOT(collapseChildren(QModelIndex)));
    connect(localsView, SIGNAL(requestAssignValue(QString,QString)),
        this, SLOT(assignValueInDebugger(QString,QString)));
    connect(localsView, SIGNAL(requestWatchExpression(QString)),
        this, SLOT(watchExpression(QString)));

    // Watchers 
    QTreeView *watchersView = qobject_cast<QTreeView *>(m_watchersWindow);
    watchersView->setModel(m_watchHandler->model());
    connect(watchersView, SIGNAL(requestAssignValue(QString,QString)),
        this, SLOT(assignValueInDebugger(QString,QString)));
    connect(watchersView, SIGNAL(requestExpandChildren(QModelIndex)),
        this, SLOT(expandChildren(QModelIndex)));
    connect(watchersView, SIGNAL(requestCollapseChildren(QModelIndex)),
        this, SLOT(collapseChildren(QModelIndex)));
    connect(watchersView, SIGNAL(requestWatchExpression(QString)),
        this, SLOT(watchExpression(QString)));
    connect(watchersView, SIGNAL(requestRemoveWatchExpression(QString)),
        this, SLOT(removeWatchExpression(QString)));
hjk's avatar
hjk committed
310
311
312
313
    connect(m_watchHandler, SIGNAL(sessionValueRequested(QString,QVariant*)),
        this, SIGNAL(sessionValueRequested(QString,QVariant*)));
    connect(m_watchHandler, SIGNAL(setSessionValueRequested(QString,QVariant)),
        this, SIGNAL(setSessionValueRequested(QString,QVariant)));
con's avatar
con committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327

    // Tooltip
    QTreeView *tooltipView = qobject_cast<QTreeView *>(m_tooltipWindow);
    tooltipView->setModel(m_watchHandler->model());

    connect(m_watchHandler, SIGNAL(watchModelUpdateRequested()),
        this, SLOT(updateWatchModel()));

    m_startExternalAction = new QAction(this);
    m_startExternalAction->setText(tr("Start and Debug External Application..."));

    m_attachExternalAction = new QAction(this);
    m_attachExternalAction->setText(tr("Attach to Running External Application..."));

Friedemann Kleint's avatar
Friedemann Kleint committed
328
#ifndef Q_OS_WIN
329
330
    m_attachCoreAction = new QAction(this);
    m_attachCoreAction->setText(tr("Attach to Core..."));
Friedemann Kleint's avatar
Friedemann Kleint committed
331
332
    connect(m_attachCoreAction, SIGNAL(triggered()), this, SLOT(attachCore()));
#endif
333

con's avatar
con committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    m_continueAction = new QAction(this);
    m_continueAction->setText(tr("Continue"));
    m_continueAction->setIcon(QIcon(":/gdbdebugger/images/debugger_continue_small.png"));

    m_stopAction = new QAction(this);
    m_stopAction->setText(tr("Interrupt"));
    m_stopAction->setIcon(QIcon(":/gdbdebugger/images/debugger_interrupt_small.png"));

    m_resetAction = new QAction(this);
    m_resetAction->setText(tr("Reset Debugger"));

    m_nextAction = new QAction(this);
    m_nextAction->setText(tr("Step Over"));
    //m_nextAction->setShortcut(QKeySequence(tr("F6")));
    m_nextAction->setIcon(QIcon(":/gdbdebugger/images/debugger_stepover_small.png"));

    m_stepAction = new QAction(this);
    m_stepAction->setText(tr("Step Into"));
    //m_stepAction->setShortcut(QKeySequence(tr("F7")));
    m_stepAction->setIcon(QIcon(":/gdbdebugger/images/debugger_stepinto_small.png"));

    m_nextIAction = new QAction(this);
    m_nextIAction->setText(tr("Step Over Instruction"));
    //m_nextIAction->setShortcut(QKeySequence(tr("Shift+F6")));
    m_nextIAction->setIcon(QIcon(":/gdbdebugger/images/debugger_stepoverproc_small.png"));

    m_stepIAction = new QAction(this);
    m_stepIAction->setText(tr("Step One Instruction"));
    //m_stepIAction->setShortcut(QKeySequence(tr("Shift+F9")));
    m_stepIAction->setIcon(QIcon(":/gdbdebugger/images/debugger_steponeproc_small.png"));

    m_stepOutAction = new QAction(this);
    m_stepOutAction->setText(tr("Step Out"));
    //m_stepOutAction->setShortcut(QKeySequence(tr("Shift+F7")));
    m_stepOutAction->setIcon(QIcon(":/gdbdebugger/images/debugger_stepout_small.png"));

    m_runToLineAction = new QAction(this);
    m_runToLineAction->setText(tr("Run to Line"));

    m_runToFunctionAction = new QAction(this);
    m_runToFunctionAction->setText(tr("Run to Outermost Function"));

    m_jumpToLineAction = new QAction(this);
    m_jumpToLineAction->setText(tr("Jump to Line"));

    m_breakAction = new QAction(this);
    m_breakAction->setText(tr("Toggle Breakpoint"));

    m_breakByFunctionAction = new QAction(this);
    m_breakByFunctionAction->setText(tr("Set Breakpoint at Function..."));

    m_breakAtMainAction = new QAction(this);
386
    m_breakAtMainAction->setText(tr("Set Breakpoint at Function \"main\""));
con's avatar
con committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

    m_watchAction = new QAction(this);
    m_watchAction->setText(tr("Add to Watch Window"));

    // For usuage hints oin focus{In,Out}
    //connect(m_outputWindow, SIGNAL(statusMessageRequested(QString,int)),
    //    this, SLOT(showStatusMessage(QString,int)));

    connect(m_continueAction, SIGNAL(triggered()),
        this, SLOT(continueExec()));

    connect(m_startExternalAction, SIGNAL(triggered()),
        this, SLOT(startExternalApplication()));
    connect(m_attachExternalAction, SIGNAL(triggered()),
        this, SLOT(attachExternalApplication()));

    connect(m_stopAction, SIGNAL(triggered()),
        this, SLOT(interruptDebuggingRequest()));
    connect(m_resetAction, SIGNAL(triggered()),
        this, SLOT(exitDebugger()));
    connect(m_nextAction, SIGNAL(triggered()),
        this, SLOT(nextExec()));
    connect(m_stepAction, SIGNAL(triggered()),
        this, SLOT(stepExec()));
    connect(m_nextIAction, SIGNAL(triggered()),
        this, SLOT(nextIExec()));
    connect(m_stepIAction, SIGNAL(triggered()),
        this, SLOT(stepIExec()));
    connect(m_stepOutAction, SIGNAL(triggered()),
        this, SLOT(stepOutExec()));
    connect(m_runToLineAction, SIGNAL(triggered()),
        this, SLOT(runToLineExec()));
    connect(m_runToFunctionAction, SIGNAL(triggered()),
        this, SLOT(runToFunctionExec()));
    connect(m_jumpToLineAction, SIGNAL(triggered()),
        this, SLOT(jumpToLineExec()));
    connect(m_watchAction, SIGNAL(triggered()),
        this, SLOT(addToWatchWindow()));
    connect(m_breakAction, SIGNAL(triggered()),
        this, SLOT(toggleBreakpoint()));
    connect(m_breakByFunctionAction, SIGNAL(triggered()),
        this, SLOT(breakByFunction()));
    connect(m_breakAtMainAction, SIGNAL(triggered()),
        this, SLOT(breakAtMain()));

432
433
    connect(m_statusTimer, SIGNAL(timeout()),
        this, SLOT(clearStatusMessage()));
con's avatar
con committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

    connect(m_outputWindow, SIGNAL(commandExecutionRequested(QString)),
        this, SLOT(executeDebuggerCommand(QString)));


    m_breakDock = createDockForWidget(m_breakWindow);

    m_disassemblerDock = createDockForWidget(m_disassemblerWindow);
    connect(m_disassemblerDock->toggleViewAction(), SIGNAL(toggled(bool)),
        this, SLOT(reloadDisassembler()), Qt::QueuedConnection);

    m_modulesDock = createDockForWidget(m_modulesWindow);
    connect(m_modulesDock->toggleViewAction(), SIGNAL(toggled(bool)),
        this, SLOT(reloadModules()), Qt::QueuedConnection);

    m_registerDock = createDockForWidget(m_registerWindow);
    connect(m_registerDock->toggleViewAction(), SIGNAL(toggled(bool)),
        this, SLOT(reloadRegisters()), Qt::QueuedConnection);

    m_outputDock = createDockForWidget(m_outputWindow);

    m_stackDock = createDockForWidget(m_stackWindow);

457
458
459
460
    m_sourceFilesDock = createDockForWidget(m_sourceFilesWindow);
    connect(m_sourceFilesDock->toggleViewAction(), SIGNAL(toggled(bool)),
        this, SLOT(reloadSourceFiles()), Qt::QueuedConnection);

con's avatar
con committed
461
462
463
464
465
466
467
    m_threadsDock = createDockForWidget(m_threadsWindow);

    setStatus(DebuggerProcessNotReady);
    gdbEngine = createGdbEngine(this);
    winEngine = createWinEngine(this);
    scriptEngine = createScriptEngine(this);
    setDebuggerType(GdbDebugger);
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
468
469
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << gdbEngine << winEngine << scriptEngine;
470
471
472

    connect(action(UseDumpers), SIGNAL(triggered(bool)),
        this, SLOT(setUseDumpers(bool)));
473
474
    connect(action(DebugDumpers), SIGNAL(triggered(bool)),
        this, SLOT(setDebugDumpers(bool)));
con's avatar
con committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
}

void DebuggerManager::setDebuggerType(DebuggerType type)
{
    switch (type) {
        case GdbDebugger:
            m_engine = gdbEngine;
            break;
        case ScriptDebugger:
            m_engine = scriptEngine;
            break;
        case WinDebugger:
            m_engine = winEngine;
            break;
    }
}

IDebuggerEngine *DebuggerManager::engine()
{
    return m_engine;
}

IDebuggerManagerAccessForEngines *DebuggerManager::engineInterface()
{
    return dynamic_cast<IDebuggerManagerAccessForEngines *>(this);
}

void DebuggerManager::createDockWidgets()
{
    QSplitter *localsAndWatchers = new QSplitter(Qt::Vertical, 0);
    localsAndWatchers->setWindowTitle(m_localsWindow->windowTitle());
    localsAndWatchers->addWidget(m_localsWindow);
    localsAndWatchers->addWidget(m_watchersWindow);
    localsAndWatchers->setStretchFactor(0, 3);
    localsAndWatchers->setStretchFactor(1, 1);
    m_watchDock = createDockForWidget(localsAndWatchers);
}

hjk's avatar
hjk committed
513
514
515
516
517
518
519
520
521
522
void DebuggerManager::createNewDock(QWidget *widget)
{
    QDockWidget *dockWidget = new QDockWidget(widget->windowTitle(), m_mainWindow);
    dockWidget->setObjectName(widget->windowTitle());
    dockWidget->setFeatures(QDockWidget::DockWidgetClosable);
    dockWidget->setWidget(widget);
    m_mainWindow->addDockWidget(Qt::TopDockWidgetArea, dockWidget);
    dockWidget->show();
}

con's avatar
con committed
523
524
525
526
QDockWidget *DebuggerManager::createDockForWidget(QWidget *widget)
{
    QDockWidget *dockWidget = new QDockWidget(widget->windowTitle(), m_mainWindow);
    dockWidget->setObjectName(widget->windowTitle());
527
    dockWidget->setFeatures(QDockWidget::DockWidgetClosable);
con's avatar
con committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
    dockWidget->setTitleBarWidget(new QWidget(dockWidget));
    dockWidget->setWidget(widget);
    connect(dockWidget->toggleViewAction(), SIGNAL(toggled(bool)),
        this, SLOT(dockToggled(bool)), Qt::QueuedConnection);
    m_dockWidgets.append(dockWidget);
    return dockWidget;
}

void DebuggerManager::setSimpleDockWidgetArrangement()
{
    foreach (QDockWidget *dockWidget, m_dockWidgets)
        m_mainWindow->removeDockWidget(dockWidget);

    foreach (QDockWidget *dockWidget, m_dockWidgets) {
        m_mainWindow->addDockWidget(Qt::BottomDockWidgetArea, dockWidget);
        dockWidget->show();
    }

    m_mainWindow->tabifyDockWidget(m_watchDock, m_breakDock);
    m_mainWindow->tabifyDockWidget(m_watchDock, m_disassemblerDock);
    m_mainWindow->tabifyDockWidget(m_watchDock, m_modulesDock);
    m_mainWindow->tabifyDockWidget(m_watchDock, m_outputDock);
    m_mainWindow->tabifyDockWidget(m_watchDock, m_registerDock);
    m_mainWindow->tabifyDockWidget(m_watchDock, m_threadsDock);
552
    m_mainWindow->tabifyDockWidget(m_watchDock, m_sourceFilesDock);
con's avatar
con committed
553
554
555

    // They are rarely used even in ordinary debugging. Hiding them also saves
    // cycles since the corresponding information won't be retrieved.
556
    m_sourceFilesDock->hide();
con's avatar
con committed
557
558
559
560
561
562
563
564
565
    m_registerDock->hide();
    m_disassemblerDock->hide();
    m_modulesDock->hide();
    m_outputDock->hide();
}

void DebuggerManager::setLocked(bool locked)
{
    const QDockWidget::DockWidgetFeatures features =
566
            (locked) ? QDockWidget::DockWidgetClosable :
con's avatar
con committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
                       QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable;

    foreach (QDockWidget *dockWidget, m_dockWidgets) {
        QWidget *titleBarWidget = dockWidget->titleBarWidget();
        if (locked && !titleBarWidget)
            titleBarWidget = new QWidget(dockWidget);
        else if (!locked && titleBarWidget) {
            delete titleBarWidget;
            titleBarWidget = 0;
        }
        dockWidget->setTitleBarWidget(titleBarWidget);
        dockWidget->setFeatures(features);
    }
}

void DebuggerManager::dockToggled(bool on)
{
    QDockWidget *dw = qobject_cast<QDockWidget *>(sender()->parent());
    if (on && dw)
        dw->raise();
}

QAbstractItemModel *DebuggerManager::threadsModel()
{
    return qobject_cast<ThreadsWindow*>(m_threadsWindow)->model();
}

594
595
596
597
598
void DebuggerManager::clearStatusMessage()
{
    m_statusLabel->setText(m_lastPermanentStatusMessage);
}

con's avatar
con committed
599
600
601
void DebuggerManager::showStatusMessage(const QString &msg, int timeout)
{
    Q_UNUSED(timeout)
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
602
603
    if (Debugger::Constants::Internal::debug)
        qDebug() << "STATUS MSG: " << msg;
con's avatar
con committed
604
    showDebuggerOutput("status:", msg);
605
606
607
608
609
610
611
612
    m_statusLabel->setText("   " + msg);
    if (timeout > 0) {
        m_statusTimer->setSingleShot(true);
        m_statusTimer->start(timeout);
    } else {
        m_lastPermanentStatusMessage = msg;
        m_statusTimer->stop();
    }
con's avatar
con committed
613
614
}

615
616
617
618
619
620
void DebuggerManager::notifyInferiorStopRequested()
{
    setStatus(DebuggerInferiorStopRequested);
    showStatusMessage(tr("Stop requested..."), 5000);
}

con's avatar
con committed
621
622
623
624
625
626
627
628
629
630
void DebuggerManager::notifyInferiorStopped()
{
    resetLocation();
    setStatus(DebuggerInferiorStopped);
    showStatusMessage(tr("Stopped."), 5000);
}

void DebuggerManager::notifyInferiorRunningRequested()
{
    setStatus(DebuggerInferiorRunningRequested);
631
    showStatusMessage(tr("Running requested..."), 5000);
con's avatar
con committed
632
633
634
635
636
637
638
639
640
641
}

void DebuggerManager::notifyInferiorRunning()
{
    setStatus(DebuggerInferiorRunning);
    showStatusMessage(tr("Running..."), 5000);
}

void DebuggerManager::notifyInferiorExited()
{
hjk's avatar
hjk committed
642
    setStatus(DebuggerProcessNotReady);
con's avatar
con committed
643
644
645
646
647
    showStatusMessage(tr("Stopped."), 5000);
}

void DebuggerManager::notifyInferiorPidChanged(int pid)
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
648
649
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << pid;
con's avatar
con committed
650
651
652
653
    //QMessageBox::warning(0, "PID", "PID: " + QString::number(pid)); 
    emit inferiorPidChanged(pid);
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
654
void DebuggerManager::showApplicationOutput(const QString &str)
con's avatar
con committed
655
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
656
     emit applicationOutputAvailable(str);
con's avatar
con committed
657
658
659
660
}

void DebuggerManager::shutdown()
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
661
662
663
664
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << m_engine;

    if (m_engine)
665
666
667
668
669
670
671
672
673
674
        m_engine->shutdown();
    m_engine = 0;

    delete scriptEngine;
    scriptEngine = 0;
    delete gdbEngine;
    gdbEngine = 0;
    delete winEngine;
    winEngine = 0;

con's avatar
con committed
675
676
677
678
679
680
681
682
    // Delete these manually before deleting the manager
    // (who will delete the models for most views)
    delete m_breakWindow;
    delete m_disassemblerWindow;
    delete m_modulesWindow;
    delete m_outputWindow;
    delete m_registerWindow;
    delete m_stackWindow;
683
    delete m_sourceFilesWindow;
con's avatar
con committed
684
685
686
687
688
689
690
691
692
693
694
    delete m_threadsWindow;
    delete m_tooltipWindow;
    delete m_watchersWindow;
    delete m_localsWindow;
    // These widgets are all in some layout which will take care of deletion.
    m_breakWindow = 0;
    m_disassemblerWindow = 0;
    m_modulesWindow = 0;
    m_outputWindow = 0;
    m_registerWindow = 0;
    m_stackWindow = 0;
695
    m_sourceFilesWindow = 0;
con's avatar
con committed
696
697
698
699
700
701
702
    m_threadsWindow = 0;
    m_tooltipWindow = 0;
    m_watchersWindow = 0;
    m_localsWindow = 0;

    delete m_breakHandler;
    delete m_disassemblerHandler;
dt's avatar
dt committed
703
    delete m_threadsHandler;
con's avatar
con committed
704
705
706
707
708
709
    delete m_modulesHandler;
    delete m_registerHandler;
    delete m_stackHandler;
    delete m_watchHandler;
    m_breakHandler = 0;
    m_disassemblerHandler = 0;
dt's avatar
dt committed
710
    m_threadsHandler = 0;
con's avatar
con committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
    m_modulesHandler = 0;
    m_registerHandler = 0;
    m_stackHandler = 0;
    m_watchHandler = 0;
    //qDebug() << "DEBUGGER_MANAGER SHUTDOWN END";
}

void DebuggerManager::toggleBreakpoint()
{
    QString fileName;
    int lineNumber = -1;
    queryCurrentTextEditor(&fileName, &lineNumber, 0);
    if (lineNumber == -1)
        return;
    toggleBreakpoint(fileName, lineNumber);
}

void DebuggerManager::toggleBreakpoint(const QString &fileName, int lineNumber)
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
730
731
732
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << fileName << lineNumber;

733
734
    QTC_ASSERT(m_engine, return);
    QTC_ASSERT(m_breakHandler, return);
hjk's avatar
hjk committed
735
736
737
    if (status() != DebuggerInferiorRunning
         && status() != DebuggerInferiorStopped 
         && status() != DebuggerProcessNotReady) {
738
739
740
741
742
        showStatusMessage(tr("Changing breakpoint state requires either a "
            "fully running or fully stopped application."));
        return;
    }

con's avatar
con committed
743
744
    int index = m_breakHandler->indexOf(fileName, lineNumber);
    if (index == -1)
745
        m_breakHandler->setBreakpoint(fileName, lineNumber);
con's avatar
con committed
746
    else
747
748
        m_breakHandler->removeBreakpoint(index);
    m_engine->attemptBreakpointSynchronization();
con's avatar
con committed
749
750
}

751
752
753
754
755
void DebuggerManager::attemptBreakpointSynchronization()
{
    m_engine->attemptBreakpointSynchronization();
}

con's avatar
con committed
756
757
void DebuggerManager::setToolTipExpression(const QPoint &pos, const QString &exp)
{
758
759
    QTC_ASSERT(m_engine, return);
    m_engine->setToolTipExpression(pos, exp);
con's avatar
con committed
760
761
762
763
}

void DebuggerManager::updateWatchModel()
{
764
765
    QTC_ASSERT(m_engine, return);
    m_engine->updateWatchModel();
con's avatar
con committed
766
767
768
769
}

void DebuggerManager::expandChildren(const QModelIndex &idx)
{
770
771
    QTC_ASSERT(m_watchHandler, return);
    m_watchHandler->expandChildren(idx);
con's avatar
con committed
772
773
774
775
}

void DebuggerManager::collapseChildren(const QModelIndex &idx)
{
776
777
    QTC_ASSERT(m_watchHandler, return);
    m_watchHandler->collapseChildren(idx);
con's avatar
con committed
778
779
}

780
void DebuggerManager::removeWatchExpression(const QString &exp)
con's avatar
con committed
781
{
782
783
    QTC_ASSERT(m_watchHandler, return);
    m_watchHandler->removeWatchExpression(exp);
con's avatar
con committed
784
785
786
787
}

QVariant DebuggerManager::sessionValue(const QString &name)
{
788
    // this is answered by the plugin
con's avatar
con committed
789
790
791
792
793
794
795
    QVariant value;
    emit sessionValueRequested(name, &value);
    return value;
}

QVariant DebuggerManager::configValue(const QString &name)
{
796
    // this is answered by the plugin
con's avatar
con committed
797
798
799
800
801
802
803
    QVariant value;
    emit configValueRequested(name, &value);
    return value;
}

void DebuggerManager::queryConfigValue(const QString &name, QVariant *value)
{
804
    // this is answered by the plugin
con's avatar
con committed
805
806
807
808
809
    emit configValueRequested(name, value);
}

void DebuggerManager::setConfigValue(const QString &name, const QVariant &value)
{
810
    // this is answered by the plugin
con's avatar
con committed
811
812
813
814
815
    emit setConfigValueRequested(name, value);
}

void DebuggerManager::startExternalApplication()
{
816
    if (!startNewDebugger(StartExternal))
con's avatar
con committed
817
818
819
820
821
        emit debuggingFinished();
}

void DebuggerManager::attachExternalApplication()
{
822
    if (!startNewDebugger(AttachExternal))
con's avatar
con committed
823
824
825
        emit debuggingFinished();
}

826
827
828
829
830
831
void DebuggerManager::attachCore()
{
    if (!startNewDebugger(AttachCore))
        emit debuggingFinished();
}

Friedemann Kleint's avatar
Friedemann Kleint committed
832
// Figure out the debugger type of an executable
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
833
834
835
836
837
838
839
840
static bool determineDebuggerType(const QString &executable,
                                  DebuggerManager::DebuggerType *dt,
                                  QString *errorMessage)
{
    if (executable.endsWith(QLatin1String(".js"))) {
        *dt = DebuggerManager::ScriptDebugger;
        return true;
    }
841
#ifndef Q_OS_WIN
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
842
    *dt = DebuggerManager::GdbDebugger;
843
    Q_UNUSED(errorMessage)
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
    return true;
#else
    // If a file has PDB files, it has been compiled by VS.
    QStringList pdbFiles;
    if (!getPDBFiles(executable, &pdbFiles, errorMessage))
        return false;
    if (pdbFiles.empty()) {
        *dt = DebuggerManager::GdbDebugger;
        return true;
    }
    // We need the CDB debugger in order to be able to debug VS
    // executables
    if (!winEngine) {
        *errorMessage = DebuggerManager::tr("Debugging VS executables is not supported.");
        return false;
    }
    *dt = DebuggerManager::WinDebugger;
    return true;
#endif
}

Friedemann Kleint's avatar
Friedemann Kleint committed
865
866
867
868
869
870
871
872
873
874
875
876
877
878
// Figure out the debugger type of a PID
static bool determineDebuggerType(int  /* pid */,
                                  DebuggerManager::DebuggerType *dt,
                                  QString * /*errorMessage*/)
{
#ifdef Q_OS_WIN
    // Preferably Windows debugger
    *dt = winEngine ? DebuggerManager::WinDebugger : DebuggerManager::GdbDebugger;
#else
    *dt = DebuggerManager::GdbDebugger;
#endif
    return true;
}

879
bool DebuggerManager::startNewDebugger(DebuggerStartMode mode)
con's avatar
con committed
880
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
881
882
883
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << mode;

con's avatar
con committed
884
885
886
    m_startMode = mode;
    // FIXME: Clean up

Friedemann Kleint's avatar
Friedemann Kleint committed
887
888
    switch  (startMode()) {
    case StartExternal: {
con's avatar
con committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
        StartExternalDialog dlg(mainWindow());
        dlg.setExecutableFile(
            configValue(QLatin1String("LastExternalExecutableFile")).toString());
        dlg.setExecutableArguments(
            configValue(QLatin1String("LastExternalExecutableArguments")).toString());
        if (dlg.exec() != QDialog::Accepted)
            return false;
        setConfigValue(QLatin1String("LastExternalExecutableFile"),
            dlg.executableFile());
        setConfigValue(QLatin1String("LastExternalExecutableArguments"),
            dlg.executableArguments());
        m_executable = dlg.executableFile();
        m_processArgs = dlg.executableArguments().split(' ');
        m_workingDir = QString();
        m_attachedPID = -1;
Friedemann Kleint's avatar
Friedemann Kleint committed
904
905
906
    }
        break;
    case AttachExternal: {
907
        AttachExternalDialog dlg(mainWindow());
con's avatar
con committed
908
909
910
911
912
913
        if (dlg.exec() != QDialog::Accepted)
            return false;
        m_executable = QString();
        m_processArgs = QStringList();
        m_workingDir = QString();
        m_attachedPID = dlg.attachPID();
914
915
916
917
918
        if (m_attachedPID == 0) {
            QMessageBox::warning(mainWindow(), tr("Warning"),
                tr("Cannot attach to PID 0"));
            return false;
        }
Friedemann Kleint's avatar
Friedemann Kleint committed
919
920
921
    }
        break;
    case StartInternal:
con's avatar
con committed
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
        if (m_executable.isEmpty()) {
            QString startDirectory = m_executable;
            if (m_executable.isEmpty()) {
                QString fileName;
                emit currentTextEditorRequested(&fileName, 0, 0);
                if (!fileName.isEmpty()) {
                    const QFileInfo editorFile(fileName);
                    startDirectory = editorFile.dir().absolutePath();
                }
            }
            StartExternalDialog dlg(mainWindow());
            dlg.setExecutableFile(startDirectory);
            if (dlg.exec() != QDialog::Accepted)
                return false;
            m_executable = dlg.executableFile();
            m_processArgs = dlg.executableArguments().split(' ');
            m_workingDir = QString();
            m_attachedPID = 0;
        } else {
            //m_executable = QDir::convertSeparators(m_executable);
            //m_processArgs = sd.processArgs.join(QLatin1String(" "));
            m_attachedPID = 0;
        }
Friedemann Kleint's avatar
Friedemann Kleint committed
945
946
        break;
    case AttachCore: {
947
        AttachCoreDialog dlg(mainWindow());
948
949
        dlg.setExecutableFile(
            configValue(QLatin1String("LastExternalExecutableFile")).toString());
950
951
        dlg.setCoreFile(
            configValue(QLatin1String("LastExternalCoreFile")).toString());
952
953
954
955
        if (dlg.exec() != QDialog::Accepted)
            return false;
        setConfigValue(QLatin1String("LastExternalExecutableFile"),
            dlg.executableFile());
956
957
        setConfigValue(QLatin1String("LastExternalCoreFile"),
            dlg.coreFile());
958
        m_executable = dlg.executableFile();
959
960
        m_coreFile = dlg.coreFile();
        m_processArgs.clear();
961
962
        m_workingDir = QString();
        m_attachedPID = -1;
con's avatar
con committed
963
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
964
965
        break;
    }
con's avatar
con committed
966
967
968

    emit debugModeRequested();

Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
969
970
    DebuggerType type;
    QString errorMessage;
Friedemann Kleint's avatar
Friedemann Kleint committed
971
972
973
974
    const bool hasDebugger = startMode() == AttachExternal ?
         determineDebuggerType(m_attachedPID, &type, &errorMessage) :
         determineDebuggerType(m_executable, &type, &errorMessage);
    if (!hasDebugger) {
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
975
976
977
978
979
980
981
        QMessageBox::warning(mainWindow(), tr("Warning"),
                tr("Cannot debug '%1': %2").arg(m_executable, errorMessage));
        return false;

    }
    if (Debugger::Constants::Internal::debug)
        qDebug() << m_executable << type;
con's avatar
con committed
982

Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
983
    setDebuggerType(type);
984
    setBusyCursor(false);
hjk's avatar
hjk committed
985
986
987
    setStatus(DebuggerProcessStartingUp);
    if (!m_engine->startDebugger()) {
        setStatus(DebuggerProcessNotReady);
con's avatar
con committed
988
        return false;
hjk's avatar
hjk committed
989
    }
con's avatar
con committed
990
991
992
993
994
995
996
997
998
999
1000
1001
1002

    return true;
}

void DebuggerManager::cleanupViews()
{
    resetLocation();
    breakHandler()->setAllPending();
    stackHandler()->removeAll();
    threadsHandler()->removeAll();
    disassemblerHandler()->removeAll();
    modulesHandler()->removeAll();
    watchHandler()->cleanup();
1003
    m_sourceFilesWindow->removeAll();
con's avatar
con committed
1004
1005
1006
1007
}

void DebuggerManager::exitDebugger()
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1008
1009
1010
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO;

hjk's avatar
hjk committed
1011
1012
    if (m_engine)
        m_engine->exitDebugger();
con's avatar
con committed
1013
1014
1015
1016
1017
1018
1019
1020
    cleanupViews();
    setStatus(DebuggerProcessNotReady);
    setBusyCursor(false);
    emit debuggingFinished();
}

void DebuggerManager::assignValueInDebugger(const QString &expr, const QString &value)
{
1021
1022
    QTC_ASSERT(m_engine, return);
    m_engine->assignValueInDebugger(expr, value);
con's avatar
con committed
1023
1024
1025
1026
}

void DebuggerManager::activateFrame(int index)
{
1027
1028
    QTC_ASSERT(m_engine, return);
    m_engine->activateFrame(index);
con's avatar
con committed
1029
1030
1031
1032
}

void DebuggerManager::selectThread(int index)
{
1033
1034
    QTC_ASSERT(m_engine, return);
    m_engine->selectThread(index);
con's avatar
con committed
1035
1036
1037
1038
}

void DebuggerManager::loadAllSymbols()
{
1039
1040
    QTC_ASSERT(m_engine, return);
    m_engine->loadAllSymbols();
con's avatar
con committed
1041
1042
1043
1044
}

void DebuggerManager::loadSymbols(const QString &module)
{
1045
1046
    QTC_ASSERT(m_engine, return);
    m_engine->loadSymbols(module);
con's avatar
con committed
1047
1048
1049
1050
}

void DebuggerManager::stepExec()
{
1051
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1052
    resetLocation();
1053
    m_engine->stepExec();
con's avatar
con committed
1054
1055
1056
1057
} 

void DebuggerManager::stepOutExec()
{
1058
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1059
    resetLocation();
1060
    m_engine->stepOutExec();
con's avatar
con committed
1061
1062
1063
1064
}

void DebuggerManager::nextExec()
{
1065
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1066
    resetLocation();
1067
    m_engine->nextExec();
con's avatar
con committed
1068
1069
1070
1071
}

void DebuggerManager::stepIExec()
{
1072
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1073
    resetLocation();
1074
    m_engine->stepIExec();
con's avatar
con committed
1075
1076
1077
1078
}

void DebuggerManager::nextIExec()
{
1079
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1080
    resetLocation();
1081
    m_engine->nextIExec();
con's avatar
con committed
1082
1083
1084
1085
}

void DebuggerManager::executeDebuggerCommand(const QString &command)
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1086
1087
1088
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO <<command;

1089
1090
    QTC_ASSERT(m_engine, return);
    m_engine->executeDebuggerCommand(command);
con's avatar
con committed
1091
1092
1093
1094
}

void DebuggerManager::sessionLoaded()
{
1095
1096
1097
    cleanupViews();
    setStatus(DebuggerProcessNotReady);
    setBusyCursor(false);
con's avatar
con committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
    loadSessionData();
}

void DebuggerManager::aboutToSaveSession()
{
    saveSessionData();
}

void DebuggerManager::loadSessionData()
{
1108
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1109
    m_breakHandler->loadSessionData();
hjk's avatar
hjk committed
1110
    m_watchHandler->loadSessionData();
1111
    m_engine->loadSessionData();
con's avatar
con committed
1112
1113
1114
1115
}

void DebuggerManager::saveSessionData()
{
1116
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1117
    m_breakHandler->saveSessionData();
hjk's avatar
hjk committed
1118
    m_watchHandler->saveSessionData();
1119
    m_engine->saveSessionData();
con's avatar
con committed
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
}

void DebuggerManager::dumpLog()
{
    QString fileName = QFileDialog::getSaveFileName(mainWindow(),
        tr("Save Debugger Log"), QDir::tempPath());
    if (fileName.isEmpty())
        return;
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly))
        return;
    QTextStream ts(&file);      
    ts << m_outputWindow->inputContents();
    ts << "\n\n=======================================\n\n";
    ts << m_outputWindow->combinedContents();
}

void DebuggerManager::addToWatchWindow()
{
    // requires a selection, but that's the only case we want...
    QObject *ob = 0;
    queryCurrentTextEditor(0, 0, &ob);
    QPlainTextEdit *editor = qobject_cast<QPlainTextEdit*>(ob);
    if (!editor)
        return;
    QTextCursor tc = editor->textCursor();
    watchExpression(tc.selectedText());
}

void DebuggerManager::watchExpression(const QString &expression)
{
1151
1152
    QTC_ASSERT(m_watchHandler, return);
    m_watchHandler->watchExpression(expression);
con's avatar
con committed
1153
1154
1155
1156
}

void DebuggerManager::setBreakpoint(const QString &fileName, int lineNumber)
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1157
1158
1159
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << fileName << lineNumber;

1160
1161
1162
1163
    QTC_ASSERT(m_breakHandler, return);
    QTC_ASSERT(m_engine, return);
    m_breakHandler->setBreakpoint(fileName, lineNumber);
    m_engine->attemptBreakpointSynchronization();
con's avatar
con committed
1164
1165
1166
1167
}

void DebuggerManager::breakByFunction(const QString &functionName)
{
1168
1169
1170
1171
    QTC_ASSERT(m_breakHandler, return);
    QTC_ASSERT(m_engine, return);
    m_breakHandler->breakByFunction(functionName);
    m_engine->attemptBreakpointSynchronization();
con's avatar
con committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
}

void DebuggerManager::breakByFunction()
{
    BreakByFunctionDialog dlg(m_mainWindow);
    if (dlg.exec()) 
        breakByFunction(dlg.functionName());
}

void DebuggerManager::breakAtMain()
{
#ifdef Q_OS_WIN
    breakByFunction("qMain");
#else
    breakByFunction("main");
#endif
}

1190
1191
1192
1193
static bool isAllowedTransition(int from, int to)
{
    return (from == -1)
      || (from == DebuggerProcessNotReady && to == DebuggerProcessStartingUp)
1194
      //|| (from == DebuggerProcessStartingUp && to == DebuggerInferiorStopped)
1195
1196
1197
      || (from == DebuggerInferiorStopped && to == DebuggerInferiorRunningRequested)
      || (from == DebuggerInferiorRunningRequested && to == DebuggerInferiorRunning)
      || (from == DebuggerInferiorRunning && to == DebuggerInferiorStopRequested)
1198
      || (from == DebuggerInferiorRunning && to == DebuggerInferiorStopped)
1199
1200
1201
1202
      || (from == DebuggerInferiorStopRequested && to == DebuggerInferiorStopped)
      || (to == DebuggerProcessNotReady);  
}

con's avatar
con committed
1203
1204
void DebuggerManager::setStatus(int status)
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1205
1206
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << "STATUS CHANGE: from" << m_status << "to" << status;
con's avatar
con committed
1207
1208
1209
1210

    if (status == m_status)
        return;

1211
1212
1213
    if (!isAllowedTransition(m_status, status))
        qDebug() << "UNEXPECTED TRANSITION:  " << m_status << status;

con's avatar
con committed
1214
1215
1216
1217
1218
    m_status = status;

    const bool started = status == DebuggerInferiorRunning
        || status == DebuggerInferiorRunningRequested
        || status == DebuggerInferiorStopRequested
hjk's avatar
hjk committed
1219
        || status == DebuggerInferiorStopped;
con's avatar
con committed
1220
1221
1222

    const bool starting = status == DebuggerProcessStartingUp;
    const bool running = status == DebuggerInferiorRunning;
1223
1224
1225

    const bool ready = status == DebuggerInferiorStopped
            && startMode() != AttachCore;
con's avatar
con committed
1226
1227
1228

    m_startExternalAction->setEnabled(!started && !starting);
    m_attachExternalAction->setEnabled(!started && !starting);
Friedemann Kleint's avatar
Friedemann Kleint committed
1229
1230
    if (m_attachCoreAction)
        m_attachCoreAction->setEnabled(!started && !starting);
con's avatar
con committed
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
    m_watchAction->setEnabled(ready);
    m_breakAction->setEnabled(true);

    bool interruptIsExit = !running;
    if (interruptIsExit) {
        m_stopAction->setIcon(QIcon(":/gdbdebugger/images/debugger_stop_small.png"));
        m_stopAction->setText(tr("Stop Debugger"));
    } else {
        m_stopAction->setIcon(QIcon(":/gdbdebugger/images/debugger_interrupt_small.png"));
        m_stopAction->setText(tr("Interrupt"));
    }

    m_stopAction->setEnabled(started);
    m_resetAction->setEnabled(true);

    m_stepAction->setEnabled(ready);
    m_stepOutAction->setEnabled(ready);
    m_runToLineAction->setEnabled(ready);
    m_runToFunctionAction->setEnabled(ready);
    m_jumpToLineAction->setEnabled(ready);
    m_nextAction->setEnabled(ready);
    m_stepIAction->setEnabled(ready);
    m_nextIAction->setEnabled(ready);
    //showStatusMessage(QString("started: %1, running: %2").arg(started).arg(running));
    emit statusChanged(m_status);
    const bool notbusy = ready || status == DebuggerProcessNotReady;
    setBusyCursor(!notbusy);
}

void DebuggerManager::setBusyCursor(bool busy)
{
1262
    //qDebug() << "BUSY FROM: " << m_busy << " TO: " <<  m_busy;
con's avatar
con committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
    if (busy == m_busy)
        return;
    m_busy = busy;

    QCursor cursor(busy ? Qt::BusyCursor : Qt::ArrowCursor);
    m_breakWindow->setCursor(cursor);
    m_disassemblerWindow->setCursor(cursor);
    m_localsWindow->setCursor(cursor);
    m_modulesWindow->setCursor(cursor);
    m_outputWindow->setCursor(cursor);
    m_registerWindow->setCursor(cursor);
    m_stackWindow->setCursor(cursor);
1275
    m_sourceFilesWindow->setCursor(cursor);
con's avatar
con committed
1276
1277
1278
1279
1280
1281
1282
    m_threadsWindow->setCursor(cursor);
    m_tooltipWindow->setCursor(cursor);
    m_watchersWindow->setCursor(cursor);
}

bool DebuggerManager::skipKnownFrames() const
{
1283
    return m_settings.m_skipKnownFrames;
con's avatar
con committed
1284
1285
1286
1287
}

bool DebuggerManager::debugDumpers() const
{
1288
    return m_settings.m_debugDumpers;
con's avatar
con committed
1289
1290
}

1291
bool DebuggerManager::useDumpers() const
con's avatar
con committed
1292
{
1293
    return m_settings.m_useDumpers;
con's avatar
con committed
1294
1295
}

1296
void DebuggerManager::setUseDumpers(bool on)
1297
{
1298
    QTC_ASSERT(m_engine, return);
1299
    m_settings.m_useDumpers = on;
1300
1301
1302
1303
}

void DebuggerManager::setDebugDumpers(bool on)
{
1304
    QTC_ASSERT(m_engine, return);
1305
1306
1307
1308
1309
1310
1311
1312
    m_settings.m_debugDumpers = on;
}

void DebuggerManager::setSkipKnownFrames(bool on)
{
    m_settings.m_skipKnownFrames = on;
}

con's avatar
con committed
1313
1314
1315
1316
1317
1318
1319
1320
void DebuggerManager::queryCurrentTextEditor(QString *fileName, int *lineNumber,
    QObject **object)
{
    emit currentTextEditorRequested(fileName, lineNumber, object);
}

void DebuggerManager::continueExec()
{
1321
    m_engine->continueInferior();
con's avatar
con committed
1322
1323
1324
1325
}

void DebuggerManager::interruptDebuggingRequest()
{
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1326
1327
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << status();
1328
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1329
1330
1331
1332
1333
    bool interruptIsExit = (status() != DebuggerInferiorRunning);
    if (interruptIsExit)
        exitDebugger();
    else {
        setStatus(DebuggerInferiorStopRequested);
1334
        m_engine->interruptInferior();
con's avatar
con committed
1335
1336
1337
1338
1339
1340
    }
}


void DebuggerManager::runToLineExec()
{
1341
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1342
1343
1344
    QString fileName;
    int lineNumber = -1;
    emit currentTextEditorRequested(&fileName, &lineNumber, 0);
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1345
1346
1347
    if (!fileName.isEmpty()) {
        if (Debugger::Constants::Internal::debug)
            qDebug() << Q_FUNC_INFO << fileName << lineNumber;
1348
        m_engine->runToLineExec(fileName, lineNumber);
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1349
    }
con's avatar
con committed
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
}

void DebuggerManager::runToFunctionExec()
{
    QString fileName;
    int lineNumber = -1;
    QObject *object = 0;
    emit currentTextEditorRequested(&fileName, &lineNumber, &object);
    QPlainTextEdit *ed = qobject_cast<QPlainTextEdit*>(object);
    if (!ed)
        return;
    QTextCursor cursor = ed->textCursor();
    QString functionName = cursor.selectedText();
    if (functionName.isEmpty()) {
        const QTextBlock block = cursor.block();
        const QString line = block.text();
        foreach (const QString &str, line.trimmed().split('(')) {
            QString a;
            for (int i = str.size(); --i >= 0; ) {
                if (!str.at(i).isLetterOrNumber())
                    break;
                a = str.at(i) + a;
            }
            if (!a.isEmpty()) {
                functionName = a;
                break;
            }
        }
    }
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1379
1380
1381
    if (Debugger::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << functionName;

con's avatar
con committed
1382
    if (!functionName.isEmpty())
1383
        m_engine->runToFunctionExec(functionName);
con's avatar
con committed
1384
1385
1386
1387
1388
1389
1390
}

void DebuggerManager::jumpToLineExec()
{
    QString fileName;
    int lineNumber = -1;
    emit currentTextEditorRequested(&fileName, &lineNumber, 0);
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1391
1392
1393
    if (!fileName.isEmpty()) {
        if (Debugger::Constants::Internal::debug)
            qDebug() << Q_FUNC_INFO << fileName << lineNumber;
1394
        m_engine->jumpToLineExec(fileName, lineNumber);
Friedemann Kleint's avatar
m.txt    
Friedemann Kleint committed
1395
    }
con's avatar
con committed
1396
1397
1398
1399
}

void DebuggerManager::resetLocation()
{
1400
    // connected to the plugin
con's avatar
con committed
1401
1402
1403
1404
1405
1406
    emit resetLocationRequested();
}

void DebuggerManager::gotoLocation(const QString &fileName, int line,
    bool setMarker)
{
1407
    // connected to the plugin
con's avatar
con committed
1408
1409
1410
    emit gotoLocationRequested(fileName, line, setMarker);
}

1411
1412
1413
1414
1415
1416
1417
void DebuggerManager::fileOpen(const QString &fileName)
{
    // connected to the plugin
    emit gotoLocationRequested(fileName, 1, false);
}


con's avatar
con committed
1418
1419
1420
1421
1422
1423
1424
1425
//////////////////////////////////////////////////////////////////////
//
// Disassembler specific stuff
//
//////////////////////////////////////////////////////////////////////

void DebuggerManager::reloadDisassembler()
{
1426
    QTC_ASSERT(m_engine, return);
con's avatar
con committed
1427
1428
    if (!m_disassemblerDock || !m_disassemblerDock->isVisible())
        return;
1429
    m_engine->reloadDisassembler();
con's avatar
con committed
1430
1431
1432
1433
1434
1435
1436
1437
1438
}

void DebuggerManager::disassemblerDockToggled(bool on)
{
    if (on)
        reloadDisassembler();
}


1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
//////////////////////////////////////////////////////////////////////
//
// Sourec files specific stuff
//
//////////////////////////////////////////////////////////////////////

void DebuggerManager::reloadSourceFiles()
{
    if (!m_sourceFilesDock || !m_sourceFilesDock->isVisible())
        return;
    m_engine->reloadSourceFiles();
}

void DebuggerManager::sourceFilesDockToggled(bool on)
{
    if (on)
        reloadSourceFiles();
}


con's avatar
con committed
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
//////////////////////////////////////////////////////////////////////
//
// Modules specific stuff
//
//////////////////////////////////////////////////////////////////////

void DebuggerManager::reloadModules()
{
    if (!m_modulesDock || !m_modulesDock->isVisible())
        return;
1469
    m_engine->reloadModules();
con's avatar
con committed
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
}

void DebuggerManager::modulesDockToggled(bool on)
{
    if (on)
        reloadModules();
}


//////////////////////////////////////////////////////////////////////
//
// Output specific stuff
//
//////////////////////////////////////////////////////////////////////

void DebuggerManager::showDebuggerOutput(const QString &prefix, const QString &msg)
{
1487
    QTC_ASSERT(m_outputWindow, return);
con's avatar
con committed
1488
1489
1490
1491
1492
    m_outputWindow->showOutput(prefix, msg);
}

void DebuggerManager::showDebuggerInput(const QString &prefix, const QString &msg)
{
1493
    QTC_ASSERT(m_outputWindow, return);
con's avatar
con committed
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
    m_outputWindow->showInput(prefix, msg);
}


//////////////////////////////////////////////////////////////////////
//
// Register specific stuff
//
//////////////////////////////////////////////////////////////////////

void DebuggerManager::registerDockToggled(bool on)
{
    if (on)
        reloadRegisters();
}

void DebuggerManager::reloadRegisters()
{
    if (!m_registerDock || !m_registerDock->isVisible())
        return;
1514
    m_engine->reloadRegisters();
con's avatar
con committed
1515
1516
1517
1518
}


#include "debuggermanager.moc"