|
| 1 | +#include <node_dtrace.h> |
| 2 | + |
| 3 | +#ifdef HAVE_DTRACE |
| 4 | +#include "node_provider.h" |
| 5 | +#else |
| 6 | +#define NODE_HTTP_SERVER_REQUEST(arg0, arg1) |
| 7 | +#define NODE_HTTP_SERVER_REQUEST_ENABLED() (0) |
| 8 | +#define NODE_HTTP_SERVER_RESPONSE(arg0) |
| 9 | +#define NODE_HTTP_SERVER_RESPONSE_ENABLED() (0) |
| 10 | +#define NODE_NET_SERVER_CONNECTION(arg0) |
| 11 | +#define NODE_NET_SERVER_CONNECTION_ENABLED() (0) |
| 12 | +#define NODE_NET_STREAM_END(arg0) |
| 13 | +#define NODE_NET_STREAM_END_ENABLED() (0) |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace node { |
| 17 | + |
| 18 | +using namespace v8; |
| 19 | + |
| 20 | +#define SLURP_STRING(obj, member, valp) \ |
| 21 | + String::Utf8Value _##member(obj->Get(String::New(#member))->ToString()); \ |
| 22 | + if ((*(const char **)valp = *_##member) == NULL) \ |
| 23 | + *(const char **)valp = "<unknown>"; |
| 24 | + |
| 25 | +#define SLURP_INT(obj, member, valp) \ |
| 26 | + *valp = obj->Get(String::New(#member))->ToInteger()->Value(); |
| 27 | + |
| 28 | +#define SLURP_CONNECTION(arg, conn) \ |
| 29 | + node_dtrace_connection_t conn; \ |
| 30 | + Local<Object> _##conn = Local<Object>::Cast(arg); \ |
| 31 | + SLURP_INT(_##conn, fd, &conn.fd); \ |
| 32 | + SLURP_STRING(_##conn, remoteAddress, &conn.remote); \ |
| 33 | + SLURP_INT(_##conn, remotePort, &conn.port); |
| 34 | + |
| 35 | +Handle<Value> DTRACE_NET_SERVER_CONNECTION(const Arguments& args) { |
| 36 | + if (!NODE_NET_SERVER_CONNECTION_ENABLED()) |
| 37 | + return Undefined(); |
| 38 | + |
| 39 | + HandleScope scope; |
| 40 | + |
| 41 | + SLURP_CONNECTION(args[0], conn); |
| 42 | + NODE_NET_SERVER_CONNECTION(&conn); |
| 43 | + |
| 44 | + return Undefined(); |
| 45 | +} |
| 46 | + |
| 47 | +Handle<Value> DTRACE_NET_STREAM_END(const Arguments& args) { |
| 48 | + if (!NODE_NET_STREAM_END_ENABLED()) |
| 49 | + return Undefined(); |
| 50 | + |
| 51 | + HandleScope scope; |
| 52 | + |
| 53 | + SLURP_CONNECTION(args[0], conn); |
| 54 | + NODE_NET_STREAM_END(&conn); |
| 55 | + |
| 56 | + return Undefined(); |
| 57 | +} |
| 58 | + |
| 59 | +Handle<Value> DTRACE_HTTP_SERVER_REQUEST(const Arguments& args) { |
| 60 | + node_dtrace_http_request_t req; |
| 61 | + |
| 62 | + if (!NODE_HTTP_SERVER_REQUEST_ENABLED()) |
| 63 | + return Undefined(); |
| 64 | + |
| 65 | + HandleScope scope; |
| 66 | + |
| 67 | + Local<Object> arg0 = Local<Object>::Cast(args[0]); |
| 68 | + Local<Object> arg1 = Local<Object>::Cast(args[1]); |
| 69 | + |
| 70 | + SLURP_STRING(arg0, url, &req.url); |
| 71 | + SLURP_STRING(arg0, method, &req.method); |
| 72 | + |
| 73 | + SLURP_CONNECTION(args[1], conn); |
| 74 | + |
| 75 | + NODE_HTTP_SERVER_REQUEST(&req, &conn); |
| 76 | + return Undefined(); |
| 77 | +} |
| 78 | + |
| 79 | +Handle<Value> DTRACE_HTTP_SERVER_RESPONSE(const Arguments& args) { |
| 80 | + if (!NODE_HTTP_SERVER_RESPONSE_ENABLED()) |
| 81 | + return Undefined(); |
| 82 | + |
| 83 | + HandleScope scope; |
| 84 | + |
| 85 | + SLURP_CONNECTION(args[0], conn); |
| 86 | + NODE_HTTP_SERVER_RESPONSE(&conn); |
| 87 | + |
| 88 | + return Undefined(); |
| 89 | +} |
| 90 | + |
| 91 | +#define NODE_PROBE(name) #name, name |
| 92 | + |
| 93 | +void InitDTrace(Handle<Object> target) { |
| 94 | + static struct { |
| 95 | + const char *name; |
| 96 | + Handle<Value> (*func)(const Arguments&); |
| 97 | + Persistent<FunctionTemplate> templ; |
| 98 | + } tab[] = { |
| 99 | + { NODE_PROBE(DTRACE_NET_SERVER_CONNECTION) }, |
| 100 | + { NODE_PROBE(DTRACE_NET_STREAM_END) }, |
| 101 | + { NODE_PROBE(DTRACE_HTTP_SERVER_REQUEST) }, |
| 102 | + { NODE_PROBE(DTRACE_HTTP_SERVER_RESPONSE) }, |
| 103 | + { NULL } |
| 104 | + }; |
| 105 | + |
| 106 | + for (int i = 0; tab[i].name != NULL; i++) { |
| 107 | + tab[i].templ = Persistent<FunctionTemplate>::New( |
| 108 | + FunctionTemplate::New(tab[i].func)); |
| 109 | + target->Set(String::NewSymbol(tab[i].name), tab[i].templ->GetFunction()); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +} |
0 commit comments