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 <cmath> |
6 | 6 |
7 #include <cmath> | 7 #include "include/libplatform/v8-tracing.h" |
8 | 8 |
9 #include "base/trace_event/common/trace_event_common.h" | 9 #include "base/trace_event/common/trace_event_common.h" |
10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace platform { | 13 namespace platform { |
14 namespace tracing { | 14 namespace tracing { |
15 | 15 |
16 // Currently we do not support JSON-escaping strings in trace arguments. | 16 // Writes the given string to a stream, taking care to escape characters |
17 // Thus we perform an IsJSONString() check before writing any string argument. | 17 // when necessary. |
18 // In particular, this means strings cannot have control characters or \. | 18 V8_INLINE static void WriteJSONStringToStream(const char* str, |
19 V8_INLINE static bool IsJSONString(const char* str) { | 19 std::ostream& stream) { |
20 size_t len = strlen(str); | 20 size_t len = strlen(str); |
| 21 stream << "\""; |
21 for (size_t i = 0; i < len; ++i) { | 22 for (size_t i = 0; i < len; ++i) { |
22 if (iscntrl(str[i]) || str[i] == '\\') { | 23 // All of the permitted escape sequences in JSON strings, as per |
23 return false; | 24 // https://mathiasbynens.be/notes/javascript-escapes |
| 25 switch (str[i]) { |
| 26 case '\b': |
| 27 stream << "\\b"; |
| 28 break; |
| 29 case '\f': |
| 30 stream << "\\f"; |
| 31 break; |
| 32 case '\n': |
| 33 stream << "\\n"; |
| 34 break; |
| 35 case '\r': |
| 36 stream << "\\r"; |
| 37 break; |
| 38 case '\t': |
| 39 stream << "\\t"; |
| 40 break; |
| 41 case '\"': |
| 42 stream << "\\\""; |
| 43 break; |
| 44 case '\\': |
| 45 stream << "\\\\"; |
| 46 break; |
| 47 // Note that because we use double quotes for JSON strings, |
| 48 // we don't need to escape single quotes. |
| 49 default: |
| 50 stream << str[i]; |
| 51 break; |
24 } | 52 } |
25 } | 53 } |
26 return true; | 54 stream << "\""; |
27 } | 55 } |
28 | 56 |
29 void JSONTraceWriter::AppendArgValue(uint8_t type, | 57 void TraceSerializer::AppendArgValue(uint8_t type, |
30 TraceObject::ArgValue value) { | 58 TraceObject::ArgValue value) { |
31 switch (type) { | 59 switch (type) { |
32 case TRACE_VALUE_TYPE_BOOL: | 60 case TRACE_VALUE_TYPE_BOOL: |
33 stream_ << (value.as_bool ? "true" : "false"); | 61 stream_ << (value.as_bool ? "true" : "false"); |
34 break; | 62 break; |
35 case TRACE_VALUE_TYPE_UINT: | 63 case TRACE_VALUE_TYPE_UINT: |
36 stream_ << value.as_uint; | 64 stream_ << value.as_uint; |
37 break; | 65 break; |
38 case TRACE_VALUE_TYPE_INT: | 66 case TRACE_VALUE_TYPE_INT: |
39 stream_ << value.as_int; | 67 stream_ << value.as_int; |
(...skipping 25 matching lines...) Expand all Loading... |
65 stream_ << real; | 93 stream_ << real; |
66 break; | 94 break; |
67 } | 95 } |
68 case TRACE_VALUE_TYPE_POINTER: | 96 case TRACE_VALUE_TYPE_POINTER: |
69 // JSON only supports double and int numbers. | 97 // JSON only supports double and int numbers. |
70 // So as not to lose bits from a 64-bit pointer, output as a hex string. | 98 // So as not to lose bits from a 64-bit pointer, output as a hex string. |
71 stream_ << "\"" << value.as_pointer << "\""; | 99 stream_ << "\"" << value.as_pointer << "\""; |
72 break; | 100 break; |
73 case TRACE_VALUE_TYPE_STRING: | 101 case TRACE_VALUE_TYPE_STRING: |
74 case TRACE_VALUE_TYPE_COPY_STRING: | 102 case TRACE_VALUE_TYPE_COPY_STRING: |
75 // Strings are currently not JSON-escaped, so we need to perform a check | 103 if (value.as_string == nullptr) { |
76 // to see if they are valid JSON strings. | 104 stream_ << "\"NULL\""; |
77 CHECK(value.as_string != nullptr && IsJSONString(value.as_string)); | 105 } else { |
78 stream_ << (value.as_string ? value.as_string : "NULL"); | 106 WriteJSONStringToStream(value.as_string, stream_); |
| 107 } |
79 break; | 108 break; |
80 default: | 109 default: |
81 UNREACHABLE(); | 110 UNREACHABLE(); |
82 break; | 111 break; |
83 } | 112 } |
84 } | 113 } |
85 | 114 |
86 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { | 115 TraceSerializer::TraceSerializer(std::ostream& stream) : stream_(stream) {} |
| 116 |
| 117 void TraceSerializer::WritePrefix() { |
87 stream_ << "{\"traceEvents\":["; | 118 stream_ << "{\"traceEvents\":["; |
| 119 append_comma_ = false; |
88 } | 120 } |
89 | 121 |
90 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } | 122 void TraceSerializer::WriteSuffix() { stream_ << "]}"; } |
91 | 123 |
92 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { | 124 void TraceSerializer::AppendTraceEvent(TraceObject* trace_event) { |
93 if (append_comma_) stream_ << ","; | 125 if (append_comma_) stream_ << ","; |
94 append_comma_ = true; | 126 append_comma_ = true; |
95 stream_ << "{\"pid\":" << trace_event->pid() | 127 stream_ << "{\"pid\":" << trace_event->pid() |
96 << ",\"tid\":" << trace_event->tid() | 128 << ",\"tid\":" << trace_event->tid() |
97 << ",\"ts\":" << trace_event->ts() | 129 << ",\"ts\":" << trace_event->ts() |
98 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" | 130 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" |
99 << trace_event->phase() << "\",\"cat\":\"" | 131 << trace_event->phase() << "\",\"cat\":\"" |
100 << TracingController::GetCategoryGroupName( | 132 << TracingController::GetCategoryGroupName( |
101 trace_event->category_enabled_flag()) | 133 trace_event->category_enabled_flag()) |
102 << "\",\"name\":\"" << trace_event->name() | 134 << "\",\"name\":\"" << trace_event->name() |
(...skipping 13 matching lines...) Expand all Loading... |
116 TraceObject::ArgValue* arg_values = trace_event->arg_values(); | 148 TraceObject::ArgValue* arg_values = trace_event->arg_values(); |
117 for (int i = 0; i < trace_event->num_args(); ++i) { | 149 for (int i = 0; i < trace_event->num_args(); ++i) { |
118 if (i > 0) stream_ << ","; | 150 if (i > 0) stream_ << ","; |
119 stream_ << "\"" << arg_names[i] << "\":"; | 151 stream_ << "\"" << arg_names[i] << "\":"; |
120 AppendArgValue(arg_types[i], arg_values[i]); | 152 AppendArgValue(arg_types[i], arg_values[i]); |
121 } | 153 } |
122 stream_ << "}}"; | 154 stream_ << "}}"; |
123 // TODO(fmeawad): Add support for Flow Events. | 155 // TODO(fmeawad): Add support for Flow Events. |
124 } | 156 } |
125 | 157 |
126 void JSONTraceWriter::Flush() {} | |
127 | |
128 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { | |
129 return new JSONTraceWriter(stream); | |
130 } | |
131 | |
132 } // namespace tracing | 158 } // namespace tracing |
133 } // namespace platform | 159 } // namespace platform |
134 } // namespace v8 | 160 } // namespace v8 |
OLD | NEW |