gdbengine.h 15.3 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"
hjk's avatar
hjk committed
37
#include "outputcollector.h"
38

con's avatar
con committed
39 40 41 42 43 44
#include <QtCore/QByteArray>
#include <QtCore/QHash>
#include <QtCore/QMap>
#include <QtCore/QObject>
#include <QtCore/QProcess>
#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 49 50 51 52 53 54 55 56 57 58
#include <QtCore/QVariant>

QT_BEGIN_NAMESPACE
class QAction;
class QAbstractItemModel;
class QWidget;
QT_END_NAMESPACE

namespace Debugger {
namespace Internal {

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

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

68 69
class AttachGdbAdapter;
class CoreGdbAdapter;
70 71
class PlainGdbAdapter;
class RemoteGdbAdapter;
72
class TrkGdbAdapter;
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 *parent);
con's avatar
con committed
88 89 90
    ~GdbEngine();

signals:
91 92
    void gdbInputAvailable(int channel, const QString &msg);
    void gdbOutputAvailable(int channel, const QString &msg);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
93
    void applicationOutputAvailable(const QString &output);
con's avatar
con committed
94 95

private:
96 97
    friend class AttachGdbAdapter;
    friend class CoreGdbAdapter;
hjk's avatar
hjk committed
98
    friend class PlainGdbAdapter;
99
    friend class RemoteGdbAdapter;
100
    friend class TrkGdbAdapter;
hjk's avatar
hjk committed
101

con's avatar
con committed
102 103 104 105 106 107 108 109 110 111
    //
    // IDebuggerEngine implementation
    //
    void stepExec();
    void stepOutExec();
    void nextExec();
    void stepIExec();
    void nextIExec();

    void shutdown();
112
    void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
hjk's avatar
hjk committed
113
    void startDebugger(const DebuggerStartParametersPtr &sp);
con's avatar
con committed
114
    void exitDebugger();
115
    void detachDebugger();
con's avatar
con committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    void continueInferior();
    void interruptInferior();

    void runToLineExec(const QString &fileName, int lineNumber);
    void runToFunctionExec(const QString &functionName);
    void jumpToLineExec(const QString &fileName, int lineNumber);

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

    Q_SLOT void attemptBreakpointSynchronization();

    void assignValueInDebugger(const QString &expr, const QString &value);
    void executeDebuggerCommand(const QString & command);
131
    void watchPoint(const QPoint &);
con's avatar
con committed
132 133 134

    void loadSymbols(const QString &moduleName);
    void loadAllSymbols();
135
    virtual QList<Symbol> moduleSymbols(const QString &moduleName);
con's avatar
con committed
136

137
    void fetchMemory(MemoryViewAgent *agent, quint64 addr, quint64 length);
hjk's avatar
hjk committed
138
    void handleFetchMemory(const GdbResponse &response);
139

140 141 142 143
    void fetchDisassembler(DisassemblerViewAgent *agent,
        const StackFrame &frame);
    void fetchDisassemblerByAddress(DisassemblerViewAgent *agent,
        bool useMixedMode);
hjk's avatar
hjk committed
144 145 146
    void handleFetchDisassemblerByLine(const GdbResponse &response);
    void handleFetchDisassemblerByAddress1(const GdbResponse &response);
    void handleFetchDisassemblerByAddress0(const GdbResponse &response);
147

148 149
    Q_SLOT void setDebugDebuggingHelpers(const QVariant &on);
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
150

con's avatar
con committed
151 152 153
    //
    // Own stuff
    //
154

con's avatar
con committed
155 156 157
    int currentFrame() const;

    bool supportsThreads() const;
hjk's avatar
hjk committed
158
    void gotoLocation(const StackFrame &frame, bool setLocationMarker);
con's avatar
con committed
159

160 161
    void connectAdapter();
    void disconnectAdapter();
162
    void initializeVariables();
con's avatar
con committed
163 164 165 166
    QString fullName(const QString &fileName);
    // get one usable name out of these, try full names first
    QString fullName(const QStringList &candidates);

hjk's avatar
hjk committed
167
    void handleResult(const GdbResponse &response);
con's avatar
con committed
168

169 170 171 172 173 174
public: // otherwise the Qt flag macros are unhappy
    enum GdbCommandFlag {
        NoFlags = 0,
        NeedsStop = 1,
        Discardable = 2,
        RebuildModel = 4,
175
        WatchUpdate = Discardable | RebuildModel,
176
        EmbedToken = 8
177 178 179
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)

hjk's avatar
hjk committed
180

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

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

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

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

228
    void updateAll();
con's avatar
con committed
229 230 231 232 233 234
    void updateLocals();

private slots:
    void gdbProcError(QProcess::ProcessError error);
    void readGdbStandardOutput();
    void readGdbStandardError();
235
    void readDebugeeOutput(const QByteArray &data);
con's avatar
con committed
236

hjk's avatar
hjk committed
237 238 239 240 241 242 243 244 245 246
    void handleAdapterStarted();
    void handleAdapterStartFailed(const QString &msg);

