cdbengine.cpp 122 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30
#include "cdbengine.h"
hjk's avatar
hjk committed
31
32

#include "bytearrayinputstream.h"
33
#include "cdboptionspage.h"
hjk's avatar
hjk committed
34
#include "cdbparsehelpers.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <debugger/breakhandler.h>
#include <debugger/debuggeractions.h>
#include <debugger/debuggercore.h>
#include <debugger/debuggerprotocol.h>
#include <debugger/debuggermainwindow.h>
#include <debugger/debuggerstartparameters.h>
#include <debugger/debuggertooltipmanager.h>
#include <debugger/disassembleragent.h>
#include <debugger/disassemblerlines.h>
#include <debugger/memoryagent.h>
#include <debugger/moduleshandler.h>
#include <debugger/registerhandler.h>
#include <debugger/stackhandler.h>
#include <debugger/threadshandler.h>
#include <debugger/watchhandler.h>
#include <debugger/procinterrupt.h>
#include <debugger/sourceutils.h>
#include <debugger/shared/cdbsymbolpathlisteditor.h>
#include <debugger/shared/hostutils.h>
55
56

#include <coreplugin/icore.h>
57
#include <projectexplorer/taskhub.h>
58
#include <texteditor/itexteditor.h>
59

60
#include <utils/synchronousprocess.h>
61
#include <utils/qtcprocess.h>
62
63
64
#include <utils/winutils.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
65
#include <utils/consoleprocess.h>
66
#include <utils/fileutils.h>
67
#include <utils/hostosinfo.h>
68
69

#include <cplusplus/findcdbbreakpoint.h>
70
#include <cpptools/cppmodelmanagerinterface.h>
71

72
73
#include <QDir>
#include <QMessageBox>
74
75
76

#include <cctype>

77
78
Q_DECLARE_METATYPE(Debugger::Internal::DisassemblerAgent*)
Q_DECLARE_METATYPE(Debugger::Internal::MemoryAgent*)
79
80

enum { debug = 0 };
81
enum { debugLocals = 0 };
82
enum { debugSourceMapping = 0 };
83
enum { debugWatches = 0 };
84
85
enum { debugBreakpoints = 0 };

hjk's avatar
hjk committed
86
87
88
89
90
91
enum HandleLocalsFlags
{
    PartialLocalsUpdate = 0x1,
    LocalsUpdateForNewFrame = 0x2
};

92
#if 0
93
#  define STATE_DEBUG(state, func, line, notifyFunc) qDebug("%s in %s at %s:%d", notifyFunc, stateName(state), func, line);
94
#else
95
#  define STATE_DEBUG(state, func, line, notifyFunc)
96
97
#endif

98
99
100
101
102
103
104
105
/*!
    \class Debugger::Internal::CdbEngine

    Cdb engine version 2: Run the CDB process on pipes and parse its output.
    The engine relies on a CDB extension Qt Creator provides as an extension
    library (32/64bit), which is loaded into cdb.exe. It serves to:

    \list
106
    \li Notify the engine about the state of the debugging session:
107
        \list
108
109
110
111
        \li idle: (hooked up with .idle_cmd) debuggee stopped
        \li accessible: Debuggee stopped, cdb.exe accepts commands
        \li inaccessible: Debuggee runs, no way to post commands
        \li session active/inactive: Lost debuggee, terminating.
112
        \endlist
113
    \li Hook up with output/event callbacks and produce formatted output to be able
114
       to catch application output and exceptions.
115
    \li Provide some extension commands that produce output in a standardized (GDBMI)
116
117
      format that ends up in handleExtensionMessage(), for example:
      \list
118
119
120
121
      \li pid     Return debuggee pid for interrupting.
      \li locals  Print locals from SymbolGroup
      \li expandLocals Expand locals in symbol group
      \li registers, modules, threads
122
123
124
125
126
127
128
      \endlist
   \endlist

   Debugger commands can be posted by calling:

   \list

129
130
    \li postCommand(): Does not expect a reply
    \li postBuiltinCommand(): Run a builtin-command producing free-format, multiline output
131
132
       that is captured by enclosing it in special tokens using the 'echo' command and
       then invokes a callback with a CdbBuiltinCommand structure.
133
    \li postExtensionCommand(): Run a command provided by the extension producing
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
       one-line output and invoke a callback with a CdbExtensionCommand structure
       (output is potentially split up in chunks).
    \endlist


    Startup sequence:
    [Console: The console stub launches the process. On process startup,
              launchCDB() is called with AttachExternal].
    setupEngine() calls launchCDB() with the startparameters. The debuggee
    runs into the initial breakpoint (session idle). EngineSetupOk is
    notified (inferior still stopped). setupInferior() is then called
    which does breakpoint synchronization and issues the extension 'pid'
    command to obtain the inferior pid (which also hooks up the output callbacks).
     handlePid() notifies notifyInferiorSetupOk.
    runEngine() is then called which issues 'g' to continue the inferior.
    Shutdown mostly uses notifyEngineSpontaneousShutdown() as cdb just quits
    when the inferior exits (except attach modes).
*/
152

153
using namespace ProjectExplorer;
154
using namespace Utils;
155

156
namespace Debugger {
157
namespace Internal {
158

159
160
static const char localsPrefixC[] = "local.";

hjk's avatar
hjk committed
161
162
struct MemoryViewCookie
{
163
    explicit MemoryViewCookie(MemoryAgent *a = 0, QObject *e = 0,
164
                              quint64 addr = 0, quint64 l = 0) :
hjk's avatar
hjk committed
165
166
        agent(a), editorToken(e), address(addr), length(l)
    {}
167

168
    MemoryAgent *agent;
169
170
171
172
173
    QObject *editorToken;
    quint64 address;
    quint64 length;
};

174
175
176
177
178
179
180
181
182
struct MemoryChangeCookie
{
    explicit MemoryChangeCookie(quint64 addr = 0, const QByteArray &d = QByteArray()) :
                               address(addr), data(d) {}

