gdbengine.h 16.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"
hjk's avatar
hjk committed
36
#include "abstractgdbadapter.h"
37
#include "outputcollector.h"
38
#include "watchutils.h"
39

40 41
#include <consoleprocess.h>

con's avatar
con committed
42 43 44 45 46 47
#include <QtCore/QByteArray>
#include <QtCore/QHash>
#include <QtCore/QMap>
#include <QtCore/QObject>
#include <QtCore/QProcess>
#include <QtCore/QPoint>
48
#include <QtCore/QTextCodec>
49
#include <QtCore/QTime>
con's avatar
con committed
50 51 52 53 54 55 56 57 58 59 60
#include <QtCore/QVariant>

QT_BEGIN_NAMESPACE
class QAction;
class QAbstractItemModel;
class QWidget;
QT_END_NAMESPACE

namespace Debugger {
namespace Internal {

61

con's avatar
con committed
62 63 64 65 66 67 68 69
class DebuggerManager;
class IDebuggerManagerAccessForEngines;
class GdbResultRecord;
class GdbMi;

class WatchData;
class BreakpointData;

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

hjk's avatar
hjk committed
78
class PlainGdbAdapter : public AbstractGdbAdapter
79 80
{
public:
hjk's avatar
hjk committed
81 82
    PlainGdbAdapter(QObject *parent = 0)
        : AbstractGdbAdapter(parent)
83 84 85 86 87 88 89
    {
        connect(&m_proc, SIGNAL(error(QProcess::ProcessError)),
            this, SIGNAL(error(QProcess::ProcessError)));
        connect(&m_proc, SIGNAL(readyReadStandardOutput()),
            this, SIGNAL(readyReadStandardOutput()));
        connect(&m_proc, SIGNAL(readyReadStandardError()),
            this, SIGNAL(readyReadStandardError()));
hjk's avatar
hjk committed
90 91
        connect(&m_proc, SIGNAL(started()),
            this, SIGNAL(started()));
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        connect(&m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
            this, SIGNAL(finished(int, QProcess::ExitStatus)));
    }

    void start(const QString &program, const QStringList &args,
        QIODevice::OpenMode mode) { m_proc.start(program, args, mode); }
    void kill() { m_proc.kill(); }
    void terminate() { m_proc.terminate(); }
    bool waitForStarted(int msecs) { return m_proc.waitForStarted(msecs); }
    bool waitForFinished(int msecs) { return m_proc.waitForFinished(msecs); }
    QProcess::ProcessState state() const { return m_proc.state(); }
    QString errorString() const { return m_proc.errorString(); }
    QByteArray readAllStandardError() { return m_proc.readAllStandardError(); }
    QByteArray readAllStandardOutput() { return m_proc.readAllStandardOutput(); }
    qint64 write(const char *data) { return m_proc.write(data); }
    void setWorkingDirectory(const QString &dir) { m_proc.setWorkingDirectory(dir); }
    void setEnvironment(const QStringList &env) { m_proc.setEnvironment(env); }
hjk's avatar
hjk committed
109
    bool isAdapter() const { return false; }
hjk's avatar
hjk committed
110 111
    void attach();
    void interruptInferior();
112 113 114 115

private:
    QProcess m_proc;
};
hjk's avatar
hjk committed
116

con's avatar
con committed
117 118 119 120 121
class GdbEngine : public IDebuggerEngine
{
    Q_OBJECT

public:
hjk's avatar
hjk committed
122
    GdbEngine(DebuggerManager *parent, AbstractGdbAdapter *gdbAdapter);
con's avatar
con committed
123 124 125
    ~GdbEngine();

signals:
126 127
    void gdbInputAvailable(int channel, const QString &msg);
    void gdbOutputAvailable(int channel, const QString &msg);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
128
    void applicationOutputAvailable(const QString &output);
con's avatar
con committed
129 130

private:
hjk's avatar
hjk committed
131 132
    friend class PlainGdbAdapter;
    friend class TrkGdbAdapter;
hjk's avatar
hjk committed
133 134

    const DebuggerStartParameters &startParameters() const
hjk's avatar
hjk committed
135
        { return *m_startParameters; }
con's avatar
con committed
136 137 138 139 140 141 142 143 144 145
    //
    // IDebuggerEngine implementation
    //
    void stepExec();
    void stepOutExec();
    void nextExec();
    void stepIExec();
    void nextIExec();

