gdbengine.h 15.8 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
};

hjk's avatar
hjk committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
enum GdbAdapterState
{
    AdapterNotRunning,
    AdapterStarting,
    AdapterStarted,
    AdapterStartFailed,
    InferiorPreparing,
    InferiorPrepared,
    InferiorPreparationFailed,
    InferiorStarting,
    InferiorStarted,
    InferiorStartFailed,
    InferiorShuttingDown,
    InferiorShutDown,
    InferiorShutdownFailed,
    AdapterShuttingDown,
    //AdapterShutDown,  // use AdapterNotRunning
    AdapterShutdownFailed,
};

con's avatar
con committed
102 103 104 105 106
class GdbEngine : public IDebuggerEngine
{
    Q_OBJECT

public:
hjk's avatar
hjk committed
107
    explicit GdbEngine(DebuggerManager *parent);
con's avatar
con committed
108 109 110
    ~GdbEngine();

signals:
111 112
    void gdbInputAvailable(int channel, const QString &msg);
    void gdbOutputAvailable(int channel, const QString &msg);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
113
    void applicationOutputAvailable(const QString &output);
con's avatar
con committed
114 115

private:
116 117
    friend class AttachGdbAdapter;
    friend class CoreGdbAdapter;
hjk's avatar
hjk committed
118
    friend class PlainGdbAdapter;
119
    friend class RemoteGdbAdapter;
120
    friend class TrkGdbAdapter;
hjk's avatar
hjk committed
121

con's avatar
con committed
122 123 124 125 126 127 128 129 130 131
    //
    // IDebuggerEngine implementation
    //
    void stepExec();
    void stepOutExec();
    void nextExec();
    void stepIExec();
    void nextIExec();

    void shutdown();
132
    void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
hjk's avatar
hjk committed
133
    void startDebugger(const DebuggerStartParametersPtr &sp);
con's avatar
con committed
134
    void exitDebugger();
135
    void detachDebugger();
con's avatar
con committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    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);
151
    void watchPoint(const QPoint &);
con's avatar
con committed
152 153 154

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

157
    void fetchMemory(MemoryViewAgent *agent, quint64 addr, quint64 length);
hjk's avatar
hjk committed
158
    void handleFetchMemory(const GdbResponse &response);
159

160 161 162 163
    void fetchDisassembler(DisassemblerViewAgent *agent,
        const StackFrame &frame);
    void fetchDisassemblerByAddress(DisassemblerViewAgent *agent,
        bool useMixedMode);
hjk's avatar
hjk committed
164 165 166
    void handleFetchDisassemblerByLine(const GdbResponse &response);
    void handleFetchDisassemblerByAddress1(const GdbResponse &response);
    void handleFetchDisassemblerByAddress0(const GdbResponse &response);
167

168 169
    Q_SLOT void setDebugDebuggingHelpers(const QVariant &on);
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
170

con's avatar
con committed
171 172 173
    //
    // Own stuff
    //
174

con's avatar
con committed
175 176 177
    int currentFrame() const;

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

180 181
    void connectAdapter();
    void disconnectAdapter();
182
    void initializeVariables();
con's avatar
con committed
183 184 185 186
    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
187
    void handleResult(const GdbResponse &response);
con's avatar
con committed
188

189 190 191 192 193 194
public: // otherwise the Qt flag macros are unhappy
    enum GdbCommandFlag {
        NoFlags = 0,
        NeedsStop = 1,
        Discardable = 2,
        RebuildModel = 4,
195
        WatchUpdate = Discardable | RebuildModel,
196
        EmbedToken = 8
197 198 199
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)

hjk's avatar
hjk committed
200

201
private:
hjk's avatar
hjk committed
202
    typedef void (GdbEngine::*GdbCommandCallback)
hjk's avatar
hjk committed
203
        (const GdbResponse &response);
hjk's avatar
hjk committed
204
    typedef void (AbstractGdbAdapter::*AdapterCallback)
hjk's avatar
hjk committed
205
        (const GdbResponse &response);
206 207 208

    struct GdbCommand
    {
hjk's avatar
hjk committed
209 210 211
        GdbCommand()
            : flags(0), callback(0), adapterCallback(0), callbackName(0)
        {}
212 213 214

        int flags;
        GdbCommandCallback callback;
hjk's avatar
hjk committed
215
        AdapterCallback adapterCallback;
216 217 218
        const char *callbackName;
        QString command;
        QVariant cookie;
219
        QTime postTime;
220 221
    };

