2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-17 21:00:11 +01:00
|
|
|
#include "node_dtrace.h"
|
2014-04-10 02:33:33 +02:00
|
|
|
|
2011-01-25 02:50:10 +01:00
|
|
|
#ifdef HAVE_DTRACE
|
|
|
|
#include "node_provider.h"
|
2012-06-12 01:23:17 +02:00
|
|
|
#elif HAVE_ETW
|
|
|
|
#include "node_win32_etw_provider-inl.h"
|
2011-01-25 02:50:10 +01:00
|
|
|
#else
|
|
|
|
#define NODE_HTTP_SERVER_REQUEST(arg0, arg1)
|
|
|
|
#define NODE_HTTP_SERVER_REQUEST_ENABLED() (0)
|
|
|
|
#define NODE_HTTP_SERVER_RESPONSE(arg0)
|
|
|
|
#define NODE_HTTP_SERVER_RESPONSE_ENABLED() (0)
|
2011-02-10 03:50:26 +01:00
|
|
|
#define NODE_HTTP_CLIENT_REQUEST(arg0, arg1)
|
|
|
|
#define NODE_HTTP_CLIENT_REQUEST_ENABLED() (0)
|
|
|
|
#define NODE_HTTP_CLIENT_RESPONSE(arg0)
|
|
|
|
#define NODE_HTTP_CLIENT_RESPONSE_ENABLED() (0)
|
2011-01-25 02:50:10 +01:00
|
|
|
#define NODE_NET_SERVER_CONNECTION(arg0)
|
|
|
|
#define NODE_NET_SERVER_CONNECTION_ENABLED() (0)
|
|
|
|
#define NODE_NET_STREAM_END(arg0)
|
|
|
|
#define NODE_NET_STREAM_END_ENABLED() (0)
|
2014-03-15 07:15:11 +01:00
|
|
|
#define NODE_GC_START(arg0, arg1, arg2)
|
|
|
|
#define NODE_GC_DONE(arg0, arg1, arg2)
|
2011-01-25 02:50:10 +01:00
|
|
|
#endif
|
|
|
|
|
2019-05-09 21:52:38 +02:00
|
|
|
#include "env-inl.h"
|
2018-04-16 16:58:19 +02:00
|
|
|
#include "node_errors.h"
|
2014-11-17 21:00:11 +01:00
|
|
|
|
2019-02-17 03:53:47 +01:00
|
|
|
#include <cstring>
|
2014-11-17 21:00:11 +01:00
|
|
|
|
2011-01-25 02:50:10 +01:00
|
|
|
namespace node {
|
|
|
|
|
2019-03-09 09:28:04 +01:00
|
|
|
using v8::Context;
|
2013-07-03 04:23:44 +02:00
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::GCCallbackFlags;
|
|
|
|
using v8::GCType;
|
|
|
|
using v8::HandleScope;
|
2014-03-15 07:15:11 +01:00
|
|
|
using v8::Isolate;
|
2013-07-03 04:23:44 +02:00
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2011-01-25 02:50:10 +01:00
|
|
|
|
2017-10-05 13:45:26 +02:00
|
|
|
#define SLURP_STRING(obj, member, valp) \
|
|
|
|
if (!(obj)->IsObject()) { \
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
|
2013-07-03 04:23:44 +02:00
|
|
|
"expected object for " #obj " to contain string member " #member); \
|
2017-10-05 13:45:26 +02:00
|
|
|
} \
|
|
|
|
node::Utf8Value _##member(env->isolate(), \
|
2018-11-08 07:22:13 +01:00
|
|
|
obj->Get(env->context(), \
|
|
|
|
OneByteString(env->isolate(), #member)).ToLocalChecked()); \
|
2017-10-05 13:45:26 +02:00
|
|
|
if ((*(const char **)valp = *_##member) == nullptr) \
|
2011-01-25 02:50:10 +01:00
|
|
|
*(const char **)valp = "<unknown>";
|
|
|
|
|
2018-09-02 17:49:11 +02:00
|
|
|
#define SLURP_INT(obj, member, valp) \
|
|
|
|
if (!(obj)->IsObject()) { \
|
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE( \
|
|
|
|
env, \
|
|
|
|
"expected object for " #obj " to contain integer member " #member); \
|
|
|
|
} \
|
2018-11-08 07:22:13 +01:00
|
|
|
*valp = obj->Get(env->context(), \
|
|
|
|
OneByteString(env->isolate(), #member)).ToLocalChecked() \
|
2018-09-02 17:49:11 +02:00
|
|
|
->Int32Value(env->context()) \
|
|
|
|
.FromJust();
|
2011-01-25 02:50:10 +01:00
|
|
|
|
2017-10-05 13:45:26 +02:00
|
|
|
#define SLURP_OBJECT(obj, member, valp) \
|
|
|
|
if (!(obj)->IsObject()) { \
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
|
|
|
|
"expected object for " #obj " to contain object member " #member); \
|
2017-10-05 13:45:26 +02:00
|
|
|
} \
|
2018-11-08 07:22:13 +01:00
|
|
|
*valp = Local<Object>::Cast(obj->Get(env->context(), \
|
|
|
|
OneByteString(env->isolate(), #member)).ToLocalChecked());
|
2011-02-10 03:50:26 +01:00
|
|
|
|
2017-10-05 13:45:26 +02:00
|
|
|
#define SLURP_CONNECTION(arg, conn) \
|
|
|
|
if (!(arg)->IsObject()) { \
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
|
|
|
|
"expected argument " #arg " to be a connection object"); \
|
2017-10-05 13:45:26 +02:00
|
|
|
} \
|
|
|
|
node_dtrace_connection_t conn; \
|
|
|
|
Local<Object> _##conn = Local<Object>::Cast(arg); \
|
|
|
|
Local<Value> _handle = \
|
2018-11-08 07:22:13 +01:00
|
|
|
(_##conn)->Get(env->context(), \
|
|
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "_handle")) \
|
|
|
|
.ToLocalChecked(); \
|
2017-10-05 13:45:26 +02:00
|
|
|
if (_handle->IsObject()) { \
|
|
|
|
SLURP_INT(_handle.As<Object>(), fd, &conn.fd); \
|
|
|
|
} else { \
|
|
|
|
conn.fd = -1; \
|
|
|
|
} \
|
|
|
|
SLURP_STRING(_##conn, remoteAddress, &conn.remote); \
|
|
|
|
SLURP_INT(_##conn, remotePort, &conn.port); \
|
2011-02-10 03:50:26 +01:00
|
|
|
SLURP_INT(_##conn, bufferSize, &conn.buffered);
|
|
|
|
|
2017-10-05 13:45:26 +02:00
|
|
|
#define SLURP_CONNECTION_HTTP_CLIENT(arg, conn) \
|
|
|
|
if (!(arg)->IsObject()) { \
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
|
|
|
|
"expected argument " #arg " to be a connection object"); \
|
2017-10-05 13:45:26 +02:00
|
|
|
} \
|
|
|
|
node_dtrace_connection_t conn; \
|
|
|
|
Local<Object> _##conn = Local<Object>::Cast(arg); \
|
|
|
|
SLURP_INT(_##conn, fd, &conn.fd); \
|
|
|
|
SLURP_STRING(_##conn, host, &conn.remote); \
|
|
|
|
SLURP_INT(_##conn, port, &conn.port); \
|
2011-02-10 03:50:26 +01:00
|
|
|
SLURP_INT(_##conn, bufferSize, &conn.buffered);
|
|
|
|
|
2017-10-05 13:45:26 +02:00
|
|
|
#define SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(arg0, arg1, conn) \
|
|
|
|
if (!(arg0)->IsObject()) { \
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
|
|
|
|
"expected argument " #arg0 " to be a connection object"); \
|
2017-10-05 13:45:26 +02:00
|
|
|
} \
|
|
|
|
if (!(arg1)->IsObject()) { \
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
|
|
|
|
"expected argument " #arg1 " to be a connection object"); \
|
2017-10-05 13:45:26 +02:00
|
|
|
} \
|
|
|
|
node_dtrace_connection_t conn; \
|
|
|
|
Local<Object> _##conn = Local<Object>::Cast(arg0); \
|
|
|
|
SLURP_INT(_##conn, fd, &conn.fd); \
|
|
|
|
SLURP_INT(_##conn, bufferSize, &conn.buffered); \
|
|
|
|
_##conn = Local<Object>::Cast(arg1); \
|
|
|
|
SLURP_STRING(_##conn, host, &conn.remote); \
|
2011-02-10 03:50:26 +01:00
|
|
|
SLURP_INT(_##conn, port, &conn.port);
|
|
|
|
|
2011-01-25 02:50:10 +01:00
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
void DTRACE_NET_SERVER_CONNECTION(const FunctionCallbackInfo<Value>& args) {
|
2011-01-25 02:50:10 +01:00
|
|
|
if (!NODE_NET_SERVER_CONNECTION_ENABLED())
|
2013-07-03 04:23:44 +02:00
|
|
|
return;
|
2014-10-13 15:19:55 +02:00
|
|
|
Environment* env = Environment::GetCurrent(args);
|
2011-01-25 02:50:10 +01:00
|
|
|
SLURP_CONNECTION(args[0], conn);
|
2013-03-29 00:51:52 +01:00
|
|
|
NODE_NET_SERVER_CONNECTION(&conn, conn.remote, conn.port, conn.fd);
|
2011-01-25 02:50:10 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
|
|
|
|
void DTRACE_NET_STREAM_END(const FunctionCallbackInfo<Value>& args) {
|
2011-01-25 02:50:10 +01:00
|
|
|
if (!NODE_NET_STREAM_END_ENABLED())
|
2013-07-03 04:23:44 +02:00
|
|
|
return;
|
2014-10-13 15:19:55 +02:00
|
|
|
Environment* env = Environment::GetCurrent(args);
|
2011-01-25 02:50:10 +01:00
|
|
|
SLURP_CONNECTION(args[0], conn);
|
2013-03-29 00:51:52 +01:00
|
|
|
NODE_NET_STREAM_END(&conn, conn.remote, conn.port, conn.fd);
|
2011-01-25 02:50:10 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
void DTRACE_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>& args) {
|
2011-06-04 16:04:38 +02:00
|
|
|
node_dtrace_http_server_request_t req;
|
2011-01-25 02:50:10 +01:00
|
|
|
|
|
|
|
if (!NODE_HTTP_SERVER_REQUEST_ENABLED())
|
2013-07-03 04:23:44 +02:00
|
|
|
return;
|
2011-01-25 02:50:10 +01:00
|
|
|
|
2014-10-13 15:19:55 +02:00
|
|
|
Environment* env = Environment::GetCurrent(args);
|
2014-02-21 14:02:42 +01:00
|
|
|
HandleScope scope(env->isolate());
|
2011-01-25 02:50:10 +01:00
|
|
|
Local<Object> arg0 = Local<Object>::Cast(args[0]);
|
2011-06-04 16:04:38 +02:00
|
|
|
Local<Object> headers;
|
2011-01-25 02:50:10 +01:00
|
|
|
|
2011-06-08 03:57:15 +02:00
|
|
|
memset(&req, 0, sizeof(req));
|
2011-06-04 16:04:38 +02:00
|
|
|
req._un.version = 1;
|
2011-01-25 02:50:10 +01:00
|
|
|
SLURP_STRING(arg0, url, &req.url);
|
|
|
|
SLURP_STRING(arg0, method, &req.method);
|
2011-06-04 16:04:38 +02:00
|
|
|
SLURP_OBJECT(arg0, headers, &headers);
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
if (!(headers)->IsObject()) {
|
2018-04-16 16:58:19 +02:00
|
|
|
return node::THROW_ERR_INVALID_ARG_TYPE(env,
|
|
|
|
"expected object for request to contain string member headers");
|
2013-07-03 04:23:44 +02:00
|
|
|
}
|
2011-06-04 16:04:38 +02:00
|
|
|
|
2018-11-08 07:22:13 +01:00
|
|
|
Local<Value> strfwdfor = headers->Get(
|
|
|
|
env->context(), env->x_forwarded_string()).ToLocalChecked();
|
2015-01-07 23:13:35 +01:00
|
|
|
node::Utf8Value fwdfor(env->isolate(), strfwdfor);
|
2011-06-04 16:04:38 +02:00
|
|
|
|
2014-10-22 03:29:32 +02:00
|
|
|
if (!strfwdfor->IsString() || (req.forwardedFor = *fwdfor) == nullptr)
|
2011-06-08 03:57:15 +02:00
|
|
|
req.forwardedFor = const_cast<char*>("");
|
2011-06-04 16:04:38 +02:00
|
|
|
|
2011-01-25 02:50:10 +01:00
|
|
|
SLURP_CONNECTION(args[1], conn);
|
2013-03-28 21:52:43 +01:00
|
|
|
NODE_HTTP_SERVER_REQUEST(&req, &conn, conn.remote, conn.port, req.method, \
|
2013-03-29 00:51:52 +01:00
|
|
|
req.url, conn.fd);
|
2011-01-25 02:50:10 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
|
|
|
|
void DTRACE_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value>& args) {
|
2011-01-25 02:50:10 +01:00
|
|
|
if (!NODE_HTTP_SERVER_RESPONSE_ENABLED())
|
2013-07-03 04:23:44 +02:00
|
|
|
return;
|
2014-10-13 15:19:55 +02:00
|
|
|
Environment* env = Environment::GetCurrent(args);
|
2011-01-25 02:50:10 +01:00
|
|
|
SLURP_CONNECTION(args[0], conn);
|
2013-03-29 00:51:52 +01:00
|
|
|
NODE_HTTP_SERVER_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
|
2011-01-25 02:50:10 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
|
|
|
|
void DTRACE_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>& args) {
|
2011-06-04 16:04:38 +02:00
|
|
|
node_dtrace_http_client_request_t req;
|
2018-05-16 11:50:48 +02:00
|
|
|
char* header;
|
2011-02-10 03:50:26 +01:00
|
|
|
|
|
|
|
if (!NODE_HTTP_CLIENT_REQUEST_ENABLED())
|
2013-07-03 04:23:44 +02:00
|
|
|
return;
|
2011-02-10 03:50:26 +01:00
|
|
|
|
2014-10-13 15:19:55 +02:00
|
|
|
Environment* env = Environment::GetCurrent(args);
|
2014-02-21 14:02:42 +01:00
|
|
|
HandleScope scope(env->isolate());
|
2011-02-10 03:50:26 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For the method and URL, we're going to dig them out of the header. This
|
|
|
|
* is not as efficient as it could be, but we would rather not force the
|
|
|
|
* caller here to retain their method and URL until the time at which
|
|
|
|
* DTRACE_HTTP_CLIENT_REQUEST can be called.
|
|
|
|
*/
|
|
|
|
Local<Object> arg0 = Local<Object>::Cast(args[0]);
|
|
|
|
SLURP_STRING(arg0, _header, &header);
|
|
|
|
|
|
|
|
req.method = header;
|
|
|
|
|
|
|
|
while (*header != '\0' && *header != ' ')
|
|
|
|
header++;
|
|
|
|
|
|
|
|
if (*header != '\0')
|
|
|
|
*header++ = '\0';
|
|
|
|
|
|
|
|
req.url = header;
|
|
|
|
|
|
|
|
while (*header != '\0' && *header != ' ')
|
|
|
|
header++;
|
|
|
|
|
|
|
|
*header = '\0';
|
|
|
|
|
|
|
|
SLURP_CONNECTION_HTTP_CLIENT(args[1], conn);
|
2013-03-28 21:52:43 +01:00
|
|
|
NODE_HTTP_CLIENT_REQUEST(&req, &conn, conn.remote, conn.port, req.method, \
|
2013-03-29 00:51:52 +01:00
|
|
|
req.url, conn.fd);
|
2011-02-10 03:50:26 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
|
|
|
|
void DTRACE_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>& args) {
|
2011-02-10 03:50:26 +01:00
|
|
|
if (!NODE_HTTP_CLIENT_RESPONSE_ENABLED())
|
2013-07-03 04:23:44 +02:00
|
|
|
return;
|
2014-10-13 15:19:55 +02:00
|
|
|
Environment* env = Environment::GetCurrent(args);
|
2011-02-10 03:50:26 +01:00
|
|
|
SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(args[0], args[1], conn);
|
2013-03-29 00:51:52 +01:00
|
|
|
NODE_HTTP_CLIENT_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
|
2011-02-10 03:50:26 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:14:49 +01:00
|
|
|
void dtrace_gc_start(Isolate* isolate,
|
|
|
|
GCType type,
|
|
|
|
GCCallbackFlags flags,
|
|
|
|
void* data) {
|
2014-03-15 07:15:11 +01:00
|
|
|
// Previous versions of this probe point only logged type and flags.
|
|
|
|
// That's why for reasons of backwards compatibility the isolate goes last.
|
|
|
|
NODE_GC_START(type, flags, isolate);
|
2011-02-10 03:50:26 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:14:49 +01:00
|
|
|
void dtrace_gc_done(Isolate* isolate,
|
|
|
|
GCType type,
|
|
|
|
GCCallbackFlags flags,
|
|
|
|
void* data) {
|
2014-03-15 07:15:11 +01:00
|
|
|
// Previous versions of this probe point only logged type and flags.
|
|
|
|
// That's why for reasons of backwards compatibility the isolate goes last.
|
|
|
|
NODE_GC_DONE(type, flags, isolate);
|
2011-02-10 03:50:26 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:23:44 +02:00
|
|
|
|
2019-03-09 09:28:04 +01:00
|
|
|
void InitDTrace(Environment* env) {
|
2012-06-12 01:23:17 +02:00
|
|
|
#ifdef HAVE_ETW
|
2019-02-03 15:14:19 +01:00
|
|
|
// ETW is neither thread-safe nor does it clean up resources on exit,
|
|
|
|
// so we can use it only on the main thread.
|
|
|
|
if (env->is_main_thread()) {
|
|
|
|
init_etw();
|
|
|
|
}
|
2012-06-12 01:23:17 +02:00
|
|
|
#endif
|
|
|
|
|
2019-03-18 16:14:49 +01:00
|
|
|
// We need to use the variant of GC callbacks that takes data to
|
|
|
|
// avoid running into DCHECKs when multiple Environments try to add
|
|
|
|
// the same callback to the same isolate multiple times.
|
|
|
|
env->isolate()->AddGCPrologueCallback(dtrace_gc_start, env);
|
|
|
|
env->isolate()->AddGCEpilogueCallback(dtrace_gc_done, env);
|
|
|
|
env->AddCleanupHook([](void* data) {
|
|
|
|
Environment* env = static_cast<Environment*>(data);
|
|
|
|
env->isolate()->RemoveGCPrologueCallback(dtrace_gc_start, env);
|
|
|
|
env->isolate()->RemoveGCEpilogueCallback(dtrace_gc_done, env);
|
|
|
|
}, env);
|
2019-03-09 09:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeDTrace(Local<Object> target,
|
|
|
|
Local<Value> unused,
|
|
|
|
Local<Context> context,
|
|
|
|
void* priv) {
|
|
|
|
Environment* env = Environment::GetCurrent(context);
|
|
|
|
|
|
|
|
#if defined HAVE_DTRACE || defined HAVE_ETW
|
|
|
|
# define NODE_PROBE(name) env->SetMethod(target, #name, name);
|
|
|
|
NODE_PROBE(DTRACE_NET_SERVER_CONNECTION)
|
|
|
|
NODE_PROBE(DTRACE_NET_STREAM_END)
|
|
|
|
NODE_PROBE(DTRACE_HTTP_SERVER_REQUEST)
|
|
|
|
NODE_PROBE(DTRACE_HTTP_SERVER_RESPONSE)
|
|
|
|
NODE_PROBE(DTRACE_HTTP_CLIENT_REQUEST)
|
|
|
|
NODE_PROBE(DTRACE_HTTP_CLIENT_RESPONSE)
|
|
|
|
# undef NODE_PROBE
|
2011-02-10 03:50:26 +01:00
|
|
|
#endif
|
2011-01-25 02:50:10 +01:00
|
|
|
}
|
|
|
|
|
2013-07-31 20:07:29 +02:00
|
|
|
} // namespace node
|
2019-03-09 09:28:04 +01:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_INTERNAL(dtrace, node::InitializeDTrace)
|