Skip to content

Commit

Permalink
Enable support for ETW events on xplat via LTTng
Browse files Browse the repository at this point in the history
Running build.sh with the --lttng flag will generate ETW-like event emitting
functions that produce LTTng events. This means that you can get improved
diagnostics on non-windows platforms like so:

```bash
./build.sh --lttng # + whatever other flags
lttng create mySession
lttng enable-event -u "JScript:*" # or more specific events specified in the ETW manifest
lttng start
out/Release/ch script.js
lttng stop
lttng view
```
  • Loading branch information
MSLaguana committed Nov 22, 2017
1 parent 25daa12 commit 28ba2f0
Show file tree
Hide file tree
Showing 10 changed files with 806 additions and 7 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,21 @@ add_definitions(
-DNO_PAL_MINMAX
-DPAL_STDCPP_COMPAT
)

if (ENABLE_JS_LTTNG_SH)
unset(ENABLE_JS_LTTNG_SH CACHE)
include_directories (
${CMAKE_CURRENT_SOURCE_DIR}/out/lttng
)
add_subdirectory (out/lttng)

add_definitions(
-DENABLE_JS_ETW
-DENABLE_JS_LTTNG
)
set(USE_LTTNG "1")
endif()

add_subdirectory (lib)

add_subdirectory (bin)
2 changes: 1 addition & 1 deletion bin/GCStress/StubExternalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void ConfigParserAPI::DisplayInitialOutput(__in LPWSTR moduleName)
Output::Print(_u("INIT: DLL Path : %s\n"), moduleName);
}

