|
7 | 7 | #define NODE_HTTP_SERVER_REQUEST_ENABLED() (0) |
8 | 8 | #define NODE_HTTP_SERVER_RESPONSE(arg0) |
9 | 9 | #define NODE_HTTP_SERVER_RESPONSE_ENABLED() (0) |
| 10 | +#define NODE_HTTP_CLIENT_REQUEST(arg0, arg1) |
| 11 | +#define NODE_HTTP_CLIENT_REQUEST_ENABLED() (0) |
| 12 | +#define NODE_HTTP_CLIENT_RESPONSE(arg0) |
| 13 | +#define NODE_HTTP_CLIENT_RESPONSE_ENABLED() (0) |
10 | 14 | #define NODE_NET_SERVER_CONNECTION(arg0) |
11 | 15 | #define NODE_NET_SERVER_CONNECTION_ENABLED() (0) |
12 | 16 | #define NODE_NET_STREAM_END(arg0) |
13 | 17 | #define NODE_NET_STREAM_END_ENABLED() (0) |
| 18 | +#define NODE_NET_SOCKET_READ(arg0, arg1) |
| 19 | +#define NODE_NET_SOCKET_READ_ENABLED() (0) |
| 20 | +#define NODE_NET_SOCKET_WRITE(arg0, arg1) |
| 21 | +#define NODE_NET_SOCKET_WRITE_ENABLED() (0) |
| 22 | +#define NODE_GC_START(arg0, arg1) |
| 23 | +#define NODE_GC_DONE(arg0, arg1) |
14 | 24 | #endif |
15 | 25 |
|
16 | 26 | namespace node { |
17 | 27 |
|
18 | 28 | using namespace v8; |
19 | 29 |
|
20 | 30 | #define SLURP_STRING(obj, member, valp) \ |
| 31 | + if (!(obj)->IsObject()) { \ |
| 32 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 33 | + "object for " #obj " to contain string member " #member)))); \ |
| 34 | + } \ |
21 | 35 | String::Utf8Value _##member(obj->Get(String::New(#member))->ToString()); \ |
22 | 36 | if ((*(const char **)valp = *_##member) == NULL) \ |
23 | 37 | *(const char **)valp = "<unknown>"; |
24 | 38 |
|
25 | 39 | #define SLURP_INT(obj, member, valp) \ |
| 40 | + if (!(obj)->IsObject()) { \ |
| 41 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 42 | + "object for " #obj " to contain integer member " #member)))); \ |
| 43 | + } \ |
26 | 44 | *valp = obj->Get(String::New(#member))->ToInteger()->Value(); |
27 | 45 |
|
| 46 | +#define SLURP_OBJECT(obj, member, valp) \ |
| 47 | + if (!(obj)->IsObject()) { \ |
| 48 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 49 | + "object for " #obj " to contain object member " #member)))); \ |
| 50 | + } \ |
| 51 | + *valp = Local<Object>::Cast(obj->Get(String::New(#member))); |
| 52 | + |
28 | 53 | #define SLURP_CONNECTION(arg, conn) \ |
| 54 | + if (!(arg)->IsObject()) { \ |
| 55 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 56 | + "argument " #arg " to be a connection object")))); \ |
| 57 | + } \ |
29 | 58 | node_dtrace_connection_t conn; \ |
30 | 59 | Local<Object> _##conn = Local<Object>::Cast(arg); \ |
31 | 60 | SLURP_INT(_##conn, fd, &conn.fd); \ |
32 | 61 | SLURP_STRING(_##conn, remoteAddress, &conn.remote); \ |
33 | | - SLURP_INT(_##conn, remotePort, &conn.port); |
| 62 | + SLURP_INT(_##conn, remotePort, &conn.port); \ |
| 63 | + SLURP_INT(_##conn, bufferSize, &conn.buffered); |
| 64 | + |
| 65 | +#define SLURP_CONNECTION_HTTP_CLIENT(arg, conn) \ |
| 66 | + if (!(arg)->IsObject()) { \ |
| 67 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 68 | + "argument " #arg " to be a connection object")))); \ |
| 69 | + } \ |
| 70 | + node_dtrace_connection_t conn; \ |
| 71 | + Local<Object> _##conn = Local<Object>::Cast(arg); \ |
| 72 | + SLURP_INT(_##conn, fd, &conn.fd); \ |
| 73 | + SLURP_STRING(_##conn, host, &conn.remote); \ |
| 74 | + SLURP_INT(_##conn, port, &conn.port); \ |
| 75 | + SLURP_INT(_##conn, bufferSize, &conn.buffered); |
| 76 | + |
| 77 | +#define SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(arg0, arg1, conn) \ |
| 78 | + if (!(arg0)->IsObject()) { \ |
| 79 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 80 | + "argument " #arg0 " to be a connection object")))); \ |
| 81 | + } \ |
| 82 | + if (!(arg1)->IsObject()) { \ |
| 83 | + return (ThrowException(Exception::Error(String::New("expected " \ |
| 84 | + "argument " #arg1 " to be a connection object")))); \ |
| 85 | + } \ |
| 86 | + node_dtrace_connection_t conn; \ |
| 87 | + Local<Object> _##conn = Local<Object>::Cast(arg0); \ |
| 88 | + SLURP_INT(_##conn, fd, &conn.fd); \ |
| 89 | + SLURP_INT(_##conn, bufferSize, &conn.buffered); \ |
| 90 | + _##conn = Local<Object>::Cast(arg1); \ |
| 91 | + SLURP_STRING(_##conn, host, &conn.remote); \ |
| 92 | + SLURP_INT(_##conn, port, &conn.port); |
| 93 | + |
34 | 94 |
|
35 | 95 | Handle<Value> DTRACE_NET_SERVER_CONNECTION(const Arguments& args) { |
36 | 96 | if (!NODE_NET_SERVER_CONNECTION_ENABLED()) |
@@ -56,6 +116,48 @@ Handle<Value> DTRACE_NET_STREAM_END(const Arguments& args) { |
56 | 116 | return Undefined(); |
57 | 117 | } |
58 | 118 |
|
| 119 | +Handle<Value> DTRACE_NET_SOCKET_READ(const Arguments& args) { |
| 120 | + if (!NODE_NET_SOCKET_READ_ENABLED()) |
| 121 | + return Undefined(); |
| 122 | + |
| 123 | + HandleScope scope; |
| 124 | + int nbytes; |
| 125 | + |
| 126 | + SLURP_CONNECTION(args[0], conn); |
| 127 | + |
| 128 | + if (!args[1]->IsNumber()) { |
| 129 | + return (ThrowException(Exception::Error(String::New("expected " |
| 130 | + "argument 1 to be number of bytes")))); |
| 131 | + } |
| 132 | + |
| 133 | + nbytes = args[1]->Int32Value(); |
| 134 | + |
| 135 | + NODE_NET_SOCKET_READ(&conn, nbytes); |
| 136 | + |
| 137 | + return Undefined(); |
| 138 | +} |
| 139 | + |
| 140 | +Handle<Value> DTRACE_NET_SOCKET_WRITE(const Arguments& args) { |
| 141 | + if (!NODE_NET_SOCKET_WRITE_ENABLED()) |
| 142 | + return Undefined(); |
| 143 | + |
| 144 | + HandleScope scope; |
| 145 | + int nbytes; |
| 146 | + |
| 147 | + SLURP_CONNECTION(args[0], conn); |
| 148 | + |
| 149 | + if (!args[1]->IsNumber()) { |
| 150 | + return (ThrowException(Exception::Error(String::New("expected " |
| 151 | + "argument 1 to be number of bytes")))); |
| 152 | + } |
| 153 | + |
| 154 | + nbytes = args[1]->Int32Value(); |
| 155 | + |
| 156 | + NODE_NET_SOCKET_WRITE(&conn, nbytes); |
| 157 | + |
| 158 | + return Undefined(); |
| 159 | +} |
| 160 | + |
59 | 161 | Handle<Value> DTRACE_HTTP_SERVER_REQUEST(const Arguments& args) { |
60 | 162 | node_dtrace_http_request_t req; |
61 | 163 |
|
@@ -65,7 +167,6 @@ Handle<Value> DTRACE_HTTP_SERVER_REQUEST(const Arguments& args) { |
65 | 167 | HandleScope scope; |
66 | 168 |
|
67 | 169 | Local<Object> arg0 = Local<Object>::Cast(args[0]); |
68 | | - Local<Object> arg1 = Local<Object>::Cast(args[1]); |
69 | 170 |
|
70 | 171 | SLURP_STRING(arg0, url, &req.url); |
71 | 172 | SLURP_STRING(arg0, method, &req.method); |
@@ -88,8 +189,72 @@ Handle<Value> DTRACE_HTTP_SERVER_RESPONSE(const Arguments& args) { |
88 | 189 | return Undefined(); |
89 | 190 | } |
90 | 191 |
|
| 192 | +Handle<Value> DTRACE_HTTP_CLIENT_REQUEST(const Arguments& args) { |
| 193 | + node_dtrace_http_request_t req; |
| 194 | + char *header; |
| 195 | + |
| 196 | + if (!NODE_HTTP_CLIENT_REQUEST_ENABLED()) |
| 197 | + return Undefined(); |
| 198 | + |
| 199 | + HandleScope scope; |
| 200 | + |
| 201 | + /* |
| 202 | + * For the method and URL, we're going to dig them out of the header. This |
| 203 | + * is not as efficient as it could be, but we would rather not force the |
| 204 | + * caller here to retain their method and URL until the time at which |
| 205 | + * DTRACE_HTTP_CLIENT_REQUEST can be called. |
| 206 | + */ |
| 207 | + Local<Object> arg0 = Local<Object>::Cast(args[0]); |
| 208 | + SLURP_STRING(arg0, _header, &header); |
| 209 | + |
| 210 | + req.method = header; |
| 211 | + |
| 212 | + while (*header != '\0' && *header != ' ') |
| 213 | + header++; |
| 214 | + |
| 215 | + if (*header != '\0') |
| 216 | + *header++ = '\0'; |
| 217 | + |
| 218 | + req.url = header; |
| 219 | + |
| 220 | + while (*header != '\0' && *header != ' ') |
| 221 | + header++; |
| 222 | + |
| 223 | + *header = '\0'; |
| 224 | + |
| 225 | + SLURP_CONNECTION_HTTP_CLIENT(args[1], conn); |
| 226 | + NODE_HTTP_CLIENT_REQUEST(&req, &conn); |
| 227 | + return Undefined(); |
| 228 | +} |
| 229 | + |
| 230 | +Handle<Value> DTRACE_HTTP_CLIENT_RESPONSE(const Arguments& args) { |
| 231 | + if (!NODE_HTTP_CLIENT_RESPONSE_ENABLED()) |
| 232 | + return Undefined(); |
| 233 | + |
| 234 | + HandleScope scope; |
| 235 | + |
| 236 | + SLURP_CONNECTION_HTTP_CLIENT_RESPONSE(args[0], args[1], conn); |
| 237 | + NODE_HTTP_CLIENT_RESPONSE(&conn); |
| 238 | + |
| 239 | + return Undefined(); |
| 240 | +} |
| 241 | + |
91 | 242 | #define NODE_PROBE(name) #name, name |
92 | 243 |
|
| 244 | +static int dtrace_gc_start(GCType type, GCCallbackFlags flags) { |
| 245 | + NODE_GC_START(type, flags); |
| 246 | + /* |
| 247 | + * We avoid the tail-call elimination of the USDT probe (which screws up |
| 248 | + * args) by forcing a return of 0. |
| 249 | + */ |
| 250 | + return 0; |
| 251 | +} |
| 252 | + |
| 253 | +static int dtrace_gc_done(GCType type, GCCallbackFlags flags) { |
| 254 | + NODE_GC_DONE(type, flags); |
| 255 | + return 0; |
| 256 | +} |
| 257 | + |
93 | 258 | void InitDTrace(Handle<Object> target) { |
94 | 259 | static struct { |
95 | 260 | const char *name; |
@@ -98,8 +263,12 @@ void InitDTrace(Handle<Object> target) { |
98 | 263 | } tab[] = { |
99 | 264 | { NODE_PROBE(DTRACE_NET_SERVER_CONNECTION) }, |
100 | 265 | { NODE_PROBE(DTRACE_NET_STREAM_END) }, |
| 266 | + { NODE_PROBE(DTRACE_NET_SOCKET_READ) }, |
| 267 | + { NODE_PROBE(DTRACE_NET_SOCKET_WRITE) }, |
101 | 268 | { NODE_PROBE(DTRACE_HTTP_SERVER_REQUEST) }, |
102 | 269 | { NODE_PROBE(DTRACE_HTTP_SERVER_RESPONSE) }, |
| 270 | + { NODE_PROBE(DTRACE_HTTP_CLIENT_REQUEST) }, |
| 271 | + { NODE_PROBE(DTRACE_HTTP_CLIENT_RESPONSE) }, |
103 | 272 | { NULL } |
104 | 273 | }; |
105 | 274 |
|
@@ -108,6 +277,11 @@ void InitDTrace(Handle<Object> target) { |
108 | 277 | FunctionTemplate::New(tab[i].func)); |
109 | 278 | target->Set(String::NewSymbol(tab[i].name), tab[i].templ->GetFunction()); |
110 | 279 | } |
| 280 | + |
| 281 | +#ifdef HAVE_DTRACE |
| 282 | + v8::V8::AddGCPrologueCallback((GCPrologueCallback)dtrace_gc_start); |
| 283 | + v8::V8::AddGCEpilogueCallback((GCEpilogueCallback)dtrace_gc_done); |
| 284 | +#endif |
111 | 285 | } |
112 | 286 |
|
113 | 287 | } |