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"
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 RemoteGdbAdapter;
96
    friend class TrkGdbAdapter;
hjk's avatar
hjk committed
97

con's avatar
con committed
98 99 100 101 102 103 104 105 106 107
    //
    // IDebuggerEngine implementation
    //
    void stepExec();
    void stepOutExec();
    void nextExec();
    void stepIExec();
    void nextIExec();

    void shutdown();
108
    void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
hjk's avatar
hjk committed
109
    void startDebugger(const DebuggerStartParametersPtr &sp);
con's avatar
con committed
110
    void exitDebugger();
111
    void detachDebugger();
con's avatar
con committed
112

113 114
    void continueInferiorInternal();
    void autoContinueInferior();
con's avatar
con committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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);
129
    void watchPoint(const QPoint &);
con's avatar
con committed
130 131 132

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

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

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

146 147
    Q_SLOT void setDebugDebuggingHelpers(const QVariant &on);
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
148
    Q_SLOT void setAutoDerefPointers(const QVariant &on);
hjk's avatar
hjk committed
149 150
    bool isGdbEngine() const { return true; }
    bool isSynchroneous() const;
151

hjk's avatar
hjk committed
152
    bool checkConfiguration(int toolChain, QString *errorMessage, QString *settingsPage= 0) const;
153

con's avatar
con committed
154 155 156
    //
    // Own stuff
    //
157

con's avatar
con committed
158 159 160
    int currentFrame() const;

    bool supportsThreads() const;
hjk's avatar
hjk committed
161
    void gotoLocation(const StackFrame &frame, bool setLocationMarker);
162
    StackFrame parseStackFrame(const GdbMi &mi, int level);
con's avatar
con committed
163

164
    void connectAdapter();
165
    void initializeVariables();
con's avatar
con committed
166 167 168 169
    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
170
    void handleResult(const GdbResponse &response);
con's avatar
con committed
171

172 173 174
public: // otherwise the Qt flag macros are unhappy
    enum GdbCommandFlag {
        NoFlags = 0,
175 176 177
        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
178
        WatchUpdate = Discardable | RebuildModel,
179
        EmbedToken = 8,   // Expand %1 in the command to the command token
180 181
        RunRequest = 16,  // Callback expect GdbResultRunning instead of GdbResultDone
        ExitRequest = 32  // Callback expect GdbResultExit instead of GdbResultDone
182 183 184
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)

hjk's avatar
hjk committed
185

186
private:
hjk's avatar
hjk committed
187
    typedef void (GdbEngine::*GdbCommandCallback)
hjk's avatar
hjk committed
188
        (const GdbResponse &response);
hjk's avatar
hjk committed
189
    typedef void (AbstractGdbAdapter::*AdapterCallback)
hjk's avatar
hjk committed
190
        (const GdbResponse &response);
191 192 193

    struct GdbCommand
    {
hjk's avatar
hjk committed
194 195 196
        GdbCommand()
            : flags(0), callback(0), adapterCallback(0), callbackName(0)
        {}
197 198 199

        int flags;
        GdbCommandCallback callback;
hjk's avatar
hjk committed
200
        AdapterCallback adapterCallback;
201 202 203
        const char *callbackName;
        QString command;
        QVariant cookie;
204
        QTime postTime;
205 206
    };

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

233
    void updateAll();
234
    void updateLocals(const QVariant &cookie = QVariant());
con's avatar
con committed
235

236 237 238 239 240
    void gdbInputAvailable(int channel, const QString &msg)
    { m_manager->showDebuggerInput(channel, msg); }
    void gdbOutputAvailable(int channel, const QString &msg)
    { m_manager->showDebuggerOutput(channel, msg); }

con's avatar
con committed
241 242 243
private slots:
    void readGdbStandardOutput();
    void readGdbStandardError();
244
    void readDebugeeOutput(const QByteArray &data);
con's avatar
con committed
245

hjk's avatar
hjk committed
246
    void handleAdapterStarted();
247
    void handleAdapterStartFailed(const QString &msg, const QString &settingsIdHint = QString());
hjk's avatar
hjk committed
248 249 250 251 252

    void handleInferiorStartFailed(const QString &msg);
    void handleInferiorShutDown();
    void handleInferiorShutdownFailed(const QString &msg);

253
    void handleAdapterCrashed(const QString &msg);
hjk's avatar
hjk committed
254 255 256
    void handleAdapterShutDown();
    void handleAdapterShutdownFailed(const QString &msg);

con's avatar
con committed
257
private:
258
    void handleResponse(const QByteArray &buff);
259
    void handleStopResponse(const GdbMi &data);
hjk's avatar
hjk committed
260
    void handleStop1(const GdbResponse &response);
261
    void handleStop1(const GdbMi &data);
hjk's avatar
hjk committed
262
    void handleStop2(const GdbResponse &response);
hjk's avatar
hjk committed
263
    void handleStop2(const GdbMi &data);
hjk's avatar
hjk committed
264 265
    void handleResultRecord(const GdbResponse &response);
    void handleExecContinue(const GdbResponse &response);
266
//    void handleExecRunToFunction(const GdbResponse &response);
hjk's avatar
hjk committed
267 268 269
    void handleShowVersion(const GdbResponse &response);
    void handleQuerySources(const GdbResponse &response);
    void handleWatchPoint(const GdbResponse &response);
hjk's avatar
hjk committed
270
    void handleIsSynchroneous(const GdbResponse &response);
271
    bool showToolTip();
con's avatar
con committed
272

hjk's avatar
hjk committed
273
    // Convenience
