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 m : namespace kasko {
26 m : namespace testing {
27 m : namespace {
28 :
29 m : void EndSession(IDebugClient* debug_client) {
30 m : HRESULT result = debug_client->EndSession(DEBUG_END_ACTIVE_TERMINATE);
31 m : DCHECK(SUCCEEDED(result)) << "EndSession failed: " << ::common::LogHr(result);
32 m : }
33 :
34 m : } // namespace
35 :
36 m : HRESULT VisitMinidump(const base::FilePath& path,
37 m : const MinidumpVisitor& visitor) {
38 : // Create a debugging client.
39 m : base::win::ScopedComPtr<IDebugClient4> debug_client_4;
40 m : base::win::ScopedComPtr<IDebugClient> debug_client;
41 m : HRESULT result =
42 m : ::DebugCreate(__uuidof(IDebugClient4), debug_client_4.ReceiveVoid());
43 m : if (!SUCCEEDED(result)) {
44 m : LOG(ERROR) << "DebugCreate failed: " << ::common::LogHr(result);
45 m : return result;
46 m : }
47 :
48 m : result = debug_client_4.QueryInterface(__uuidof(IDebugClient),
49 m : debug_client.ReceiveVoid());
50 m : if (!SUCCEEDED(result)) {
51 m : LOG(ERROR) << "QI(IDebugClient) failed: " << ::common::LogHr(result);
52 m : return result;
53 m : }
54 :
55 : // Ask the debugger to open our dump file.
56 m : result = debug_client_4->OpenDumpFileWide(path.value().c_str(), NULL);
57 m : if (!SUCCEEDED(result)) {
58 m : LOG(ERROR) << "OpenDumpFileWide failed: " << ::common::LogHr(result);
59 m : return result;
60 m : }
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 m : base::ScopedClosureRunner end_debugger_session(
66 m : base::Bind(&EndSession, base::Unretained(debug_client.get())));
67 :
68 : // The following will block until the dump file has finished loading.
69 m : base::win::ScopedComPtr<IDebugControl> debug_control;
70 m : result = debug_client_4.QueryInterface(__uuidof(IDebugControl),
71 m : debug_control.ReceiveVoid());
72 m : if (!SUCCEEDED(result)) {
73 m : LOG(ERROR) << "QI(IDebugControl) failed: " << ::common::LogHr(result);
74 m : return result;
75 m : }
76 :
77 m : result = debug_control->WaitForEvent(0, INFINITE);
78 m : if (!SUCCEEDED(result)) {
79 m : LOG(ERROR) << "WaitForEvent failed: " << ::common::LogHr(result);
80 m : return result;
81 m : }
82 :
83 m : base::win::ScopedComPtr<IDebugSymbols> debug_symbols;
84 m : result = debug_client_4.QueryInterface(__uuidof(IDebugSymbols),
85 m : debug_symbols.ReceiveVoid());
86 m : if (!SUCCEEDED(result)) {
87 m : LOG(ERROR) << "QI(IDebugSymbols) failed: " << ::common::LogHr(result);
88 m : return result;
89 m : }
90 :
91 m : IDebugClient4* dc4 = debug_client_4.get();
92 m : visitor.Run(dc4, debug_control.get(), debug_symbols.get());
93 m : return S_OK;
94 m : }
95 :
96 m : } // namespace testing
97 m : } // namespace kasko
|