| 6 | #define BASE_TRACE_EVENT_TRACE_LOG_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 |
| 11 | #include <atomic> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <unordered_map> |
| 15 | #include <vector> |
| 16 |
| 17 | #include "base/atomicops.h" |
| 18 | #include "base/containers/stack.h" |
| 19 | #include "base/gtest_prod_util.h" |
| 20 | #include "base/macros.h" |
| 21 | #include "base/memory/scoped_refptr.h" |
| 22 | #include "base/single_thread_task_runner.h" |
| 23 | #include "base/time/time_override.h" |
| 24 | #include "base/trace_event/category_registry.h" |
| 25 | #include "base/trace_event/memory_dump_provider.h" |
| 26 | #include "base/trace_event/trace_config.h" |
| 27 | #include "base/trace_event/trace_event_impl.h" |
| 28 | #include "build/build_config.h" |
| 29 |
| 30 | namespace base { |
| 31 | class RefCountedString; |
| 32 |
| 33 | template <typename T> |
| 34 | class NoDestructor; |
| 35 |
| 36 | namespace trace_event { |
| 37 |
| 38 | struct TraceCategory; |
| 39 | class TraceBuffer; |
| 40 | class TraceBufferChunk; |
| 41 | class TraceEvent; |
| 42 | class TraceEventFilter; |
| 43 | class TraceEventMemoryOverhead; |
| 44 |
| 45 | struct BASE_EXPORT TraceLogStatus { |
| 46 | TraceLogStatus(); |
| 47 | ~TraceLogStatus(); |
| 48 | uint32_t event_capacity; |
| 49 | uint32_t event_count; |
| 50 | }; |
| 51 |
| 52 | class BASE_EXPORT TraceLog : public MemoryDumpProvider { |
| 53 | public: |
| 54 | // Argument passed to TraceLog::SetEnabled. |
| 55 | enum Mode : uint8_t { |
| 56 | // Enables normal tracing (recording trace events in the trace buffer). |
| 57 | RECORDING_MODE = 1 << 0, |
| 58 |
| 59 | // Trace events are enabled just for filtering but not for recording. Only |
| 60 | // event filters config of |trace_config| argument is used. |
| 61 | FILTERING_MODE = 1 << 1 |
| 62 | }; |
| 63 |
| 64 | static TraceLog* GetInstance(); |
| 65 |
| 66 | // Retrieves a copy (for thread-safety) of the current TraceConfig. |
| 67 | TraceConfig GetCurrentTraceConfig() const; |
| 68 |
| 69 | // Initializes the thread-local event buffer, if not already initialized and |
| 70 | // if the current thread supports that (has a message loop). |
| 71 | void InitializeThreadLocalEventBufferIfSupported(); |
| 72 |
| 73 | // See TraceConfig comments for details on how to control which categories |
| 74 | // will be traced. SetDisabled must be called distinctly for each mode that is |
| 75 | // enabled. If tracing has already been enabled for recording, category filter |
| 76 | // (enabled and disabled categories) will be merged into the current category |
| 77 | // filter. Enabling RECORDING_MODE does not enable filters. Trace event |
| 78 | // filters will be used only if FILTERING_MODE is set on |modes_to_enable|. |
| 79 | // Conversely to RECORDING_MODE, FILTERING_MODE doesn't support upgrading, |
| 80 | // i.e. filters can only be enabled if not previously enabled. |
| 81 | void SetEnabled(const TraceConfig& trace_config, uint8_t modes_to_enable); |
| 82 |
| 83 | // TODO(ssid): Remove the default SetEnabled and IsEnabled. They should take |
| 84 | // Mode as argument. |
| 85 |
| 86 | // Disables tracing for all categories for the specified |modes_to_disable| |
| 87 | // only. Only RECORDING_MODE is taken as default |modes_to_disable|. |
| 88 | void SetDisabled(); |
| 89 | void SetDisabled(uint8_t modes_to_disable); |
| 90 |
| 91 | // Returns true if TraceLog is enabled on recording mode. |
| 92 | // Note: Returns false even if FILTERING_MODE is enabled. |
| 93 | bool IsEnabled() { |
| 94 | AutoLock lock(lock_); |
| 95 | return enabled_modes_ & RECORDING_MODE; |
| 96 | } |
| 97 |
| 98 | // Returns a bitmap of enabled modes from TraceLog::Mode. |
| 99 | uint8_t enabled_modes() { return enabled_modes_; } |
| 100 |
| 101 | // The number of times we have begun recording traces. If tracing is off, |
| 102 | // returns -1. If tracing is on, then it returns the number of times we have |
| 103 | // recorded a trace. By watching for this number to increment, you can |
| 104 | // passively discover when a new trace has begun. This is then used to |
| 105 | // implement the TRACE_EVENT_IS_NEW_TRACE() primitive. |
| 106 | int GetNumTracesRecorded(); |
| 107 |
| 108 | #if defined(OS_ANDROID) |
| 109 | void StartATrace(); |
| 110 | void StopATrace(); |
| 111 | void AddClockSyncMetadataEvent(); |
| 112 | #endif |
| 113 |
| 114 | // Enabled state listeners give a callback when tracing is enabled or |
| 115 | // disabled. This can be used to tie into other library's tracing systems |
| 116 | // on-demand. |
| 117 | class BASE_EXPORT EnabledStateObserver { |
| 118 | public: |
| 119 | virtual ~EnabledStateObserver() = default; |
| 120 |
| 121 | // Called just after the tracing system becomes enabled, outside of the |
| 122 | // |lock_|. TraceLog::IsEnabled() is true at this point. |
| 123 | virtual void OnTraceLogEnabled() = 0; |
| 124 |
| 125 | // Called just after the tracing system disables, outside of the |lock_|. |
| 126 | // TraceLog::IsEnabled() is false at this point. |
| 127 | virtual void OnTraceLogDisabled() = 0; |
| 128 | }; |
| 129 | // Adds an observer. Cannot be called from within the observer callback. |
| 130 | void AddEnabledStateObserver(EnabledStateObserver* listener); |
| 131 | // Removes an observer. Cannot be called from within the observer callback. |
| 132 | void RemoveEnabledStateObserver(EnabledStateObserver* listener); |
| 133 | // Adds an observer that is owned by TraceLog. This is useful for agents that |
| 134 | // implement tracing feature that needs to stay alive as long as TraceLog |
| 135 | // does. |
| 136 | void AddOwnedEnabledStateObserver( |
| 137 | std::unique_ptr<EnabledStateObserver> listener); |
| 138 | bool HasEnabledStateObserver(EnabledStateObserver* listener) const; |
| 139 |
| 140 | // Asynchronous enabled state listeners. When tracing is enabled or disabled, |
| 141 | // for each observer, a task for invoking its appropriate callback is posted |
| 142 | // to the thread from which AddAsyncEnabledStateObserver() was called. This |
| 143 | // allows the observer to be safely destroyed, provided that it happens on the |
| 144 | // same thread that invoked AddAsyncEnabledStateObserver(). |
| 145 | class BASE_EXPORT AsyncEnabledStateObserver { |
| 146 | public: |
| 147 | virtual ~AsyncEnabledStateObserver() = default; |
| 148 |
| 149 | // Posted just after the tracing system becomes enabled, outside |lock_|. |
| 150 | // TraceLog::IsEnabled() is true at this point. |
| 151 | virtual void OnTraceLogEnabled() = 0; |
| 152 |
| 153 | // Posted just after the tracing system becomes disabled, outside |lock_|. |
| 154 | // TraceLog::IsEnabled() is false at this point. |
| 155 | virtual void OnTraceLogDisabled() = 0; |
| 156 | }; |
| 157 | // TODO(oysteine): This API originally needed to use WeakPtrs as the observer |
| 158 | // list was copied under the global trace lock, but iterated over outside of |
| 159 | // that lock so that observers could add tracing. The list is now protected by |
| 160 | // its own lock, so this can be changed to a raw ptr. |
| 161 | void AddAsyncEnabledStateObserver( |
| 162 | WeakPtr<AsyncEnabledStateObserver> listener); |
| 163 | void RemoveAsyncEnabledStateObserver(AsyncEnabledStateObserver* listener); |
| 164 | bool HasAsyncEnabledStateObserver(AsyncEnabledStateObserver* listener) const; |
| 165 |
| 166 | TraceLogStatus GetStatus() const; |
| 167 | bool BufferIsFull() const; |
| 168 |
| 169 | // Computes an estimate of the size of the TraceLog including all the retained |
| 170 | // objects. |
| 171 | void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 172 |
| 173 | void SetArgumentFilterPredicate( |
| 174 | const ArgumentFilterPredicate& argument_filter_predicate); |
| 175 | ArgumentFilterPredicate GetArgumentFilterPredicate() const; |
| 176 |
| 177 | void SetMetadataFilterPredicate( |
| 178 | const MetadataFilterPredicate& metadata_filter_predicate); |
| 179 | MetadataFilterPredicate GetMetadataFilterPredicate() const; |
| 180 |
| 181 | // Flush all collected events to the given output callback. The callback will |
| 182 | // be called one or more times either synchronously or asynchronously from |
| 183 | // the current thread with IPC-bite-size chunks. The string format is |
| 184 | // undefined. Use TraceResultBuffer to convert one or more trace strings to |
| 185 | // JSON. The callback can be null if the caller doesn't want any data. |
| 186 | // Due to the implementation of thread-local buffers, flush can't be |
| 187 | // done when tracing is enabled. If called when tracing is enabled, the |
| 188 | // callback will be called directly with (empty_string, false) to indicate |
| 189 | // the end of this unsuccessful flush. Flush does the serialization |
| 190 | // on the same thread if the caller doesn't set use_worker_thread explicitly. |
| 191 | using OutputCallback = |
| 192 | base::RepeatingCallback<void(const scoped_refptr<base::RefCountedString>&, |
| 193 | bool has_more_events)>; |
| 194 | void Flush(const OutputCallback& cb, bool use_worker_thread = false); |
| 195 |
| 196 | // Cancels tracing and discards collected data. |
| 197 | void CancelTracing(const OutputCallback& cb); |
| 198 |
| 199 | using AddTraceEventOverrideFunction = void (*)(TraceEvent*, |
| 200 | bool thread_will_flush, |
| 201 | TraceEventHandle* handle); |
| 202 | using OnFlushFunction = void (*)(); |
| 203 | using UpdateDurationFunction = |
| 204 | void (*)(const unsigned char* category_group_enabled, |
| 205 | const char* name, |
| 206 | TraceEventHandle handle, |
| 207 | int thread_id, |
| 208 | bool explicit_timestamps, |
| 209 | const TimeTicks& now, |
| 210 | const ThreadTicks& thread_now, |
| 211 | ThreadInstructionCount thread_instruction_now); |
| 212 | // The callbacks will be called up until the point where the flush is |
| 213 | // finished, i.e. must be callable until OutputCallback is called with |
| 214 | // has_more_events==false. |
| 215 | void SetAddTraceEventOverrides( |
| 216 | const AddTraceEventOverrideFunction& add_event_override, |
| 217 | const OnFlushFunction& on_flush_callback, |
| 218 | const UpdateDurationFunction& update_duration_callback); |
| 219 |
| 220 | // Called by TRACE_EVENT* macros, don't call this directly. |
| 221 | // The name parameter is a category group for example: |
| 222 | // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent") |
| 223 | static const unsigned char* GetCategoryGroupEnabled(const char* name); |
| 224 | static const char* GetCategoryGroupName( |
| 225 | const unsigned char* category_group_enabled); |
| 226 | static constexpr const unsigned char* GetBuiltinCategoryEnabled( |
| 227 | const char* name) { |
| 228 | TraceCategory* builtin_category = |
| 229 | CategoryRegistry::GetBuiltinCategoryByName(name); |
| 230 | if (builtin_category) |
| 231 | return builtin_category->state_ptr(); |
| 232 | return nullptr; |
| 233 | } |
| 234 |
| 235 | // Called by TRACE_EVENT* macros, don't call this directly. |
| 236 | // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied |
| 237 | // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. |
| 238 | bool ShouldAddAfterUpdatingState(char phase, |
| 239 | const unsigned char* category_group_enabled, |
| 240 | const char* name, |
| 241 | unsigned long long id, |
| 242 | int thread_id, |
| 243 | TraceArguments* args); |
| 244 | TraceEventHandle AddTraceEvent(char phase, |
| 245 | const unsigned char* category_group_enabled, |
| 246 | const char* name, |
| 247 | const char* scope, |
| 248 | unsigned long long id, |
| 249 | TraceArguments* args, |
| 250 | unsigned int flags); |
| 251 | TraceEventHandle AddTraceEventWithBindId( |
| 252 | char phase, |
| 253 | const unsigned char* category_group_enabled, |
| 254 | const char* name, |
| 255 | const char* scope, |
| 256 | unsigned long long id, |
| 257 | unsigned long long bind_id, |
| 258 | TraceArguments* args, |
| 259 | unsigned int flags); |
| 260 | TraceEventHandle AddTraceEventWithProcessId( |
| 261 | char phase, |
| 262 | const unsigned char* category_group_enabled, |
| 263 | const char* name, |
| 264 | const char* scope, |
| 265 | unsigned long long id, |
| 266 | int process_id, |
| 267 | TraceArguments* args, |
| 268 | unsigned int flags); |
| 269 | TraceEventHandle AddTraceEventWithThreadIdAndTimestamp( |
| 270 | char phase, |
| 271 | const unsigned char* category_group_enabled, |
| 272 | const char* name, |
| 273 | const char* scope, |
| 274 | unsigned long long id, |
| 275 | int thread_id, |
| 276 | const TimeTicks& timestamp, |
| 277 | TraceArguments* args, |
| 278 | unsigned int flags); |
| 279 | TraceEventHandle AddTraceEventWithThreadIdAndTimestamp( |
| 280 | char phase, |
| 281 | const unsigned char* category_group_enabled, |
| 282 | const char* name, |
| 283 | const char* scope, |
| 284 | unsigned long long id, |
| 285 | unsigned long long bind_id, |
| 286 | int thread_id, |
| 287 | const TimeTicks& timestamp, |
| 288 | TraceArguments* args, |
| 289 | unsigned int flags); |
| 290 |
| 291 | // Adds a metadata event that will be written when the trace log is flushed. |
| 292 | void AddMetadataEvent(const unsigned char* category_group_enabled, |
| 293 | const char* name, |
| 294 | TraceArguments* args, |
| 295 | unsigned int flags); |
| 296 |
| 297 | void UpdateTraceEventDuration(const unsigned char* category_group_enabled, |
| 298 | const char* name, |
| 299 | TraceEventHandle handle); |
| 300 |
| 301 | void UpdateTraceEventDurationExplicit( |
| 302 | const unsigned char* category_group_enabled, |
| 303 | const char* name, |
| 304 | TraceEventHandle handle, |
| 305 | int thread_id, |
| 306 | bool explicit_timestamps, |
| 307 | const TimeTicks& now, |
| 308 | const ThreadTicks& thread_now, |
| 309 | ThreadInstructionCount thread_instruction_now); |
| 310 |
| 311 | void EndFilteredEvent(const unsigned char* category_group_enabled, |
| 312 | const char* name, |
| 313 | TraceEventHandle handle); |
| 314 |
| 315 | int process_id() const { return process_id_; } |
| 316 | const std::string& process_name() const { return process_name_; } |
| 317 |
| 318 | uint64_t MangleEventId(uint64_t id); |
| 319 |
| 320 | // Exposed for unittesting: |
| 321 |
| 322 | // Testing factory for TraceEventFilter. |
| 323 | typedef std::unique_ptr<TraceEventFilter> (*FilterFactoryForTesting)( |
| 324 | const std::string& /* predicate_name */); |
| 325 | void SetFilterFactoryForTesting(FilterFactoryForTesting factory) { |
| 326 | filter_factory_for_testing_ = factory; |
| 327 | } |
| 328 |
| 329 | // Allows clearing up our singleton instance. |
| 330 | static void ResetForTesting(); |
| 331 |
| 332 | // Allow tests to inspect TraceEvents. |
| 333 | TraceEvent* GetEventByHandle(TraceEventHandle handle); |
| 334 |
| 335 | void SetProcessID(int process_id); |
| 336 |
| 337 | // Process sort indices, if set, override the order of a process will appear |
| 338 | // relative to other processes in the trace viewer. Processes are sorted first |
| 339 | // on their sort index, ascending, then by their name, and then tid. |
| 340 | void SetProcessSortIndex(int sort_index); |
| 341 |
| 342 | // Sets the name of the process. |
| 343 | void set_process_name(const std::string& process_name) { |
| 344 | AutoLock lock(lock_); |
| 345 | process_name_ = process_name; |
| 346 | } |
| 347 |
| 348 | bool IsProcessNameEmpty() const { return process_name_.empty(); } |
| 349 |
| 350 | // Processes can have labels in addition to their names. Use labels, for |
| 351 | // instance, to list out the web page titles that a process is handling. |
| 352 | void UpdateProcessLabel(int label_id, const std::string& current_label); |
| 353 | void RemoveProcessLabel(int label_id); |
| 354 |
| 355 | // Thread sort indices, if set, override the order of a thread will appear |
| 356 | // within its process in the trace viewer. Threads are sorted first on their |
| 357 | // sort index, ascending, then by their name, and then tid. |
| 358 | void SetThreadSortIndex(PlatformThreadId thread_id, int sort_index); |
| 359 |
| 360 | // Allow setting an offset between the current TimeTicks time and the time |
| 361 | // that should be reported. |
| 362 | void SetTimeOffset(TimeDelta offset); |
| 363 |
| 364 | size_t GetObserverCountForTest() const; |
| 365 |
| 366 | // Call this method if the current thread may block the message loop to |
| 367 | // prevent the thread from using the thread-local buffer because the thread |
| 368 | // may not handle the flush request in time causing lost of unflushed events. |
| 369 | void SetCurrentThreadBlocksMessageLoop(); |
| 370 |
| 371 | #if defined(OS_WIN) |
| 372 | // This function is called by the ETW exporting module whenever the ETW |
| 373 | // keyword (flags) changes. This keyword indicates which categories should be |
| 374 | // exported, so whenever it changes, we adjust accordingly. |
| 375 | void UpdateETWCategoryGroupEnabledFlags(); |
| 376 | #endif |
| 377 |
| 378 | // Replaces |logged_events_| with a new TraceBuffer for testing. |
| 379 | void SetTraceBufferForTesting(std::unique_ptr<TraceBuffer> trace_buffer); |
| 380 |
| 381 | private: |
| 382 | typedef unsigned int InternalTraceOptions; |
| 383 |
| 384 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 385 | TraceBufferRingBufferGetReturnChunk); |
| 386 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 387 | TraceBufferRingBufferHalfIteration); |
| 388 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 389 | TraceBufferRingBufferFullIteration); |
| 390 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, TraceBufferVectorReportFull); |
| 391 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 392 | ConvertTraceConfigToInternalOptions); |
| 393 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 394 | TraceRecordAsMuchAsPossibleMode); |
| 395 | FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, ConfigTraceBufferLimit); |
| 396 |
| 397 | friend class base::NoDestructor<TraceLog>; |
| 398 |
| 399 | // MemoryDumpProvider implementation. |
| 400 | bool OnMemoryDump(const MemoryDumpArgs& args, |
| 401 | ProcessMemoryDump* pmd) override; |
| 402 |
| 403 | // Enable/disable each category group based on the current mode_, |
| 404 | // category_filter_ and event_filters_enabled_. |
| 405 | // Enable the category group in the recording mode if category_filter_ matches |
| 406 | // the category group, is not null. Enable category for filtering if any |
| 407 | // filter in event_filters_enabled_ enables it. |
| 408 | void UpdateCategoryRegistry(); |
| 409 | void UpdateCategoryState(TraceCategory* category); |
| 410 |
| 411 | void CreateFiltersForTraceConfig(); |
| 412 |
| 413 | InternalTraceOptions GetInternalOptionsFromTraceConfig( |
| 414 | const TraceConfig& config); |
| 415 |
| 416 | class ThreadLocalEventBuffer; |
| 417 | class OptionalAutoLock; |
| 418 | struct RegisteredAsyncObserver; |
| 419 |
| 420 | TraceLog(); |
| 421 | ~TraceLog() override; |
| 422 | void AddMetadataEventsWhileLocked(); |
| 423 | template <typename T> |
| 424 | void AddMetadataEventWhileLocked(int thread_id, |
| 425 | const char* metadata_name, |
| 426 | const char* arg_name, |
| 427 | const T& value); |
| 428 |
| 429 | InternalTraceOptions trace_options() const { |
| 430 | return static_cast<InternalTraceOptions>( |
| 431 | subtle::NoBarrier_Load(&trace_options_)); |
| 432 | } |
| 433 |
| 434 | TraceBuffer* trace_buffer() const { return logged_events_.get(); } |
| 435 | TraceBuffer* CreateTraceBuffer(); |
| 436 |
| 437 | std::string EventToConsoleMessage(unsigned char phase, |
| 438 | const TimeTicks& timestamp, |
| 439 | TraceEvent* trace_event); |
| 440 |
| 441 | TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, |
| 442 | bool check_buffer_is_full); |
| 443 | void CheckIfBufferIsFullWhileLocked(); |
| 444 | void SetDisabledWhileLocked(uint8_t modes); |
| 445 |
| 446 | TraceEvent* GetEventByHandleInternal(TraceEventHandle handle, |
| 447 | OptionalAutoLock* lock); |
| 448 |
| 449 | void FlushInternal(const OutputCallback& cb, |
| 450 | bool use_worker_thread, |
| 451 | bool discard_events); |
| 452 |
| 453 | // |generation| is used in the following callbacks to check if the callback |
| 454 | // is called for the flush of the current |logged_events_|. |
| 455 | void FlushCurrentThread(int generation, bool discard_events); |
| 456 | // Usually it runs on a different thread. |
| 457 | static void ConvertTraceEventsToTraceFormat( |
| 458 | std::unique_ptr<TraceBuffer> logged_events, |
| 459 | const TraceLog::OutputCallback& flush_output_callback, |
| 460 | const ArgumentFilterPredicate& argument_filter_predicate); |
| 461 | void FinishFlush(int generation, bool discard_events); |
| 462 | void OnFlushTimeout(int generation, bool discard_events); |
| 463 |
| 464 | int generation() const { |
| 465 | return static_cast<int>(subtle::NoBarrier_Load(&generation_)); |
| 466 | } |
| 467 | bool CheckGeneration(int generation) const { |
| 468 | return generation == this->generation(); |
| 469 | } |
| 470 | void UseNextTraceBuffer(); |
| 471 |
| 472 | TimeTicks OffsetNow() const { |
| 473 | // This should be TRACE_TIME_TICKS_NOW but include order makes that hard. |
| 474 | return OffsetTimestamp(base::subtle::TimeTicksNowIgnoringOverride()); |
| 475 | } |
| 476 | TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const { |
| 477 | return timestamp - time_offset_; |
| 478 | } |
| 479 |
| 480 | // Internal representation of trace options since we store the currently used |
| 481 | // trace option as an AtomicWord. |
| 482 | static const InternalTraceOptions kInternalNone; |
| 483 | static const InternalTraceOptions kInternalRecordUntilFull; |
| 484 | static const InternalTraceOptions kInternalRecordContinuously; |
| 485 | static const InternalTraceOptions kInternalEchoToConsole; |
| 486 | static const InternalTraceOptions kInternalRecordAsMuchAsPossible; |
| 487 | static const InternalTraceOptions kInternalEnableArgumentFilter; |
| 488 |
| 489 | // This lock protects TraceLog member accesses (except for members protected |
| 490 | // by thread_info_lock_) from arbitrary threads. |
| 491 | mutable Lock lock_; |
| 492 | // This lock protects accesses to thread_names_, thread_event_start_times_ |
| 493 | // and thread_colors_. |
| 494 | Lock thread_info_lock_; |
| 495 | uint8_t enabled_modes_; // See TraceLog::Mode. |
| 496 | int num_traces_recorded_; |
| 497 | std::unique_ptr<TraceBuffer> logged_events_; |
| 498 | std::vector<std::unique_ptr<TraceEvent>> metadata_events_; |
| 499 |
| 500 | // The lock protects observers access. |
| 501 | mutable Lock observers_lock_; |
| 502 | bool dispatching_to_observers_ = false; |
| 503 | std::vector<EnabledStateObserver*> enabled_state_observers_; |
| 504 | std::map<AsyncEnabledStateObserver*, RegisteredAsyncObserver> |
| 505 | async_observers_; |
| 506 | // Manages ownership of the owned observers. The owned observers will also be |
| 507 | // added to |enabled_state_observers_|. |
| 508 | std::vector<std::unique_ptr<EnabledStateObserver>> |
| 509 | owned_enabled_state_observer_copy_; |
| 510 |
| 511 | std::string process_name_; |
| 512 | std::unordered_map<int, std::string> process_labels_; |
| 513 | int process_sort_index_; |
| 514 | std::unordered_map<int, int> thread_sort_indices_; |
| 515 | std::unordered_map<int, std::string> thread_names_; |
| 516 | base::Time process_creation_time_; |
| 517 |
| 518 | // The following two maps are used only when ECHO_TO_CONSOLE. |
| 519 | std::unordered_map<int, base::stack<TimeTicks>> thread_event_start_times_; |
| 520 | std::unordered_map<std::string, int> thread_colors_; |
| 521 |
| 522 | TimeTicks buffer_limit_reached_timestamp_; |
| 523 |
| 524 | // XORed with TraceID to make it unlikely to collide with other processes. |
| 525 | unsigned long long process_id_hash_; |
| 526 |
| 527 | int process_id_; |
| 528 |
| 529 | TimeDelta time_offset_; |
| 530 |
| 531 | subtle::AtomicWord /* Options */ trace_options_; |
| 532 |
| 533 | TraceConfig trace_config_; |
| 534 | TraceConfig::EventFilters enabled_event_filters_; |
| 535 |
| 536 | ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; |
| 537 | ThreadLocalBoolean thread_blocks_message_loop_; |
| 538 | ThreadLocalBoolean thread_is_in_trace_event_; |
| 539 |
| 540 | // Contains task runners for the threads that have had at least one event |
| 541 | // added into the local event buffer. |
| 542 | std::unordered_map<int, scoped_refptr<SingleThreadTaskRunner>> |
| 543 | thread_task_runners_; |
| 544 |
| 545 | // For events which can't be added into the thread local buffer, e.g. events |
| 546 | // from threads without a message loop. |
| 547 | std::unique_ptr<TraceBufferChunk> thread_shared_chunk_; |
| 548 | size_t thread_shared_chunk_index_; |
| 549 |
| 550 | // Set when asynchronous Flush is in progress. |
| 551 | OutputCallback flush_output_callback_; |
| 552 | scoped_refptr<SequencedTaskRunner> flush_task_runner_; |
| 553 | ArgumentFilterPredicate argument_filter_predicate_; |
| 554 | MetadataFilterPredicate metadata_filter_predicate_; |
| 555 | subtle::AtomicWord generation_; |
| 556 | bool use_worker_thread_; |
| 557 | std::atomic<AddTraceEventOverrideFunction> add_trace_event_override_{nullptr}; |
| 558 | std::atomic<OnFlushFunction> on_flush_override_{nullptr}; |
| 559 | std::atomic<UpdateDurationFunction> update_duration_override_{nullptr}; |
| 560 |
| 561 | FilterFactoryForTesting filter_factory_for_testing_; |
| 562 |
| 563 | DISALLOW_COPY_AND_ASSIGN(TraceLog); |
| 564 | }; |
| 565 |
| 566 | } // namespace trace_event |
| 567 | } // namespace base |
| 568 |
| 569 | #endif // BASE_TRACE_EVENT_TRACE_LOG_H_ |
| 570 |