gdbengine.h 18.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 32
#ifndef DEBUGGER_GDBENGINE_H
#define DEBUGGER_GDBENGINE_H

33
#include "debuggerengine.h"
34
#include "gdbmi.h"
ck's avatar
ck committed
35
#include "localgdbprocess.h"
36
#include "watchutils.h"
37

con's avatar
con committed
38
#include <QtCore/QByteArray>
hjk's avatar
hjk committed
39
#include <QtCore/QFutureInterface>
con's avatar
con committed
40 41
#include <QtCore/QHash>
#include <QtCore/QMap>
42
#include <QtCore/QMultiMap>
con's avatar
con committed
43 44
#include <QtCore/QObject>
#include <QtCore/QPoint>
hjk's avatar
hjk committed
45
#include <QtCore/QSet>
46
#include <QtCore/QTextCodec>
47
#include <QtCore/QTime>
con's avatar
con committed
48
#include <QtCore/QVariant>
49
#include <QtCore/QSharedPointer>
con's avatar
con committed
50 51

QT_BEGIN_NAMESPACE
52 53
class QMainWindow;
class QMessageBox;
54
class QTimer;
con's avatar
con committed
55 56 57 58 59
QT_END_NAMESPACE

namespace Debugger {
namespace Internal {

hjk's avatar
hjk committed
60
class AbstractGdbAdapter;
ck's avatar
ck committed
61
class AbstractGdbProcess;
hjk's avatar
hjk committed
62
class GdbResponse;
con's avatar
con committed
63 64 65
class GdbMi;

class BreakpointData;
66
class WatchData;
67
class DisassemblerAgentCookie;
con's avatar
con committed
68

69 70
class AttachGdbAdapter;
class CoreGdbAdapter;
ck's avatar
ck committed
71 72
class LocalPlainGdbAdapter;
class RemoteGdbServerAdapter;
73
class TrkGdbAdapter;
74

75
enum DebuggingHelperState
con's avatar
con committed
76
{
77 78 79 80
    DebuggingHelperUninitialized,
    DebuggingHelperLoadTried,
    DebuggingHelperAvailable,
    DebuggingHelperUnavailable,
con's avatar
con committed
81 82
};

83

84
class GdbEngine : public DebuggerEngine
con's avatar
con committed
85 86 87 88
{
    Q_OBJECT

public:
89
    explicit GdbEngine(const DebuggerStartParameters &startParameters);
con's avatar
con committed
90 91 92
    ~GdbEngine();

private:
93
    friend class AbstractGdbAdapter;
ck's avatar
ck committed
94
    friend class AbstractPlainGdbAdapter;
95 96
    friend class AttachGdbAdapter;
    friend class CoreGdbAdapter;
ck's avatar
ck committed
97
    friend class LocalPlainGdbAdapter;
98
    friend class TermGdbAdapter;
ck's avatar
ck committed
99 100
    friend class RemoteGdbServerAdapter;
    friend class RemotePlainGdbAdapter;
101
    friend class TrkGdbAdapter;
102
    friend class TcfTrkGdbAdapter;
hjk's avatar
hjk committed
103

104
private: ////////// General Interface //////////
con's avatar
con committed
105

106
    virtual void startDebugger();
107
    virtual unsigned debuggerCapabilities() const;
108 109 110
    virtual void exitDebugger();
    virtual void detachDebugger();
    virtual void shutdown();
con's avatar
con committed
111

112
    virtual void executeDebuggerCommand(const QString &command);
113
    virtual QString qtNamespace() const { return m_dumperHelper.qtNamespace(); }
con's avatar
con committed
114

115
private: ////////// General State //////////
con's avatar
con committed
116

117 118 119
    void initializeVariables();
    DebuggerStartMode startMode() const;
    Q_SLOT void setAutoDerefPointers(const QVariant &on);
120

121
    bool m_registerNamesListed;
122

123
private: ////////// Gdb Process Management //////////
124

125
    AbstractGdbAdapter *createAdapter();
126
    void connectAdapter();
127 128 129
    bool startGdb(const QStringList &args = QStringList(),
                  const QString &gdb = QString(),
                  const QString &settingsIdHint = QString());
130
    void startInferiorPhase2();
131

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
132 133 134
    void handleInferiorShutdown(const GdbResponse &response);
    void handleGdbExit(const GdbResponse &response);

135
private slots:
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
136 137
    void handleGdbFinished(int, QProcess::ExitStatus status);
    void handleGdbError(QProcess::ProcessError error);
138 139 140
    void readGdbStandardOutput();
    void readGdbStandardError();
    void readDebugeeOutput(const QByteArray &data);
con's avatar
con committed
141

142
    void handleAdapterStarted();
143 144
    void handleAdapterStartFailed(const QString &msg,
        const QString &settingsIdHint = QString());
con's avatar
con committed
145

146 147
    void handleInferiorPrepared();

148 149 150
    void handleInferiorStartFailed(const QString &msg);

    void handleAdapterCrashed(const QString &msg);
con's avatar
con committed
151

152 153 154 155 156
private:
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

    QByteArray m_inbuffer;
157
    bool m_busy;
con's avatar
con committed
158

159 160
    AbstractGdbAdapter *m_gdbAdapter;

161 162 163 164
    // Name of the convenience variable containing the last
    // known function return value.
    QByteArray m_resultVarName;

165 166
private: ////////// Gdb Command Management //////////

167
    public: // Otherwise the Qt flag macros are unhappy.
168 169
    enum GdbCommandFlag {
        NoFlags = 0,
170 171 172 173 174 175
        // The command needs a stopped inferior.
        NeedsStop = 1, 
        // No need to wait for the reply before continuing inferior.
        Discardable = 2,
        // Trigger watch model rebuild when no such commands are pending anymore.
        RebuildWatchModel = 4,
176
        WatchUpdate = Discardable | RebuildWatchModel,
177 178 179 180 181 182 183 184 185 186
        // We can live without recieving an answer.
        NonCriticalResponse = 8,
        // Callback expects GdbResultRunning instead of GdbResultDone.
        RunRequest = 16,
        // Callback expects GdbResultExit instead of GdbResultDone.
        ExitRequest = 32,
        // Auto-set inferior shutdown related states.
        LosesChild = 64,
        // Trigger breakpoint model rebuild when no such commands are pending anymore.
        RebuildBreakpointModel = 128,
187 188
        // This command needs to be send immediately.
        Immediate = 256,
189 190
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)
191
    private:
192

hjk's avatar
hjk committed
193
    typedef void (GdbEngine::*GdbCommandCallback)
hjk's avatar
hjk committed
194
        (const GdbResponse &response);
hjk's avatar
hjk committed
195
    typedef void (AbstractGdbAdapter::*AdapterCallback)
hjk's avatar
hjk committed
196
        (const GdbResponse &response);
197 198 199

    struct GdbCommand
    {
hjk's avatar
hjk committed
200 201 202
        GdbCommand()
            : flags(0), callback(0), adapterCallback(0), callbackName(0)
        {}
203 204 205

        int flags;
        GdbCommandCallback callback;
hjk's avatar
hjk committed
206
        AdapterCallback adapterCallback;
207
        const char *callbackName;
208
        QByteArray command;
209
        QVariant cookie;
210
        QTime postTime;
211 212
    };

213
    // Type and cookie are sender-internal data, opaque for the "event
con's avatar
con committed
214
    // queue". resultNeeded == true increments m_pendingResults on
hjk's avatar
hjk committed
215
    // send and decrements on receipt, effectively preventing
con's avatar
con committed
216
    // watch model updates before everything is finished.
hjk's avatar
hjk committed
217
    void flushCommand(const GdbCommand &cmd);
218
    void postCommand(const QByteArray &command,
219 220 221 222
                     GdbCommandFlags flags,
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
223
    void postCommand(const QByteArray &command,
224 225 226
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
227
    void postCommand(const QByteArray &command,
hjk's avatar
hjk committed
228 229 230
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
231
    void postCommand(const QByteArray &command,
232 233 234 235
                     GdbCommandFlags flags,
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
236
    void postCommandHelper(const GdbCommand &cmd);
237
    void flushQueuedCommands();
238
    Q_SLOT void commandTimeout();
con's avatar
con committed
239 240
    void setTokenBarrier();

241
    QHash<int, GdbCommand> m_cookieForToken;
242
    int commandTimeoutTime() const;
243
    QTimer *m_commandTimer;
con's avatar
con committed
244

245 246
    QByteArray m_pendingConsoleStreamOutput;
    QByteArray m_pendingLogStreamOutput;
247

248
    // This contains the first token number for the current round
249 250 251
    // of evaluation. Responses with older tokens are considers
    // out of date and discarded.
    int m_oldestAcceptableToken;
con's avatar
con committed
252

253
    int m_pendingWatchRequests; // Watch updating commands in flight
254
    int m_pendingBreakpointRequests; // Watch updating commands in flight
hjk's avatar
hjk committed
255

256
    typedef void (GdbEngine::*CommandsDoneCallback)();
257
    // This function is called after all previous responses have been received.
258
    CommandsDoneCallback m_commandsDoneCallback;
hjk's avatar
hjk committed
259

260
    QList<GdbCommand> m_commandsToRunOnTemporaryBreak;
hjk's avatar
hjk committed
261
    int gdbVersion() const { return m_gdbVersion; }
262 263

private: ////////// Gdb Output, State & Capability Handling //////////
hjk's avatar
hjk committed
264

265
    void handleResponse(const QByteArray &buff);
266
    void handleStopResponse(const GdbMi &data);
267
    void handleResultRecord(GdbResponse *response);
hjk's avatar
hjk committed
268
    void handleStop1(const GdbResponse &response);
269
    void handleStop1(const GdbMi &data);
270 271
    StackFrame parseStackFrame(const GdbMi &mi, int level);

hjk's avatar
hjk committed
272
    bool isSynchroneous() const { return hasPython(); }
273
    virtual bool hasPython() const;
274 275 276
    bool supportsThreads() const;

    // Gdb initialization sequence
hjk's avatar
hjk committed
277
    void handleShowVersion(const GdbResponse &response);
278
    void handleHasPython(const GdbResponse &response);
con's avatar
con committed
279

280
    int m_gdbVersion; // 6.8.0 is 60800
281
    int m_gdbBuildVersion; // MAC only?
282
    bool m_isMacGdb;
283
    bool m_hasPython;
hjk's avatar
hjk committed
284

285
private: ////////// Inferior Management //////////
286

287
    // This should be always the last call in a function.
288
    Q_SLOT virtual void attemptBreakpointSynchronization();
289

hjk's avatar
hjk committed
290 291 292 293 294
    virtual void executeStep();
    virtual void executeStepOut();
    virtual void executeNext();
    virtual void executeStepI();
    virtual void executeNextI();
con's avatar
con committed
295

296 297 298 299
    void continueInferiorInternal();
    void autoContinueInferior();
    virtual void continueInferior();
    virtual void interruptInferior();
300
    void interruptInferiorTemporarily();
con's avatar
con committed
301

hjk's avatar
hjk committed
302 303
    virtual void executeRunToLine(const QString &fileName, int lineNumber);
    virtual void executeRunToFunction(const QString &functionName);
304
//    void handleExecRunToFunction(const GdbResponse &response);
hjk's avatar
hjk committed
305 306
    virtual void executeJumpToLine(const QString &fileName, int lineNumber);
    virtual void executeReturn();
con's avatar
con committed
307

hjk's avatar
hjk committed
308 309 310 311
    void handleExecuteContinue(const GdbResponse &response);
    void handleExecuteStep(const GdbResponse &response);
    void handleExecuteNext(const GdbResponse &response);
    void handleExecuteReturn(const GdbResponse &response);
con's avatar
con committed
312

313
    void maybeHandleInferiorPidChanged(const QString &pid);
314 315
    void handleInfoProc(const GdbResponse &response);

316
    QByteArray m_entryPoint;
hjk's avatar
hjk committed
317
    QFutureInterface<void> *m_progress;
318

319 320 321 322 323
private: ////////// View & Data Stuff //////////

    virtual void selectThread(int index);
    virtual void activateFrame(int index);

con's avatar
con committed
324 325 326
    //
    // Breakpoint specific stuff
    //
hjk's avatar
hjk committed
327
    void handleBreakList(const GdbResponse &response);
con's avatar
con committed
328
    void handleBreakList(const GdbMi &table);
hjk's avatar
hjk committed
329
    void handleBreakIgnore(const GdbResponse &response);
330
    void handleBreakDisable(const GdbResponse &response);
hjk's avatar
hjk committed
331
    void handleBreakInsert1(const GdbResponse &response);
332
    void handleBreakInsert2(const GdbResponse &response);
hjk's avatar
hjk committed
333 334
    void handleBreakCondition(const GdbResponse &response);
    void handleBreakInfo(const GdbResponse &response);
335
    void handleWatchInsert(const GdbResponse &response);
336
    void handleInfoLine(const GdbResponse &response);
con's avatar
con committed
337
    void extractDataFromInfoBreak(const QString &output, BreakpointData *data);
338
    void setBreakpointDataFromOutput(BreakpointData *data, const GdbMi &bkpt);
339
    QByteArray breakpointLocation(const BreakpointData *data);
con's avatar
con committed
340
    void sendInsertBreakpoint(int index);
341
    QString breakLocation(const QString &file) const;
342
    void reloadBreakListInternal();
con's avatar
con committed
343 344 345 346

    //
    // Modules specific stuff
    //
347 348
    virtual void loadSymbols(const QString &moduleName);
    virtual void loadAllSymbols();
hjk's avatar
hjk committed
349
    virtual void requestModuleSymbols(const QString &moduleName);
350
    virtual void reloadModules();
351
    void reloadModulesInternal();
hjk's avatar
hjk committed
352
    void handleModulesList(const GdbResponse &response);
con's avatar
con committed
353

354
    bool m_modulesListOutdated;
con's avatar
con committed
355

356 357 358 359 360 361 362 363 364
    //
    // Snapshot specific stuff
    //
    virtual void makeSnapshot();
    void handleMakeSnapshot(const GdbResponse &response);
    void handleActivateSnapshot(const GdbResponse &response);
    void activateSnapshot(int index);
    void activateSnapshot2();

con's avatar
con committed
365 366
    //
    // Register specific stuff
hjk's avatar
hjk committed
367
    //
368 369
    Q_SLOT virtual void reloadRegisters();
    virtual void setRegisterValue(int nr, const QString &value);
hjk's avatar
hjk committed
370 371
    void handleRegisterListNames(const GdbResponse &response);
    void handleRegisterListValues(const GdbResponse &response);
con's avatar
con committed
372

373 374 375
    //
    // Disassembler specific stuff
    //
376
    virtual void fetchDisassembler(DisassemblerViewAgent *agent);
377
    void fetchDisassemblerByAddress(const DisassemblerAgentCookie &ac,
378
        bool useMixedMode);
379
    void fetchDisassemblerByCli(const DisassemblerAgentCookie &ac,
380
        bool useMixedMode);
381
    void fetchDisassemblerByAddressCli(const DisassemblerAgentCookie &ac);
382
    void handleFetchDisassemblerByCli(const GdbResponse &response);
383 384 385 386 387
    void handleFetchDisassemblerByLine(const GdbResponse &response);
    void handleFetchDisassemblerByAddress1(const GdbResponse &response);
    void handleFetchDisassemblerByAddress0(const GdbResponse &response);
    QString parseDisassembler(const GdbMi &lines);

388 389
    //
    // Source file specific stuff
hjk's avatar
hjk committed
390
    //
391
    virtual void reloadSourceFiles();
392
    void reloadSourceFilesInternal();
393 394 395
    void handleQuerySources(const GdbResponse &response);

    QString fullName(const QString &fileName);
396 397 398 399 400
#ifdef Q_OS_WIN
    QString cleanupFullName(const QString &fileName);
#else
    QString cleanupFullName(const QString &fileName) { return fileName; }
#endif
401 402 403 404

    // awful hack to keep track of used files
    QMap<QString, QString> m_shortToFullName;
    QMap<QString, QString> m_fullToShortName;
con's avatar
con committed
405

406
    void invalidateSourcesList();
407 408
    bool m_sourcesListOutdated;
    bool m_sourcesListUpdating;
409
    bool m_breakListOutdated;
410

con's avatar
con committed
411 412
    //
    // Stack specific stuff
hjk's avatar
hjk committed
413
    //
414
    void updateAll();
415 416
        void updateAllClassic();
        void updateAllPython();
hjk's avatar
hjk committed
417 418
    void handleStackListFrames(const GdbResponse &response);
    void handleStackSelectThread(const GdbResponse &response);
419
    void handleStackSelectFrame(const GdbResponse &response);
420 421
    void handleThreadListIds(const GdbResponse &response);
    void handleThreadInfo(const GdbResponse &response);
hjk's avatar
hjk committed
422
    Q_SLOT void reloadStack(bool forceGotoLocation);
423
    Q_SLOT virtual void reloadFullStack();
424
    int currentFrame() const;
con's avatar
con committed
425

426 427
    QList<GdbMi> m_currentFunctionArgs;
    QString m_currentFrame;
con's avatar
con committed
428 429

    //
430
    // Watch specific stuff
hjk's avatar
hjk committed
431
    //
hjk's avatar
hjk committed
432 433
    virtual void setToolTipExpression(const QPoint &mousePos,
        TextEditor::ITextEditor *editor, int cursorPos);
con's avatar
con committed
434

435 436
    virtual void assignValueInDebugger(const QString &expr, const QString &value);

437 438
    virtual void fetchMemory(MemoryViewAgent *agent, QObject *token,
        quint64 addr, quint64 length);
439 440 441 442
    void handleFetchMemory(const GdbResponse &response);

    virtual void watchPoint(const QPoint &);
    void handleWatchPoint(const GdbResponse &response);
con's avatar
con committed
443 444

    // FIXME: BaseClass. called to improve situation for a watch item
445
    void updateSubItemClassic(const WatchData &data);
con's avatar
con committed
446

447
    void virtual updateWatchData(const WatchData &data);
448
    Q_SLOT void updateWatchDataHelper(const WatchData &data);
449
    void rebuildWatchModel();
450
    bool showToolTip();
con's avatar
con committed
451 452 453

    void insertData(const WatchData &data);
    void sendWatchParameters(const QByteArray &params0);
454
    void createGdbVariableClassic(const WatchData &data);
con's avatar
con committed
455

456 457
    void runDebuggingHelperClassic(const WatchData &data, bool dumpChildren);
    void runDirectDebuggingHelperClassic(const WatchData &data, bool dumpChildren);
458
    bool hasDebuggingHelperForType(const QString &type) const;
con's avatar
con committed
459

460 461
    void handleVarListChildrenClassic(const GdbResponse &response);
    void handleVarListChildrenHelperClassic(const GdbMi &child,
462
        const WatchData &parent);
hjk's avatar
hjk committed
463 464
    void handleVarCreate(const GdbResponse &response);
    void handleVarAssign(const GdbResponse &response);
465
    void handleEvaluateExpressionClassic(const GdbResponse &response);
466 467 468
    void handleQueryDebuggingHelperClassic(const GdbResponse &response);
    void handleDebuggingHelperValue2Classic(const GdbResponse &response);
    void handleDebuggingHelperValue3Classic(const GdbResponse &response);
hjk's avatar
hjk committed
469 470
    void handleDebuggingHelperEditValue(const GdbResponse &response);
    void handleDebuggingHelperSetup(const GdbResponse &response);
471
    void handleDebuggingHelperVersionCheckClassic(const GdbResponse &response);
472

473 474 475
    Q_SLOT void createFullBacktrace();
    void handleCreateFullBacktrace(const GdbResponse &response);

476
    void updateLocals(const QVariant &cookie = QVariant());
477 478 479
        void updateLocalsClassic(const QVariant &cookie);
        void updateLocalsPython(const QByteArray &varList);
            void handleStackFramePython(const GdbResponse &response);
480

481 482 483
    void handleStackListLocalsClassic(const GdbResponse &response);
    void handleStackListLocalsPython(const GdbResponse &response);

484 485 486 487
    WatchData localVariable(const GdbMi &item,
                            const QStringList &uninitializedVariables,
                            QMap<QByteArray, int> *seen);
    void setLocals(const QList<GdbMi> &locals);
488
    void handleStackListArgumentsClassic(const GdbResponse &response);
con's avatar
con committed
489

490
    QSet<QByteArray> m_processedNames;
con's avatar
con committed
491

492 493 494
    //
    // Dumper Management
    //
495
    bool checkDebuggingHelpers();
496 497 498 499 500
        bool checkDebuggingHelpersClassic();
    void setDebuggingHelperStateClassic(DebuggingHelperState);
    void tryLoadDebuggingHelpersClassic();
    void tryQueryDebuggingHelpersClassic();
    Q_SLOT void recheckDebuggingHelperAvailabilityClassic();
501 502
    void connectDebuggingHelperActions();
    void disconnectDebuggingHelperActions();
503
    Q_SLOT void setDebugDebuggingHelpersClassic(const QVariant &on);
504
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
505

506 507
    DebuggingHelperState m_debuggingHelperState;
    QtDumperHelper m_dumperHelper;
508
    QString m_gdb;
509

510 511 512
    // 
    // Convenience Functions
    //
513
    QString errorMessage(QProcess::ProcessError error);
514 515
    QMessageBox *showMessageBox(int icon, const QString &title, const QString &text,
        int buttons = 0);
516
    QMainWindow *mainWindow() const;
ck's avatar
ck committed
517
    AbstractGdbProcess *gdbProc() const;
518

519 520 521
    static QString m_toolTipExpression;
    static QPoint m_toolTipPos;
    static QByteArray tooltipINameForExpression(const QByteArray &exp);
con's avatar
con committed
522 523 524 525 526
};

} // namespace Internal
} // namespace Debugger

527 528
Q_DECLARE_OPERATORS_FOR_FLAGS(Debugger::Internal::GdbEngine::GdbCommandFlags)

con's avatar
con committed
529
#endif // DEBUGGER_GDBENGINE_H