gdbengine.h 16.1 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 62 63 64 65 66 67
class DebuggerManager;
class IDebuggerManagerAccessForEngines;
class GdbResultRecord;
class GdbMi;

class WatchData;
class BreakpointData;

68
enum DebuggingHelperState
con's avatar
con committed
69
{
70 71 72 73
    DebuggingHelperUninitialized,
    DebuggingHelperLoadTried,
    DebuggingHelperAvailable,
    DebuggingHelperUnavailable,
con's avatar
con committed
74 75 76 77 78 79 80
};

class GdbEngine : public IDebuggerEngine
{
    Q_OBJECT

public:
hjk's avatar
hjk committed
81
    explicit GdbEngine(DebuggerManager *parent);
con's avatar
con committed
82
    ~GdbEngine();
hjk's avatar
hjk committed
83
    void setGdbAdapter(AbstractGdbAdapter *adapter);
con's avatar
con committed
84 85

signals:
86 87
    void gdbInputAvailable(int channel, const QString &msg);
    void gdbOutputAvailable(int channel, const QString &msg);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
88
    void applicationOutputAvailable(const QString &output);
con's avatar
con committed
89 90

private:
hjk's avatar
hjk committed
91 92
    friend class PlainGdbAdapter;
    friend class TrkGdbAdapter;
hjk's avatar
hjk committed
93 94

    const DebuggerStartParameters &startParameters() const
hjk's avatar
hjk committed
95
        { return *m_startParameters; }
con's avatar
con committed
96 97 98 99 100 101 102 103 104 105
    //
    // IDebuggerEngine implementation
    //
    void stepExec();
    void stepOutExec();
    void nextExec();
    void stepIExec();
    void nextIExec();

    void shutdown();
106
    void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
hjk's avatar
hjk committed
107
    void startDebugger(const DebuggerStartParametersPtr &sp);
con's avatar
con committed
108
    void exitDebugger();
hjk's avatar
hjk committed
109
    void exitDebugger2();
110
    void detachDebugger();
con's avatar
con committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    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);
126
    void watchPoint(const QPoint &);
con's avatar
con committed
127 128 129

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

132 133 134
    void fetchMemory(MemoryViewAgent *agent, quint64 addr, quint64 length);
    void handleFetchMemory(const GdbResultRecord &record, const QVariant &cookie);

135 136 137 138 139 140 141 142 143 144 145
    void fetchDisassembler(DisassemblerViewAgent *agent,
        const StackFrame &frame);
    void fetchDisassemblerByAddress(DisassemblerViewAgent *agent,
        bool useMixedMode);
    void handleFetchDisassemblerByLine(const GdbResultRecord &record,
        const QVariant &cookie);
    void handleFetchDisassemblerByAddress1(const GdbResultRecord &record,
        const QVariant &cookie);
    void handleFetchDisassemblerByAddress0(const GdbResultRecord &record,
        const QVariant &cookie);

146 147
    Q_SLOT void setDebugDebuggingHelpers(const QVariant &on);
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
148

con's avatar
con committed
149 150 151
    //
    // Own stuff
    //
152

con's avatar
con committed
153 154 155
    int currentFrame() const;

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

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

    void handleResult(const GdbResultRecord &, int type, const QVariant &);

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

hjk's avatar
hjk committed
177

178
private:
hjk's avatar
hjk committed
179 180 181 182
    typedef void (GdbEngine::*GdbCommandCallback)
        (const GdbResultRecord &record, const QVariant &cookie);
    typedef void (AbstractGdbAdapter::*AdapterCallback)
        (const GdbResultRecord &record, const QVariant &cookie);
183 184 185

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

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

con's avatar
con committed
199 200 201 202
    // 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
203
    void flushCommand(const GdbCommand &cmd);
