1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "eventcallback.h"
34 #include "extensioncontext.h"
35 #include "stringutils.h"
36 #include "gdbmihelpers.h"
38 static const char eventContextC[] = "event";
40 // Special exception codes (see dbgwinutils.cpp).
41 enum { winExceptionCppException = 0xe06d7363,
42 winExceptionStartupCompleteTrap = 0x406d1388,
43 winExceptionRpcServerUnavailable = 0x6ba,
44 winExceptionRpcServerInvalid = 0x6a6,
45 winExceptionDllNotFound = 0xc0000135,
46 winExceptionDllEntryPointNoFound = 0xc0000139,
47 winExceptionDllInitFailed = 0xc0000142,
48 winExceptionMissingSystemFile = 0xc0000143,
49 winExceptionAppInitFailed = 0xc0000143
53 \class IDebugEventCallbacks
55 Event handler wrapping the original IDebugEventCallbacks
56 to catch and store exceptions (report crashes as stop reasons).
57 \ingroup qtcreatorcdbext
60 EventCallback::EventCallback(IDebugEventCallbacks *wrapped) :
65 EventCallback::~EventCallback()
69 STDMETHODIMP EventCallback::QueryInterface(
71 IN REFIID InterfaceId,
76 if (IsEqualIID(InterfaceId, __uuidof(IUnknown)) ||
77 IsEqualIID(InterfaceId, __uuidof(IDebugOutputCallbacks))) {
78 *Interface = (IDebugOutputCallbacks *)this;
86 STDMETHODIMP_(ULONG) EventCallback::AddRef(THIS)
88 // This class is designed to be static so
89 // there's no true refcount.
93 STDMETHODIMP_(ULONG) EventCallback::Release(THIS)
95 // This class is designed to be static so
96 // there's no true refcount.
100 STDMETHODIMP EventCallback::GetInterestMask(THIS_ __out PULONG mask)
103 m_wrapped->GetInterestMask(mask);
105 *mask |= DEBUG_EVENT_CREATE_PROCESS | DEBUG_EVENT_EXIT_PROCESS
106 | DEBUG_EVENT_BREAKPOINT
107 | DEBUG_EVENT_EXCEPTION | DEBUG_EVENT_LOAD_MODULE | DEBUG_EVENT_UNLOAD_MODULE;
111 STDMETHODIMP EventCallback::Breakpoint(THIS_ __in PDEBUG_BREAKPOINT b)
113 // Breakpoint hit - Set the stop reason parameters on the extension context.
114 typedef ExtensionContext::StopReasonMap StopReasonMap;
115 typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
119 StopReasonMap stopReason;
120 if (SUCCEEDED(b->GetId(&uId)))
121 stopReason.insert(StopReasonMapValue(std::string("breakpointId"), toString(uId)));
122 if (SUCCEEDED(b->GetOffset(&address)))
123 stopReason.insert(StopReasonMapValue(std::string("breakpointAddress"), toString(address)));
124 ExtensionContext::instance().setStopReason(stopReason, ExtensionContext::breakPointStopReasonC);
125 return m_wrapped ? m_wrapped->Breakpoint(b) : S_OK;
128 static inline ExtensionContext::StopReasonMap exceptionParameters(const EXCEPTION_RECORD64 &e,
129 unsigned firstChance)
131 typedef ExtensionContext::StopReasonMap StopReasonMap;
132 typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
133 // Fill exception record
134 StopReasonMap parameters;
135 parameters.insert(StopReasonMapValue(std::string("firstChance"), toString(firstChance)));
136 parameters.insert(StopReasonMapValue(std::string("exceptionAddress"),
137 toString(e.ExceptionAddress)));
138 parameters.insert(StopReasonMapValue(std::string("exceptionCode"),
139 toString(e.ExceptionCode)));
140 parameters.insert(StopReasonMapValue(std::string("exceptionFlags"),
141 toString(e.ExceptionFlags)));
142 // Hard code some parameters (used for access violations)
143 if (e.NumberParameters >= 1)
144 parameters.insert(StopReasonMapValue(std::string("exceptionInformation0"),
145 toString(e.ExceptionInformation[0])));
146 if (e.NumberParameters >= 2)
147 parameters.insert(StopReasonMapValue(std::string("exceptionInformation1"),
148 toString(e.ExceptionInformation[1])));
149 // Add top stack frame if possible
151 std::string errorMessage;
152 // If it is a C++ exception, get frame #2 (first outside MSVC runtime)
153 const unsigned frameNumber = e.ExceptionCode == winExceptionCppException ? 2 : 0;
154 if (getFrame(frameNumber, &frame, &errorMessage)) {
155 if (!frame.fullPathName.empty()) {
156 parameters.insert(StopReasonMapValue(std::string("exceptionFile"),
157 wStringToString(frame.fullPathName)));
158 parameters.insert(StopReasonMapValue(std::string("exceptionLine"),
159 toString(frame.line)));
161 if (!frame.function.empty())
162 parameters.insert(StopReasonMapValue(std::string("exceptionFunction"),
163 wStringToString(frame.function)));
168 STDMETHODIMP EventCallback::Exception(
170 __in PEXCEPTION_RECORD64 Ex,
171 __in ULONG FirstChance
174 // Report the exception as GBMI and set potential stop reason
175 const ExtensionContext::StopReasonMap parameters =
176 exceptionParameters(*Ex, FirstChance);
178 std::ostringstream str;
179 formatGdbmiHash(str, parameters);
180 ExtensionContext::instance().setStopReason(parameters, "exception");
181 ExtensionContext::instance().report('E', 0, 0, "exception", "%s", str.str().c_str());
182 return m_wrapped ? m_wrapped->Exception(Ex, FirstChance) : S_OK;
185 STDMETHODIMP EventCallback::CreateThread(
188 __in ULONG64 DataOffset,
189 __in ULONG64 StartOffset
192 return m_wrapped ? m_wrapped->CreateThread(Handle, DataOffset, StartOffset) : S_OK;
195 STDMETHODIMP EventCallback::ExitThread(
200 return m_wrapped ? m_wrapped->ExitThread(ExitCode) : S_OK;
203 STDMETHODIMP EventCallback::CreateProcess(
205 __in ULONG64 ImageFileHandle,
207 __in ULONG64 BaseOffset,
208 __in ULONG ModuleSize,
209 __in_opt PCSTR ModuleName,
210 __in_opt PCSTR ImageName,
212 __in ULONG TimeDateStamp,
213 __in ULONG64 InitialThreadHandle,
214 __in ULONG64 ThreadDataOffset,
215 __in ULONG64 StartOffset
218 return m_wrapped ? m_wrapped->CreateProcess(ImageFileHandle, Handle,
219 BaseOffset, ModuleSize, ModuleName,
220 ImageName, CheckSum, TimeDateStamp,
221 InitialThreadHandle, ThreadDataOffset,
225 STDMETHODIMP EventCallback::ExitProcess(
230 ExtensionContext::instance().report('E', 0, 0, eventContextC, "Process exited (%lu)",
232 const HRESULT hr = m_wrapped ? m_wrapped->ExitProcess(ExitCode) : S_OK;
233 // Remotely debugged process exited, there is no session-inactive notification.
234 // Note: We get deleted here, so, order is important.
235 ExtensionContext::instance().unhookCallbacks();
239 STDMETHODIMP EventCallback::LoadModule(
241 __in ULONG64 ImageFileHandle,
242 __in ULONG64 BaseOffset,
243 __in ULONG ModuleSize,
244 __in_opt PCSTR ModuleName,
245 __in_opt PCSTR ImageName,
247 __in ULONG TimeDateStamp
250 return m_wrapped ? m_wrapped->LoadModule(ImageFileHandle, BaseOffset,
251 ModuleSize, ModuleName, ImageName,
252 CheckSum, TimeDateStamp) : S_OK;
255 STDMETHODIMP EventCallback::UnloadModule(
257 __in_opt PCSTR ImageBaseName,
258 __in ULONG64 BaseOffset
261 return m_wrapped ? m_wrapped->UnloadModule(ImageBaseName, BaseOffset) : S_OK;
264 STDMETHODIMP EventCallback::SystemError(
270 return m_wrapped ? m_wrapped->SystemError(Error, Level) : S_OK;
273 STDMETHODIMP EventCallback::SessionStatus(
278 return m_wrapped ? m_wrapped->SessionStatus(Status) : S_OK;
281 STDMETHODIMP EventCallback::ChangeDebuggeeState(
284 __in ULONG64 Argument
287 return m_wrapped ? m_wrapped->ChangeDebuggeeState(Flags, Argument) : S_OK;
290 STDMETHODIMP EventCallback::ChangeEngineState(
293 __in ULONG64 Argument
296 return m_wrapped ? m_wrapped->ChangeEngineState(Flags, Argument) : S_OK;
299 STDMETHODIMP EventCallback::ChangeSymbolState(
302 __in ULONG64 Argument
305 return m_wrapped ? m_wrapped->ChangeSymbolState(Flags, Argument) : S_OK;