OLD | NEW |
---|---|
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" | |
18 #include "util/win/time.h" | 22 #include "util/win/time.h" |
19 | 23 |
20 namespace crashpad { | 24 namespace crashpad { |
21 | 25 |
26 namespace { | |
27 | |
28 NTSTATUS NtQuerySystemInformation( | |
29 SYSTEM_INFORMATION_CLASS system_information_class, | |
30 PVOID system_information, | |
31 ULONG system_information_length, | |
32 PULONG return_length) { | |
33 static decltype(::NtQuerySystemInformation)* nt_query_system_information = | |
34 reinterpret_cast<decltype(::NtQuerySystemInformation)*>(GetProcAddress( | |
35 LoadLibrary(L"ntdll.dll"), "NtQuerySystemInformation")); | |
36 DCHECK(nt_query_system_information); | |
37 return nt_query_system_information(system_information_class, | |
38 system_information, | |
39 system_information_length, | |
40 return_length); | |
41 } | |
42 | |
43 // Copied from ntstatus.h because um/winnt.h conflicts with general inclusion of | |
44 // ntstatus.h. | |
45 #define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L) | |
Mark Mentovai
2015/05/08 16:54:20
Drop the extra whitespace. Even though it’s just a
scottmg
2015/05/08 18:24:01
Done.
| |
46 #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) | |
47 | |
48 // Kernel mode enumerations for thread state. | |
49 enum class KTHREAD_STATE : uint32_t { | |
Mark Mentovai
2015/05/08 16:54:19
If this isn’t supposed to match names defined else
Mark Mentovai
2015/05/08 16:54:19
If there’s a good reference for where this came fr
scottmg
2015/05/08 18:24:01
Done.
scottmg
2015/05/08 18:24:02
Done.
| |
50 Initialized, | |
51 Ready, | |
52 Running, | |
53 Standby, | |
54 Terminated, | |
55 Waiting, | |
56 Transition, | |
57 DeferredReady, | |
58 GateWait, | |
59 MaximumThreadState | |
Mark Mentovai
2015/05/08 16:54:20
I don’t like these “max” things in enums unless th
scottmg
2015/05/08 18:24:02
Done.
| |
60 }; | |
61 | |
62 // Gets a pointer to the process information structure after a given one, or | |
63 // null when iteration is complete, assuming they've been retrieved in a block | |
64 // via NtQuerySystemInformation(). | |
65 template <class Traits> | |
66 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* NextProcess( | |
67 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process) { | |
68 ULONG offset = process->NextEntryOffset; | |
69 if (offset == 0) | |
70 return nullptr; | |
71 return reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>( | |
72 reinterpret_cast<uint8_t*>(process) + offset); | |
Mark Mentovai
2015/05/08 16:54:20
Assure no overflow. Ideally, this function would k
Mark Mentovai
2015/05/08 18:14:14
Mark Mentovai wrote:
scottmg
2015/05/08 18:24:02
Done.
scottmg
2015/05/08 18:27:10
(Un)Done.
| |
73 } | |
74 | |
75 //! \brief Retrieves the SYSTEM_PROCESS_INFORMATION for a given process. | |
76 //! | |
77 //! The returned pointer points into the memory block stored by \a buffer. | |
78 //! Ownership of \a buffer is transferred to the caller. | |
79 //! | |
80 //! \return Pointer to the process' data, or nullptr if it was not found or on | |
81 //! error. On error, a message will be logged. | |
Mark Mentovai
2015/05/08 16:54:20
Hanging indent.
scottmg
2015/05/08 18:24:01
Done.
| |
82 template <class Traits> | |
83 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* GetProcessInformation( | |
84 HANDLE process_handle, | |
85 scoped_ptr<uint8_t[]>* buffer) { | |
86 ULONG buffer_size = 16384; | |
87 buffer->reset(new uint8_t[buffer_size]); | |
88 NTSTATUS status; | |
89 // This must be in retry loop, as we're racing with process creation on the | |
90 // system to find a buffer large enough to hold all process information. | |
91 for (int tries = 0; tries < 20; ++tries) { | |
92 const int kSystemExtendedProcessInformation = 57; | |
93 status = crashpad::NtQuerySystemInformation( | |
94 static_cast<SYSTEM_INFORMATION_CLASS>( | |
95 kSystemExtendedProcessInformation), | |
96 reinterpret_cast<void*>(buffer->get()), | |
97 buffer_size, | |
98 &buffer_size); | |
99 if (status == STATUS_BUFFER_TOO_SMALL || | |
100 status == STATUS_INFO_LENGTH_MISMATCH) { | |
101 buffer->reset(new uint8_t[buffer_size]); | |
Mark Mentovai
2015/05/08 16:54:19
Maybe leave a little extra slop in here so that if
scottmg
2015/05/08 18:24:01
Done.
| |
102 } else { | |
103 break; | |
104 } | |
105 } | |
106 | |
107 if (!NT_SUCCESS(status)) { | |
108 LOG(ERROR) << "NtQuerySystemInformation failed: " << status; | |
Mark Mentovai
2015/05/08 16:54:20
These are more sensible to print in hex, although
scottmg
2015/05/08 18:24:02
Done.
| |
109 return nullptr; | |
110 } | |
111 | |
112 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process = | |
113 reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>( | |
114 buffer->get()); | |
115 DWORD process_id = GetProcessId(process_handle); | |
116 do { | |
117 if (process->UniqueProcessId == process_id) | |
118 return process; | |
119 } while (process = NextProcess(process)); | |
120 | |
121 LOG(ERROR) << "process " << process_id << " not found"; | |
122 return nullptr; | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 ProcessReaderWin::Thread::Thread() | |
128 : id(0), | |
129 suspend_count(0), | |
130 priority_class(0), | |
131 priority(0), | |
132 teb(0), | |
133 stack_region_address(0), | |
134 stack_region_size(0) { | |
135 } | |
136 | |
22 ProcessReaderWin::ProcessReaderWin() | 137 ProcessReaderWin::ProcessReaderWin() |
23 : process_(INVALID_HANDLE_VALUE), | 138 : process_(INVALID_HANDLE_VALUE), |
24 process_info_(), | 139 process_info_(), |
25 modules_(), | 140 modules_(), |
26 initialized_() { | 141 initialized_() { |
27 } | 142 } |
28 | 143 |
29 ProcessReaderWin::~ProcessReaderWin() { | 144 ProcessReaderWin::~ProcessReaderWin() { |
30 } | 145 } |
31 | 146 |
32 bool ProcessReaderWin::Initialize(HANDLE process) { | 147 bool ProcessReaderWin::Initialize(HANDLE process) { |
33 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); | 148 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
34 | 149 |
35 process_ = process; | 150 process_ = process; |
36 process_info_.Initialize(process); | 151 process_info_.Initialize(process); |
37 | 152 |
153 InitializeThreads(); | |
Mark Mentovai
2015/05/08 16:54:20
You can do this in Threads() at the expense of ano
scottmg
2015/05/08 18:24:01
Better? I guess? Done.
| |
154 | |
38 INITIALIZATION_STATE_SET_VALID(initialized_); | 155 INITIALIZATION_STATE_SET_VALID(initialized_); |
39 return true; | 156 return true; |
40 } | 157 } |
41 | 158 |
42 bool ProcessReaderWin::ReadMemory(WinVMAddress at, | 159 bool ProcessReaderWin::ReadMemory(WinVMAddress at, |
43 WinVMSize num_bytes, | 160 WinVMSize num_bytes, |
44 void* into) { | 161 void* into) { |
45 SIZE_T bytes_read; | 162 SIZE_T bytes_read; |
46 if (!ReadProcessMemory(process_, | 163 if (!ReadProcessMemory(process_, |
47 reinterpret_cast<void*>(at), | 164 reinterpret_cast<void*>(at), |
(...skipping 22 matching lines...) Expand all Loading... | |
70 FILETIME creation, exit, kernel, user; | 187 FILETIME creation, exit, kernel, user; |
71 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) { | 188 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) { |
72 PLOG(ERROR) << "GetProcessTimes"; | 189 PLOG(ERROR) << "GetProcessTimes"; |
73 return false; | 190 return false; |
74 } | 191 } |
75 *user_time = FiletimeToTimevalInterval(user); | 192 *user_time = FiletimeToTimevalInterval(user); |
76 *system_time = FiletimeToTimevalInterval(kernel); | 193 *system_time = FiletimeToTimevalInterval(kernel); |
77 return true; | 194 return true; |
78 } | 195 } |
79 | 196 |
197 const std::vector<ProcessReaderWin::Thread>& ProcessReaderWin::Threads() { | |
198 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | |
199 return threads_; | |
200 } | |
201 | |
80 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() { | 202 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() { |
81 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 203 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
82 | 204 |
83 if (!process_info_.Modules(&modules_)) { | 205 if (!process_info_.Modules(&modules_)) { |
84 LOG(ERROR) << "couldn't retrieve modules"; | 206 LOG(ERROR) << "couldn't retrieve modules"; |
85 } | 207 } |
86 | 208 |
87 return modules_; | 209 return modules_; |
88 } | 210 } |
89 | 211 |
212 void ProcessReaderWin::InitializeThreads() { | |
213 DCHECK(threads_.empty()); | |
214 | |
215 #if ARCH_CPU_32_BITS | |
216 using SizeTraits = process_types::internal::Traits32; | |
217 #else | |
218 using SizeTraits = process_types::internal::Traits64; | |
219 #endif | |
220 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information; | |
221 scoped_ptr<uint8_t[]> buffer; | |
222 process_information = | |
223 GetProcessInformation<process_types::internal::Traits64>(process_, | |
Mark Mentovai
2015/05/08 16:54:19
I don’t think that this will work for ARCH_CPU_32_
scottmg
2015/05/08 18:24:01
Quite right, wouldn't even compile. I was fiddling
| |
224 &buffer); | |
225 if (!process_information) | |
226 return; | |
227 | |
228 for (unsigned i = 0; i < process_information->NumberOfThreads; ++i) { | |
229 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>& | |
230 thread_info = process_information->Threads[i]; | |
231 Thread thread; | |
232 thread.id = thread_info.ClientId.UniqueThread; | |
233 // TODO(scottmg): I'm not sure how to get SuspendCount. A rough version is | |
Mark Mentovai
2015/05/08 16:54:20
Can you add some blank lines before these comments
scottmg
2015/05/08 18:24:02
Done.
| |
234 // to set a suspended count if not currently Running. | |
Mark Mentovai
2015/05/08 16:54:20
The ntexapi.h snippets I’ve found online make it s
scottmg
2015/05/08 18:24:02
Looking at https://msdn.microsoft.com/en-us/librar
| |
235 thread.suspend_count = | |
236 thread_info.ThreadState != static_cast<ULONG>(KTHREAD_STATE::Running) | |
237 ? 0 | |
238 : 1; | |
239 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from | |
240 // the Priority, BasePriority, and | |
241 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 . | |
242 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate | |
243 // both of those at the same time if it's useful. | |
244 thread.priority_class = NORMAL_PRIORITY_CLASS; | |
Mark Mentovai
2015/05/08 16:54:20
And also a blank line after this so it’s obvious t
scottmg
2015/05/08 18:24:01
Done.
| |
245 thread.priority = thread_info.Priority; | |
246 thread.teb = thread_info.TebBase; | |
247 // While there are semi-documented fields in the thread structure called | |
248 // StackBase and StackLimit, they don't appear to be correct in practice (or | |
Mark Mentovai
2015/05/08 16:54:19
I’m not totally surprised, the stack is more of a
scottmg
2015/05/08 18:24:01
They're funny, they sort of "seem" like the right
| |
249 // at least, I don't know how to interpret them). Instead, read the TIB | |
250 // (Thread Information Block) which is the first element of the TEB, and use | |
251 // its stack fields. | |
252 process_types::NT_TIB<SizeTraits> tib; | |
253 if (!ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) { | |
254 thread.stack_region_address = 0; | |
Mark Mentovai
2015/05/08 16:54:19
The constructor already zeroed out these fields.
scottmg
2015/05/08 18:24:01
Oops, yes. Forgot to remove them when I added Thre
| |
255 thread.stack_region_size = 0; | |
256 } else { | |
257 thread.stack_region_address = tib.StackBase; | |
258 // Note, "backwards" because of direction of stack growth. | |
259 thread.stack_region_size = tib.StackBase - tib.StackLimit; | |
260 } | |
261 threads_.push_back(thread); | |
262 } | |
263 } | |
264 | |
90 } // namespace crashpad | 265 } // namespace crashpad |
OLD | NEW |