Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to use Profiling API right from C#?

Tags:

I just want to use .NET Profiling API (ICorProfilerCallback etc) but at the same time don't want to deal with C++. I've been looking around for a while and haven't found any example in C# but C# + C++ where the most interesting part is written using C++.

like image 948
Alex LaWay Avatar asked Apr 20 '11 21:04

Alex LaWay


People also ask

What is API profiling?

Typically, the profiling API is used to write a code profiler, which is a program that monitors the execution of a managed application. The profiling API is used by a profiler DLL, which is loaded into the same process as the application that is being profiled.

What is Cor_enable_profiling?

COR_ENABLE_PROFILING: The CLR connects to a profiler only if this environment variable exists and is set to 1. COR_PROFILER: If the COR_ENABLE_PROFILING check passes, the CLR connects to the profiler that has this CLSID or ProgID, which must have been stored previously in the registry.


1 Answers

No, you cannot implement the CLR profiling APIs in managed code (C# or otherwise) since the profiling callbacks are called at very specific times when the managed environment is assumed to be in a certain state. Implementing your callbacks in managed code would violate a lot of assumptions.

David Broman, the developer of the CLR profiling APIs, has this to say:

You need to write your profiler in C++. The profiler is called by the runtime at very delicate points during execution of the profiled application, and it is often extremely unsafe to be running managed code at those points.

David's blog is a great resource for dealing with the CLR profiling APIs.

like image 98
Chris Schmich Avatar answered Jan 03 '23 00:01

Chris Schmich