204
    void postCommand(const QString &command,
205 206 207 208
                     GdbCommandFlags flags,
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
209
    void postCommand(const QString &command,
210 211 212
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
hjk's avatar
hjk committed
213 214 215 216 217
    void postCommand(const QString &command,
                     AdapterCallback callback,
                     const char *callbackName,
                     const QVariant &cookie = QVariant());
    void postCommandHelper(const GdbCommand &cmd);
con's avatar
con committed
218 219 220 221 222 223 224 225
    void setTokenBarrier();

    void updateLocals();

private slots:
    void gdbProcError(QProcess::ProcessError error);
    void readGdbStandardOutput();
    void readGdbStandardError();
226 227
    void readUploadStandardOutput();
    void readUploadStandardError();
228
    void readDebugeeOutput(const QByteArray &data);
229
    void uploadProcError(QProcess::ProcessError error);
230
    void emitStartFailed();
con's avatar
con committed
231

hjk's avatar
hjk committed
232 233 234 235 236 237 238 239 240 241 242 243 244
    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);

    void handleAdapterShutDown();
    void handleAdapterShutdownFailed(const QString &msg);

con's avatar
con committed
245 246
private:
    int terminationIndex(const QByteArray &buffer, int &length);
247
    void handleResponse(const QByteArray &buff);
248 249
    void handleStart(const GdbResultRecord &response, const QVariant &);
    void handleAttach(const GdbResultRecord &, const QVariant &);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
250
    void handleAqcuiredInferior();
con's avatar
con committed
251
    void handleAsyncOutput(const GdbMi &data);
hjk's avatar
hjk committed
252 253 254
    void handleStop1(const GdbResultRecord &, const QVariant &cookie);
    void handleStop2(const GdbResultRecord &, const QVariant &cookie);
    void handleStop2(const GdbMi &data);
con's avatar
con committed
255
    void handleResultRecord(const GdbResultRecord &response);
256
    void handleFileExecAndSymbols(const GdbResultRecord &response, const QVariant &);
hjk's avatar
hjk committed
257
    void handleExecContinue(const GdbResultRecord &response, const QVariant &);
hjk's avatar
hjk committed
258
    //void handleExecRun(const GdbResultRecord &response, const QVariant &);
259 260 261 262 263 264 265 266 267 268
    void handleExecJumpToLine(const GdbResultRecord &response, const QVariant &);
    void handleExecRunToFunction(const GdbResultRecord &response, const QVariant &);
    void handleInfoShared(const GdbResultRecord &response, const QVariant &);
    void handleInfoProc(const GdbResultRecord &response, const QVariant &);
    void handleInfoThreads(const GdbResultRecord &response, const QVariant &);
    void handleShowVersion(const GdbResultRecord &response, const QVariant &);
    void handleQueryPwd(const GdbResultRecord &response, const QVariant &);
    void handleQuerySources(const GdbResultRecord &response, const QVariant &);
    void handleTargetCore(const GdbResultRecord &, const QVariant &);
    void handleExit(const GdbResultRecord &, const QVariant &);
hjk's avatar
hjk committed
269
    void handleExitHelper(const GdbResultRecord &, const QVariant &);
270 271
    void handleSetTargetAsync(const GdbResultRecord &, const QVariant &);
    void handleTargetRemote(const GdbResultRecord &, const QVariant &);
272
    void handleWatchPoint(const GdbResultRecord &, const QVariant &);
hjk's avatar
hjk committed
273
    void debugMessage(const QString &msg);
274
    bool showToolTip();
con's avatar
con committed
275

hjk's avatar
hjk committed
276 277 278 279 280 281
    // 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(); }
hjk's avatar
hjk committed
282
    DebuggerStartMode startMode() const { return m_startParameters->startMode; }
hjk's avatar
hjk committed
283 284
    qint64 inferiorPid() const { return m_manager->inferiorPid(); }

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

289 290 291
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

con's avatar
con committed
292 293
    QByteArray m_inbuffer;

hjk's avatar
hjk committed
294
    AbstractGdbAdapter *m_gdbAdapter;
295
    QProcess m_uploadProc;
con's avatar
con committed
296

297
    QHash<int, GdbCommand> m_cookieForToken;
con's avatar
con committed
298 299 300 301 302 303 304 305 306 307 308 309
    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
310
    int m_gdbBuildVersion; // MAC only? 
con's avatar
con committed
311 312

    // awful hack to keep track of used files
313 314
    QMap<QString, QString> m_shortToFullName;
    QMap<QString, QString> m_fullToShortName;
con's avatar
con committed
315 316 317 318

    //
    // Breakpoint specific stuff
    //
319
    void handleBreakList(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
320
    void handleBreakList(const GdbMi &table);
321 322 323 324 325
    void handleBreakIgnore(const GdbResultRecord &record, const QVariant &cookie);
    void handleBreakInsert(const GdbResultRecord &record, const QVariant &cookie);
    void handleBreakInsert1(const GdbResultRecord &record, const QVariant &cookie);
    void handleBreakCondition(const GdbResultRecord &record, const QVariant &cookie);
    void handleBreakInfo(const GdbResultRecord &record, const QVariant &cookie);
con's avatar
con committed
326 327 328 329 330 331 332 333
    void extractDataFromInfoBreak(const QString &output, BreakpointData *data);
    void breakpointDataFromOutput(BreakpointData *data, const GdbMi &bkpt);
    void sendInsertBreakpoint(int index);

    //
    // Modules specific stuff
    //
    void reloadModules();
334
    void handleModulesList(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
335 336 337 338 339


    //
    // Register specific stuff
    // 
340
    Q_SLOT void reloadRegisters();
341
    void setRegisterValue(int nr, const QString &value);
342 343
    void handleRegisterListNames(const GdbResultRecord &record, const QVariant &);
    void handleRegisterListValues(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
344

345 346 347 348
    //
    // Source file specific stuff
    // 
    void reloadSourceFiles();
con's avatar
con committed
349 350 351 352

    //
    // Stack specific stuff
    // 
353 354 355
    void handleStackListFrames(const GdbResultRecord &record, const QVariant &cookie);
    void handleStackSelectThread(const GdbResultRecord &, const QVariant &);
    void handleStackListThreads(const GdbResultRecord &record, const QVariant &cookie);
356 357
    Q_SLOT void reloadStack();
    Q_SLOT void reloadFullStack();
con's avatar
con committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371


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

372
    void updateWatchData(const WatchData &data);
373
    Q_SLOT void updateWatchDataHelper(const WatchData &data);
hjk's avatar
hjk committed
374
    void rebuildModel();
con's avatar
con committed
375 376 377 378 379 380 381

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

    void maybeHandleInferiorPidChanged(const QString &pid);

382
    void tryLoadDebuggingHelpers();
383
    void tryQueryDebuggingHelpers();
384 385 386 387
    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
388 389

    void handleVarListChildren(const GdbResultRecord &record,
390
        const QVariant &cookie);
con's avatar
con committed
391
    void handleVarCreate(const GdbResultRecord &record,
392 393
        const QVariant &cookie);
    void handleVarAssign(const GdbResultRecord &, const QVariant &);
con's avatar
con committed
394
    void handleEvaluateExpression(const GdbResultRecord &record,
395
        const QVariant &cookie);
396 397
    //void handleToolTip(const GdbResultRecord &record,
    //    const QVariant &cookie);
398
    void handleQueryDebuggingHelper(const GdbResultRecord &record, const QVariant &);
399
    void handleDebuggingHelperValue1(const GdbResultRecord &record,
400
        const QVariant &cookie);
401
    void handleDebuggingHelperValue2(const GdbResultRecord &record,
402
        const QVariant &cookie);
403
    void handleDebuggingHelperValue3(const GdbResultRecord &record,
404
        const QVariant &cookie);
405
    void handleDebuggingHelperEditValue(const GdbResultRecord &record);
406 407 408
    void handleDebuggingHelperSetup(const GdbResultRecord &record, const QVariant &);
    void handleStackListLocals(const GdbResultRecord &record, const QVariant &);
    void handleStackListArguments(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
409 410 411
    void handleVarListChildrenHelper(const GdbMi &child,
        const WatchData &parent);
    void setWatchDataType(WatchData &data, const GdbMi &mi);
412
    void setWatchDataDisplayedType(WatchData &data, const GdbMi &mi);
hjk's avatar
hjk committed
413
    void setLocals(const QList<GdbMi> &locals);
414 415
   
    bool startModeAllowsDumpers() const;
416
    QString parseDisassembler(const GdbMi &lines);
con's avatar
con committed
417 418

    int m_pendingRequests;
419
    QSet<QString> m_processedNames; 
con's avatar
con committed
420

421
    QtDumperHelper m_dumperHelper;
con's avatar
con committed
422
    
423
    DebuggingHelperState m_debuggingHelperState;
con's avatar
con committed
424 425 426 427
    QList<GdbMi> m_currentFunctionArgs;
    QString m_currentFrame;
    QMap<QString, QString> m_varToType;

hjk's avatar
hjk committed
428 429 430 431 432
    typedef void (GdbEngine::*Continuation)();
    // function called after all previous responses have been received
    Continuation m_continuationAfterDone;
    void handleInitialBreakpointsSet();

433
    bool m_waitingForFirstBreakpointToBeHit;
434
    bool m_modulesListOutdated;
435

436
    QList<GdbCommand> m_commandsToRunOnTemporaryBreak;
hjk's avatar
hjk committed
437

hjk's avatar
hjk committed
438
    DebuggerManager * const m_manager;
439
    IDebuggerManagerAccessForEngines * const qq;
hjk's avatar
hjk committed
440
    DebuggerStartParametersPtr m_startParameters;
441
    // make sure to re-initialize new members in initializeVariables();
hjk's avatar
hjk committed
442 443
public:
    OutputCollector m_outputCollector;
con's avatar
con committed
444 445 446 447 448
};

} // namespace Internal
} // namespace Debugger

449 450
Q_DECLARE_OPERATORS_FOR_FLAGS(Debugger::Internal::GdbEngine::GdbCommandFlags)

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