|
| 1 | +// Copyright 2018 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "src/api.h" |
| 6 | +#include "src/builtins/builtins-utils.h" |
| 7 | +#include "src/builtins/builtins.h" |
| 8 | +#include "src/counters.h" |
| 9 | +#include "src/json-stringifier.h" |
| 10 | +#include "src/objects-inl.h" |
| 11 | + |
| 12 | +namespace v8 { |
| 13 | +namespace internal { |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +using v8::tracing::TracedValue; |
| 18 | + |
| 19 | +#define MAX_STACK_LENGTH 100 |
| 20 | + |
| 21 | +class MaybeUtf8 { |
| 22 | + public: |
| 23 | + explicit MaybeUtf8(Isolate* isolate, Handle<String> string) : buf_(data_) { |
| 24 | + string = String::Flatten(isolate, string); |
| 25 | + int len; |
| 26 | + if (string->IsOneByteRepresentation()) { |
| 27 | + // Technically this allows unescaped latin1 characters but the trace |
| 28 | + // events mechanism currently does the same and the current consuming |
| 29 | + // tools are tolerant of it. A more correct approach here would be to |
| 30 | + // escape non-ascii characters but this is easier and faster. |
| 31 | + len = string->length(); |
| 32 | + AllocateSufficientSpace(len); |
| 33 | + if (len > 0) { |
| 34 | + // Why copy? Well, the trace event mechanism requires null-terminated |
| 35 | + // strings, the bytes we get from SeqOneByteString are not. buf_ is |
| 36 | + // guaranteed to be null terminated. |
| 37 | + memcpy(buf_, Handle<SeqOneByteString>::cast(string)->GetChars(), len); |
| 38 | + } |
| 39 | + } else { |
| 40 | + Local<v8::String> local = Utils::ToLocal(string); |
| 41 | + len = local->Utf8Length(); |
| 42 | + AllocateSufficientSpace(len); |
| 43 | + if (len > 0) { |
| 44 | + local->WriteUtf8(reinterpret_cast<char*>(buf_)); |
| 45 | + } |
| 46 | + } |
| 47 | + buf_[len] = 0; |
| 48 | + } |
| 49 | + const char* operator*() const { return reinterpret_cast<const char*>(buf_); } |
| 50 | + |
| 51 | + private: |
| 52 | + void AllocateSufficientSpace(int len) { |
| 53 | + if (len + 1 > MAX_STACK_LENGTH) { |
| 54 | + allocated_.reset(new uint8_t[len + 1]); |
| 55 | + buf_ = allocated_.get(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // In the most common cases, the buffer here will be stack allocated. |
| 60 | + // A heap allocation will only occur if the data is more than MAX_STACK_LENGTH |
| 61 | + // Given that this is used primarily for trace event categories and names, |
| 62 | + // the MAX_STACK_LENGTH should be more than enough. |
| 63 | + uint8_t* buf_; |
| 64 | + uint8_t data_[MAX_STACK_LENGTH]; |
| 65 | + std::unique_ptr<uint8_t> allocated_; |
| 66 | +}; |
| 67 | + |
| 68 | +class JsonTraceValue : public ConvertableToTraceFormat { |
| 69 | + public: |
| 70 | + explicit JsonTraceValue(Isolate* isolate, Handle<String> object) { |
| 71 | + // object is a JSON string serialized using JSON.stringify() from within |
| 72 | + // the BUILTIN(Trace) method. This may (likely) contain UTF8 values so |
| 73 | + // to grab the appropriate buffer data we have to serialize it out. We |
| 74 | + // hold on to the bits until the AppendAsTraceFormat method is called. |
| 75 | + MaybeUtf8 data(isolate, object); |
| 76 | + data_ = *data; |
| 77 | + } |
| 78 | + |
| 79 | + void AppendAsTraceFormat(std::string* out) const override { *out += data_; } |
| 80 | + |
| 81 | + private: |
| 82 | + std::string data_; |
| 83 | +}; |
| 84 | + |
| 85 | +const uint8_t* GetCategoryGroupEnabled(Isolate* isolate, |
| 86 | + Handle<String> string) { |
| 87 | + MaybeUtf8 category(isolate, string); |
| 88 | + return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(*category); |
| 89 | +} |
| 90 | + |
| 91 | +#undef MAX_STACK_LENGTH |
| 92 | + |
| 93 | +} // namespace |
| 94 | + |
| 95 | +// Builins::kIsTraceCategoryEnabled(category) : bool |
| 96 | +BUILTIN(IsTraceCategoryEnabled) { |
| 97 | + HandleScope scope(isolate); |
| 98 | + Handle<Object> category = args.atOrUndefined(isolate, 1); |
| 99 | + if (!category->IsString()) { |
| 100 | + THROW_NEW_ERROR_RETURN_FAILURE( |
| 101 | + isolate, NewTypeError(MessageTemplate::kTraceEventCategoryError)); |
| 102 | + } |
| 103 | + return isolate->heap()->ToBoolean( |
| 104 | + *GetCategoryGroupEnabled(isolate, Handle<String>::cast(category))); |
| 105 | +} |
| 106 | + |
| 107 | +// Builtins::kTrace(phase, category, name, id, data) : bool |
| 108 | +BUILTIN(Trace) { |
| 109 | + HandleScope handle_scope(isolate); |
| 110 | + |
| 111 | + Handle<Object> phase_arg = args.atOrUndefined(isolate, 1); |
| 112 | + Handle<Object> category = args.atOrUndefined(isolate, 2); |
| 113 | + Handle<Object> name_arg = args.atOrUndefined(isolate, 3); |
| 114 | + Handle<Object> id_arg = args.atOrUndefined(isolate, 4); |
| 115 | + Handle<Object> data_arg = args.atOrUndefined(isolate, 5); |
| 116 | + |
| 117 | + const uint8_t* category_group_enabled = |
| 118 | + GetCategoryGroupEnabled(isolate, Handle<String>::cast(category)); |
| 119 | + |
| 120 | + // Exit early if the category group is not enabled. |
| 121 | + if (!*category_group_enabled) { |
| 122 | + return ReadOnlyRoots(isolate).false_value(); |
| 123 | + } |
| 124 | + |
| 125 | + if (!phase_arg->IsNumber()) { |
| 126 | + THROW_NEW_ERROR_RETURN_FAILURE( |
| 127 | + isolate, NewTypeError(MessageTemplate::kTraceEventPhaseError)); |
| 128 | + } |
| 129 | + if (!category->IsString()) { |
| 130 | + THROW_NEW_ERROR_RETURN_FAILURE( |
| 131 | + isolate, NewTypeError(MessageTemplate::kTraceEventCategoryError)); |
| 132 | + } |
| 133 | + if (!name_arg->IsString()) { |
| 134 | + THROW_NEW_ERROR_RETURN_FAILURE( |
| 135 | + isolate, NewTypeError(MessageTemplate::kTraceEventNameError)); |
| 136 | + } |
| 137 | + |
| 138 | + uint32_t flags = TRACE_EVENT_FLAG_COPY; |
| 139 | + int32_t id = 0; |
| 140 | + if (!id_arg->IsNullOrUndefined(isolate)) { |
| 141 | + if (!id_arg->IsNumber()) { |
| 142 | + THROW_NEW_ERROR_RETURN_FAILURE( |
| 143 | + isolate, NewTypeError(MessageTemplate::kTraceEventIDError)); |
| 144 | + } |
| 145 | + flags |= TRACE_EVENT_FLAG_HAS_ID; |
| 146 | + id = DoubleToInt32(id_arg->Number()); |
| 147 | + } |
| 148 | + |
| 149 | + Handle<String> name_str = Handle<String>::cast(name_arg); |
| 150 | + if (name_str->length() == 0) { |
| 151 | + THROW_NEW_ERROR_RETURN_FAILURE( |
| 152 | + isolate, NewTypeError(MessageTemplate::kTraceEventNameLengthError)); |
| 153 | + } |
| 154 | + MaybeUtf8 name(isolate, name_str); |
| 155 | + |
| 156 | + // We support passing one additional trace event argument with the |
| 157 | + // name "data". Any JSON serializable value may be passed. |
| 158 | + static const char* arg_name = "data"; |
| 159 | + int32_t num_args = 0; |
| 160 | + uint8_t arg_type; |
| 161 | + uint64_t arg_value; |
| 162 | + |
| 163 | + if (!data_arg->IsUndefined(isolate)) { |
| 164 | + // Serializes the data argument as a JSON string, which is then |
| 165 | + // copied into an object. This eliminates duplicated code but |
| 166 | + // could have perf costs. It is also subject to all the same |
| 167 | + // limitations as JSON.stringify() as it relates to circular |
| 168 | + // references and value limitations (e.g. BigInt is not supported). |
| 169 | + JsonStringifier stringifier(isolate); |
| 170 | + Handle<Object> result; |
| 171 | + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 172 | + isolate, result, |
| 173 | + stringifier.Stringify(data_arg, isolate->factory()->undefined_value(), |
| 174 | + isolate->factory()->undefined_value())); |
| 175 | + std::unique_ptr<JsonTraceValue> traced_value; |
| 176 | + traced_value.reset( |
| 177 | + new JsonTraceValue(isolate, Handle<String>::cast(result))); |
| 178 | + tracing::SetTraceValue(std::move(traced_value), &arg_type, &arg_value); |
| 179 | + num_args++; |
| 180 | + } |
| 181 | + |
| 182 | + TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 183 | + static_cast<char>(DoubleToInt32(phase_arg->Number())), |
| 184 | + category_group_enabled, *name, tracing::kGlobalScope, id, tracing::kNoId, |
| 185 | + num_args, &arg_name, &arg_type, &arg_value, flags); |
| 186 | + |
| 187 | + return ReadOnlyRoots(isolate).true_value(); |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace internal |
| 191 | +} // namespace v8 |
0 commit comments