274
    QMainWindow *mainWindow() const;
275
    DebuggerStartMode startMode() const;
hjk's avatar
hjk committed
276
    qint64 inferiorPid() const { return m_manager->inferiorPid(); }
277
    void handleInferiorPidChanged(qint64 pid) { manager()->notifyInferiorPidChanged(pid); }
hjk's avatar
hjk committed
278

279 280
    void handleChildren(const WatchData &parent, const GdbMi &child,
        QList<WatchData> *insertions);
281 282
    const bool m_dumperInjectionLoad;

283 284 285
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

con's avatar
con committed
286 287
    QByteArray m_inbuffer;

288
    QHash<int, GdbCommand> m_cookieForToken;
con's avatar
con committed
289 290 291 292 293 294 295 296 297 298

    QByteArray m_pendingConsoleStreamOutput;
    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
299
    int m_gdbBuildVersion; // MAC only?
hjk's avatar
hjk committed
300
    bool m_isSynchroneous; // Can act synchroneously?
con's avatar
con committed
301 302

    // awful hack to keep track of used files
303 304
    QMap<QString, QString> m_shortToFullName;
    QMap<QString, QString> m_fullToShortName;
con's avatar
con committed
305 306 307 308

    //
    // Breakpoint specific stuff
    //
hjk's avatar
hjk committed
309
    void handleBreakList(const GdbResponse &response);
con's avatar
con committed
310
    void handleBreakList(const GdbMi &table);
hjk's avatar
hjk committed
311 312 313 314 315
    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
316 317 318 319 320 321 322 323
    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
324
    void handleModulesList(const GdbResponse &response);
con's avatar
con committed
325 326 327 328


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

335 336
    //
    // Source file specific stuff
hjk's avatar
hjk committed
337
    //
338
    void reloadSourceFiles();
con's avatar
con committed
339 340 341

    //
    // Stack specific stuff
hjk's avatar
hjk committed
342
    //
hjk's avatar
hjk committed
343 344 345
    void handleStackListFrames(const GdbResponse &response);
    void handleStackSelectThread(const GdbResponse &response);
    void handleStackListThreads(const GdbResponse &response);
hjk's avatar
hjk committed
346 347 348
    void handleStackFrame1(const GdbResponse &response);
    void handleStackFrame2(const GdbResponse &response);
    QByteArray m_firstChunk;
hjk's avatar
hjk committed
349
    Q_SLOT void reloadStack(bool forceGotoLocation);
350
    Q_SLOT void reloadFullStack();
con's avatar
con committed
351 352 353 354


    //
    // Tooltip specific stuff
hjk's avatar
hjk committed
355
    //
con's avatar
con committed
356 357 358 359 360 361 362 363 364
    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);

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

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

    void maybeHandleInferiorPidChanged(const QString &pid);

375
    void tryLoadDebuggingHelpers();
376
    void tryQueryDebuggingHelpers();
377 378 379 380
    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
381

hjk's avatar
hjk committed
382 383 384 385 386 387 388 389 390 391 392 393 394
    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
395 396 397
    void handleVarListChildrenHelper(const GdbMi &child,
        const WatchData &parent);
    void setWatchDataType(WatchData &data, const GdbMi &mi);
398
    void setWatchDataDisplayedType(WatchData &data, const GdbMi &mi);
399 400 401
    inline WatchData localVariable(const GdbMi &item,
                                   const QStringList &uninitializedVariables,
                                   QMap<QByteArray, int> *seen);
402 403
    void connectDebuggingHelperActions();
    void disconnectDebuggingHelperActions();
404
    AbstractGdbAdapter *createAdapter(const DebuggerStartParametersPtr &dp);
hjk's avatar
hjk committed
405

406
    bool startModeAllowsDumpers() const;
407
    QString parseDisassembler(const GdbMi &lines);
con's avatar
con committed
408 409

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

412
    QtDumperHelper m_dumperHelper;
hjk's avatar
hjk committed
413

414
    DebuggingHelperState m_debuggingHelperState;
con's avatar
con committed
415 416 417 418
    QList<GdbMi> m_currentFunctionArgs;
    QString m_currentFrame;
    QMap<QString, QString> m_varToType;

419
    typedef void (GdbEngine::*CommandsDoneCallback)();
hjk's avatar
hjk committed
420
    // function called after all previous responses have been received
421
    CommandsDoneCallback m_commandsDoneCallback;
hjk's avatar
hjk committed
422
    void startInferior();
hjk's avatar
hjk committed
423

424
    bool m_modulesListOutdated;
425

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

hjk's avatar
hjk committed
428
    DebuggerStartParametersPtr m_startParameters;
429
    // make sure to re-initialize new members in initializeVariables();
430

431 432 433
    QSharedPointer<TrkOptions> m_trkOptions;

    AbstractGdbAdapter *m_gdbAdapter;
hjk's avatar
hjk committed
434

hjk's avatar
hjk committed
435
public:
436
    QString errorMessage(QProcess::ProcessError error);
437
    void showMessageBox(int icon, const QString &title, const QString &text);
438
    void debugMessage(const QString &msg);
439
    void addOptionPages(QList<Core::IOptionsPage*> *opts) const;
440 441
    const DebuggerStartParameters &startParameters() const
        { return *m_startParameters; }
con's avatar
con committed
442 443 444 445 446
};

} // namespace Internal
} // namespace Debugger

447 448
Q_DECLARE_OPERATORS_FOR_FLAGS(Debugger::Internal::GdbEngine::GdbCommandFlags)

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