Chromium Code Reviews| 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 | |
| 7 #include "src/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
| 10 #include "src/tracing/trace-event.h" | |
| 8 | 11 |
| 9 namespace v8 { | 12 namespace v8 { |
| 10 namespace platform { | 13 namespace platform { |
| 11 namespace tracing { | 14 namespace tracing { |
| 12 | 15 |
| 13 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { | 16 void EscapeJSONString(Isolate* isolate, const char* str, std::ostream& stream) { |
| 17 HandleScope handle_scope(isolate); | |
| 18 Local<Context> context = Context::New(isolate); | |
| 19 Context::Scope context_scope(context); | |
| 20 Local<String> v8_str = | |
| 21 String::NewFromUtf8(isolate, str, NewStringType::kNormal) | |
| 22 .ToLocalChecked(); | |
| 23 Local<Object> v8_obj = v8_str->ToObject(context).ToLocalChecked(); | |
| 24 String::Utf8Value escaped_str( | |
| 25 JSON::Stringify(context, v8_obj).ToLocalChecked()); | |
| 26 stream << *escaped_str; | |
| 27 } | |
| 28 | |
| 29 void JSONTraceWriter::AppendTraceValue(uint8_t type, | |
| 30 TraceObject::TraceValue 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 EscapeJSONString(isolate_, value.as_string ? value.as_string : "NULL", | |
|
lpy
2016/07/28 21:18:20
Why do you think this should be escaped? Can you g
rskang
2016/07/28 21:25:54
Example, for single string argument:
TRACE_EVENT1(
rskang
2016/07/28 22:45:36
I have removed JSON escaping for a later CL.
| |
| 76 stream_); | |
| 77 break; | |
| 78 default: | |
| 79 UNREACHABLE(); | |
| 80 break; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 JSONTraceWriter::JSONTraceWriter(Isolate* isolate, std::ostream& stream) | |
| 85 : isolate_(isolate), stream_(stream) { | |
| 14 stream_ << "{\"traceEvents\":["; | 86 stream_ << "{\"traceEvents\":["; |
| 15 } | 87 } |
| 16 | 88 |
| 17 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } | 89 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } |
| 18 | 90 |
| 19 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { | 91 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { |
| 20 if (append_comma_) stream_ << ","; | 92 if (append_comma_) stream_ << ","; |
| 21 append_comma_ = true; | 93 append_comma_ = true; |
| 22 if (trace_event->scope() == NULL) { | 94 stream_ << "{\"pid\":" << trace_event->pid() |
| 23 stream_ << "{\"pid\":" << trace_event->pid() | 95 << ",\"tid\":" << trace_event->tid() |
| 24 << ",\"tid\":" << trace_event->tid() | 96 << ",\"ts\":" << trace_event->ts() |
| 25 << ",\"ts\":" << trace_event->ts() | 97 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" |
| 26 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" | 98 << trace_event->phase() << "\",\"cat\":\"" |
| 27 << trace_event->phase() << "\",\"cat\":\"" | 99 << TracingController::GetCategoryGroupName( |
| 28 << TracingController::GetCategoryGroupName( | 100 trace_event->category_enabled_flag()) |
| 29 trace_event->category_enabled_flag()) | 101 << "\",\"name\":"; |
| 30 << "\",\"name\":\"" << trace_event->name() | 102 EscapeJSONString(isolate_, trace_event->name(), stream_); |
| 31 << "\",\"args\":{},\"dur\":" << trace_event->duration() | 103 stream_ << ",\"dur\":" << trace_event->duration() |
| 32 << ",\"tdur\":" << trace_event->cpu_duration() << "}"; | 104 << ",\"tdur\":" << trace_event->cpu_duration(); |
| 33 } else { | 105 if (trace_event->flags() & TRACE_EVENT_FLAG_HAS_ID) { |
| 34 stream_ << "{\"pid\":" << trace_event->pid() | 106 if (trace_event->scope() != internal::tracing::kGlobalScope) { |
| 35 << ",\"tid\":" << trace_event->tid() | 107 stream_ << ",\"scope\":\"" << trace_event->scope() << "\""; |
| 36 << ",\"ts\":" << trace_event->ts() | 108 } |
| 37 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" | 109 stream_ << ",\"id\":" << trace_event->id(); |
| 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 } | 110 } |
| 111 stream_ << ",\"args\":{"; | |
| 112 const char** arg_names = trace_event->arg_names(); | |
| 113 const uint8_t* arg_types = trace_event->arg_types(); | |
| 114 TraceObject::TraceValue* arg_values = trace_event->arg_values(); | |
| 115 for (int i = 0; i < kTraceMaxNumArgs && arg_names[i]; ++i) { | |
| 116 if (i > 0) stream_ << ","; | |
| 117 stream_ << "\"" << arg_names[i] << "\":"; | |
| 118 AppendTraceValue(arg_types[i], arg_values[i]); | |
| 119 } | |
| 120 stream_ << "}}"; | |
| 46 // TODO(fmeawad): Add support for Flow Events. | 121 // TODO(fmeawad): Add support for Flow Events. |
| 47 } | 122 } |
| 48 | 123 |
| 49 void JSONTraceWriter::Flush() {} | 124 void JSONTraceWriter::Flush() {} |
| 50 | 125 |
| 51 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { | 126 TraceWriter* TraceWriter::CreateJSONTraceWriter(Isolate* isolate, |
| 52 return new JSONTraceWriter(stream); | 127 std::ostream& stream) { |
| 128 return new JSONTraceWriter(isolate, stream); | |
| 53 } | 129 } |
| 54 | 130 |
| 55 } // namespace tracing | 131 } // namespace tracing |
| 56 } // namespace platform | 132 } // namespace platform |
| 57 } // namespace v8 | 133 } // namespace v8 |
| OLD | NEW |