GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_trace_events.cc Lines: 73 77 94.8 %
Date: 2021-09-17 04:12:37 Branches: 19 34 55.9 %

Line Branch Exec Source
1
#include "base_object-inl.h"
2
#include "env-inl.h"
3
#include "memory_tracker-inl.h"
4
#include "node.h"
5
#include "node_external_reference.h"
6
#include "node_internals.h"
7
#include "node_v8_platform-inl.h"
8
#include "tracing/agent.h"
9
#include "util-inl.h"
10
11
#include <set>
12
#include <string>
13
14
namespace node {
15
16
class ExternalReferenceRegistry;
17
18
using v8::Array;
19
using v8::Context;
20
using v8::Function;
21
using v8::FunctionCallbackInfo;
22
using v8::FunctionTemplate;
23
using v8::Local;
24
using v8::NewStringType;
25
using v8::Object;
26
using v8::String;
27
using v8::Value;
28
29
class NodeCategorySet : public BaseObject {
30
 public:
31
  static void Initialize(Local<Object> target,
32
                  Local<Value> unused,
33
                  Local<Context> context,
34
                  void* priv);
35
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
36
  static void New(const FunctionCallbackInfo<Value>& args);
37
  static void Enable(const FunctionCallbackInfo<Value>& args);
38
  static void Disable(const FunctionCallbackInfo<Value>& args);
39
40
26
  const std::set<std::string>& GetCategories() const { return categories_; }
41
42
  void MemoryInfo(MemoryTracker* tracker) const override {
43
    tracker->TrackField("categories", categories_);
44
  }
45
46
  SET_MEMORY_INFO_NAME(NodeCategorySet)
47
  SET_SELF_SIZE(NodeCategorySet)
48
49
 private:
50
19
  NodeCategorySet(Environment* env,
51
                  Local<Object> wrap,
52
19
                  std::set<std::string>&& categories) :
53
19
        BaseObject(env, wrap), categories_(std::move(categories)) {
54
19
    MakeWeak();
55
19
  }
56
57
  bool enabled_ = false;
58
  const std::set<std::string> categories_;
59
};
60
61
19
void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) {
62
19
  Environment* env = Environment::GetCurrent(args);
63
19
  std::set<std::string> categories;
64
19
  CHECK(args[0]->IsArray());
65
19
  Local<Array> cats = args[0].As<Array>();
66
76
  for (size_t n = 0; n < cats->Length(); n++) {
67
    Local<Value> category;
68
38
    if (!cats->Get(env->context(), n).ToLocal(&category)) return;
69
19
    Utf8Value val(env->isolate(), category);
70
19
    if (!*val) return;
71
19
    categories.emplace(*val);
72
  }
73
19
  CHECK_NOT_NULL(GetTracingAgentWriter());
74
19
  new NodeCategorySet(env, args.This(), std::move(categories));
75
}
76
77
20
void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
78
  NodeCategorySet* category_set;
79
20
  ASSIGN_OR_RETURN_UNWRAP(&category_set, args.Holder());
80
20
  CHECK_NOT_NULL(category_set);
81
20
  const auto& categories = category_set->GetCategories();
82

20
  if (!category_set->enabled_ && !categories.empty()) {
83
    // Starts the Tracing Agent if it wasn't started already (e.g. through
84
    // a command line flag.)
85
20
    StartTracingAgent();
86
20
    GetTracingAgentWriter()->Enable(categories);
87
20
    category_set->enabled_ = true;
88
  }
89
}
90
91
6
void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
92
  NodeCategorySet* category_set;
93
6
  ASSIGN_OR_RETURN_UNWRAP(&category_set, args.Holder());
94
6
  CHECK_NOT_NULL(category_set);
95
6
  const auto& categories = category_set->GetCategories();
96

6
  if (category_set->enabled_ && !categories.empty()) {
97
6
    GetTracingAgentWriter()->Disable(categories);
98
6
    category_set->enabled_ = false;
99
  }
100
}
101
102
17
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
103
17
  Environment* env = Environment::GetCurrent(args);
104
  std::string categories =
105
34
      GetTracingAgentWriter()->agent()->GetEnabledCategories();
106
17
  if (!categories.empty()) {
107
42
    args.GetReturnValue().Set(
108
14
      String::NewFromUtf8(env->isolate(),
109
                          categories.c_str(),
110
                          NewStringType::kNormal,
111
14
                          categories.size()).ToLocalChecked());
112
  }
113
17
}
114
115
610
static void SetTraceCategoryStateUpdateHandler(
116
    const FunctionCallbackInfo<Value>& args) {
117
610
  Environment* env = Environment::GetCurrent(args);
118
610
  CHECK(args[0]->IsFunction());
119
1220
  env->set_trace_category_state_function(args[0].As<Function>());
120
610
}
121
122
610
void NodeCategorySet::Initialize(Local<Object> target,
123
                Local<Value> unused,
124
                Local<Context> context,
125
                void* priv) {
126
610
  Environment* env = Environment::GetCurrent(context);
127
128
610
  env->SetMethod(target, "getEnabledCategories", GetEnabledCategories);
129
610
  env->SetMethod(
130
      target, "setTraceCategoryStateUpdateHandler",
131
      SetTraceCategoryStateUpdateHandler);
132
133
  Local<FunctionTemplate> category_set =
134
610
      env->NewFunctionTemplate(NodeCategorySet::New);
135
1220
  category_set->InstanceTemplate()->SetInternalFieldCount(
136
      NodeCategorySet::kInternalFieldCount);
137
610
  category_set->Inherit(BaseObject::GetConstructorTemplate(env));
138
610
  env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable);
139
610
  env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
140
141
610
  env->SetConstructorFunction(target, "CategorySet", category_set);
142
143
  Local<String> isTraceCategoryEnabled =
144
610
      FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");
145
610
  Local<String> trace = FIXED_ONE_BYTE_STRING(env->isolate(), "trace");
146
147
  // Grab the trace and isTraceCategoryEnabled intrinsics from the binding
148
  // object and expose those to our binding layer.
149
610
  Local<Object> binding = context->GetExtrasBindingObject();
150
610
  target->Set(context, isTraceCategoryEnabled,
151
1830
              binding->Get(context, isTraceCategoryEnabled).ToLocalChecked())
152
                  .Check();
153
610
  target->Set(context, trace,
154
1220
              binding->Get(context, trace).ToLocalChecked()).Check();
155
610
}
156
157
4795
void NodeCategorySet::RegisterExternalReferences(
158
    ExternalReferenceRegistry* registry) {
159
4795
  registry->Register(GetEnabledCategories);
160
4795
  registry->Register(SetTraceCategoryStateUpdateHandler);
161
4795
  registry->Register(NodeCategorySet::New);
162
4795
  registry->Register(NodeCategorySet::Enable);
163
4795
  registry->Register(NodeCategorySet::Disable);
164
4795
}
165
166
}  // namespace node
167
168
4864
NODE_MODULE_CONTEXT_AWARE_INTERNAL(trace_events,
169
                                   node::NodeCategorySet::Initialize)
170
4795
NODE_MODULE_EXTERNAL_REFERENCE(
171
    trace_events, node::NodeCategorySet::RegisterExternalReferences)