Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "base/debug/trace_event.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include "base/debug/trace_event_win.h" | 10 #include "base/debug/trace_event_win.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 *out += temp_string; | 76 *out += temp_string; |
| 77 break; | 77 break; |
| 78 case TRACE_TYPE_POINTER: | 78 case TRACE_TYPE_POINTER: |
| 79 base::snprintf(temp_string, arraysize(temp_string), "%llu", | 79 base::snprintf(temp_string, arraysize(temp_string), "%llu", |
| 80 static_cast<unsigned long long>( | 80 static_cast<unsigned long long>( |
| 81 reinterpret_cast<intptr_t>( | 81 reinterpret_cast<intptr_t>( |
| 82 as_pointer()))); | 82 as_pointer()))); |
| 83 *out += temp_string; | 83 *out += temp_string; |
| 84 break; | 84 break; |
| 85 case TRACE_TYPE_STRING: | 85 case TRACE_TYPE_STRING: |
| 86 case TRACE_TYPE_STATIC_STRING: | |
| 86 *out += "\""; | 87 *out += "\""; |
| 87 start_pos = out->size(); | 88 start_pos = out->size(); |
| 88 *out += as_string(); | 89 *out += as_string()? as_string() : "NULL"; |
|
joth
2011/08/31 13:43:33
nit: space before ?
jbates
2011/08/31 17:11:11
Done.
| |
| 89 // replace " character with ' | 90 // replace " character with ' |
| 90 while ((start_pos = out->find_first_of('\"', start_pos)) != | 91 while ((start_pos = out->find_first_of('\"', start_pos)) != |
| 91 std::string::npos) | 92 std::string::npos) |
| 92 (*out)[start_pos] = '\''; | 93 (*out)[start_pos] = '\''; |
| 93 *out += "\""; | 94 *out += "\""; |
| 94 break; | 95 break; |
| 95 default: | 96 default: |
| 96 NOTREACHED() << "Don't know how to print this value"; | 97 NOTREACHED() << "Don't know how to print this value"; |
| 97 break; | 98 break; |
| 98 } | 99 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 arg_names_[1] = arg2_name; | 170 arg_names_[1] = arg2_name; |
| 170 arg_values_[0] = arg1_val; | 171 arg_values_[0] = arg1_val; |
| 171 arg_values_[1] = arg2_val; | 172 arg_values_[1] = arg2_val; |
| 172 | 173 |
| 173 size_t alloc_size = 0; | 174 size_t alloc_size = 0; |
| 174 if (copy) { | 175 if (copy) { |
| 175 alloc_size += GetAllocLength(name); | 176 alloc_size += GetAllocLength(name); |
| 176 alloc_size += GetAllocLength(arg1_name); | 177 alloc_size += GetAllocLength(arg1_name); |
| 177 alloc_size += GetAllocLength(arg2_name); | 178 alloc_size += GetAllocLength(arg2_name); |
| 178 } | 179 } |
| 180 | |
| 181 bool arg1_is_copy = (arg1_val.type() == TraceValue::TRACE_TYPE_STRING); | |
| 182 bool arg2_is_copy = (arg2_val.type() == TraceValue::TRACE_TYPE_STRING); | |
| 183 | |
| 179 // We always take a copy of string arg_vals, even if |copy| was not set. | 184 // We always take a copy of string arg_vals, even if |copy| was not set. |
|
joth
2011/08/31 13:43:33
this comment is a little misleading now.
// We onl
jbates
2011/08/31 17:11:11
Done.
| |
| 180 if (arg1_val.type() == TraceValue::TRACE_TYPE_STRING) | 185 if (arg1_is_copy) |
| 181 alloc_size += GetAllocLength(arg1_val.as_string()); | 186 alloc_size += GetAllocLength(arg1_val.as_string()); |
| 182 if (arg2_val.type() == TraceValue::TRACE_TYPE_STRING) | 187 if (arg2_is_copy) |
| 183 alloc_size += GetAllocLength(arg2_val.as_string()); | 188 alloc_size += GetAllocLength(arg2_val.as_string()); |
| 184 | 189 |
| 185 if (alloc_size) { | 190 if (alloc_size) { |
| 186 parameter_copy_storage_ = new base::RefCountedString; | 191 parameter_copy_storage_ = new base::RefCountedString; |
| 187 parameter_copy_storage_->data().resize(alloc_size); | 192 parameter_copy_storage_->data().resize(alloc_size); |
| 188 char* ptr = string_as_array(¶meter_copy_storage_->data()); | 193 char* ptr = string_as_array(¶meter_copy_storage_->data()); |
| 189 const char* end = ptr + alloc_size; | 194 const char* end = ptr + alloc_size; |
| 190 if (copy) { | 195 if (copy) { |
| 191 CopyTraceEventParameter(&ptr, &name_, end); | 196 CopyTraceEventParameter(&ptr, &name_, end); |
| 192 CopyTraceEventParameter(&ptr, &arg_names_[0], end); | 197 CopyTraceEventParameter(&ptr, &arg_names_[0], end); |
| 193 CopyTraceEventParameter(&ptr, &arg_names_[1], end); | 198 CopyTraceEventParameter(&ptr, &arg_names_[1], end); |
| 194 } | 199 } |
| 195 if (arg_values_[0].type() == TraceValue::TRACE_TYPE_STRING) | 200 if (arg1_is_copy) |
| 196 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); | 201 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); |
| 197 if (arg_values_[1].type() == TraceValue::TRACE_TYPE_STRING) | 202 if (arg2_is_copy) |
| 198 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); | 203 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); |
| 199 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; | 204 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; |
| 200 } | 205 } |
| 201 } | 206 } |
| 202 | 207 |
| 203 TraceEvent::~TraceEvent() { | 208 TraceEvent::~TraceEvent() { |
| 204 } | 209 } |
| 205 | 210 |
| 206 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 211 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
| 207 size_t start, | 212 size_t start, |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 } | 360 } |
| 356 } | 361 } |
| 357 | 362 |
| 358 int TraceLog::AddTraceEvent(TraceEventPhase phase, | 363 int TraceLog::AddTraceEvent(TraceEventPhase phase, |
| 359 const TraceCategory* category, | 364 const TraceCategory* category, |
| 360 const char* name, | 365 const char* name, |
| 361 const char* arg1_name, TraceValue arg1_val, | 366 const char* arg1_name, TraceValue arg1_val, |
| 362 const char* arg2_name, TraceValue arg2_val, | 367 const char* arg2_name, TraceValue arg2_val, |
| 363 int threshold_begin_id, | 368 int threshold_begin_id, |
| 364 int64 threshold, | 369 int64 threshold, |
| 365 bool copy) { | 370 EventFlags flags) { |
| 366 DCHECK(name); | 371 DCHECK(name); |
| 367 #ifdef USE_UNRELIABLE_NOW | 372 #ifdef USE_UNRELIABLE_NOW |
| 368 TimeTicks now = TimeTicks::HighResNow(); | 373 TimeTicks now = TimeTicks::HighResNow(); |
| 369 #else | 374 #else |
| 370 TimeTicks now = TimeTicks::Now(); | 375 TimeTicks now = TimeTicks::Now(); |
| 371 #endif | 376 #endif |
| 372 BufferFullCallback buffer_full_callback_copy; | 377 BufferFullCallback buffer_full_callback_copy; |
| 373 int ret_begin_id = -1; | 378 int ret_begin_id = -1; |
| 374 { | 379 { |
| 375 AutoLock lock(lock_); | 380 AutoLock lock(lock_); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 420 return -1; | 425 return -1; |
| 421 } | 426 } |
| 422 } | 427 } |
| 423 ret_begin_id = static_cast<int>(logged_events_.size()); | 428 ret_begin_id = static_cast<int>(logged_events_.size()); |
| 424 logged_events_.push_back( | 429 logged_events_.push_back( |
| 425 TraceEvent(static_cast<unsigned long>(base::GetCurrentProcId()), | 430 TraceEvent(static_cast<unsigned long>(base::GetCurrentProcId()), |
| 426 thread_id, | 431 thread_id, |
| 427 now, phase, category, name, | 432 now, phase, category, name, |
| 428 arg1_name, arg1_val, | 433 arg1_name, arg1_val, |
| 429 arg2_name, arg2_val, | 434 arg2_name, arg2_val, |
| 430 copy)); | 435 flags & EVENT_FLAG_COPY)); |
| 431 | 436 |
| 432 if (logged_events_.size() == kTraceEventBufferSize) { | 437 if (logged_events_.size() == kTraceEventBufferSize) { |
| 433 buffer_full_callback_copy = buffer_full_callback_; | 438 buffer_full_callback_copy = buffer_full_callback_; |
| 434 } | 439 } |
| 435 } // release lock | 440 } // release lock |
| 436 | 441 |
| 437 if (!buffer_full_callback_copy.is_null()) | 442 if (!buffer_full_callback_copy.is_null()) |
| 438 buffer_full_callback_copy.Run(); | 443 buffer_full_callback_copy.Run(); |
| 439 | 444 |
| 440 return ret_begin_id; | 445 return ret_begin_id; |
| 441 } | 446 } |
| 442 | 447 |
| 443 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, | 448 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, |
| 444 const char* name, | 449 const char* name, |
| 445 const void* id, | 450 const void* id, |
| 446 const char* extra) { | 451 const char* extra) { |
| 447 // Legacy trace points on windows called to ETW | |
| 448 #if defined(OS_WIN) | 452 #if defined(OS_WIN) |
| 449 TraceEventETWProvider::Trace(name, phase, id, extra); | 453 TraceEventETWProvider::Trace(name, phase, id, extra); |
| 450 #endif | 454 #endif |
| 455 INTERNAL_TRACE_EVENT_ADD(phase, | |
| 456 "ETW Trace Event", name, "id", id, "extra", TRACE_STR_COPY(extra), | |
| 457 base::debug::TraceLog::EVENT_FLAG_NONE); | |
| 458 } | |
| 451 | 459 |
| 452 // Also add new trace event behavior | 460 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, |
| 453 static const TraceCategory* category = GetCategory("ETW Trace Event"); | 461 const char* name, |
| 454 if (category->enabled) { | 462 const void* id, |
| 455 TraceLog* tracelog = TraceLog::GetInstance(); | 463 const std::string& extra) |
| 456 if (!tracelog) | 464 { |
| 457 return; | 465 #if defined(OS_WIN) |
| 458 tracelog->AddTraceEvent(phase, category, name, | 466 TraceEventETWProvider::Trace(name, phase, id, extra); |
| 459 "id", id, | 467 #endif |
| 460 "extra", extra ? extra : "", | 468 INTERNAL_TRACE_EVENT_ADD(phase, |
| 461 -1, 0, false); | 469 "ETW Trace Event", name, "id", id, "extra", extra, |
| 462 } | 470 base::debug::TraceLog::EVENT_FLAG_NONE); |
| 463 } | 471 } |
| 464 | 472 |
| 465 void TraceLog::AddCurrentMetadataEvents() { | 473 void TraceLog::AddCurrentMetadataEvents() { |
| 466 lock_.AssertAcquired(); | 474 lock_.AssertAcquired(); |
| 467 for(base::hash_map<PlatformThreadId, std::string>::iterator it = | 475 for(base::hash_map<PlatformThreadId, std::string>::iterator it = |
| 468 thread_names_.begin(); | 476 thread_names_.begin(); |
| 469 it != thread_names_.end(); | 477 it != thread_names_.end(); |
| 470 it++) { | 478 it++) { |
| 471 if (!it->second.empty()) | 479 if (!it->second.empty()) |
| 472 logged_events_.push_back( | 480 logged_events_.push_back( |
| 473 TraceEvent(static_cast<unsigned long>(base::GetCurrentProcId()), | 481 TraceEvent(static_cast<unsigned long>(base::GetCurrentProcId()), |
| 474 it->first, | 482 it->first, |
| 475 TimeTicks(), base::debug::TRACE_EVENT_PHASE_METADATA, | 483 TimeTicks(), base::debug::TRACE_EVENT_PHASE_METADATA, |
| 476 g_category_metadata, "thread_name", | 484 g_category_metadata, "thread_name", |
| 477 "name", it->second.c_str(), | 485 "name", it->second, |
| 478 NULL, 0, | 486 NULL, 0, |
| 479 false)); | 487 false)); |
| 480 } | 488 } |
| 481 } | 489 } |
| 482 | 490 |
| 483 void TraceLog::Resurrect() { | 491 void TraceLog::Resurrect() { |
| 484 StaticMemorySingletonTraits<TraceLog>::Resurrect(); | 492 StaticMemorySingletonTraits<TraceLog>::Resurrect(); |
| 485 } | 493 } |
| 486 | 494 |
| 487 namespace internal { | 495 namespace internal { |
| 488 | 496 |
| 489 void TraceEndOnScopeClose::Initialize(const TraceCategory* category, | 497 void TraceEndOnScopeClose::Initialize(const TraceCategory* category, |
| 490 const char* name) { | 498 const char* name) { |
| 491 data_.category = category; | 499 data_.category = category; |
| 492 data_.name = name; | 500 data_.name = name; |
| 493 p_data_ = &data_; | 501 p_data_ = &data_; |
| 494 } | 502 } |
| 495 | 503 |
| 496 void TraceEndOnScopeClose::AddEventIfEnabled() { | 504 void TraceEndOnScopeClose::AddEventIfEnabled() { |
| 497 // Only called when p_data_ is non-null. | 505 // Only called when p_data_ is non-null. |
| 498 if (p_data_->category->enabled) { | 506 if (p_data_->category->enabled) { |
| 499 base::debug::TraceLog::GetInstance()->AddTraceEvent( | 507 base::debug::TraceLog::GetInstance()->AddTraceEvent( |
| 500 base::debug::TRACE_EVENT_PHASE_END, | 508 base::debug::TRACE_EVENT_PHASE_END, |
| 501 p_data_->category, | 509 p_data_->category, |
| 502 p_data_->name, | 510 p_data_->name, |
| 503 NULL, 0, NULL, 0, | 511 NULL, 0, NULL, 0, |
| 504 -1, 0, false); | 512 -1, 0, TraceLog::EVENT_FLAG_NONE); |
| 505 } | 513 } |
| 506 } | 514 } |
| 507 | 515 |
| 508 void TraceEndOnScopeCloseThreshold::Initialize(const TraceCategory* category, | 516 void TraceEndOnScopeCloseThreshold::Initialize(const TraceCategory* category, |
| 509 const char* name, | 517 const char* name, |
| 510 int threshold_begin_id, | 518 int threshold_begin_id, |
| 511 int64 threshold) { | 519 int64 threshold) { |
| 512 data_.category = category; | 520 data_.category = category; |
| 513 data_.name = name; | 521 data_.name = name; |
| 514 data_.threshold_begin_id = threshold_begin_id; | 522 data_.threshold_begin_id = threshold_begin_id; |
| 515 data_.threshold = threshold; | 523 data_.threshold = threshold; |
| 516 p_data_ = &data_; | 524 p_data_ = &data_; |
| 517 } | 525 } |
| 518 | 526 |
| 519 void TraceEndOnScopeCloseThreshold::AddEventIfEnabled() { | 527 void TraceEndOnScopeCloseThreshold::AddEventIfEnabled() { |
| 520 // Only called when p_data_ is non-null. | 528 // Only called when p_data_ is non-null. |
| 521 if (p_data_->category->enabled) { | 529 if (p_data_->category->enabled) { |
| 522 base::debug::TraceLog::GetInstance()->AddTraceEvent( | 530 base::debug::TraceLog::GetInstance()->AddTraceEvent( |
| 523 base::debug::TRACE_EVENT_PHASE_END, | 531 base::debug::TRACE_EVENT_PHASE_END, |
| 524 p_data_->category, | 532 p_data_->category, |
| 525 p_data_->name, | 533 p_data_->name, |
| 526 NULL, 0, NULL, 0, | 534 NULL, 0, NULL, 0, |
| 527 p_data_->threshold_begin_id, p_data_->threshold, false); | 535 p_data_->threshold_begin_id, p_data_->threshold, |
| 536 TraceLog::EVENT_FLAG_NONE); | |
| 528 } | 537 } |
| 529 } | 538 } |
| 530 | 539 |
| 531 } // namespace internal | 540 } // namespace internal |
| 532 | 541 |
| 533 } // namespace debug | 542 } // namespace debug |
| 534 } // namespace base | 543 } // namespace base |
| OLD | NEW |