| 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" |
| 11 #endif | 11 #endif |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/ref_counted_memory.h" | |
| 15 #include "base/process_util.h" | 14 #include "base/process_util.h" |
| 16 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 17 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 18 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 19 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 20 #include "base/time.h" | 19 #include "base/time.h" |
| 21 | 20 |
| 22 #define USE_UNRELIABLE_NOW | 21 #define USE_UNRELIABLE_NOW |
| 23 | 22 |
| 24 class DeleteTraceLogForTesting { | 23 class DeleteTraceLogForTesting { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 111 } |
| 113 | 112 |
| 114 //////////////////////////////////////////////////////////////////////////////// | 113 //////////////////////////////////////////////////////////////////////////////// |
| 115 // | 114 // |
| 116 // TraceEvent | 115 // TraceEvent |
| 117 // | 116 // |
| 118 //////////////////////////////////////////////////////////////////////////////// | 117 //////////////////////////////////////////////////////////////////////////////// |
| 119 | 118 |
| 120 namespace { | 119 namespace { |
| 121 | 120 |
| 122 const char* GetPhaseStr(TraceEventPhase phase) { | |
| 123 switch(phase) { | |
| 124 case TRACE_EVENT_PHASE_BEGIN: | |
| 125 return "B"; | |
| 126 case TRACE_EVENT_PHASE_INSTANT: | |
| 127 return "I"; | |
| 128 case TRACE_EVENT_PHASE_END: | |
| 129 return "E"; | |
| 130 case TRACE_EVENT_PHASE_METADATA: | |
| 131 return "M"; | |
| 132 default: | |
| 133 NOTREACHED() << "Invalid phase argument"; | |
| 134 return "?"; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } | 121 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } |
| 139 | 122 |
| 140 // Copies |*member| into |*buffer|, sets |*member| to point to this new | 123 // Copies |*member| into |*buffer|, sets |*member| to point to this new |
| 141 // location, and then advances |*buffer| by the amount written. | 124 // location, and then advances |*buffer| by the amount written. |
| 142 void CopyTraceEventParameter(char** buffer, | 125 void CopyTraceEventParameter(char** buffer, |
| 143 const char** member, | 126 const char** member, |
| 144 const char* end) { | 127 const char* end) { |
| 145 if (*member) { | 128 if (*member) { |
| 146 size_t written = strlcpy(*buffer, *member, end - *buffer) + 1; | 129 size_t written = strlcpy(*buffer, *member, end - *buffer) + 1; |
| 147 DCHECK_LE(static_cast<int>(written), end - *buffer); | 130 DCHECK_LE(static_cast<int>(written), end - *buffer); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); | 197 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); |
| 215 if (arg2_is_copy) | 198 if (arg2_is_copy) |
| 216 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); | 199 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); |
| 217 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; | 200 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; |
| 218 } | 201 } |
| 219 } | 202 } |
| 220 | 203 |
| 221 TraceEvent::~TraceEvent() { | 204 TraceEvent::~TraceEvent() { |
| 222 } | 205 } |
| 223 | 206 |
| 207 const char* TraceEvent::GetPhaseString(TraceEventPhase phase) { |
| 208 switch(phase) { |
| 209 case TRACE_EVENT_PHASE_BEGIN: |
| 210 return "B"; |
| 211 case TRACE_EVENT_PHASE_INSTANT: |
| 212 return "I"; |
| 213 case TRACE_EVENT_PHASE_END: |
| 214 return "E"; |
| 215 case TRACE_EVENT_PHASE_METADATA: |
| 216 return "M"; |
| 217 default: |
| 218 NOTREACHED() << "Invalid phase argument"; |
| 219 return "?"; |
| 220 } |
| 221 } |
| 222 |
| 223 TraceEventPhase TraceEvent::GetPhase(const char* phase) { |
| 224 switch(*phase) { |
| 225 case 'B': |
| 226 return TRACE_EVENT_PHASE_BEGIN; |
| 227 case 'I': |
| 228 return TRACE_EVENT_PHASE_INSTANT; |
| 229 case 'E': |
| 230 return TRACE_EVENT_PHASE_END; |
| 231 case 'M': |
| 232 return TRACE_EVENT_PHASE_METADATA; |
| 233 default: |
| 234 NOTREACHED() << "Invalid phase name"; |
| 235 return TRACE_EVENT_PHASE_METADATA; |
| 236 } |
| 237 } |
| 238 |
| 224 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 239 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
| 225 size_t start, | 240 size_t start, |
| 226 size_t count, | 241 size_t count, |
| 227 std::string* out) { | 242 std::string* out) { |
| 228 for (size_t i = 0; i < count && start + i < events.size(); ++i) { | 243 for (size_t i = 0; i < count && start + i < events.size(); ++i) { |
| 229 if (i > 0) | 244 if (i > 0) |
| 230 *out += ","; | 245 *out += ","; |
| 231 events[i + start].AppendAsJSON(out); | 246 events[i + start].AppendAsJSON(out); |
| 232 } | 247 } |
| 233 } | 248 } |
| 234 | 249 |
| 235 void TraceEvent::AppendAsJSON(std::string* out) const { | 250 void TraceEvent::AppendAsJSON(std::string* out) const { |
| 236 const char* phase_str = GetPhaseStr(phase_); | 251 const char* phase_str = GetPhaseString(phase_); |
| 237 int64 time_int64 = timestamp_.ToInternalValue(); | 252 int64 time_int64 = timestamp_.ToInternalValue(); |
| 238 // Category name checked at category creation time. | 253 // Category name checked at category creation time. |
| 239 DCHECK(!strchr(name_, '"')); | 254 DCHECK(!strchr(name_, '"')); |
| 240 StringAppendF(out, | 255 StringAppendF(out, |
| 241 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," | 256 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," |
| 242 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", | 257 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", |
| 243 category_->name, | 258 category_->name, |
| 244 static_cast<int>(process_id_), | 259 static_cast<int>(process_id_), |
| 245 static_cast<int>(thread_id_), | 260 static_cast<int>(thread_id_), |
| 246 static_cast<long long>(time_int64), | 261 static_cast<long long>(time_int64), |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 NULL, 0, NULL, 0, | 666 NULL, 0, NULL, 0, |
| 652 p_data_->threshold_begin_id, p_data_->threshold, | 667 p_data_->threshold_begin_id, p_data_->threshold, |
| 653 TraceLog::EVENT_FLAG_NONE); | 668 TraceLog::EVENT_FLAG_NONE); |
| 654 } | 669 } |
| 655 } | 670 } |
| 656 | 671 |
| 657 } // namespace internal | 672 } // namespace internal |
| 658 | 673 |
| 659 } // namespace debug | 674 } // namespace debug |
| 660 } // namespace base | 675 } // namespace base |
| OLD | NEW |