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

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

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