@@ -13,7 +13,6 @@ using v8::Array;
13
13
using v8::Context;
14
14
using v8::FunctionCallbackInfo;
15
15
using v8::FunctionTemplate;
16
- using v8::Int32;
17
16
using v8::Local;
18
17
using v8::Object;
19
18
using v8::String;
@@ -99,137 +98,12 @@ void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
99
98
}
100
99
}
101
100
102
- // The tracing APIs require category groups to be pointers to long-lived
103
- // strings. Those strings are stored here.
104
- static std::unordered_set<std::string> category_groups;
105
- static Mutex category_groups_mutex;
106
-
107
- // Gets a pointer to the category-enabled flags for a tracing category group,
108
- // if tracing is enabled for it.
109
- static const uint8_t * GetCategoryGroupEnabled (const char * category_group) {
110
- CHECK_NOT_NULL (category_group);
111
- return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (category_group);
112
- }
113
-
114
- static const char * GetCategoryGroup (Environment* env,
115
- const Local<Value> category_value) {
116
- CHECK (category_value->IsString ());
117
-
118
- Utf8Value category (env->isolate (), category_value);
119
- Mutex::ScopedLock lock (category_groups_mutex);
120
- // If the value already exists in the set, insertion.first will point
121
- // to the existing value. Thus, this will maintain a long lived pointer
122
- // to the category c-string.
123
- auto insertion = category_groups.insert (category.out ());
124
-
125
- // The returned insertion is a pair whose first item is the object that was
126
- // inserted or that blocked the insertion and second item is a boolean
127
- // indicating whether it was inserted.
128
- return insertion.first ->c_str ();
129
- }
130
-
131
- static void Emit (const FunctionCallbackInfo<Value>& args) {
132
- Environment* env = Environment::GetCurrent (args);
133
- Local<Context> context = env->context ();
134
-
135
- // Args: [type, category, name, id, argName, argValue]
136
- CHECK_GE (args.Length (), 3 );
137
-
138
- // Check the category group first, to avoid doing more work if it's not
139
- // enabled.
140
- const char * category_group = GetCategoryGroup (env, args[1 ]);
141
- const uint8_t * category_group_enabled =
142
- GetCategoryGroupEnabled (category_group);
143
- if (*category_group_enabled == 0 ) return ;
144
-
145
- // get trace_event phase
146
- CHECK (args[0 ]->IsNumber ());
147
- char phase = static_cast <char >(args[0 ]->Int32Value (context).ToChecked ());
148
-
149
- // get trace_event name
150
- CHECK (args[2 ]->IsString ());
151
- Utf8Value name_value (env->isolate (), args[2 ]);
152
- const char * name = name_value.out ();
153
-
154
- // get trace_event id
155
- int64_t id = 0 ;
156
- bool has_id = false ;
157
- if (args.Length () >= 4 && !args[3 ]->IsUndefined () && !args[3 ]->IsNull ()) {
158
- has_id = true ;
159
- CHECK (args[3 ]->IsNumber ());
160
- id = args[3 ]->IntegerValue (context).ToChecked ();
161
- }
162
-
163
- // TODO(AndreasMadsen): String values are not supported.
164
- int32_t num_args = 0 ;
165
- const char * arg_names[2 ];
166
- uint8_t arg_types[2 ];
167
- uint64_t arg_values[2 ];
168
-
169
- // Create Utf8Value in the function scope to prevent deallocation.
170
- // The c-string will be copied by TRACE_EVENT_API_ADD_TRACE_EVENT at the end.
171
- Utf8Value arg1NameValue (env->isolate (), args[4 ]);
172
- Utf8Value arg2NameValue (env->isolate (), args[6 ]);
173
-
174
- if (args.Length () >= 6 &&
175
- (!args[4 ]->IsUndefined () || !args[5 ]->IsUndefined ())) {
176
- num_args = 1 ;
177
- arg_types[0 ] = TRACE_VALUE_TYPE_INT;
178
-
179
- CHECK (args[4 ]->IsString ());
180
- arg_names[0 ] = arg1NameValue.out ();
181
-
182
- CHECK (args[5 ]->IsNumber ());
183
- arg_values[0 ] = args[5 ]->IntegerValue (context).ToChecked ();
184
- }
185
-
186
- if (args.Length () >= 8 &&
187
- (!args[6 ]->IsUndefined () || !args[7 ]->IsUndefined ())) {
188
- num_args = 2 ;
189
- arg_types[1 ] = TRACE_VALUE_TYPE_INT;
190
-
191
- CHECK (args[6 ]->IsString ());
192
- arg_names[1 ] = arg2NameValue.out ();
193
-
194
- CHECK (args[7 ]->IsNumber ());
195
- arg_values[1 ] = args[7 ]->IntegerValue (context).ToChecked ();
196
- }
197
-
198
- // The TRACE_EVENT_FLAG_COPY flag indicates that the name and argument
199
- // name should be copied thus they don't need to long-lived pointers.
200
- // The category name still won't be copied and thus need to be a long-lived
201
- // pointer.
202
- uint32_t flags = TRACE_EVENT_FLAG_COPY;
203
- if (has_id) {
204
- flags |= TRACE_EVENT_FLAG_HAS_ID;
205
- }
206
-
207
- const char * scope = node::tracing::kGlobalScope ;
208
- uint64_t bind_id = node::tracing::kNoId ;
209
-
210
- TRACE_EVENT_API_ADD_TRACE_EVENT (
211
- phase, category_group_enabled, name, scope, id, bind_id,
212
- num_args, arg_names, arg_types, arg_values,
213
- flags);
214
- }
215
-
216
- static void CategoryGroupEnabled (const FunctionCallbackInfo<Value>& args) {
217
- Environment* env = Environment::GetCurrent (args);
218
-
219
- const char * category_group = GetCategoryGroup (env, args[0 ]);
220
- const uint8_t * category_group_enabled =
221
- GetCategoryGroupEnabled (category_group);
222
- args.GetReturnValue ().Set (*category_group_enabled > 0 );
223
- }
224
-
225
101
void Initialize (Local<Object> target,
226
102
Local<Value> unused,
227
103
Local<Context> context,
228
104
void * priv) {
229
105
Environment* env = Environment::GetCurrent (context);
230
106
231
- env->SetMethod (target, " emit" , Emit);
232
- env->SetMethod (target, " categoryGroupEnabled" , CategoryGroupEnabled);
233
107
env->SetMethod (target, " getEnabledCategories" , GetEnabledCategories);
234
108
235
109
Local<FunctionTemplate> category_set =
0 commit comments