| 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 #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" |
| 11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/string_util.h" |
| 17 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 18 #include "base/string_tokenizer.h" | 19 #include "base/strings/string_tokenizer.h" |
| 19 #include "base/string_util.h" | |
| 20 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
| 21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 22 #include "base/threading/platform_thread.h" | 22 #include "base/threading/platform_thread.h" |
| 23 #include "base/threading/thread_local.h" | 23 #include "base/threading/thread_local.h" |
| 24 #include "base/time.h" | 24 #include "base/time.h" |
| 25 #include "base/utf_string_conversions.h" | 25 #include "base/utf_string_conversions.h" |
| 26 | 26 |
| 27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 28 #include "base/debug/trace_event_win.h" | 28 #include "base/debug/trace_event_win.h" |
| 29 #endif | 29 #endif |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 bool arg_is_copy[kTraceMaxNumArgs]; | 156 bool arg_is_copy[kTraceMaxNumArgs]; |
| 157 for (i = 0; i < num_args; ++i) { | 157 for (i = 0; i < num_args; ++i) { |
| 158 // We only take a copy of arg_vals if they are of type COPY_STRING. | 158 // We only take a copy of arg_vals if they are of type COPY_STRING. |
| 159 arg_is_copy[i] = (arg_types_[i] == TRACE_VALUE_TYPE_COPY_STRING); | 159 arg_is_copy[i] = (arg_types_[i] == TRACE_VALUE_TYPE_COPY_STRING); |
| 160 if (arg_is_copy[i]) | 160 if (arg_is_copy[i]) |
| 161 alloc_size += GetAllocLength(arg_values_[i].as_string); | 161 alloc_size += GetAllocLength(arg_values_[i].as_string); |
| 162 } | 162 } |
| 163 | 163 |
| 164 if (alloc_size) { | 164 if (alloc_size) { |
| 165 parameter_copy_storage_ = new base::RefCountedString; | 165 parameter_copy_storage_ = new RefCountedString; |
| 166 parameter_copy_storage_->data().resize(alloc_size); | 166 parameter_copy_storage_->data().resize(alloc_size); |
| 167 char* ptr = string_as_array(¶meter_copy_storage_->data()); | 167 char* ptr = string_as_array(¶meter_copy_storage_->data()); |
| 168 const char* end = ptr + alloc_size; | 168 const char* end = ptr + alloc_size; |
| 169 if (copy) { | 169 if (copy) { |
| 170 CopyTraceEventParameter(&ptr, &name_, end); | 170 CopyTraceEventParameter(&ptr, &name_, end); |
| 171 for (i = 0; i < num_args; ++i) | 171 for (i = 0; i < num_args; ++i) |
| 172 CopyTraceEventParameter(&ptr, &arg_names_[i], end); | 172 CopyTraceEventParameter(&ptr, &arg_names_[i], end); |
| 173 } | 173 } |
| 174 for (i = 0; i < num_args; ++i) { | 174 for (i = 0; i < num_args; ++i) { |
| 175 if (arg_is_copy[i]) | 175 if (arg_is_copy[i]) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 } | 272 } |
| 273 | 273 |
| 274 //////////////////////////////////////////////////////////////////////////////// | 274 //////////////////////////////////////////////////////////////////////////////// |
| 275 // | 275 // |
| 276 // TraceResultBuffer | 276 // TraceResultBuffer |
| 277 // | 277 // |
| 278 //////////////////////////////////////////////////////////////////////////////// | 278 //////////////////////////////////////////////////////////////////////////////// |
| 279 | 279 |
| 280 TraceResultBuffer::OutputCallback | 280 TraceResultBuffer::OutputCallback |
| 281 TraceResultBuffer::SimpleOutput::GetCallback() { | 281 TraceResultBuffer::SimpleOutput::GetCallback() { |
| 282 return base::Bind(&SimpleOutput::Append, base::Unretained(this)); | 282 return Bind(&SimpleOutput::Append, Unretained(this)); |
| 283 } | 283 } |
| 284 | 284 |
| 285 void TraceResultBuffer::SimpleOutput::Append( | 285 void TraceResultBuffer::SimpleOutput::Append( |
| 286 const std::string& json_trace_output) { | 286 const std::string& json_trace_output) { |
| 287 json_output += json_trace_output; | 287 json_output += json_trace_output; |
| 288 } | 288 } |
| 289 | 289 |
| 290 TraceResultBuffer::TraceResultBuffer() : append_comma_(false) { | 290 TraceResultBuffer::TraceResultBuffer() : append_comma_(false) { |
| 291 } | 291 } |
| 292 | 292 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: | 358 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: |
| 359 // ANNOTATE_BENIGN_RACE_SIZED(g_category_enabled, sizeof(g_category_enabled), | 359 // ANNOTATE_BENIGN_RACE_SIZED(g_category_enabled, sizeof(g_category_enabled), |
| 360 // "trace_event category enabled"); | 360 // "trace_event category enabled"); |
| 361 for (int i = 0; i < TRACE_EVENT_MAX_CATEGORIES; ++i) { | 361 for (int i = 0; i < TRACE_EVENT_MAX_CATEGORIES; ++i) { |
| 362 ANNOTATE_BENIGN_RACE(&g_category_enabled[i], | 362 ANNOTATE_BENIGN_RACE(&g_category_enabled[i], |
| 363 "trace_event category enabled"); | 363 "trace_event category enabled"); |
| 364 } | 364 } |
| 365 #if defined(OS_NACL) // NaCl shouldn't expose the process id. | 365 #if defined(OS_NACL) // NaCl shouldn't expose the process id. |
| 366 SetProcessID(0); | 366 SetProcessID(0); |
| 367 #else | 367 #else |
| 368 SetProcessID(static_cast<int>(base::GetCurrentProcId())); | 368 SetProcessID(static_cast<int>(GetCurrentProcId())); |
| 369 #endif | 369 #endif |
| 370 } | 370 } |
| 371 | 371 |
| 372 TraceLog::~TraceLog() { | 372 TraceLog::~TraceLog() { |
| 373 } | 373 } |
| 374 | 374 |
| 375 const unsigned char* TraceLog::GetCategoryEnabled(const char* name) { | 375 const unsigned char* TraceLog::GetCategoryEnabled(const char* name) { |
| 376 TraceLog* tracelog = GetInstance(); | 376 TraceLog* tracelog = GetInstance(); |
| 377 if (!tracelog) { | 377 if (!tracelog) { |
| 378 DCHECK(!g_category_enabled[g_category_already_shutdown]); | 378 DCHECK(!g_category_enabled[g_category_already_shutdown]); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 435 |
| 436 if (!category_enabled) { | 436 if (!category_enabled) { |
| 437 // Create a new category | 437 // Create a new category |
| 438 DCHECK(g_category_index < TRACE_EVENT_MAX_CATEGORIES) << | 438 DCHECK(g_category_index < TRACE_EVENT_MAX_CATEGORIES) << |
| 439 "must increase TRACE_EVENT_MAX_CATEGORIES"; | 439 "must increase TRACE_EVENT_MAX_CATEGORIES"; |
| 440 if (g_category_index < TRACE_EVENT_MAX_CATEGORIES) { | 440 if (g_category_index < TRACE_EVENT_MAX_CATEGORIES) { |
| 441 int new_index = g_category_index++; | 441 int new_index = g_category_index++; |
| 442 // Don't hold on to the name pointer, so that we can create categories | 442 // Don't hold on to the name pointer, so that we can create categories |
| 443 // with strings not known at compile time (this is required by | 443 // with strings not known at compile time (this is required by |
| 444 // SetWatchEvent). | 444 // SetWatchEvent). |
| 445 const char* new_name = base::strdup(name); | 445 const char* new_name = strdup(name); |
| 446 ANNOTATE_LEAKING_OBJECT_PTR(new_name); | 446 ANNOTATE_LEAKING_OBJECT_PTR(new_name); |
| 447 g_categories[new_index] = new_name; | 447 g_categories[new_index] = new_name; |
| 448 DCHECK(!g_category_enabled[new_index]); | 448 DCHECK(!g_category_enabled[new_index]); |
| 449 if (enable_count_) { | 449 if (enable_count_) { |
| 450 // Note that if both included and excluded_categories are empty, the | 450 // Note that if both included and excluded_categories are empty, the |
| 451 // else clause below excludes nothing, thereby enabling this category. | 451 // else clause below excludes nothing, thereby enabling this category. |
| 452 if (!included_categories_.empty()) { | 452 if (!included_categories_.empty()) { |
| 453 EnableMatchingCategory(new_index, included_categories_, | 453 EnableMatchingCategory(new_index, included_categories_, |
| 454 CATEGORY_ENABLED, 0); | 454 CATEGORY_ENABLED, 0); |
| 455 } else { | 455 } else { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 int thread_id = static_cast<int>(PlatformThread::CurrentId()); | 650 int thread_id = static_cast<int>(PlatformThread::CurrentId()); |
| 651 | 651 |
| 652 const char* new_name = PlatformThread::GetName(); | 652 const char* new_name = PlatformThread::GetName(); |
| 653 // Check if the thread name has been set or changed since the previous | 653 // Check if the thread name has been set or changed since the previous |
| 654 // call (if any), but don't bother if the new name is empty. Note this will | 654 // call (if any), but don't bother if the new name is empty. Note this will |
| 655 // not detect a thread name change within the same char* buffer address: we | 655 // not detect a thread name change within the same char* buffer address: we |
| 656 // favor common case performance over corner case correctness. | 656 // favor common case performance over corner case correctness. |
| 657 if (new_name != g_current_thread_name.Get().Get() && | 657 if (new_name != g_current_thread_name.Get().Get() && |
| 658 new_name && *new_name) { | 658 new_name && *new_name) { |
| 659 g_current_thread_name.Get().Set(new_name); | 659 g_current_thread_name.Get().Set(new_name); |
| 660 base::hash_map<int, std::string>::iterator existing_name = | 660 hash_map<int, std::string>::iterator existing_name = |
| 661 thread_names_.find(thread_id); | 661 thread_names_.find(thread_id); |
| 662 if (existing_name == thread_names_.end()) { | 662 if (existing_name == thread_names_.end()) { |
| 663 // This is a new thread id, and a new name. | 663 // This is a new thread id, and a new name. |
| 664 thread_names_[thread_id] = new_name; | 664 thread_names_[thread_id] = new_name; |
| 665 } else { | 665 } else { |
| 666 // This is a thread id that we've seen before, but potentially with a | 666 // This is a thread id that we've seen before, but potentially with a |
| 667 // new name. | 667 // new name. |
| 668 std::vector<base::StringPiece> existing_names; | 668 std::vector<StringPiece> existing_names; |
| 669 Tokenize(existing_name->second, ",", &existing_names); | 669 Tokenize(existing_name->second, ",", &existing_names); |
| 670 bool found = std::find(existing_names.begin(), | 670 bool found = std::find(existing_names.begin(), |
| 671 existing_names.end(), | 671 existing_names.end(), |
| 672 new_name) != existing_names.end(); | 672 new_name) != existing_names.end(); |
| 673 if (!found) { | 673 if (!found) { |
| 674 existing_name->second.push_back(','); | 674 existing_name->second.push_back(','); |
| 675 existing_name->second.append(new_name); | 675 existing_name->second.append(new_name); |
| 676 } | 676 } |
| 677 } | 677 } |
| 678 } | 678 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 } | 749 } |
| 750 | 750 |
| 751 void TraceLog::CancelWatchEvent() { | 751 void TraceLog::CancelWatchEvent() { |
| 752 AutoLock lock(lock_); | 752 AutoLock lock(lock_); |
| 753 watch_category_ = NULL; | 753 watch_category_ = NULL; |
| 754 watch_event_name_ = ""; | 754 watch_event_name_ = ""; |
| 755 } | 755 } |
| 756 | 756 |
| 757 void TraceLog::AddThreadNameMetadataEvents() { | 757 void TraceLog::AddThreadNameMetadataEvents() { |
| 758 lock_.AssertAcquired(); | 758 lock_.AssertAcquired(); |
| 759 for(base::hash_map<int, std::string>::iterator it = thread_names_.begin(); | 759 for(hash_map<int, std::string>::iterator it = thread_names_.begin(); |
| 760 it != thread_names_.end(); | 760 it != thread_names_.end(); |
| 761 it++) { | 761 it++) { |
| 762 if (!it->second.empty()) { | 762 if (!it->second.empty()) { |
| 763 int num_args = 1; | 763 int num_args = 1; |
| 764 const char* arg_name = "name"; | 764 const char* arg_name = "name"; |
| 765 unsigned char arg_type; | 765 unsigned char arg_type; |
| 766 unsigned long long arg_value; | 766 unsigned long long arg_value; |
| 767 trace_event_internal::SetTraceValue(it->second, &arg_type, &arg_value); | 767 trace_event_internal::SetTraceValue(it->second, &arg_type, &arg_value); |
| 768 logged_events_.push_back( | 768 logged_events_.push_back( |
| 769 TraceEvent(it->first, | 769 TraceEvent(it->first, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 793 unsigned long long pid = static_cast<unsigned long long>(process_id_); | 793 unsigned long long pid = static_cast<unsigned long long>(process_id_); |
| 794 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; | 794 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; |
| 795 } | 795 } |
| 796 | 796 |
| 797 void TraceLog::SetTimeOffset(TimeDelta offset) { | 797 void TraceLog::SetTimeOffset(TimeDelta offset) { |
| 798 time_offset_ = offset; | 798 time_offset_ = offset; |
| 799 } | 799 } |
| 800 | 800 |
| 801 } // namespace debug | 801 } // namespace debug |
| 802 } // namespace base | 802 } // namespace base |
| OLD | NEW |