OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_DEBUG_ACTIVITY_ANALYZER_H_ | 5 #ifndef BASE_DEBUG_ACTIVITY_ANALYZER_H_ |
6 #define BASE_DEBUG_ACTIVITY_ANALYZER_H_ | 6 #define BASE_DEBUG_ACTIVITY_ANALYZER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
| 12 #include <vector> |
12 | 13 |
13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
14 #include "base/debug/activity_tracker.h" | 15 #include "base/debug/activity_tracker.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 namespace debug { | 18 namespace debug { |
18 | 19 |
19 class GlobalActivityAnalyzer; | 20 class GlobalActivityAnalyzer; |
20 | 21 |
21 // This class provides analysis of data captured from a ThreadActivityTracker. | 22 // This class provides analysis of data captured from a ThreadActivityTracker. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 71 |
71 ~ThreadActivityAnalyzer(); | 72 ~ThreadActivityAnalyzer(); |
72 | 73 |
73 // Adds information from the global analyzer. | 74 // Adds information from the global analyzer. |
74 void AddGlobalInformation(GlobalActivityAnalyzer* global); | 75 void AddGlobalInformation(GlobalActivityAnalyzer* global); |
75 | 76 |
76 // Returns true iff the contained data is valid. Results from all other | 77 // Returns true iff the contained data is valid. Results from all other |
77 // methods are undefined if this returns false. | 78 // methods are undefined if this returns false. |
78 bool IsValid() { return activity_snapshot_valid_; } | 79 bool IsValid() { return activity_snapshot_valid_; } |
79 | 80 |
| 81 // Gets the process id and its creation stamp. |
| 82 int64_t GetProcessId(int64_t* out_stamp = nullptr) { |
| 83 if (out_stamp) |
| 84 *out_stamp = activity_snapshot_.create_stamp; |
| 85 return activity_snapshot_.process_id; |
| 86 } |
| 87 |
80 // Gets the name of the thread. | 88 // Gets the name of the thread. |
81 const std::string& GetThreadName() { | 89 const std::string& GetThreadName() { |
82 return activity_snapshot_.thread_name; | 90 return activity_snapshot_.thread_name; |
83 } | 91 } |
84 | 92 |
85 // Gets the TheadKey for this thread. | 93 // Gets the TheadKey for this thread. |
86 ThreadKey GetThreadKey() { | 94 ThreadKey GetThreadKey() { |
87 return ThreadKey(activity_snapshot_.process_id, | 95 return ThreadKey(activity_snapshot_.process_id, |
88 activity_snapshot_.thread_id); | 96 activity_snapshot_.thread_id); |
89 } | 97 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 137 |
130 ~GlobalActivityAnalyzer(); | 138 ~GlobalActivityAnalyzer(); |
131 | 139 |
132 #if !defined(OS_NACL) | 140 #if !defined(OS_NACL) |
133 // Creates a global analyzer using the contents of a file given in | 141 // Creates a global analyzer using the contents of a file given in |
134 // |file_path|. | 142 // |file_path|. |
135 static std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile( | 143 static std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile( |
136 const FilePath& file_path); | 144 const FilePath& file_path); |
137 #endif // !defined(OS_NACL) | 145 #endif // !defined(OS_NACL) |
138 | 146 |
139 // Iterates over all known valid analyzers or returns null if there are no | 147 // Iterates over all known valid processes and returns their PIDs or zero |
140 // more. Ownership stays with the global analyzer object and all existing | 148 // if there are no more. Calls to GetFirstProcess() will perform a global |
141 // analyzer pointers are invalidated when GetFirstAnalyzer() is called. | 149 // snapshot in order to provide a relatively consistent state across the |
142 ThreadActivityAnalyzer* GetFirstAnalyzer(); | 150 // future calls to GetNextProcess() and GetFirst/NextAnalyzer(). PIDs are |
| 151 // returned in the order they're found meaning that a first-launched |
| 152 // controlling process will be found first. Note, however, that space |
| 153 // freed by an exiting process may be re-used by a later process. |
| 154 int64_t GetFirstProcess(); |
| 155 int64_t GetNextProcess(); |
| 156 |
| 157 // Iterates over all known valid analyzers for the a given process or returns |
| 158 // null if there are no more. |
| 159 // |
| 160 // GetFirstProcess() must be called first in order to capture a global |
| 161 // snapshot! Ownership stays with the global analyzer object and all existing |
| 162 // analyzer pointers are invalidated when GetFirstProcess() is called. |
| 163 ThreadActivityAnalyzer* GetFirstAnalyzer(int64_t pid); |
143 ThreadActivityAnalyzer* GetNextAnalyzer(); | 164 ThreadActivityAnalyzer* GetNextAnalyzer(); |
144 | 165 |
145 // Gets the analyzer for a specific thread or null if there is none. | 166 // Gets the analyzer for a specific thread or null if there is none. |
146 // Ownership stays with the global analyzer object. | 167 // Ownership stays with the global analyzer object. |
147 ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key); | 168 ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key); |
148 | 169 |
149 // Extract user data based on a reference and its identifier. | 170 // Extract user data based on a reference and its identifier. |
150 ActivityUserData::Snapshot GetUserDataSnapshot(uint32_t ref, uint32_t id); | 171 ActivityUserData::Snapshot GetUserDataSnapshot(int64_t pid, |
| 172 uint32_t ref, |
| 173 uint32_t id); |
151 | 174 |
152 // Extract the global user data. | 175 // Extract the data for a specific process. An empty snapshot will be |
153 ActivityUserData::Snapshot GetGlobalUserDataSnapshot(); | 176 // returned if the process is not known. |
| 177 const ActivityUserData::Snapshot& GetProcessDataSnapshot(int64_t pid); |
| 178 |
| 179 // Extract the global data. |
| 180 const ActivityUserData::Snapshot& GetGlobalDataSnapshot(); |
154 | 181 |
155 // Gets all log messages stored within. | 182 // Gets all log messages stored within. |
156 std::vector<std::string> GetLogMessages(); | 183 std::vector<std::string> GetLogMessages(); |
157 | 184 |
158 // Gets all the known modules. | 185 // Gets all the known modules. |
159 std::vector<GlobalActivityTracker::ModuleInfo> GetModules(); | 186 std::vector<GlobalActivityTracker::ModuleInfo> GetModules(); |
160 | 187 |
161 // Gets the corresponding "program location" for a given "program counter". | 188 // Gets the corresponding "program location" for a given "program counter". |
162 // This will return {0,0} if no mapping could be found. | 189 // This will return {0,0} if no mapping could be found. |
163 ProgramLocation GetProgramLocationFromAddress(uint64_t address); | 190 ProgramLocation GetProgramLocationFromAddress(uint64_t address); |
164 | 191 |
165 private: | 192 private: |
166 using AnalyzerMap = | 193 using AnalyzerMap = |
167 std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>; | 194 std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>; |
168 | 195 |
| 196 struct UserDataSnapshot { |
| 197 // Complex class needs out-of-line ctor/dtor. |
| 198 UserDataSnapshot(); |
| 199 UserDataSnapshot(const UserDataSnapshot& rhs); |
| 200 UserDataSnapshot(UserDataSnapshot&& rhs); |
| 201 ~UserDataSnapshot(); |
| 202 |
| 203 int64_t process_id; |
| 204 int64_t create_stamp; |
| 205 ActivityUserData::Snapshot data; |
| 206 }; |
| 207 |
169 // Finds, creates, and indexes analyzers for all known processes and threads. | 208 // Finds, creates, and indexes analyzers for all known processes and threads. |
170 void PrepareAllAnalyzers(); | 209 void PrepareAllAnalyzers(); |
171 | 210 |
172 // The persistent memory allocator holding all tracking data. | 211 // The persistent memory allocator holding all tracking data. |
173 std::unique_ptr<PersistentMemoryAllocator> allocator_; | 212 std::unique_ptr<PersistentMemoryAllocator> allocator_; |
174 | 213 |
| 214 // The time stamp when analysis began. This is used to prevent looking into |
| 215 // process IDs that get reused when analyzing a live system. |
| 216 int64_t analysis_stamp_; |
| 217 |
175 // The iterator for finding tracking information in the allocator. | 218 // The iterator for finding tracking information in the allocator. |
176 PersistentMemoryAllocator::Iterator allocator_iterator_; | 219 PersistentMemoryAllocator::Iterator allocator_iterator_; |
177 | 220 |
178 // A set of all tracker memory references found within the allocator. | 221 // A set of all interesting memory references found within the allocator. |
179 std::set<PersistentMemoryAllocator::Reference> tracker_references_; | 222 std::set<PersistentMemoryAllocator::Reference> memory_references_; |
| 223 |
| 224 // A set of all process-data memory references found within the allocator. |
| 225 std::map<int64_t, UserDataSnapshot> process_data_; |
| 226 |
| 227 // A set of all process IDs collected during PrepareAllAnalyzers. These are |
| 228 // popped and returned one-by-one with calls to GetFirst/NextProcess(). |
| 229 std::vector<int64_t> process_ids_; |
180 | 230 |
181 // A map, keyed by ThreadKey, of all valid activity analyzers. | 231 // A map, keyed by ThreadKey, of all valid activity analyzers. |
182 AnalyzerMap analyzers_; | 232 AnalyzerMap analyzers_; |
183 | 233 |
184 // The iterator within the analyzers_ map for returning analyzers through | 234 // The iterator within the analyzers_ map for returning analyzers through |
185 // first/next iteration. | 235 // first/next iteration. |
186 AnalyzerMap::iterator analyzers_iterator_; | 236 AnalyzerMap::iterator analyzers_iterator_; |
| 237 int64_t analyzers_iterator_pid_; |
| 238 |
| 239 // Snapshot of the global data. |
| 240 ActivityUserData::Snapshot global_data_snapshot_; |
187 | 241 |
188 DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer); | 242 DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer); |
189 }; | 243 }; |
190 | 244 |
191 } // namespace debug | 245 } // namespace debug |
192 } // namespace base | 246 } // namespace base |
193 | 247 |
194 #endif // BASE_DEBUG_ACTIVITY_ANALYZER_H_ | 248 #endif // BASE_DEBUG_ACTIVITY_ANALYZER_H_ |
OLD | NEW |