1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #include "node_dtrace.h"
23 #include "node_win32_etw_provider.h"
24 #include "node_etw_provider.h"
25 #include "node_win32_etw_provider-inl.h"
29 using v8::JitCodeEvent;
33 REGHANDLE node_provider;
34 EventRegisterFunc event_register;
35 EventUnregisterFunc event_unregister;
36 EventWriteFunc event_write;
38 static uv_async_t dispatch_etw_events_change_async;
41 char prefix[32 - sizeof(size_t)];
45 // The v8 CODE_ADDED event name has a prefix indicating the type of event.
46 // Many of these are internal to v8.
47 // The trace_codes array specifies which types are written.
48 struct v8tags trace_codes[] = {
49 #define MAKE_V8TAG(s) { s, sizeof(s) - 1 }
50 MAKE_V8TAG("LazyCompile:"),
51 MAKE_V8TAG("Script:"),
52 MAKE_V8TAG("Function:"),
53 MAKE_V8TAG("RegExp:"),
58 /* Below are some code prefixes which are not being written.
66 * "CallPreMonomorphic:"
72 // v8 sometimes puts a '*' or '~' in front of the name.
73 #define V8_MARKER1 '*'
74 #define V8_MARKER2 '~'
77 // If prefix is not in filtered list return -1,
78 // else return length of prefix and marker.
79 int FilterCodeEvents(const char* name, size_t len) {
80 for (int i = 0; i < ARRAY_SIZE(trace_codes); i++) {
81 size_t prelen = trace_codes[i].prelen;
83 if (strncmp(name, trace_codes[i].prefix, prelen) == 0) {
84 if (name[prelen] == V8_MARKER1 || name[prelen] == V8_MARKER2)
94 // callback from V8 module passes symbol and address info for stack walk
95 void CodeAddressNotification(const JitCodeEvent* jevent) {
97 if (NODE_V8SYMBOL_ENABLED()) {
98 switch (jevent->type) {
99 case JitCodeEvent::CODE_ADDED:
100 pre_offset = FilterCodeEvents(jevent->name.str, jevent->name.len);
101 if (pre_offset >= 0) {
102 // skip over prefix and marker
103 NODE_V8SYMBOL_ADD(jevent->name.str + pre_offset,
104 jevent->name.len - pre_offset,
109 case JitCodeEvent::CODE_REMOVED:
110 NODE_V8SYMBOL_REMOVE(jevent->code_start, 0);
112 case JitCodeEvent::CODE_MOVED:
113 NODE_V8SYMBOL_MOVE(jevent->code_start, jevent->new_code_start);
122 // Call v8 to enable or disable code event callbacks.
123 // Must be on default thread to do this.
124 // Note: It is possible to call v8 from ETW thread, but then
125 // event callbacks are received in the same thread. Attempts
126 // to write ETW events in this thread will fail.
127 void etw_events_change_async(uv_async_t* handle) {
128 if (events_enabled > 0) {
129 NODE_V8SYMBOL_RESET();
130 v8::Isolate::GetCurrent()->SetJitCodeEventHandler(
131 v8::kJitCodeEventEnumExisting,
132 CodeAddressNotification);
134 v8::Isolate::GetCurrent()->SetJitCodeEventHandler(
135 v8::kJitCodeEventDefault,
141 // This callback is called by ETW when consumers of our provider
142 // are enabled or disabled.
143 // The callback is dispatched on ETW thread.
144 // Before calling into V8 to enable code events, switch to default thread.
145 void NTAPI etw_events_enable_callback(
149 ULONGLONG MatchAnyKeyword,
150 ULONGLONG MatchAllKeywords,
151 PEVENT_FILTER_DESCRIPTOR FilterData,
152 PVOID CallbackContext) {
155 if (events_enabled == 1) {
156 uv_async_send(&dispatch_etw_events_change_async);
160 if (events_enabled == 0) {
161 uv_async_send(&dispatch_etw_events_change_async);
170 advapi = LoadLibraryW(L"advapi32.dll");
172 event_register = (EventRegisterFunc)
173 GetProcAddress(advapi, "EventRegister");
174 event_unregister = (EventUnregisterFunc)
175 GetProcAddress(advapi, "EventUnregister");
176 event_write = (EventWriteFunc)GetProcAddress(advapi, "EventWrite");
178 // create async object used to invoke main thread from callback
179 uv_async_init(uv_default_loop(),
180 &dispatch_etw_events_change_async,
181 etw_events_change_async);
182 uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_etw_events_change_async));
184 if (event_register) {
185 DWORD status = event_register(&NODE_ETW_PROVIDER,
186 etw_events_enable_callback,
189 CHECK_EQ(status, ERROR_SUCCESS);
195 void shutdown_etw() {
196 if (advapi && event_unregister && node_provider) {
197 event_unregister(node_provider);
202 v8::Isolate::GetCurrent()->SetJitCodeEventHandler(
203 v8::kJitCodeEventDefault,