    quint64 address;
    QByteArray data;
};

183
184
struct ConditionalBreakPointCookie
{
185
186
    ConditionalBreakPointCookie(BreakpointModelId i = BreakpointModelId()) : id(i) {}
    BreakpointModelId id;
187
188
189
    GdbMi stopReason;
};

190
} // namespace Internal
191
192
} // namespace Debugger

193
Q_DECLARE_METATYPE(Debugger::Internal::MemoryViewCookie)
194
Q_DECLARE_METATYPE(Debugger::Internal::MemoryChangeCookie)
195
Q_DECLARE_METATYPE(Debugger::Internal::ConditionalBreakPointCookie)
196
197

namespace Debugger {
198
namespace Internal {
199

200
static inline bool isCreatorConsole(const DebuggerStartParameters &sp)
hjk's avatar
hjk committed
201
{
202
    return !debuggerCore()->boolSetting(UseCdbConsole) && sp.useTerminal
203
           && (sp.startMode == StartInternal || sp.startMode == StartExternal);
hjk's avatar
hjk committed
204
}
205

hjk's avatar
hjk committed
206
207
static QMessageBox *
nonModalMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text)
208
209
210
211
212
213
214
215
{
    QMessageBox *mb = new QMessageBox(icon, title, text, QMessageBox::Ok,
                                      debuggerCore()->mainWindow());
    mb->setAttribute(Qt::WA_DeleteOnClose);
    mb->show();
    return mb;
}

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Base data structure for command queue entries with callback
struct CdbCommandBase
{
    typedef CdbEngine::BuiltinCommandHandler CommandHandler;

    CdbCommandBase();
    CdbCommandBase(const QByteArray  &cmd, int token, unsigned flags,
                   unsigned nc, const QVariant &cookie);

    int token;
    unsigned flags;
    QByteArray command;
    QVariant cookie;
    // Continue with another commands as specified in CommandSequenceFlags
    unsigned commandSequence;
};

CdbCommandBase::CdbCommandBase() :
    token(0), flags(0), commandSequence(0)
{
}

CdbCommandBase::CdbCommandBase(const QByteArray  &cmd, int t, unsigned f,
                               unsigned nc, const QVariant &c) :
    token(t), flags(f), command(cmd), cookie(c), commandSequence(nc)
{
}

// Queue entry for builtin commands producing free-format
// line-by-line output.
struct CdbBuiltinCommand : public CdbCommandBase
{
    typedef CdbEngine::BuiltinCommandHandler CommandHandler;

    CdbBuiltinCommand() {}
    CdbBuiltinCommand(const QByteArray  &cmd, int token, unsigned flags,
                      CommandHandler h,
                      unsigned nc, const QVariant &cookie) :
hjk's avatar
hjk committed
254
255
        CdbCommandBase(cmd, token, flags, nc, cookie), handler(h)
    {}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317


    QByteArray joinedReply() const;

    CommandHandler handler;
    QList<QByteArray> reply;
};

QByteArray CdbBuiltinCommand::joinedReply() const
{
    if (reply.isEmpty())
        return QByteArray();
    QByteArray answer;
    answer.reserve(120  * reply.size());
    foreach (const QByteArray &l, reply) {
        answer += l;
        answer += '\n';
    }
    return answer;
}

// Queue entry for Qt Creator extension commands producing one-line
// output with success flag and error message.
struct CdbExtensionCommand : public CdbCommandBase
{
    typedef CdbEngine::ExtensionCommandHandler CommandHandler;

    CdbExtensionCommand() : success(false) {}
    CdbExtensionCommand(const QByteArray  &cmd, int token, unsigned flags,
                      CommandHandler h,
                      unsigned nc, const QVariant &cookie) :
        CdbCommandBase(cmd, token, flags, nc, cookie), handler(h),success(false) {}

    CommandHandler handler;
    QByteArray reply;
    QByteArray errorMessage;
    bool success;
};

template <class CommandPtrType>
int indexOfCommand(const QList<CommandPtrType> &l, int token)
{
    const int count = l.size();
    for (int i = 0; i < count; i++)
        if (l.at(i)->token == token)
            return i;
    return -1;
}

static inline bool validMode(DebuggerStartMode sm)
{
    switch (sm) {
    case NoStartMode:
    case StartRemoteGdb:
        return false;
    default:
        break;
    }
    return true;
}

// Accessed by RunControlFactory
318
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp, QString *errorMessage)
319
{
320
    if (HostOsInfo::isWindowsHost()) {
321
322
323
324
325
        if (validMode(sp.startMode))
            return new CdbEngine(sp);
        *errorMessage = QLatin1String("Internal error: Invalid start parameters passed for thee CDB engine.");
    } else {
        *errorMessage = QString::fromLatin1("Unsupported debug mode");
326
    }
327
328
329
    return 0;
}

330
void addCdbOptionPages(QList<Core::IOptionsPage *> *opts)
331
{
332
    if (HostOsInfo::isWindowsHost()) {
333
        opts->push_back(new CdbOptionsPage);
334
335
        opts->push_back(new CdbPathsPage);
    }
336
337
338
339
}

#define QT_CREATOR_CDB_EXT "qtcreatorcdbext"

340
CdbEngine::CdbEngine(const DebuggerStartParameters &sp) :
341
    DebuggerEngine(sp),
342
343
    m_creatorExtPrefix("<qtcreatorcdbext>|"),
    m_tokenPrefix("<token>"),
344
    m_effectiveStartMode(NoStartMode),
345
346
347
348
    m_accessible(false),
    m_specialStopMode(NoSpecialStop),
    m_nextCommandToken(0),
    m_currentBuiltinCommandIndex(-1),
