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: 24 64 37.5 %
Date: 2019-02-01 22:03:38 Branches: 1 40 2.5 %

Line Branch Exec Source
1
#include "base_object-inl.h"
2
#include "env.h"
3
#include "node.h"
4
#include "node_v8_platform-inl.h"
5
#include "tracing/agent.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
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
  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
  NodeCategorySet(Environment* env,
45
                  Local<Object> wrap,
46
                  std::set<std::string>&& categories) :
47
        BaseObject(env, wrap), categories_(std::move(categories)) {
48
    MakeWeak();
49
  }
50
51
  bool enabled_ = false;
52
  const std::set<std::string> categories_;
53
};
54
55
void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) {
56
  Environment* env = Environment::GetCurrent(args);
57
  std::set<std::string> categories;
58
  CHECK(args[0]->IsArray());
59
  Local<Array> cats = args[0].As<Array>();
60
  for (size_t n = 0; n < cats->Length(); n++) {
61
    Local<Value> category;
62
    if (!cats->Get(env->context(), n).ToLocal(&category)) return;
63
    Utf8Value val(env->isolate(), category);
64
    if (!*val) return;
65
    categories.emplace(*val);
66
  }
67
  CHECK_NOT_NULL(GetTracingAgentWriter());
68
  new NodeCategorySet(env, args.This(), std::move(categories));
69
}
70
71
void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
72
  NodeCategorySet* category_set;
73
  ASSIGN_OR_RETURN_UNWRAP(&category_set, args.Holder());
74
  CHECK_NOT_NULL(category_set);
75
  const auto& categories = category_set->GetCategories();
76
  if (!category_set->enabled_ && !categories.empty()) {
77
    GetTracingAgentWriter()->Enable(categories);
78
    category_set->enabled_ = true;
79
  }
80
}
81
82
void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
83
  NodeCategorySet* category_set;
84
  ASSIGN_OR_RETURN_UNWRAP(&category_set, args.Holder());
85
  CHECK_NOT_NULL(category_set);
86
  const auto& categories = category_set->GetCategories();
87
  if (category_set->enabled_ && !categories.empty()) {
88
    GetTracingAgentWriter()->Disable(categories);
89
    category_set->enabled_ = false;
90
  }
91
}
92
93
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
94
  Environment* env = Environment::GetCurrent(args);
95
  std::string categories =
96
      GetTracingAgentWriter()->agent()->GetEnabledCategories();
97
  if (!categories.empty()) {
98
    args.GetReturnValue().Set(
99
      String::NewFromUtf8(env->isolate(),
100
                          categories.c_str(),
101
                          NewStringType::kNormal,
102
                          categories.size()).ToLocalChecked());
103
  }
104
}
105
106
164
static void SetTraceCategoryStateUpdateHandler(
107
    const FunctionCallbackInfo<Value>& args) {
108
164
  Environment* env = Environment::GetCurrent(args);
109
328
  CHECK(args[0]->IsFunction());
110
328
  env->set_trace_category_state_function(args[0].As<Function>());
111
164
}
112
113
164
void NodeCategorySet::Initialize(Local<Object> target,
114
                Local<Value> unused,
115
                Local<Context> context,
116
                void* priv) {
117
164
  Environment* env = Environment::GetCurrent(context);
118
119
164
  env->SetMethod(target, "getEnabledCategories", GetEnabledCategories);
120
  env->SetMethod(
121
      target, "setTraceCategoryStateUpdateHandler",
122
164
      SetTraceCategoryStateUpdateHandler);
123
124
  Local<FunctionTemplate> category_set =
125
164
      env->NewFunctionTemplate(NodeCategorySet::New);
126
328
  category_set->InstanceTemplate()->SetInternalFieldCount(1);
127
164
  env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable);
128
164
  env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
129
130
  target->Set(env->context(),
131
              FIXED_ONE_BYTE_STRING(env->isolate(), "CategorySet"),
132
984
              category_set->GetFunction(env->context()).ToLocalChecked())
133
328
              .FromJust();
134
135
  Local<String> isTraceCategoryEnabled =
136
164
      FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled");
137
164
  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
164
  Local<Object> binding = context->GetExtrasBindingObject();
142
  target->Set(context, isTraceCategoryEnabled,
143
492
              binding->Get(context, isTraceCategoryEnabled).ToLocalChecked())
144
328
                  .FromJust();
145
  target->Set(context, trace,
146
492
              binding->Get(context, trace).ToLocalChecked()).FromJust();
147
148
  target->Set(context,
149
              FIXED_ONE_BYTE_STRING(env->isolate(), "traceCategoryState"),
150
656
              env->trace_category_state().GetJSArray()).FromJust();
151
164
}
152
153
}  // namespace node
154
155
164
NODE_MODULE_CONTEXT_AWARE_INTERNAL(trace_events,
156
                                   node::NodeCategorySet::Initialize)