Skip to content

Commit 88ebe68

Browse files
committed
[MERGE #4314 @MSLaguana] Add support for emitting ETW events to LTTng
Merge pull request #4314 from MSLaguana:lttng We define a number of ETW events on windows that we can emit for diagnostics purposes. With these changes, we support generating LTTng bindings for these same events so the same diagnostics approach can be used on linux and osx. The script that generates the LTTng bindings from the ETW manifest is based on a similar script used by the dotnet team (see https://github.com/dotnet/coreclr/blob/master/src/scripts/genXplatLttng.py), but I have attempted to simplify it and adapt it for our purposes.
2 parents 81011f8 + 41d2de9 commit 88ebe68

21 files changed

+841
-95
lines changed

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,5 +491,21 @@ add_definitions(
491491
-DNO_PAL_MINMAX
492492
-DPAL_STDCPP_COMPAT
493493
)
494+
495+
if (ENABLE_JS_LTTNG_SH)
496+
unset(ENABLE_JS_LTTNG_SH CACHE)
497+
include_directories (
498+
${CMAKE_CURRENT_SOURCE_DIR}/out/lttng
499+
)
500+
add_subdirectory ($ENV{TARGET_PATH}/lttng ${CMAKE_CURRENT_BINARY_DIR}/lttng)
501+
502+
add_definitions(
503+
-DENABLE_JS_ETW
504+
-DENABLE_JS_LTTNG
505+
)
506+
set(USE_LTTNG "1")
507+
endif()
508+
494509
add_subdirectory (lib)
510+
495511
add_subdirectory (bin)

bin/GCStress/StubExternalApi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void ConfigParserAPI::DisplayInitialOutput(__in LPWSTR moduleName)
2121
Output::Print(_u("INIT: DLL Path : %s\n"), moduleName);
2222
}
2323

