OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project 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 "src/libplatform/tracing/trace-writer.h" | 5 #include "src/libplatform/tracing/trace-writer.h" |
6 | 6 |
| 7 #include <cmath> |
| 8 |
| 9 #include "base/trace_event/common/trace_event_common.h" |
7 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
8 | 11 |
9 namespace v8 { | 12 namespace v8 { |
10 namespace platform { | 13 namespace platform { |
11 namespace tracing { | 14 namespace tracing { |
12 | 15 |
| 16 bool IsJSONString(const char* str) { |
| 17 size_t len = strlen(str); |
| 18 for (size_t i = 0; i < len; ++i) { |
| 19 if (iscntrl(str[i]) || str[i] == '\"' || str[i] == '\\') { |
| 20 return false; |
| 21 } |
| 22 } |
| 23 return true; |
| 24 } |
| 25 |
| 26 void JSONTraceWriter::AppendTraceValue(uint8_t type, |
| 27 TraceObject::TraceValue value) { |
| 28 switch (type) { |
| 29 case TRACE_VALUE_TYPE_BOOL: |
| 30 stream_ << (value.as_bool ? "true" : "false"); |
| 31 break; |
| 32 case TRACE_VALUE_TYPE_UINT: |
| 33 stream_ << value.as_uint; |
| 34 break; |
| 35 case TRACE_VALUE_TYPE_INT: |
| 36 stream_ << value.as_int; |
| 37 break; |
| 38 case TRACE_VALUE_TYPE_DOUBLE: { |
| 39 std::string real; |
| 40 double val = value.as_double; |
| 41 if (std::isfinite(val)) { |
| 42 std::ostringstream convert_stream; |
| 43 convert_stream << val; |
| 44 real = convert_stream.str(); |
| 45 // Ensure that the number has a .0 if there's no decimal or 'e'. This |
| 46 // makes sure that when we read the JSON back, it's interpreted as a |
| 47 // real rather than an int. |
| 48 if (real.find('.') == std::string::npos && |
| 49 real.find('e') == std::string::npos && |
| 50 real.find('E') == std::string::npos) { |
| 51 real += ".0"; |
| 52 } |
| 53 } else if (std::isnan(val)) { |
| 54 // The JSON spec doesn't allow NaN and Infinity (since these are |
| 55 // objects in EcmaScript). Use strings instead. |
| 56 real = "\"NaN\""; |
| 57 } else if (val < 0) { |
| 58 real = "\"-Infinity\""; |
| 59 } else { |
| 60 real = "\"Infinity\""; |
| 61 } |
| 62 stream_ << real; |
| 63 break; |
| 64 } |
| 65 case TRACE_VALUE_TYPE_POINTER: |
| 66 // JSON only supports double and int numbers. |
| 67 // So as not to lose bits from a 64-bit pointer, output as a hex string. |
| 68 stream_ << "\"" << value.as_pointer << "\""; |
| 69 break; |
| 70 case TRACE_VALUE_TYPE_STRING: |
| 71 case TRACE_VALUE_TYPE_COPY_STRING: |
| 72 // Strings are currently not JSON-escaped, so we cannot allow control |
| 73 // characters or " or \. |
| 74 CHECK(IsJSONString(value.as_string)); |
| 75 stream_ << "\"" << value.as_string << "\""; |
| 76 break; |
| 77 default: |
| 78 UNREACHABLE(); |
| 79 break; |
| 80 } |
| 81 } |
| 82 |
13 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { | 83 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { |
14 stream_ << "{\"traceEvents\":["; | 84 stream_ << "{\"traceEvents\":["; |
15 } | 85 } |
16 | 86 |
17 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } | 87 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } |
18 | 88 |
19 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { | 89 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { |
20 if (append_comma_) stream_ << ","; | 90 if (append_comma_) stream_ << ","; |
21 append_comma_ = true; | 91 append_comma_ = true; |
22 if (trace_event->scope() == NULL) { | 92 stream_ << "{\"pid\":" << trace_event->pid() |
23 stream_ << "{\"pid\":" << trace_event->pid() | 93 << ",\"tid\":" << trace_event->tid() |
24 << ",\"tid\":" << trace_event->tid() | 94 << ",\"ts\":" << trace_event->ts() |
25 << ",\"ts\":" << trace_event->ts() | 95 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" |
26 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" | 96 << trace_event->phase() << "\",\"cat\":\"" |
27 << trace_event->phase() << "\",\"cat\":\"" | 97 << TracingController::GetCategoryGroupName( |
28 << TracingController::GetCategoryGroupName( | 98 trace_event->category_enabled_flag()) |
29 trace_event->category_enabled_flag()) | 99 << "\",\"name\":\"" << trace_event->name() |
30 << "\",\"name\":\"" << trace_event->name() | 100 << "\",\"dur\":" << trace_event->duration() |
31 << "\",\"args\":{},\"dur\":" << trace_event->duration() | 101 << ",\"tdur\":" << trace_event->cpu_duration(); |
32 << ",\"tdur\":" << trace_event->cpu_duration() << "}"; | 102 if (trace_event->flags() & TRACE_EVENT_FLAG_HAS_ID) { |
33 } else { | 103 if (trace_event->scope() != nullptr) { |
34 stream_ << "{\"pid\":" << trace_event->pid() | 104 stream_ << ",\"scope\":\"" << trace_event->scope() << "\""; |
35 << ",\"tid\":" << trace_event->tid() | 105 } |
36 << ",\"ts\":" << trace_event->ts() | 106 stream_ << ",\"id\":" << trace_event->id(); |
37 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" | |
38 << trace_event->phase() << "\",\"cat\":\"" | |
39 << TracingController::GetCategoryGroupName( | |
40 trace_event->category_enabled_flag()) | |
41 << "\",\"name\":\"" << trace_event->name() << "\",\"scope\":\"" | |
42 << trace_event->scope() | |
43 << "\",\"args\":{},\"dur\":" << trace_event->duration() | |
44 << ",\"tdur\":" << trace_event->cpu_duration() << "}"; | |
45 } | 107 } |
| 108 stream_ << ",\"args\":{"; |
| 109 const char** arg_names = trace_event->arg_names(); |
| 110 const uint8_t* arg_types = trace_event->arg_types(); |
| 111 TraceObject::TraceValue* arg_values = trace_event->arg_values(); |
| 112 for (int i = 0; i < kTraceMaxNumArgs && arg_names[i]; ++i) { |
| 113 if (i > 0) stream_ << ","; |
| 114 stream_ << "\"" << arg_names[i] << "\":"; |
| 115 AppendTraceValue(arg_types[i], arg_values[i]); |
| 116 } |
| 117 stream_ << "}}"; |
46 // TODO(fmeawad): Add support for Flow Events. | 118 // TODO(fmeawad): Add support for Flow Events. |
47 } | 119 } |
48 | 120 |
49 void JSONTraceWriter::Flush() {} | 121 void JSONTraceWriter::Flush() {} |
50 | 122 |
51 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { | 123 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { |
52 return new JSONTraceWriter(stream); | 124 return new JSONTraceWriter(stream); |
53 } | 125 } |
54 | 126 |
55 } // namespace tracing | 127 } // namespace tracing |
56 } // namespace platform | 128 } // namespace platform |
57 } // namespace v8 | 129 } // namespace v8 |
OLD | NEW |