Skip to content

Commit

Permalink
[MERGE #4314 @MSLaguana] Add support for emitting ETW events to LTTng
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MSLaguana committed Dec 7, 2017
2 parents 81011f8 + 41d2de9 commit 88ebe68
Show file tree
Hide file tree
Showing 21 changed files with 841 additions and 95 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -491,5 +491,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 ($ENV{TARGET_PATH}/lttng ${CMAKE_CURRENT_BINARY_DIR}/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
18 changes: 17 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 @@ -516,6 +523,15 @@ else
exit 1
fi
fi
export TARGET_PATH

if [[ $HAS_LTTNG == 1 ]]; then
CHAKRACORE_ROOT=`dirname $0`
python $CHAKRACORE_ROOT/tools/lttng.py --man $CHAKRACORE_ROOT/manifests/Microsoft-Scripting-Chakra-Instrumentation.man --intermediate $TARGET_PATH/intermediate
mkdir -p $TARGET_PATH/lttng
(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/
fi


BUILD_DIRECTORY="${TARGET_PATH}/${BUILD_TYPE:0}"
echo "Build path: ${BUILD_DIRECTORY}"
Expand Down Expand Up @@ -603,7 +619,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
12 changes: 12 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,16 @@ add_library (ChakraCoreStatic STATIC
$<TARGET_OBJECTS:Chakra.Parser>
${wasm_objects}
${wabt_objects}
${lttng_objects}
)

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

if(CC_TARGET_OS_OSX)
target_link_libraries(ChakraCoreStatic
"-framework CoreFoundation"
Expand Down
4 changes: 3 additions & 1 deletion 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 @@ -64,4 +65,5 @@ void EtwTraceCore::UnRegister()
}
}

#endif
#endif // !ENABLE_JS_LTTNG
#endif // ENABLE_JS_ETW
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 // ENABLE_JS_LTTNG

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

#ifdef ENABLE_JS_ETW
#if defined(ENABLE_JS_ETW) && ! defined(ENABLE_JS_LTTNG)
// LTTng has no concept of EventActivityIdControl
// 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 +6537,8 @@ RecyclerParallelThread::StaticThreadProc(LPVOID lpParameter)
dllHandle = NULL;
}
#endif
#ifdef ENABLE_JS_ETW
#if defined(ENABLE_JS_ETW) && ! defined(ENABLE_JS_LTTNG)
// LTTng has no concept of EventActivityIdControl
// 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 @@ -8776,4 +8778,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
4 changes: 2 additions & 2 deletions lib/Jsrt/JsrtSourceHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace Js

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

Expand Down Expand Up @@ -285,7 +285,7 @@ namespace Js

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

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
4 changes: 2 additions & 2 deletions lib/Runtime/Base/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6159,11 +6159,11 @@ void ScriptContext::RegisterPrototypeChainEnsuredToHaveOnlyWritableDataPropertie

if (emitV2AsyncStackEvent)
{
JS_ETW(EventWriteJSCRIPT_ASYNCCAUSALITY_STACKTRACE_V2(operationID, frameCount, nameBufferLength, sizeof(StackFrameInfo), &stackFrames.Item(0), nameBufferString));
JS_ETW(EventWriteJSCRIPT_ASYNCCAUSALITY_STACKTRACE_V2(operationID, frameCount, nameBufferLength, nameBufferString, sizeof(StackFrameInfo), &stackFrames.Item(0)));
}
else
{
JS_ETW(EventWriteJSCRIPT_STACKTRACE(operationID, frameCount, nameBufferLength, sizeof(StackFrameInfo), &stackFrames.Item(0), nameBufferString));
JS_ETW(EventWriteJSCRIPT_STACKTRACE(operationID, frameCount, nameBufferLength, nameBufferString, sizeof(StackFrameInfo), &stackFrames.Item(0)));
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Runtime/Library/GlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,8 +1621,7 @@ namespace Js
return function->GetScriptContext()->GetLibrary()->GetUndefined();
}

Js::JavascriptString* jsString = Js::JavascriptConversion::ToString(args[1], function->GetScriptContext());
PlatformAgnostic::EventTrace::FireGenericEventTrace(jsString->GetSz());
JS_ETW(EventWriteJSCRIPT_INTERNAL_GENERIC_EVENT(Js::JavascriptConversion::ToString(args[1], function->GetScriptContext())->GetSz()));
return function->GetScriptContext()->GetLibrary()->GetUndefined();
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@
<ClCompile Include="$(MSBuildThisFileDirectory)Platform\Windows\SystemInfo.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Platform\Windows\Thread.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Platform\Common\UnicodeText.Common.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Platform\Windows\EventTrace.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Platform\Windows\PerfTrace.cpp" />
</ItemGroup>
<ItemGroup Condition="'$(IntlICU)'=='true'">
<ClCompile Include="$(MSBuildThisFileDirectory)Platform\Common\Intl.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChakraPlatform.h" />
<ClInclude Include="EventTrace.h" />
<ClInclude Include="PerfTrace.h" />
<ClInclude Include="RuntimePlatformAgnosticPch.h" />
<ClInclude Include="UnicodeText.h" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="Platform\Windows\UnicodeText.cpp">
<Filter>Platform\Windows</Filter>
</ClCompile>
<ClCompile Include="Platform\Windows\EventTrace.cpp">
<Filter>Platform\Windows</Filter>
</ClCompile>
<ClCompile Include="Platform\Windows\DaylightHelper.cpp">
<Filter>Platform\Windows</Filter>
</ClCompile>
Expand Down Expand Up @@ -51,14 +48,11 @@
</ClInclude>
<ClInclude Include="ChakraPlatform.h" />
<ClInclude Include="RuntimePlatformAgnosticPch.h" />
<ClInclude Include="EventTrace.h">
<Filter>Interfaces</Filter>
</ClInclude>
<ClInclude Include="PerfTrace.h">
<Filter>Interfaces</Filter>
</ClInclude>
<ClInclude Include="IPlatformAgnosticResource.h">
<Filter>Interfaces</Filter>
</ClInclude>
</ItemGroup>
</Project>
</Project>
1 change: 0 additions & 1 deletion lib/Runtime/PlatformAgnostic/ChakraPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma once

#include "UnicodeText.h"
#include "EventTrace.h"

// Configure whether we configure a signal handler
// to produce perf-<pid>.map files
Expand Down
15 changes: 0 additions & 15 deletions lib/Runtime/PlatformAgnostic/EventTrace.h

This file was deleted.

1 change: 0 additions & 1 deletion lib/Runtime/PlatformAgnostic/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ set(PL_SOURCE_FILES
Common/UnicodeText.Common.cpp
Common/HiResTimer.cpp
Common/DateTime.cpp
Linux/EventTrace.cpp
Linux/UnicodeText.ICU.cpp
Linux/NumbersUtility.cpp
Linux/Thread.cpp
Expand Down
18 changes: 0 additions & 18 deletions lib/Runtime/PlatformAgnostic/Platform/Linux/EventTrace.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions lib/Runtime/PlatformAgnostic/Platform/Windows/EventTrace.cpp

This file was deleted.

Loading

0 comments on commit 88ebe68

Please sign in to comment.