Skip to content

Commit 35a1421

Browse files
author
Igor Zinkovsky
committed
Windows: Enable ETW events.
This commit enables ETW events to be fired on Windows for existing DTrace probes. ETW instrumentation is enabled by default. It is possible to build node.exe without ETW instrumentation by using --without-etw option with configure script.
1 parent 74c8041 commit 35a1421

11 files changed

Lines changed: 500 additions & 12 deletions

configure

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ parser.add_option("--without-dtrace",
115115
dest="without_dtrace",
116116
help="Build without DTrace")
117117

118+
parser.add_option("--with-etw",
119+
action="store_true",
120+
dest="with_etw",
121+
help="Build with ETW (default is true on Windows)")
122+
123+
parser.add_option("--without-etw",
124+
action="store_true",
125+
dest="without_etw",
126+
help="Build without ETW")
127+
118128
# CHECKME does this still work with recent releases of V8?
119129
parser.add_option("--gdb",
120130
action="store_true",
@@ -273,6 +283,15 @@ def configure_node(o):
273283
o['variables']['node_use_dtrace'] = 'false'
274284

275285

286+
# By default, enable ETW on Windows.
287+
if sys.platform.startswith('win32'):
288+
o['variables']['node_use_etw'] = b(not options.without_etw);
289+
elif b(options.with_etw) == 'true':
290+
raise Exception('ETW is only supported on Windows.')
291+
else:
292+
o['variables']['node_use_etw'] = 'false'
293+
294+
276295
def configure_libz(o):
277296
o['variables']['node_shared_zlib'] = b(options.shared_zlib)
278297

node.gyp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# See http://codereview.chromium.org/8159015
66
'werror': '',
77
'node_use_dtrace%': 'false',
8+
'node_use_etw%': 'false',
89
'node_shared_v8%': 'false',
910
'node_shared_zlib%': 'false',
1011
'node_use_openssl%': 'true',
@@ -163,7 +164,18 @@
163164
}
164165
] ],
165166
} ],
166-
167+
[ 'node_use_etw=="true"', {
168+
'defines': [ 'HAVE_ETW=1' ],
169+
'dependencies': [ 'node_etw' ],
170+
'sources': [
171+
'src/node_win32_etw_provider.h',
172+
'src/node_win32_etw_provider-inl.h',
173+
'src/node_win32_etw_provider.cc',
174+
'src/node_dtrace.cc',
175+
'<(SHARED_INTERMEDIATE_DIR)/node_etw_provider.h',
176+
'<(SHARED_INTERMEDIATE_DIR)/node_etw_provider.rc',
177+
]
178+
} ],
167179
[ 'node_shared_v8=="true"', {
168180
'sources': [
169181
'<(node_shared_v8_includes)/v8.h',
@@ -228,7 +240,23 @@
228240
},
229241
},
230242
},
231-
243+
# generate ETW header and resource files
244+
{
245+
'target_name': 'node_etw',
246+
'type': 'none',
247+
'conditions': [
248+
[ 'node_use_etw=="true"', {
249+
'actions': [
250+
{
251+
'action_name': 'node_etw',
252+
'inputs': [ 'src/res/node_etw_provider.man' ],
253+
'outputs': [ '<(SHARED_INTERMEDIATE_DIR)' ],
254+
'action': [ 'mc <@(_inputs) -h <@(_outputs) -r <@(_outputs)' ]
255+
}
256+
]
257+
} ]
258+
]
259+
},
232260
{
233261
'target_name': 'node_js2c',
234262
'type': 'none',
@@ -251,7 +279,7 @@
251279
# action?
252280

253281
'conditions': [
254-
[ 'node_use_dtrace=="true"', {
282+
[ 'node_use_dtrace=="true" or node_use_etw=="true"', {
255283
'action': [
256284
'python',
257285
'tools/js2c.py',

src/node.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
#include "uv.h"
2727

2828
#include "v8-debug.h"
29-
#ifdef HAVE_DTRACE
29+
#if defined HAVE_DTRACE || defined HAVE_ETW
3030
# include "node_dtrace.h"
3131
#endif
3232

33+
#ifdef HAVE_ETW
34+
# include "node_win32_etw_provider.h"
35+
#endif
36+
3337
#include <locale.h>
3438
#include <signal.h>
3539
#include <stdio.h>
@@ -2325,7 +2329,7 @@ void Load(Handle<Object> process_l) {
23252329
Local<Object> global = v8::Context::GetCurrent()->Global();
23262330
Local<Value> args[1] = { Local<Value>::New(process_l) };
23272331

2328-
#ifdef HAVE_DTRACE
2332+
#if defined HAVE_DTRACE || defined HAVE_ETW
23292333
InitDTrace(global);
23302334
#endif
23312335

@@ -2894,6 +2898,10 @@ int Start(int argc, char *argv[]) {
28942898
// watchers, it blocks.
28952899
uv_run(uv_default_loop());
28962900

2901+
#ifdef HAVE_ETW
2902+
shutdown_etw();
2903+
#endif
2904+
28972905
EmitExit(process_l);
28982906
RunAtExit();
28992907

src/node_dtrace.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
#ifdef HAVE_DTRACE
2626
#include "node_provider.h"
27+
#elif HAVE_ETW
28+
#include "node_win32_etw_provider.h"
29+
#include "node_win32_etw_provider-inl.h"
2730
#else
2831
#define NODE_HTTP_SERVER_REQUEST(arg0, arg1)
2932
#define NODE_HTTP_SERVER_REQUEST_ENABLED() (0)
@@ -315,7 +318,11 @@ void InitDTrace(Handle<Object> target) {
315318
target->Set(String::NewSymbol(tab[i].name), tab[i].templ->GetFunction());
316319
}
317320

318-
#ifdef HAVE_DTRACE
321+
#ifdef HAVE_ETW
322+
init_etw();
323+
#endif
324+
325+
#if defined HAVE_DTRACE || defined HAVE_ETW
319326
v8::V8::AddGCPrologueCallback((GCPrologueCallback)dtrace_gc_start);
320327
v8::V8::AddGCEpilogueCallback((GCEpilogueCallback)dtrace_gc_done);
321328
#endif

src/node_win32_etw_provider-inl.h

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
#ifndef SRC_ETW_INL_H_
23+
#define SRC_ETW_INL_H_
24+
25+
#include "node_win32_etw_provider.h"
26+
#include "node_etw_provider.h"
27+
28+
namespace node {
29+
30+
using namespace v8;
31+
32+
// From node_win32_etw_provider.cc
33+
extern REGHANDLE node_provider;
34+
extern EventWriteFunc event_write;
35+
extern int events_enabled;
36+
37+
#define ETW_WRITE_STRING_DATA(data_descriptor, data) \
38+
EventDataDescCreate(data_descriptor, \
39+
data, \
40+
(strlen(data) + 1) * sizeof(char));
41+
42+
#define ETW_WRITE_INT32_DATA(data_descriptor, data) \
43+
EventDataDescCreate(data_descriptor, data, sizeof(int32_t));
44+
45+
#define ETW_WRITE_NET_CONNECTION(descriptors, conn) \
46+
ETW_WRITE_INT32_DATA(descriptors, &conn->fd); \
47+
ETW_WRITE_INT32_DATA(descriptors + 1, &conn->port); \
48+
ETW_WRITE_STRING_DATA(descriptors + 2, conn->remote); \
49+
ETW_WRITE_INT32_DATA(descriptors + 3, &conn->buffered);
50+
51+
#define ETW_WRITE_HTTP_SERVER_REQUEST(descriptors, req) \
52+
ETW_WRITE_STRING_DATA(descriptors, req->url); \
53+
ETW_WRITE_STRING_DATA(descriptors + 1, req->method); \
54+
ETW_WRITE_STRING_DATA(descriptors + 2, req->forwardedFor);
55+
56+
#define ETW_WRITE_HTTP_CLIENT_REQUEST(descriptors, req) \
57+
ETW_WRITE_STRING_DATA(descriptors, req->url); \
58+
ETW_WRITE_STRING_DATA(descriptors + 1, req->method);
59+
60+
#define ETW_WRITE_GC(descriptors, type, flags) \
61+
ETW_WRITE_INT32_DATA(descriptors, &type); \
62+
ETW_WRITE_INT32_DATA(descriptors + 1, &flags);
63+
64+
#define ETW_WRITE_EVENT(eventDescriptor, dataDescriptors) \
65+
DWORD status = event_write(node_provider, \
66+
&eventDescriptor, \
67+
sizeof(dataDescriptors)/sizeof(*dataDescriptors),\
68+
dataDescriptors); \
69+
assert(status == ERROR_SUCCESS);
70+
71+
72+
void NODE_HTTP_SERVER_REQUEST(node_dtrace_http_server_request_t* req,
73+
node_dtrace_connection_t* conn) {
74+
EVENT_DATA_DESCRIPTOR descriptors[7];
75+
ETW_WRITE_HTTP_SERVER_REQUEST(descriptors, req);
76+
ETW_WRITE_NET_CONNECTION(descriptors + 3, conn);
77+
ETW_WRITE_EVENT(NODE_HTTP_SERVER_REQUEST_EVENT, descriptors);
78+
}
79+
80+
81+
void NODE_HTTP_SERVER_RESPONSE(node_dtrace_connection_t* conn) {
82+
EVENT_DATA_DESCRIPTOR descriptors[4];
83+
ETW_WRITE_NET_CONNECTION(descriptors, conn);
84+
ETW_WRITE_EVENT(NODE_HTTP_SERVER_RESPONSE_EVENT, descriptors);
85+
}
86+
87+
88+
void NODE_HTTP_CLIENT_REQUEST(node_dtrace_http_client_request_t* req,
89+
node_dtrace_connection_t* conn) {
90+
EVENT_DATA_DESCRIPTOR descriptors[6];
91+
ETW_WRITE_HTTP_CLIENT_REQUEST(descriptors, req);
92+
ETW_WRITE_NET_CONNECTION(descriptors + 2, conn);
93+
ETW_WRITE_EVENT(NODE_HTTP_CLIENT_REQUEST_EVENT, descriptors);
94+
}
95+
96+
97+
void NODE_HTTP_CLIENT_RESPONSE(node_dtrace_connection_t* conn) {
98+
EVENT_DATA_DESCRIPTOR descriptors[4];
99+
ETW_WRITE_NET_CONNECTION(descriptors, conn);
100+
ETW_WRITE_EVENT(NODE_HTTP_CLIENT_RESPONSE_EVENT, descriptors);
101+
}
102+
103+
104+
void NODE_NET_SERVER_CONNECTION(node_dtrace_connection_t* conn) {
105+
EVENT_DATA_DESCRIPTOR descriptors[4];
106+
ETW_WRITE_NET_CONNECTION(descriptors, conn);
107+
ETW_WRITE_EVENT(NODE_NET_SERVER_CONNECTION_EVENT, descriptors);
108+
}
109+
110+
111+
void NODE_NET_STREAM_END(node_dtrace_connection_t* conn) {
112+
EVENT_DATA_DESCRIPTOR descriptors[4];
113+
ETW_WRITE_NET_CONNECTION(descriptors, conn);
114+
ETW_WRITE_EVENT(NODE_NET_STREAM_END_EVENT, descriptors);
115+
}
116+
117+
118+
void NODE_GC_START(GCType type, GCCallbackFlags flags) {
119+
EVENT_DATA_DESCRIPTOR descriptors[2];
120+
ETW_WRITE_GC(descriptors, type, flags);
121+
ETW_WRITE_EVENT(NODE_GC_START_EVENT, descriptors);
122+
}
123+
124+
125+
void NODE_GC_DONE(GCType type, GCCallbackFlags flags) {
126+
EVENT_DATA_DESCRIPTOR descriptors[2];
127+
ETW_WRITE_GC(descriptors, type, flags);
128+
ETW_WRITE_EVENT(NODE_GC_DONE_EVENT, descriptors);
129+
}
130+
131+
132+
bool NODE_HTTP_SERVER_REQUEST_ENABLED() { return events_enabled > 0; }
133+
bool NODE_HTTP_SERVER_RESPONSE_ENABLED() { return events_enabled > 0; }
134+
bool NODE_HTTP_CLIENT_REQUEST_ENABLED() { return events_enabled > 0; }
135+
bool NODE_HTTP_CLIENT_RESPONSE_ENABLED() { return events_enabled > 0; }
136+
bool NODE_NET_SERVER_CONNECTION_ENABLED() { return events_enabled > 0; }
137+
bool NODE_NET_STREAM_END_ENABLED() { return events_enabled > 0; }
138+
bool NODE_NET_SOCKET_READ_ENABLED() { return events_enabled > 0; }
139+
bool NODE_NET_SOCKET_WRITE_ENABLED() { return events_enabled > 0; }
140+
}
141+
#endif // SRC_ETW_INL_H_

src/node_win32_etw_provider.cc

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
#include "node_win32_etw_provider.h"
24+
#include "node_etw_provider.h"
25+
26+
namespace node {
27+
28+
using namespace v8;
29+
30+
HMODULE advapi;
31+
REGHANDLE node_provider;
32+
EventRegisterFunc event_register;
33+
EventUnregisterFunc event_unregister;
34+
EventWriteFunc event_write;
35+
int events_enabled;
36+
37+
// This callback is called by ETW when consumers of our provider
38+
// are enabled or disabled.
39+
// The callback is dispatched on ETW thread.
40+
void NTAPI etw_events_enable_callback(
41+
LPCGUID SourceId,
42+
ULONG IsEnabled,
43+
UCHAR Level,
44+
ULONGLONG MatchAnyKeyword,
45+
ULONGLONG MatchAllKeywords,
46+
PEVENT_FILTER_DESCRIPTOR FilterData,
47+
PVOID CallbackContext) {
48+
if (IsEnabled) {
49+
events_enabled++;
50+
} else {
51+
events_enabled--;
52+
}
53+
}
54+
55+
56+
void init_etw() {
57+
events_enabled = 0;
58+
59+
advapi = LoadLibrary("advapi32.dll");
60+
if (advapi) {
61+
event_register = (EventRegisterFunc)
62+
GetProcAddress(advapi, "EventRegister");
63+
event_unregister = (EventUnregisterFunc)
64+
GetProcAddress(advapi, "EventUnregister");
65+
event_write = (EventWriteFunc)GetProcAddress(advapi, "EventWrite");
66+
67+
if (event_register) {
68+
DWORD status = event_register(&NODE_ETW_PROVIDER,
69+
etw_events_enable_callback,
70+
NULL,
71+
&node_provider);
72+
assert(status == ERROR_SUCCESS);
73+
}
74+
}
75+
}
76+
77+
78+
void shutdown_etw() {
79+
if (advapi && event_unregister && node_provider) {
80+
event_unregister(node_provider);
81+
node_provider = 0;
82+
}
83+
84+
events_enabled = 0;
85+
86+
if (advapi) {
87+
FreeLibrary(advapi);
88+
advapi = NULL;
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)