jay
jay

Reputation: 61

Instrumentation Profiler in CoreCLR - Ways to Load HelperAssembly in to dotnet process

I am trying to Instrument .NET Core web applications that runs on .NET Core 3.1 using CoreCLR Profiler in linux centos7. I have set the environment values CORECLR_PROFILER , CORECLR_ENABLE_PROFILING and CORECLR_PROFILER_PATH, where my CoreCLRProfiler dll gets attached to dotnet.exe and it is getting the callbacks.

I am able to get all the callbacks,but when i allow injecting the code into the Webapplication's method then the app is getting crashed(dotnet.exe gets killed) as it couldnt find the injected function call.

I have created helper assembly(.NET standard 2.0) with the injected functions body and signed it with strong name and installed it in to the GAC. And also used DefineAssemblyRef(),DefineTypeRefByName() and DefineMemberRef() from IMetaDataAssemblyEmit to load assembly and its class methods. And also tried by placing dotnet standard dll in application folder. But the helper assembly is not loaded to dotnet.exe process.

Where should my helper assembly placed..? and

how can I load helper assembly to dotnet process from my native coreclr profiler?

It would be much helpful if i get some correct direction to load or use helper assembly to dotnet process.

Thanks in advance.

Upvotes: 2

Views: 592

Answers (1)

Egozy
Egozy

Reputation: 380

I played around a lot with loading a managed DLL and calling it from our Profiler: many different approaches, almost all of them had a limitation one way or another. The problem we saw was that if the method that calls the external dll is already compiled, it is now too late to load the external dll. Even if the method is compiled and the dll is loaded as part of the method (before the call is made to the dll), this is still too late for the CLR. What you can do is a bit patchy but it works: instrument a call to https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.assemblyresolve and add your own method that looks up in a specific place. This has to be done as early as possible (before the method calling this assembly is compiled). Note that this will support .Net Framework as well as .Net Core. If you only need support for .Net Core, you can use the way described here: https://github.com/richlander/dotnet-core-assembly-loading

Upvotes: 1

Related Questions