gdbengine.h 16.2 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
** 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 "idebuggerengine.h"
hjk's avatar
hjk committed
34
#include "debuggermanager.h" // only for StartParameters
35
#include "gdbmi.h"
36
#include "watchutils.h"
37

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

QT_BEGIN_NAMESPACE
class QAction;
class QAbstractItemModel;
class QWidget;
53
class QMainWindow;
con's avatar
con committed
54 55 56
QT_END_NAMESPACE

namespace Debugger {
57
class DebuggerManager;
con's avatar
con committed
58 59
namespace Internal {

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

class BreakpointData;
65
class WatchData;
con's avatar
con committed
66

67 68
class AttachGdbAdapter;
class CoreGdbAdapter;
69 70
class PlainGdbAdapter;
class RemoteGdbAdapter;
71
class TrkGdbAdapter;
72
struct TrkOptions;
73

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

class GdbEngine : public IDebuggerEngine
{
    Q_OBJECT

public:
hjk's avatar
hjk committed
87
    explicit GdbEngine(DebuggerManager *manager);
con's avatar
con committed
88 89 90
    ~GdbEngine();

private:
91
    friend class AbstractGdbAdapter;
92 93
    friend class AttachGdbAdapter;
    friend class CoreGdbAdapter;
hjk's avatar
hjk committed
94
    friend class PlainGdbAdapter;
95
    friend class TermGdbAdapter;
96
    friend class RemoteGdbAdapter;
97
    friend class TrkGdbAdapter;
hjk's avatar
hjk committed
98

99
private: ////////// General Interface //////////
con's avatar
con committed
100

101
    virtual void addOptionPages(QList<Core::IOptionsPage*> *opts) const;
con's avatar
con committed
102

103
    virtual bool checkConfiguration(int toolChain, QString *errorMessage, QString *settingsPage= 0) const;
con's avatar
con committed
104

105
    virtual bool isGdbEngine() const { return true; }
con's avatar
con committed
106

107 108 109 110
    virtual void startDebugger(const DebuggerStartParametersPtr &sp);
    virtual void exitDebugger();
    virtual void detachDebugger();
    virtual void shutdown();
con's avatar
con committed
111

112
    virtual void executeDebuggerCommand(const QString &command);
con's avatar
con committed
113

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

116 117 118 119 120
    void initializeVariables();
    DebuggerStartMode startMode() const;
    const DebuggerStartParameters &startParameters() const
        { return *m_startParameters; }
    Q_SLOT void setAutoDerefPointers(const QVariant &on);
121

122 123
    DebuggerStartParametersPtr m_startParameters;
    QSharedPointer<TrkOptions> m_trkOptions;
124

125
private: ////////// Gdb Process Management //////////
126

127 128
    AbstractGdbAdapter *createAdapter(const DebuggerStartParametersPtr &dp);
    void connectAdapter();
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
129
    bool startGdb(const QStringList &args = QStringList(), const QString &gdb = 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 136 137 138
    void gdbInputAvailable(int channel, const QString &msg)
    { m_manager->showDebuggerInput(channel, msg); }
    void gdbOutputAvailable(int channel, const QString &msg)
    { m_manager->showDebuggerOutput(channel, msg); }
139

140
private slots:
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
141 142
    void handleGdbFinished(int, QProcess::ExitStatus status);
    void handleGdbError(QProcess::ProcessError error);
143 144 145
    void readGdbStandardOutput();
    void readGdbStandardError();
    void readDebugeeOutput(const QByteArray &data);
con's avatar
con committed
146

147 148
    void handleAdapterStarted();
    void handleAdapterStartFailed(const QString &msg, const QString &settingsIdHint = QString());
con's avatar
con committed
149

150 151
    void handleInferiorPrepared();

152 153 154
    void handleInferiorStartFailed(const QString &msg);

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

156 157 158 159 160
private:
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

    QByteArray m_inbuffer;
con's avatar
con committed
161

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
162
    QProcess m_gdbProc;
163 164 165 166 167
    AbstractGdbAdapter *m_gdbAdapter;

private: ////////// Gdb Command Management //////////

    public: // otherwise the Qt flag macros are unhappy
168 169
    enum GdbCommandFlag {
        NoFlags = 0,
170 171 172
        NeedsStop = 1,    // The command needs a stopped inferior
        Discardable = 2,  // No need to wait for the reply before continuing inferior
        RebuildModel = 4, // Trigger model rebuild when no such commands are pending any more
173
        WatchUpdate = Discardable | RebuildModel,
174
        EmbedToken = 8,   // Expand %1 in the command to the command token
175 176
        RunRequest = 16,  // Callback expect GdbResultRunning instead of GdbResultDone
        ExitRequest = 32  // Callback expect GdbResultExit instead of GdbResultDone
177 178
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)
179
    private:
180

hjk's avatar
hjk committed
181
    typedef void (GdbEngine::*GdbCommandCallback)
hjk's avatar
hjk committed
182
        (const GdbResponse &response);
hjk's avatar
hjk committed
183
    typedef void (AbstractGdbAdapter::*AdapterCallback)
hjk's avatar
hjk committed
184
        (const GdbResponse &response);
185 186 187

    struct GdbCommand
    {
hjk's avatar
hjk committed
188 189 190
        GdbCommand()
            : flags(0), callback(0), adapterCallback(0), callbackName(0)
        {}
191 192 193

        int flags;
        GdbCommandCallback callback;
hjk's avatar
hjk committed
194
        AdapterCallback adapterCallback;
195 196 197
        const char *callbackName;
        QString command;
        QVariant cookie;
198
        QTime postTime;
199 200
    };

con's avatar
con committed
201 202
    // type and cookie are sender-internal data, opaque for the "event
    // queue". resultNeeded == true increments m_pendingResults on
hjk's avatar
hjk committed
203
    // send and decrements on receipt, effectively preventing
con's avatar
con committed
204
    // watch model updates before everything is finished.
hjk's avatar
hjk committed
205
    void flushCommand(const GdbCommand &cmd);
206
    void postCommand(const QString &command,
207 208 209 210
                     GdbCommandFlags flags,
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
211
    void postCommand(const QString &command,
212 213 214
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
215 216 217 218
    void postCommand(const QString &command,
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
219 220 221 222 223
    void postCommand(const QString &command,
                     GdbCommandFlags flags,
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
224
    void postCommandHelper(const GdbCommand &cmd);
con's avatar
con committed
225 226
    void setTokenBarrier();

227
    QHash<int, GdbCommand> m_cookieForToken;
con's avatar
con committed
228

229 230
    QByteArray m_pendingConsoleStreamOutput;
    QByteArray m_pendingLogStreamOutput;
231

232 233 234 235
    // contains the first token number for the current round
    // of evaluation. Responses with older tokens are considers
    // out of date and discarded.
    int m_oldestAcceptableToken;
con's avatar
con committed
236

237
    int m_pendingRequests; // Watch updating commands in flight
hjk's avatar
hjk committed
238

239 240 241
    typedef void (GdbEngine::*CommandsDoneCallback)();
    // function called after all previous responses have been received
    CommandsDoneCallback m_commandsDoneCallback;
hjk's avatar
hjk committed
242

243 244 245
    QList<GdbCommand> m_commandsToRunOnTemporaryBreak;

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

247
    void handleResponse(const QByteArray &buff);
248
    void handleStopResponse(const GdbMi &data);
249
    void handleResultRecord(GdbResponse *response);
hjk's avatar
hjk committed
250
    void handleStop1(const GdbResponse &response);
251
    void handleStop1(const GdbMi &data);
hjk's avatar
hjk committed
252
    void handleStop2(const GdbResponse &response);
hjk's avatar
hjk committed
253
    void handleStop2(const GdbMi &data);
254 255 256 257 258 259
    StackFrame parseStackFrame(const GdbMi &mi, int level);

    virtual bool isSynchroneous() const;
    bool supportsThreads() const;

    // Gdb initialization sequence
hjk's avatar
hjk committed
260
    void handleShowVersion(const GdbResponse &response);
hjk's avatar
hjk committed
261
    void handleIsSynchroneous(const GdbResponse &response);
con's avatar
con committed
262

263
    int m_gdbVersion; // 6.8.0 is 60800
264
    int m_gdbBuildVersion; // MAC only?
265
    bool m_isMacGdb;
266
    bool m_isSynchroneous; // Can act synchroneously?
hjk's avatar
hjk committed
267

268
private: ////////// Inferior Management //////////
269

270
    // This should be always the last call in a function.
271
    Q_SLOT virtual void attemptBreakpointSynchronization();
272
    void attemptBreakpointSynchronization2(const GdbResponse &);
273

274 275 276 277 278
    virtual void stepExec();
    virtual void stepOutExec();
    virtual void nextExec();
    virtual void stepIExec();
    virtual void nextIExec();
con's avatar
con committed
279

280 281 282 283
    void continueInferiorInternal();
    void autoContinueInferior();
    virtual void continueInferior();
    virtual void interruptInferior();
con's avatar
con committed
284

285 286 287 288
    virtual void runToLineExec(const QString &fileName, int lineNumber);
    virtual void runToFunctionExec(const QString &functionName);
//    void handleExecRunToFunction(const GdbResponse &response);
    virtual void jumpToLineExec(const QString &fileName, int lineNumber);
con's avatar
con committed
289

290
    void handleExecContinue(const GdbResponse &response);
con's avatar
con committed
291

292 293 294
    qint64 inferiorPid() const { return m_manager->inferiorPid(); }
    void handleInferiorPidChanged(qint64 pid) { manager()->notifyInferiorPidChanged(pid); }
    void maybeHandleInferiorPidChanged(const QString &pid);
con's avatar
con committed
295

296 297 298 299 300 301
private: ////////// View & Data Stuff //////////

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

    void gotoLocation(const StackFrame &frame, bool setLocationMarker);
con's avatar
con committed
302 303 304 305

    //
    // Breakpoint specific stuff
    //
hjk's avatar
hjk committed
306
    void handleBreakList(const GdbResponse &response);
con's avatar
con committed
307
    void handleBreakList(const GdbMi &table);
hjk's avatar
hjk committed
308 309 310 311 312
    void handleBreakIgnore(const GdbResponse &response);
    void handleBreakInsert(const GdbResponse &response);
    void handleBreakInsert1(const GdbResponse &response);
    void handleBreakCondition(const GdbResponse &response);
    void handleBreakInfo(const GdbResponse &response);
con's avatar
con committed
313 314 315
    void extractDataFromInfoBreak(const QString &output, BreakpointData *data);
    void breakpointDataFromOutput(BreakpointData *data, const GdbMi &bkpt);
    void sendInsertBreakpoint(int index);
316
    QString breakLocation(const QString &file) const;
con's avatar
con committed
317 318 319 320

    //
    // Modules specific stuff
    //
321 322 323
    virtual void loadSymbols(const QString &moduleName);
    virtual void loadAllSymbols();
    virtual QList<Symbol> moduleSymbols(const QString &moduleName);
con's avatar
con committed
324
    void reloadModules();
hjk's avatar
hjk committed
325
    void handleModulesList(const GdbResponse &response);
con's avatar
con committed
326

327
    bool m_modulesListOutdated;
con's avatar
con committed
328 329 330

    //
    // Register specific stuff
hjk's avatar
hjk committed
331
    //
332
    Q_SLOT void reloadRegisters();
333
    void setRegisterValue(int nr, const QString &value);
hjk's avatar
hjk committed
334 335
    void handleRegisterListNames(const GdbResponse &response);
    void handleRegisterListValues(const GdbResponse &response);
con's avatar
con committed
336

337 338 339 340 341 342 343 344 345 346 347 348
    //
    // Disassembler specific stuff
    //
    virtual void fetchDisassembler(DisassemblerViewAgent *agent,
        const StackFrame &frame);
    void fetchDisassemblerByAddress(DisassemblerViewAgent *agent,
        bool useMixedMode);
    void handleFetchDisassemblerByLine(const GdbResponse &response);
    void handleFetchDisassemblerByAddress1(const GdbResponse &response);
    void handleFetchDisassemblerByAddress0(const GdbResponse &response);
    QString parseDisassembler(const GdbMi &lines);

349 350
    //
    // Source file specific stuff
hjk's avatar
hjk committed
351
    //
352
    void reloadSourceFiles();
353 354 355 356 357 358 359
    void handleQuerySources(const GdbResponse &response);

    QString fullName(const QString &fileName);

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

361 362 363
    bool m_sourcesListOutdated;
    bool m_sourcesListUpdating;

con's avatar
con committed
364 365
    //
    // Stack specific stuff
hjk's avatar
hjk committed
366
    //
367
    void updateAll();
hjk's avatar
hjk committed
368 369 370
    void handleStackListFrames(const GdbResponse &response);
    void handleStackSelectThread(const GdbResponse &response);
    void handleStackListThreads(const GdbResponse &response);
hjk's avatar
hjk committed
371 372
    void handleStackFrame1(const GdbResponse &response);
    void handleStackFrame2(const GdbResponse &response);
hjk's avatar
hjk committed
373
    Q_SLOT void reloadStack(bool forceGotoLocation);
374
    Q_SLOT void reloadFullStack();
375
    int currentFrame() const;
con's avatar
con committed
376

377 378 379
    QList<GdbMi> m_currentFunctionArgs;
    QByteArray m_firstChunk;
    QString m_currentFrame;
con's avatar
con committed
380 381

    //
382
    // Watch specific stuff
hjk's avatar
hjk committed
383
    //
384
    virtual void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
con's avatar
con committed
385

386 387 388 389 390 391 392
    virtual void assignValueInDebugger(const QString &expr, const QString &value);

    virtual void fetchMemory(MemoryViewAgent *agent, quint64 addr, quint64 length);
    void handleFetchMemory(const GdbResponse &response);

    virtual void watchPoint(const QPoint &);
    void handleWatchPoint(const GdbResponse &response);
con's avatar
con committed
393 394 395

    // FIXME: BaseClass. called to improve situation for a watch item
    void updateSubItem(const WatchData &data);
396 397
    void handleChildren(const WatchData &parent, const GdbMi &child,
        QList<WatchData> *insertions);
con's avatar
con committed
398

399
    void updateWatchData(const WatchData &data);
400
    Q_SLOT void updateWatchDataHelper(const WatchData &data);
hjk's avatar
hjk committed
401
    void rebuildModel();
402
    bool showToolTip();
con's avatar
con committed
403 404 405 406 407

    void insertData(const WatchData &data);
    void sendWatchParameters(const QByteArray &params0);
    void createGdbVariable(const WatchData &data);

408 409 410
    void runDebuggingHelper(const WatchData &data, bool dumpChildren);
    void runDirectDebuggingHelper(const WatchData &data, bool dumpChildren);
    bool hasDebuggingHelperForType(const QString &type) const;
con's avatar
con committed
411

hjk's avatar
hjk committed
412
    void handleVarListChildren(const GdbResponse &response);
413 414
    void handleVarListChildrenHelper(const GdbMi &child,
        const WatchData &parent);
hjk's avatar
hjk committed
415 416 417 418 419 420 421 422 423 424
    void handleVarCreate(const GdbResponse &response);
    void handleVarAssign(const GdbResponse &response);
    void handleEvaluateExpression(const GdbResponse &response);
    //void handleToolTip(const GdbResponse &response);
    void handleQueryDebuggingHelper(const GdbResponse &response);
    void handleDebuggingHelperValue1(const GdbResponse &response);
    void handleDebuggingHelperValue2(const GdbResponse &response);
    void handleDebuggingHelperValue3(const GdbResponse &response);
    void handleDebuggingHelperEditValue(const GdbResponse &response);
    void handleDebuggingHelperSetup(const GdbResponse &response);
425 426

    void updateLocals(const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
427
    void handleStackListLocals(const GdbResponse &response);
428 429 430 431
    WatchData localVariable(const GdbMi &item,
                            const QStringList &uninitializedVariables,
                            QMap<QByteArray, int> *seen);
    void setLocals(const QList<GdbMi> &locals);
hjk's avatar
hjk committed
432
    void handleStackListArguments(const GdbResponse &response);
con's avatar
con committed
433
    void setWatchDataType(WatchData &data, const GdbMi &mi);
434
    void setWatchDataDisplayedType(WatchData &data, const GdbMi &mi);
con's avatar
con committed
435

hjk's avatar
hjk committed
436
    QSet<QString> m_processedNames;
con's avatar
con committed
437 438
    QMap<QString, QString> m_varToType;

439
private: ////////// Dumper Management //////////
hjk's avatar
hjk committed
440

441 442 443 444 445 446 447 448
    bool startModeAllowsDumpers() const;
    void tryLoadDebuggingHelpers();
    void tryQueryDebuggingHelpers();
    Q_SLOT void recheckDebuggingHelperAvailability();
    void connectDebuggingHelperActions();
    void disconnectDebuggingHelperActions();
    Q_SLOT void setDebugDebuggingHelpers(const QVariant &on);
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
449

450 451 452
    const bool m_dumperInjectionLoad;
    DebuggingHelperState m_debuggingHelperState;
    QtDumperHelper m_dumperHelper;
453

454
private: ////////// Convenience Functions //////////
hjk's avatar
hjk committed
455

456
    QString errorMessage(QProcess::ProcessError error);
457
    void showMessageBox(int icon, const QString &title, const QString &text);
458
    void debugMessage(const QString &msg);
459 460
    QMainWindow *mainWindow() const;

con's avatar
con committed
461 462 463 464 465
};

} // namespace Internal
} // namespace Debugger

466 467
Q_DECLARE_OPERATORS_FOR_FLAGS(Debugger::Internal::GdbEngine::GdbCommandFlags)

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