Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: base/debug/trace_event_impl.cc

Issue 11823016: Trace category groups and category filter. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Tagged category support cleanup, parameter renaming, documentation updated. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "base/debug/trace_event_impl.h" 5 #include "base/debug/trace_event_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/debug/leak_annotations.h" 10 #include "base/debug/leak_annotations.h"
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 uintptr_t category_ptr = reinterpret_cast<uintptr_t>(category_enabled); 386 uintptr_t category_ptr = reinterpret_cast<uintptr_t>(category_enabled);
387 DCHECK(category_ptr >= category_begin && 387 DCHECK(category_ptr >= category_begin &&
388 category_ptr < reinterpret_cast<uintptr_t>(g_category_enabled + 388 category_ptr < reinterpret_cast<uintptr_t>(g_category_enabled +
389 TRACE_EVENT_MAX_CATEGORIES)) << 389 TRACE_EVENT_MAX_CATEGORIES)) <<
390 "out of bounds category pointer"; 390 "out of bounds category pointer";
391 uintptr_t category_index = 391 uintptr_t category_index =
392 (category_ptr - category_begin) / sizeof(g_category_enabled[0]); 392 (category_ptr - category_begin) / sizeof(g_category_enabled[0]);
393 return g_categories[category_index]; 393 return g_categories[category_index];
394 } 394 }
395 395
396 static bool DoesCategoryContainMatchingTag(const char* tagged_category,
rterrazas 2013/01/15 08:08:31 Implemented the support for multi tag and single t
397 const char* tag_pattern) {
398 CStringTokenizer tag_tokens(tagged_category,
399 tagged_category + strlen(tagged_category), ",");
400 while (tag_tokens.GetNext()) {
401 std::string trimmed_tag;
402 //Trimming tokens to allow categories with arbitrary spaces:
403 // i.e: "webkit, input" or "webkit , input".
404 TrimWhitespaceASCII(tag_tokens.token(), TRIM_ALL, &trimmed_tag);
405 if(trimmed_tag.length() == 0)
406 continue;
407 if (MatchPattern(trimmed_tag.c_str(), tag_pattern))
408 return true;
409 }
410 return false;
411 }
412
396 static void EnableMatchingCategory(int category_index, 413 static void EnableMatchingCategory(int category_index,
397 const std::vector<std::string>& patterns, 414 const std::vector<std::string>& tag_patterns,
398 unsigned char matched_value, 415 unsigned char matched_value,
399 unsigned char unmatched_value) { 416 unsigned char unmatched_value) {
400 std::vector<std::string>::const_iterator ci = patterns.begin(); 417 std::vector<std::string>::const_iterator ci = tag_patterns.begin();
401 bool is_match = false; 418 bool is_match = false;
402 for (; ci != patterns.end(); ++ci) { 419 for (; ci != tag_patterns.end(); ++ci) {
403 is_match = MatchPattern(g_categories[category_index], ci->c_str()); 420 is_match = DoesCategoryContainMatchingTag(g_categories[category_index],
421 ci->c_str());
422
404 if (is_match) 423 if (is_match)
405 break; 424 break;
406 } 425 }
407 g_category_enabled[category_index] = is_match ? 426 g_category_enabled[category_index] = is_match ?
408 matched_value : unmatched_value; 427 matched_value : unmatched_value;
409 } 428 }
410 429
411 // Enable/disable each category based on the category filters in |patterns|. 430 // Enable/disable each category based on the category filters in |tag_patterns|.
412 // If the category name matches one of the patterns, its enabled status is set 431 // If the category name contains a tag that matches one of the tag_patterns,
413 // to |matched_value|. Otherwise its enabled status is set to |unmatched_value|. 432 // its enabled status is set to |matched_value|.
414 static void EnableMatchingCategories(const std::vector<std::string>& patterns, 433 // Otherwise its enabled status is set to |unmatched_value|.
415 unsigned char matched_value, 434 static void EnableMatchingCategories(
416 unsigned char unmatched_value) { 435 const std::vector<std::string>& tag_patterns,
436 unsigned char matched_value,
437 unsigned char unmatched_value) {
417 for (int i = 0; i < g_category_index; i++) 438 for (int i = 0; i < g_category_index; i++)
418 EnableMatchingCategory(i, patterns, matched_value, unmatched_value); 439 EnableMatchingCategory(i, tag_patterns, matched_value, unmatched_value);
419 } 440 }
420 441
421 const unsigned char* TraceLog::GetCategoryEnabledInternal(const char* name) { 442 const unsigned char* TraceLog::GetCategoryEnabledInternal(const char* name) {
422 AutoLock lock(lock_); 443 AutoLock lock(lock_);
423 DCHECK(!strchr(name, '"')) << "Category names may not contain double quote"; 444 DCHECK(!strchr(name, '"')) << "Category names may not contain double quote";
424 445
425 unsigned char* category_enabled = NULL; 446 unsigned char* category_enabled = NULL;
426 // Search for pre-existing category matching this name 447 // Search for pre-existing category matching this name
427 for (int i = 0; i < g_category_index; i++) { 448 for (int i = 0; i < g_category_index; i++) {
428 if (strcmp(g_categories[i], name) == 0) { 449 if (strcmp(g_categories[i], name) == 0) {
429 category_enabled = &g_category_enabled[i]; 450 category_enabled = &g_category_enabled[i];
430 break; 451 break;
431 } 452 }
432 } 453 }
433 454
434 if (!category_enabled) { 455 if (!category_enabled) {
435 // Create a new category 456 // Create a new category
436 DCHECK(g_category_index < TRACE_EVENT_MAX_CATEGORIES) << 457 DCHECK(g_category_index < TRACE_EVENT_MAX_CATEGORIES) <<
437 "must increase TRACE_EVENT_MAX_CATEGORIES"; 458 "must increase TRACE_EVENT_MAX_CATEGORIES";
438 if (g_category_index < TRACE_EVENT_MAX_CATEGORIES) { 459 if (g_category_index < TRACE_EVENT_MAX_CATEGORIES) {
439 int new_index = g_category_index++; 460 int new_index = g_category_index++;
440 // Don't hold on to the name pointer, so that we can create categories 461 // Don't hold on to the name pointer, so that we can create categories
441 // with strings not known at compile time (this is required by 462 // with strings not known at compile time (this is required by
442 // SetWatchEvent). 463 // SetWatchEvent).
443 const char* new_name = base::strdup(name); 464 const char* new_name = base::strdup(name);
444 ANNOTATE_LEAKING_OBJECT_PTR(new_name); 465 ANNOTATE_LEAKING_OBJECT_PTR(new_name);
445 g_categories[new_index] = new_name; 466 g_categories[new_index] = new_name;
446 DCHECK(!g_category_enabled[new_index]); 467 DCHECK(!g_category_enabled[new_index]);
447 if (enabled_) { 468 if (enabled_) {
448 // Note that if both included and excluded_categories are empty, the 469 // Note that if both included and excluded_tag_patterns are empty, the
449 // else clause below excludes nothing, thereby enabling this category. 470 // else clause below excludes nothing, thereby enabling this category.
450 if (!included_categories_.empty()) { 471 if (!included_tag_patterns_.empty()) {
451 EnableMatchingCategory(new_index, included_categories_, 472 EnableMatchingCategory(new_index, included_tag_patterns_,
452 CATEGORY_ENABLED, 0); 473 CATEGORY_ENABLED, 0);
453 } else { 474 } else {
454 EnableMatchingCategory(new_index, excluded_categories_, 475 EnableMatchingCategory(new_index, excluded_tag_patterns_,
455 0, CATEGORY_ENABLED); 476 0, CATEGORY_ENABLED);
456 } 477 }
457 } else { 478 } else {
458 g_category_enabled[new_index] = 0; 479 g_category_enabled[new_index] = 0;
459 } 480 }
460 category_enabled = &g_category_enabled[new_index]; 481 category_enabled = &g_category_enabled[new_index];
461 } else { 482 } else {
462 category_enabled = &g_category_enabled[g_category_categories_exhausted]; 483 category_enabled = &g_category_enabled[g_category_categories_exhausted];
463 } 484 }
464 } 485 }
465 #if defined(OS_ANDROID) 486 #if defined(OS_ANDROID)
466 ApplyATraceEnabledFlag(category_enabled); 487 ApplyATraceEnabledFlag(category_enabled);
467 #endif 488 #endif
468 return category_enabled; 489 return category_enabled;
469 } 490 }
470 491
471 void TraceLog::GetKnownCategories(std::vector<std::string>* categories) { 492 void TraceLog::GetKnownCategories(std::vector<std::string>* categories) {
472 AutoLock lock(lock_); 493 AutoLock lock(lock_);
473 for (int i = 0; i < g_category_index; i++) 494 for (int i = 0; i < g_category_index; i++)
474 categories->push_back(g_categories[i]); 495 categories->push_back(g_categories[i]);
475 } 496 }
476 497
477 void TraceLog::SetEnabled(const std::vector<std::string>& included_categories, 498 void TraceLog::SetEnabled(const std::vector<std::string>& included_tag_patterns,
478 const std::vector<std::string>& excluded_categories) { 499 const std::vector<std::string>& excluded_tag_patterns) {
rterrazas 2013/01/15 08:08:31 Crossed the 80 char barrier, seemed moving the par
rterrazas 2013/01/18 01:25:35 Done.
479 AutoLock lock(lock_); 500 AutoLock lock(lock_);
480 if (enabled_) 501 if (enabled_)
481 return; 502 return;
482 503
483 if (dispatching_to_observer_list_) { 504 if (dispatching_to_observer_list_) {
484 DLOG(ERROR) << 505 DLOG(ERROR) <<
485 "Cannot manipulate TraceLog::Enabled state from an observer."; 506 "Cannot manipulate TraceLog::Enabled state from an observer.";
486 return; 507 return;
487 } 508 }
488 509
489 dispatching_to_observer_list_ = true; 510 dispatching_to_observer_list_ = true;
490 FOR_EACH_OBSERVER(EnabledStateChangedObserver, enabled_state_observer_list_, 511 FOR_EACH_OBSERVER(EnabledStateChangedObserver, enabled_state_observer_list_,
491 OnTraceLogWillEnable()); 512 OnTraceLogWillEnable());
492 dispatching_to_observer_list_ = false; 513 dispatching_to_observer_list_ = false;
493 514
494 logged_events_.reserve(1024); 515 logged_events_.reserve(1024);
495 enabled_ = true; 516 enabled_ = true;
496 included_categories_ = included_categories; 517 included_tag_patterns_ = included_tag_patterns;
497 excluded_categories_ = excluded_categories; 518 excluded_tag_patterns_ = excluded_tag_patterns;
498 // Note that if both included and excluded_categories are empty, the else 519 // Note that if both included and excluded_tag_patterns are empty, the
499 // clause below excludes nothing, thereby enabling all categories. 520 // else clause below excludes nothing, thereby enabling all categories.
500 if (!included_categories_.empty()) 521 if (!included_tag_patterns_.empty())
501 EnableMatchingCategories(included_categories_, CATEGORY_ENABLED, 0); 522 EnableMatchingCategories(included_tag_patterns_, CATEGORY_ENABLED, 0);
502 else 523 else
503 EnableMatchingCategories(excluded_categories_, 0, CATEGORY_ENABLED); 524 EnableMatchingCategories(excluded_tag_patterns_, 0, CATEGORY_ENABLED);
504 } 525 }
505 526
506 void TraceLog::SetEnabled(const std::string& categories) { 527 void TraceLog::SetEnabled(const std::string& tag_patterns) {
507 std::vector<std::string> included, excluded; 528 std::vector<std::string> included, excluded;
508 // Tokenize list of categories, delimited by ','. 529 // Tokenize list of tag patterns, delimited by ','.
509 StringTokenizer tokens(categories, ","); 530 StringTokenizer tokens(tag_patterns, ",");
510 while (tokens.GetNext()) { 531 while (tokens.GetNext()) {
511 bool is_included = true; 532 bool is_included = true;
512 std::string category = tokens.token(); 533 std::string tag_pattern = tokens.token();
513 // Excluded categories start with '-'. 534 // Excluded tags start with '-'.
514 if (category.at(0) == '-') { 535 if (tag_pattern.at(0) == '-') {
515 // Remove '-' from category string. 536 // Remove '-' from tag pattern string.
516 category = category.substr(1); 537 tag_pattern = tag_pattern.substr(1);
517 is_included = false; 538 is_included = false;
518 } 539 }
519 if (is_included) 540 if (is_included)
520 included.push_back(category); 541 included.push_back(tag_pattern);
521 else 542 else
522 excluded.push_back(category); 543 excluded.push_back(tag_pattern);
523 } 544 }
524 SetEnabled(included, excluded); 545 SetEnabled(included, excluded);
525 } 546 }
526 547
527 void TraceLog::GetEnabledTraceCategories( 548 void TraceLog::GetEnabledTraceCategories(
528 std::vector<std::string>* included_out, 549 std::vector<std::string>* included_out,
529 std::vector<std::string>* excluded_out) { 550 std::vector<std::string>* excluded_out) {
530 AutoLock lock(lock_); 551 AutoLock lock(lock_);
531 if (enabled_) { 552 if (enabled_) {
532 *included_out = included_categories_; 553 *included_out = included_tag_patterns_;
533 *excluded_out = excluded_categories_; 554 *excluded_out = excluded_tag_patterns_;
534 } 555 }
535 } 556 }
536 557
537 void TraceLog::SetDisabled() { 558 void TraceLog::SetDisabled() {
538 AutoLock lock(lock_); 559 AutoLock lock(lock_);
539 if (!enabled_) 560 if (!enabled_)
540 return; 561 return;
541 562
542 if (dispatching_to_observer_list_) { 563 if (dispatching_to_observer_list_) {
543 DLOG(ERROR) 564 DLOG(ERROR)
544 << "Cannot manipulate TraceLog::Enabled state from an observer."; 565 << "Cannot manipulate TraceLog::Enabled state from an observer.";
545 return; 566 return;
546 } 567 }
547 568
548 dispatching_to_observer_list_ = true; 569 dispatching_to_observer_list_ = true;
549 FOR_EACH_OBSERVER(EnabledStateChangedObserver, enabled_state_observer_list_, 570 FOR_EACH_OBSERVER(EnabledStateChangedObserver, enabled_state_observer_list_,
550 OnTraceLogWillDisable()); 571 OnTraceLogWillDisable());
551 dispatching_to_observer_list_ = false; 572 dispatching_to_observer_list_ = false;
552 573
553 enabled_ = false; 574 enabled_ = false;
554 included_categories_.clear(); 575 included_tag_patterns_.clear();
555 excluded_categories_.clear(); 576 excluded_tag_patterns_.clear();
556 watch_category_ = NULL; 577 watch_category_ = NULL;
557 watch_event_name_ = ""; 578 watch_event_name_ = "";
558 for (int i = 0; i < g_category_index; i++) 579 for (int i = 0; i < g_category_index; i++)
559 g_category_enabled[i] = 0; 580 g_category_enabled[i] = 0;
560 AddThreadNameMetadataEvents(); 581 AddThreadNameMetadataEvents();
561 #if defined(OS_ANDROID) 582 #if defined(OS_ANDROID)
562 AddClockSyncMetadataEvents(); 583 AddClockSyncMetadataEvents();
563 #endif 584 #endif
564 } 585 }
565 586
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 unsigned long long pid = static_cast<unsigned long long>(process_id_); 801 unsigned long long pid = static_cast<unsigned long long>(process_id_);
781 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; 802 process_id_hash_ = (offset_basis ^ pid) * fnv_prime;
782 } 803 }
783 804
784 void TraceLog::SetTimeOffset(TimeDelta offset) { 805 void TraceLog::SetTimeOffset(TimeDelta offset) {
785 time_offset_ = offset; 806 time_offset_ = offset;
786 } 807 }
787 808
788 } // namespace debug 809 } // namespace debug
789 } // namespace base 810 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698