Skip to content

Commit

Permalink
Rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nrcventura committed Nov 17, 2023
1 parent fd0cc1e commit 4595082
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace NewRelic.Agent.Core
{
/// <summary>
/// Cache of the Agent methods that can be invoked by the byte-code injected by the profiler.
/// The class used by the byte-code injected by the profiler to invoke arbitrary methods defined in the managed agent.
/// </summary>
public class ProfilerAgentMethodCallCache
public class ProfilerAgentMethodInvoker
{
private static readonly ConcurrentDictionary<string, Func<object[], object>> _invokerCache = new ConcurrentDictionary<string, Func<object[], object>>();

Expand All @@ -24,7 +24,7 @@ public class ProfilerAgentMethodCallCache
/// A reference to the method that is used to invoked the requrested agent method. The result is treated as an object
/// to simplify the type definition injected by the profiler to store this reference.
/// </returns>
public static object GetInvokerFromCache()
public static object GetInvoker()
{
return (Func<string, string, string, Type[], Type, object[], object>)GetAndInvokeMethodFromCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,14 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
{
if (_agentCallStrategy == AgentCallStyle::Strategy::InAgentCache)
{
// Ensure that the managed agent is loaded
_instructions->AppendString(_instrumentationSettings->GetCorePath());
_instructions->Append(_X("call void [") + _instructions->GetCoreLibAssemblyName() + _X("]System.CannotUnloadAppDomainException::EnsureInitialized(string)"));

// Get the Func holding a reference to the ProfilerAgentMethodCallCache.GetAndInvokeMethodFromCache method
_instructions->Append(_X("call object [") + _instructions->GetCoreLibAssemblyName() + _X("]System.CannotUnloadAppDomainException::GetMethodCacheLookupMethod()"));
_instructions->Append(CEE_CASTCLASS, _X("class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Func`7<string, string, string, class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Type[], class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Type, object[], object>"));
// result = System.CannotUnloadAppDomainException.InvokeAgentMethodInvokerFunc("C:\path\to\NewRelic.Agent.Core", "NewRelic_Delegate_API_<function name><function signature>", "NewRelic.Core.AgentApi", "<function name>", new System.Type[] { <parameter types> }, <return type>, new object[] { <method parameters> })

xstring_t className = _X("NewRelic.Agent.Core.AgentApi");
xstring_t methodName = _function->GetFunctionName();
xstring_t keyName = className + _X(".") + methodName + _X("_") + to_xstring((unsigned long)_function->GetFunctionId());
auto argumentTypesLambda = GetArrayOfTypeParametersLamdba();

_instructions->AppendString(_instrumentationSettings->GetCorePath());
_instructions->AppendString(keyName);
_instructions->AppendString(className);
_instructions->AppendString(methodName);
Expand All @@ -77,7 +72,7 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter

BuildObjectArrayOfParameters();

_instructions->Append(CEE_CALLVIRT, _X("instance !6 class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Func`7<string, string, string, class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Type[], class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Type, object[], object>::Invoke(!0, !1, !2, !3, !4, !5)"));
_instructions->Append(_X("call object [") + _instructions->GetCoreLibAssemblyName() + _X("]System.CannotUnloadAppDomainException::InvokeAgentMethodInvokerFunc(string, string, string, string, class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Type[], class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Type, object[])"));

if (_methodSignature->_returnType->_kind == SignatureParser::ReturnType::Kind::VOID_RETURN_TYPE)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
{
BuildEnsureInitializedMethod();
}
else if (_function->GetFunctionName() == _X("GetMethodCacheLookupMethod"))
else if (_function->GetFunctionName() == _X("GetAgentMethodInvokerObject"))
{
BuildGetMethodCacheLookupMethodMethod();
BuildGetAgentMethodInvokerObject();
}
else if (_function->GetFunctionName() == _X("GetAgentShimFinishTracerDelegateMethod"))
else if (_function->GetFunctionName() == _X("GetAgentShimFinishTracerDelegateFunc"))
{
BuildGetAgentShimFinishTracerDelegateMethod();
BuildGetAgentShimFinishTracerDelegateFunc();
}
else if (_function->GetFunctionName() == _X("GetMethodInfoFromAgentCache"))
else if (_function->GetFunctionName() == _X("InvokeAgentMethodInvokerFunc"))
{
BuildGetMethodInfoFromAgentCache();
BuildInvokeAgentMethodInvokerFunc();
}
else if (_function->GetFunctionName() == _X("StoreMethodCacheLookupMethod"))
else if (_function->GetFunctionName() == _X("StoreAgentMethodInvokerFunc"))
{
BuildStoreMethodCacheLookupMethod();
BuildStoreAgentMethodInvokerFunc();
}
else if (_function->GetFunctionName() == _X("StoreAgentShimFinishTracerDelegateMethod"))
else if (_function->GetFunctionName() == _X("StoreAgentShimFinishTracerDelegateFunc"))
{
BuildStoreAgentShimFinishTracerDelegateMethod();
BuildStoreAgentShimFinishTracerDelegateFunc();
}
else
{
Expand Down Expand Up @@ -176,42 +176,45 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
_instructions->Append(CEE_RET);
}

void BuildGetMethodInfoFromAgentCache()
// resultObject InvokeAgentMethodInvokerFunc(string assemblyPath, string storageKey, string typeName, string methodName, Type[] parameterTypes, Type returnType, object[] methodParameters)
void BuildInvokeAgentMethodInvokerFunc()
{
_instructions->Append(CEE_LDARG_1);
_instructions->Append(CEE_LDARG_0);
_instructions->Append(_X("call void System.CannotUnloadAppDomainException::EnsureInitialized(string)"));

_instructions->Append(_X("call object System.CannotUnloadAppDomainException::GetMethodCacheLookupMethod()"));
_instructions->Append(_X("call object System.CannotUnloadAppDomainException::GetAgentMethodInvokerObject()"));

_instructions->Append(CEE_CASTCLASS, _X("class System.Func`5<string, string, string, class System.Type[], class System.Reflection.MethodInfo>"));
_instructions->Append(CEE_LDARG_0);
_instructions->Append(CEE_CASTCLASS, _X("class System.Func`7<string, string, string, class System.Type[], class System.Type, object[], object>"));
_instructions->Append(CEE_LDARG_1);
_instructions->Append(CEE_LDARG_2);
_instructions->Append(CEE_LDARG_3);
_instructions->Append(CEE_LDARG_S, (uint8_t)4);
_instructions->Append(CEE_CALLVIRT, _X("instance !4 class System.Func`5<string, string, string, class System.Type[], class System.Reflection.MethodInfo>::Invoke(!0, !1, !2, !3)"));
_instructions->Append(CEE_LDARG_S, (uint8_t)5);
_instructions->Append(CEE_LDARG_S, (uint8_t)6);
_instructions->Append(CEE_CALLVIRT, _X("instance !6 class System.Func`7<string, string, string, class System.Type[], class System.Type, object[], object>::Invoke(!0, !1, !2, !3, !4, !5)"));

_instructions->Append(CEE_RET);
}

void BuildGetMethodCacheLookupMethodMethod()
void BuildGetAgentMethodInvokerObject()
{
_instructions->Append(CEE_VOLATILE);
_instructions->Append(CEE_LDSFLD, _X("object __NRInitializer__::_methodCache"));
_instructions->Append(CEE_RET);
}

void BuildGetAgentShimFinishTracerDelegateMethod()
void BuildGetAgentShimFinishTracerDelegateFunc()
{
_instructions->Append(CEE_VOLATILE);
_instructions->Append(CEE_LDSFLD, _X("object __NRInitializer__::_tracerFunc"));
_instructions->Append(CEE_RET);
}

void BuildStoreMethodCacheLookupMethod()
void BuildStoreAgentMethodInvokerFunc()
{
_instructions->Append(CEE_LDARG_0);
_instructions->AppendString(_X("NewRelic.Agent.Core.ProfilerAgentMethodCallCache"));
_instructions->AppendString(_X("GetInvokerFromCache"));
_instructions->AppendString(_X("NewRelic.Agent.Core.ProfilerAgentMethodInvoker"));
_instructions->AppendString(_X("GetInvoker"));
_instructions->Append(CEE_LDNULL);
_instructions->Append(CEE_CALL, _X("class System.Reflection.MethodInfo System.CannotUnloadAppDomainException::GetMethodViaReflectionOrThrow(string,string,string,class System.Type[])"));

Expand All @@ -224,7 +227,7 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
_instructions->Append(CEE_RET);
}

void BuildStoreAgentShimFinishTracerDelegateMethod()
void BuildStoreAgentShimFinishTracerDelegateFunc()
{
_instructions->Append(CEE_LDARG_0);
_instructions->AppendString(_X("NewRelic.Agent.Core.AgentShim"));
Expand All @@ -248,10 +251,10 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
auto afterInit = _instructions->AppendJump(CEE_BRTRUE);

_instructions->Append(CEE_LDARG_0);
_instructions->Append(_X("call void System.CannotUnloadAppDomainException::StoreAgentShimFinishTracerDelegateMethod(string)"));
_instructions->Append(_X("call void System.CannotUnloadAppDomainException::StoreAgentShimFinishTracerDelegateFunc(string)"));

_instructions->Append(CEE_LDARG_0);
_instructions->Append(_X("call void System.CannotUnloadAppDomainException::StoreMethodCacheLookupMethod(string)"));
_instructions->Append(_X("call void System.CannotUnloadAppDomainException::StoreAgentMethodInvokerFunc(string)"));

_instructions->AppendLabel(afterInit);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
_instructions->Append(_X("call void [") + _instructions->GetCoreLibAssemblyName() + _X("]System.CannotUnloadAppDomainException::EnsureInitialized(string)"));

// Get the Func holding a reference to NewRelic.Agent.Core.AgentShim.GetFinishTracerDelegateParameterWrapper
_instructions->Append(_X("call object [") + _instructions->GetCoreLibAssemblyName() + _X("]System.CannotUnloadAppDomainException::GetAgentShimFinishTracerDelegateMethod()"));
_instructions->Append(_X("call object [") + _instructions->GetCoreLibAssemblyName() + _X("]System.CannotUnloadAppDomainException::GetAgentShimFinishTracerDelegateFunc()"));
_instructions->Append(CEE_CASTCLASS, _X("class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Func`2<object[], class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Action`2<object, class [") + _instructions->GetCoreLibAssemblyName() + _X("]System.Exception>>"));
}
else
Expand Down
10 changes: 5 additions & 5 deletions src/Agent/NewRelic/Profiler/MethodRewriter/Instrumentors.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter
function->GetFunctionName() != _X("GetMethodFromAppDomainStorage") &&
function->GetFunctionName() != _X("GetMethodFromAppDomainStorageOrReflectionOrThrow") &&
function->GetFunctionName() != _X("StoreMethodInAppDomainStorageOrThrow") &&
function->GetFunctionName() != _X("GetMethodCacheLookupMethod") &&
function->GetFunctionName() != _X("GetAgentShimFinishTracerDelegateMethod") &&
function->GetFunctionName() != _X("GetMethodInfoFromAgentCache") &&
function->GetFunctionName() != _X("StoreMethodCacheLookupMethod") &&
function->GetFunctionName() != _X("StoreAgentShimFinishTracerDelegateMethod") &&
function->GetFunctionName() != _X("InvokeAgentMethodInvokerFunc") &&
function->GetFunctionName() != _X("GetAgentShimFinishTracerDelegateFunc") &&
function->GetFunctionName() != _X("GetAgentMethodInvokerObject") &&
function->GetFunctionName() != _X("StoreAgentMethodInvokerFunc") &&
function->GetFunctionName() != _X("StoreAgentShimFinishTracerDelegateFunc") &&
function->GetFunctionName() != _X("EnsureInitialized"))
return false;

Expand Down
10 changes: 5 additions & 5 deletions src/Agent/NewRelic/Profiler/MethodRewriter/MethodRewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter {
_instrumentedFunctionNames->emplace(_X("GetTypeViaReflectionOrThrow"));
_instrumentedFunctionNames->emplace(_X("LoadAssemblyOrThrow"));
_instrumentedFunctionNames->emplace(_X("StoreMethodInAppDomainStorageOrThrow"));
_instrumentedFunctionNames->emplace(_X("GetMethodCacheLookupMethod"));
_instrumentedFunctionNames->emplace(_X("GetAgentShimFinishTracerDelegateMethod"));
_instrumentedFunctionNames->emplace(_X("StoreAgentShimFinishTracerDelegateMethod"));
_instrumentedFunctionNames->emplace(_X("StoreMethodCacheLookupMethod"));
_instrumentedFunctionNames->emplace(_X("GetAgentMethodInvokerObject"));
_instrumentedFunctionNames->emplace(_X("GetAgentShimFinishTracerDelegateFunc"));
_instrumentedFunctionNames->emplace(_X("StoreAgentShimFinishTracerDelegateFunc"));
_instrumentedFunctionNames->emplace(_X("StoreAgentMethodInvokerObject"));
_instrumentedFunctionNames->emplace(_X("EnsureInitialized"));
_instrumentedFunctionNames->emplace(_X("GetMethodInfoFromAgentCache"));
_instrumentedFunctionNames->emplace(_X("InvokeAgentMethodInvokerFunc"));

auto instrumentationPoints = _instrumentationConfiguration->GetInstrumentationPoints();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,19 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodCacheLookupMethod)
TEST_METHOD(helper_method_InvokeAgentMethodInvokerFunc)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodCacheLookupMethod");
function->_functionName = _X("InvokeAgentMethodInvokerFunc");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodInfoFromAgentCache)
TEST_METHOD(helper_method_GetAgentMethodInvokerObject)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodInfoFromAgentCache");
function->_functionName = _X("GetAgentMethodInvokerObject");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,14 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
AssertHelperMethodIsInstrumented(true, _X("StoreMethodInAppDomainStorageOrThrow"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_GetMethodCacheLookupMethod)
TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_InvokeAgentMethodInvokerFunc)
{
AssertHelperMethodIsInstrumented(true, _X("GetMethodCacheLookupMethod"));
AssertHelperMethodIsInstrumented(true, _X("InvokeAgentMethodInvokerFunc"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_GetMethodInfoFromAgentCache)
TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_GetAgentMethodInvokerObject)
{
AssertHelperMethodIsInstrumented(true, _X("GetMethodInfoFromAgentCache"));
AssertHelperMethodIsInstrumented(true, _X("GetAgentMethodInvokerObject"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_EnsureInitialized)
Expand Down Expand Up @@ -381,14 +381,14 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
AssertHelperMethodIsInstrumented(false, _X("StoreMethodInAppDomainStorageOrThrow"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_GetMethodCacheLookupMethod)
TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_InvokeAgentMethodInvokerFunc)
{
AssertHelperMethodIsInstrumented(false, _X("GetMethodCacheLookupMethod"));
AssertHelperMethodIsInstrumented(false, _X("InvokeAgentMethodInvokerFunc"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_GetMethodInfoFromAgentCache)
TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_GetAgentMethodInvokerObject)
{
AssertHelperMethodIsInstrumented(false, _X("GetMethodInfoFromAgentCache"));
AssertHelperMethodIsInstrumented(false, _X("GetAgentMethodInvokerObject"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_EnsureInitialized)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("StoreMethodInAppDomainStorageOrThrow")));
}

TEST_METHOD(ShouldInstrumentFunction_GetMethodCacheLookupMethod)
TEST_METHOD(ShouldInstrumentFunction_InvokeAgentMethodInvokerFunc)
{
auto function = std::make_shared<MockFunction>();

auto methodRewriter = GetMethodRewriterWithConfigurationForFunction(function);

Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("GetMethodCacheLookupMethod")));
Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("InvokeAgentMethodInvokerFunc")));
}

TEST_METHOD(ShouldInstrumentFunction_EnsureInitialized)
Expand All @@ -300,13 +300,13 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("EnsureInitialized")));
}

TEST_METHOD(ShouldInstrumentFunction_GetMethodInfoFromAgentCache)
TEST_METHOD(ShouldInstrumentFunction_GetAgentMethodInvokerObject)
{
auto function = std::make_shared<MockFunction>();

auto methodRewriter = GetMethodRewriterWithConfigurationForFunction(function);

Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("GetMethodInfoFromAgentCache")));
Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("GetAgentMethodInvokerObject")));
}

private:
Expand Down
Loading

0 comments on commit 4595082

Please sign in to comment.