#ifdef ENABLE_JS_ETW
#if defined(ENABLE_JS_ETW) && !defined(ENABLE_JS_LTTNG)
void EtwCallbackApi::OnSessionChange(ULONG /* controlCode */, PVOID /* callbackContext */)
{
// Does nothing
Expand Down
15 changes: 14 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ PRINT_USAGE() {
echo " --libs-only Do not build CH and GCStress"
echo " --lto Enables LLVM Full LTO"
echo " --lto-thin Enables LLVM Thin LTO - xcode 8+ or clang 3.9+"
echo " --lttng Enables LTTng support for ETW events"
echo " --static Build as static library. Default: shared library"
echo " --sanitize=CHECKS Build with clang -fsanitize checks,"
echo " e.g. undefined,signed-integer-overflow."
Expand Down Expand Up @@ -104,6 +105,7 @@ OS_LINUX=0
OS_APT_GET=0
OS_UNIX=0
LTO=""
LTTNG=""
TARGET_OS=""
ENABLE_CC_XPLAT_TRACE=""
WB_CHECK=
Expand Down Expand Up @@ -230,6 +232,11 @@ while [[ $# -gt 0 ]]; do
HAS_LTO=1
;;

--lttng)
LTTNG="-DENABLE_JS_LTTNG_SH=1"
HAS_LTTNG=1
;;

-n | --ninja)
CMAKE_GEN="-G Ninja"
MAKE=ninja
Expand Down Expand Up @@ -418,6 +425,12 @@ if [[ ${#_VERBOSE} > 0 ]]; then
echo ""
fi

if [[ $HAS_LTTNG == 1 ]]; then
python tools/lttng.py --man `pwd`/manifests/Microsoft-Scripting-Chakra-Instrumentation.man --intermediate `pwd`/out/intermediate
mkdir -p `pwd`/out/lttng
diff -q `pwd`/out/intermediate/lttng/jscriptEtw.h `pwd`/out/lttng/jscriptEtw.h || cp `pwd`/out/intermediate/lttng/* `pwd`/out/lttng/
fi

# if LTO build is enabled and cc-toolchain/clang was compiled, use it instead
if [[ $HAS_LTO == 1 ]]; then
if [[ -f "${CHAKRACORE_DIR}/cc-toolchain/build/bin/clang++" ]]; then
Expand Down Expand Up @@ -602,7 +615,7 @@ fi

echo Generating $BUILD_TYPE makefiles
echo $EXTRA_DEFINES
cmake $CMAKE_GEN $CC_PREFIX $ICU_PATH $LTO $STATIC_LIBRARY $ARCH $TARGET_OS \
cmake $CMAKE_GEN $CC_PREFIX $ICU_PATH $LTO $LTTNG $STATIC_LIBRARY $ARCH $TARGET_OS \
$ENABLE_CC_XPLAT_TRACE $EXTRA_DEFINES -DCMAKE_BUILD_TYPE=$BUILD_TYPE $SANITIZE $NO_JIT $INTL_ICU \
$WITHOUT_FEATURES $WB_FLAG $WB_ARGS $CMAKE_EXPORT_COMPILE_COMMANDS $LIBS_ONLY_BUILD\
$VALGRIND $BUILD_RELATIVE_DIRECTORY
Expand Down
11 changes: 11 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ if(CAN_BUILD_WABT)
set(wabt_includes ${CHAKRACORE_SOURCE_DIR}/lib/wabt)
endif()

if (USE_LTTNG)
set(lttng_objects $<TARGET_OBJECTS:Chakra.LTTng>)
endif()

add_library (ChakraCoreStatic STATIC
ChakraCoreStatic.cpp
$<TARGET_OBJECTS:Chakra.Pal>
Expand All @@ -38,8 +42,15 @@ add_library (ChakraCoreStatic STATIC
$<TARGET_OBJECTS:Chakra.Parser>
${wasm_objects}
${wabt_objects}
${lttng_objects}
)

if(USE_LTTNG)
target_link_libraries(ChakraCoreStatic
-llttng-ust
)
endif()

if(CC_TARGET_OS_OSX)
target_link_libraries(ChakraCoreStatic
"-framework CoreFoundation"
Expand Down
2 changes: 2 additions & 0 deletions lib/Common/Core/EtwTraceCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Core/EtwTraceCore.h"

#ifdef ENABLE_JS_ETW
#ifndef ENABLE_JS_LTTNG
extern "C" {
ETW_INLINE
VOID EtwCallback(
Expand Down Expand Up @@ -65,3 +66,4 @@ void EtwTraceCore::UnRegister()
}

#endif
#endif
5 changes: 5 additions & 0 deletions lib/Common/Core/EtwTraceCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
#define JS_ETW(s) s
#define IS_JS_ETW(s) s

#ifdef ENABLE_JS_LTTNG
#include "jscriptEtw.h"

#else
// C-style callback
extern "C" {
void EtwCallback(
Expand Down Expand Up @@ -87,6 +91,7 @@ class EtwTraceCore

static bool s_registered;
};
#endif

#else
#define GCETW(e, ...)
Expand Down
6 changes: 3 additions & 3 deletions lib/Common/Memory/Recycler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5968,7 +5968,7 @@ Recycler::ThreadProc()
}
#endif

#ifdef ENABLE_JS_ETW
#if defined(ENABLE_JS_ETW) && ! defined(ENABLE_JS_LTTNG)
// Create an ETW ActivityId for this thread, to help tools correlate ETW events we generate
GUID activityId = { 0 };
auto eventActivityIdControlResult = EventActivityIdControl(EVENT_ACTIVITY_CTRL_CREATE_SET_ID, &activityId);
Expand Down Expand Up @@ -6536,7 +6536,7 @@ RecyclerParallelThread::StaticThreadProc(LPVOID lpParameter)
dllHandle = NULL;
}
#endif
#ifdef ENABLE_JS_ETW
#if defined(ENABLE_JS_ETW) && ! defined(ENABLE_JS_LTTNG)
// Create an ETW ActivityId for this thread, to help tools correlate ETW events we generate
GUID activityId = { 0 };
auto eventActivityIdControlResult = EventActivityIdControl(EVENT_ACTIVITY_CTRL_CREATE_SET_ID, &activityId);
Expand Down Expand Up @@ -8757,4 +8757,4 @@ template char* Recycler::AllocZeroWithAttributesInlined<RecyclerVisitedHostTrace
template char* Recycler::AllocZeroWithAttributesInlined<RecyclerVisitedHostFinalizableBits, /* nothrow = */true>(size_t);
template char* Recycler::AllocZeroWithAttributesInlined<RecyclerVisitedHostTracedBits, /* nothrow = */true>(size_t);
template char* Recycler::AllocZeroWithAttributesInlined<LeafBit, /* nothrow = */true>(size_t);
#endif
#endif
2 changes: 1 addition & 1 deletion lib/Jsrt/JsrtHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void JsrtCallbackState::ObjectBeforeCallectCallbackWrapper(JsObjectBeforeCollect
ConfigParser::ParseOnModuleLoad(parser, mod);
}

#ifdef ENABLE_JS_ETW
#if defined(ENABLE_JS_ETW) && !defined(ENABLE_JS_LTTNG)
EtwTrace::Register();
#endif
#ifdef VTUNE_PROFILING
Expand Down
8 changes: 7 additions & 1 deletion lib/Runtime/Base/EtwTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

using namespace Js;

#ifndef ENABLE_JS_LTTNG
//
// This C style callback is invoked by ETW when a trace session is started/stopped
// by an ETW controller for the Jscript and MSHTML providers.
Expand Down Expand Up @@ -47,14 +48,17 @@ void EtwCallbackApi::OnSessionChange(ULONG controlCode, PVOID callbackContext)
}
}
}
#endif

//
// Registers the ETW provider - this is usually done on Jscript DLL load
// After registration, we will receive callbacks when ETW tracing is enabled/disabled.
//
void EtwTrace::Register()
{
#ifndef ENABLE_JS_LTTNG
EtwTraceCore::Register();
#endif

#ifdef TEST_ETW_EVENTS
TestEtwEventSink::Load();
Expand All @@ -66,8 +70,10 @@ void EtwTrace::Register()
//
void EtwTrace::UnRegister()
{
#ifndef ENABLE_JS_LTTNG
EtwTraceCore::UnRegister();

#endif

#ifdef TEST_ETW_EVENTS
TestEtwEventSink::Unload();
#endif
Expand Down
Loading

0 comments on commit 28ba2f0

Please sign in to comment.