重写IL以在方法调用周围注入try-finally

时间:2015-12-07 12:55:31

标签: c# .net il mono.cecil fody

我想将sql日志记录注入一些方法。基本上我想改造

    public static object IDbCommandTest_ExecuteScalar(IDbCommand command)
    {
        // .. do stuff
        command.CommandText = "SELECT ...";
        var obj = command.ExecuteScalar();
        Console.WriteLine("SQL returned " + obj);
        // do other stuff
        return obj;
    }

    public static object IDbCommandTest_ExecuteScalar_Transformed(IDbCommand command)
    {
        // .. do stuff
        command.CommandText = "SELECT ...";
        object obj;
        using (SqlLogger.Enter(command, "IDbCommand", "ExecuteScalar"))
        {
            obj = command.ExecuteScalar();
        }
        Console.WriteLine("SQL returned " + obj);
        // do other stuff
        return obj;
    }

我让简单的案例工作,我目前面临的问题是,在输入.try时,堆栈必须为空。

我的原始假设是IDbCommand本身在调用ExecuteScalar之前直接加载,所以我搜索callvirt,然后使用Instruction.Previous作为.try的开头}。

但如果之后使用ExecuteScalar的返回值,编译器将生成以下IL:

    IL_004d: ldarg.0
    IL_004e: ldloc.1
    IL_004f: callvirt instance object [System.Data]System.Data.IDbCommand::ExecuteScalar()
    IL_0054: call string Class_which_uses_obj::DoStuff(object)

使用我的原始算法,将其转换为

    IL_0050: ldarg.0
    .try
    {
        IL_0051: ldloc.1
        IL_0052: dup
        IL_0053: ldstr "IDbCommand"
        IL_0058: ldstr "get_FileName"
        IL_005d: call class [mscorlib]System.IDisposable SqlLogger::Enter(class [System.Data]System.Data.IDbCommand, string, string)
        IL_0062: stloc.3
        IL_0063: callvirt instance object [System.Data]System.Data.IDbCommand::ExecuteScalar()
        IL_0068: stloc.s 4
        IL_006a: leave.s IL_0077
    } // end .try
    finally
    {
        IL_006c: nop
        IL_006d: ldloc.3
        IL_006e: brfalse.s IL_0076

        IL_0070: ldloc.3
        IL_0071: callvirt instance void [mscorlib]System.IDisposable::Dispose()

        IL_0076: endfinally
    } // end handler

    IL_0077: nop
    IL_0078: ldloc.s 4
    IL_007a: call string        IL_0050: ldarg.0
    .try
    {
        IL_0051: ldloc.1
        IL_0052: dup
        IL_0053: ldstr "IDbCommand"
        IL_0058: ldstr "ExecuteScalar"
        IL_005d: call class [mscorlib]System.IDisposable Nemetschek.Allready.SqlLogger::Enter(class [System.Data]System.Data.IDbCommand, string, string)
        IL_0062: stloc.3
        IL_0063: callvirt instance object [System.Data]System.Data.IDbCommand::ExecuteScalar()
        IL_0068: stloc.s 4
        IL_006a: leave.s IL_0077
    } // end .try
    finally
    {
        IL_006c: nop
        IL_006d: ldloc.3
        IL_006e: brfalse.s IL_0076

        IL_0070: ldloc.3
        IL_0071: callvirt instance void [mscorlib]System.IDisposable::Dispose()

        IL_0076: endfinally
    } // end handler

    IL_0077: nop
    IL_0078: ldloc.s 4
    IL_007a: call string Nemetschek.Allready.Logistics.DbTools.CDbTools::GetSafeStringEmpty(object)(object)

然后PEVERIFY抱怨我输入一个非空堆栈的.try。

是否有一种简单的方法可以将try-finally注入到ExecuteScalar中,或者我是否需要对整个方法进行完整的流分析,计算任何点的堆栈深度,然后加载/恢复之前/之后的值尝试/最后?

修改

我已经能够通过向上/向下扫描直到找到堆栈深度为0的两个点来工作。在我的有限测试中,这似乎有效,但我仍然会对'干净'感兴趣实施而不是盲目扫描IL。

1 个答案:

答案 0 :(得分:4)

我会将对ExecuteScalar的调用重写为对辅助方法的调用:

static object ExecuteScalarWrapper(SqlCommand command, string logString) {
        using (SqlLogger.Enter(command, logString))
        {
            return command.ExecuteScalar();
        }
}

ExecuteScalarWrapper将是一个静态辅助方法,您可以使用C#和reference。

编写

然后,您不需要注入任何try-blocks。你只需要替换模式

ld... command
call ExecuteScalar

ld... command
ld... logString
call ExecuteScalarWrapper

这应该更容易,因为堆栈布局和修改是本地定义的,不需要任何复杂的推理。

现在JIT为你做了所有繁重的工作。