    void handleInferiorPrepared();
    void handleInferiorPreparationFailed(const QString &msg);
    void handleInferiorStarted();
    void handleInferiorStartFailed(const QString &msg);
    void handleInferiorShutDown();
    void handleInferiorShutdownFailed(const QString &msg);

247
    void handleAdapterCrashed();
hjk's avatar
hjk committed
248 249 250
    void handleAdapterShutDown();
    void handleAdapterShutdownFailed(const QString &msg);

con's avatar
con committed
251 252
private:
    int terminationIndex(const QByteArray &buffer, int &length);
253
    void handleResponse(const QByteArray &buff);
hjk's avatar
hjk committed
254
    void handleStart(const GdbResponse &response);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
255
    void handleAqcuiredInferior();
con's avatar
con committed
256
    void handleAsyncOutput(const GdbMi &data);
hjk's avatar
hjk committed
257 258
    void handleStop1(const GdbResponse &response);
    void handleStop2(const GdbResponse &response);
hjk's avatar
hjk committed
259
    void handleStop2(const GdbMi &data);
hjk's avatar
hjk committed
260 261 262 263 264 265 266 267 268 269 270 271
    void handleResultRecord(const GdbResponse &response);
    void handleFileExecAndSymbols(const GdbResponse &response);
    void handleExecContinue(const GdbResponse &response);
    void handleExecJumpToLine(const GdbResponse &response);
    void handleExecRunToFunction(const GdbResponse &response);
    void handleInfoShared(const GdbResponse &response);
    void handleInfoProc(const GdbResponse &response);
    void handleInfoThreads(const GdbResponse &response);
    void handleShowVersion(const GdbResponse &response);
    void handleQueryPwd(const GdbResponse &response);
    void handleQuerySources(const GdbResponse &response);
    void handleWatchPoint(const GdbResponse &response);
272
    bool showToolTip();
con's avatar
con committed
273

hjk's avatar
hjk committed
274 275 276 277 278 279
    // Convenience
    DebuggerManager *manager() { return m_manager; }
    void showStatusMessage(const QString &msg, int timeout = -1)
        { m_manager->showStatusMessage(msg, timeout); }
    int status() const { return m_manager->status(); }
    QMainWindow *mainWindow() const { return m_manager->mainWindow(); }
280
    DebuggerStartMode startMode() const;
hjk's avatar
hjk committed
281 282
    qint64 inferiorPid() const { return m_manager->inferiorPid(); }

283 284
    void handleChildren(const WatchData &parent, const GdbMi &child,
        QList<WatchData> *insertions);
285 286
    const bool m_dumperInjectionLoad;

287 288 289
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

con's avatar
con committed
290 291
    QByteArray m_inbuffer;

292
    QHash<int, GdbCommand> m_cookieForToken;
con's avatar
con committed
293 294 295 296 297 298 299 300 301 302 303 304
    QHash<int, QByteArray> m_customOutputForToken;

    QByteArray m_pendingConsoleStreamOutput;
    QByteArray m_pendingTargetStreamOutput;
    QByteArray m_pendingLogStreamOutput;

    // 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;

    int m_gdbVersion; // 6.8.0 is 680
hjk's avatar
hjk committed
305
    int m_gdbBuildVersion; // MAC only? 
con's avatar
con committed
306 307

    // awful hack to keep track of used files
308 309
    QMap<QString, QString> m_shortToFullName;
    QMap<QString, QString> m_fullToShortName;
con's avatar
con committed
310 311 312 313

    //
    // Breakpoint specific stuff
    //
hjk's avatar
hjk committed
314
    void handleBreakList(const GdbResponse &response);
con's avatar
con committed
315
    void handleBreakList(const GdbMi &table);
hjk's avatar
hjk committed
316 317 318 319 320
    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
321 322 323 324 325 326 327 328
    void extractDataFromInfoBreak(const QString &output, BreakpointData *data);
    void breakpointDataFromOutput(BreakpointData *data, const GdbMi &bkpt);
    void sendInsertBreakpoint(int index);