hjk's avatar
hjk committed
349
    m_extensionCommandPrefixBA("!" QT_CREATOR_CDB_EXT "."),
350
351
    m_operateByInstructionPending(true),
    m_operateByInstruction(true), // Default CDB setting
352
    m_verboseLogPending(true),
353
    m_verboseLog(false), // Default CDB setting
354
355
    m_notifyEngineShutdownOnTermination(false),
    m_hasDebuggee(false),
356
    m_cdbIs64Bit(false),
357
    m_wow64State(wow64Uninitialized),
358
    m_elapsedLogTime(0),
359
    m_sourceStepInto(false),
360
    m_watchPointX(0),
361
362
    m_watchPointY(0),
    m_ignoreCdbOutput(false)
363
{
364
365
    connect(debuggerCore()->action(OperateByInstruction), SIGNAL(triggered(bool)),
            this, SLOT(operateByInstructionTriggered(bool)));
366
367
    connect(debuggerCore()->action(VerboseLog), SIGNAL(triggered(bool)),
            this, SLOT(verboseLogTriggered(bool)));
368
369
370
371
372
373
374
    setObjectName(QLatin1String("CdbEngine"));
    connect(&m_process, SIGNAL(finished(int)), this, SLOT(processFinished()));
    connect(&m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError()));
    connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOut()));
    connect(&m_process, SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardOut()));
}

375
376
377
void CdbEngine::init()
{
    m_effectiveStartMode = NoStartMode;
378
    notifyInferiorPid(0);
379
380
381
382
    m_accessible = false;
    m_specialStopMode = NoSpecialStop;
    m_nextCommandToken  = 0;
    m_currentBuiltinCommandIndex = -1;
383
    m_operateByInstructionPending = debuggerCore()->action(OperateByInstruction)->isChecked();
384
    m_verboseLogPending = debuggerCore()->boolSetting(VerboseLog);
385
    m_operateByInstruction = true; // Default CDB setting
386
    m_verboseLog = false; // Default CDB setting
387
388
389
390
391
    m_notifyEngineShutdownOnTermination = false;
    m_hasDebuggee = false;
    m_sourceStepInto = false;
    m_watchPointX = m_watchPointY = 0;
    m_ignoreCdbOutput = false;
392
    m_watchInameToName.clear();
393
    m_wow64State = wow64Uninitialized;
394
395
396
397
398
399

    m_outputBuffer.clear();
    m_builtinCommandQueue.clear();
    m_extensionCommandQueue.clear();
    m_extensionMessageBuffer.clear();
    m_pendingBreakpointMap.clear();
400
    m_customSpecialStopData.clear();
401
    m_symbolAddressCache.clear();
402
    m_coreStopReason.reset();
403
404
405
406
407
408
409
410
411
412
413
414
415

    // Create local list of mappings in native separators
    m_sourcePathMappings.clear();
    const QSharedPointer<GlobalDebuggerOptions> globalOptions = debuggerCore()->globalDebuggerOptions();
    if (!globalOptions->sourcePathMap.isEmpty()) {
        typedef GlobalDebuggerOptions::SourcePathMap::const_iterator SourcePathMapIterator;
        m_sourcePathMappings.reserve(globalOptions->sourcePathMap.size());
        const SourcePathMapIterator cend = globalOptions->sourcePathMap.constEnd();
        for (SourcePathMapIterator it = globalOptions->sourcePathMap.constBegin(); it != cend; ++it) {
            m_sourcePathMappings.push_back(SourcePathMapping(QDir::toNativeSeparators(it.key()),
                                                             QDir::toNativeSeparators(it.value())));
        }
    }
416
417
    // update source path maps from debugger start params
    mergeStartParametersSourcePathMap();
418
    QTC_ASSERT(m_process.state() != QProcess::Running, SynchronousProcess::stopProcess(m_process));
419
420
}

421
422
423
424
425
426
CdbEngine::~CdbEngine()
{
}

void CdbEngine::operateByInstructionTriggered(bool operateByInstruction)
{
427
428
429
    // To be set next time session becomes accessible
    m_operateByInstructionPending = operateByInstruction;
    if (state() == InferiorStopOk)
430
431
432
        syncOperateByInstruction(operateByInstruction);
}

433
434
435
436
437
438
439
void CdbEngine::verboseLogTriggered(bool verboseLog)
{
    m_verboseLogPending = verboseLog;
    if (state() == InferiorStopOk)
        syncVerboseLog(verboseLog);
}

440
441
void CdbEngine::syncOperateByInstruction(bool operateByInstruction)
{
442
443
    if (debug)
        qDebug("syncOperateByInstruction current: %d new %d", m_operateByInstruction, operateByInstruction);
444
445
    if (m_operateByInstruction == operateByInstruction)
        return;
446
    QTC_ASSERT(m_accessible, return);
447
448
449
    m_operateByInstruction = operateByInstruction;
    postCommand(m_operateByInstruction ? QByteArray("l-t") : QByteArray("l+t"), 0);
    postCommand(m_operateByInstruction ? QByteArray("l-s") : QByteArray("l+s"), 0);
450
451
}

452
453
454
455
456
457
458
459
460
void CdbEngine::syncVerboseLog(bool verboseLog)
{
    if (m_verboseLog == verboseLog)
        return;
    QTC_ASSERT(m_accessible, return);
    m_verboseLog = verboseLog;
    postCommand(m_verboseLog ? QByteArray("!sym noisy") : QByteArray("!sym quiet"), 0);
}

461
bool CdbEngine::setToolTipExpression(const QPoint &mousePos,
462
463
                                     TextEditor::ITextEditor *editor,
                                     const DebuggerToolTipContext &contextIn)
