OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qtcreatorcdbext / eventcallback.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "eventcallback.h"
34 #include "extensioncontext.h"
35 #include "stringutils.h"
36 #include "gdbmihelpers.h"
37
38 static const char eventContextC[] = "event";
39
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
50 };
51
52 /*!
53     \class IDebugEventCallbacks
54
55     Event handler wrapping the original IDebugEventCallbacks
56     to catch and store exceptions (report crashes as stop reasons).
57     \ingroup qtcreatorcdbext
58 */
59
60 EventCallback::EventCallback(IDebugEventCallbacks *wrapped) :
61     m_wrapped(wrapped)
62 {
63 }
64
65 EventCallback::~EventCallback()
66 {
67 }
68
69 STDMETHODIMP EventCallback::QueryInterface(
70     THIS_
71     IN REFIID InterfaceId,
72     OUT PVOID* Interface)
73 {
74     *Interface = NULL;
75
76     if (IsEqualIID(InterfaceId, __uuidof(IUnknown)) ||
77         IsEqualIID(InterfaceId, __uuidof(IDebugOutputCallbacks)))  {
78         *Interface = (IDebugOutputCallbacks *)this;
79         AddRef();
80         return S_OK;
81     } else {
82         return E_NOINTERFACE;
83     }
84 }
85
86 STDMETHODIMP_(ULONG) EventCallback::AddRef(THIS)
87 {
88     // This class is designed to be static so
89     // there's no true refcount.
90     return 1;
91 }
92
93 STDMETHODIMP_(ULONG) EventCallback::Release(THIS)
94 {
95     // This class is designed to be static so
96     // there's no true refcount.
97     return 0;
98 }
99
100 STDMETHODIMP EventCallback::GetInterestMask(THIS_ __out PULONG mask)
101 {
102     if (m_wrapped)
103         m_wrapped->GetInterestMask(mask);
104
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;
108     return S_OK;
109 }
110
111 STDMETHODIMP EventCallback::Breakpoint(THIS_ __in PDEBUG_BREAKPOINT b)
112 {
113     // Breakpoint hit - Set the stop reason parameters on the extension context.
114     typedef ExtensionContext::StopReasonMap StopReasonMap;
115     typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
116
117     ULONG uId = 0;
118     ULONG64 address = 0;
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;
126 }
127
128 static inline ExtensionContext::StopReasonMap exceptionParameters(const EXCEPTION_RECORD64 &e,
129                                                                   unsigned firstChance)
130 {
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
150     StackFrame frame;
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)));
160         }
161         if (!frame.function.empty())
162             parameters.insert(StopReasonMapValue(std::string("exceptionFunction"),
163                                                  wStringToString(frame.function)));
164     } // getCurrentFrame
165     return parameters;
166 }
167
168 STDMETHODIMP EventCallback::Exception(
169     THIS_
170     __in PEXCEPTION_RECORD64 Ex,
171     __in ULONG FirstChance
172     )
173 {
174     // Report the exception as GBMI and set potential stop reason
175     const ExtensionContext::StopReasonMap parameters =
176             exceptionParameters(*Ex, FirstChance);
177
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;
183 }
184
185 STDMETHODIMP EventCallback::CreateThread(
186     THIS_
187     __in ULONG64 Handle,
188     __in ULONG64 DataOffset,
189     __in ULONG64 StartOffset
190     )
191 {
192     return m_wrapped ? m_wrapped->CreateThread(Handle, DataOffset, StartOffset) : S_OK;
193 }
194
195 STDMETHODIMP EventCallback::ExitThread(
196     THIS_
197     __in ULONG  ExitCode
198     )
199 {
200     return m_wrapped ? m_wrapped->ExitThread(ExitCode) : S_OK;
201 }
202
203 STDMETHODIMP EventCallback::CreateProcess(
204     THIS_
205     __in ULONG64 ImageFileHandle,
206     __in ULONG64 Handle,
207     __in ULONG64 BaseOffset,
208     __in ULONG    ModuleSize,
209     __in_opt PCSTR ModuleName,
210     __in_opt PCSTR ImageName,
211     __in ULONG  CheckSum,
212     __in ULONG  TimeDateStamp,
213     __in ULONG64 InitialThreadHandle,
214     __in ULONG64 ThreadDataOffset,
215     __in ULONG64 StartOffset
216     )
217 {
218     return m_wrapped ? m_wrapped->CreateProcess(ImageFileHandle, Handle,
219                                                  BaseOffset, ModuleSize, ModuleName,
220                                                  ImageName, CheckSum, TimeDateStamp,
221                                                  InitialThreadHandle, ThreadDataOffset,
222                                                  StartOffset) : S_OK;
223 }
224
225 STDMETHODIMP EventCallback::ExitProcess(
226     THIS_
227     __in ULONG ExitCode
228     )
229 {
230     ExtensionContext::instance().report('E', 0, 0, eventContextC, "Process exited (%lu)",
231                                         ExitCode);
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();
236     return hr;
237 }
238
239 STDMETHODIMP EventCallback::LoadModule(
240     THIS_
241     __in ULONG64 ImageFileHandle,
242     __in ULONG64 BaseOffset,
243     __in ULONG ModuleSize,
244     __in_opt PCSTR ModuleName,
245     __in_opt PCSTR ImageName,
246     __in ULONG CheckSum,
247     __in ULONG TimeDateStamp
248     )
249 {
250     return m_wrapped ? m_wrapped->LoadModule(ImageFileHandle, BaseOffset,
251                                              ModuleSize, ModuleName, ImageName,
252                                              CheckSum, TimeDateStamp) : S_OK;
253 }
254
255 STDMETHODIMP EventCallback::UnloadModule(
256     THIS_
257     __in_opt PCSTR ImageBaseName,
258     __in ULONG64 BaseOffset
259     )
260 {
261     return m_wrapped ? m_wrapped->UnloadModule(ImageBaseName, BaseOffset) : S_OK;
262 }
263
264 STDMETHODIMP EventCallback::SystemError(
265     THIS_
266     __in ULONG Error,
267     __in ULONG Level
268     )
269 {
270     return m_wrapped ? m_wrapped->SystemError(Error, Level) : S_OK;
271 }
272
273 STDMETHODIMP EventCallback::SessionStatus(
274     THIS_
275     __in ULONG Status
276     )
277 {
278     return m_wrapped ? m_wrapped->SessionStatus(Status) : S_OK;
279 }
280
281 STDMETHODIMP EventCallback::ChangeDebuggeeState(
282     THIS_
283     __in ULONG Flags,
284     __in ULONG64 Argument
285     )
286 {
287     return m_wrapped ? m_wrapped->ChangeDebuggeeState(Flags, Argument) : S_OK;
288 }
289
290 STDMETHODIMP EventCallback::ChangeEngineState(
291     THIS_
292     __in ULONG Flags,
293     __in ULONG64 Argument
294     )
295 {
296     return m_wrapped ? m_wrapped->ChangeEngineState(Flags, Argument) : S_OK;
297 }
298
299 STDMETHODIMP EventCallback::ChangeSymbolState(
300     THIS_
301     __in ULONG Flags,
302     __in ULONG64 Argument
303     )
304 {
305     return m_wrapped ? m_wrapped->ChangeSymbolState(Flags, Argument) : S_OK;
306 }