OSDN Git Service

It's 2011 now.
[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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "eventcallback.h"
35 #include "extensioncontext.h"
36 #include "stringutils.h"
37 #include "gdbmihelpers.h"
38
39 static const char eventContextC[] = "event";
40
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
51 };
52
53 EventCallback::EventCallback(IDebugEventCallbacks *wrapped) :
54     m_wrapped(wrapped)
55 {
56 }
57
58 EventCallback::~EventCallback()
59 {
60 }
61
62 STDMETHODIMP EventCallback::QueryInterface(
63     THIS_
64     IN REFIID InterfaceId,
65     OUT PVOID* Interface)
66 {
67     *Interface = NULL;
68
69     if (IsEqualIID(InterfaceId, __uuidof(IUnknown)) ||
70         IsEqualIID(InterfaceId, __uuidof(IDebugOutputCallbacks)))  {
71         *Interface = (IDebugOutputCallbacks *)this;
72         AddRef();
73         return S_OK;
74     } else {
75         return E_NOINTERFACE;
76     }
77 }
78
79 STDMETHODIMP_(ULONG) EventCallback::AddRef(THIS)
80 {
81     // This class is designed to be static so
82     // there's no true refcount.
83     return 1;
84 }
85
86 STDMETHODIMP_(ULONG) EventCallback::Release(THIS)
87 {
88     // This class is designed to be static so
89     // there's no true refcount.
90     return 0;
91 }
92
93 STDMETHODIMP EventCallback::GetInterestMask(THIS_ __out PULONG mask)
94 {
95     if (m_wrapped)
96         m_wrapped->GetInterestMask(mask);
97
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;
101     return S_OK;
102 }
103
104 STDMETHODIMP EventCallback::Breakpoint(THIS_ __in PDEBUG_BREAKPOINT b)
105 {
106     // Breakpoint hit - Set the stop reason parameters on the extension context.
107     typedef ExtensionContext::StopReasonMap StopReasonMap;
108     typedef ExtensionContext::StopReasonMap::value_type StopReasonMapValue;
109
110     ULONG id = 0;
111     ULONG64 address = 0;
112     b->GetId(&id);
113     b->GetOffset(&address);
114
115     StopReasonMap stopReason;
116     stopReason.insert(StopReasonMapValue(std::string("breakpointId"), toString(id)));
117     if (address)
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;
121 }
122
123 static inline ExtensionContext::StopReasonMap exceptionParameters(const EXCEPTION_RECORD64 &e,
124                                                                   unsigned firstChance)
125 {
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
145     StackFrame frame;
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)));
155         }
156         if (!frame.function.empty())
157             parameters.insert(StopReasonMapValue(std::string("exceptionFunction"),
158                                                  wStringToString(frame.function)));
159     } // getCurrentFrame
160     return parameters;
161 }
162
163 STDMETHODIMP EventCallback::Exception(
164     THIS_
165     __in PEXCEPTION_RECORD64 Ex,
166     __in ULONG FirstChance
167     )
168 {
169     // Report the exception as GBMI and set potential stop reason
170     const ExtensionContext::StopReasonMap parameters =
171             exceptionParameters(*Ex, FirstChance);
172
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;
178 }
179
180 STDMETHODIMP EventCallback::CreateThread(
181     THIS_
182     __in ULONG64 Handle,
183     __in ULONG64 DataOffset,
184     __in ULONG64 StartOffset
185     )
186 {
187     return m_wrapped ? m_wrapped->CreateThread(Handle, DataOffset, StartOffset) : S_OK;
188 }
189
190 STDMETHODIMP EventCallback::ExitThread(
191     THIS_
192     __in ULONG  ExitCode
193     )
194 {
195     return m_wrapped ? m_wrapped->ExitThread(ExitCode) : S_OK;
196 }
197
198 STDMETHODIMP EventCallback::CreateProcess(
199     THIS_
200     __in ULONG64 ImageFileHandle,
201     __in ULONG64 Handle,
202     __in ULONG64 BaseOffset,
203     __in ULONG    ModuleSize,
204     __in_opt PCSTR ModuleName,
205     __in_opt PCSTR ImageName,
206     __in ULONG  CheckSum,
207     __in ULONG  TimeDateStamp,
208     __in ULONG64 InitialThreadHandle,
209     __in ULONG64 ThreadDataOffset,
210     __in ULONG64 StartOffset
211     )
212 {
213     return m_wrapped ? m_wrapped->CreateProcess(ImageFileHandle, Handle,
214                                                  BaseOffset, ModuleSize, ModuleName,
215                                                  ImageName, CheckSum, TimeDateStamp,
216                                                  InitialThreadHandle, ThreadDataOffset,
217                                                  StartOffset) : S_OK;
218 }
219
220 STDMETHODIMP EventCallback::ExitProcess(
221     THIS_
222     __in ULONG ExitCode
223     )
224 {
225     ExtensionContext::instance().report('E', 0, 0, eventContextC, "Process exited (%lu)",
226                                         ExitCode);
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();
231     return hr;
232 }
233
234 STDMETHODIMP EventCallback::LoadModule(
235     THIS_
236     __in ULONG64 ImageFileHandle,
237     __in ULONG64 BaseOffset,
238     __in ULONG ModuleSize,
239     __in_opt PCSTR ModuleName,
240     __in_opt PCSTR ImageName,
241     __in ULONG CheckSum,
242     __in ULONG TimeDateStamp
243     )
244 {
245     return m_wrapped ? m_wrapped->LoadModule(ImageFileHandle, BaseOffset,
246                                              ModuleSize, ModuleName, ImageName,
247                                              CheckSum, TimeDateStamp) : S_OK;
248 }
249
250 STDMETHODIMP EventCallback::UnloadModule(
251     THIS_
252     __in_opt PCSTR ImageBaseName,
253     __in ULONG64 BaseOffset
254     )
255 {
256     return m_wrapped ? m_wrapped->UnloadModule(ImageBaseName, BaseOffset) : S_OK;
257 }
258
259 STDMETHODIMP EventCallback::SystemError(
260     THIS_
261     __in ULONG Error,
262     __in ULONG Level
263     )
264 {
265     return m_wrapped ? m_wrapped->SystemError(Error, Level) : S_OK;
266 }
267
268 STDMETHODIMP EventCallback::SessionStatus(
269     THIS_
270     __in ULONG Status
271     )
272 {
273     return m_wrapped ? m_wrapped->SessionStatus(Status) : S_OK;
274 }
275
276 STDMETHODIMP EventCallback::ChangeDebuggeeState(
277     THIS_
278     __in ULONG Flags,
279     __in ULONG64 Argument
280     )
281 {
282     return m_wrapped ? m_wrapped->ChangeDebuggeeState(Flags, Argument) : S_OK;
283 }
284
285 STDMETHODIMP EventCallback::ChangeEngineState(
286     THIS_
287     __in ULONG Flags,
288     __in ULONG64 Argument
289     )
290 {
291     return m_wrapped ? m_wrapped->ChangeEngineState(Flags, Argument) : S_OK;
292 }
293
294 STDMETHODIMP EventCallback::ChangeSymbolState(
295     THIS_
296     __in ULONG Flags,
297     __in ULONG64 Argument
298     )
299 {
300     return m_wrapped ? m_wrapped->ChangeSymbolState(Flags, Argument) : S_OK;
301 }