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

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: don't bother range checking kernel data 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"
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)
46 #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
47
48 // Kernel mode enumerations for thread state.
49 // See http://www.nirsoft.net/kernel_struct/vista/KTHREAD_STATE.html and
50 // https://msdn.microsoft.com/en-us/library/system.diagnostics.threadstate(v=vs. 110).aspx
51 enum class KTHREAD_STATE : uint32_t {
Mark Mentovai 2015/05/08 20:30:21 KTHREAD_STATE intentional instead of KThreadState?
scottmg 2015/05/08 21:05:44 That was to match the standard kernel/ntinternals
52 kkInitialized,
53 kReady,
54 kRunning,
55 kStandby,
56 kTerminated,
57 kWaiting,
58 kTransition,
59 kDeferredReady,
60 kGateWait,
61 };
62
63 // Gets a pointer to the process information structure after a given one, or
64 // null when iteration is complete, assuming they've been retrieved in a block
65 // via NtQuerySystemInformation().
66 template <class Traits>
67 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* NextProcess(
68 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process) {
69 ULONG offset = process->NextEntryOffset;
70 if (offset == 0)
71 return nullptr;
72 return reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>(
73 reinterpret_cast<uint8_t*>(process) + offset);
74 }
75
76 //! \brief Retrieves the SYSTEM_PROCESS_INFORMATION for a given process.
77 //!
78 //! The returned pointer points into the memory block stored by \a buffer.
79 //! Ownership of \a buffer is transferred to the caller.
80 //!
81 //! \return Pointer to the process' data, or nullptr if it was not found or on
82 //! error. On error, a message will be logged.
83 template <class Traits>
84 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* GetProcessInformation(
85 HANDLE process_handle,
86 scoped_ptr<uint8_t[]>* buffer) {
87 ULONG buffer_size = 16384;
88 buffer->reset(new uint8_t[buffer_size]);
89 NTSTATUS status;
90 // This must be in retry loop, as we're racing with process creation on the
91 // system to find a buffer large enough to hold all process information.
92 for (int tries = 0; tries < 20; ++tries) {
93 const int kSystemExtendedProcessInformation = 57;
94 status = crashpad::NtQuerySystemInformation(
95 static_cast<SYSTEM_INFORMATION_CLASS>(
96 kSystemExtendedProcessInformation),
97 reinterpret_cast<void*>(buffer->get()),
98 buffer_size,
99 &buffer_size);
100 if (status == STATUS_BUFFER_TOO_SMALL ||
101 status == STATUS_INFO_LENGTH_MISMATCH) {
102 buffer->reset(new uint8_t[buffer_size]);
Mark Mentovai 2015/05/08 20:30:21 No slop? (It’s OK if you’re against doing it, but
scottmg 2015/05/08 21:05:44 Oops, I did it, then undid it when I added the ran
103 } else {
104 break;
105 }
106 }
107
108 if (!NT_SUCCESS(status)) {
109 LOG(ERROR) << "NtQuerySystemInformation failed: " << std::hex << status;
110 return nullptr;
111 }
112
113 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process =
114 reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>(
115 buffer->get());
116 DWORD process_id = GetProcessId(process_handle);
117 do {
118 if (process->UniqueProcessId == process_id)
119 return process;
120 } while (process = NextProcess(process));
121
122 LOG(ERROR) << "process " << process_id << " not found";
123 return nullptr;
124 }
125
126 } // namespace
127
128 ProcessReaderWin::Thread::Thread()
129 : id(0),
130 teb(0),
131 stack_region_address(0),
132 stack_region_size(0),
133 suspend_count(0),
134 priority_class(0),
135 priority(0) {
136 }
137
22 ProcessReaderWin::ProcessReaderWin() 138 ProcessReaderWin::ProcessReaderWin()
23 : process_(INVALID_HANDLE_VALUE), 139 : process_(INVALID_HANDLE_VALUE),
24 process_info_(), 140 process_info_(),
25 modules_(), 141 modules_(),
Mark Mentovai 2015/05/08 20:30:21 threads_(),
scottmg 2015/05/08 21:05:44 Done.
142 initialized_threads_(false),
26 initialized_() { 143 initialized_() {
27 } 144 }
28 145
29 ProcessReaderWin::~ProcessReaderWin() { 146 ProcessReaderWin::~ProcessReaderWin() {
30 } 147 }
31 148
32 bool ProcessReaderWin::Initialize(HANDLE process) { 149 bool ProcessReaderWin::Initialize(HANDLE process) {
33 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); 150 INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
34 151
35 process_ = process; 152 process_ = process;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
200 if (initialized_threads_)
201 return threads_;
202
Mark Mentovai 2015/05/08 20:30:21 Set initialized_threads_ to true here, so that if
scottmg 2015/05/08 21:05:44 Done.
203 DCHECK(threads_.empty());
204
205 #if ARCH_CPU_32_BITS
206 using SizeTraits = process_types::internal::Traits32;
207 #else
208 using SizeTraits = process_types::internal::Traits64;
209 #endif
210 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information;
Mark Mentovai 2015/05/08 20:30:21 No need to declare this until you initialize it, a
scottmg 2015/05/08 21:05:45 Done.
211 scoped_ptr<uint8_t[]> buffer;
212 process_information = GetProcessInformation<SizeTraits>(process_, &buffer);
213 if (!process_information)
214 return threads_;
215
216 for (unsigned i = 0; i < process_information->NumberOfThreads; ++i) {
Mark Mentovai 2015/05/08 20:30:21 “unsigned long” to match decltype(process_informat
scottmg 2015/05/08 21:05:44 Done.
217 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>&
218 thread_info = process_information->Threads[i];
219 Thread thread;
220 thread.id = thread_info.ClientId.UniqueThread;
221
222 // TODO(scottmg): I'm not sure how to get SuspendCount. A rough version is
223 // to set a suspended count if if's currently waiting.
224 thread.suspend_count =
225 thread_info.ThreadState == static_cast<ULONG>(KTHREAD_STATE::kGateWait)
Mark Mentovai 2015/05/08 20:30:21 Not that Stack Overflow is known for correctness,
scottmg 2015/05/08 21:05:45 I think that's "sort of" right, in that that is wh
Mark Mentovai 2015/05/08 21:15:00 scottmg wrote:
scottmg 2015/05/09 02:46:15 I see, my mistake. I created a thread with CREATE
226 ? 1
227 : 0;
228
229 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from
230 // the Priority, BasePriority, and
231 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 .
232 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate
233 // both of those at the same time if it's useful.
234 thread.priority_class = NORMAL_PRIORITY_CLASS;
235
236 thread.priority = thread_info.Priority;
237 thread.teb = thread_info.TebBase;
238
239 // While there are semi-documented fields in the thread structure called
240 // StackBase and StackLimit, they don't appear to be correct in practice (or
241 // at least, I don't know how to interpret them). Instead, read the TIB
242 // (Thread Information Block) which is the first element of the TEB, and use
243 // its stack fields.
244 process_types::NT_TIB<SizeTraits> tib;
245 if (ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) {
246 thread.stack_region_address = tib.StackBase;
247 // Note, "backwards" because of direction of stack growth.
248 thread.stack_region_size = tib.StackBase - tib.StackLimit;
249 }
250 threads_.push_back(thread);
251 }
252
253 initialized_threads_ = true;
254 return threads_;
255 }
256
80 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() { 257 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() {
81 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 258 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
82 259
83 if (!process_info_.Modules(&modules_)) { 260 if (!process_info_.Modules(&modules_)) {
84 LOG(ERROR) << "couldn't retrieve modules"; 261 LOG(ERROR) << "couldn't retrieve modules";
85 } 262 }
86 263
87 return modules_; 264 return modules_;
88 } 265 }
89 266
90 } // namespace crashpad 267 } // 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