24-
#ifdef ENABLE_JS_ETW
24+
#if defined(ENABLE_JS_ETW) && !defined(ENABLE_JS_LTTNG)
2525
void EtwCallbackApi::OnSessionChange(ULONG /* controlCode */, PVOID /* callbackContext */)
2626
{
2727
// Does nothing

build.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ PRINT_USAGE() {
5252
echo " --libs-only Do not build CH and GCStress"
5353
echo " --lto Enables LLVM Full LTO"
5454
echo " --lto-thin Enables LLVM Thin LTO - xcode 8+ or clang 3.9+"
55+
echo " --lttng Enables LTTng support for ETW events"
5556
echo " --static Build as static library. Default: shared library"
5657
echo " --sanitize=CHECKS Build with clang -fsanitize checks,"
5758
echo " e.g. undefined,signed-integer-overflow."
@@ -104,6 +105,7 @@ OS_LINUX=0
104105
OS_APT_GET=0
105106
OS_UNIX=0
106107
LTO=""
108+
LTTNG=""
107109
TARGET_OS=""
108110
ENABLE_CC_XPLAT_TRACE=""
109111
WB_CHECK=
@@ -230,6 +232,11 @@ while [[ $# -gt 0 ]]; do
230232
HAS_LTO=1
231233
;;
232234

235+
--lttng)
236+
LTTNG="-DENABLE_JS_LTTNG_SH=1"
237+
HAS_LTTNG=1
238+
;;
239+
233240
-n | --ninja)
234241
CMAKE_GEN="-G Ninja"
235242
MAKE=ninja
@@ -516,6 +523,15 @@ else
516523
exit 1
517524
fi
518525
fi
526+
export TARGET_PATH
527+
528+
if [[ $HAS_LTTNG == 1 ]]; then
529+
CHAKRACORE_ROOT=`dirname $0`
530+
python $CHAKRACORE_ROOT/tools/lttng.py --man $CHAKRACORE_ROOT/manifests/Microsoft-Scripting-Chakra-Instrumentation.man --intermediate $TARGET_PATH/intermediate
531+
mkdir -p $TARGET_PATH/lttng
532+
(diff -q $TARGET_PATH/intermediate/lttng/jscriptEtw.h $TARGET_PATH/lttng/jscriptEtw.h && echo "jscriptEtw.h up to date; skipping") || cp $TARGET_PATH/intermediate/lttng/* $TARGET_PATH/lttng/
533+
fi
534+
519535

520536
BUILD_DIRECTORY="${TARGET_PATH}/${BUILD_TYPE:0}"
521537
echo "Build path: ${BUILD_DIRECTORY}"
@@ -603,7 +619,7 @@ fi
603619

604620
echo Generating $BUILD_TYPE makefiles
605621
echo $EXTRA_DEFINES
606-
cmake $CMAKE_GEN $CC_PREFIX $ICU_PATH $LTO $STATIC_LIBRARY $ARCH $TARGET_OS \
622+
cmake $CMAKE_GEN $CC_PREFIX $ICU_PATH $LTO $LTTNG $STATIC_LIBRARY $ARCH $TARGET_OS \
607623
$ENABLE_CC_XPLAT_TRACE $EXTRA_DEFINES -DCMAKE_BUILD_TYPE=$BUILD_TYPE $SANITIZE $NO_JIT $INTL_ICU \
608624
$WITHOUT_FEATURES $WB_FLAG $WB_ARGS $CMAKE_EXPORT_COMPILE_COMMANDS $LIBS_ONLY_BUILD\
609625
$VALGRIND $BUILD_RELATIVE_DIRECTORY

lib/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if(CAN_BUILD_WABT)
1414
set(wabt_includes ${CHAKRACORE_SOURCE_DIR}/lib/wabt)
1515
endif()
1616

17+
if (USE_LTTNG)
18+
set(lttng_objects $<TARGET_OBJECTS:Chakra.LTTng>)
19+
endif()
20+
1721
add_library (ChakraCoreStatic STATIC
1822
ChakraCoreStatic.cpp
1923
$<TARGET_OBJECTS:Chakra.Pal>
@@ -38,8 +42,16 @@ add_library (ChakraCoreStatic STATIC
3842
$<TARGET_OBJECTS:Chakra.Parser>
3943
${wasm_objects}
4044
${wabt_objects}
45+
${lttng_objects}
4146
)
4247

48+
if(USE_LTTNG)
49+
target_link_libraries(ChakraCoreStatic
50+
-llttng-ust
51+
-ldl
52+
)
53+
endif()
54+
4355
if(CC_TARGET_OS_OSX)
4456
target_link_libraries(ChakraCoreStatic
4557
"-framework CoreFoundation"

lib/Common/Core/EtwTraceCore.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Core/EtwTraceCore.h"
77

88
#ifdef ENABLE_JS_ETW
9+
#ifndef ENABLE_JS_LTTNG
910
extern "C" {
1011
ETW_INLINE
1112
VOID EtwCallback(
@@ -64,4 +65,5 @@ void EtwTraceCore::UnRegister()
6465
}
6566
}
6667

67-
#endif
68+
#endif // !ENABLE_JS_LTTNG
69+
#endif // ENABLE_JS_ETW

lib/Common/Core/EtwTraceCore.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
#define JS_ETW(s) s
4848
#define IS_JS_ETW(s) s
4949

50+
#ifdef ENABLE_JS_LTTNG
51+
#include "jscriptEtw.h"
52+
53+
#else
5054
// C-style callback
5155
extern "C" {
5256
void EtwCallback(
@@ -87,6 +91,7 @@ class EtwTraceCore
8791

8892
static bool s_registered;
8993
};
94+
#endif // ENABLE_JS_LTTNG
9095

9196
#else
9297
#define GCETW(e, ...)

lib/Common/Memory/Recycler.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5968,7 +5968,8 @@ Recycler::ThreadProc()
59685968
}
59695969
#endif
59705970

5971-
#ifdef ENABLE_JS_ETW
5971+
#if defined(ENABLE_JS_ETW) && ! defined(ENABLE_JS_LTTNG)
5972+
// LTTng has no concept of EventActivityIdControl
59725973
// Create an ETW ActivityId for this thread, to help tools correlate ETW events we generate
59735974
GUID activityId = { 0 };
59745975
auto eventActivityIdControlResult = EventActivityIdControl(EVENT_ACTIVITY_CTRL_CREATE_SET_ID, &activityId);
@@ -6536,7 +6537,8 @@ RecyclerParallelThread::StaticThreadProc(LPVOID lpParameter)
65366537
dllHandle = NULL;
65376538
}
65386539
#endif
6539-
#ifdef ENABLE_JS_ETW
6540+
#if defined(ENABLE_JS_ETW) && ! defined(ENABLE_JS_LTTNG)
6541+
// LTTng has no concept of EventActivityIdControl
65406542
// Create an ETW ActivityId for this thread, to help tools correlate ETW events we generate
65416543
GUID activityId = { 0 };
65426544
auto eventActivityIdControlResult = EventActivityIdControl(EVENT_ACTIVITY_CTRL_CREATE_SET_ID, &activityId);
@@ -8776,4 +8778,4 @@ template char* Recycler::AllocZeroWithAttributesInlined<RecyclerVisitedHostTrace
87768778
template char* Recycler::AllocZeroWithAttributesInlined<RecyclerVisitedHostFinalizableBits, /* nothrow = */true>(size_t);
87778779
template char* Recycler::AllocZeroWithAttributesInlined<RecyclerVisitedHostTracedBits, /* nothrow = */true>(size_t);
87788780
template char* Recycler::AllocZeroWithAttributesInlined<LeafBit, /* nothrow = */true>(size_t);
8779-
#endif
8781+
#endif

lib/Jsrt/JsrtHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void JsrtCallbackState::ObjectBeforeCallectCallbackWrapper(JsObjectBeforeCollect
143143
ConfigParser::ParseOnModuleLoad(parser, mod);
144144
}
145145

146-
#ifdef ENABLE_JS_ETW
146+
#if defined(ENABLE_JS_ETW) && !defined(ENABLE_JS_LTTNG)
147147
EtwTrace::Register();
148148
#endif
149149
#ifdef VTUNE_PROFILING

lib/Jsrt/JsrtSourceHolder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace Js
113113

114114
#if ENABLE_DEBUG_CONFIG_OPTIONS
115115
AssertMsg(reasonString != nullptr, "Reason string for why we are mapping the source was not provided.");
116-
JS_ETW(EventWriteJSCRIPT_SOURCEMAPPING((uint32)wcslen(reasonString), reasonString, (ushort)requestedFor));
116+
JS_ETW(EventWriteJSCRIPT_SOURCEMAPPING(reasonString, (ushort)requestedFor));
117117
#endif
118118
}
119119

@@ -285,7 +285,7 @@ namespace Js
285285

286286
#if ENABLE_DEBUG_CONFIG_OPTIONS
287287
AssertMsg(reasonString != nullptr, "Reason string for why we are mapping the source was not provided.");
288-
JS_ETW(EventWriteJSCRIPT_SOURCEMAPPING((uint32)wcslen(reasonString), reasonString, (ushort)requestedFor));
288+
JS_ETW(EventWriteJSCRIPT_SOURCEMAPPING(reasonString, (ushort)requestedFor));
289289
#endif
290290
}
291291

