Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Basic infrastructure for binder tracing (#27383)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Oct 28, 2019
1 parent b46c857 commit f55bc3a
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,25 @@ private void ActivityChanging(AsyncLocalValueChangedArgs<ActivityInfo?> args)
// currently we do nothing, as that seems better than setting to Guid.Emtpy.
}

// Assembly load runtime activity name
private const string AssemblyLoadName = "AssemblyLoad";

/// <summary>
/// Called by the runtime to start an assembly load activity
/// </summary>
private static void StartAssemblyLoad(ref Guid activityId, ref Guid relatedActivityId)
{
Instance.OnStart(NativeRuntimeEventSource.Log.Name, AssemblyLoadName, 0, ref activityId, ref relatedActivityId, EventActivityOptions.Recursive);
}

/// <summary>
/// Called by the runtime to stop an assembly load activity
/// </summary>
private static void StopAssemblyLoad(ref Guid activityId)
{
Instance.OnStop(NativeRuntimeEventSource.Log.Name, AssemblyLoadName, 0, ref activityId);
}

/// <summary>
/// Async local variables have the property that the are automatically copied whenever a task is created and used
/// while that task is running. Thus m_current 'flows' to any task that is caused by the current thread that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public partial class AssemblyLoadContext
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Assembly[] GetLoadedAssemblies();

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsTracingEnabled();

