Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: snapshot/win/process_reader_win.cc

Issue 1131473005: win: Add thread snapshot and memory snapshot for stacks (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: comment Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « snapshot/win/process_reader_win.h ('k') | snapshot/win/process_snapshot_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Crashpad Authors. All rights reserved. 1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "snapshot/win/process_reader_win.h" 15 #include "snapshot/win/process_reader_win.h"
16 16
17 #include <winternl.h>
18
19 #include "base/memory/scoped_ptr.h"
17 #include "base/numerics/safe_conversions.h" 20 #include "base/numerics/safe_conversions.h"
21 #include "util/win/process_structs.h"
22 #include "util/win/scoped_handle.h"
18 #include "util/win/time.h" 23 #include "util/win/time.h"
19 24
20 namespace crashpad { 25 namespace crashpad {
21 26
27 namespace {
28
29 NTSTATUS NtQuerySystemInformation(
30 SYSTEM_INFORMATION_CLASS system_information_class,
31 PVOID system_information,
32 ULONG system_information_length,
33 PULONG return_length) {
34 static decltype(::NtQuerySystemInformation)* nt_query_system_information =
35 reinterpret_cast<decltype(::NtQuerySystemInformation)*>(GetProcAddress(
36 LoadLibrary(L"ntdll.dll"), "NtQuerySystemInformation"));
37 DCHECK(nt_query_system_information);
38 return nt_query_system_information(system_information_class,
39 system_information,
40 system_information_length,
41 return_length);
42 }
43
44 // The 4th argument is CLIENT_ID*, but as we can't typedef that, we simply cast
45 // to void* here.
46 typedef NTSTATUS(WINAPI* NtOpenThreadFunction)(
47 PHANDLE ThreadHandle,
48 ACCESS_MASK DesiredAccess,
49 POBJECT_ATTRIBUTES ObjectAttributes,
50 const void* ClientId);
51
52 template <class Traits>
53 NTSTATUS NtOpenThread(PHANDLE thread_handle,
54 ACCESS_MASK desired_access,
55 POBJECT_ATTRIBUTES object_attributes,
56 const process_types::CLIENT_ID<Traits>* client_id) {
57 static NtOpenThreadFunction nt_open_thread =
58 reinterpret_cast<NtOpenThreadFunction>(
59 GetProcAddress(LoadLibrary(L"ntdll.dll"), "NtOpenThread"));
60 DCHECK(nt_open_thread);
61 return nt_open_thread(thread_handle,
62 desired_access,
63 object_attributes,
64 static_cast<const void*>(client_id));
65 }
66
67 NTSTATUS NtQueryInformationThread(HANDLE thread_handle,
68 THREADINFOCLASS thread_information_class,
69 PVOID thread_information,
70 ULONG thread_information_length,
71 PULONG return_length) {
72 static decltype(::NtQueryInformationThread)* nt_query_information_thread =
73 reinterpret_cast<decltype(::NtQueryInformationThread)*>(GetProcAddress(
74 LoadLibrary(L"ntdll.dll"), "NtQueryInformationThread"));
75 DCHECK(nt_query_information_thread);
76 return nt_query_information_thread(thread_handle,
77 thread_information_class,
78 thread_information,
79 thread_information_length,
80 return_length);
81 }
82
83 // Copied from ntstatus.h because um/winnt.h conflicts with general inclusion of
84 // ntstatus.h.
85 #define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L)
86 #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
87
88 // Gets a pointer to the process information structure after a given one, or
89 // null when iteration is complete, assuming they've been retrieved in a block
90 // via NtQuerySystemInformation().
91 template <class Traits>
92 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* NextProcess(
93 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process) {
94 ULONG offset = process->NextEntryOffset;
95 if (offset == 0)
96 return nullptr;
97 return reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>(
98 reinterpret_cast<uint8_t*>(process) + offset);
99 }
100
101 //! \brief Retrieves the SYSTEM_PROCESS_INFORMATION for a given process.
102 //!
103 //! The returned pointer points into the memory block stored by \a buffer.
104 //! Ownership of \a buffer is transferred to the caller.
105 //!
106 //! \return Pointer to the process' data, or nullptr if it was not found or on
107 //! error. On error, a message will be logged.
108 template <class Traits>
109 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* GetProcessInformation(
110 HANDLE process_handle,
111 scoped_ptr<uint8_t[]>* buffer) {
112 ULONG buffer_size = 16384;
113 buffer->reset(new uint8_t[buffer_size]);
114 NTSTATUS status;
115 // This must be in retry loop, as we're racing with process creation on the
116 // system to find a buffer large enough to hold all process information.
117 for (int tries = 0; tries < 20; ++tries) {
118 const int kSystemExtendedProcessInformation = 57;
119 status = crashpad::NtQuerySystemInformation(
120 static_cast<SYSTEM_INFORMATION_CLASS>(
121 kSystemExtendedProcessInformation),
122 reinterpret_cast<void*>(buffer->get()),
123 buffer_size,
124 &buffer_size);
125 if (status == STATUS_BUFFER_TOO_SMALL ||
126 status == STATUS_INFO_LENGTH_MISMATCH) {
127 // Add a little extra to try to avoid an additional loop iteration. We're
128 // racing with system-wide process creation between here and the next call
129 // to NtQuerySystemInformation().
130 buffer_size += 4096;
131 buffer->reset(new uint8_t[buffer_size]);
132 } else {
133 break;
134 }
135 }
136
137 if (!NT_SUCCESS(status)) {
138 LOG(ERROR) << "NtQuerySystemInformation failed: " << std::hex << status;
139 return nullptr;
140 }
141
142 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process =
143 reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>(
144 buffer->get());
145 DWORD process_id = GetProcessId(process_handle);
146 do {
147 if (process->UniqueProcessId == process_id)
148 return process;
149 } while (process = NextProcess(process));
150
151 LOG(ERROR) << "process " << process_id << " not found";
152 return nullptr;
153 }
154
155 template <class Traits>
156 uint32_t GetThreadSuspendCount(
157 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<Traits>&
158 thread_info) {
159 // Wait reason values are from KWAIT_REASON in wdm.h. We don't need all of
160 // them, so just declare the one we need.
161 const ULONG kWaitReasonSuspended = 5;
162
163 // Kernel mode enumerations for thread state come from
164 // http://www.nirsoft.net/kernel_struct/vista/KTHREAD_STATE.html and
165 // https://msdn.microsoft.com/en-us/library/system.diagnostics.threadstate(v=v s.110).aspx
166 const ULONG kThreadStateWaiting = 5;
167 const ULONG kThreadStateGateWait = 8;
168
169 bool suspended = (thread_info.ThreadState == kThreadStateWaiting ||
170 thread_info.ThreadState == kThreadStateGateWait) &&
171 thread_info.WaitReason == kWaitReasonSuspended;
172 if (!suspended)
173 return 0;
174
175 HANDLE thread_handle;
176 ACCESS_MASK query_access = THREAD_QUERY_LIMITED_INFORMATION;
177 OBJECT_ATTRIBUTES object_attributes;
178 InitializeObjectAttributes(&object_attributes, nullptr, 0, nullptr, nullptr);
179 NTSTATUS status = crashpad::NtOpenThread(
180 &thread_handle, query_access, &object_attributes, &thread_info.ClientId);
181 if (!NT_SUCCESS(status)) {
182 LOG(WARNING) << "couldn't open thread to retrieve suspend count";
183 // Fall back to something semi-reasonable. We know we're suspended at this
184 // point, so just return 1.
185 return 1;
186 }
187
188 // Take ownership of this handle so we close on exit. NtClose and CloseHandle
189 // are identical.
190 ScopedKernelHANDLE handle(thread_handle);
191
192 // From ntddk.h. winternl.h defines THREADINFOCLASS, but only one value.
193 const int kThreadSuspendCount = 35;
194 ULONG suspend_count;
195 status = crashpad::NtQueryInformationThread(
196 handle.get(),
197 static_cast<THREADINFOCLASS>(kThreadSuspendCount),
198 &suspend_count,
199 sizeof(suspend_count),
200 nullptr);
201 if (!NT_SUCCESS(status)) {
202 LOG(WARNING) << "NtQueryInformationThread failed" << std::hex << status;
203 return 1;
204 }
205
206 return suspend_count;
207 }
208
209 } // namespace
210
211 ProcessReaderWin::Thread::Thread()
212 : id(0),
213 teb(0),
214 stack_region_address(0),
215 stack_region_size(0),
216 suspend_count(0),
217 priority_class(0),
218 priority(0) {
219 }
220
22 ProcessReaderWin::ProcessReaderWin() 221 ProcessReaderWin::ProcessReaderWin()
23 : process_(INVALID_HANDLE_VALUE), 222 : process_(INVALID_HANDLE_VALUE),
24 process_info_(), 223 process_info_(),
224 threads_(),
25 modules_(), 225 modules_(),
226 initialized_threads_(false),
26 initialized_() { 227 initialized_() {
27 } 228 }
28 229
29 ProcessReaderWin::~ProcessReaderWin() { 230 ProcessReaderWin::~ProcessReaderWin() {
30 } 231 }
31 232
32 bool ProcessReaderWin::Initialize(HANDLE process) { 233 bool ProcessReaderWin::Initialize(HANDLE process) {
33 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); 234 INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
34 235
35 process_ = process; 236 process_ = process;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 FILETIME creation, exit, kernel, user; 271 FILETIME creation, exit, kernel, user;
71 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) { 272 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) {
72 PLOG(ERROR) << "GetProcessTimes"; 273 PLOG(ERROR) << "GetProcessTimes";
73 return false; 274 return false;
74 } 275 }
75 *user_time = FiletimeToTimevalInterval(user); 276 *user_time = FiletimeToTimevalInterval(user);
76 *system_time = FiletimeToTimevalInterval(kernel); 277 *system_time = FiletimeToTimevalInterval(kernel);
77 return true; 278 return true;
78 } 279 }
79 280
281 const std::vector<ProcessReaderWin::Thread>& ProcessReaderWin::Threads() {
282 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
283
284 if (initialized_threads_)
285 return threads_;
286
287 initialized_threads_ = true;
288
289 DCHECK(threads_.empty());
290
291 #if ARCH_CPU_32_BITS
292 using SizeTraits = process_types::internal::Traits32;
293 #else
294 using SizeTraits = process_types::internal::Traits64;
295 #endif
296 scoped_ptr<uint8_t[]> buffer;
297 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information =
298 GetProcessInformation<SizeTraits>(process_, &buffer);
299 if (!process_information)
300 return threads_;
301
302 for (unsigned long i = 0; i < process_information->NumberOfThreads; ++i) {
303 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>&
304 thread_info = process_information->Threads[i];
305 Thread thread;
306 thread.id = thread_info.ClientId.UniqueThread;
307 thread.suspend_count = GetThreadSuspendCount(thread_info);
308
309 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from
310 // the Priority, BasePriority, and
311 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 .
312 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate
313 // both of those at the same time if it's useful.
314 thread.priority_class = NORMAL_PRIORITY_CLASS;
315
316 thread.priority = thread_info.Priority;
317 thread.teb = thread_info.TebBase;
318
319 // While there are semi-documented fields in the thread structure called
320 // StackBase and StackLimit, they don't appear to be correct in practice (or
321 // at least, I don't know how to interpret them). Instead, read the TIB
322 // (Thread Information Block) which is the first element of the TEB, and use
323 // its stack fields.
324 process_types::NT_TIB<SizeTraits> tib;
325 if (ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) {
326 thread.stack_region_address = tib.StackBase;
327 // Note, "backwards" because of direction of stack growth.
328 thread.stack_region_size = tib.StackBase - tib.StackLimit;
329 }
330 threads_.push_back(thread);
331 }
332
333 return threads_;
334 }
335
80 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() { 336 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() {
81 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 337 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
82 338
83 if (!process_info_.Modules(&modules_)) { 339 if (!process_info_.Modules(&modules_)) {
84 LOG(ERROR) << "couldn't retrieve modules"; 340 LOG(ERROR) << "couldn't retrieve modules";
85 } 341 }
86 342
87 return modules_; 343 return modules_;
88 } 344 }
89 345
90 } // namespace crashpad 346 } // namespace crashpad
OLDNEW
« no previous file with comments | « snapshot/win/process_reader_win.h ('k') | snapshot/win/process_snapshot_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698