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"
16 namespace trace_event
{
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(";
42 TraceConfig::TraceConfig() {
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
;
58 case RECORD_CONTINUOUSLY
:
59 trace_options_string
= kRecordContinuously
;
61 case RECORD_AS_MUCH_AS_POSSIBLE
:
62 trace_options_string
= kRecordAsMuchAsPossible
;
65 trace_options_string
= kTraceToConsole
;
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
);
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
) {
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_
;
109 const TraceConfig::StringList
& TraceConfig::GetSyntheticDelayValues() const {
110 return synthetic_delays_
;
113 std::string
TraceConfig::ToString() const {
114 base::DictionaryValue dict
;
118 base::JSONWriter::Write(dict
, &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
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
),
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())) {
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();
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
169 category_group_disabled
= true;
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
)
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
202 if (HasIncludedPatterns() && config
.HasIncludedPatterns()) {
203 included_categories_
.insert(included_categories_
.end(),
204 config
.included_categories_
.begin(),
205 config
.included_categories_
.end());
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
)) {
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;
268 enable_sampling_
= enable_sampling
;
270 bool enable_systrace
;
271 if (!dict
->GetBoolean(kEnableSystraceParam
, &enable_systrace
))
272 enable_systrace_
= false;
274 enable_systrace_
= enable_systrace
;
276 bool enable_argument_filter
;
277 if (!dict
->GetBoolean(kEnableArgumentFilterParam
, &enable_argument_filter
))
278 enable_argument_filter_
= false;
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
;
297 std::vector
<std::string
>::iterator iter
;
298 base::SplitString(category_filter_string
, ',', &split
);
299 for (iter
= split
.begin(); iter
!= split
.end(); ++iter
) {
300 std::string category
= *iter
;
301 // Ignore empty categories.
302 if (category
.empty())
304 // Synthetic delays are of the form 'DELAY(delay;option;option;...)'.
305 if (category
.find(kSyntheticDelayCategoryFilterPrefix
) == 0 &&
306 category
.at(category
.size() - 1) == ')') {
307 category
= category
.substr(
308 strlen(kSyntheticDelayCategoryFilterPrefix
),
309 category
.size() - strlen(kSyntheticDelayCategoryFilterPrefix
) - 1);
310 size_t name_length
= category
.find(';');
311 if (name_length
!= std::string::npos
&& name_length
> 0 &&
312 name_length
!= category
.size() - 1) {
313 synthetic_delays_
.push_back(category
);
315 } else if (category
.at(0) == '-') {
316 // Excluded categories start with '-'.
317 // Remove '-' from category string.
318 category
= category
.substr(1);
319 excluded_categories_
.push_back(category
);
320 } else if (category
.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
321 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
322 disabled_categories_
.push_back(category
);
324 included_categories_
.push_back(category
);
329 record_mode_
= RECORD_UNTIL_FULL
;
330 enable_sampling_
= false;
331 enable_systrace_
= false;
332 enable_argument_filter_
= false;
333 if(!trace_options_string
.empty()) {
334 std::vector
<std::string
> split
;
335 std::vector
<std::string
>::iterator iter
;
336 base::SplitString(trace_options_string
, ',', &split
);
337 for (iter
= split
.begin(); iter
!= split
.end(); ++iter
) {
338 if (*iter
== kRecordUntilFull
) {
339 record_mode_
= RECORD_UNTIL_FULL
;
340 } else if (*iter
== kRecordContinuously
) {
341 record_mode_
= RECORD_CONTINUOUSLY
;
342 } else if (*iter
== kTraceToConsole
) {
343 record_mode_
= ECHO_TO_CONSOLE
;
344 } else if (*iter
== kRecordAsMuchAsPossible
) {
345 record_mode_
= RECORD_AS_MUCH_AS_POSSIBLE
;
346 } else if (*iter
== kEnableSampling
) {
347 enable_sampling_
= true;
348 } else if (*iter
== kEnableSystrace
) {
349 enable_systrace_
= true;
350 } else if (*iter
== kEnableArgumentFilter
) {
351 enable_argument_filter_
= true;
357 void TraceConfig::SetCategoriesFromIncludedList(
358 const base::ListValue
& included_list
) {
359 included_categories_
.clear();
360 for (size_t i
= 0; i
< included_list
.GetSize(); ++i
) {
361 std::string category
;
362 if (!included_list
.GetString(i
, &category
))
364 if (category
.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
365 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
366 disabled_categories_
.push_back(category
);
368 included_categories_
.push_back(category
);
373 void TraceConfig::SetCategoriesFromExcludedList(
374 const base::ListValue
& excluded_list
) {
375 excluded_categories_
.clear();
376 for (size_t i
= 0; i
< excluded_list
.GetSize(); ++i
) {
377 std::string category
;
378 if (excluded_list
.GetString(i
, &category
))
379 excluded_categories_
.push_back(category
);
383 void TraceConfig::SetSyntheticDelaysFromList(const base::ListValue
& list
) {
384 synthetic_delays_
.clear();
385 for (size_t i
= 0; i
< list
.GetSize(); ++i
) {
387 if (!list
.GetString(i
, &delay
))
389 // Synthetic delays are of the form "delay;option;option;...".
390 size_t name_length
= delay
.find(';');
391 if (name_length
!= std::string::npos
&& name_length
> 0 &&
392 name_length
!= delay
.size() - 1) {
393 synthetic_delays_
.push_back(delay
);
398 void TraceConfig::AddCategoryToDict(base::DictionaryValue
& dict
,
400 const StringList
& categories
) const {
401 if (categories
.empty())
404 scoped_ptr
<base::ListValue
> list(new base::ListValue());
405 for (StringList::const_iterator ci
= categories
.begin();
406 ci
!= categories
.end();
408 list
->AppendString(*ci
);
411 dict
.Set(param
, list
.Pass());
414 void TraceConfig::ToDict(base::DictionaryValue
& dict
) const {
415 switch (record_mode_
) {
416 case RECORD_UNTIL_FULL
:
417 dict
.SetString(kRecordModeParam
, kRecordUntilFull
);
419 case RECORD_CONTINUOUSLY
:
420 dict
.SetString(kRecordModeParam
, kRecordContinuously
);
422 case RECORD_AS_MUCH_AS_POSSIBLE
:
423 dict
.SetString(kRecordModeParam
, kRecordAsMuchAsPossible
);
425 case ECHO_TO_CONSOLE
:
426 dict
.SetString(kRecordModeParam
, kTraceToConsole
);
432 if (enable_sampling_
)
433 dict
.SetBoolean(kEnableSamplingParam
, true);
435 dict
.SetBoolean(kEnableSamplingParam
, false);
437 if (enable_systrace_
)
438 dict
.SetBoolean(kEnableSystraceParam
, true);
440 dict
.SetBoolean(kEnableSystraceParam
, false);
442 if (enable_argument_filter_
)
443 dict
.SetBoolean(kEnableArgumentFilterParam
, true);
445 dict
.SetBoolean(kEnableArgumentFilterParam
, false);
447 StringList
categories(included_categories_
);
448 categories
.insert(categories
.end(),
449 disabled_categories_
.begin(),
450 disabled_categories_
.end());
451 AddCategoryToDict(dict
, kIncludedCategoriesParam
, categories
);
452 AddCategoryToDict(dict
, kExcludedCategoriesParam
, excluded_categories_
);
453 AddCategoryToDict(dict
, kSyntheticDelaysParam
, synthetic_delays_
);
456 std::string
TraceConfig::ToTraceOptionsString() const {
458 switch (record_mode_
) {
459 case RECORD_UNTIL_FULL
:
460 ret
= kRecordUntilFull
;
462 case RECORD_CONTINUOUSLY
:
463 ret
= kRecordContinuously
;
465 case RECORD_AS_MUCH_AS_POSSIBLE
:
466 ret
= kRecordAsMuchAsPossible
;
468 case ECHO_TO_CONSOLE
:
469 ret
= kTraceToConsole
;
474 if (enable_sampling_
)
475 ret
= ret
+ "," + kEnableSampling
;
476 if (enable_systrace_
)
477 ret
= ret
+ "," + kEnableSystrace
;
478 if (enable_argument_filter_
)
479 ret
= ret
+ "," + kEnableArgumentFilter
;
483 void TraceConfig::WriteCategoryFilterString(const StringList
& values
,
485 bool included
) const {
486 bool prepend_comma
= !out
->empty();
488 for (StringList::const_iterator ci
= values
.begin();
489 ci
!= values
.end(); ++ci
) {
490 if (token_cnt
> 0 || prepend_comma
)
491 StringAppendF(out
, ",");
492 StringAppendF(out
, "%s%s", (included
? "" : "-"), ci
->c_str());
497 void TraceConfig::WriteCategoryFilterString(const StringList
& delays
,
498 std::string
* out
) const {
499 bool prepend_comma
= !out
->empty();
501 for (StringList::const_iterator ci
= delays
.begin();
502 ci
!= delays
.end(); ++ci
) {
503 if (token_cnt
> 0 || prepend_comma
)
504 StringAppendF(out
, ",");
505 StringAppendF(out
, "%s%s)", kSyntheticDelayCategoryFilterPrefix
,
511 bool TraceConfig::IsCategoryEnabled(const char* category_name
) const {
512 StringList::const_iterator ci
;
514 // Check the disabled- filters and the disabled-* wildcard first so that a
515 // "*" filter does not include the disabled.
516 for (ci
= disabled_categories_
.begin();
517 ci
!= disabled_categories_
.end();
519 if (base::MatchPattern(category_name
, ci
->c_str()))
523 if (base::MatchPattern(category_name
, TRACE_DISABLED_BY_DEFAULT("*")))
526 for (ci
= included_categories_
.begin();
527 ci
!= included_categories_
.end();
529 if (base::MatchPattern(category_name
, ci
->c_str()))
536 bool TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
537 const std::string
& str
) {
538 return str
.empty() ||
540 str
.at(str
.length() - 1) == ' ';
543 bool TraceConfig::HasIncludedPatterns() const {
544 return !included_categories_
.empty();
547 } // namespace trace_event