    //
    // Modules specific stuff
    //
    void reloadModules();
hjk's avatar
hjk committed
329
    void handleModulesList(const GdbResponse &response);
con's avatar
con committed
330 331 332 333 334


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

340 341 342 343
    //
    // Source file specific stuff
    // 
    void reloadSourceFiles();
con's avatar
con committed
344 345 346 347

    //
    // Stack specific stuff
    // 
hjk's avatar
hjk committed
348 349 350
    void handleStackListFrames(const GdbResponse &response);
    void handleStackSelectThread(const GdbResponse &response);
    void handleStackListThreads(const GdbResponse &response);
351 352
    Q_SLOT void reloadStack();
    Q_SLOT void reloadFullStack();
con's avatar
con committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366


    //
    // Tooltip specific stuff
    // 
    void sendToolTipCommand(const QString &command, const QString &cookie);


    //
    // Watch specific stuff
    //
    // FIXME: BaseClass. called to improve situation for a watch item
    void updateSubItem(const WatchData &data);

367
    void updateWatchData(const WatchData &data);
368
    Q_SLOT void updateWatchDataHelper(const WatchData &data);
hjk's avatar
hjk committed
369
    void rebuildModel();
con's avatar
con committed
370 371 372 373 374 375 376

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

    void maybeHandleInferiorPidChanged(const QString &pid);

377
    void tryLoadDebuggingHelpers();
378
    void tryQueryDebuggingHelpers();
379 380 381 382
    Q_SLOT void recheckDebuggingHelperAvailability();
    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
383

hjk's avatar
hjk committed
384 385 386 387 388 389 390 391 392 393 394 395 396
    void handleVarListChildren(const GdbResponse &response);
    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);
    void handleStackListLocals(const GdbResponse &response);
    void handleStackListArguments(const GdbResponse &response);
con's avatar
con committed
397 398 399
    void handleVarListChildrenHelper(const GdbMi &child,
        const WatchData &parent);
    void setWatchDataType(WatchData &data, const GdbMi &mi);
400
    void setWatchDataDisplayedType(WatchData &data, const GdbMi &mi);
hjk's avatar
hjk committed
401
    void setLocals(const QList<GdbMi> &locals);
402 403
    void connectDebuggingHelperActions();
    void disconnectDebuggingHelperActions();
404 405
   
    bool startModeAllowsDumpers() const;
406
    QString parseDisassembler(const GdbMi &lines);
con's avatar
con committed
407 408

    int m_pendingRequests;
409
    QSet<QString> m_processedNames; 
con's avatar
con committed
410

411
    QtDumperHelper m_dumperHelper;
con's avatar
con committed
412
    
413
    DebuggingHelperState m_debuggingHelperState;
con's avatar
con committed
414 415 416 417
    QList<GdbMi> m_currentFunctionArgs;
    QString m_currentFrame;
    QMap<QString, QString> m_varToType;

hjk's avatar
hjk committed
418 419 420 421 422
    typedef void (GdbEngine::*Continuation)();
    // function called after all previous responses have been received
    Continuation m_continuationAfterDone;
    void handleInitialBreakpointsSet();

423
    bool m_modulesListOutdated;
424

425
    QList<GdbCommand> m_commandsToRunOnTemporaryBreak;
hjk's avatar
hjk committed
426

hjk's avatar
hjk committed
427
    DebuggerManager * const m_manager;
428
    IDebuggerManagerAccessForEngines * const qq;
hjk's avatar
hjk committed
429
    DebuggerStartParametersPtr m_startParameters;
430
    // make sure to re-initialize new members in initializeVariables();
431

432 433
    // only one of those is active at a given time, available in m_gdbAdapter
    AbstractGdbAdapter *m_gdbAdapter;  // pointer to one listed below
434 435
    AttachGdbAdapter *m_attachAdapter; // owned
    CoreGdbAdapter *m_coreAdapter;     // owned
436 437
    PlainGdbAdapter *m_plainAdapter;   // owned
    RemoteGdbAdapter *m_remoteAdapter; // owned
438
    TrkGdbAdapter *m_trkAdapter;       // owned
439
    
hjk's avatar
hjk committed
440
public:
441
    void showMessageBox(int icon, const QString &title, const QString &text);
442
    void debugMessage(const QString &msg);
443
    void addOptionPages(QList<Core::IOptionsPage*> *opts) const;
444 445
    const DebuggerStartParameters &startParameters() const
        { return *m_startParameters; }
446

hjk's avatar
hjk committed
447
    OutputCollector m_outputCollector;
con's avatar
con committed
448 449 450 451 452
};

} // namespace Internal
} // namespace Debugger

453 454
Q_DECLARE_OPERATORS_FOR_FLAGS(Debugger::Internal::GdbEngine::GdbCommandFlags)

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