SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / base / trace_event / trace_config.cc
blob974e40fe4b79e8fc362b0664f91d0c1bc0b941d2
1 // Copyright (c) 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.
5 #include "base/trace_event/trace_config.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/pattern.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_tokenizer.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/trace_event/trace_event.h"
15 namespace base {
16 namespace trace_event {
18 namespace {
20 // String options that can be used to initialize TraceOptions.
21 const char kRecordUntilFull[] = "record-until-full";
22 const char kRecordContinuously[] = "record-continuously";
23 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible";
24 const char kTraceToConsole[] = "trace-to-console";
25 const char kEnableSampling[] = "enable-sampling";
26 const char kEnableSystrace[] = "enable-systrace";
27 const char kEnableArgumentFilter[] = "enable-argument-filter";
29 // String parameters that can be used to parse the trace config string.
30 const char kRecordModeParam[] = "record_mode";
31 const char kEnableSamplingParam[] = "enable_sampling";
32 const char kEnableSystraceParam[] = "enable_systrace";
33 const char kEnableArgumentFilterParam[] = "enable_argument_filter";
34 const char kIncludedCategoriesParam[] = "included_categories";
35 const char kExcludedCategoriesParam[] = "excluded_categories";
36 const char kSyntheticDelaysParam[] = "synthetic_delays";
38 const char kSyntheticDelayCategoryFilterPrefix[] = "DELAY(";
40 } // namespace
42 TraceConfig::TraceConfig() {
43 InitializeDefault();
46 TraceConfig::TraceConfig(const std::string& category_filter_string,
47 const std::string& trace_options_string) {
48 InitializeFromStrings(category_filter_string, trace_options_string);
51 TraceConfig::TraceConfig(const std::string& category_filter_string,
52 TraceRecordMode record_mode) {
53 std::string trace_options_string;
54 switch (record_mode) {
55 case RECORD_UNTIL_FULL:
56 trace_options_string = kRecordUntilFull;
57 break;
58 case RECORD_CONTINUOUSLY:
59 trace_options_string = kRecordContinuously;
60 break;
61 case RECORD_AS_MUCH_AS_POSSIBLE:
62 trace_options_string = kRecordAsMuchAsPossible;
63 break;
64 case ECHO_TO_CONSOLE:
65 trace_options_string = kTraceToConsole;
66 break;
67 default:
68 NOTREACHED();
70 InitializeFromStrings(category_filter_string, trace_options_string);
73 TraceConfig::TraceConfig(const std::string& config_string) {
74 if (!config_string.empty())
75 InitializeFromConfigString(config_string);
76 else
77 InitializeDefault();
80 TraceConfig::TraceConfig(const TraceConfig& tc)
81 : record_mode_(tc.record_mode_),
82 enable_sampling_(tc.enable_sampling_),
83 enable_systrace_(tc.enable_systrace_),
84 enable_argument_filter_(tc.enable_argument_filter_),
85 included_categories_(tc.included_categories_),
86 disabled_categories_(tc.disabled_categories_),
87 excluded_categories_(tc.excluded_categories_),
88 synthetic_delays_(tc.synthetic_delays_) {
91 TraceConfig::~TraceConfig() {
94 TraceConfig& TraceConfig::operator=(const TraceConfig& rhs) {
95 if (this == &rhs)
96 return *this;
98 record_mode_ = rhs.record_mode_;
99 enable_sampling_ = rhs.enable_sampling_;
100 enable_systrace_ = rhs.enable_systrace_;
101 enable_argument_filter_ = rhs.enable_argument_filter_;
102 included_categories_ = rhs.included_categories_;
103 disabled_categories_ = rhs.disabled_categories_;
104 excluded_categories_ = rhs.excluded_categories_;
105 synthetic_delays_ = rhs.synthetic_delays_;
106 return *this;
109 const TraceConfig::StringList& TraceConfig::GetSyntheticDelayValues() const {
110 return synthetic_delays_;
113 std::string TraceConfig::ToString() const {
114 base::DictionaryValue dict;
115 ToDict(dict);
117 std::string json;
118 base::JSONWriter::Write(dict, &json);
120 return json;
123 std::string TraceConfig::ToCategoryFilterString() const {
124 std::string filter_string;
125 WriteCategoryFilterString(included_categories_, &filter_string, true);
126 WriteCategoryFilterString(disabled_categories_, &filter_string, true);
127 WriteCategoryFilterString(excluded_categories_, &filter_string, false);
128 WriteCategoryFilterString(synthetic_delays_, &filter_string);
129 return filter_string;
132 bool TraceConfig::IsCategoryGroupEnabled(
133 const char* category_group_name) const {
134 // TraceLog should call this method only as part of enabling/disabling
135 // categories.
137 bool had_enabled_by_default = false;
138 DCHECK(category_group_name);
139 CStringTokenizer category_group_tokens(
140 category_group_name, category_group_name + strlen(category_group_name),
141 ",");
142 while (category_group_tokens.GetNext()) {
143 std::string category_group_token = category_group_tokens.token();
144 // Don't allow empty tokens, nor tokens with leading or trailing space.
145 DCHECK(!TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
146 category_group_token))
147 << "Disallowed category string";
148 if (IsCategoryEnabled(category_group_token.c_str())) {
149 return true;
151 if (!base::MatchPattern(category_group_token.c_str(),
152 TRACE_DISABLED_BY_DEFAULT("*")))
153 had_enabled_by_default = true;
155 // Do a second pass to check for explicitly disabled categories
156 // (those explicitly enabled have priority due to first pass).
157 category_group_tokens.Reset();
158 bool category_group_disabled = false;
159 while (category_group_tokens.GetNext()) {
160 std::string category_group_token = category_group_tokens.token();
161 for (StringList::const_iterator ci = excluded_categories_.begin();
162 ci != excluded_categories_.end();
163 ++ci) {
164 if (base::MatchPattern(category_group_token.c_str(), ci->c_str())) {
165 // Current token of category_group_name is present in excluded_list.
166 // Flag the exclusion and proceed further to check if any of the
167 // remaining categories of category_group_name is not present in the
168 // excluded_ list.
169 category_group_disabled = true;
170 break;
172 // One of the category of category_group_name is not present in
173 // excluded_ list. So, it has to be included_ list. Enable the
174 // category_group_name for recording.
175 category_group_disabled = false;
177 // One of the categories present in category_group_name is not present in
178 // excluded_ list. Implies this category_group_name group can be enabled
179 // for recording, since one of its groups is enabled for recording.
180 if (!category_group_disabled)
181 break;
183 // If the category group is not excluded, and there are no included patterns
184 // we consider this category group enabled, as long as it had categories
185 // other than disabled-by-default.
186 return !category_group_disabled &&
187 included_categories_.empty() && had_enabled_by_default;
190 void TraceConfig::Merge(const TraceConfig& config) {
191 if (record_mode_ != config.record_mode_
192 || enable_sampling_ != config.enable_sampling_
193 || enable_systrace_ != config.enable_systrace_
194 || enable_argument_filter_ != config.enable_argument_filter_) {
195 DLOG(ERROR) << "Attempting to merge trace config with a different "
196 << "set of options.";
199 // Keep included patterns only if both filters have an included entry.
200 // Otherwise, one of the filter was specifying "*" and we want to honor the
201 // broadest filter.
202 if (HasIncludedPatterns() && config.HasIncludedPatterns()) {
203 included_categories_.insert(included_categories_.end(),
204 config.included_categories_.begin(),
205 config.included_categories_.end());
206 } else {
207 included_categories_.clear();
210 disabled_categories_.insert(disabled_categories_.end(),
211 config.disabled_categories_.begin(),
212 config.disabled_categories_.end());
213 excluded_categories_.insert(excluded_categories_.end(),
214 config.excluded_categories_.begin(),
215 config.excluded_categories_.end());
216 synthetic_delays_.insert(synthetic_delays_.end(),
217 config.synthetic_delays_.begin(),
218 config.synthetic_delays_.end());
221 void TraceConfig::Clear() {
222 record_mode_ = RECORD_UNTIL_FULL;
223 enable_sampling_ = false;
224 enable_systrace_ = false;
225 enable_argument_filter_ = false;
226 included_categories_.clear();
227 disabled_categories_.clear();
228 excluded_categories_.clear();
229 synthetic_delays_.clear();
232 void TraceConfig::InitializeDefault() {
233 record_mode_ = RECORD_UNTIL_FULL;
234 enable_sampling_ = false;
235 enable_systrace_ = false;
236 enable_argument_filter_ = false;
237 excluded_categories_.push_back("*Debug");
238 excluded_categories_.push_back("*Test");
241 void TraceConfig::InitializeFromConfigString(const std::string& config_string) {
242 scoped_ptr<base::Value> value(base::JSONReader::Read(config_string));
243 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) {
244 InitializeDefault();
245 return;
247 scoped_ptr<base::DictionaryValue> dict(
248 static_cast<base::DictionaryValue*>(value.release()));
250 record_mode_ = RECORD_UNTIL_FULL;
251 std::string record_mode;
252 if (dict->GetString(kRecordModeParam, &record_mode)) {
253 if (record_mode == kRecordUntilFull) {
254 record_mode_ = RECORD_UNTIL_FULL;
255 } else if (record_mode == kRecordContinuously) {
256 record_mode_ = RECORD_CONTINUOUSLY;
257 } else if (record_mode == kTraceToConsole) {
258 record_mode_ = ECHO_TO_CONSOLE;
259 } else if (record_mode == kRecordAsMuchAsPossible) {
260 record_mode_ = RECORD_AS_MUCH_AS_POSSIBLE;
264 bool enable_sampling;
265 if (!dict->GetBoolean(kEnableSamplingParam, &enable_sampling))
266 enable_sampling_ = false;
267 else
268 enable_sampling_ = enable_sampling;
270 bool enable_systrace;
271 if (!dict->GetBoolean(kEnableSystraceParam, &enable_systrace))
272 enable_systrace_ = false;
273 else
274 enable_systrace_ = enable_systrace;
276 bool enable_argument_filter;
277 if (!dict->GetBoolean(kEnableArgumentFilterParam, &enable_argument_filter))
278 enable_argument_filter_ = false;
279 else
280 enable_argument_filter_ = enable_argument_filter;
283 base::ListValue* category_list = NULL;
284 if (dict->GetList(kIncludedCategoriesParam, &category_list))
285 SetCategoriesFromIncludedList(*category_list);
286 if (dict->GetList(kExcludedCategoriesParam, &category_list))
287 SetCategoriesFromExcludedList(*category_list);
288 if (dict->GetList(kSyntheticDelaysParam, &category_list))
289 SetSyntheticDelaysFromList(*category_list);
292 void TraceConfig::InitializeFromStrings(
293 const std::string& category_filter_string,
294 const std::string& trace_options_string) {
295 if (!category_filter_string.empty()) {
296 std::vector<std::string> split = base::SplitString(
297 category_filter_string, ",", base::TRIM_WHITESPACE,
298 base::SPLIT_WANT_ALL);
299 std::vector<std::string>::iterator iter;
300 for (iter = split.begin(); iter != split.end(); ++iter) {
301 std::string category = *iter;
302 // Ignore empty categories.
303 if (category.empty())
304 continue;
305 // Synthetic delays are of the form 'DELAY(delay;option;option;...)'.
306 if (category.find(kSyntheticDelayCategoryFilterPrefix) == 0 &&
307 category.at(category.size() - 1) == ')') {
308 category = category.substr(
309 strlen(kSyntheticDelayCategoryFilterPrefix),
310 category.size() - strlen(kSyntheticDelayCategoryFilterPrefix) - 1);
311 size_t name_length = category.find(';');
312 if (name_length != std::string::npos && name_length > 0 &&
313 name_length != category.size() - 1) {
314 synthetic_delays_.push_back(category);
316 } else if (category.at(0) == '-') {
317 // Excluded categories start with '-'.
318 // Remove '-' from category string.
319 category = category.substr(1);
320 excluded_categories_.push_back(category);
321 } else if (category.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
322 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
323 disabled_categories_.push_back(category);
324 } else {
325 included_categories_.push_back(category);
330 record_mode_ = RECORD_UNTIL_FULL;
331 enable_sampling_ = false;
332 enable_systrace_ = false;
333 enable_argument_filter_ = false;
334 if(!trace_options_string.empty()) {
335 std::vector<std::string> split = base::SplitString(
336 trace_options_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
337 std::vector<std::string>::iterator iter;
338 for (iter = split.begin(); iter != split.end(); ++iter) {
339 if (*iter == kRecordUntilFull) {
340 record_mode_ = RECORD_UNTIL_FULL;
341 } else if (*iter == kRecordContinuously) {
342 record_mode_ = RECORD_CONTINUOUSLY;
343 } else if (*iter == kTraceToConsole) {
344 record_mode_ = ECHO_TO_CONSOLE;
345 } else if (*iter == kRecordAsMuchAsPossible) {
346 record_mode_ = RECORD_AS_MUCH_AS_POSSIBLE;
347 } else if (*iter == kEnableSampling) {
348 enable_sampling_ = true;
349 } else if (*iter == kEnableSystrace) {
350 enable_systrace_ = true;
351 } else if (*iter == kEnableArgumentFilter) {
352 enable_argument_filter_ = true;
358 void TraceConfig::SetCategoriesFromIncludedList(
359 const base::ListValue& included_list) {
360 included_categories_.clear();
361 for (size_t i = 0; i < included_list.GetSize(); ++i) {
362 std::string category;
363 if (!included_list.GetString(i, &category))
364 continue;
365 if (category.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
366 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
367 disabled_categories_.push_back(category);
368 } else {
369 included_categories_.push_back(category);
374 void TraceConfig::SetCategoriesFromExcludedList(
375 const base::ListValue& excluded_list) {
376 excluded_categories_.clear();
377 for (size_t i = 0; i < excluded_list.GetSize(); ++i) {
378 std::string category;
379 if (excluded_list.GetString(i, &category))
380 excluded_categories_.push_back(category);
384 void TraceConfig::SetSyntheticDelaysFromList(const base::ListValue& list) {
385 synthetic_delays_.clear();
386 for (size_t i = 0; i < list.GetSize(); ++i) {
387 std::string delay;
388 if (!list.GetString(i, &delay))
389 continue;
390 // Synthetic delays are of the form "delay;option;option;...".
391 size_t name_length = delay.find(';');
392 if (name_length != std::string::npos && name_length > 0 &&
393 name_length != delay.size() - 1) {
394 synthetic_delays_.push_back(delay);
399 void TraceConfig::AddCategoryToDict(base::DictionaryValue& dict,
400 const char* param,
401 const StringList& categories) const {
402 if (categories.empty())
403 return;
405 scoped_ptr<base::ListValue> list(new base::ListValue());
406 for (StringList::const_iterator ci = categories.begin();
407 ci != categories.end();
408 ++ci) {
409 list->AppendString(*ci);
412 dict.Set(param, list.Pass());
415 void TraceConfig::ToDict(base::DictionaryValue& dict) const {
416 switch (record_mode_) {
417 case RECORD_UNTIL_FULL:
418 dict.SetString(kRecordModeParam, kRecordUntilFull);
419 break;
420 case RECORD_CONTINUOUSLY:
421 dict.SetString(kRecordModeParam, kRecordContinuously);
422 break;
423 case RECORD_AS_MUCH_AS_POSSIBLE:
424 dict.SetString(kRecordModeParam, kRecordAsMuchAsPossible);
425 break;
426 case ECHO_TO_CONSOLE:
427 dict.SetString(kRecordModeParam, kTraceToConsole);
428 break;
429 default:
430 NOTREACHED();
433 if (enable_sampling_)
434 dict.SetBoolean(kEnableSamplingParam, true);
435 else
436 dict.SetBoolean(kEnableSamplingParam, false);
438 if (enable_systrace_)
439 dict.SetBoolean(kEnableSystraceParam, true);
440 else
441 dict.SetBoolean(kEnableSystraceParam, false);
443 if (enable_argument_filter_)
444 dict.SetBoolean(kEnableArgumentFilterParam, true);
445 else
446 dict.SetBoolean(kEnableArgumentFilterParam, false);
448 StringList categories(included_categories_);
449 categories.insert(categories.end(),
450 disabled_categories_.begin(),
451 disabled_categories_.end());
452 AddCategoryToDict(dict, kIncludedCategoriesParam, categories);
453 AddCategoryToDict(dict, kExcludedCategoriesParam, excluded_categories_);
454 AddCategoryToDict(dict, kSyntheticDelaysParam, synthetic_delays_);
457 std::string TraceConfig::ToTraceOptionsString() const {
458 std::string ret;
459 switch (record_mode_) {
460 case RECORD_UNTIL_FULL:
461 ret = kRecordUntilFull;
462 break;
463 case RECORD_CONTINUOUSLY:
464 ret = kRecordContinuously;
465 break;
466 case RECORD_AS_MUCH_AS_POSSIBLE:
467 ret = kRecordAsMuchAsPossible;
468 break;
469 case ECHO_TO_CONSOLE:
470 ret = kTraceToConsole;
471 break;
472 default:
473 NOTREACHED();
475 if (enable_sampling_)
476 ret = ret + "," + kEnableSampling;
477 if (enable_systrace_)
478 ret = ret + "," + kEnableSystrace;
479 if (enable_argument_filter_)
480 ret = ret + "," + kEnableArgumentFilter;
481 return ret;
484 void TraceConfig::WriteCategoryFilterString(const StringList& values,
485 std::string* out,
486 bool included) const {
487 bool prepend_comma = !out->empty();
488 int token_cnt = 0;
489 for (StringList::const_iterator ci = values.begin();
490 ci != values.end(); ++ci) {
491 if (token_cnt > 0 || prepend_comma)
492 StringAppendF(out, ",");
493 StringAppendF(out, "%s%s", (included ? "" : "-"), ci->c_str());
494 ++token_cnt;
498 void TraceConfig::WriteCategoryFilterString(const StringList& delays,
499 std::string* out) const {
500 bool prepend_comma = !out->empty();
501 int token_cnt = 0;
502 for (StringList::const_iterator ci = delays.begin();
503 ci != delays.end(); ++ci) {
504 if (token_cnt > 0 || prepend_comma)
505 StringAppendF(out, ",");
506 StringAppendF(out, "%s%s)", kSyntheticDelayCategoryFilterPrefix,
507 ci->c_str());
508 ++token_cnt;
512 bool TraceConfig::IsCategoryEnabled(const char* category_name) const {
513 StringList::const_iterator ci;
515 // Check the disabled- filters and the disabled-* wildcard first so that a
516 // "*" filter does not include the disabled.
517 for (ci = disabled_categories_.begin();
518 ci != disabled_categories_.end();
519 ++ci) {
520 if (base::MatchPattern(category_name, ci->c_str()))
521 return true;
524 if (base::MatchPattern(category_name, TRACE_DISABLED_BY_DEFAULT("*")))
525 return false;
527 for (ci = included_categories_.begin();
528 ci != included_categories_.end();
529 ++ci) {
530 if (base::MatchPattern(category_name, ci->c_str()))
531 return true;
534 return false;
537 bool TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
538 const std::string& str) {
539 return str.empty() ||
540 str.at(0) == ' ' ||
541 str.at(str.length() - 1) == ' ';
544 bool TraceConfig::HasIncludedPatterns() const {
545 return !included_categories_.empty();
548 } // namespace trace_event
549 } // namespace base