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