lib/Runtime/Base/EtwTrace.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using namespace Js;
1212

13+
#ifndef ENABLE_JS_LTTNG
1314
//
1415
// This C style callback is invoked by ETW when a trace session is started/stopped
1516
// by an ETW controller for the Jscript and MSHTML providers.
@@ -47,14 +48,17 @@ void EtwCallbackApi::OnSessionChange(ULONG controlCode, PVOID callbackContext)
4748
}
4849
}
4950
}
51+
#endif
5052

5153
//
5254
// Registers the ETW provider - this is usually done on Jscript DLL load
5355
// After registration, we will receive callbacks when ETW tracing is enabled/disabled.
5456
//
5557
void EtwTrace::Register()
5658
{
59+
#ifndef ENABLE_JS_LTTNG
5760
EtwTraceCore::Register();
61+
#endif
5862

5963
#ifdef TEST_ETW_EVENTS
6064
TestEtwEventSink::Load();
@@ -66,8 +70,10 @@ void EtwTrace::Register()
6670
//
6771
void EtwTrace::UnRegister()
6872
{
73+
#ifndef ENABLE_JS_LTTNG
6974
EtwTraceCore::UnRegister();
70-
75+
#endif
76+
7177
#ifdef TEST_ETW_EVENTS
7278
TestEtwEventSink::Unload();
7379
#endif

0 commit comments

Comments
 (0)