1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "eventcallback.h"
35 #include "extensioncontext.h"
36 #include "stringutils.h"
37 #include "gdbmihelpers.h"
39 static const char eventContextC[] = "event";
41 // Special exception codes (see dbgwinutils.cpp).
42 enum { winExceptionCppException = 0xe06d7363,
43 winExceptionStartupCompleteTrap = 0x406d1388,
44 winExceptionRpcServerUnavailable = 0x6ba,
45 winExceptionRpcServerInvalid = 0x6a6,
46 winExceptionDllNotFound = 0xc0000135,
47 winExceptionDllEntryPointNoFound = 0xc0000139,
48 winExceptionDllInitFailed = 0xc0000142,
49 winExceptionMissingSystemFile = 0xc0000143,
50 winExceptionAppInitFailed = 0xc0000143
53 EventCallback::EventCallback(IDebugEventCallbacks *wrapped) :
58 EventCallback::~EventCallback()
62 STDMETHODIMP EventCallback::QueryInterface(
64 IN REFIID InterfaceId,
69 if (IsEqualIID(InterfaceId, __uuidof(IUnknown)) ||
70 IsEqualIID(InterfaceId, __uuidof(IDebugOutputCallbacks))) {
71 *Interface = (IDebugOutputCallbacks *)this;
79 STDMETHODIMP_(ULONG) EventCallback::AddRef(THIS)
81 // This class is designed to be static so
82 // there's no true refcount.
86 STDMETHODIMP_(ULONG) EventCallback::Release(THIS)
88 // This class is designed to be static so
89 // there's no true refcount.
93 STDMETHODIMP EventCallback::GetInterestMask(THIS_ __out PULONG mask)
96 m_wrapped->GetInterestMask(mask);
98 *mask |= DEBUG_EVENT_CREATE_PROCESS | DEBUG_EVENT_EXIT_PROCESS
99 | DEBUG_EVENT_BREAKPOINT
100 | DEBUG_EVENT_EXCEPTION | DEBUG_EVENT_LOAD_MODULE | DEBUG_EVENT_UNLOAD_MODULE;
104 STDMETHODIMP EventCallback::Breakpoint(THIS_ __in PDEBUG_BREAKPOINT b)
106 // Breakpoint hit - Set the stop reason parameters on the extension context.
107 typedef ExtensionContext::StopReasonMap StopReasonMap;
108 typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
113 b->GetOffset(&address);
115 StopReasonMap stopReason;
116 stopReason.insert(StopReasonMapValue(std::string("breakpointId"), toString(id)));
118 stopReason.insert(StopReasonMapValue(std::string("breakpointAddress"), toString(address)));
119 ExtensionContext::instance().setStopReason(stopReason, "breakpoint");
120 return m_wrapped ? m_wrapped->Breakpoint(b) : S_OK;
123 static inline ExtensionContext::StopReasonMap exceptionParameters(const EXCEPTION_RECORD64 &e,
124 unsigned firstChance)
126 typedef ExtensionContext::StopReasonMap StopReasonMap;
127 typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
128 // Fill exception record
129 StopReasonMap parameters;
130 parameters.insert(StopReasonMapValue(std::string("firstChance"), toString(firstChance)));
131 parameters.insert(StopReasonMapValue(std::string("exceptionAddress"),
132 toString(e.ExceptionAddress)));
133 parameters.insert(StopReasonMapValue(std::string("exceptionCode"),
134 toString(e.ExceptionCode)));
135 parameters.insert(StopReasonMapValue(std::string("exceptionFlags"),
136 toString(e.ExceptionFlags)));
137 // Hard code some parameters (used for access violations)
138 if (e.NumberParameters >= 1)
139 parameters.insert(StopReasonMapValue(std::string("exceptionInformation0"),
140 toString(e.ExceptionInformation[0])));
141 if (e.NumberParameters >= 2)
142 parameters.insert(StopReasonMapValue(std::string("exceptionInformation1"),
143 toString(e.ExceptionInformation[1])));
144 // Add top stack frame if possible
146 std::string errorMessage;
147 // If it is a C++ exception, get frame #2 (first outside MSVC runtime)
148 const unsigned frameNumber = e.ExceptionCode == winExceptionCppException ? 2 : 0;
149 if (getFrame(frameNumber, &frame, &errorMessage)) {
150 if (!frame.fullPathName.empty()) {
151 parameters.insert(StopReasonMapValue(std::string("exceptionFile"),
152 wStringToString(frame.fullPathName)));
153 parameters.insert(StopReasonMapValue(std::string("exceptionLine"),
154 toString(frame.line)));
156 if (!frame.function.empty())
157 parameters.insert(StopReasonMapValue(std::string("exceptionFunction"),
158 wStringToString(frame.function)));
163 STDMETHODIMP EventCallback::Exception(
165 __in PEXCEPTION_RECORD64 Ex,
166 __in ULONG FirstChance
169 // Report the exception as GBMI and set potential stop reason
170 const ExtensionContext::StopReasonMap parameters =
171 exceptionParameters(*Ex, FirstChance);
173 std::ostringstream str;
174 formatGdbmiHash(str, parameters);
175 ExtensionContext::instance().setStopReason(parameters, "exception");
176 ExtensionContext::instance().report('E', 0, 0, "exception", "%s", str.str().c_str());
177 return m_wrapped ? m_wrapped->Exception(Ex, FirstChance) : S_OK;
180 STDMETHODIMP EventCallback::CreateThread(
183 __in ULONG64 DataOffset,
184 __in ULONG64 StartOffset
187 return m_wrapped ? m_wrapped->CreateThread(Handle, DataOffset, StartOffset) : S_OK;
190 STDMETHODIMP EventCallback::ExitThread(
195 return m_wrapped ? m_wrapped->ExitThread(ExitCode) : S_OK;
198 STDMETHODIMP EventCallback::CreateProcess(
200 __in ULONG64 ImageFileHandle,
202 __in ULONG64 BaseOffset,
203 __in ULONG ModuleSize,
204 __in_opt PCSTR ModuleName,
205 __in_opt PCSTR ImageName,
207 __in ULONG TimeDateStamp,
208 __in ULONG64 InitialThreadHandle,
209 __in ULONG64 ThreadDataOffset,
210 __in ULONG64 StartOffset
213 return m_wrapped ? m_wrapped->CreateProcess(ImageFileHandle, Handle,
214 BaseOffset, ModuleSize, ModuleName,
215 ImageName, CheckSum, TimeDateStamp,
216 InitialThreadHandle, ThreadDataOffset,
220 STDMETHODIMP EventCallback::ExitProcess(
225 ExtensionContext::instance().report('E', 0, 0, eventContextC, "Process exited (%lu)",
227 const HRESULT hr = m_wrapped ? m_wrapped->ExitProcess(ExitCode) : S_OK;
228 // Remotely debugged process exited, there is no session-inactive notification.
229 // Note: We get deleted here, so, order is important.
230 ExtensionContext::instance().unhookCallbacks();
234 STDMETHODIMP EventCallback::LoadModule(
236 __in ULONG64 ImageFileHandle,
237 __in ULONG64 BaseOffset,
238 __in ULONG ModuleSize,
239 __in_opt PCSTR ModuleName,
240 __in_opt PCSTR ImageName,
242 __in ULONG TimeDateStamp
245 return m_wrapped ? m_wrapped->LoadModule(ImageFileHandle, BaseOffset,
246 ModuleSize, ModuleName, ImageName,
247 CheckSum, TimeDateStamp) : S_OK;
250 STDMETHODIMP EventCallback::UnloadModule(
252 __in_opt PCSTR ImageBaseName,
253 __in ULONG64 BaseOffset
256 return m_wrapped ? m_wrapped->UnloadModule(ImageBaseName, BaseOffset) : S_OK;
259 STDMETHODIMP EventCallback::SystemError(
265 return m_wrapped ? m_wrapped->SystemError(Error, Level) : S_OK;
268 STDMETHODIMP EventCallback::SessionStatus(
273 return m_wrapped ? m_wrapped->SessionStatus(Status) : S_OK;
276 STDMETHODIMP EventCallback::ChangeDebuggeeState(
279 __in ULONG64 Argument
282 return m_wrapped ? m_wrapped->ChangeDebuggeeState(Flags, Argument) : S_OK;
285 STDMETHODIMP EventCallback::ChangeEngineState(
288 __in ULONG64 Argument
291 return m_wrapped ? m_wrapped->ChangeEngineState(Flags, Argument) : S_OK;
294 STDMETHODIMP EventCallback::ChangeSymbolState(
297 __in ULONG64 Argument
300 return m_wrapped ? m_wrapped->ChangeSymbolState(Flags, Argument) : S_OK;