    void shutdown();
146
    void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
hjk's avatar
hjk committed
147
    void startDebugger(const DebuggerStartParametersPtr &sp);
hjk's avatar
hjk committed
148
    Q_SLOT void startDebugger2();
con's avatar
con committed
149
    void exitDebugger();
hjk's avatar
hjk committed
150
    void exitDebugger2();
151
    void detachDebugger();
con's avatar
con committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

    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);
167
    void watchPoint(const QPoint &);
con's avatar
con committed
168 169 170

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

173 174 175
    void fetchMemory(MemoryViewAgent *agent, quint64 addr, quint64 length);
    void handleFetchMemory(const GdbResultRecord &record, const QVariant &cookie);

176 177 178 179 180 181 182 183 184 185 186
    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);

187 188
    Q_SLOT void setDebugDebuggingHelpers(const QVariant &on);
    Q_SLOT void setUseDebuggingHelpers(const QVariant &on);
189

con's avatar
con committed
190 191 192
    //
    // Own stuff
    //
193

con's avatar
con committed
194 195 196
    int currentFrame() const;

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

199 200
    void initializeConnections();
    void initializeVariables();
con's avatar
con committed
201 202 203 204 205 206
    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 &);

207 208 209 210 211 212
public: // otherwise the Qt flag macros are unhappy
    enum GdbCommandFlag {
        NoFlags = 0,
        NeedsStop = 1,
        Discardable = 2,
        RebuildModel = 4,
213
        WatchUpdate = Discardable | RebuildModel,
214
        EmbedToken = 8
215 216 217
    };
    Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)

hjk's avatar
hjk committed
218

219
private:
220 221 222 223 224 225 226 227 228 229 230
    typedef void (GdbEngine::*GdbCommandCallback)(const GdbResultRecord &record, const QVariant &cookie);

    struct GdbCommand
    {
        GdbCommand() : flags(0), callback(0), callbackName(0) {}

        int flags;
        GdbCommandCallback callback;
        const char *callbackName;
        QString command;
        QVariant cookie;
231
        QTime postTime;
232 233
    };

con's avatar
con committed
234 235 236 237
    // 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.
238
    void flushCommand(GdbCommand &cmd);
