1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/bindings/source_location.h"
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/tracing/protos/chrome_track_event.pbzero.h"
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
#include "third_party/blink/renderer/platform/bindings/thread_debugger.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding_macros.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/traced_value.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_proto.h"
#include "v8/include/v8-inspector-protocol.h"
namespace blink {
namespace {
String ToPlatformString(const v8_inspector::StringView& string) {
if (string.is8Bit()) {
// SAFETY: v8_inspector::StringView guarantees characters8() and length()
// are safe.
return String(
UNSAFE_BUFFERS(base::span(string.characters8(), string.length())));
}
// SAFETY: v8_inspector::StringView guarantees characters16() and length()
// are safe.
return String(UNSAFE_BUFFERS(base::span(
reinterpret_cast<const UChar*>(string.characters16()), string.length())));
}
String ToPlatformString(std::unique_ptr<v8_inspector::StringBuffer> buffer) {
if (!buffer)
return String();
return ToPlatformString(buffer->string());
}
} // namespace
// static
std::unique_ptr<SourceLocation> SourceLocation::CaptureWithFullStackTrace() {
std::unique_ptr<v8_inspector::V8StackTrace> stack_trace =
CaptureStackTraceInternal(true);
if (stack_trace && !stack_trace->isEmpty()) {
return CreateFromNonEmptyV8StackTraceInternal(std::move(stack_trace));
}
return std::make_unique<SourceLocation>(String(), String(), 0, 0, nullptr, 0);
}
// static
std::unique_ptr<v8_inspector::V8StackTrace>
SourceLocation::CaptureStackTraceInternal(bool full) {
v8::Isolate* isolate = v8::Isolate::TryGetCurrent();
ThreadDebugger* debugger = ThreadDebugger::From(isolate);
if (!debugger || !isolate->InContext())
return nullptr;
ScriptForbiddenScope::AllowUserAgentScript allow_scripting;
return debugger->GetV8Inspector()->captureStackTrace(full);
}
// static
std::unique_ptr<SourceLocation>
SourceLocation::CreateFromNonEmptyV8StackTraceInternal(
std::unique_ptr<v8_inspector::V8StackTrace> stack_trace) {
// Retrieve the data before passing the ownership to SourceLocation.
String url = ToPlatformString(stack_trace->topSourceURL());
String function = ToPlatformString(stack_trace->topFunctionName());
unsigned line_number = stack_trace->topLineNumber();
unsigned column_number = stack_trace->topColumnNumber();
int script_id = stack_trace->topScriptId();
return base::WrapUnique(
new SourceLocation(url, function, line_number, column_number,
std::move(stack_trace), script_id));
}
SourceLocation::SourceLocation(const String& url, int char_position)
: url_(url),
line_number_(0),
column_number_(0),
char_position_(char_position),
script_id_(0) {}
SourceLocation::SourceLocation(const String& url,
int char_position,
unsigned line_number,
unsigned column_number)
: url_(url),
line_number_(line_number),
column_number_(column_number),
char_position_(char_position),
script_id_(0) {}
SourceLocation::SourceLocation(
const String& url,
const String& function,
unsigned line_number,
unsigned column_number,
std::unique_ptr<v8_inspector::V8StackTrace> stack_trace,
int script_id)
: url_(url),
function_(function),
line_number_(line_number),
column_number_(column_number),
char_position_(-1),
stack_trace_(std::move(stack_trace)),
script_id_(script_id) {}
SourceLocation::~SourceLocation() = default;
std::unique_ptr<SourceLocation> SourceLocation::Clone() const {
return base::WrapUnique(new SourceLocation(
url_, function_, line_number_, column_number_,
stack_trace_ ? stack_trace_->clone() : nullptr, script_id_));
}
void SourceLocation::WriteIntoTrace(
perfetto::TracedProto<SourceLocation::Proto> proto) const {
if (!stack_trace_ || stack_trace_->isEmpty()) {
return;
}
proto->set_function_name(
ToPlatformString(stack_trace_->topFunctionName()).Utf8());
proto->set_script_id(stack_trace_->topScriptId());
proto->set_url(ToPlatformString(stack_trace_->topSourceURL()).Utf8());
proto->set_line_number(stack_trace_->topLineNumber());
proto->set_column_number(stack_trace_->topColumnNumber());
proto->set_stack_trace(ToString().Utf8());
// TODO(https://crbug.com/1396277): This should be a WriteIntoTrace function
// once v8 has support for perfetto tracing (which is currently missing for v8
// chromium).
for (const auto& frame : stack_trace_->frames()) {
auto& stack_trace_pb = *(proto->add_stack_frames());
stack_trace_pb.set_function_name(
ToPlatformString(frame.functionName).Utf8());
auto& script_location = *(stack_trace_pb.set_script_location());
script_location.set_source_url(ToPlatformString(frame.sourceURL).Utf8());
script_location.set_line_number(frame.lineNumber);
script_location.set_column_number(frame.columnNumber);
}
}
void SourceLocation::WriteIntoTrace(perfetto::TracedValue context) const {
if (!stack_trace_ || stack_trace_->isEmpty()) {
return;
}
// TODO(altimin): Add TracedValue support to v8::StringView and remove
// ToPlatformString calls.
auto array = std::move(context).WriteArray();
for (const auto& frame : stack_trace_->frames()) {
auto dict = array.AppendDictionary();
dict.Add("functionName", ToPlatformString(frame.functionName).Utf8());
dict.Add("scriptId", String::Number(frame.scriptId));
dict.Add("url", ToPlatformString(frame.sourceURL).Utf8());
dict.Add("lineNumber", frame.lineNumber);
dict.Add("columnNumber", frame.columnNumber);
}
}
void SourceLocation::ToTracedValue(TracedValue* value, const char* name) const {
if (!stack_trace_ || stack_trace_->isEmpty())
return;
value->BeginArray(name);
value->BeginDictionary();
value->SetString("functionName",
ToPlatformString(stack_trace_->topFunctionName()));
value->SetInteger("scriptId", stack_trace_->topScriptId());
value->SetString("url", ToPlatformString(stack_trace_->topSourceURL()));
value->SetInteger("lineNumber", stack_trace_->topLineNumber());
value->SetInteger("columnNumber", stack_trace_->topColumnNumber());
value->BeginArray("stackFrames");
for (const auto& frame : stack_trace_->frames()) {
value->BeginDictionary();
value->SetString("functionName", ToPlatformString(frame.functionName));
value->BeginDictionary("scriptLocation");
value->SetString("sourceURL", ToPlatformString(frame.sourceURL));
value->SetInteger("lineNumber", frame.lineNumber);
value->SetInteger("columnNumber", frame.columnNumber);
value->EndDictionary(/*scriptLocation*/);
value->EndDictionary();
}
value->EndArray(/*stackFrames*/);
value->EndDictionary();
value->EndArray();
}
String SourceLocation::ToString() const {
if (!stack_trace_)
return String();
return ToPlatformString(stack_trace_->toString());
}
std::unique_ptr<v8_inspector::protocol::Runtime::API::StackTrace>
SourceLocation::BuildInspectorObject() const {
return BuildInspectorObject(std::numeric_limits<int>::max());
}
std::unique_ptr<v8_inspector::protocol::Runtime::API::StackTrace>
SourceLocation::BuildInspectorObject(int max_async_depth) const {
return stack_trace_ ? stack_trace_->buildInspectorObject(max_async_depth)
: nullptr;
}
std::unique_ptr<SourceLocation> CaptureSourceLocation(const String& url,
unsigned line_number,
unsigned column_number) {
std::unique_ptr<v8_inspector::V8StackTrace> stack_trace =
SourceLocation::CaptureStackTraceInternal(false);
if (stack_trace && !stack_trace->isEmpty()) {
return SourceLocation::CreateFromNonEmptyV8StackTraceInternal(
std::move(stack_trace));
}
return std::make_unique<SourceLocation>(
url, String(), line_number, column_number, std::move(stack_trace));
}
std::unique_ptr<SourceLocation> CaptureSourceLocation() {
std::unique_ptr<v8_inspector::V8StackTrace> stack_trace =
SourceLocation::CaptureStackTraceInternal(false);
if (stack_trace && !stack_trace->isEmpty()) {
return SourceLocation::CreateFromNonEmptyV8StackTraceInternal(
std::move(stack_trace));
}
return std::make_unique<SourceLocation>(String(), String(), 0, 0,
std::move(stack_trace));
}
std::unique_ptr<SourceLocation> CaptureSourceLocation(
v8::Isolate* isolate,
v8::Local<v8::Function> function) {
if (!function.IsEmpty()) {
v8::Location location = function->GetScriptLocation();
return std::make_unique<SourceLocation>(
ToCoreStringWithUndefinedOrNullCheck(
isolate, function->GetScriptOrigin().ResourceName()),
ToCoreStringWithUndefinedOrNullCheck(isolate, function->GetName()),
location.GetLineNumber() + 1, location.GetColumnNumber() + 1, nullptr,
function->ScriptId());
}
return std::make_unique<SourceLocation>(String(), String(), 0, 0, nullptr, 0);
}
} // namespace blink
|