跟着我以前的答案,我設法創建了一個基本的異常查找程序。它使用基於反射的ILReader
類,在海博羅的MSDN博客上提供here。 (只需添加到項目的引用。)
更新:
- 現在處理局部變量和堆棧。
- 正確地檢測到方法調用或字段返回的異常,然後拋出。
- 現在處理堆棧推動/彈出充分和適當。
以下是代碼全部。您只需要使用GetAllExceptions(MethodBase)
方法作爲擴展或靜態方法。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using ClrTest.Reflection;
public static class ExceptionAnalyser
{
public static ReadOnlyCollection<Type> GetAllExceptions(this MethodBase method)
{
var exceptionTypes = new HashSet<Type>();
var visitedMethods = new HashSet<MethodBase>();
var localVars = new Type[ushort.MaxValue];
var stack = new Stack<Type>();
GetAllExceptions(method, exceptionTypes, visitedMethods, localVars, stack, 0);
return exceptionTypes.ToList().AsReadOnly();
}
public static void GetAllExceptions(MethodBase method, HashSet<Type> exceptionTypes,
HashSet<MethodBase> visitedMethods, Type[] localVars, Stack<Type> stack, int depth)
{
var ilReader = new ILReader(method);
var allInstructions = ilReader.ToArray();
ILInstruction instruction;
for (int i = 0; i < allInstructions.Length; i++)
{
instruction = allInstructions[i];
if (instruction is InlineMethodInstruction)
{
var methodInstruction = (InlineMethodInstruction)instruction;
if (!visitedMethods.Contains(methodInstruction.Method))
{
visitedMethods.Add(methodInstruction.Method);
GetAllExceptions(methodInstruction.Method, exceptionTypes, visitedMethods,
localVars, stack, depth + 1);
}
var curMethod = methodInstruction.Method;
if (curMethod is ConstructorInfo)
stack.Push(((ConstructorInfo)curMethod).DeclaringType);
else if (method is MethodInfo)
stack.Push(((MethodInfo)curMethod).ReturnParameter.ParameterType);
}
else if (instruction is InlineFieldInstruction)
{
var fieldInstruction = (InlineFieldInstruction)instruction;
stack.Push(fieldInstruction.Field.FieldType);
}
else if (instruction is ShortInlineBrTargetInstruction)
{
}
else if (instruction is InlineBrTargetInstruction)
{
}
else
{
switch (instruction.OpCode.Value)
{
// ld*
case 0x06:
stack.Push(localVars[0]);
break;
case 0x07:
stack.Push(localVars[1]);
break;
case 0x08:
stack.Push(localVars[2]);
break;
case 0x09:
stack.Push(localVars[3]);
break;
case 0x11:
{
var index = (ushort)allInstructions[i + 1].OpCode.Value;
stack.Push(localVars[index]);
break;
}
// st*
case 0x0A:
localVars[0] = stack.Pop();
break;
case 0x0B:
localVars[1] = stack.Pop();
break;
case 0x0C:
localVars[2] = stack.Pop();
break;
case 0x0D:
localVars[3] = stack.Pop();
break;
case 0x13:
{
var index = (ushort)allInstructions[i + 1].OpCode.Value;
localVars[index] = stack.Pop();
break;
}
// throw
case 0x7A:
if (stack.Peek() == null)
break;
if (!typeof(Exception).IsAssignableFrom(stack.Peek()))
{
//var ops = allInstructions.Select(f => f.OpCode).ToArray();
//break;
}
exceptionTypes.Add(stack.Pop());
break;
default:
switch (instruction.OpCode.StackBehaviourPop)
{
case StackBehaviour.Pop0:
break;
case StackBehaviour.Pop1:
case StackBehaviour.Popi:
case StackBehaviour.Popref:
case StackBehaviour.Varpop:
stack.Pop();
break;
case StackBehaviour.Pop1_pop1:
case StackBehaviour.Popi_pop1:
case StackBehaviour.Popi_popi:
case StackBehaviour.Popi_popi8:
case StackBehaviour.Popi_popr4:
case StackBehaviour.Popi_popr8:
case StackBehaviour.Popref_pop1:
case StackBehaviour.Popref_popi:
stack.Pop();
stack.Pop();
break;
case StackBehaviour.Popref_popi_pop1:
case StackBehaviour.Popref_popi_popi:
case StackBehaviour.Popref_popi_popi8:
case StackBehaviour.Popref_popi_popr4:
case StackBehaviour.Popref_popi_popr8:
case StackBehaviour.Popref_popi_popref:
stack.Pop();
stack.Pop();
stack.Pop();
break;
}
switch (instruction.OpCode.StackBehaviourPush)
{
case StackBehaviour.Push0:
break;
case StackBehaviour.Push1:
case StackBehaviour.Pushi:
case StackBehaviour.Pushi8:
case StackBehaviour.Pushr4:
case StackBehaviour.Pushr8:
case StackBehaviour.Pushref:
case StackBehaviour.Varpush:
stack.Push(null);
break;
case StackBehaviour.Push1_push1:
stack.Push(null);
stack.Push(null);
break;
}
break;
}
}
}
}
}
總之,該算法遞歸枚舉(深度優先)調用的指定一個範圍內的任何方法,通過讀取CIL指令(以及跟蹤的方法已經訪問過)。它維護一個可以使用HashSet<T>
對象拋出的集合的單個列表,最後返回該對象。它另外還維護一個局部變量和一個堆棧的數組,以便跟蹤在創建後不立即拋出的異常。
當然,這個代碼在它的當前狀態中是不可錯誤的。有跡象表明,我需要爲它是強大的,即一些改進:
檢測未使用異常的構造函數直接拋出的異常。 (即從局部變量或方法調用中檢索異常。)
支持異常彈出堆棧,然後再推回去。
- 添加流量控制檢測。除非檢測到
rethrow
指令,否則處理任何拋出的異常的Try-catch塊應該從列表中刪除相應的異常。
除此之外,我相信代碼是合理完成。在我弄清楚如何進行流量控制檢測之前,可能需要更多的調查(儘管我相信我可以看到它是如何在IL級別上運行的)。
如果要創建一個功能齊全的「異常分析器」,這些函數可能會變成整個庫,但希望這至少可以爲這樣的工具提供一個良好的起點,如果尚未足夠好的話其當前狀態。
無論如何,希望有所幫助!
是的,我很遺憾得到同樣的錯誤。事實證明,所需的邏輯再次比我想象的要複雜得多。然而,對於堆棧的一些虛擬推/拉處理應該完成這項工作 - 這是我現在正在嘗試的。 – Noldorin 2009-06-17 00:06:43
澄清的要點,但是你是指由開發人員編寫的函數拋出的所有異常(例如,在函數中拋出命令)或者也可能由函數調用的其他例程拋出的所有異常(例如,無效操作.NET庫調用拋出的表達式)? – rjzii 2009-06-18 13:09:34
我只是想知道你爲什麼要重新發明輪子?如果一個工具已經被創建完成,那麼爲什麼要編寫希望這樣做的代碼呢? – Mark 2009-06-18 20:28:04