是否可以直接从 C# 使用 Profiling API?

问题描述:

我只想使用 .NET Profiling API(ICorProfilerCallback 等)但同时不想处理 C++.我已经环顾了一段时间,没有找到任何 C# 的例子,但是 C# + C++ 中最有趣的部分是使用 C++ 编写的.

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++.

不,您不能在托管代码(C# 或其他)中实现 CLR 分析 API,因为在假定托管环境的特定时间调用分析回调处于某种状态.在托管代码中实现回调会违反很多假设.

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.

CLR 分析 API 的开发人员 David Broman 拥有 这个要说:

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

您需要将分析器写入C++.探查器由在非常微妙的点运行时执行分析的应用程序,并且通常非常不安全在这些点运行托管代码.

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 的博客是处理 CLR 分析 API 的重要资源.

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