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/string_split.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/trace_event/trace_event.h"
15 namespace trace_event
{
19 // String options that can be used to initialize TraceOptions.
20 const char kRecordUntilFull
[] = "record-until-full";
21 const char kRecordContinuously
[] = "record-continuously";
22 const char kRecordAsMuchAsPossible
[] = "record-as-much-as-possible";
23 const char kTraceToConsole
[] = "trace-to-console";
24 const char kEnableSampling
[] = "enable-sampling";
25 const char kEnableSystrace
[] = "enable-systrace";
26 const char kEnableArgumentFilter
[] = "enable-argument-filter";
28 // String parameters that can be used to parse the trace config string.
29 const char kRecordModeParam
[] = "record_mode";
30 const char kEnableSamplingParam
[] = "enable_sampling";
31 const char kEnableSystraceParam
[] = "enable_systrace";
32 const char kEnableArgumentFilterParam
[] = "enable_argument_filter";
33 const char kIncludedCategoriesParam
[] = "included_categories";
34 const char kExcludedCategoriesParam
[] = "excluded_categories";
35 const char kSyntheticDelaysParam
[] = "synthetic_delays";
37 const char kSyntheticDelayCategoryFilterPrefix
[] = "DELAY(";
41 TraceConfig::TraceConfig() {
45 TraceConfig::TraceConfig(const std::string
& category_filter_string
,
46 const std::string
& trace_options_string
) {
47 InitializeFromStrings(category_filter_string
, trace_options_string
);
50 TraceConfig::TraceConfig(const std::string
& category_filter_string
,
51 TraceRecordMode record_mode
) {
52 std::string trace_options_string
;
53 switch (record_mode
) {
54 case RECORD_UNTIL_FULL
:
55 trace_options_string
= kRecordUntilFull
;
57 case RECORD_CONTINUOUSLY
:
58 trace_options_string
= kRecordContinuously
;
60 case RECORD_AS_MUCH_AS_POSSIBLE
:
61 trace_options_string
= kRecordAsMuchAsPossible
;
64 trace_options_string
= kTraceToConsole
;
69 InitializeFromStrings(category_filter_string
, trace_options_string
);
72 TraceConfig::TraceConfig(const std::string
& config_string
) {
73 if (!config_string
.empty())
74 InitializeFromConfigString(config_string
);
79 TraceConfig::TraceConfig(const TraceConfig
& tc
)
80 : record_mode_(tc
.record_mode_
),
81 enable_sampling_(tc
.enable_sampling_
),
82 enable_systrace_(tc
.enable_systrace_
),
83 enable_argument_filter_(tc
.enable_argument_filter_
),
84 included_categories_(tc
.included_categories_
),
85 disabled_categories_(tc
.disabled_categories_
),
86 excluded_categories_(tc
.excluded_categories_
),
87 synthetic_delays_(tc
.synthetic_delays_
) {
90 TraceConfig::~TraceConfig() {
93 TraceConfig
& TraceConfig::operator=(const TraceConfig
& rhs
) {
97 record_mode_
= rhs
.record_mode_
;
98 enable_sampling_
= rhs
.enable_sampling_
;
99 enable_systrace_
= rhs
.enable_systrace_
;
100 enable_argument_filter_
= rhs
.enable_argument_filter_
;
101 included_categories_
= rhs
.included_categories_
;
102 disabled_categories_
= rhs
.disabled_categories_
;
103 excluded_categories_
= rhs
.excluded_categories_
;
104 synthetic_delays_
= rhs
.synthetic_delays_
;
108 const TraceConfig::StringList
& TraceConfig::GetSyntheticDelayValues() const {
109 return synthetic_delays_
;
112 std::string
TraceConfig::ToString() const {
113 base::DictionaryValue dict
;
117 base::JSONWriter::Write(dict
, &json
);
122 std::string
TraceConfig::ToCategoryFilterString() const {
123 std::string filter_string
;
124 WriteCategoryFilterString(included_categories_
, &filter_string
, true);
125 WriteCategoryFilterString(disabled_categories_
, &filter_string
, true);
126 WriteCategoryFilterString(excluded_categories_
, &filter_string
, false);
127 WriteCategoryFilterString(synthetic_delays_
, &filter_string
);
128 return filter_string
;
131 bool TraceConfig::IsCategoryGroupEnabled(
132 const char* category_group_name
) const {
133 // TraceLog should call this method only as part of enabling/disabling
136 bool had_enabled_by_default
= false;
137 DCHECK(category_group_name
);
138 CStringTokenizer
category_group_tokens(
139 category_group_name
, category_group_name
+ strlen(category_group_name
),
141 while (category_group_tokens
.GetNext()) {
142 std::string category_group_token
= category_group_tokens
.token();
143 // Don't allow empty tokens, nor tokens with leading or trailing space.
144 DCHECK(!TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
145 category_group_token
))
146 << "Disallowed category string";
147 if (IsCategoryEnabled(category_group_token
.c_str())) {
150 if (!MatchPattern(category_group_token
.c_str(),
151 TRACE_DISABLED_BY_DEFAULT("*")))
152 had_enabled_by_default
= true;
154 // Do a second pass to check for explicitly disabled categories
155 // (those explicitly enabled have priority due to first pass).
156 category_group_tokens
.Reset();
157 bool category_group_disabled
= false;
158 while (category_group_tokens
.GetNext()) {
159 std::string category_group_token
= category_group_tokens
.token();
160 for (StringList::const_iterator ci
= excluded_categories_
.begin();
161 ci
!= excluded_categories_
.end();
163 if (MatchPattern(category_group_token
.c_str(), ci
->c_str())) {
164 // Current token of category_group_name is present in excluded_list.
165 // Flag the exclusion and proceed further to check if any of the
166 // remaining categories of category_group_name is not present in the
168 category_group_disabled
= true;
171 // One of the category of category_group_name is not present in
172 // excluded_ list. So, it has to be included_ list. Enable the
173 // category_group_name for recording.
174 category_group_disabled
= false;
176 // One of the categories present in category_group_name is not present in
177 // excluded_ list. Implies this category_group_name group can be enabled
178 // for recording, since one of its groups is enabled for recording.
179 if (!category_group_disabled
)
182 // If the category group is not excluded, and there are no included patterns
183 // we consider this category group enabled, as long as it had categories
184 // other than disabled-by-default.
185 return !category_group_disabled
&&
186 included_categories_
.empty() && had_enabled_by_default
;
189 void TraceConfig::Merge(const TraceConfig
& config
) {
190 if (record_mode_
!= config
.record_mode_
191 || enable_sampling_
!= config
.enable_sampling_
192 || enable_systrace_
!= config
.enable_systrace_
193 || enable_argument_filter_
!= config
.enable_argument_filter_
) {
194 DLOG(ERROR
) << "Attempting to merge trace config with a different "
195 << "set of options.";
198 // Keep included patterns only if both filters have an included entry.
199 // Otherwise, one of the filter was specifying "*" and we want to honor the
201 if (HasIncludedPatterns() && config
.HasIncludedPatterns()) {
202 included_categories_
.insert(included_categories_
.end(),
203 config
.included_categories_
.begin(),
204 config
.included_categories_
.end());
206 included_categories_
.clear();
209 disabled_categories_
.insert(disabled_categories_
.end(),
210 config
.disabled_categories_
.begin(),
211 config
.disabled_categories_
.end());
212 excluded_categories_
.insert(excluded_categories_
.end(),
213 config
.excluded_categories_
.begin(),
214 config
.excluded_categories_
.end());
215 synthetic_delays_
.insert(synthetic_delays_
.end(),
216 config
.synthetic_delays_
.begin(),
217 config
.synthetic_delays_
.end());
220 void TraceConfig::Clear() {
221 record_mode_
= RECORD_UNTIL_FULL
;
222 enable_sampling_
= false;
223 enable_systrace_
= false;
224 enable_argument_filter_
= false;
225 included_categories_
.clear();
226 disabled_categories_
.clear();
227 excluded_categories_
.clear();
228 synthetic_delays_
.clear();
231 void TraceConfig::InitializeDefault() {
232 record_mode_
= RECORD_UNTIL_FULL
;
233 enable_sampling_
= false;
234 enable_systrace_
= false;
235 enable_argument_filter_
= false;
236 excluded_categories_
.push_back("*Debug");
237 excluded_categories_
.push_back("*Test");
240 void TraceConfig::InitializeFromConfigString(const std::string
& config_string
) {
241 scoped_ptr
<base::Value
> value(base::JSONReader::Read(config_string
));
242 if (!value
|| !value
->IsType(base::Value::TYPE_DICTIONARY
)) {
246 scoped_ptr
<base::DictionaryValue
> dict(
247 static_cast<base::DictionaryValue
*>(value
.release()));
249 record_mode_
= RECORD_UNTIL_FULL
;
250 std::string record_mode
;
251 if (dict
->GetString(kRecordModeParam
, &record_mode
)) {
252 if (record_mode
== kRecordUntilFull
) {
253 record_mode_
= RECORD_UNTIL_FULL
;
254 } else if (record_mode
== kRecordContinuously
) {
255 record_mode_
= RECORD_CONTINUOUSLY
;
256 } else if (record_mode
== kTraceToConsole
) {
257 record_mode_
= ECHO_TO_CONSOLE
;
258 } else if (record_mode
== kRecordAsMuchAsPossible
) {
259 record_mode_
= RECORD_AS_MUCH_AS_POSSIBLE
;
263 bool enable_sampling
;
264 if (!dict
->GetBoolean(kEnableSamplingParam
, &enable_sampling
))
265 enable_sampling_
= false;
267 enable_sampling_
= enable_sampling
;
269 bool enable_systrace
;
270 if (!dict
->GetBoolean(kEnableSystraceParam
, &enable_systrace
))
271 enable_systrace_
= false;
273 enable_systrace_
= enable_systrace
;
275 bool enable_argument_filter
;
276 if (!dict
->GetBoolean(kEnableArgumentFilterParam
, &enable_argument_filter
))
277 enable_argument_filter_
= false;
279 enable_argument_filter_
= enable_argument_filter
;
282 base::ListValue
* category_list
= NULL
;
283 if (dict
->GetList(kIncludedCategoriesParam
, &category_list
))
284 SetCategoriesFromIncludedList(*category_list
);
285 if (dict
->GetList(kExcludedCategoriesParam
, &category_list
))
286 SetCategoriesFromExcludedList(*category_list
);
287 if (dict
->GetList(kSyntheticDelaysParam
, &category_list
))
288 SetSyntheticDelaysFromList(*category_list
);
291 void TraceConfig::InitializeFromStrings(
292 const std::string
& category_filter_string
,
293 const std::string
& trace_options_string
) {
294 if (!category_filter_string
.empty()) {
295 std::vector
<std::string
> split
;
296 std::vector
<std::string
>::iterator iter
;
297 base::SplitString(category_filter_string
, ',', &split
);
298 for (iter
= split
.begin(); iter
!= split
.end(); ++iter
) {
299 std::string category
= *iter
;
300 // Ignore empty categories.
301 if (category
.empty())
303 // Synthetic delays are of the form 'DELAY(delay;option;option;...)'.
304 if (category
.find(kSyntheticDelayCategoryFilterPrefix
) == 0 &&
305 category
.at(category
.size() - 1) == ')') {
306 category
= category
.substr(
307 strlen(kSyntheticDelayCategoryFilterPrefix
),
308 category
.size() - strlen(kSyntheticDelayCategoryFilterPrefix
) - 1);
309 size_t name_length
= category
.find(';');
310 if (name_length
!= std::string::npos
&& name_length
> 0 &&
311 name_length
!= category
.size() - 1) {
312 synthetic_delays_
.push_back(category
);
314 } else if (category
.at(0) == '-') {
315 // Excluded categories start with '-'.
316 // Remove '-' from category string.
317 category
= category
.substr(1);
318 excluded_categories_
.push_back(category
);
319 } else if (category
.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
320 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
321 disabled_categories_
.push_back(category
);
323 included_categories_
.push_back(category
);
328 record_mode_
= RECORD_UNTIL_FULL
;
329 enable_sampling_
= false;
330 enable_systrace_
= false;
331 enable_argument_filter_
= false;
332 if(!trace_options_string
.empty()) {
333 std::vector
<std::string
> split
;
334 std::vector
<std::string
>::iterator iter
;
335 base::SplitString(trace_options_string
, ',', &split
);
336 for (iter
= split
.begin(); iter
!= split
.end(); ++iter
) {
337 if (*iter
== kRecordUntilFull
) {
338 record_mode_
= RECORD_UNTIL_FULL
;
339 } else if (*iter
== kRecordContinuously
) {
340 record_mode_
= RECORD_CONTINUOUSLY
;
341 } else if (*iter
== kTraceToConsole
) {
342 record_mode_
= ECHO_TO_CONSOLE
;
343 } else if (*iter
== kRecordAsMuchAsPossible
) {
344 record_mode_
= RECORD_AS_MUCH_AS_POSSIBLE
;
345 } else if (*iter
== kEnableSampling
) {
346 enable_sampling_
= true;
347 } else if (*iter
== kEnableSystrace
) {
348 enable_systrace_
= true;
349 } else if (*iter
== kEnableArgumentFilter
) {
350 enable_argument_filter_
= true;
356 void TraceConfig::SetCategoriesFromIncludedList(
357 const base::ListValue
& included_list
) {
358 included_categories_
.clear();
359 for (size_t i
= 0; i
< included_list
.GetSize(); ++i
) {
360 std::string category
;
361 if (!included_list
.GetString(i
, &category
))
363 if (category
.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
364 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
365 disabled_categories_
.push_back(category
);
367 included_categories_
.push_back(category
);
372 void TraceConfig::SetCategoriesFromExcludedList(
373 const base::ListValue
& excluded_list
) {
374 excluded_categories_
.clear();
375 for (size_t i
= 0; i
< excluded_list
.GetSize(); ++i
) {
376 std::string category
;
377 if (excluded_list
.GetString(i
, &category
))
378 excluded_categories_
.push_back(category
);
382 void TraceConfig::SetSyntheticDelaysFromList(const base::ListValue
& list
) {
383 synthetic_delays_
.clear();
384 for (size_t i
= 0; i
< list
.GetSize(); ++i
) {
386 if (!list
.GetString(i
, &delay
))
388 // Synthetic delays are of the form "delay;option;option;...".
389 size_t name_length
= delay
.find(';');
390 if (name_length
!= std::string::npos
&& name_length
> 0 &&
391 name_length
!= delay
.size() - 1) {
392 synthetic_delays_
.push_back(delay
);
397 void TraceConfig::AddCategoryToDict(base::DictionaryValue
& dict
,
399 const StringList
& categories
) const {
400 if (categories
.empty())
403 scoped_ptr
<base::ListValue
> list(new base::ListValue());
404 for (StringList::const_iterator ci
= categories
.begin();
405 ci
!= categories
.end();
407 list
->AppendString(*ci
);
410 dict
.Set(param
, list
.Pass());
413 void TraceConfig::ToDict(base::DictionaryValue
& dict
) const {
414 switch (record_mode_
) {
415 case RECORD_UNTIL_FULL
:
416 dict
.SetString(kRecordModeParam
, kRecordUntilFull
);
418 case RECORD_CONTINUOUSLY
:
419 dict
.SetString(kRecordModeParam
, kRecordContinuously
);
421 case RECORD_AS_MUCH_AS_POSSIBLE
:
422 dict
.SetString(kRecordModeParam
, kRecordAsMuchAsPossible
);
424 case ECHO_TO_CONSOLE
:
425 dict
.SetString(kRecordModeParam
, kTraceToConsole
);
431 if (enable_sampling_
)
432 dict
.SetBoolean(kEnableSamplingParam
, true);
434 dict
.SetBoolean(kEnableSamplingParam
, false);
436 if (enable_systrace_
)
437 dict
.SetBoolean(kEnableSystraceParam
, true);
439 dict
.SetBoolean(kEnableSystraceParam
, false);
441 if (enable_argument_filter_
)
442 dict
.SetBoolean(kEnableArgumentFilterParam
, true);
444 dict
.SetBoolean(kEnableArgumentFilterParam
, false);
446 StringList
categories(included_categories_
);
447 categories
.insert(categories
.end(),
448 disabled_categories_
.begin(),
449 disabled_categories_
.end());
450 AddCategoryToDict(dict
, kIncludedCategoriesParam
, categories
);
451 AddCategoryToDict(dict
, kExcludedCategoriesParam
, excluded_categories_
);
452 AddCategoryToDict(dict
, kSyntheticDelaysParam
, synthetic_delays_
);
455 std::string
TraceConfig::ToTraceOptionsString() const {
457 switch (record_mode_
) {
458 case RECORD_UNTIL_FULL
:
459 ret
= kRecordUntilFull
;
461 case RECORD_CONTINUOUSLY
:
462 ret
= kRecordContinuously
;
464 case RECORD_AS_MUCH_AS_POSSIBLE
:
465 ret
= kRecordAsMuchAsPossible
;
467 case ECHO_TO_CONSOLE
:
468 ret
= kTraceToConsole
;
473 if (enable_sampling_
)
474 ret
= ret
+ "," + kEnableSampling
;
475 if (enable_systrace_
)
476 ret
= ret
+ "," + kEnableSystrace
;
477 if (enable_argument_filter_
)
478 ret
= ret
+ "," + kEnableArgumentFilter
;
482 void TraceConfig::WriteCategoryFilterString(const StringList
& values
,
484 bool included
) const {
485 bool prepend_comma
= !out
->empty();
487 for (StringList::const_iterator ci
= values
.begin();
488 ci
!= values
.end(); ++ci
) {
489 if (token_cnt
> 0 || prepend_comma
)
490 StringAppendF(out
, ",");
491 StringAppendF(out
, "%s%s", (included
? "" : "-"), ci
->c_str());
496 void TraceConfig::WriteCategoryFilterString(const StringList
& delays
,
497 std::string
* out
) const {
498 bool prepend_comma
= !out
->empty();
500 for (StringList::const_iterator ci
= delays
.begin();
501 ci
!= delays
.end(); ++ci
) {
502 if (token_cnt
> 0 || prepend_comma
)
503 StringAppendF(out
, ",");
504 StringAppendF(out
, "%s%s)", kSyntheticDelayCategoryFilterPrefix
,
510 bool TraceConfig::IsCategoryEnabled(const char* category_name
) const {
511 StringList::const_iterator ci
;
513 // Check the disabled- filters and the disabled-* wildcard first so that a
514 // "*" filter does not include the disabled.
515 for (ci
= disabled_categories_
.begin();
516 ci
!= disabled_categories_
.end();
518 if (MatchPattern(category_name
, ci
->c_str()))
522 if (MatchPattern(category_name
, TRACE_DISABLED_BY_DEFAULT("*")))
525 for (ci
= included_categories_
.begin();
526 ci
!= included_categories_
.end();
528 if (MatchPattern(category_name
, ci
->c_str()))
535 bool TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
536 const std::string
& str
) {
537 return str
.empty() ||
539 str
.at(str
.length() - 1) == ' ';
542 bool TraceConfig::HasIncludedPatterns() const {
543 return !included_categories_
.empty();
546 } // namespace trace_event