1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TRACE_EVENT_TRACE_CONFIG_H_ 6 #define BASE_TRACE_EVENT_TRACE_CONFIG_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <set> 12 #include <string> 13 #include <unordered_set> 14 #include <vector> 15 16 #include "base/base_export.h" 17 #include "base/gtest_prod_util.h" 18 #include "base/strings/string_piece.h" 19 #include "base/trace_event/memory_dump_request_args.h" 20 #include "base/trace_event/trace_config_category_filter.h" 21 #include "base/values.h" 22 23 namespace base::trace_event { 24 25 class ConvertableToTraceFormat; 26 27 // Options determines how the trace buffer stores data. 28 // A Java counterpart will be generated for this enum. 29 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base 30 enum TraceRecordMode { 31 // Record until the trace buffer is full. 32 RECORD_UNTIL_FULL, 33 34 // Record until the user ends the trace. The trace buffer is a fixed size 35 // and we use it as a ring buffer during recording. 36 RECORD_CONTINUOUSLY, 37 38 // Record until the trace buffer is full, but with a huge buffer size. 39 RECORD_AS_MUCH_AS_POSSIBLE, 40 41 // Echo to console. Events are discarded. 42 ECHO_TO_CONSOLE, 43 }; 44 45 class BASE_EXPORT TraceConfig { 46 public: 47 using StringList = std::vector<std::string>; 48 49 // Specifies the memory dump config for tracing. 50 // Used only when "memory-infra" category is enabled. 51 struct BASE_EXPORT MemoryDumpConfig { 52 MemoryDumpConfig(); 53 MemoryDumpConfig(const MemoryDumpConfig& other); 54 ~MemoryDumpConfig(); 55 56 // Specifies the triggers in the memory dump config. 57 struct Trigger { 58 friend bool operator==(const Trigger&, const Trigger&) = default; 59 60 uint32_t min_time_between_dumps_ms; 61 MemoryDumpLevelOfDetail level_of_detail; 62 MemoryDumpType trigger_type; 63 }; 64 65 // Specifies the configuration options for the heap profiler. 66 struct HeapProfiler { 67 // Default value for |breakdown_threshold_bytes|. 68 enum { kDefaultBreakdownThresholdBytes = 1024 }; 69 70 HeapProfiler(); 71 72 // Reset the options to default. 73 void Clear(); 74 75 friend bool operator==(const HeapProfiler&, 76 const HeapProfiler&) = default; 77 78 uint32_t breakdown_threshold_bytes; 79 }; 80 81 friend bool operator==(const MemoryDumpConfig&, 82 const MemoryDumpConfig&) = default; 83 84 // Reset the values in the config. 85 void Clear(); 86 87 void Merge(const MemoryDumpConfig& config); 88 89 // Set of memory dump modes allowed for the tracing session. The explicitly 90 // triggered dumps will be successful only if the dump mode is allowed in 91 // the config. 92 std::set<MemoryDumpLevelOfDetail> allowed_dump_modes; 93 94 std::vector<Trigger> triggers; 95 HeapProfiler heap_profiler_options; 96 }; 97 98 class BASE_EXPORT ProcessFilterConfig { 99 public: 100 ProcessFilterConfig(); 101 explicit ProcessFilterConfig( 102 const std::unordered_set<base::ProcessId>& included_process_ids); 103 ProcessFilterConfig(const ProcessFilterConfig&); 104 ~ProcessFilterConfig(); 105 empty()106 bool empty() const { return included_process_ids_.empty(); } 107 108 void Clear(); 109 void Merge(const ProcessFilterConfig&); 110 111 void InitializeFromConfigDict(const Value::Dict&); 112 void ToDict(Value::Dict& dict) const; 113 114 bool IsEnabled(base::ProcessId) const; included_process_ids()115 const std::unordered_set<base::ProcessId>& included_process_ids() const { 116 return included_process_ids_; 117 } 118 119 friend bool operator==(const ProcessFilterConfig&, 120 const ProcessFilterConfig&) = default; 121 122 private: 123 std::unordered_set<base::ProcessId> included_process_ids_; 124 }; 125 126 class BASE_EXPORT EventFilterConfig { 127 public: 128 explicit EventFilterConfig(const std::string& predicate_name); 129 EventFilterConfig(const EventFilterConfig& tc); 130 131 ~EventFilterConfig(); 132 133 EventFilterConfig& operator=(const EventFilterConfig& rhs); 134 135 bool IsEquivalentTo(const EventFilterConfig& other) const; 136 137 void InitializeFromConfigDict(const Value::Dict& event_filter); 138 139 void SetCategoryFilter(const TraceConfigCategoryFilter& category_filter); 140 141 void ToDict(Value::Dict& filter_dict) const; 142 143 bool GetArgAsSet(const char* key, std::unordered_set<std::string>*) const; 144 145 bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const; 146 predicate_name()147 const std::string& predicate_name() const { return predicate_name_; } filter_args()148 const Value::Dict& filter_args() const { return args_; } category_filter()149 const TraceConfigCategoryFilter& category_filter() const { 150 return category_filter_; 151 } 152 153 private: 154 std::string predicate_name_; 155 TraceConfigCategoryFilter category_filter_; 156 Value::Dict args_; 157 }; 158 typedef std::vector<EventFilterConfig> EventFilters; 159 160 static std::string TraceRecordModeToStr(TraceRecordMode record_mode); 161 162 TraceConfig(); 163 164 // Create TraceConfig object from category filter and trace options strings. 165 // 166 // |category_filter_string| is a comma-delimited list of category wildcards. 167 // A category can have an optional '-' prefix to make it an excluded category. 168 // All the same rules apply above, so for example, having both included and 169 // excluded categories in the same list would not be supported. 170 // 171 // |trace_options_string| is a comma-delimited list of trace options. 172 // Possible options are: "record-until-full", "record-continuously", 173 // "record-as-much-as-possible", "trace-to-console", "enable-systrace" and 174 // "enable-argument-filter". 175 // The first 4 options are trace recoding modes and hence 176 // mutually exclusive. If more than one trace recording modes appear in the 177 // options_string, the last one takes precedence. If none of the trace 178 // recording mode is specified, recording mode is RECORD_UNTIL_FULL. 179 // 180 // The trace option will first be reset to the default option 181 // (record_mode set to RECORD_UNTIL_FULL, enable_systrace and 182 // enable_argument_filter set to false) before options parsed from 183 // |trace_options_string| are applied on it. If |trace_options_string| is 184 // invalid, the final state of trace options is undefined. 185 // 186 // Example: TraceConfig("test_MyTest*", "record-until-full"); 187 // Example: TraceConfig("test_MyTest*,test_OtherStuff", 188 // "record-continuously"); 189 // Example: TraceConfig("-excluded_category1,-excluded_category2", 190 // "record-until-full, trace-to-console"); 191 // would set ECHO_TO_CONSOLE as the recording mode. 192 // Example: TraceConfig("-*,webkit", ""); 193 // would disable everything but webkit; and use default options. 194 // Example: TraceConfig("-webkit", ""); 195 // would enable everything but webkit; and use default options. 196 TraceConfig(StringPiece category_filter_string, 197 StringPiece trace_options_string); 198 199 TraceConfig(StringPiece category_filter_string, TraceRecordMode record_mode); 200 201 // Create TraceConfig object from the trace config string. 202 // 203 // |config_string| is a dictionary formatted as a JSON string, containing both 204 // category filters and trace options. 205 // 206 // Example: 207 // { 208 // "record_mode": "record-continuously", 209 // "enable_systrace": true, 210 // "enable_argument_filter": true, 211 // "included_categories": ["included", 212 // "inc_pattern*", 213 // "disabled-by-default-memory-infra"], 214 // "excluded_categories": ["excluded", "exc_pattern*"], 215 // "memory_dump_config": { 216 // "triggers": [ 217 // { 218 // "mode": "detailed", 219 // "periodic_interval_ms": 2000 220 // } 221 // ] 222 // } 223 // } 224 // 225 // Note: memory_dump_config can be specified only if 226 // disabled-by-default-memory-infra category is enabled. 227 explicit TraceConfig(StringPiece config_string); 228 229 // Functionally identical to the above, but takes a parsed dictionary as input 230 // instead of its JSON serialization. 231 explicit TraceConfig(const Value::Dict& config); 232 233 TraceConfig(const TraceConfig& tc); 234 235 ~TraceConfig(); 236 237 TraceConfig& operator=(const TraceConfig& rhs); 238 239 bool IsEquivalentTo(const TraceConfig& other) const; 240 GetTraceRecordMode()241 TraceRecordMode GetTraceRecordMode() const { return record_mode_; } GetTraceBufferSizeInEvents()242 size_t GetTraceBufferSizeInEvents() const { 243 return trace_buffer_size_in_events_; 244 } GetTraceBufferSizeInKb()245 size_t GetTraceBufferSizeInKb() const { return trace_buffer_size_in_kb_; } IsSystraceEnabled()246 bool IsSystraceEnabled() const { return enable_systrace_; } IsArgumentFilterEnabled()247 bool IsArgumentFilterEnabled() const { return enable_argument_filter_; } 248 SetTraceRecordMode(TraceRecordMode mode)249 void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; } SetTraceBufferSizeInEvents(size_t size)250 void SetTraceBufferSizeInEvents(size_t size) { 251 trace_buffer_size_in_events_ = size; 252 } SetTraceBufferSizeInKb(size_t size)253 void SetTraceBufferSizeInKb(size_t size) { trace_buffer_size_in_kb_ = size; } EnableSystrace()254 void EnableSystrace() { enable_systrace_ = true; } 255 void EnableSystraceEvent(const std::string& systrace_event); EnableArgumentFilter()256 void EnableArgumentFilter() { enable_argument_filter_ = true; } 257 void EnableHistogram(const std::string& histogram_name); 258 259 // Writes the string representation of the TraceConfig. The string is JSON 260 // formatted. 261 std::string ToString() const; 262 263 // Returns a copy of the TraceConfig wrapped in a ConvertableToTraceFormat 264 std::unique_ptr<ConvertableToTraceFormat> AsConvertableToTraceFormat() const; 265 266 // Write the string representation of the CategoryFilter part. 267 std::string ToCategoryFilterString() const; 268 269 // Write the string representation of the trace options part (record mode, 270 // systrace, argument filtering). Does not include category filters, event 271 // filters, or memory dump configs. 272 std::string ToTraceOptionsString() const; 273 274 #if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY) 275 // Write the serialized perfetto::TrackEventConfig corresponding to this 276 // TraceConfig. 277 std::string ToPerfettoTrackEventConfigRaw( 278 bool privacy_filtering_enabled) const; 279 #endif // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY) 280 281 // Returns true if at least one category in the list is enabled by this 282 // trace config. This is used to determine if the category filters are 283 // enabled in the TRACE_* macros. 284 bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const; 285 286 // Merges config with the current TraceConfig 287 void Merge(const TraceConfig& config); 288 289 void Clear(); 290 291 // Clears and resets the memory dump config. 292 void ResetMemoryDumpConfig(const MemoryDumpConfig& memory_dump_config); 293 category_filter()294 const TraceConfigCategoryFilter& category_filter() const { 295 return category_filter_; 296 } 297 memory_dump_config()298 const MemoryDumpConfig& memory_dump_config() const { 299 return memory_dump_config_; 300 } 301 process_filter_config()302 const ProcessFilterConfig& process_filter_config() const { 303 return process_filter_config_; 304 } 305 void SetProcessFilterConfig(const ProcessFilterConfig&); 306 event_filters()307 const EventFilters& event_filters() const { return event_filters_; } SetEventFilters(const EventFilters & filter_configs)308 void SetEventFilters(const EventFilters& filter_configs) { 309 event_filters_ = filter_configs; 310 } 311 312 // Returns true if event names should not contain package names. IsEventPackageNameFilterEnabled()313 bool IsEventPackageNameFilterEnabled() const { 314 return enable_event_package_name_filter_; 315 } 316 317 // If `enabled` is true, event names will not contain package names. SetEventPackageNameFilterEnabled(bool enabled)318 void SetEventPackageNameFilterEnabled(bool enabled) { 319 enable_event_package_name_filter_ = enabled; 320 } 321 systrace_events()322 const std::unordered_set<std::string>& systrace_events() const { 323 return systrace_events_; 324 } 325 histogram_names()326 const std::unordered_set<std::string>& histogram_names() const { 327 return histogram_names_; 328 } 329 330 private: 331 FRIEND_TEST_ALL_PREFIXES(TraceConfigTest, TraceConfigFromValidLegacyFormat); 332 FRIEND_TEST_ALL_PREFIXES(TraceConfigTest, 333 TraceConfigFromInvalidLegacyStrings); 334 FRIEND_TEST_ALL_PREFIXES(TraceConfigTest, SystraceEventsSerialization); 335 336 // The default trace config, used when none is provided. 337 // Allows all non-disabled-by-default categories through, except if they end 338 // in the suffix 'Debug' or 'Test'. 339 void InitializeDefault(); 340 341 // Initialize from a config dictionary. 342 void InitializeFromConfigDict(const Value::Dict& dict); 343 344 // Initialize from a config string. 345 void InitializeFromConfigString(StringPiece config_string); 346 347 // Initialize from category filter and trace options strings 348 void InitializeFromStrings(StringPiece category_filter_string, 349 StringPiece trace_options_string); 350 351 void SetMemoryDumpConfigFromConfigDict(const Value::Dict& memory_dump_config); 352 void SetDefaultMemoryDumpConfig(); 353 354 void SetHistogramNamesFromConfigList(const Value::List& histogram_names); 355 void SetEventFiltersFromConfigList(const Value::List& event_filters); 356 Value ToValue() const; 357 358 TraceRecordMode record_mode_; 359 size_t trace_buffer_size_in_events_ = 0; // 0 specifies default size 360 size_t trace_buffer_size_in_kb_ = 0; // 0 specifies default size 361 bool enable_systrace_ : 1; 362 bool enable_argument_filter_ : 1; 363 364 TraceConfigCategoryFilter category_filter_; 365 366 MemoryDumpConfig memory_dump_config_; 367 ProcessFilterConfig process_filter_config_; 368 369 EventFilters event_filters_; 370 bool enable_event_package_name_filter_ : 1; 371 std::unordered_set<std::string> histogram_names_; 372 std::unordered_set<std::string> systrace_events_; 373 }; 374 375 } // namespace base::trace_event 376 377 #endif // BASE_TRACE_EVENT_TRACE_CONFIG_H_ 378