Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_lttng.cc
Go to the documentation of this file.
1 #include "util.h"
2 
3 #ifdef HAVE_LTTNG
4 #include "node_lttng.h"
5 #include "node_lttng_provider.h"
6 #include <string.h>
7 #else
8 #define NODE_HTTP_SERVER_REQUEST(arg0, arg1)
9 #define NODE_HTTP_SERVER_REQUEST_ENABLED() (0)
10 #define NODE_HTTP_SERVER_RESPONSE(arg0)
11 #define NODE_HTTP_SERVER_RESPONSE_ENABLED() (0)
12 #define NODE_HTTP_CLIENT_REQUEST(arg0, arg1)
13 #define NODE_HTTP_CLIENT_REQUEST_ENABLED() (0)
14 #define NODE_HTTP_CLIENT_RESPONSE(arg0)
15 #define NODE_HTTP_CLIENT_RESPONSE_ENABLED() (0)
16 #define NODE_NET_SERVER_CONNECTION(arg0)
17 #define NODE_NET_SERVER_CONNECTION_ENABLED() (0)
18 #define NODE_NET_STREAM_END(arg0)
19 #define NODE_NET_STREAM_END_ENABLED() (0)
20 #define NODE_GC_START(arg0, arg1, arg2)
21 #define NODE_GC_DONE(arg0, arg1, arg2)
22 #endif
23 
24 #include "env.h"
25 #include "env-inl.h"
26 
27 namespace node {
28 
29 using v8::FunctionCallbackInfo;
30 using v8::FunctionTemplate;
31 using v8::GCCallbackFlags;
32 using v8::GCType;
33 using v8::HandleScope;
34 using v8::Isolate;
35 using v8::Local;
36 using v8::Object;
37 using v8::String;
38 using v8::Value;
39 
40 #define SLURP_STRING(obj, member, valp) \
41  if (!(obj)->IsObject()) { \
42  return env->ThrowError( \
43  "expected object for " #obj " to contain string member " #member); \
44  } \
45  node::Utf8Value _##member(env->isolate(), \
46  obj->Get(OneByteString(env->isolate(), #member))); \
47  if ((*(const char **)valp = *_##member) == nullptr) \
48  *(const char **)valp = "<unknown>";
49 
50 #define SLURP_INT(obj, member, valp) \
51  if (!(obj)->IsObject()) { \
52  return env->ThrowError( \
53  "expected object for " #obj " to contain integer member " #member); \
54  } \
55  *valp = obj->Get(OneByteString(env->isolate(), #member)) \
56  ->ToInteger(env->isolate())->Value();
57 
58 #define SLURP_OBJECT(obj, member, valp) \
59  if (!(obj)->IsObject()) { \
60  return env->ThrowError( \
61  "expected object for " #obj " to contain object member " #member); \
62  } \
63  *valp = Local<Object>::Cast(obj->Get(OneByteString(env->isolate(), #member)));
64 
65 #define SLURP_CONNECTION(arg, conn) \
66  if (!(arg)->IsObject()) { \
67  return env->ThrowError( \
68  "expected argument " #arg " to be a connection object"); \
69  } \
70  node_lttng_connection_t conn; \
71  Local<Object> _##conn = Local<Object>::Cast(arg); \
72  Local<Value> _handle = \
73  (_##conn)->Get(FIXED_ONE_BYTE_STRING(env->isolate(), "_handle")); \
74  if (_handle->IsObject()) { \
75  SLURP_INT(_handle.As<Object>(), fd, &conn.fd); \
76  } else { \
77  conn.fd = -1; \
78  } \
79  SLURP_STRING(_##conn, remoteAddress, &conn.remote); \
80  SLURP_INT(_##conn, remotePort, &conn.port); \
81  SLURP_INT(_##conn, bufferSize, &conn.buffered);
82 
83 #define SLURP_CONNECTION_HTTP_CLIENT(arg, conn) \
84  if (!(arg)->IsObject()) { \
85  return env->ThrowError( \
86  "expected argument " #arg " to be a connection object"); \
87  } \
88  node_lttng_connection_t conn; \
89  Local<Object> _##conn = Local<Object>::Cast(arg); \
90  SLURP_INT(_##conn, fd, &conn.fd); \
91  SLURP_STRING(_##conn, host, &conn.remote); \
92  SLURP_INT(_##conn, port, &conn.port); \
93  SLURP_INT(_##conn, bufferSize, &conn.buffered);
94 
95 #define SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(arg0, arg1, conn) \
96  if (!(arg0)->IsObject()) { \
97  return env->ThrowError( \
98  "expected argument " #arg0 " to be a connection object"); \
99  } \
100  if (!(arg1)->IsObject()) { \
101  return env->ThrowError( \
102  "expected argument " #arg1 " to be a connection object"); \
103  } \
104  node_lttng_connection_t conn; \
105  Local<Object> _##conn = Local<Object>::Cast(arg0); \
106  SLURP_INT(_##conn, fd, &conn.fd); \
107  SLURP_INT(_##conn, bufferSize, &conn.buffered); \
108  _##conn = Local<Object>::Cast(arg1); \
109  SLURP_STRING(_##conn, host, &conn.remote); \
110  SLURP_INT(_##conn, port, &conn.port);
111 
112 
113 void LTTNG_NET_SERVER_CONNECTION(const FunctionCallbackInfo<Value>& args) {
115  return;
116  Environment* env = Environment::GetCurrent(args);
117  SLURP_CONNECTION(args[0], conn);
118  NODE_NET_SERVER_CONNECTION(&conn, conn.remote, conn.port, conn.fd);
119 }
120 
121 
122 void LTTNG_NET_STREAM_END(const FunctionCallbackInfo<Value>& args) {
124  return;
125  Environment* env = Environment::GetCurrent(args);
126  SLURP_CONNECTION(args[0], conn);
127  NODE_NET_STREAM_END(&conn, conn.remote, conn.port, conn.fd);
128 }
129 
130 
131 void LTTNG_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>& args) {
132  node_lttng_http_server_request_t req;
133 
135  return;
136 
137  if (!args[0]->IsObject())
138  return;
139 
140  Environment* env = Environment::GetCurrent(args);
141  Local<Object> arg0 = args[0].As<Object>();
142  Local<Object> headers;
143 
144  memset(&req, 0, sizeof(req));
145  req._un.version = 1;
146  SLURP_STRING(arg0, url, &req.url);
147  SLURP_STRING(arg0, method, &req.method);
148  SLURP_OBJECT(arg0, headers, &headers);
149 
150  if (!(headers)->IsObject()) {
151  return env->ThrowError(
152  "expected object for request to contain string member headers");
153  }
154 
155  Local<Value> strfwdfor = headers->Get(env->x_forwarded_string());
156  node::Utf8Value fwdfor(env->isolate(), strfwdfor);
157  req.forwarded_for = *fwdfor;
158 
159  if (!strfwdfor->IsString() || req.forwarded_for == nullptr)
160  req.forwarded_for = "";
161 
162  SLURP_CONNECTION(args[1], conn);
163  NODE_HTTP_SERVER_REQUEST(&req, &conn, conn.remote, conn.port, req.method, \
164  req.url, conn.fd);
165 }
166 
167 
168 void LTTNG_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value>& args) {
170  return;
171  Environment* env = Environment::GetCurrent(args);
172  SLURP_CONNECTION(args[0], conn);
173  NODE_HTTP_SERVER_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
174 }
175 
176 
177 void LTTNG_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>& args) {
178  node_lttng_http_client_request_t req;
179  char* header;
180 
182  return;
183 
184  Environment* env = Environment::GetCurrent(args);
185 
186  /*
187  * For the method and URL, we're going to dig them out of the header. This
188  * is not as efficient as it could be, but we would rather not force the
189  * caller here to retain their method and URL until the time at which
190  * LTTNG_HTTP_CLIENT_REQUEST can be called.
191  */
192  Local<Object> arg0 = args[0].As<Object>();
193  SLURP_STRING(arg0, _header, &header);
194 
195  req.method = header;
196 
197  while (*header != '\0' && *header != ' ')
198  header++;
199 
200  if (*header != '\0')
201  *header++ = '\0';
202 
203  req.url = header;
204 
205  while (*header != '\0' && *header != ' ')
206  header++;
207 
208  *header = '\0';
209 
210  SLURP_CONNECTION_HTTP_CLIENT(args[1], conn);
211  NODE_HTTP_CLIENT_REQUEST(&req, &conn, conn.remote, conn.port, req.method, \
212  req.url, conn.fd);
213 }
214 
215 
216 void LTTNG_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>& args) {
218  return;
219  Environment* env = Environment::GetCurrent(args);
220  SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(args[0], args[1], conn);
221  NODE_HTTP_CLIENT_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
222 }
223 
224 
225 void lttng_gc_start(Isolate* isolate, GCType type, GCCallbackFlags flags) {
226  NODE_GC_START(type, flags, isolate);
227 }
228 
229 
230 void lttng_gc_done(Isolate* isolate, GCType type, GCCallbackFlags flags) {
231  NODE_GC_DONE(type, flags, isolate);
232 }
233 
234 void InitLTTNG(Environment* env, Local<Object> target) {
235  HandleScope scope(env->isolate());
236 
237  static struct {
238  const char *name;
239  void (*func)(const FunctionCallbackInfo<Value>&);
240  } tab[] = {
241 #define NODE_PROBE(name) #name, name
248 #undef NODE_PROBE
249  };
250 
251  for (size_t i = 0; i < arraysize(tab); i++) {
252  Local<String> key = OneByteString(env->isolate(), tab[i].name);
253  Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
254  target->Set(key, val);
255  }
256 
257 #if defined HAVE_LTTNG
258  env->isolate()->AddGCPrologueCallback(lttng_gc_start);
259  env->isolate()->AddGCEpilogueCallback(lttng_gc_done);
260 #endif
261 }
262 
263 } // namespace node
this func
Definition: v8ustack.d:371
void InitLTTNG(Environment *env, Local< Object > target)
Definition: node_lttng.cc:234
void LTTNG_NET_STREAM_END(const FunctionCallbackInfo< Value > &args)
Definition: node_lttng.cc:122
#define NODE_NET_SERVER_CONNECTION(arg0)
Definition: node_lttng.cc:16
#define NODE_HTTP_CLIENT_REQUEST(arg0, arg1)
Definition: node_lttng.cc:12
#define NODE_HTTP_SERVER_REQUEST_ENABLED()
Definition: node_lttng.cc:9
void LTTNG_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo< Value > &args)
Definition: node_lttng.cc:216
#define NODE_HTTP_CLIENT_RESPONSE_ENABLED()
Definition: node_lttng.cc:15
void LTTNG_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo< Value > &args)
Definition: node_lttng.cc:177
#define SLURP_STRING(obj, member, valp)
Definition: node_lttng.cc:40
#define NODE_NET_STREAM_END(arg0)
Definition: node_lttng.cc:18
#define NODE_GC_START(arg0, arg1, arg2)
Definition: node_lttng.cc:20
#define NODE_HTTP_SERVER_REQUEST(arg0, arg1)
Definition: node_lttng.cc:8
#define NODE_GC_DONE(arg0, arg1, arg2)
Definition: node_lttng.cc:21
#define SLURP_CONNECTION(arg, conn)
Definition: node_lttng.cc:65
#define SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(arg0, arg1, conn)
Definition: node_lttng.cc:95
#define NODE_PROBE(name)
#define NODE_HTTP_SERVER_RESPONSE(arg0)
Definition: node_lttng.cc:10
#define NODE_NET_STREAM_END_ENABLED()
Definition: node_lttng.cc:19
#define NODE_NET_SERVER_CONNECTION_ENABLED()
Definition: node_lttng.cc:17
#define NODE_HTTP_CLIENT_RESPONSE(arg0)
Definition: node_lttng.cc:14
#define SLURP_CONNECTION_HTTP_CLIENT(arg, conn)
Definition: node_lttng.cc:83
void LTTNG_HTTP_SERVER_REQUEST(const FunctionCallbackInfo< Value > &args)
Definition: node_lttng.cc:131
uv_fs_t req
Definition: node_file.cc:374
void lttng_gc_done(Isolate *isolate, GCType type, GCCallbackFlags flags)
Definition: node_lttng.cc:230
#define NODE_HTTP_SERVER_RESPONSE_ENABLED()
Definition: node_lttng.cc:11
method
Definition: node.d:195
void LTTNG_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo< Value > &args)
Definition: node_lttng.cc:168
void LTTNG_NET_SERVER_CONNECTION(const FunctionCallbackInfo< Value > &args)
Definition: node_lttng.cc:113
#define SLURP_OBJECT(obj, member, valp)
Definition: node_lttng.cc:58
void lttng_gc_start(Isolate *isolate, GCType type, GCCallbackFlags flags)
Definition: node_lttng.cc:225
#define NODE_HTTP_CLIENT_REQUEST_ENABLED()
Definition: node_lttng.cc:13