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-09-23 22:30:09 Branches: 22 40 55.0 %

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

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

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