464
{
465
466
467
468
    if (debug)
        qDebug() << Q_FUNC_INFO;
    // Need a stopped debuggee and a cpp file in a valid frame
    if (state() != InferiorStopOk || !isCppEditor(editor) || stackHandler()->currentIndex() < 0)
469
        return false;
470
471
472
    // Determine expression and function
    int line;
    int column;
473
    DebuggerToolTipContext context = contextIn;
474
    QString exp = fixCppExpression(cppExpressionAt(editor, context.position, &line, &column, &context.function));
475
    // Are we in the current stack frame
476
    if (context.function.isEmpty() || exp.isEmpty() || context.function != stackHandler()->currentFrame().function)
477
        return false;
478
479
480
    // Show tooltips of local variables only. Anything else can slow debugging down.
    const WatchData *localVariable = watchHandler()->findCppLocalVariable(exp);
    if (!localVariable)
481
        return false;
482
    DebuggerToolTipWidget *tw = new DebuggerToolTipWidget;
483
    tw->setContext(context);
484
    tw->setIname(localVariable->iname);
485
    tw->acquireEngine(this);
486
    DebuggerToolTipManager::instance()->showToolTip(mousePos, editor, tw);
487
    return true;
488
489
490
}

// Determine full path to the CDB extension library.
491
QString CdbEngine::extensionLibraryName(bool is64Bit)
492
493
494
495
{
    // Determine extension lib name and path to use
    QString rc;
    QTextStream(&rc) << QFileInfo(QCoreApplication::applicationDirPath()).path()
hjk's avatar
hjk committed
496
                     << "/lib/" << (is64Bit ? QT_CREATOR_CDB_EXT "64" : QT_CREATOR_CDB_EXT "32")
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
                     << '/' << QT_CREATOR_CDB_EXT << ".dll";
    return rc;
}

// Determine environment for CDB.exe, start out with run config and
// add CDB extension path merged with system value should there be one.
static QStringList mergeEnvironment(QStringList runConfigEnvironment,
                                    QString cdbExtensionPath)
{
    // Determine CDB extension path from Qt Creator
    static const char cdbExtensionPathVariableC[] = "_NT_DEBUGGER_EXTENSION_PATH";
    const QByteArray oldCdbExtensionPath = qgetenv(cdbExtensionPathVariableC);
    if (!oldCdbExtensionPath.isEmpty()) {
        cdbExtensionPath.append(QLatin1Char(';'));
        cdbExtensionPath.append(QString::fromLocal8Bit(oldCdbExtensionPath));
    }
    // We do not assume someone sets _NT_DEBUGGER_EXTENSION_PATH in the run
    // config, just to make sure, delete any existing entries
    const QString cdbExtensionPathVariableAssign =
            QLatin1String(cdbExtensionPathVariableC) + QLatin1Char('=');
    for (QStringList::iterator it = runConfigEnvironment.begin(); it != runConfigEnvironment.end() ; ) {
        if (it->startsWith(cdbExtensionPathVariableAssign)) {
            it = runConfigEnvironment.erase(it);
            break;
        } else {
            ++it;
        }
    }
    runConfigEnvironment.append(cdbExtensionPathVariableAssign +
                                QDir::toNativeSeparators(cdbExtensionPath));
    return runConfigEnvironment;
}

int CdbEngine::elapsedLogTime() const
{
    const int elapsed = m_logTime.elapsed();
    const int delta = elapsed - m_elapsedLogTime;
    m_elapsedLogTime = elapsed;
    return delta;
}

538
539
540
541
542
// Start the console stub with the sub process. Continue in consoleStubProcessStarted.
bool CdbEngine::startConsole(const DebuggerStartParameters &sp, QString *errorMessage)
{
    if (debug)
        qDebug("startConsole %s", qPrintable(sp.executable));
543
544
    m_consoleStub.reset(new ConsoleProcess);
    m_consoleStub->setMode(ConsoleProcess::Suspend);
545
546
    connect(m_consoleStub.data(), SIGNAL(processError(QString)),
            SLOT(consoleStubError(QString)));
547
548
    connect(m_consoleStub.data(), SIGNAL(processStarted()),
            SLOT(consoleStubProcessStarted()));
549
    connect(m_consoleStub.data(), SIGNAL(stubStopped()),
550
551
            SLOT(consoleStubExited()));
    m_consoleStub->setWorkingDirectory(sp.workingDirectory);
552
553
    if (sp.environment.size())
        m_consoleStub->setEnvironment(sp.environment);
554
555
556
557
558
559
560
    if (!m_consoleStub->start(sp.executable, sp.processArgs)) {
        *errorMessage = tr("The console process '%1' could not be started.").arg(sp.executable);
        return false;
    }
    return true;
}

561
void CdbEngine::consoleStubError(const QString &msg)
562
{
563
    if (debug)
564
565
566
567
        qDebug("consoleStubProcessMessage() in %s %s", stateName(state()), qPrintable(msg));
    if (state() == EngineSetupRequested) {
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineSetupFailed")
        notifyEngineSetupFailed();
568
    } else {
569
570
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineIll")
        notifyEngineIll();
571
    }
572
    nonModalMessageBox(QMessageBox::Critical, tr("Debugger Error"), msg);
573
574
575
576
577
578
579
580
581
582
583
584
}

void CdbEngine::consoleStubProcessStarted()
{
    if (debug)
        qDebug("consoleStubProcessStarted() PID=%lld", m_consoleStub->applicationPID());
    // Attach to console process.
    DebuggerStartParameters attachParameters = startParameters();
    attachParameters.executable.clear();
    attachParameters.processArgs.clear();
    attachParameters.attachPID = m_consoleStub->applicationPID();
    attachParameters.startMode = AttachExternal;
585
    attachParameters.useTerminal = false;
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
    showMessage(QString::fromLatin1("Attaching to %1...").arg(attachParameters.attachPID), LogMisc);
    QString errorMessage;
    if (!launchCDB(attachParameters, &errorMessage)) {
        showMessage(errorMessage, LogError);
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineSetupFailed")
        notifyEngineSetupFailed();
    }
}