con's avatar
con committed
222 223
    // type and cookie are sender-internal data, opaque for the "event
    // queue". resultNeeded == true increments m_pendingResults on
hjk's avatar
hjk committed
224
    // send and decrements on receipt, effectively preventing
con's avatar
con committed
225
    // watch model updates before everything is finished.
hjk's avatar
hjk committed
226
    void flushCommand(const GdbCommand &cmd);
227
    void postCommand(const QString &command,
228 229 230 231
                     GdbCommandFlags flags,
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
232
    void postCommand(const QString &command,
233 234 235
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
236 237 238 239
    void postCommand(const QString &command,
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
240 241 242 243 244
    void postCommand(const QString &command,
                     GdbCommandFlags flags,
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
245
    void postCommandHelper(const GdbCommand &cmd);
con's avatar
con committed
246 247
    void setTokenBarrier();

248
    void updateAll();
con's avatar
con committed
249 250 251 252 253 254
    void updateLocals();

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

hjk's avatar
hjk committed
257 258 259 260 261 262 263 264 265 266
    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);

267
    void handleAdapterCrashed();
hjk's avatar
hjk committed
268 269 270
    void handleAdapterShutDown();
    void handleAdapterShutdownFailed(const QString &msg);

con's avatar
con committed
271 272
private:
    int terminationIndex(const QByteArray &buffer, int &length);
273
    void handleResponse(const QByteArray &buff);
hjk's avatar
hjk committed
274
    void handleStart(const GdbResponse &response);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
275
    void handleAqcuiredInferior();
con's avatar
con committed
276
    void handleAsyncOutput(const GdbMi &data);
hjk's avatar
hjk committed
277 278
    void handleStop1(const GdbResponse &response);
    void handleStop2(const GdbResponse &response);
hjk's avatar
hjk committed
279
    void handleStop2(const GdbMi &data);
hjk's avatar
hjk committed
280 281 282 283 284 285 286 287 288 289 290 291
    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);
292
    bool showToolTip();
con's avatar
con committed
293

hjk's avatar
hjk committed
294 295 296 297 298 299
    // 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(); }
300
    DebuggerStartMode startMode() const;
hjk's avatar
hjk committed
301 302
    qint64 inferiorPid() const { return m_manager->inferiorPid(); }

303 304
    void handleChildren(const WatchData &parent, const GdbMi &child,
        QList<WatchData> *insertions);
305 306
    const bool m_dumperInjectionLoad;

307 308 309
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

con's avatar
con committed
310 311
    QByteArray m_inbuffer;

312
    QHash<int, GdbCommand> m_cookieForToken;
con's avatar
con committed
313 314 315 316 317 318 319 320 321 322 323 324
    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
325
    int m_gdbBuildVersion; // MAC only?
con's avatar
con committed
326 327

    // awful hack to keep track of used files
328 329
    QMap<QString, QString> m_shortToFullName;
    QMap<QString, QString> m_fullToShortName;
con's avatar
con committed
330 331 332 333

    //
    // Breakpoint specific stuff
    //
hjk's avatar
hjk committed
334
    void handleBreakList(const GdbResponse &response);
con's avatar
con committed
335
    void handleBreakList(const GdbMi &table);
hjk's avatar
hjk committed
336 337 338 339 340
    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
341 342 343 344 345 346 347 348
    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
349
    void handleModulesList(const GdbResponse &response);
con's avatar
con committed
350 351 352 353


    //
    // Register specific stuff
hjk's avatar
hjk committed
354
    //
355
    Q_SLOT void reloadRegisters();
356
    void setRegisterValue(int nr, const QString &value);
hjk's avatar
hjk committed
357 358
    void handleRegisterListNames(const GdbResponse &response);
    void handleRegisterListValues(const GdbResponse &response);
con's avatar
con committed
359

360 361
    //
    // Source file specific stuff
hjk's avatar
hjk committed
362
    //
363
    void reloadSourceFiles();
con's avatar
con committed
364 365 366

    //
    // Stack specific stuff
hjk's avatar
hjk committed
367
    //
hjk's avatar
hjk committed
368 369 370
    void handleStackListFrames(const GdbResponse &response);
    void handleStackSelectThread(const GdbResponse &response);
    void handleStackListThreads(const GdbResponse &response);
371 372
    Q_SLOT void reloadStack();
    Q_SLOT void reloadFullStack();
con's avatar
con committed
373 374 375 376


    //
    // Tooltip specific stuff
hjk's avatar
hjk committed
377
    //
con's avatar
con committed
378 379 380 381 382 383 384 385 386
    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);

387
    void updateWatchData(const WatchData &data);
388
    Q_SLOT void updateWatchDataHelper(const WatchData &data);
hjk's avatar
hjk committed
389
    void rebuildModel();
con's avatar
con committed
390 391 392 393 394 395 396

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

    void maybeHandleInferiorPidChanged(const QString &pid);

397
    void tryLoadDebuggingHelpers();
398
    void tryQueryDebuggingHelpers();
399 400 401 402
    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
403

hjk's avatar
hjk committed
404 405 406 407 408 409 410 411 412 413 414 415 416
    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
417 418 419
    void handleVarListChildrenHelper(const GdbMi &child,
        const WatchData &parent);
    void setWatchDataType(WatchData &data, const GdbMi &mi);
420
    void setWatchDataDisplayedType(WatchData &data, const GdbMi &mi);
hjk's avatar
hjk committed
421
    void setLocals(const QList<GdbMi> &locals);
422 423
    void connectDebuggingHelperActions();
    void disconnectDebuggingHelperActions();
hjk's avatar
hjk committed
424

425
    bool startModeAllowsDumpers() const;
426
    QString parseDisassembler(const GdbMi &lines);
con's avatar
con committed
427 428

    int m_pendingRequests;
hjk's avatar
hjk committed
429
    QSet<QString> m_processedNames;
con's avatar
con committed
430

431
    QtDumperHelper m_dumperHelper;
hjk's avatar
hjk committed
432

433
    DebuggingHelperState m_debuggingHelperState;
con's avatar
con committed
434 435 436 437
    QList<GdbMi> m_currentFunctionArgs;
    QString m_currentFrame;
    QMap<QString, QString> m_varToType;

hjk's avatar
hjk committed
438 439 440 441 442
    typedef void (GdbEngine::*Continuation)();
    // function called after all previous responses have been received
    Continuation m_continuationAfterDone;
    void handleInitialBreakpointsSet();

443
    bool m_modulesListOutdated;
444

445
    QList<GdbCommand> m_commandsToRunOnTemporaryBreak;
hjk's avatar
hjk committed
446

hjk's avatar
hjk committed
447
    DebuggerManager * const m_manager;
448
    IDebuggerManagerAccessForEngines * const qq;
hjk's avatar
hjk committed
449
    DebuggerStartParametersPtr m_startParameters;
450
    // make sure to re-initialize new members in initializeVariables();
451

452 453
    // only one of those is active at a given time, available in m_gdbAdapter
    AbstractGdbAdapter *m_gdbAdapter;  // pointer to one listed below
454 455
    AttachGdbAdapter *m_attachAdapter; // owned
    CoreGdbAdapter *m_coreAdapter;     // owned
456 457
    PlainGdbAdapter *m_plainAdapter;   // owned
    RemoteGdbAdapter *m_remoteAdapter; // owned
458
    TrkGdbAdapter *m_trkAdapter;       // owned
hjk's avatar
hjk committed
459 460 461 462 463 464 465

    // State
    friend class AbstractGdbAdapter;
    GdbAdapterState m_state;
    void setState(GdbAdapterState state);
    GdbAdapterState state() const;

hjk's avatar
hjk committed
466
public:
467
    void showMessageBox(int icon, const QString &title, const QString &text);
468
    void debugMessage(const QString &msg);
469
    void addOptionPages(QList<Core::IOptionsPage*> *opts) const;
470 471
    const DebuggerStartParameters &startParameters() const
        { return *m_startParameters; }
472

hjk's avatar
hjk committed
473
    OutputCollector m_outputCollector;
con's avatar
con committed
474 475 476 477 478
};

} // namespace Internal
} // namespace Debugger

479 480
Q_DECLARE_OPERATORS_FOR_FLAGS(Debugger::Internal::GdbEngine::GdbCommandFlags)

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