private Assembly InternalLoadFromPath(string? assemblyPath, string? nativeImagePath)
{
RuntimeAssembly? loadedAssembly = null;
Expand Down
4 changes: 4 additions & 0 deletions src/binder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(BINDER_COMMON_SOURCES
assemblyidentitycache.cpp
coreclrbindercommon.cpp
fusionassemblyname.cpp
bindertracing.cpp
)

set(BINDER_COMMON_HEADERS
Expand All @@ -38,6 +39,7 @@ set(BINDER_COMMON_HEADERS
inc/assemblyversion.hpp
inc/assemblyversion.inl
inc/bindertypes.hpp
inc/bindertracing.h
inc/bindinglog.hpp
inc/bindinglog.inl
inc/bindresult.hpp
Expand All @@ -64,11 +66,13 @@ set(BINDER_COMMON_HEADERS

set(BINDER_SOURCES
${BINDER_COMMON_SOURCES}
activitytracker.cpp
clrprivbinderassemblyloadcontext.cpp
)

set(BINDER_HEADERS
${BINDER_COMMON_HEADERS}
inc/activitytracker.h
inc/clrprivbinderassemblyloadcontext.h
inc/contextentry.hpp
)
Expand Down
39 changes: 39 additions & 0 deletions src/binder/activitytracker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ============================================================
//
// activitytracker.cpp
//


//
// Helpers for interaction with the managed ActivityTracker
//
// ============================================================

#include "common.h"
#include "activitytracker.h"

void ActivityTracker::Start(/*out*/ GUID *activityId, /*out*/ GUID *relatedActivityId)
{
GCX_COOP();

PREPARE_NONVIRTUAL_CALLSITE(METHOD__ACTIVITY_TRACKER__START_ASSEMBLY_LOAD);
DECLARE_ARGHOLDER_ARRAY(args, 2);
args[ARGNUM_0] = PTR_TO_ARGHOLDER(activityId);
args[ARGNUM_1] = PTR_TO_ARGHOLDER(relatedActivityId);

CALL_MANAGED_METHOD_NORET(args)
}

void ActivityTracker::Stop(/*out*/ GUID *activityId)
{
GCX_COOP();

PREPARE_NONVIRTUAL_CALLSITE(METHOD__ACTIVITY_TRACKER__STOP_ASSEMBLY_LOAD);
DECLARE_ARGHOLDER_ARRAY(args, 1);
args[ARGNUM_0] = PTR_TO_ARGHOLDER(activityId);

CALL_MANAGED_METHOD_NORET(args)
}
111 changes: 111 additions & 0 deletions src/binder/bindertracing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ============================================================
//
// bindertracing.cpp
//


//
// Implements helpers for binder tracing
//
// ============================================================

#include "common.h"
#include "bindertracing.h"

#include "activitytracker.h"

#ifdef FEATURE_EVENT_TRACE
#include "eventtracebase.h"
#endif // FEATURE_EVENT_TRACE

using namespace BINDER_SPACE;

namespace
{
void FireAssemblyLoadStart(const BinderTracing::AssemblyBindOperation::BindRequest &request)
{
#ifdef FEATURE_EVENT_TRACE
if (!EventEnabledAssemblyLoadStart())
return;

GUID activityId = GUID_NULL;
GUID relatedActivityId = GUID_NULL;
ActivityTracker::Start(&activityId, &relatedActivityId);

FireEtwAssemblyLoadStart(
GetClrInstanceId(),
request.AssemblyName,
request.AssemblyPath,
request.RequestingAssembly,
request.AssemblyLoadContext,
&activityId,
&relatedActivityId);
#endif // FEATURE_EVENT_TRACE
}

void FireAssemblyLoadStop(const BinderTracing::AssemblyBindOperation::BindRequest &request, bool success, const WCHAR *resultName, const WCHAR *resultPath, bool cached)
{
#ifdef FEATURE_EVENT_TRACE
if (!EventEnabledAssemblyLoadStop())
return;

GUID activityId = GUID_NULL;
ActivityTracker::Stop(&activityId);

FireEtwAssemblyLoadStop(
GetClrInstanceId(),
request.AssemblyName,
request.AssemblyPath,
request.RequestingAssembly,
request.AssemblyLoadContext,
success,
resultName,
resultPath,
cached,
&activityId);
#endif // FEATURE_EVENT_TRACE
}
}

bool BinderTracing::IsEnabled()
{
#ifdef FEATURE_EVENT_TRACE
// Just check for the AssemblyLoadStart event being enabled.
return EventEnabledAssemblyLoadStart();
#endif // FEATURE_EVENT_TRACE
return false;
}

namespace BinderTracing
{
AssemblyBindOperation::AssemblyBindOperation(AssemblySpec *assemblySpec)
: m_bindRequest { assemblySpec }
, m_success { false }
, m_cached { false }
{
_ASSERTE(assemblySpec != nullptr);

// ActivityTracker or EventSource may have triggered the system satellite load.
// Don't track system satellite binding to avoid potential infinite recursion.
m_trackingBind = BinderTracing::IsEnabled() && !m_bindRequest.AssemblySpec->IsMscorlibSatellite();
if (m_trackingBind)
{
m_bindRequest.AssemblySpec->GetFileOrDisplayName(ASM_DISPLAYF_VERSION | ASM_DISPLAYF_CULTURE | ASM_DISPLAYF_PUBLIC_KEY_TOKEN, m_bindRequest.AssemblyName);
FireAssemblyLoadStart(m_bindRequest);
}
}

AssemblyBindOperation::~AssemblyBindOperation()
{
if (m_trackingBind)
FireAssemblyLoadStop(m_bindRequest, m_success, m_resultName.GetUnicode(), m_resultPath.GetUnicode(), m_cached);
}

void AssemblyBindOperation::SetResult(PEAssembly *assembly)
{
m_success = assembly != nullptr;
}
}
17 changes: 17 additions & 0 deletions src/binder/inc/activitytracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// activitytracker.h
//

#ifndef __ACTIVITY_TRACKER_H__
#define __ACTIVITY_TRACKER_H__

namespace ActivityTracker
{
void Start(/*out*/ GUID *activityId, /*out*/ GUID *relatedActivityId);
void Stop(/*out*/ GUID *activityId);
};

#endif // __ACTIVITY_TRACKER_H__
50 changes: 50 additions & 0 deletions src/binder/inc/bindertracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// bindertracing.h
//

#ifndef __BINDER_TRACING_H__
#define __BINDER_TRACING_H__

class AssemblySpec;
class PEAssembly;

namespace BinderTracing
{
bool IsEnabled();

// If tracing is enabled, this class fires an assembly bind start event on construction
// and the corresponding stop event on destruction
class AssemblyBindOperation
{
public:
// This class assumes the assembly spec will have a longer lifetime than itself
AssemblyBindOperation(AssemblySpec *assemblySpec);
~AssemblyBindOperation();

void SetResult(PEAssembly *assembly);

struct BindRequest
{
AssemblySpec *AssemblySpec;
SString AssemblyName;
SString AssemblyPath;
SString RequestingAssembly;
SString AssemblyLoadContext;
};

private:
BindRequest m_bindRequest;

bool m_trackingBind;

bool m_success;
SString m_resultName;
SString m_resultPath;
bool m_cached;
};
};

#endif // __BINDER_TRACING_H__
Loading

0 comments on commit f55bc3a

Please sign in to comment.