void CdbEngine::consoleStubExited()
{
}

void CdbEngine::setupEngine()
{
    if (debug)
        qDebug(">setupEngine");
603
    // Nag to add symbol server and cache
604
605
606
    QStringList symbolPaths = debuggerCore()->stringListSetting(CdbSymbolPaths);
    if (CdbSymbolPathListEditor::promptToAddSymbolPaths(&symbolPaths))
        debuggerCore()->action(CdbSymbolPaths)->setValue(symbolPaths);
607

608
609
610
    init();
    if (!m_logTime.elapsed())
        m_logTime.start();
611
612
613
614
615
616
    QString errorMessage;
    // Console: Launch the stub with the suspended application and attach to it
    // CDB in theory has a command line option '-2' that launches a
    // console, too, but that immediately closes when the debuggee quits.
    // Use the Creator stub instead.
    const DebuggerStartParameters &sp = startParameters();
617
    const bool launchConsole = isCreatorConsole(sp);
618
619
620
621
622
623
624
625
626
627
628
    m_effectiveStartMode = launchConsole ? AttachExternal : sp.startMode;
    const bool ok = launchConsole ?
                startConsole(startParameters(), &errorMessage) :
                launchCDB(startParameters(), &errorMessage);
    if (debug)
        qDebug("<setupEngine ok=%d", ok);
    if (!ok) {
        showMessage(errorMessage, LogError);
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineSetupFailed")
        notifyEngineSetupFailed();
    }
629
    const QString normalFormat = tr("Normal");
630
    const QStringList stringFormats = QStringList()
631
        << normalFormat << tr("Separate Window");
632
633
634
635
636
    WatchHandler *wh = watchHandler();
    wh->addTypeFormats("QString", stringFormats);
    wh->addTypeFormats("QString *", stringFormats);
    wh->addTypeFormats("QByteArray", stringFormats);
    wh->addTypeFormats("QByteArray *", stringFormats);
637
    wh->addTypeFormats("std__basic_string", stringFormats);  // Python dumper naming convention for std::[w]string
638
639
640
641
    const QStringList imageFormats = QStringList()
        << normalFormat << tr("Image");
    wh->addTypeFormats("QImage", imageFormats);
    wh->addTypeFormats("QImage *", imageFormats);
642
643
644
645
646
647
}

