GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_trace_events.cc Lines: 64 68 94.1 %
Date: 2019-01-07 12:15:22 Branches: 22 40 55.0 %

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

16
  if (!category_set->enabled_ && !categories.empty()) {
77
16
    GetTracingAgentWriter()->Enable(categories);
78
16
    category_set->enabled_ = true;
79
  }
80
}
81
82
4
void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
83
  NodeCategorySet* category_set;
84
8
  ASSIGN_OR_RETURN_UNWRAP(&category_set, args.Holder());
85
4
  CHECK_NOT_NULL(category_set);
86
4
  const auto& categories = category_set->GetCategories();
87

4
  if (category_set->enabled_ && !categories.empty()) {
88
4
    GetTracingAgentWriter()->Disable(categories);
89
4
    category_set->enabled_ = false;
90
  }
91
}
92
93
12
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
94
12
  Environment* env = Environment::GetCurrent(args);
95
  std::string categories =
96
12
      GetTracingAgentWriter()->agent()->GetEnabledCategories();
97
12
  if (!categories.empty()) {
98
    args.GetReturnValue().Set(
99
      String::NewFromUtf8(env->isolate(),
100
                          categories.c_str(),
101
                          NewStringType::kNormal,
102
27
                          categories.size()).ToLocalChecked());
103
12
  }
104
12
}
105
106
3671
static void SetTraceCategoryStateUpdateHandler(
107
    const FunctionCallbackInfo<Value>& args) {
108
3671
  Environment* env = Environment::GetCurrent(args);
109
7342
  CHECK(args[0]->IsFunction());
110
7342
  env->set_trace_category_state_function(args[0].As<Function>());
111
3671
}
112
113
3671
void NodeCategorySet::Initialize(Local<Object> target,
114
                Local<Value> unused,
115
                Local<Context> context,
116
                void* priv) {
117
3671
  Environment* env = Environment::GetCurrent(context);
118
119
3671
  env->SetMethod(target, "getEnabledCategories", GetEnabledCategories);
120
  env->SetMethod(
121
      target, "setTraceCategoryStateUpdateHandler",
122
3671
      SetTraceCategoryStateUpdateHandler);
123
124
  Local<FunctionTemplate> category_set =
125
3671
      env->NewFunctionTemplate(NodeCategorySet::New);
126
7342
  category_set->InstanceTemplate()->SetInternalFieldCount(1);
127
3671
  env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable);
128
3671
  env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
129
130
  target->Set(env->context(),
131
              FIXED_ONE_BYTE_STRING(env->isolate(), "CategorySet"),
132
22026
              category_set->GetFunction(env->context()).ToLocalChecked())
133
7342
              .FromJust();
134
135
  Local<String> isTraceCategoryEnabled =
136
3671
      FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");
137
3671
  Local<String> trace = FIXED_ONE_BYTE_STRING(env->isolate(), "trace");
138
139
  // Grab the trace and isTraceCategoryEnabled intrinsics from the binding
140
  // object and expose those to our binding layer.
141
3671
  Local<Object> binding = context->GetExtrasBindingObject();
142
  target->Set(context, isTraceCategoryEnabled,
143
11013
              binding->Get(context, isTraceCategoryEnabled).ToLocalChecked())
144
7342
                  .FromJust();
145
  target->Set(context, trace,
146
11013
              binding->Get(context, trace).ToLocalChecked()).FromJust();
147
148
  target->Set(context,
149
              FIXED_ONE_BYTE_STRING(env->isolate(), "traceCategoryState"),
150
14684
              env->trace_category_state().GetJSArray()).FromJust();
151
3671
}
152
153
}  // namespace node
154
155
3596
NODE_MODULE_CONTEXT_AWARE_INTERNAL(trace_events,
156
                                   node::NodeCategorySet::Initialize)