239
    void postCommand(const QString &command,
240 241 242 243
                     GdbCommandFlags flags,
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
244
    void postCommand(const QString &command,
245 246 247
                     GdbCommandCallback callback = 0,
                     const char *callbackName = 0,
                     const QVariant &cookie = QVariant());
con's avatar
con committed
248 249 250 251 252 253 254 255 256

    void setTokenBarrier();

    void updateLocals();

private slots:
    void gdbProcError(QProcess::ProcessError error);
    void readGdbStandardOutput();
    void readGdbStandardError();
257 258
    void readUploadStandardOutput();
    void readUploadStandardError();
259
    void readDebugeeOutput(const QByteArray &data);
260 261
    void stubStarted();
    void stubError(const QString &msg);
262
    void uploadProcError(QProcess::ProcessError error);
263
    void emitStartFailed();
con's avatar
con committed
264 265 266

private:
    int terminationIndex(const QByteArray &buffer, int &length);
267
    void handleResponse(const QByteArray &buff);
268 269 270
    void handleStart(const GdbResultRecord &response, const QVariant &);
    void handleAttach(const GdbResultRecord &, const QVariant &);
    void handleStubAttached(const GdbResultRecord &, const QVariant &);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
271
    void handleAqcuiredInferior();
272
    void handleAsyncOutput2(const GdbResultRecord &, const QVariant &cookie);
con's avatar
con committed
273 274 275
    void handleAsyncOutput2(const GdbMi &data);
    void handleAsyncOutput(const GdbMi &data);
    void handleResultRecord(const GdbResultRecord &response);
276
    void handleFileExecAndSymbols(const GdbResultRecord &response, const QVariant &);
hjk's avatar
hjk committed
277
    void handleExecContinue(const GdbResultRecord &response, const QVariant &);
278 279 280 281 282 283 284 285 286 287 288
    void handleExecRun(const GdbResultRecord &response, const QVariant &);
    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
289
    void handleExitHelper(const GdbResultRecord &, const QVariant &);
290 291
    void handleSetTargetAsync(const GdbResultRecord &, const QVariant &);
    void handleTargetRemote(const GdbResultRecord &, const QVariant &);
292
    void handleWatchPoint(const GdbResultRecord &, const QVariant &);
hjk's avatar
hjk committed
293
    void debugMessage(const QString &msg);
294
    bool showToolTip();
con's avatar
con committed
295

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

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

309 310 311 312
    OutputCollector m_outputCollector;
    QTextCodec *m_outputCodec;
    QTextCodec::ConverterState m_outputCodecState;

con's avatar
con committed
313 314
    QByteArray m_inbuffer;

hjk's avatar
hjk committed
315
    AbstractGdbAdapter *m_gdbAdapter;
316
    QProcess m_uploadProc;
con's avatar
con committed
317

318 319
    Core::Utils::ConsoleProcess m_stubProc;

320
    QHash<int, GdbCommand> m_cookieForToken;
con's avatar
con committed
321 322 323 324 325 326 327 328 329 330 331 332
    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
333
    int m_gdbBuildVersion; // MAC only? 
con's avatar
con committed
334 335

    // awful hack to keep track of used files
336 337
    QMap<QString, QString> m_shortToFullName;
    QMap<QString, QString> m_fullToShortName;
con's avatar
con committed
338 339 340 341

    //
    // Breakpoint specific stuff
    //
342
    void handleBreakList(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
343
    void handleBreakList(const GdbMi &table);
344 345 346 347 348
    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
349 350 351 352 353 354 355 356
    void extractDataFromInfoBreak(const QString &output, BreakpointData *data);
    void breakpointDataFromOutput(BreakpointData *data, const GdbMi &bkpt);
    void sendInsertBreakpoint(int index);

    //
    // Modules specific stuff
    //
    void reloadModules();
357
    void handleModulesList(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
358 359 360 361 362


    //
    // Register specific stuff
    // 
363
    Q_SLOT void reloadRegisters();
364
    void setRegisterValue(int nr, const QString &value);
365 366
    void handleRegisterListNames(const GdbResultRecord &record, const QVariant &);
    void handleRegisterListValues(const GdbResultRecord &record, const QVariant &);
con's avatar
con committed
367

368 369 370 371
    //
    // Source file specific stuff
    // 
    void reloadSourceFiles();
con's avatar
con committed
372 373 374 375

    //
    // Stack specific stuff
    // 
376 377 378
    void handleStackListFrames(const GdbResultRecord &record, const QVariant &cookie);
    void handleStackSelectThread(const GdbResultRecord &, const QVariant &);
    void handleStackListThreads(const GdbResultRecord &record, const QVariant &cookie);
379 380
    Q_SLOT void reloadStack();
    Q_SLOT void reloadFullStack();
con's avatar
con committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394


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

395
    void updateWatchData(const WatchData &data);
396
    Q_SLOT void updateWatchDataHelper(const WatchData &data);
hjk's avatar
hjk committed
397
    void rebuildModel();
con's avatar
con committed
398 399 400 401 402 403 404

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

    void maybeHandleInferiorPidChanged(const QString &pid);

405
    void tryLoadDebuggingHelpers();
406
    void tryQueryDebuggingHelpers();
407 408 409 410
    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
411 412

    void handleVarListChildren(const GdbResultRecord &record,
413
        const QVariant &cookie);
con's avatar
con committed
414
    void handleVarCreate(const GdbResultRecord &record,
415 416
        const QVariant &cookie);
    void handleVarAssign(const GdbResultRecord &, const QVariant &);
con's avatar
con committed
417
    void handleEvaluateExpression(const GdbResultRecord &record,
418
        const QVariant &cookie);
419 420
    //void handleToolTip(const GdbResultRecord &record,
    //    const QVariant &cookie);
421
    void handleQueryDebuggingHelper(const GdbResultRecord &record, const QVariant &);
422
    void handleDebuggingHelperValue1(const GdbResultRecord &record,
423
        const QVariant &cookie);
424
    void handleDebuggingHelperValue2(const GdbResultRecord &record,
425
        const QVariant &cookie);
426
    void handleDebuggingHelperValue3(const GdbResultRecord &record,
427
        const QVariant &cookie);
428
    void handleDebuggingHelperEditValue(const GdbResultRecord &record);
429 430 431
    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
432 433 434
    void handleVarListChildrenHelper(const GdbMi &child,
        const WatchData &parent);
    void setWatchDataType(WatchData &data, const GdbMi &mi);
435
    void setWatchDataDisplayedType(WatchData &data, const GdbMi &mi);
hjk's avatar
hjk committed
436
    void setLocals(const QList<GdbMi> &locals);
437 438
   
    bool startModeAllowsDumpers() const;
439
    QString parseDisassembler(const GdbMi &lines);
con's avatar
con committed
440 441

    int m_pendingRequests;
442
    QSet<QString> m_processedNames; 
con's avatar
con committed
443

444
    QtDumperHelper m_dumperHelper;
con's avatar
con committed
445
    
446
    DebuggingHelperState m_debuggingHelperState;
con's avatar
con committed
447 448 449 450
    QList<GdbMi> m_currentFunctionArgs;
    QString m_currentFrame;
    QMap<QString, QString> m_varToType;

451
    bool m_autoContinue;
452
    bool m_waitingForFirstBreakpointToBeHit;
453
    bool m_modulesListOutdated;
454

455
    QList<GdbCommand> m_commandsToRunOnTemporaryBreak;
hjk's avatar
hjk committed
456

hjk's avatar
hjk committed
457
    DebuggerManager * const m_manager;
458
    IDebuggerManagerAccessForEngines * const qq;
hjk's avatar
hjk committed
459
    DebuggerStartParametersPtr m_startParameters;
460
    // make sure to re-initialize new members in initializeVariables();
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