bool CdbEngine::launchCDB(const DebuggerStartParameters &sp, QString *errorMessage)
{
    if (debug)
        qDebug("launchCDB startMode=%d", sp.startMode);
648
    const QChar blank(QLatin1Char(' '));
649
    // Start engine which will run until initial breakpoint:
650
    // Determine binary (force MSVC), extension lib name and path to use
651
652
    // The extension is passed as relative name with the path variable set
    //(does not work with absolute path names)
hjk's avatar
hjk committed
653
    const QString executable = sp.debuggerCommand;
654
655
656
657
658
    if (executable.isEmpty()) {
        *errorMessage = tr("There is no CDB executable specified.");
        return false;
    }

659
    m_cdbIs64Bit =
660
661
662
663
664
#ifdef Q_OS_WIN
            Utils::winIs64BitBinary(executable);
#else
            false;
#endif
665
666
    if (!m_cdbIs64Bit)
        m_wow64State = noWow64Stack;
667
    const QFileInfo extensionFi(CdbEngine::extensionLibraryName(m_cdbIs64Bit));
668
669
670
671
672
    if (!extensionFi.isFile()) {
        *errorMessage = QString::fromLatin1("Internal error: The extension %1 cannot be found.").
                arg(QDir::toNativeSeparators(extensionFi.absoluteFilePath()));
        return false;
    }
673
    const QString extensionFileName = extensionFi.fileName();
674
675
    // Prepare arguments
    QStringList arguments;
676
    const bool isRemote = sp.startMode == AttachToRemoteServer;
677
678
679
680
681
    if (isRemote) { // Must be first
        arguments << QLatin1String("-remote") << sp.remoteChannel;
    } else {
        arguments << (QLatin1String("-a") + extensionFileName);
    }
682
683
684
685
    // Source line info/No terminal breakpoint / Pull extension
    arguments << QLatin1String("-lines") << QLatin1String("-G")
    // register idle (debuggee stop) notification
              << QLatin1String("-c")
686
              << QLatin1String(".idle_cmd ") + QString::fromLatin1(m_extensionCommandPrefixBA) + QLatin1String("idle");
687
688
    if (sp.useTerminal) // Separate console
        arguments << QLatin1String("-2");
689
    if (debuggerCore()->boolSetting(IgnoreFirstChanceAccessViolation))
690
        arguments << QLatin1String("-x");
691
692
693
694
695
696
697
698

    const QStringList &symbolPaths = debuggerCore()->stringListSetting(CdbSymbolPaths);
    if (!symbolPaths.isEmpty())
        arguments << QLatin1String("-y") << symbolPaths.join(QString(QLatin1Char(';')));
    const QStringList &sourcePaths = debuggerCore()->stringListSetting(CdbSourcePaths);
    if (!sourcePaths.isEmpty())
        arguments << QLatin1String("-srcpath") << sourcePaths.join(QString(QLatin1Char(';')));

699
    // Compile argument string preserving quotes
700
    QString nativeArguments = debuggerCore()->stringSetting(CdbAdditionalArguments);
701
702
703
    switch (sp.startMode) {
    case StartInternal:
    case StartExternal:
704
705
        if (!nativeArguments.isEmpty())
            nativeArguments.push_back(blank);
706
        QtcProcess::addArgs(&nativeArguments, QStringList(QDir::toNativeSeparators(sp.executable)));
707
        break;
708
    case AttachToRemoteServer:
709
        break;
710
    case AttachExternal:
711
    case AttachCrashedExternal:
712
        arguments << QLatin1String("-p") << QString::number(sp.attachPID);
713
        if (sp.startMode == AttachCrashedExternal) {
714
            arguments << QLatin1String("-e") << sp.crashParameter << QLatin1String("-g");
715
        } else {
716
            if (isCreatorConsole(startParameters()))
717
718
                arguments << QLatin1String("-pr") << QLatin1String("-pb");
        }
719
        break;
720
721
722
    case AttachCore:
        arguments << QLatin1String("-z") << sp.coreFile;
        break;
723
724
725
726
    default:
        *errorMessage = QString::fromLatin1("Internal error: Unsupported start mode %1.").arg(sp.startMode);
        return false;
    }
727
728
729
730
731
732
    if (!sp.processArgs.isEmpty()) { // Complete native argument string.
        if (!nativeArguments.isEmpty())
            nativeArguments.push_back(blank);
        nativeArguments += sp.processArgs;
    }

733
734
    const QString msg = QString::fromLatin1("Launching %1 %2\nusing %3 of %4.").
            arg(QDir::toNativeSeparators(executable),
735
                arguments.join(QString(blank)) + blank + nativeArguments,
736
737
738
739
740
                QDir::toNativeSeparators(extensionFi.absoluteFilePath()),
                extensionFi.lastModified().toString(Qt::SystemLocaleShortDate));
    showMessage(msg, LogMisc);

    m_outputBuffer.clear();
741
742
743
744
    const QStringList environment = sp.environment.size() == 0 ?
                                    QProcessEnvironment::systemEnvironment().toStringList() :
                                    sp.environment.toStringList();
    m_process.setEnvironment(mergeEnvironment(environment, extensionFi.absolutePath()));
745
746
    if (!sp.workingDirectory.isEmpty())
        m_process.setWorkingDirectory(sp.workingDirectory);
747

748
#ifdef Q_OS_WIN
749
750
    if (!nativeArguments.isEmpty()) // Appends
        m_process.setNativeArguments(nativeArguments);
751
#endif
752
753
754
755
756
757
    m_process.start(executable, arguments);
    if (!m_process.waitForStarted()) {
        *errorMessage = QString::fromLatin1("Internal error: Cannot start process %1: %2").
                arg(QDir::toNativeSeparators(executable), m_process.errorString());
        return false;
    }
hjk's avatar
hjk committed
758
759

    const unsigned long pid = Utils::qPidToPid(m_process.pid());
760
761
762
    showMessage(QString::fromLatin1("%1 running as %2").
                arg(QDir::toNativeSeparators(executable)).arg(pid), LogMisc);
    m_hasDebuggee = true;
763
764
765
766
767
    if (isRemote) { // We do not get an 'idle' in a remote session, but are accessible
        m_accessible = true;
        const QByteArray loadCommand = QByteArray(".load ")
                + extensionFileName.toLocal8Bit();
        postCommand(loadCommand, 0);
768
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineSetupOk")
769
770
        notifyEngineSetupOk();
    }
771
772
773
774
775
    return true;
}

void CdbEngine::setupInferior()
{
776
777
    if (debug)
        qDebug("setupInferior");
778
779
780
781
782
783
    // QmlCppEngine expects the QML engine to be connected before any breakpoints are hit
    // (attemptBreakpointSynchronization() will be directly called then)
    attemptBreakpointSynchronization();
    if (startParameters().breakOnMain) {
        const BreakpointParameters bp(BreakpointAtMain);
        postCommand(cdbAddBreakpointCommand(bp, m_sourcePathMappings,
784
                                            BreakpointModelId(quint16(-1)), true), 0);
785
    }
786
    postCommand("sxn 0x4000001f", 0); // Do not break on WowX86 exceptions.
787
    postCommand(".asm source_line", 0); // Source line in assembly
788
789
790
791
792
    postCommand(m_extensionCommandPrefixBA + "setparameter maxStringLength="
                + debuggerCore()->action(MaximalStringLength)->value().toByteArray()
                + " maxStackDepth="
                + debuggerCore()->action(MaximalStackDepth)->value().toByteArray()
                , 0);
793
794
795
    postExtensionCommand("pid", QByteArray(), 0, &CdbEngine::handlePid);
}

796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
static QByteArray msvcRunTime(const Abi::OSFlavor flavour)
{
    switch (flavour)  {
    case Abi::WindowsMsvc2005Flavor:
        return "MSVCR80";
    case Abi::WindowsMsvc2008Flavor:
        return "MSVCR90";
    case Abi::WindowsMsvc2010Flavor:
        return "MSVCR100";
    case Abi::WindowsMsvc2012Flavor:
        return "MSVCR110"; // #FIXME: VS2012 beta, will probably be 12 in final?
    default:
        break;
    }
    return "MSVCRT"; // MinGW, others.
}

static QByteArray breakAtFunctionCommand(const QByteArray &function,
                                         const QByteArray &module = QByteArray())
{
     QByteArray result = "bu ";
     if (!module.isEmpty()) {
         result += module;
         result += '!';
     }
     result += function;
     return result;
}

