GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_trace_events.cc Lines: 76 80 95.0 %
Date: 2020-07-19 22:14:24 Branches: 23 42 54.8 %

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
38
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
38
  std::set<std::string> categories;
64
38
  CHECK(args[0]->IsArray());
65
38
  Local<Array> cats = args[0].As<Array>();
66
76
  for (size_t n = 0; n < cats->Length(); n++) {
67
    Local<Value> category;
68
57
    if (!cats->Get(env->context(), n).ToLocal(&category)) return;
69
38
    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
22
    args.GetReturnValue().Set(
108
22
      String::NewFromUtf8(env->isolate(),
109
                          categories.c_str(),
110
                          NewStringType::kNormal,
111
11
                          categories.size()).ToLocalChecked());
112
  }
113
17
}
114
115
384
static void SetTraceCategoryStateUpdateHandler(
116
    const FunctionCallbackInfo<Value>& args) {
117
384
  Environment* env = Environment::GetCurrent(args);
118
768
  CHECK(args[0]->IsFunction());
119
768
  env->set_trace_category_state_function(args[0].As<Function>());
120
384
}
121
122
384
void NodeCategorySet::Initialize(Local<Object> target,
123
                Local<Value> unused,
124
                Local<Context> context,
125
                void* priv) {
126
384
  Environment* env = Environment::GetCurrent(context);
127
128
384
  env->SetMethod(target, "getEnabledCategories", GetEnabledCategories);
129
  env->SetMethod(
130
      target, "setTraceCategoryStateUpdateHandler",
131
384
      SetTraceCategoryStateUpdateHandler);
132
133
  Local<FunctionTemplate> category_set =
134
384
      env->NewFunctionTemplate(NodeCategorySet::New);
135
1152
  category_set->InstanceTemplate()->SetInternalFieldCount(
136
384
      NodeCategorySet::kInternalFieldCount);
137
768
  category_set->Inherit(BaseObject::GetConstructorTemplate(env));
138
384
  env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable);
139
384
  env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
140
141
768
  target->Set(env->context(),
142
              FIXED_ONE_BYTE_STRING(env->isolate(), "CategorySet"),
143
2304
              category_set->GetFunction(env->context()).ToLocalChecked())
144
              .Check();
145
146
  Local<String> isTraceCategoryEnabled =
147
384
      FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");
148
384
  Local<String> trace = FIXED_ONE_BYTE_STRING(env->isolate(), "trace");
149
150
  // Grab the trace and isTraceCategoryEnabled intrinsics from the binding
151
  // object and expose those to our binding layer.
152
384
  Local<Object> binding = context->GetExtrasBindingObject();
153
768
  target->Set(context, isTraceCategoryEnabled,
154
1152
              binding->Get(context, isTraceCategoryEnabled).ToLocalChecked())
155
                  .Check();
156
768
  target->Set(context, trace,
157
1152
              binding->Get(context, trace).ToLocalChecked()).Check();
158
384
}
159
160
4920
void NodeCategorySet::RegisterExternalReferences(
161
    ExternalReferenceRegistry* registry) {
162
4920
  registry->Register(GetEnabledCategories);
163
4920
  registry->Register(SetTraceCategoryStateUpdateHandler);
164
4920
  registry->Register(NodeCategorySet::New);
165
4920
  registry->Register(NodeCategorySet::Enable);
166
4920
  registry->Register(NodeCategorySet::Disable);
167
4920
}
168
169
}  // namespace node
170
171
4989
NODE_MODULE_CONTEXT_AWARE_INTERNAL(trace_events,
172
                                   node::NodeCategorySet::Initialize)
173

19890
NODE_MODULE_EXTERNAL_REFERENCE(
174
    trace_events, node::NodeCategorySet::RegisterExternalReferences)