| 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> | 7 #include <cmath> |
| 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 JSONTraceWriter::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; |
| (...skipping 28 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 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { |
| 87 stream_ << "{\"traceEvents\":["; | 116 stream_ << "{\"traceEvents\":["; |
| 88 } | 117 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 154 |
| 126 void JSONTraceWriter::Flush() {} | 155 void JSONTraceWriter::Flush() {} |
| 127 | 156 |
| 128 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { | 157 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { |
| 129 return new JSONTraceWriter(stream); | 158 return new JSONTraceWriter(stream); |
| 130 } | 159 } |
| 131 | 160 |
| 132 } // namespace tracing | 161 } // namespace tracing |
| 133 } // namespace platform | 162 } // namespace platform |
| 134 } // namespace v8 | 163 } // namespace v8 |
| OLD | NEW |