OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 long long as_int; | 58 long long as_int; |
59 double as_double; | 59 double as_double; |
60 const void* as_pointer; | 60 const void* as_pointer; |
61 const char* as_string; | 61 const char* as_string; |
62 }; | 62 }; |
63 | 63 |
64 TraceEvent(); | 64 TraceEvent(); |
65 TraceEvent(int thread_id, | 65 TraceEvent(int thread_id, |
66 TimeTicks timestamp, | 66 TimeTicks timestamp, |
67 char phase, | 67 char phase, |
68 const unsigned char* category_enabled, | 68 const unsigned char* category_group_enabled, |
69 const char* name, | 69 const char* name, |
70 unsigned long long id, | 70 unsigned long long id, |
71 int num_args, | 71 int num_args, |
72 const char** arg_names, | 72 const char** arg_names, |
73 const unsigned char* arg_types, | 73 const unsigned char* arg_types, |
74 const unsigned long long* arg_values, | 74 const unsigned long long* arg_values, |
75 unsigned char flags); | 75 unsigned char flags); |
76 ~TraceEvent(); | 76 ~TraceEvent(); |
77 | 77 |
78 // Serialize event data to JSON | 78 // Serialize event data to JSON |
79 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 79 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
80 size_t start, | 80 size_t start, |
81 size_t count, | 81 size_t count, |
82 std::string* out); | 82 std::string* out); |
83 void AppendAsJSON(std::string* out) const; | 83 void AppendAsJSON(std::string* out) const; |
84 | 84 |
85 static void AppendValueAsJSON(unsigned char type, | 85 static void AppendValueAsJSON(unsigned char type, |
86 TraceValue value, | 86 TraceValue value, |
87 std::string* out); | 87 std::string* out); |
88 | 88 |
89 TimeTicks timestamp() const { return timestamp_; } | 89 TimeTicks timestamp() const { return timestamp_; } |
90 | 90 |
91 // Exposed for unittesting: | 91 // Exposed for unittesting: |
92 | 92 |
93 const base::RefCountedString* parameter_copy_storage() const { | 93 const base::RefCountedString* parameter_copy_storage() const { |
94 return parameter_copy_storage_.get(); | 94 return parameter_copy_storage_.get(); |
95 } | 95 } |
96 | 96 |
97 const unsigned char* category_enabled() const { return category_enabled_; } | 97 const unsigned char* category_group_enabled() const { |
98 return category_group_enabled_; | |
99 } | |
100 | |
98 const char* name() const { return name_; } | 101 const char* name() const { return name_; } |
99 | 102 |
100 private: | 103 private: |
101 // Note: these are ordered by size (largest first) for optimal packing. | 104 // Note: these are ordered by size (largest first) for optimal packing. |
102 TimeTicks timestamp_; | 105 TimeTicks timestamp_; |
103 // id_ can be used to store phase-specific data. | 106 // id_ can be used to store phase-specific data. |
104 unsigned long long id_; | 107 unsigned long long id_; |
105 TraceValue arg_values_[kTraceMaxNumArgs]; | 108 TraceValue arg_values_[kTraceMaxNumArgs]; |
106 const char* arg_names_[kTraceMaxNumArgs]; | 109 const char* arg_names_[kTraceMaxNumArgs]; |
107 const unsigned char* category_enabled_; | 110 const unsigned char* category_group_enabled_; |
108 const char* name_; | 111 const char* name_; |
109 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 112 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
110 int thread_id_; | 113 int thread_id_; |
111 char phase_; | 114 char phase_; |
112 unsigned char flags_; | 115 unsigned char flags_; |
113 unsigned char arg_types_[kTraceMaxNumArgs]; | 116 unsigned char arg_types_[kTraceMaxNumArgs]; |
114 }; | 117 }; |
115 | 118 |
119 class BASE_EXPORT CategoryFilter { | |
120 // The default category filter, used when none is provided. | |
121 // Allows all categories through, except if they end in the suffix 'Debug' or | |
122 // 'Test'. | |
123 static const char* kDefaultCategoryFilterString; | |
124 | |
125 public: | |
126 // Constructs the default category filter. This is equivalent to | |
127 // CategoryFilter(CategoryFilter::kDefaultCategoryFilterString); | |
128 CategoryFilter(); | |
129 | |
130 // |filter_string| is a comma-delimited list of category wildcards. | |
131 // A category can have an optional '-' prefix to make it an excluded category. | |
132 // All the same rules apply above, so for example, having both included and | |
133 // excluded categories in the same list would not be supported. | |
134 // | |
135 // Example: CategoryFilter"test_MyTest*"); | |
136 // Example: CategoryFilter("test_MyTest*,test_OtherStuff"); | |
137 // Example: CategoryFilter("-excluded_category1,-excluded_category2"); | |
138 // Example: CategoryFilter("-*,webkit"); would disable everything but webkit. | |
139 // Example: CategoryFilter("*,-webkit"); would enable everything but webkit. | |
rterrazas
2013/03/04 16:55:46
^Self picked nit:
Change to->
Example: CategoryFil
| |
140 CategoryFilter(const std::string& filter_string); | |
141 | |
142 CategoryFilter(const CategoryFilter& cf) | |
143 : included_(cf.included_), | |
144 excluded_(cf.excluded_) { | |
145 } | |
146 | |
147 CategoryFilter& operator=(const CategoryFilter& rhs) { | |
148 if (this == &rhs) | |
149 return *this; | |
150 | |
151 included_ = rhs.included_; | |
152 excluded_ = rhs.excluded_; | |
153 return *this; | |
154 } | |
155 | |
156 // Writes the string representation of the CategoryFilter. This is a comma | |
157 // separated string, similar in nature to the one used to determine | |
158 // enabled/disabled category patterns, except here there is an arbitrary | |
159 // order, included categories go first, then excluded categories. Excluded | |
160 // categories are distinguished from included categories by the prefix '-'. | |
161 std::string ToString() const; | |
162 | |
163 // Determines whether category group would be enabled or | |
164 // disabled by this filter. | |
165 bool IsCategoryGroupEnabled(const char* category_group) const; | |
166 | |
167 // Merges nested_filter with the current CategoryFilter | |
168 void Merge(const CategoryFilter& nested_filter); | |
169 | |
170 // Determines whether or not we have explicitly allowed category patterns. | |
171 bool HasAllowedPatterns() const; | |
172 | |
173 // Clears both included/excluded pattern lists. This would be equivalent to | |
174 // creating a CategoryFilter with an empty string, through the constructor. | |
175 // i.e: CategoryFilter(""). | |
176 // | |
177 // When using an empty filter, all categories are considered included as we | |
178 // are not excluding anything. | |
179 void Clear(); | |
180 | |
181 private: | |
182 void Initialize(const std::string& filter_string); | |
183 void WriteString(std::string* out, bool included) const; | |
184 | |
185 std::vector<std::string> included_; | |
186 std::vector<std::string> excluded_; | |
187 | |
188 }; | |
116 | 189 |
117 // TraceResultBuffer collects and converts trace fragments returned by TraceLog | 190 // TraceResultBuffer collects and converts trace fragments returned by TraceLog |
118 // to JSON output. | 191 // to JSON output. |
119 class BASE_EXPORT TraceResultBuffer { | 192 class BASE_EXPORT TraceResultBuffer { |
120 public: | 193 public: |
121 typedef base::Callback<void(const std::string&)> OutputCallback; | 194 typedef base::Callback<void(const std::string&)> OutputCallback; |
122 | 195 |
123 // If you don't need to stream JSON chunks out efficiently, and just want to | 196 // If you don't need to stream JSON chunks out efficiently, and just want to |
124 // get a complete JSON string after calling Finish, use this struct to collect | 197 // get a complete JSON string after calling Finish, use this struct to collect |
125 // JSON trace output. | 198 // JSON trace output. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 enum Options { | 246 enum Options { |
174 RECORD_UNTIL_FULL = 1 << 0 | 247 RECORD_UNTIL_FULL = 1 << 0 |
175 }; | 248 }; |
176 | 249 |
177 static TraceLog* GetInstance(); | 250 static TraceLog* GetInstance(); |
178 | 251 |
179 // Convert the given string to trace options. Defaults to RECORD_UNTIL_FULL if | 252 // Convert the given string to trace options. Defaults to RECORD_UNTIL_FULL if |
180 // the string does not provide valid options. | 253 // the string does not provide valid options. |
181 static Options TraceOptionsFromString(const std::string& str); | 254 static Options TraceOptionsFromString(const std::string& str); |
182 | 255 |
183 // Get set of known categories. This can change as new code paths are reached. | 256 // Get set of known category groups. This can change as new code paths are |
184 // The known categories are inserted into |categories|. | 257 // reached. The known category groups are inserted into |category_groups|. |
185 void GetKnownCategories(std::vector<std::string>* categories); | 258 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); |
186 | 259 |
187 // Enable tracing for provided list of categories. If tracing is already | 260 // Retrieves the current filter used to enable categories. Only |
188 // enabled, this method does nothing -- changing categories during trace is | |
189 // not supported. | |
190 // If both included_categories and excluded_categories are empty, | |
191 // all categories are traced. | |
192 // Else if included_categories is non-empty, only those are traced. | |
193 // Else if excluded_categories is non-empty, everything but those are traced. | |
194 // Wildcards * and ? are supported (see MatchPattern in string_util.h). | |
195 void SetEnabled(const std::vector<std::string>& included_categories, | |
196 const std::vector<std::string>& excluded_categories, | |
197 Options options); | |
198 | |
199 // |categories| is a comma-delimited list of category wildcards. | |
200 // A category can have an optional '-' prefix to make it an excluded category. | |
201 // All the same rules apply above, so for example, having both included and | |
202 // excluded categories in the same list would not be supported. | |
203 // | |
204 // Example: SetEnabled("test_MyTest*"); | |
205 // Example: SetEnabled("test_MyTest*,test_OtherStuff"); | |
206 // Example: SetEnabled("-excluded_category1,-excluded_category2"); | |
207 void SetEnabled(const std::string& categories, Options options); | |
208 | |
209 // Retieves the categories set via a prior call to SetEnabled(). Only | |
210 // meaningful if |IsEnabled()| is true. | 261 // meaningful if |IsEnabled()| is true. |
211 void GetEnabledTraceCategories(std::vector<std::string>* included_out, | 262 const CategoryFilter& GetCurrentCategoryFilter(); |
212 std::vector<std::string>* excluded_out); | |
213 | 263 |
214 Options trace_options() const { return trace_options_; } | 264 Options trace_options() const { return trace_options_; } |
215 | 265 |
266 // Enables tracing. See CategoryFilter comments for details | |
267 // on how to control what categories will be traced. | |
268 void SetEnabled(const CategoryFilter& category_filter, Options options); | |
269 | |
216 // Disable tracing for all categories. | 270 // Disable tracing for all categories. |
217 void SetDisabled(); | 271 void SetDisabled(); |
218 // Helper method to enable/disable tracing for all categories. | |
219 void SetEnabled(bool enabled, Options options); | |
220 bool IsEnabled() { return !!enable_count_; } | 272 bool IsEnabled() { return !!enable_count_; } |
221 | 273 |
222 #if defined(OS_ANDROID) | 274 #if defined(OS_ANDROID) |
223 void StartATrace(); | 275 void StartATrace(); |
224 void StopATrace(); | 276 void StopATrace(); |
225 #endif | 277 #endif |
226 | 278 |
227 // Enabled state listeners give a callback when tracing is enabled or | 279 // Enabled state listeners give a callback when tracing is enabled or |
228 // disabled. This can be used to tie into other library's tracing systems | 280 // disabled. This can be used to tie into other library's tracing systems |
229 // on-demand. | 281 // on-demand. |
(...skipping 25 matching lines...) Expand all Loading... | |
255 | 307 |
256 // Flush all collected events to the given output callback. The callback will | 308 // Flush all collected events to the given output callback. The callback will |
257 // be called one or more times with IPC-bite-size chunks. The string format is | 309 // be called one or more times with IPC-bite-size chunks. The string format is |
258 // undefined. Use TraceResultBuffer to convert one or more trace strings to | 310 // undefined. Use TraceResultBuffer to convert one or more trace strings to |
259 // JSON. | 311 // JSON. |
260 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)> | 312 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)> |
261 OutputCallback; | 313 OutputCallback; |
262 void Flush(const OutputCallback& cb); | 314 void Flush(const OutputCallback& cb); |
263 | 315 |
264 // Called by TRACE_EVENT* macros, don't call this directly. | 316 // Called by TRACE_EVENT* macros, don't call this directly. |
265 static const unsigned char* GetCategoryEnabled(const char* name); | 317 // The name parameter is a category group for example: |
266 static const char* GetCategoryName(const unsigned char* category_enabled); | 318 // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent") |
319 static const unsigned char* GetCategoryGroupEnabled(const char* name); | |
320 static const char* GetCategoryGroupName( | |
321 const unsigned char* category_group_enabled); | |
267 | 322 |
268 // Called by TRACE_EVENT* macros, don't call this directly. | 323 // Called by TRACE_EVENT* macros, don't call this directly. |
269 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied | 324 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied |
270 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. | 325 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. |
271 void AddTraceEvent(char phase, | 326 void AddTraceEvent(char phase, |
272 const unsigned char* category_enabled, | 327 const unsigned char* category_group_enabled, |
273 const char* name, | 328 const char* category_group, |
274 unsigned long long id, | 329 unsigned long long id, |
275 int num_args, | 330 int num_args, |
276 const char** arg_names, | 331 const char** arg_names, |
277 const unsigned char* arg_types, | 332 const unsigned char* arg_types, |
278 const unsigned long long* arg_values, | 333 const unsigned long long* arg_values, |
279 unsigned char flags); | 334 unsigned char flags); |
280 void AddTraceEventWithThreadIdAndTimestamp( | 335 void AddTraceEventWithThreadIdAndTimestamp( |
281 char phase, | 336 char phase, |
282 const unsigned char* category_enabled, | 337 const unsigned char* category_enabled, |
283 const char* name, | 338 const char* name, |
284 unsigned long long id, | 339 unsigned long long id, |
285 int thread_id, | 340 int thread_id, |
286 const TimeTicks& timestamp, | 341 const TimeTicks& timestamp, |
287 int num_args, | 342 int num_args, |
288 const char** arg_names, | 343 const char** arg_names, |
289 const unsigned char* arg_types, | 344 const unsigned char* arg_types, |
290 const unsigned long long* arg_values, | 345 const unsigned long long* arg_values, |
291 unsigned char flags); | 346 unsigned char flags); |
292 static void AddTraceEventEtw(char phase, | 347 static void AddTraceEventEtw(char phase, |
293 const char* name, | 348 const char* category_group, |
294 const void* id, | 349 const void* id, |
295 const char* extra); | 350 const char* extra); |
296 static void AddTraceEventEtw(char phase, | 351 static void AddTraceEventEtw(char phase, |
297 const char* name, | 352 const char* category_group, |
298 const void* id, | 353 const void* id, |
299 const std::string& extra); | 354 const std::string& extra); |
300 | 355 |
301 // For every matching event, a notification will be fired. NOTE: the | 356 // For every matching event, a notification will be fired. NOTE: the |
302 // notification will fire for each matching event that has already occurred | 357 // notification will fire for each matching event that has already occurred |
303 // since tracing was started (including before tracing if the process was | 358 // since tracing was started (including before tracing if the process was |
304 // started with tracing turned on). | 359 // started with tracing turned on). |
305 void SetWatchEvent(const std::string& category_name, | 360 void SetWatchEvent(const std::string& category_name, |
306 const std::string& event_name); | 361 const std::string& event_name); |
307 // Cancel the watch event. If tracing is enabled, this may race with the | 362 // Cancel the watch event. If tracing is enabled, this may race with the |
(...skipping 17 matching lines...) Expand all Loading... | |
325 return logged_events_[index]; | 380 return logged_events_[index]; |
326 } | 381 } |
327 | 382 |
328 void SetProcessID(int process_id); | 383 void SetProcessID(int process_id); |
329 | 384 |
330 // Allow setting an offset between the current TimeTicks time and the time | 385 // Allow setting an offset between the current TimeTicks time and the time |
331 // that should be reported. | 386 // that should be reported. |
332 void SetTimeOffset(TimeDelta offset); | 387 void SetTimeOffset(TimeDelta offset); |
333 | 388 |
334 private: | 389 private: |
390 | |
335 // This allows constructor and destructor to be private and usable only | 391 // This allows constructor and destructor to be private and usable only |
336 // by the Singleton class. | 392 // by the Singleton class. |
337 friend struct StaticMemorySingletonTraits<TraceLog>; | 393 friend struct StaticMemorySingletonTraits<TraceLog>; |
338 | 394 |
339 // The pointer returned from GetCategoryEnabledInternal() points to a value | 395 // Enable/disable each category group based on the current category_filter_. |
340 // with zero or more of the following bits. Used in this class only. | 396 // If the category group contains a category that matches an included category |
397 // pattern, that category group will be enabled. | |
398 void EnableIncludedCategoryGroups(); | |
399 void EnableIncludedCategoryGroup(int category_index); | |
400 | |
401 // The pointer returned from GetCategoryGroupEnabledInternal() points to a | |
402 // value with zero or more of the following bits. Used in this class only. | |
341 // The TRACE_EVENT macros should only use the value as a bool. | 403 // The TRACE_EVENT macros should only use the value as a bool. |
342 enum CategoryEnabledFlags { | 404 enum CategoryEnabledFlags { |
343 // Normal enabled flag for categories enabled with Enable(). | 405 // Normal enabled flag for categories enabled with Enable(). |
344 CATEGORY_ENABLED = 1 << 0, | 406 CATEGORY_ENABLED = 1 << 0, |
345 // On Android if ATrace is enabled, all categories will have this bit. | 407 // On Android if ATrace is enabled, all categories will have this bit. |
346 // Not used on other platforms. | 408 // Not used on other platforms. |
347 ATRACE_ENABLED = 1 << 1 | 409 ATRACE_ENABLED = 1 << 1 |
348 }; | 410 }; |
349 | 411 |
350 // Helper class for managing notification_thread_count_ and running | 412 // Helper class for managing notification_thread_count_ and running |
(...skipping 14 matching lines...) Expand all Loading... | |
365 inline void SendNotificationIfAny(); | 427 inline void SendNotificationIfAny(); |
366 | 428 |
367 private: | 429 private: |
368 TraceLog* trace_log_; | 430 TraceLog* trace_log_; |
369 NotificationCallback callback_copy_; | 431 NotificationCallback callback_copy_; |
370 int notification_; | 432 int notification_; |
371 }; | 433 }; |
372 | 434 |
373 TraceLog(); | 435 TraceLog(); |
374 ~TraceLog(); | 436 ~TraceLog(); |
375 const unsigned char* GetCategoryEnabledInternal(const char* name); | 437 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); |
376 void AddThreadNameMetadataEvents(); | 438 void AddThreadNameMetadataEvents(); |
377 | 439 |
378 #if defined(OS_ANDROID) | 440 #if defined(OS_ANDROID) |
379 void SendToATrace(char phase, | 441 void SendToATrace(char phase, |
380 const char* category, | 442 const char* category_group, |
381 const char* name, | 443 const char* name, |
382 int num_args, | 444 int num_args, |
383 const char** arg_names, | 445 const char** arg_names, |
384 const unsigned char* arg_types, | 446 const unsigned char* arg_types, |
385 const unsigned long long* arg_values); | 447 const unsigned long long* arg_values); |
386 static void ApplyATraceEnabledFlag(unsigned char* category_enabled); | 448 static void ApplyATraceEnabledFlag(unsigned char* category_group_enabled); |
387 #endif | 449 #endif |
388 | 450 |
389 // TODO(nduca): switch to per-thread trace buffers to reduce thread | 451 // TODO(nduca): switch to per-thread trace buffers to reduce thread |
390 // synchronization. | 452 // synchronization. |
391 // This lock protects TraceLog member accesses from arbitrary threads. | 453 // This lock protects TraceLog member accesses from arbitrary threads. |
392 Lock lock_; | 454 Lock lock_; |
393 int enable_count_; | 455 int enable_count_; |
394 NotificationCallback notification_callback_; | 456 NotificationCallback notification_callback_; |
395 std::vector<TraceEvent> logged_events_; | 457 std::vector<TraceEvent> logged_events_; |
396 std::vector<std::string> included_categories_; | 458 CategoryFilter category_filter_; |
397 std::vector<std::string> excluded_categories_; | |
398 bool dispatching_to_observer_list_; | 459 bool dispatching_to_observer_list_; |
399 ObserverList<EnabledStateChangedObserver> enabled_state_observer_list_; | 460 ObserverList<EnabledStateChangedObserver> enabled_state_observer_list_; |
400 | 461 |
401 base::hash_map<int, std::string> thread_names_; | 462 base::hash_map<int, std::string> thread_names_; |
402 | 463 |
403 // XORed with TraceID to make it unlikely to collide with other processes. | 464 // XORed with TraceID to make it unlikely to collide with other processes. |
404 unsigned long long process_id_hash_; | 465 unsigned long long process_id_hash_; |
405 | 466 |
406 int process_id_; | 467 int process_id_; |
407 | 468 |
408 TimeDelta time_offset_; | 469 TimeDelta time_offset_; |
409 | 470 |
410 // Allow tests to wake up when certain events occur. | 471 // Allow tests to wake up when certain events occur. |
411 const unsigned char* watch_category_; | 472 const unsigned char* watch_category_; |
412 std::string watch_event_name_; | 473 std::string watch_event_name_; |
413 | 474 |
414 Options trace_options_; | 475 Options trace_options_; |
415 | 476 |
416 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 477 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
417 }; | 478 }; |
418 | 479 |
419 } // namespace debug | 480 } // namespace debug |
420 } // namespace base | 481 } // namespace base |
421 | 482 |
422 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 483 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
OLD | NEW |