| 6 | #define BASE_DEBUG_ACTIVITY_ANALYZER_H_ |
| 7 |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <set> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 |
| 14 | #include "base/base_export.h" |
| 15 | #include "base/debug/activity_tracker.h" |
| 16 | #include "base/memory/shared_memory_mapping.h" |
| 17 |
| 18 | namespace base { |
| 19 | namespace debug { |
| 20 |
| 21 | class GlobalActivityAnalyzer; |
| 22 |
| 23 | // This class provides analysis of data captured from a ThreadActivityTracker. |
| 24 | // When created, it takes a snapshot of the data held by the tracker and |
| 25 | // makes that information available to other code. |
| 26 | class BASE_EXPORT ThreadActivityAnalyzer { |
| 27 | public: |
| 28 | struct BASE_EXPORT Snapshot : ThreadActivityTracker::Snapshot { |
| 29 | Snapshot(); |
| 30 | ~Snapshot(); |
| 31 |
| 32 | // The user-data snapshot for an activity, matching the |activity_stack| |
| 33 | // of ThreadActivityTracker::Snapshot, if any. |
| 34 | std::vector<ActivityUserData::Snapshot> user_data_stack; |
| 35 | }; |
| 36 |
| 37 | // This class provides keys that uniquely identify a thread, even across |
| 38 | // multiple processes. |
| 39 | class ThreadKey { |
| 40 | public: |
| 41 | ThreadKey(int64_t pid, int64_t tid) : pid_(pid), tid_(tid) {} |
| 42 |
| 43 | bool operator<(const ThreadKey& rhs) const { |
| 44 | if (pid_ != rhs.pid_) |
| 45 | return pid_ < rhs.pid_; |
| 46 | return tid_ < rhs.tid_; |
| 47 | } |
| 48 |
| 49 | bool operator==(const ThreadKey& rhs) const { |
| 50 | return (pid_ == rhs.pid_ && tid_ == rhs.tid_); |
| 51 | } |
| 52 |
| 53 | private: |
| 54 | int64_t pid_; |
| 55 | int64_t tid_; |
| 56 | }; |
| 57 |
| 58 | // Creates an analyzer for an existing activity |tracker|. A snapshot is taken |
| 59 | // immediately and the tracker is not referenced again. |
| 60 | explicit ThreadActivityAnalyzer(const ThreadActivityTracker& tracker); |
| 61 |
| 62 | // Creates an analyzer for a block of memory currently or previously in-use |
| 63 | // by an activity-tracker. A snapshot is taken immediately and the memory |
| 64 | // is not referenced again. |
| 65 | ThreadActivityAnalyzer(void* base, size_t size); |
| 66 |
| 67 | // Creates an analyzer for a block of memory held within a persistent-memory |
| 68 | // |allocator| at the given |reference|. A snapshot is taken immediately and |
| 69 | // the memory is not referenced again. |
| 70 | ThreadActivityAnalyzer(PersistentMemoryAllocator* allocator, |
| 71 | PersistentMemoryAllocator::Reference reference); |
| 72 |
| 73 | ~ThreadActivityAnalyzer(); |
| 74 |
| 75 | // Adds information from the global analyzer. |
| 76 | void AddGlobalInformation(GlobalActivityAnalyzer* global); |
| 77 |
| 78 | // Returns true iff the contained data is valid. Results from all other |
| 79 | // methods are undefined if this returns false. |
| 80 | bool IsValid() { return activity_snapshot_valid_; } |
| 81 |
| 82 | // Gets the process id and its creation stamp. |
| 83 | int64_t GetProcessId(int64_t* out_stamp = nullptr) { |
| 84 | if (out_stamp) |
| 85 | *out_stamp = activity_snapshot_.create_stamp; |
| 86 | return activity_snapshot_.process_id; |
| 87 | } |
| 88 |
| 89 | // Gets the name of the thread. |
| 90 | const std::string& GetThreadName() { |
| 91 | return activity_snapshot_.thread_name; |
| 92 | } |
| 93 |
| 94 | // Gets the TheadKey for this thread. |
| 95 | ThreadKey GetThreadKey() { |
| 96 | return ThreadKey(activity_snapshot_.process_id, |
| 97 | activity_snapshot_.thread_id); |
| 98 | } |
| 99 |
| 100 | const Snapshot& activity_snapshot() { return activity_snapshot_; } |
| 101 |
| 102 | private: |
| 103 | friend class GlobalActivityAnalyzer; |
| 104 |
| 105 | // The snapshot of the activity tracker taken at the moment of construction. |
| 106 | Snapshot activity_snapshot_; |
| 107 |
| 108 | // Flag indicating if the snapshot data is valid. |
| 109 | bool activity_snapshot_valid_; |
| 110 |
| 111 | // A reference into a persistent memory allocator, used by the global |
| 112 | // analyzer to know where this tracker came from. |
| 113 | PersistentMemoryAllocator::Reference allocator_reference_ = 0; |
| 114 |
| 115 | DISALLOW_COPY_AND_ASSIGN(ThreadActivityAnalyzer); |
| 116 | }; |
| 117 |
| 118 |
| 119 | // This class manages analyzers for all known processes and threads as stored |
| 120 | // in a persistent memory allocator. It supports retrieval of them through |
| 121 | // iteration and directly using a ThreadKey, which allows for cross-references |
| 122 | // to be resolved. |
| 123 | // Note that though atomic snapshots are used and everything has its snapshot |
| 124 | // taken at the same time, the multi-snapshot itself is not atomic and thus may |
| 125 | // show small inconsistencies between threads if attempted on a live system. |
| 126 | class BASE_EXPORT GlobalActivityAnalyzer { |
| 127 | public: |
| 128 | struct ProgramLocation { |
| 129 | int module; |
| 130 | uintptr_t offset; |
| 131 | }; |
| 132 |
| 133 | using ThreadKey = ThreadActivityAnalyzer::ThreadKey; |
| 134 |
| 135 | // Creates a global analyzer from a persistent memory allocator. |
| 136 | explicit GlobalActivityAnalyzer( |
| 137 | std::unique_ptr<PersistentMemoryAllocator> allocator); |
| 138 |
| 139 | ~GlobalActivityAnalyzer(); |
| 140 |
| 141 | // Creates a global analyzer using a given persistent-memory |allocator|. |
| 142 | static std::unique_ptr<GlobalActivityAnalyzer> CreateWithAllocator( |
| 143 | std::unique_ptr<PersistentMemoryAllocator> allocator); |
| 144 |
| 145 | #if !defined(OS_NACL) |
| 146 | // Creates a global analyzer using the contents of a file given in |
| 147 | // |file_path|. |
| 148 | static std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile( |
| 149 | const FilePath& file_path); |
| 150 | #endif // !defined(OS_NACL) |
| 151 |
| 152 | // Like above but accesses an allocator in a mapped shared-memory segment. |
| 153 | static std::unique_ptr<GlobalActivityAnalyzer> CreateWithSharedMemory( |
| 154 | base::ReadOnlySharedMemoryMapping mapping); |
| 155 |
| 156 | // Iterates over all known valid processes and returns their PIDs or zero |
| 157 | // if there are no more. Calls to GetFirstProcess() will perform a global |
| 158 | // snapshot in order to provide a relatively consistent state across the |
| 159 | // future calls to GetNextProcess() and GetFirst/NextAnalyzer(). PIDs are |
| 160 | // returned in the order they're found meaning that a first-launched |
| 161 | // controlling process will be found first. Note, however, that space |
| 162 | // freed by an exiting process may be re-used by a later process. |
| 163 | int64_t GetFirstProcess(); |
| 164 | int64_t GetNextProcess(); |
| 165 |
| 166 | // Iterates over all known valid analyzers for the a given process or returns |
| 167 | // null if there are no more. |
| 168 | // |
| 169 | // GetFirstProcess() must be called first in order to capture a global |
| 170 | // snapshot! Ownership stays with the global analyzer object and all existing |
| 171 | // analyzer pointers are invalidated when GetFirstProcess() is called. |
| 172 | ThreadActivityAnalyzer* GetFirstAnalyzer(int64_t pid); |
| 173 | ThreadActivityAnalyzer* GetNextAnalyzer(); |
| 174 |
| 175 | // Gets the analyzer for a specific thread or null if there is none. |
| 176 | // Ownership stays with the global analyzer object. |
| 177 | ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key); |
| 178 |
| 179 | // Extract user data based on a reference and its identifier. |
| 180 | ActivityUserData::Snapshot GetUserDataSnapshot(int64_t pid, |
| 181 | uint32_t ref, |
| 182 | uint32_t id); |
| 183 |
| 184 | // Extract the data for a specific process. An empty snapshot will be |
| 185 | // returned if the process is not known. |
| 186 | const ActivityUserData::Snapshot& GetProcessDataSnapshot(int64_t pid); |
| 187 |
| 188 | // Gets all log messages stored within. |
| 189 | std::vector<std::string> GetLogMessages(); |
| 190 |
| 191 | // Gets modules corresponding to a pid. This pid must come from a call to |
| 192 | // GetFirst/NextProcess. Only modules that were first registered prior to |
| 193 | // GetFirstProcess's snapshot are returned. |
| 194 | std::vector<GlobalActivityTracker::ModuleInfo> GetModules(int64_t pid); |
| 195 |
| 196 | // Gets the corresponding "program location" for a given "program counter". |
| 197 | // This will return {0,0} if no mapping could be found. |
| 198 | ProgramLocation GetProgramLocationFromAddress(uint64_t address); |
| 199 |
| 200 | // Returns whether the data is complete. Data can be incomplete if the |
| 201 | // recording size quota is hit. |
| 202 | bool IsDataComplete() const; |
| 203 |
| 204 | private: |
| 205 | using AnalyzerMap = |
| 206 | std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>; |
| 207 |
| 208 | struct UserDataSnapshot { |
| 209 | // Complex class needs out-of-line ctor/dtor. |
| 210 | UserDataSnapshot(); |
| 211 | UserDataSnapshot(const UserDataSnapshot& rhs); |
| 212 | UserDataSnapshot(UserDataSnapshot&& rhs); |
| 213 | ~UserDataSnapshot(); |
| 214 |
| 215 | int64_t process_id; |
| 216 | int64_t create_stamp; |
| 217 | ActivityUserData::Snapshot data; |
| 218 | }; |
| 219 |
| 220 | // Finds, creates, and indexes analyzers for all known processes and threads. |
| 221 | void PrepareAllAnalyzers(); |
| 222 |
| 223 | // The persistent memory allocator holding all tracking data. |
| 224 | std::unique_ptr<PersistentMemoryAllocator> allocator_; |
| 225 |
| 226 | // The time stamp when analysis began. This is used to prevent looking into |
| 227 | // process IDs that get reused when analyzing a live system. |
| 228 | int64_t analysis_stamp_; |
| 229 |
| 230 | // The iterator for finding tracking information in the allocator. |
| 231 | PersistentMemoryAllocator::Iterator allocator_iterator_; |
| 232 |
| 233 | // A set of all interesting memory references found within the allocator. |
| 234 | std::set<PersistentMemoryAllocator::Reference> memory_references_; |
| 235 |
| 236 | // A set of all process-data memory references found within the allocator. |
| 237 | std::map<int64_t, UserDataSnapshot> process_data_; |
| 238 |
| 239 | // A set of all process IDs collected during PrepareAllAnalyzers. These are |
| 240 | // popped and returned one-by-one with calls to GetFirst/NextProcess(). |
| 241 | std::vector<int64_t> process_ids_; |
| 242 |
| 243 | // A map, keyed by ThreadKey, of all valid activity analyzers. |
| 244 | AnalyzerMap analyzers_; |
| 245 |
| 246 | // The iterator within the analyzers_ map for returning analyzers through |
| 247 | // first/next iteration. |
| 248 | AnalyzerMap::iterator analyzers_iterator_; |
| 249 | int64_t analyzers_iterator_pid_; |
| 250 |
| 251 | DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer); |
| 252 | }; |
| 253 |
| 254 | } // namespace debug |
| 255 | } // namespace base |
| 256 |
| 257 | #endif // BASE_DEBUG_ACTIVITY_ANALYZER_H_ |
| 258 |