2009-03-26 16:49:28 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2009-03-26 16:49:28 +01:00
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2009-03-26 16:49:28 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cdbstacktracecontext.h"
|
2009-07-13 09:11:07 +02:00
|
|
|
#include "cdbstackframecontext.h"
|
2009-04-07 17:07:11 +02:00
|
|
|
#include "cdbbreakpoint.h"
|
2009-03-26 16:49:28 +01:00
|
|
|
#include "cdbsymbolgroupcontext.h"
|
|
|
|
|
#include "cdbdebugengine_p.h"
|
2009-04-29 14:15:09 +02:00
|
|
|
#include "cdbdumperhelper.h"
|
2009-03-26 16:49:28 +01:00
|
|
|
|
2009-04-07 17:07:11 +02:00
|
|
|
#include <QtCore/QDir>
|
2009-04-21 12:30:12 +02:00
|
|
|
#include <QtCore/QTextStream>
|
2009-04-07 17:07:11 +02:00
|
|
|
|
2009-03-26 16:49:28 +01:00
|
|
|
namespace Debugger {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2009-10-06 15:50:48 +02:00
|
|
|
const char *CdbStackTraceContext::winFuncFastSystemCallRet = "ntdll!KiFastSystemCallRet";
|
|
|
|
|
const char *CdbStackTraceContext::winFuncDebugBreakPoint = "ntdll!DbgBreakPoint";
|
|
|
|
|
const char *CdbStackTraceContext::winFuncWaitForPrefix = "kernel32!WaitFor";
|
|
|
|
|
|
2009-04-29 14:15:09 +02:00
|
|
|
CdbStackTraceContext::CdbStackTraceContext(const QSharedPointer<CdbDumperHelper> &dumper) :
|
|
|
|
|
m_dumper(dumper),
|
|
|
|
|
m_cif(dumper->comInterfaces()),
|
2009-04-14 15:04:19 +02:00
|
|
|
m_instructionOffset(0)
|
2009-03-26 16:49:28 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 14:15:09 +02:00
|
|
|
CdbStackTraceContext *CdbStackTraceContext::create(const QSharedPointer<CdbDumperHelper> &dumper,
|
2009-03-26 16:49:28 +01:00
|
|
|
unsigned long threadId,
|
|
|
|
|
QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
if (debugCDB)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << threadId;
|
|
|
|
|
// fill the DEBUG_STACK_FRAME array
|
|
|
|
|
ULONG frameCount;
|
2009-04-29 14:15:09 +02:00
|
|
|
CdbStackTraceContext *ctx = new CdbStackTraceContext(dumper);
|
2009-09-29 17:33:51 +02:00
|
|
|
const HRESULT hr = dumper->comInterfaces()->debugControl->GetStackTrace(0, 0, 0, ctx->m_cdbFrames, CdbStackTraceContext::maxFrames, &frameCount);
|
2009-03-26 16:49:28 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
delete ctx;
|
|
|
|
|
*errorMessage = msgComFailed("GetStackTrace", hr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (!ctx->init(frameCount, errorMessage)) {
|
|
|
|
|
delete ctx;
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CdbStackTraceContext::~CdbStackTraceContext()
|
|
|
|
|
{
|
2009-04-29 14:15:09 +02:00
|
|
|
qDeleteAll(m_frameContexts);
|
2009-03-26 16:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CdbStackTraceContext::init(unsigned long frameCount, QString * /*errorMessage*/)
|
|
|
|
|
{
|
|
|
|
|
if (debugCDB)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << frameCount;
|
|
|
|
|
|
2009-04-29 14:15:09 +02:00
|
|
|
m_frameContexts.resize(frameCount);
|
2009-07-13 09:11:07 +02:00
|
|
|
qFill(m_frameContexts, static_cast<CdbStackFrameContext*>(0));
|
2009-03-26 16:49:28 +01:00
|
|
|
|
|
|
|
|
// Convert the DEBUG_STACK_FRAMEs to our StackFrame structure and populate the frames
|
|
|
|
|
WCHAR wszBuf[MAX_PATH];
|
|
|
|
|
for (ULONG i=0; i < frameCount; ++i) {
|
2009-10-02 15:17:50 +02:00
|
|
|
StackFrame frame;
|
|
|
|
|
frame.level = i;
|
2009-03-26 16:49:28 +01:00
|
|
|
const ULONG64 instructionOffset = m_cdbFrames[i].InstructionOffset;
|
2009-04-14 15:04:19 +02:00
|
|
|
if (i == 0)
|
|
|
|
|
m_instructionOffset = instructionOffset;
|
2009-03-26 16:49:28 +01:00
|
|
|
frame.address = QString::fromLatin1("0x%1").arg(instructionOffset, 0, 16);
|
|
|
|
|
|
2009-04-21 12:30:12 +02:00
|
|
|
m_cif->debugSymbols->GetNameByOffsetWide(instructionOffset, wszBuf, MAX_PATH, 0, 0);
|
2009-05-30 23:03:43 +09:00
|
|
|
frame.function = QString::fromUtf16(reinterpret_cast<const ushort *>(wszBuf));
|
2009-03-26 16:49:28 +01:00
|
|
|
|
|
|
|
|
ULONG ulLine;
|
|
|
|
|
ULONG64 ul64Displacement;
|
2009-04-21 12:30:12 +02:00
|
|
|
const HRESULT hr = m_cif->debugSymbols->GetLineByOffsetWide(instructionOffset, &ulLine, wszBuf, MAX_PATH, 0, &ul64Displacement);
|
2009-03-26 16:49:28 +01:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
|
frame.line = ulLine;
|
2009-04-07 17:07:11 +02:00
|
|
|
// Vitally important to use canonical file that matches editormanager,
|
|
|
|
|
// else the marker will not show.
|
2009-05-30 23:03:43 +09:00
|
|
|
frame.file = CDBBreakPoint::canonicalSourceFile(QString::fromUtf16(reinterpret_cast<const ushort *>(wszBuf)));
|
2009-03-26 16:49:28 +01:00
|
|
|
}
|
|
|
|
|
m_frames.push_back(frame);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-14 14:29:37 +02:00
|
|
|
int CdbStackTraceContext::indexOf(const QString &function) const
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
const QChar exclamationMark = QLatin1Char('!');
|
|
|
|
|
const int count = m_frames.size();
|
|
|
|
|
// Module contained ('module!foo'). Exact match
|
|
|
|
|
if (function.contains(exclamationMark)) {
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
if (m_frames.at(i).function == function)
|
|
|
|
|
return i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// No module, fuzzy match
|
|
|
|
|
QString pattern = exclamationMark + function;
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
if (m_frames.at(i).function.endsWith(pattern))
|
|
|
|
|
return i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-20 14:20:38 +02:00
|
|
|
static inline QString msgFrameContextFailed(int index, const StackFrame &f, const QString &why)
|
|
|
|
|
{
|
|
|
|
|
return QString::fromLatin1("Unable to create stack frame context #%1, %2:%3 (%4): %5").
|
|
|
|
|
arg(index).arg(f.function).arg(f.line).arg(f.file, why);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-13 09:11:07 +02:00
|
|
|
CdbStackFrameContext *CdbStackTraceContext::frameContextAt(int index, QString *errorMessage)
|
2009-03-26 16:49:28 +01:00
|
|
|
{
|
2009-04-29 14:15:09 +02:00
|
|
|
// Create a frame on demand
|
2009-03-26 16:49:28 +01:00
|
|
|
if (debugCDB)
|
2009-04-07 17:07:11 +02:00
|
|
|
qDebug() << Q_FUNC_INFO << index;
|
2009-03-26 16:49:28 +01:00
|
|
|
|
2009-04-29 14:15:09 +02:00
|
|
|
if (index < 0 || index >= m_frameContexts.size()) {
|
2009-03-26 16:49:28 +01:00
|
|
|
*errorMessage = QString::fromLatin1("%1: Index %2 out of range %3.").
|
2009-04-29 14:15:09 +02:00
|
|
|
arg(QLatin1String(Q_FUNC_INFO)).arg(index).arg(m_frameContexts.size());
|
2009-03-26 16:49:28 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2009-04-29 14:15:09 +02:00
|
|
|
if (m_frameContexts.at(index))
|
|
|
|
|
return m_frameContexts.at(index);
|
2009-04-21 12:30:12 +02:00
|
|
|
CIDebugSymbolGroup *sg = createSymbolGroup(index, errorMessage);
|
2009-05-20 14:20:38 +02:00
|
|
|
if (!sg) {
|
|
|
|
|
*errorMessage = msgFrameContextFailed(index, m_frames.at(index), *errorMessage);
|
2009-03-26 16:49:28 +01:00
|
|
|
return 0;
|
2009-05-20 14:20:38 +02:00
|
|
|
}
|
2009-03-27 17:19:39 +01:00
|
|
|
CdbSymbolGroupContext *sc = CdbSymbolGroupContext::create(QLatin1String("local"), sg, errorMessage);
|
2009-05-20 14:20:38 +02:00
|
|
|
if (!sc) {
|
|
|
|
|
*errorMessage = msgFrameContextFailed(index, m_frames.at(index), *errorMessage);
|
2009-04-29 14:15:09 +02:00
|
|
|
return 0;
|
2009-05-20 14:20:38 +02:00
|
|
|
}
|
2009-07-13 09:11:07 +02:00
|
|
|
CdbStackFrameContext *fr = new CdbStackFrameContext(m_dumper, sc);
|
|
|
|
|
m_frameContexts[index] = fr;
|
|
|
|
|
return fr;
|
2009-03-26 16:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
2009-04-21 12:30:12 +02:00
|
|
|
CIDebugSymbolGroup *CdbStackTraceContext::createSymbolGroup(int index, QString *errorMessage)
|
2009-03-26 16:49:28 +01:00
|
|
|
{
|
2009-04-21 12:30:12 +02:00
|
|
|
CIDebugSymbolGroup *sg = 0;
|
|
|
|
|
HRESULT hr = m_cif->debugSymbols->GetScopeSymbolGroup2(DEBUG_SCOPE_GROUP_LOCALS, NULL, &sg);
|
2009-03-26 16:49:28 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage = msgComFailed("GetScopeSymbolGroup", hr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-21 12:30:12 +02:00
|
|
|
hr = m_cif->debugSymbols->SetScope(0, m_cdbFrames + index, NULL, 0);
|
2009-03-26 16:49:28 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage = msgComFailed("SetScope", hr);
|
|
|
|
|
sg->Release();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// refresh with current frame
|
2009-04-21 12:30:12 +02:00
|
|
|
hr = m_cif->debugSymbols->GetScopeSymbolGroup2(DEBUG_SCOPE_GROUP_LOCALS, sg, &sg);
|
2009-03-26 16:49:28 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage = msgComFailed("GetScopeSymbolGroup", hr);
|
|
|
|
|
sg->Release();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return sg;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-21 12:30:12 +02:00
|
|
|
QString CdbStackTraceContext::toString() const
|
|
|
|
|
{
|
|
|
|
|
QString rc;
|
|
|
|
|
QTextStream str(&rc);
|
|
|
|
|
format(str);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CdbStackTraceContext::format(QTextStream &str) const
|
|
|
|
|
{
|
|
|
|
|
const int count = m_frames.count();
|
|
|
|
|
const int defaultFieldWidth = str.fieldWidth();
|
|
|
|
|
const QTextStream::FieldAlignment defaultAlignment = str.fieldAlignment();
|
|
|
|
|
for (int f = 0; f < count; f++) {
|
|
|
|
|
const StackFrame &frame = m_frames.at(f);
|
|
|
|
|
const bool hasFile = !frame.file.isEmpty();
|
|
|
|
|
// left-pad level
|
|
|
|
|
str << qSetFieldWidth(6) << left << f;
|
|
|
|
|
str.setFieldWidth(defaultFieldWidth);
|
|
|
|
|
str.setFieldAlignment(defaultAlignment);
|
|
|
|
|
if (hasFile)
|
|
|
|
|
str << QDir::toNativeSeparators(frame.file) << ':' << frame.line << " (";
|
|
|
|
|
str << frame.function;
|
|
|
|
|
if (hasFile)
|
|
|
|
|
str << ')';
|
|
|
|
|
str << '\n';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 17:20:38 +02:00
|
|
|
// Thread state helper
|
|
|
|
|
|
|
|
|
|
static inline QString msgGetThreadStateFailed(unsigned long threadId, const QString &why)
|
|
|
|
|
{
|
|
|
|
|
return QString::fromLatin1("Unable to determine the state of thread %1: %2").arg(threadId).arg(why);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 17:30:45 +02:00
|
|
|
// Determine information about thread. This changes the
|
|
|
|
|
// current thread to thread->id.
|
2009-10-05 17:20:38 +02:00
|
|
|
static inline bool getStoppedThreadState(const CdbComInterfaces &cif,
|
|
|
|
|
ThreadData *t,
|
|
|
|
|
QString *errorMessage)
|
|
|
|
|
{
|
2009-10-06 15:50:48 +02:00
|
|
|
enum { MaxFrames = 2 };
|
2009-10-05 17:20:38 +02:00
|
|
|
ULONG currentThread;
|
|
|
|
|
HRESULT hr = cif.debugSystemObjects->GetCurrentThreadId(¤tThread);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage = msgGetThreadStateFailed(t->id, msgComFailed("GetCurrentThreadId", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (currentThread != t->id) {
|
|
|
|
|
hr = cif.debugSystemObjects->SetCurrentThreadId(t->id);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage = msgGetThreadStateFailed(t->id, msgComFailed("SetCurrentThreadId", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ULONG frameCount;
|
2009-10-06 15:50:48 +02:00
|
|
|
// Ignore the top frame if it is "ntdll!KiFastSystemCallRet", which is
|
|
|
|
|
// not interesting for display.
|
|
|
|
|
DEBUG_STACK_FRAME frames[MaxFrames];
|
|
|
|
|
hr = cif.debugControl->GetStackTrace(0, 0, 0, frames, MaxFrames, &frameCount);
|
2009-10-05 17:20:38 +02:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage = msgGetThreadStateFailed(t->id, msgComFailed("GetStackTrace", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-10-06 15:50:48 +02:00
|
|
|
// Ignore the top frame if it is "ntdll!KiFastSystemCallRet", which is
|
|
|
|
|
// not interesting for display.
|
2009-10-05 17:20:38 +02:00
|
|
|
WCHAR wszBuf[MAX_PATH];
|
2009-10-06 15:50:48 +02:00
|
|
|
for (int frame = 0; frame < MaxFrames; frame++) {
|
|
|
|
|
cif.debugSymbols->GetNameByOffsetWide(frames[frame].InstructionOffset, wszBuf, MAX_PATH, 0, 0);
|
|
|
|
|
t->function = QString::fromUtf16(reinterpret_cast<const ushort *>(wszBuf));
|
|
|
|
|
if (frame != 0 || t->function != QLatin1String(CdbStackTraceContext::winFuncFastSystemCallRet)) {
|
|
|
|
|
t->address = frames[frame].InstructionOffset;
|
|
|
|
|
cif.debugSymbols->GetNameByOffsetWide(frames[frame].InstructionOffset, wszBuf, MAX_PATH, 0, 0);
|
|
|
|
|
t->function = QString::fromUtf16(reinterpret_cast<const ushort *>(wszBuf));
|
|
|
|
|
ULONG ulLine;
|
|
|
|
|
hr = cif.debugSymbols->GetLineByOffsetWide(frames[frame].InstructionOffset, &ulLine, wszBuf, MAX_PATH, 0, 0);
|
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
|
t->line = ulLine;
|
|
|
|
|
// Just display base name
|
|
|
|
|
t->file = QString::fromUtf16(reinterpret_cast<const ushort *>(wszBuf));
|
|
|
|
|
if (!t->file.isEmpty()) {
|
|
|
|
|
const int slashPos = t->file.lastIndexOf(QLatin1Char('\\'));
|
|
|
|
|
if (slashPos != -1)
|
|
|
|
|
t->file.remove(0, slashPos + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} // was not "ntdll!KiFastSystemCallRet"
|
2009-10-05 17:20:38 +02:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline QString msgGetThreadsFailed(const QString &why)
|
|
|
|
|
{
|
|
|
|
|
return QString::fromLatin1("Unable to determine the thread information: %1").arg(why);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CdbStackTraceContext::getThreads(const CdbComInterfaces &cif,
|
|
|
|
|
bool isStopped,
|
|
|
|
|
QList<ThreadData> *threads,
|
|
|
|
|
ULONG *currentThreadId,
|
|
|
|
|
QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
threads->clear();
|
|
|
|
|
ULONG threadCount;
|
|
|
|
|
*currentThreadId = 0;
|
|
|
|
|
HRESULT hr= cif.debugSystemObjects->GetNumberThreads(&threadCount);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage= msgGetThreadsFailed(msgComFailed("GetNumberThreads", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Get ids and index of current
|
|
|
|
|
if (!threadCount)
|
|
|
|
|
return true;
|
|
|
|
|
hr = cif.debugSystemObjects->GetCurrentThreadId(currentThreadId);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage= msgGetThreadsFailed(msgComFailed("GetCurrentThreadId", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<ULONG> threadIds(threadCount);
|
|
|
|
|
hr = cif.debugSystemObjects->GetThreadIdsByIndex(0, threadCount, &(*threadIds.begin()), 0);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage= msgGetThreadsFailed(msgComFailed("GetThreadIdsByIndex", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (ULONG i = 0; i < threadCount; i++) {
|
|
|
|
|
ThreadData threadData(threadIds.at(i));
|
|
|
|
|
if (isStopped) {
|
|
|
|
|
if (!getStoppedThreadState(cif, &threadData, errorMessage)) {
|
|
|
|
|
qWarning("%s\n", qPrintable(*errorMessage));
|
|
|
|
|
errorMessage->clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
threads->push_back(threadData);
|
|
|
|
|
}
|
2009-10-05 17:30:45 +02:00
|
|
|
// Restore thread id
|
|
|
|
|
if (isStopped && threads->back().id != *currentThreadId) {
|
|
|
|
|
hr = cif.debugSystemObjects->SetCurrentThreadId(*currentThreadId);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
*errorMessage= msgGetThreadsFailed(msgComFailed("SetCurrentThreadId", hr));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-05 17:20:38 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-15 14:26:08 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Debugger
|