825
826
void CdbEngine::runEngine()
{
827
828
    if (debug)
        qDebug("runEngine");
829
830
831
832
833

    const QStringList &breakEvents =
            debuggerCore()->stringListSetting(CdbBreakEvents);
    foreach (const QString &breakEvent, breakEvents)
        postCommand(QByteArray("sxe ") + breakEvent.toLatin1(), 0);
834
835
    // Break functions: each function must be fully qualified,
    // else the debugger will slow down considerably.
836
837
838
839
840
841
842
843
    if (debuggerCore()->boolSetting(CdbBreakOnCrtDbgReport)) {
        const QByteArray module = msvcRunTime(startParameters().toolChainAbi.osFlavor());
        const QByteArray debugModule = module + 'D';
        const QByteArray wideFunc = CdbOptionsPage::crtDbgReport + 'W';
        postCommand(breakAtFunctionCommand(CdbOptionsPage::crtDbgReport, module), 0);
        postCommand(breakAtFunctionCommand(wideFunc, module), 0);
        postCommand(breakAtFunctionCommand(CdbOptionsPage::crtDbgReport, debugModule), 0);
        postCommand(breakAtFunctionCommand(wideFunc, debugModule), 0);
844
    }
845
846
847
848
849
850
851
852
    if (debuggerCore()->boolSetting(BreakOnWarning)) {
        postCommand("bm /( QtCored4!qWarning", 0); // 'bm': All overloads.
        postCommand("bm /( Qt5Cored!QMessageLogger::warning", 0);
    }
    if (debuggerCore()->boolSetting(BreakOnFatal)) {
        postCommand("bm /( QtCored4!qFatal", 0); // 'bm': All overloads.
        postCommand("bm /( Qt5Cored!QMessageLogger::fatal", 0);
    }
853
854
855
856
857
858
859
    if (startParameters().startMode == AttachCore) {
        QTC_ASSERT(!m_coreStopReason.isNull(), return; );
        notifyInferiorUnrunnable();
        processStop(*m_coreStopReason, false);
    } else {
        postCommand("g", 0);
    }
860
861
}

862
863
864
865
866
bool CdbEngine::commandsPending() const
{
    return !m_builtinCommandQueue.isEmpty() || !m_extensionCommandQueue.isEmpty();
}

867
868
869
870
871
872
873
874
875
void CdbEngine::shutdownInferior()
{
    if (debug)
        qDebug("CdbEngine::shutdownInferior in state '%s', process running %d", stateName(state()),
               isCdbProcessRunning());

    if (!isCdbProcessRunning()) { // Direct launch: Terminated with process.
        if (debug)
            qDebug("notifyInferiorShutdownOk");
876
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyInferiorShutdownOk")
877
878
879
        notifyInferiorShutdownOk();
        return;
    }
880

881
882
    if (m_accessible) { // except console.
        if (startParameters().startMode == AttachExternal || startParameters().startMode == AttachCrashedExternal)
883
            detachDebugger();
884
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyInferiorShutdownOk")
885
886
        notifyInferiorShutdownOk();
    } else {
887
888
889
890
891
892
893
894
895
896
897
898
899
        // A command got stuck.
        if (commandsPending()) {
            showMessage(QLatin1String("Cannot shut down inferior due to pending commands."), LogWarning);
            STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyInferiorShutdownFailed")
            notifyInferiorShutdownFailed();
            return;
        }
        if (!canInterruptInferior()) {
            showMessage(QLatin1String("Cannot interrupt the inferior."), LogWarning);
            STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyInferiorShutdownFailed")
            notifyInferiorShutdownFailed();
            return;
        }
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
        interruptInferior(); // Calls us again
    }
}

/* shutdownEngine/processFinished:
 * Note that in the case of launching a process by the debugger, the debugger
 * automatically quits a short time after reporting the session becoming
 * inaccessible without debuggee (notifyInferiorExited). In that case,
 * processFinished() must not report any arbitrarily notifyEngineShutdownOk()
 * as not to confuse the state engine.
 */

void CdbEngine::shutdownEngine()
{
    if (debug)
915
916
917
918
        qDebug("CdbEngine::shutdownEngine in state '%s', process running %d,"
               "accessible=%d,commands pending=%d",
               stateName(state()), isCdbProcessRunning(), m_accessible,
               commandsPending());
919
920

    if (!isCdbProcessRunning()) { // Direct launch: Terminated with process.
921
        STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineShutdownOk")
922
923
924
925
        notifyEngineShutdownOk();
        return;
    }

926
    // No longer trigger anything from messages
927
    m_ignoreCdbOutput = true;
928
929
    // Go for kill if there are commands pending.
    if (m_accessible && !commandsPending()) {
930
931
        // detach (except console): Wait for debugger to finish.
        if (startParameters().startMode == AttachExternal || startParameters().startMode == AttachCrashedExternal)
932
            detachDebugger();
933
        // Remote requires a bit more force to quit.
934
        if (m_effectiveStartMode == AttachToRemoteServer) {
935
936
937
938
939
940
941
942
943
            postCommand(m_extensionCommandPrefixBA + "shutdownex", 0);
            postCommand("qq", 0);
        } else {
            postCommand("q", 0);
        }
        m_notifyEngineShutdownOnTermination = true;
        return;
    } else {
        // Remote process. No can do, currently
944
        m_notifyEngineShutdownOnTermination = true;
945
        SynchronousProcess::stopProcess(m_process);
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
        return;
    }
    // Lost debuggee, debugger should quit anytime now
    if (!m_hasDebuggee) {
        m_notifyEngineShutdownOnTermination = true;
        return;
    }
    interruptInferior();
}

