添加code,以在运行时动态方法的开始/结束结束、方法、动态、code

2023-09-04 00:53:32 作者:浅色的泡沫

我知道仪器是一种技术,动态地添加微量code到方法,以启用跟踪和调试。

I know instrumentation is a technique to add trace code dynamically into the methods to enable tracing and debugging.

我想知道,这仅仅是一个跟踪选项,硬codeD插入CLR只添加微量code,或有添加任何code的方法的能力?

I was wondering if this is only a "Trace" option, hard coded into the CLR to add only trace code, or is there the ability to add any code to the methods?

例如,我要为您在每一个方法调用开始时的状态在一定的阶级(说的权限)。我能做到这一点,通过添加动态code到的方法开始执行时间?

For example, I want to check for a condition in the beginning of every single method call in a certain class (say for permissions). Can I do this via adding dynamic code to the beginning of the methods in execution time?

我不知道这是如何跟踪仪器的东西的作品,但我不知道这是否可以被用于其他目标也一样,还是不行。

I'm not sure how this trace "instrumentation" thing works, but I'm wondering if this can be used for other goals too, or not.

推荐答案

基本上你应该做的是写一个CLR探查器,并使用的探查API 在C ++ 您需要实现ICorProfilerCallback接口。 你正在寻找的是在JITCompilationStarted回调。这种方法被称为每次管理方法被调用和JIT编译器编译之前的IL成机器code。 任何在运行时code插入的工作应该在JITCompilationStarted完成。 你可以看一下开源覆盖率工具部分覆盖,例如,如何做到这一点。

Basically what you should do is write a CLR profiler and use the profiler API in c++ You need to implement the ICorProfilerCallback interface. What you are looking for is in the JITCompilationStarted callback. This method is called each time a managed method gets called and before the jit compiler compiles the IL into machine code. Any code insertion work during run time should be done in JITCompilationStarted. You can look at the open source coverage tool part cover as an example how to do it.