Files
qt-creator/src/libs/qtcreatorcdbext/eventcallback.cpp

302 lines
9.9 KiB
C++
Raw Normal View History

/**************************************************************************
**
** This file is part of Qt Creator
**
2011-01-11 16:28:15 +01:00
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
2010-12-17 17:14:20 +01:00
** No Commercial Usage
**
2010-12-17 17:14:20 +01:00
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
2010-12-17 17:14:20 +01:00
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "eventcallback.h"
#include "extensioncontext.h"
#include "stringutils.h"
#include "gdbmihelpers.h"
static const char eventContextC[] = "event";
// Special exception codes (see dbgwinutils.cpp).
enum { winExceptionCppException = 0xe06d7363,
winExceptionStartupCompleteTrap = 0x406d1388,
winExceptionRpcServerUnavailable = 0x6ba,
winExceptionRpcServerInvalid = 0x6a6,
winExceptionDllNotFound = 0xc0000135,
winExceptionDllEntryPointNoFound = 0xc0000139,
winExceptionDllInitFailed = 0xc0000142,
winExceptionMissingSystemFile = 0xc0000143,
winExceptionAppInitFailed = 0xc0000143
};
EventCallback::EventCallback(IDebugEventCallbacks *wrapped) :
m_wrapped(wrapped)
{
}
EventCallback::~EventCallback()
{
}
STDMETHODIMP EventCallback::QueryInterface(
THIS_
IN REFIID InterfaceId,
OUT PVOID* Interface)
{
*Interface = NULL;
if (IsEqualIID(InterfaceId, __uuidof(IUnknown)) ||
IsEqualIID(InterfaceId, __uuidof(IDebugOutputCallbacks))) {
*Interface = (IDebugOutputCallbacks *)this;
AddRef();
return S_OK;
} else {
return E_NOINTERFACE;
}
}
STDMETHODIMP_(ULONG) EventCallback::AddRef(THIS)
{
// This class is designed to be static so
// there's no true refcount.
return 1;
}
STDMETHODIMP_(ULONG) EventCallback::Release(THIS)
{
// This class is designed to be static so
// there's no true refcount.
return 0;
}
STDMETHODIMP EventCallback::GetInterestMask(THIS_ __out PULONG mask)
{
if (m_wrapped)
m_wrapped->GetInterestMask(mask);
*mask |= DEBUG_EVENT_CREATE_PROCESS | DEBUG_EVENT_EXIT_PROCESS
| DEBUG_EVENT_BREAKPOINT
| DEBUG_EVENT_EXCEPTION | DEBUG_EVENT_LOAD_MODULE | DEBUG_EVENT_UNLOAD_MODULE;
return S_OK;
}
STDMETHODIMP EventCallback::Breakpoint(THIS_ __in PDEBUG_BREAKPOINT b)
{
// Breakpoint hit - Set the stop reason parameters on the extension context.
typedef ExtensionContext::StopReasonMap StopReasonMap;
typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
ULONG id = 0;
ULONG64 address = 0;
b->GetId(&id);
b->GetOffset(&address);
StopReasonMap stopReason;
stopReason.insert(StopReasonMapValue(std::string("breakpointId"), toString(id)));
if (address)
stopReason.insert(StopReasonMapValue(std::string("breakpointAddress"), toString(address)));
ExtensionContext::instance().setStopReason(stopReason, "breakpoint");
return m_wrapped ? m_wrapped->Breakpoint(b) : S_OK;
}
static inline ExtensionContext::StopReasonMap exceptionParameters(const EXCEPTION_RECORD64 &e,
unsigned firstChance)
{
typedef ExtensionContext::StopReasonMap StopReasonMap;
typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
// Fill exception record
StopReasonMap parameters;
parameters.insert(StopReasonMapValue(std::string("firstChance"), toString(firstChance)));
parameters.insert(StopReasonMapValue(std::string("exceptionAddress"),
toString(e.ExceptionAddress)));
parameters.insert(StopReasonMapValue(std::string("exceptionCode"),
toString(e.ExceptionCode)));
parameters.insert(StopReasonMapValue(std::string("exceptionFlags"),
toString(e.ExceptionFlags)));
// Hard code some parameters (used for access violations)
if (e.NumberParameters >= 1)
parameters.insert(StopReasonMapValue(std::string("exceptionInformation0"),
toString(e.ExceptionInformation[0])));
if (e.NumberParameters >= 2)
parameters.insert(StopReasonMapValue(std::string("exceptionInformation1"),
toString(e.ExceptionInformation[1])));
// Add top stack frame if possible
StackFrame frame;
std::string errorMessage;
// If it is a C++ exception, get frame #2 (first outside MSVC runtime)
const unsigned frameNumber = e.ExceptionCode == winExceptionCppException ? 2 : 0;
if (getFrame(frameNumber, &frame, &errorMessage)) {
if (!frame.fullPathName.empty()) {
parameters.insert(StopReasonMapValue(std::string("exceptionFile"),
wStringToString(frame.fullPathName)));
parameters.insert(StopReasonMapValue(std::string("exceptionLine"),
toString(frame.line)));
}
if (!frame.function.empty())
parameters.insert(StopReasonMapValue(std::string("exceptionFunction"),
wStringToString(frame.function)));
} // getCurrentFrame
return parameters;
}
STDMETHODIMP EventCallback::Exception(
THIS_
__in PEXCEPTION_RECORD64 Ex,
__in ULONG FirstChance
)
{
// Report the exception as GBMI and set potential stop reason
const ExtensionContext::StopReasonMap parameters =
exceptionParameters(*Ex, FirstChance);
std::ostringstream str;
formatGdbmiHash(str, parameters);
ExtensionContext::instance().setStopReason(parameters, ExtensionContext::breakPointStopReasonC);
ExtensionContext::instance().report('E', 0, 0, "exception", "%s", str.str().c_str());
return m_wrapped ? m_wrapped->Exception(Ex, FirstChance) : S_OK;
}
STDMETHODIMP EventCallback::CreateThread(
THIS_
__in ULONG64 Handle,
__in ULONG64 DataOffset,
__in ULONG64 StartOffset
)
{
return m_wrapped ? m_wrapped->CreateThread(Handle, DataOffset, StartOffset) : S_OK;
}
STDMETHODIMP EventCallback::ExitThread(
THIS_
__in ULONG ExitCode
)
{
return m_wrapped ? m_wrapped->ExitThread(ExitCode) : S_OK;
}
STDMETHODIMP EventCallback::CreateProcess(
THIS_
__in ULONG64 ImageFileHandle,
__in ULONG64 Handle,
__in ULONG64 BaseOffset,
__in ULONG ModuleSize,
__in_opt PCSTR ModuleName,
__in_opt PCSTR ImageName,
__in ULONG CheckSum,
__in ULONG TimeDateStamp,
__in ULONG64 InitialThreadHandle,
__in ULONG64 ThreadDataOffset,
__in ULONG64 StartOffset
)
{
return m_wrapped ? m_wrapped->CreateProcess(ImageFileHandle, Handle,
BaseOffset, ModuleSize, ModuleName,
ImageName, CheckSum, TimeDateStamp,
InitialThreadHandle, ThreadDataOffset,
StartOffset) : S_OK;
}
STDMETHODIMP EventCallback::ExitProcess(
THIS_
__in ULONG ExitCode
)
{
ExtensionContext::instance().report('E', 0, 0, eventContextC, "Process exited (%lu)",
ExitCode);
const HRESULT hr = m_wrapped ? m_wrapped->ExitProcess(ExitCode) : S_OK;
// Remotely debugged process exited, there is no session-inactive notification.
// Note: We get deleted here, so, order is important.
ExtensionContext::instance().unhookCallbacks();
return hr;
}
STDMETHODIMP EventCallback::LoadModule(
THIS_
__in ULONG64 ImageFileHandle,
__in ULONG64 BaseOffset,
__in ULONG ModuleSize,
__in_opt PCSTR ModuleName,
__in_opt PCSTR ImageName,
__in ULONG CheckSum,
__in ULONG TimeDateStamp
)
{
return m_wrapped ? m_wrapped->LoadModule(ImageFileHandle, BaseOffset,
ModuleSize, ModuleName, ImageName,
CheckSum, TimeDateStamp) : S_OK;
}
STDMETHODIMP EventCallback::UnloadModule(
THIS_
__in_opt PCSTR ImageBaseName,
__in ULONG64 BaseOffset
)
{
return m_wrapped ? m_wrapped->UnloadModule(ImageBaseName, BaseOffset) : S_OK;
}
STDMETHODIMP EventCallback::SystemError(
THIS_
__in ULONG Error,
__in ULONG Level
)
{
return m_wrapped ? m_wrapped->SystemError(Error, Level) : S_OK;
}
STDMETHODIMP EventCallback::SessionStatus(
THIS_
__in ULONG Status
)
{
return m_wrapped ? m_wrapped->SessionStatus(Status) : S_OK;
}
STDMETHODIMP EventCallback::ChangeDebuggeeState(
THIS_
__in ULONG Flags,
__in ULONG64 Argument
)
{
return m_wrapped ? m_wrapped->ChangeDebuggeeState(Flags, Argument) : S_OK;
}
STDMETHODIMP EventCallback::ChangeEngineState(
THIS_
__in ULONG Flags,
__in ULONG64 Argument
)
{
return m_wrapped ? m_wrapped->ChangeEngineState(Flags, Argument) : S_OK;
}
STDMETHODIMP EventCallback::ChangeSymbolState(
THIS_
__in ULONG Flags,
__in ULONG64 Argument
)
{
return m_wrapped ? m_wrapped->ChangeSymbolState(Flags, Argument) : S_OK;
}