void CdbEngine::processFinished()
{
    if (debug)
        qDebug("CdbEngine::processFinished %dms '%s' notify=%d (exit state=%d, ex=%d)",
               elapsedLogTime(), stateName(state()), m_notifyEngineShutdownOnTermination,
               m_process.exitStatus(), m_process.exitCode());

    const bool crashed = m_process.exitStatus() == QProcess::CrashExit;
964
    if (crashed)
965
        showMessage(tr("CDB crashed"), LogError); // not in your life.
966
    else
967
968
969
970
971
972
        showMessage(tr("CDB exited (%1)").arg(m_process.exitCode()), LogMisc);

    if (m_notifyEngineShutdownOnTermination) {
        if (crashed) {
            if (debug)
                qDebug("notifyEngineIll");
973
            STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineIll")
974
975
            notifyEngineIll();
        } else {
976
            STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineShutdownOk")
977
978
979
            notifyEngineShutdownOk();
        }
    } else {
980
981
982
983
984
985
986
987
988
        // The QML/CPP engine relies on the standard sequence of InferiorShutDown,etc.
        // Otherwise, we take a shortcut.
        if (isSlaveEngine()) {
            STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyInferiorExited")
            notifyInferiorExited();
        } else {
            STATE_DEBUG(state(), Q_FUNC_INFO, __LINE__, "notifyEngineSpontaneousShutdown")
            notifyEngineSpontaneousShutdown();
        }
989
990
991
992
993
994
995
996
    }
}

void CdbEngine::detachDebugger()
{
    postCommand(".detach", 0);
}

997
998
999
1000
1001
static inline bool isWatchIName(const QByteArray &iname)
{
    return iname.startsWith("watch");
}

1002
1003
void CdbEngine::updateWatchData(const WatchData &dataIn,
                                const WatchUpdateFlags & flags)
1004
{
1005
1006
1007
    if (debug || debugLocals || debugWatches)
        qDebug("CdbEngine::updateWatchData() %dms accessible=%d %s incr=%d: %s",
               elapsedLogTime(), m_accessible, stateName(state()),
1008
1009
1010
               flags.tryIncremental,
               qPrintable(dataIn.toString()));

1011
1012
1013
1014
1015
1016
1017
1018
    if (!m_accessible) // Add watch data while running?
        return;

    // New watch item?
    if (isWatchIName(dataIn.iname) && dataIn.isValueNeeded()) {
        QByteArray args;
        ByteArrayInputStream str(args);
        str << dataIn.iname << " \"" << dataIn.exp << '"';
1019
1020
1021
1022
        // Store the name since the CDB extension library
        // does not maintain the names of watches.
        if (!dataIn.name.isEmpty() && dataIn.name != QLatin1String(dataIn.exp))
            m_watchInameToName.insert(dataIn.iname, dataIn.name);
1023
1024
1025
1026
1027
1028
1029
        postExtensionCommand("addwatch", args, 0,
                             &CdbEngine::handleAddWatch, 0,
                             qVariantFromValue(dataIn));
        return;
    }

    if (!dataIn.hasChildren && !dataIn.isValueNeeded()) {
1030
        WatchData data = dataIn;
1031
1032
1033
1034
1035
1036
1037
        data.setAllUnneeded();
        watchHandler()->insertData(data);
        return;
    }
    updateLocalVariable(dataIn.iname);
}

1038
1039
1040
1041
1042
1043
1044
1045
1046
void CdbEngine::handleAddWatch(const CdbExtensionCommandPtr &reply)
{
    WatchData item = qvariant_cast<WatchData>(reply->cookie);
    if (debugWatches)
        qDebug() << "handleAddWatch ok="  << reply->success << item.iname;
    if (reply->success) {
        updateLocalVariable(item.iname);
    } else {
        item.setError(tr("Unable to add expression"));
hjk's avatar
hjk committed
1047
        watchHandler()->insertIncompleteData(item);
1048
        showMessage(QString::fromLatin1("Unable to add watch item '%1'/'%2': %3").
1049
                    arg(QString::fromLatin1(item.iname), QString::fromLatin1(item.exp),
1050
                        QString::fromLocal8Bit(reply->errorMessage)), LogError);
1051
1052
1053
    }
}

1054
1055
void CdbEngine::addLocalsOptions(ByteArrayInputStream &str) const
{
1056
1057
    if (debuggerCore()->boolSetting(VerboseLog))
        str << blankSeparator << "-v";
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
    if (debuggerCore()->boolSetting(UseDebuggingHelpers))
        str << blankSeparator << "-c";
    const QByteArray typeFormats = watchHandler()->typeFormatRequests();
    if (!typeFormats.isEmpty())
        str << blankSeparator << "-T " << typeFormats;
    const QByteArray individualFormats = watchHandler()->individualFormatRequests();
    if (!individualFormats.isEmpty())
        str << blankSeparator << "-I " << individualFormats;
}

1068
1069
void CdbEngine::updateLocalVariable(const QByteArray &iname)
{
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
    const bool isWatch = isWatchIName(iname);
    if (debugWatches)
        qDebug() << "updateLocalVariable watch=" << isWatch << iname;
    QByteArray localsArguments;
    ByteArrayInputStream str(localsArguments);
    addLocalsOptions(str);
    if (!isWatch) {
        const int stackFrame = stackHandler()->currentIndex();
        if (stackFrame < 0) {
            qWarning("Internal error; no stack frame in updateLocalVariable");
            return;
        }
        str << blankSeparator << stackFrame;
1083
    }
1084
    str << blankSeparator << iname;
hjk's avatar
hjk committed
1085
1086
1087
1088
    postExtensionCommand(isWatch ? "watches" : "locals",
                         localsArguments, 0,
                         &CdbEngine::handleLocals,
                         0, QVariant(int(PartialLocalsUpdate)));
1089
1090
}

hjk's avatar
hjk committed
1091
bool CdbEngine::hasCapability(unsigned cap) const
1092
{
hjk's avatar
hjk committed
1093
1094
    return cap & (DisassemblerCapability | RegisterCapability
           | ShowMemoryCapability
1095
           |WatchpointByAddressCapability|JumpToLineCapability|AddWatcherCapability|WatchWidgetsCapability
1096
           |ReloadModuleCapability
1097
           |BreakOnThrowAndCatchCapability // Sort-of: Can break on throw().
1098
           |BreakConditionCapability|TracePointCapability