-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathnode_trace_events.cc
More file actions
213 lines (183 loc) Β· 7.17 KB
/
node_trace_events.cc
File metadata and controls
213 lines (183 loc) Β· 7.17 KB
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
#include "base_object-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_v8_platform-inl.h"
#include "tracing/agent.h"
#include "util-inl.h"
#include <set>
#include <string>
namespace node {
class ExternalReferenceRegistry;
using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Uint8Array;
using v8::Value;
class NodeCategorySet : public BaseObject {
public:
static void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void New(const FunctionCallbackInfo<Value>& args);
static void Enable(const FunctionCallbackInfo<Value>& args);
static void Disable(const FunctionCallbackInfo<Value>& args);
const std::set<std::string>& GetCategories() const { return categories_; }
void MemoryInfo(MemoryTracker* tracker) const override {
tracker->TrackField("categories", categories_);
}
SET_MEMORY_INFO_NAME(NodeCategorySet)
SET_SELF_SIZE(NodeCategorySet)
private:
NodeCategorySet(Environment* env,
Local<Object> wrap,
std::set<std::string>&& categories) :
BaseObject(env, wrap), categories_(std::move(categories)) {
MakeWeak();
}
bool enabled_ = false;
const std::set<std::string> categories_;
};
void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::set<std::string> categories;
CHECK(args[0]->IsArray());
Local<Array> cats = args[0].As<Array>();
for (size_t n = 0; n < cats->Length(); n++) {
Local<Value> category;
if (!cats->Get(env->context(), n).ToLocal(&category)) return;
Utf8Value val(env->isolate(), category);
if (!*val) return;
categories.emplace(*val);
}
CHECK_NOT_NULL(GetTracingAgentWriter());
new NodeCategorySet(env, args.This(), std::move(categories));
}
void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
NodeCategorySet* category_set;
ASSIGN_OR_RETURN_UNWRAP(&category_set, args.This());
CHECK_NOT_NULL(category_set);
const auto& categories = category_set->GetCategories();
if (!category_set->enabled_ && !categories.empty()) {
// Starts the Tracing Agent if it wasn't started already (e.g. through
// a command line flag.)
StartTracingAgent();
GetTracingAgentWriter()->Enable(categories);
category_set->enabled_ = true;
}
}
void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
NodeCategorySet* category_set;
ASSIGN_OR_RETURN_UNWRAP(&category_set, args.This());
CHECK_NOT_NULL(category_set);
const auto& categories = category_set->GetCategories();
if (category_set->enabled_ && !categories.empty()) {
GetTracingAgentWriter()->Disable(categories);
category_set->enabled_ = false;
}
}
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::string categories =
GetTracingAgentWriter()->agent()->GetEnabledCategories();
Local<Value> ret;
if (!categories.empty() &&
ToV8Value(env->context(), categories, env->isolate()).ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
}
static void SetTraceCategoryStateUpdateHandler(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsFunction());
env->set_trace_category_state_function(args[0].As<Function>());
}
static void GetCategoryEnabledBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());
Isolate* isolate = args.GetIsolate();
node::Utf8Value category_name(isolate, args[0]);
const uint8_t* enabled_pointer =
TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_name.out());
uint8_t* enabled_pointer_cast = const_cast<uint8_t*>(enabled_pointer);
uint8_t size = sizeof(*enabled_pointer_cast);
#ifdef V8_ENABLE_SANDBOX
std::unique_ptr<ArrayBuffer::Allocator> allocator(
ArrayBuffer::Allocator::NewDefaultAllocator());
void* v8_data = allocator->Allocate(size);
CHECK(v8_data);
memcpy(v8_data, enabled_pointer_cast, size);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
v8_data,
size,
[](void* data, size_t length, void*) {
std::unique_ptr<ArrayBuffer::Allocator> allocator(
ArrayBuffer::Allocator::NewDefaultAllocator());
allocator->Free(data, length);
},
nullptr);
#else
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
enabled_pointer_cast, size, [](void*, size_t, void*) {}, nullptr);
#endif
auto ab = ArrayBuffer::New(isolate, std::move(bs));
v8::Local<Uint8Array> u8 = v8::Uint8Array::New(ab, 0, 1);
args.GetReturnValue().Set(u8);
}
void NodeCategorySet::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
SetMethod(context, target, "getEnabledCategories", GetEnabledCategories);
SetMethod(context,
target,
"setTraceCategoryStateUpdateHandler",
SetTraceCategoryStateUpdateHandler);
SetMethod(
context, target, "getCategoryEnabledBuffer", GetCategoryEnabledBuffer);
Local<FunctionTemplate> category_set =
NewFunctionTemplate(isolate, NodeCategorySet::New);
category_set->InstanceTemplate()->SetInternalFieldCount(
NodeCategorySet::kInternalFieldCount);
SetProtoMethod(isolate, category_set, "enable", NodeCategorySet::Enable);
SetProtoMethod(isolate, category_set, "disable", NodeCategorySet::Disable);
SetConstructorFunction(context, target, "CategorySet", category_set);
Local<String> isTraceCategoryEnabled =
FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");
Local<String> trace = FIXED_ONE_BYTE_STRING(env->isolate(), "trace");
// Grab the trace and isTraceCategoryEnabled intrinsics from the binding
// object and expose those to our binding layer.
Local<Object> binding = context->GetExtrasBindingObject();
target->Set(context, isTraceCategoryEnabled,
binding->Get(context, isTraceCategoryEnabled).ToLocalChecked())
.Check();
target->Set(context, trace,
binding->Get(context, trace).ToLocalChecked()).Check();
}
void NodeCategorySet::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(GetEnabledCategories);
registry->Register(SetTraceCategoryStateUpdateHandler);
registry->Register(GetCategoryEnabledBuffer);
registry->Register(NodeCategorySet::New);
registry->Register(NodeCategorySet::Enable);
registry->Register(NodeCategorySet::Disable);
}
} // namespace node
NODE_BINDING_CONTEXT_AWARE_INTERNAL(trace_events,
node::NodeCategorySet::Initialize)
NODE_BINDING_EXTERNAL_REFERENCE(
trace_events, node::NodeCategorySet::RegisterExternalReferences)