1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/testing/minidump_unittest_helpers.h"
16 :
17 : #include "base/bind.h"
18 : #include "base/callback.h"
19 : #include "base/callback_helpers.h"
20 : #include "base/logging.h"
21 : #include "base/files/file_path.h"
22 : #include "base/win/scoped_comptr.h"
23 : #include "syzygy/common/com_utils.h"
24 :
25 : namespace kasko {
26 : namespace testing {
27 : namespace {
28 :
29 E : void EndSession(IDebugClient* debug_client) {
30 E : HRESULT result = debug_client->EndSession(DEBUG_END_ACTIVE_TERMINATE);
31 E : DCHECK(SUCCEEDED(result)) << "EndSession failed: " << ::common::LogHr(result);
32 E : }
33 :
34 : } // namespace
35 :
36 : HRESULT VisitMinidump(const base::FilePath& path,
37 E : const MinidumpVisitor& visitor) {
38 : // Create a debugging client.
39 E : base::win::ScopedComPtr<IDebugClient4> debug_client_4;
40 E : base::win::ScopedComPtr<IDebugClient> debug_client;
41 : HRESULT result =
42 E : ::DebugCreate(__uuidof(IDebugClient4), debug_client_4.ReceiveVoid());
43 E : if (!SUCCEEDED(result)) {
44 i : LOG(ERROR) << "DebugCreate failed: " << ::common::LogHr(result);
45 i : return result;
46 : }
47 :
48 : result = debug_client_4.QueryInterface(__uuidof(IDebugClient),
49 E : debug_client.ReceiveVoid());
50 E : if (!SUCCEEDED(result)) {
51 i : LOG(ERROR) << "QI(IDebugClient) failed: " << ::common::LogHr(result);
52 i : return result;
53 : }
54 :
55 : // Ask the debugger to open our dump file.
56 E : result = debug_client_4->OpenDumpFileWide(path.value().c_str(), NULL);
57 E : if (!SUCCEEDED(result)) {
58 i : LOG(ERROR) << "OpenDumpFileWide failed: " << ::common::LogHr(result);
59 i : return result;
60 : }
61 :
62 : // Now that we have started a debugging session must ensure we will terminate
63 : // it when the test completes. Otherwise the dump file will remain open and we
64 : // won't be able to clean up our temporary directory.
65 : base::ScopedClosureRunner end_debugger_session(
66 E : base::Bind(&EndSession, base::Unretained(debug_client.get())));
67 :
68 : // The following will block until the dump file has finished loading.
69 E : base::win::ScopedComPtr<IDebugControl> debug_control;
70 : result = debug_client_4.QueryInterface(__uuidof(IDebugControl),
71 E : debug_control.ReceiveVoid());
72 E : if (!SUCCEEDED(result)) {
73 i : LOG(ERROR) << "QI(IDebugControl) failed: " << ::common::LogHr(result);
74 i : return result;
75 : }
76 :
77 E : result = debug_control->WaitForEvent(0, INFINITE);
78 E : if (!SUCCEEDED(result)) {
79 i : LOG(ERROR) << "WaitForEvent failed: " << ::common::LogHr(result);
80 i : return result;
81 : }
82 :
83 E : base::win::ScopedComPtr<IDebugSymbols> debug_symbols;
84 : result = debug_client_4.QueryInterface(__uuidof(IDebugSymbols),
85 E : debug_symbols.ReceiveVoid());
86 E : if (!SUCCEEDED(result)) {
87 i : LOG(ERROR) << "QI(IDebugSymbols) failed: " << ::common::LogHr(result);
88 i : return result;
89 : }
90 :
91 E : IDebugClient4* dc4 = debug_client_4.get();
92 E : visitor.Run(dc4, debug_control.get(), debug_symbols.get());
93 E : return S_OK;
94 E : }
95 :
96 : } // namespace testing
97 : } // namespace kasko
|