traced-value.cc 5.37 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/tracing/traced-value.h"

#include "src/base/platform/platform.h"
8
#include "src/base/vector.h"
9
#include "src/numbers/conversions.h"
10

11 12 13 14
#ifdef V8_USE_PERFETTO
#include "protos/perfetto/trace/track_event/debug_annotation.pbzero.h"
#endif

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
namespace v8 {
namespace tracing {

namespace {

#define DCHECK_CURRENT_CONTAINER_IS(x) DCHECK_EQ(x, nesting_stack_.back())
#define DCHECK_CONTAINER_STACK_DEPTH_EQ(x) DCHECK_EQ(x, nesting_stack_.size())
#ifdef DEBUG
const bool kStackTypeDict = false;
const bool kStackTypeArray = true;
#define DEBUG_PUSH_CONTAINER(x) nesting_stack_.push_back(x)
#define DEBUG_POP_CONTAINER() nesting_stack_.pop_back()
#else
#define DEBUG_PUSH_CONTAINER(x) ((void)0)
#define DEBUG_POP_CONTAINER() ((void)0)
#endif

32 33 34
void EscapeAndAppendString(const char* value, std::string* result) {
  *result += '"';
  while (*value) {
35
    unsigned char c = *value++;
36
    switch (c) {
37 38 39 40 41
      case '\b':
        *result += "\\b";
        break;
      case '\f':
        *result += "\\f";
42 43
        break;
      case '\n':
44
        *result += "\\n";
45
        break;
46 47 48 49 50 51
      case '\r':
        *result += "\\r";
        break;
      case '\t':
        *result += "\\t";
        break;
52
      case '\"':
53
        *result += "\\\"";
54 55
        break;
      case '\\':
56
        *result += "\\\\";
57 58
        break;
      default:
59 60
        if (c < '\x20' || c == '\x7F') {
          char number_buffer[8];
61 62
          base::OS::SNPrintF(number_buffer, arraysize(number_buffer), "\\u%04X",
                             static_cast<unsigned>(c));
63
          *result += number_buffer;
64
        } else {
65
          *result += c;
66 67 68
        }
    }
  }
69
  *result += '"';
70 71 72 73
}

}  // namespace

74
// static
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
std::unique_ptr<TracedValue> TracedValue::Create() {
  return std::unique_ptr<TracedValue>(new TracedValue());
}

TracedValue::TracedValue() : first_item_(true) {
  DEBUG_PUSH_CONTAINER(kStackTypeDict);
}

TracedValue::~TracedValue() {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  DEBUG_POP_CONTAINER();
  DCHECK_CONTAINER_STACK_DEPTH_EQ(0u);
}

void TracedValue::SetInteger(const char* name, int value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  WriteName(name);
  data_ += std::to_string(value);
}

void TracedValue::SetDouble(const char* name, double value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  WriteName(name);
98 99
  base::EmbeddedVector<char, 100> buffer;
  data_ += internal::DoubleToCString(value, buffer);
100 101 102 103 104 105 106 107
}

void TracedValue::SetBoolean(const char* name, bool value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  WriteName(name);
  data_ += value ? "true" : "false";
}

108
void TracedValue::SetString(const char* name, const char* value) {
109 110
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  WriteName(name);
111
  EscapeAndAppendString(value, &data_);
112 113
}

114 115 116 117 118 119 120 121
void TracedValue::SetValue(const char* name, TracedValue* value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  WriteName(name);
  std::string tmp;
  value->AppendAsTraceFormat(&tmp);
  data_ += tmp;
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
void TracedValue::BeginDictionary(const char* name) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  DEBUG_PUSH_CONTAINER(kStackTypeDict);
  WriteName(name);
  data_ += '{';
  first_item_ = true;
}

void TracedValue::BeginArray(const char* name) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  DEBUG_PUSH_CONTAINER(kStackTypeArray);
  WriteName(name);
  data_ += '[';
  first_item_ = true;
}

void TracedValue::AppendInteger(int value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  WriteComma();
  data_ += std::to_string(value);
}

void TracedValue::AppendDouble(double value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  WriteComma();
147 148
  base::EmbeddedVector<char, 100> buffer;
  data_ += internal::DoubleToCString(value, buffer);
149 150 151 152 153 154 155 156
}

void TracedValue::AppendBoolean(bool value) {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  WriteComma();
  data_ += value ? "true" : "false";
}

157
void TracedValue::AppendString(const char* value) {
158 159
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  WriteComma();
160
  EscapeAndAppendString(value, &data_);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

void TracedValue::BeginDictionary() {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  DEBUG_PUSH_CONTAINER(kStackTypeDict);
  WriteComma();
  data_ += '{';
  first_item_ = true;
}

void TracedValue::BeginArray() {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  DEBUG_PUSH_CONTAINER(kStackTypeArray);
  WriteComma();
  data_ += '[';
  first_item_ = true;
}

void TracedValue::EndDictionary() {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
  DEBUG_POP_CONTAINER();
  data_ += '}';
  first_item_ = false;
}

void TracedValue::EndArray() {
  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
  DEBUG_POP_CONTAINER();
  data_ += ']';
  first_item_ = false;
}

void TracedValue::WriteComma() {
  if (first_item_) {
    first_item_ = false;
  } else {
    data_ += ',';
  }
}

void TracedValue::WriteName(const char* name) {
  WriteComma();
  data_ += '"';
  data_ += name;
  data_ += "\":";
}

void TracedValue::AppendAsTraceFormat(std::string* out) const {
  *out += '{';
  *out += data_;
  *out += '}';
}

214 215 216 217 218 219 220 221 222 223 224
#ifdef V8_USE_PERFETTO
void TracedValue::Add(
    perfetto::protos::pbzero::DebugAnnotation* annotation) const {
  std::string json;
  json += "{";
  json += data_;
  json += "}";
  annotation->set_legacy_json_value(json);
}
#endif  // V8_USE_PERFETTO

225 226
}  // namespace tracing
}  // namespace v8