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;
hjk's avatar
hjk committed
102

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

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

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

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

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

120
    bool m_registerNamesListed;
121

122
private: ////////// Gdb Process Management //////////
123

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

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

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

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

145 146
    void handleInferiorPrepared();

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

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

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

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

158 159
    AbstractGdbAdapter *m_gdbAdapter;

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

164 165
private: ////////// Gdb Command Management //////////

166
    public: // Otherwise the Qt flag macros are unhappy.
167 168
    enum GdbCommandFlag {
        NoFlags = 0,
169 170 171 172 173 174
        // 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,
175
        WatchUpdate = Discardable | RebuildWatchModel,
176 177 178 179 180 181 182 183 184 185
        // 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,
186 187
        // This command needs to be send immediately.
        Immediate = 256,
188 189
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)
190
    private:
191

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

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

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

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

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

244 245
    QByteArray m_pendingConsoleStreamOutput;
    QByteArray m_pendingLogStreamOutput;
246

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

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

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

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

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

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

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

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

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

284
private: ////////// Inferior Management //////////
285

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

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

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

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

hjk's avatar
hjk committed
307 308 309 310
    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
311

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

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

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

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

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

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

353
    bool m_modulesListOutdated;
con's avatar
con committed
354

355 356 357 358 359 360 361 362 363
    //
    // 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
364 365
    //
    // Register specific stuff
hjk's avatar
hjk committed
366
    //
367 368
    Q_SLOT virtual void reloadRegisters();
    virtual void setRegisterValue(int nr, const QString &value);
hjk's avatar
hjk committed
369 370
    void handleRegisterListNames(const GdbResponse &response);
    void handleRegisterListValues(const GdbResponse &response);
con's avatar
con committed
371

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

} // namespace Internal
} // namespace Debugger

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

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