• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "node_dtrace.h"
23 
24 #ifdef HAVE_DTRACE
25 #include "node_provider.h"
26 #elif HAVE_ETW
27 #include "node_win32_etw_provider-inl.h"
28 #else
29 #define NODE_HTTP_SERVER_REQUEST(arg0, arg1)
30 #define NODE_HTTP_SERVER_REQUEST_ENABLED() (0)
31 #define NODE_HTTP_SERVER_RESPONSE(arg0)
32 #define NODE_HTTP_SERVER_RESPONSE_ENABLED() (0)
33 #define NODE_HTTP_CLIENT_REQUEST(arg0, arg1)
34 #define NODE_HTTP_CLIENT_REQUEST_ENABLED() (0)
35 #define NODE_HTTP_CLIENT_RESPONSE(arg0)
36 #define NODE_HTTP_CLIENT_RESPONSE_ENABLED() (0)
37 #define NODE_NET_SERVER_CONNECTION(arg0)
38 #define NODE_NET_SERVER_CONNECTION_ENABLED() (0)
39 #define NODE_NET_STREAM_END(arg0)
40 #define NODE_NET_STREAM_END_ENABLED() (0)
41 #define NODE_GC_START(arg0, arg1, arg2)
42 #define NODE_GC_DONE(arg0, arg1, arg2)
43 #endif
44 
45 #include "env-inl.h"
46 #include "node_errors.h"
47 
48 #include <cstring>
49 
50 namespace node {
51 
52 using v8::Context;
53 using v8::FunctionCallbackInfo;
54 using v8::GCCallbackFlags;
55 using v8::GCType;
56 using v8::HandleScope;
57 using v8::Isolate;
58 using v8::Local;
59 using v8::Object;
60 using v8::Value;
61 
62 #define SLURP_STRING(obj, member, valp)                                    \
63   if (!(obj)->IsObject()) {                                                \
64     return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
65         "expected object for " #obj " to contain string member " #member); \
66   }                                                                        \
67   node::Utf8Value _##member(env->isolate(),                                \
68       obj->Get(env->context(),                                             \
69                OneByteString(env->isolate(), #member)).ToLocalChecked());  \
70   if ((*(const char **)valp = *_##member) == nullptr)                      \
71     *(const char **)valp = "<unknown>";
72 
73 #define SLURP_INT(obj, member, valp)                                           \
74   if (!(obj)->IsObject()) {                                                    \
75     return node::THROW_ERR_INVALID_ARG_TYPE(                                   \
76         env,                                                                   \
77         "expected object for " #obj " to contain integer member " #member);    \
78   }                                                                            \
79   *valp = obj->Get(env->context(),                                             \
80                    OneByteString(env->isolate(), #member)).ToLocalChecked()    \
81               ->Int32Value(env->context())                                     \
82               .FromJust();
83 
84 #define SLURP_OBJECT(obj, member, valp)                                    \
85   if (!(obj)->IsObject()) {                                                \
86     return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
87         "expected object for " #obj " to contain object member " #member); \
88   }                                                                        \
89   *valp = Local<Object>::Cast(obj->Get(env->context(),                     \
90       OneByteString(env->isolate(), #member)).ToLocalChecked());
91 
92 #define SLURP_CONNECTION(arg, conn)                                        \
93   if (!(arg)->IsObject()) {                                                \
94     return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
95         "expected argument " #arg " to be a connection object");           \
96   }                                                                        \
97   node_dtrace_connection_t conn;                                           \
98   Local<Object> _##conn = Local<Object>::Cast(arg);                        \
99   Local<Value> _handle =                                                   \
100       (_##conn)->Get(env->context(),                                       \
101                      FIXED_ONE_BYTE_STRING(env->isolate(), "_handle"))     \
102                      .ToLocalChecked();                                    \
103   if (_handle->IsObject()) {                                               \
104     SLURP_INT(_handle.As<Object>(), fd, &conn.fd);                         \
105   } else {                                                                 \
106     conn.fd = -1;                                                          \
107   }                                                                        \
108   SLURP_STRING(_##conn, remoteAddress, &conn.remote);                      \
109   SLURP_INT(_##conn, remotePort, &conn.port);                              \
110   SLURP_INT(_##conn, bufferSize, &conn.buffered);
111 
112 #define SLURP_CONNECTION_HTTP_CLIENT(arg, conn)                            \
113   if (!(arg)->IsObject()) {                                                \
114     return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
115         "expected argument " #arg " to be a connection object");           \
116   }                                                                        \
117   node_dtrace_connection_t conn;                                           \
118   Local<Object> _##conn = Local<Object>::Cast(arg);                        \
119   SLURP_INT(_##conn, fd, &conn.fd);                                        \
120   SLURP_STRING(_##conn, host, &conn.remote);                               \
121   SLURP_INT(_##conn, port, &conn.port);                                    \
122   SLURP_INT(_##conn, bufferSize, &conn.buffered);
123 
124 #define SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(arg0, arg1, conn)            \
125   if (!(arg0)->IsObject()) {                                               \
126     return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
127         "expected argument " #arg0 " to be a connection object");          \
128   }                                                                        \
129   if (!(arg1)->IsObject()) {                                               \
130     return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
131         "expected argument " #arg1 " to be a connection object");          \
132   }                                                                        \
133   node_dtrace_connection_t conn;                                           \
134   Local<Object> _##conn = Local<Object>::Cast(arg0);                       \
135   SLURP_INT(_##conn, fd, &conn.fd);                                        \
136   SLURP_INT(_##conn, bufferSize, &conn.buffered);                          \
137   _##conn = Local<Object>::Cast(arg1);                                     \
138   SLURP_STRING(_##conn, host, &conn.remote);                               \
139   SLURP_INT(_##conn, port, &conn.port);
140 
141 
DTRACE_NET_SERVER_CONNECTION(const FunctionCallbackInfo<Value> & args)142 void DTRACE_NET_SERVER_CONNECTION(const FunctionCallbackInfo<Value>& args) {
143   if (!NODE_NET_SERVER_CONNECTION_ENABLED())
144     return;
145   Environment* env = Environment::GetCurrent(args);
146   SLURP_CONNECTION(args[0], conn);
147   NODE_NET_SERVER_CONNECTION(&conn, conn.remote, conn.port, conn.fd);
148 }
149 
150 
DTRACE_NET_STREAM_END(const FunctionCallbackInfo<Value> & args)151 void DTRACE_NET_STREAM_END(const FunctionCallbackInfo<Value>& args) {
152   if (!NODE_NET_STREAM_END_ENABLED())
153     return;
154   Environment* env = Environment::GetCurrent(args);
155   SLURP_CONNECTION(args[0], conn);
156   NODE_NET_STREAM_END(&conn, conn.remote, conn.port, conn.fd);
157 }
158 
DTRACE_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value> & args)159 void DTRACE_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>& args) {
160   node_dtrace_http_server_request_t req;
161 
162   if (!NODE_HTTP_SERVER_REQUEST_ENABLED())
163     return;
164 
165   Environment* env = Environment::GetCurrent(args);
166   HandleScope scope(env->isolate());
167   Local<Object> arg0 = Local<Object>::Cast(args[0]);
168   Local<Object> headers;
169 
170   memset(&req, 0, sizeof(req));
171   req._un.version = 1;
172   SLURP_STRING(arg0, url, &req.url);
173   SLURP_STRING(arg0, method, &req.method);
174   SLURP_OBJECT(arg0, headers, &headers);
175 
176   if (!(headers)->IsObject()) {
177     return node::THROW_ERR_INVALID_ARG_TYPE(env,
178         "expected object for request to contain string member headers");
179   }
180 
181   Local<Value> strfwdfor = headers->Get(
182       env->context(), env->x_forwarded_string()).ToLocalChecked();
183   node::Utf8Value fwdfor(env->isolate(), strfwdfor);
184 
185   if (!strfwdfor->IsString() || (req.forwardedFor = *fwdfor) == nullptr)
186     req.forwardedFor = const_cast<char*>("");
187 
188   SLURP_CONNECTION(args[1], conn);
189   NODE_HTTP_SERVER_REQUEST(&req, &conn, conn.remote, conn.port, req.method, \
190                            req.url, conn.fd);
191 }
192 
193 
DTRACE_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value> & args)194 void DTRACE_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value>& args) {
195   if (!NODE_HTTP_SERVER_RESPONSE_ENABLED())
196     return;
197   Environment* env = Environment::GetCurrent(args);
198   SLURP_CONNECTION(args[0], conn);
199   NODE_HTTP_SERVER_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
200 }
201 
202 
DTRACE_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value> & args)203 void DTRACE_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>& args) {
204   node_dtrace_http_client_request_t req;
205   char* header;
206 
207   if (!NODE_HTTP_CLIENT_REQUEST_ENABLED())
208     return;
209 
210   Environment* env = Environment::GetCurrent(args);
211   HandleScope scope(env->isolate());
212 
213   /*
214    * For the method and URL, we're going to dig them out of the header.  This
215    * is not as efficient as it could be, but we would rather not force the
216    * caller here to retain their method and URL until the time at which
217    * DTRACE_HTTP_CLIENT_REQUEST can be called.
218    */
219   Local<Object> arg0 = Local<Object>::Cast(args[0]);
220   SLURP_STRING(arg0, _header, &header);
221 
222   req.method = header;
223 
224   while (*header != '\0' && *header != ' ')
225     header++;
226 
227   if (*header != '\0')
228     *header++ = '\0';
229 
230   req.url = header;
231 
232   while (*header != '\0' && *header != ' ')
233     header++;
234 
235   *header = '\0';
236 
237   SLURP_CONNECTION_HTTP_CLIENT(args[1], conn);
238   NODE_HTTP_CLIENT_REQUEST(&req, &conn, conn.remote, conn.port, req.method, \
239                            req.url, conn.fd);
240 }
241 
242 
DTRACE_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value> & args)243 void DTRACE_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>& args) {
244   if (!NODE_HTTP_CLIENT_RESPONSE_ENABLED())
245     return;
246   Environment* env = Environment::GetCurrent(args);
247   SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(args[0], args[1], conn);
248   NODE_HTTP_CLIENT_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
249 }
250 
dtrace_gc_start(Isolate * isolate,GCType type,GCCallbackFlags flags,void * data)251 void dtrace_gc_start(Isolate* isolate,
252                      GCType type,
253                      GCCallbackFlags flags,
254                      void* data) {
255   // Previous versions of this probe point only logged type and flags.
256   // That's why for reasons of backwards compatibility the isolate goes last.
257   NODE_GC_START(type, flags, isolate);
258 }
259 
dtrace_gc_done(Isolate * isolate,GCType type,GCCallbackFlags flags,void * data)260 void dtrace_gc_done(Isolate* isolate,
261                     GCType type,
262                     GCCallbackFlags flags,
263                     void* data) {
264   // Previous versions of this probe point only logged type and flags.
265   // That's why for reasons of backwards compatibility the isolate goes last.
266   NODE_GC_DONE(type, flags, isolate);
267 }
268 
269 
InitDTrace(Environment * env)270 void InitDTrace(Environment* env) {
271 #ifdef HAVE_ETW
272   // ETW is neither thread-safe nor does it clean up resources on exit,
273   // so we can use it only on the main thread.
274   if (env->is_main_thread()) {
275     init_etw();
276   }
277 #endif
278 
279   // We need to use the variant of GC callbacks that takes data to
280   // avoid running into DCHECKs when multiple Environments try to add
281   // the same callback to the same isolate multiple times.
282   env->isolate()->AddGCPrologueCallback(dtrace_gc_start, env);
283   env->isolate()->AddGCEpilogueCallback(dtrace_gc_done, env);
284   env->AddCleanupHook([](void* data) {
285     Environment* env = static_cast<Environment*>(data);
286     env->isolate()->RemoveGCPrologueCallback(dtrace_gc_start, env);
287     env->isolate()->RemoveGCEpilogueCallback(dtrace_gc_done, env);
288   }, env);
289 }
290 
InitializeDTrace(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)291 void InitializeDTrace(Local<Object> target,
292                       Local<Value> unused,
293                       Local<Context> context,
294                       void* priv) {
295   Environment* env = Environment::GetCurrent(context);
296 
297 #if defined HAVE_DTRACE || defined HAVE_ETW
298 # define NODE_PROBE(name) env->SetMethod(target, #name, name);
299   NODE_PROBE(DTRACE_NET_SERVER_CONNECTION)
300   NODE_PROBE(DTRACE_NET_STREAM_END)
301   NODE_PROBE(DTRACE_HTTP_SERVER_REQUEST)
302   NODE_PROBE(DTRACE_HTTP_SERVER_RESPONSE)
303   NODE_PROBE(DTRACE_HTTP_CLIENT_REQUEST)
304   NODE_PROBE(DTRACE_HTTP_CLIENT_RESPONSE)
305 # undef NODE_PROBE
306 #endif
307 }
308 
309 }  // namespace node
310 NODE_MODULE_CONTEXT_AWARE_INTERNAL(dtrace, node::InitializeDTrace)
311