Chromium Code Reviews| Index: src/libplatform/tracing/trace-writer.cc |
| diff --git a/src/libplatform/tracing/trace-writer.cc b/src/libplatform/tracing/trace-writer.cc |
| index 464b5713fc2b69b6076f43b7cd40ef0ad7e8dd50..d8301cf3e1548f938cfa4560eb7934aada283884 100644 |
| --- a/src/libplatform/tracing/trace-writer.cc |
| +++ b/src/libplatform/tracing/trace-writer.cc |
| @@ -4,12 +4,85 @@ |
| #include "src/libplatform/tracing/trace-writer.h" |
| +#include <cmath> |
|
lpy
2016/08/02 00:07:09
From the coding style I can see we use match.h rat
rskang
2016/08/02 00:46:55
Acknowledged.
|
| + |
| +#include "base/trace_event/common/trace_event_common.h" |
| #include "src/base/platform/platform.h" |
| namespace v8 { |
| namespace platform { |
| namespace tracing { |
| +// Currently we do not support JSON-escaping strings in trace arguments. |
| +// Thus we perform an IsJSONString() check before writing any string argument. |
| +// In particular, this means strings cannot have control characters or " or \. |
| +bool IsJSONString(const char* str) { |
|
lpy
2016/08/02 00:07:09
V8_INLINE, maybe also static? I am not sure if sta
rskang
2016/08/02 00:46:55
Done.
|
| + size_t len = strlen(str); |
| + for (size_t i = 0; i < len; ++i) { |
| + if (iscntrl(str[i]) || str[i] == '\"' || str[i] == '\\') { |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +void JSONTraceWriter::AppendArgValue(uint8_t type, |
| + TraceObject::ArgValue value) { |
| + switch (type) { |
| + case TRACE_VALUE_TYPE_BOOL: |
| + stream_ << (value.as_bool ? "true" : "false"); |
| + break; |
| + case TRACE_VALUE_TYPE_UINT: |
| + stream_ << value.as_uint; |
| + break; |
| + case TRACE_VALUE_TYPE_INT: |
| + stream_ << value.as_int; |
| + break; |
| + case TRACE_VALUE_TYPE_DOUBLE: { |
| + std::string real; |
| + double val = value.as_double; |
| + if (std::isfinite(val)) { |
| + std::ostringstream convert_stream; |
| + convert_stream << val; |
| + real = convert_stream.str(); |
| + // Ensure that the number has a .0 if there's no decimal or 'e'. This |
| + // makes sure that when we read the JSON back, it's interpreted as a |
| + // real rather than an int. |
| + if (real.find('.') == std::string::npos && |
| + real.find('e') == std::string::npos && |
| + real.find('E') == std::string::npos) { |
| + real += ".0"; |
| + } |
| + } else if (std::isnan(val)) { |
| + // The JSON spec doesn't allow NaN and Infinity (since these are |
| + // objects in EcmaScript). Use strings instead. |
| + real = "\"NaN\""; |
| + } else if (val < 0) { |
| + real = "\"-Infinity\""; |
| + } else { |
| + real = "\"Infinity\""; |
| + } |
| + stream_ << real; |
| + break; |
| + } |
| + case TRACE_VALUE_TYPE_POINTER: |
| + // JSON only supports double and int numbers. |
| + // So as not to lose bits from a 64-bit pointer, output as a hex string. |
| + stream_ << "\"" << value.as_pointer << "\""; |
| + break; |
| + case TRACE_VALUE_TYPE_STRING: |
| + case TRACE_VALUE_TYPE_COPY_STRING: |
| + // Strings are currently not JSON-escaped, so we need to perform a check |
| + // to see if they are valid JSON strings. |
| + CHECK(value.as_string == nullptr || IsJSONString(value.as_string)); |
| + stream_ << "\"" << (value.as_string ? value.as_string : "NULL") << "\""; |
| + break; |
| + default: |
| + UNREACHABLE(); |
| + break; |
| + } |
| +} |
| + |
| JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { |
| stream_ << "{\"traceEvents\":["; |
| } |
| @@ -19,30 +92,33 @@ JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } |
| void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { |
| if (append_comma_) stream_ << ","; |
| append_comma_ = true; |
| - if (trace_event->scope() == NULL) { |
| - stream_ << "{\"pid\":" << trace_event->pid() |
| - << ",\"tid\":" << trace_event->tid() |
| - << ",\"ts\":" << trace_event->ts() |
| - << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" |
| - << trace_event->phase() << "\",\"cat\":\"" |
| - << TracingController::GetCategoryGroupName( |
| - trace_event->category_enabled_flag()) |
| - << "\",\"name\":\"" << trace_event->name() |
| - << "\",\"args\":{},\"dur\":" << trace_event->duration() |
| - << ",\"tdur\":" << trace_event->cpu_duration() << "}"; |
| - } else { |
| - stream_ << "{\"pid\":" << trace_event->pid() |
| - << ",\"tid\":" << trace_event->tid() |
| - << ",\"ts\":" << trace_event->ts() |
| - << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" |
| - << trace_event->phase() << "\",\"cat\":\"" |
| - << TracingController::GetCategoryGroupName( |
| - trace_event->category_enabled_flag()) |
| - << "\",\"name\":\"" << trace_event->name() << "\",\"scope\":\"" |
| - << trace_event->scope() |
| - << "\",\"args\":{},\"dur\":" << trace_event->duration() |
| - << ",\"tdur\":" << trace_event->cpu_duration() << "}"; |
| + stream_ << "{\"pid\":" << trace_event->pid() |
| + << ",\"tid\":" << trace_event->tid() |
| + << ",\"ts\":" << trace_event->ts() |
| + << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" |
| + << trace_event->phase() << "\",\"cat\":\"" |
| + << TracingController::GetCategoryGroupName( |
| + trace_event->category_enabled_flag()) |
| + << "\",\"name\":\"" << trace_event->name() |
| + << "\",\"dur\":" << trace_event->duration() |
| + << ",\"tdur\":" << trace_event->cpu_duration(); |
| + if (trace_event->flags() & TRACE_EVENT_FLAG_HAS_ID) { |
| + if (trace_event->scope() != nullptr) { |
| + stream_ << ",\"scope\":\"" << trace_event->scope() << "\""; |
| + } |
| + // So as not to lose bits from a 64-bit integer, output as a hex string. |
| + stream_ << ",\"id\":\"0x" << std::hex << trace_event->id() << "\""; |
| + } |
| + stream_ << ",\"args\":{"; |
| + const char** arg_names = trace_event->arg_names(); |
| + const uint8_t* arg_types = trace_event->arg_types(); |
| + TraceObject::ArgValue* arg_values = trace_event->arg_values(); |
| + for (int i = 0; i < trace_event->num_args(); ++i) { |
| + if (i > 0) stream_ << ","; |
| + stream_ << "\"" << arg_names[i] << "\":"; |
| + AppendArgValue(arg_types[i], arg_values[i]); |
| } |
| + stream_ << "}}"; |
| // TODO(fmeawad): Add support for Flow Events. |
| } |