5 #include "include/libplatform/v8-tracing.h" 7 #include "base/trace_event/common/trace_event_common.h" 8 #include "include/v8-platform.h" 9 #include "src/base/platform/platform.h" 10 #include "src/base/platform/time.h" 18 V8_INLINE
static size_t GetAllocLength(
const char* str) {
19 return str ? strlen(str) + 1 : 0;
24 V8_INLINE
static void CopyTraceObjectParameter(
char** buffer,
25 const char** member) {
27 size_t length = strlen(*member) + 1;
28 strncpy(*buffer, *member, length);
34 void TraceObject::Initialize(
35 char phase,
const uint8_t* category_enabled_flag,
const char* name,
36 const char* scope, uint64_t
id, uint64_t bind_id,
int num_args,
37 const char** arg_names,
const uint8_t* arg_types,
38 const uint64_t* arg_values,
39 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
41 pid_ = base::OS::GetCurrentProcessId();
42 tid_ = base::OS::GetCurrentThreadId();
44 category_enabled_flag_ = category_enabled_flag;
56 num_args_ = (num_args > kTraceMaxNumArgs) ? kTraceMaxNumArgs : num_args;
57 for (
int i = 0;
i < num_args_; ++
i) {
58 arg_names_[
i] = arg_names[
i];
59 arg_values_[
i].as_uint = arg_values[
i];
60 arg_types_[
i] = arg_types[
i];
61 if (arg_types[
i] == TRACE_VALUE_TYPE_CONVERTABLE)
62 arg_convertables_[
i] = std::move(arg_convertables[
i]);
65 bool copy = !!(flags & TRACE_EVENT_FLAG_COPY);
67 size_t alloc_size = 0;
69 alloc_size += GetAllocLength(name) + GetAllocLength(scope);
70 for (
int i = 0;
i < num_args_; ++
i) {
71 alloc_size += GetAllocLength(arg_names_[
i]);
72 if (arg_types_[
i] == TRACE_VALUE_TYPE_STRING)
73 arg_types_[
i] = TRACE_VALUE_TYPE_COPY_STRING;
77 bool arg_is_copy[kTraceMaxNumArgs];
78 for (
int i = 0;
i < num_args_; ++
i) {
80 arg_is_copy[
i] = (arg_types_[
i] == TRACE_VALUE_TYPE_COPY_STRING);
81 if (arg_is_copy[
i]) alloc_size += GetAllocLength(arg_values_[
i].as_string);
87 delete[] parameter_copy_storage_;
88 char* ptr = parameter_copy_storage_ =
new char[alloc_size];
90 CopyTraceObjectParameter(&ptr, &name_);
91 CopyTraceObjectParameter(&ptr, &scope_);
92 for (
int i = 0;
i < num_args_; ++
i) {
93 CopyTraceObjectParameter(&ptr, &arg_names_[
i]);
96 for (
int i = 0;
i < num_args_; ++
i) {
98 CopyTraceObjectParameter(&ptr, &arg_values_[
i].as_string);
104 TraceObject::~TraceObject() {
delete[] parameter_copy_storage_; }
106 void TraceObject::UpdateDuration(
int64_t timestamp,
int64_t cpu_timestamp) {
107 duration_ = timestamp - ts_;
108 cpu_duration_ = cpu_timestamp - tts_;
111 void TraceObject::InitializeForTesting(
112 char phase,
const uint8_t* category_enabled_flag,
const char* name,
113 const char* scope, uint64_t
id, uint64_t bind_id,
int num_args,
114 const char** arg_names,
const uint8_t* arg_types,
115 const uint64_t* arg_values,
116 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
118 uint64_t duration, uint64_t cpu_duration) {
122 category_enabled_flag_ = category_enabled_flag;
127 num_args_ = num_args;
131 duration_ = duration;
132 cpu_duration_ = cpu_duration;