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