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: 64 68 94.1 %
Date: 2019-05-05 22:32:45 Branches: 22 40 55.0 %

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

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

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