Created
February 6, 2012 22:35
-
-
Save rhizoome/1755521 to your computer and use it in GitHub Desktop.
Add tracing output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Mono.Cecil; | |
using Mono.Cecil.Cil; | |
using System.Reflection; | |
namespace ch.fangorn.LUI.AddTrace | |
{ | |
class MainClass | |
{ | |
public static bool verbose = false; | |
public static void Main (string[] args) | |
{ | |
//Gets the MethodInfo of Console.WriteLine() method | |
MethodInfo writeLineMethod = | |
typeof(Console).GetMethod("WriteLine", new Type[]{typeof(string)}); | |
if (args.Length < 3) { | |
Console.WriteLine ("addTrace input output libsDir [/v]"); | |
Console.WriteLine (" /v verbose"); | |
return; | |
} | |
string assemblyName = args [0]; | |
string output = args [1]; | |
string libs = args [2]; | |
((DefaultAssemblyResolver)GlobalAssemblyResolver.Instance).AddSearchDirectory (libs); | |
if (args.Length > 3) { | |
verbose = args [3] == "/v"; | |
} | |
ModuleDefinition module = ModuleDefinition.ReadModule (assemblyName); | |
MethodReference writeLineRef = module.Import(writeLineMethod); | |
//Gets all types of the MainModule of the assembly | |
foreach (TypeDefinition type in module.Types) { | |
if (type.Name != "<Module>") { | |
//Gets all methods of the current type | |
foreach (MethodDefinition method in type.Methods) { | |
string traceOutput = "t " + method.FullName; | |
Instruction ldstr = Instruction.Create(OpCodes.Ldstr, traceOutput); | |
Instruction writeTrace = Instruction.Create(OpCodes.Call, writeLineRef); | |
if(method.HasBody) { | |
method.Body.Instructions.Insert(0, writeTrace); | |
method.Body.Instructions.Insert(0, ldstr); | |
} | |
} | |
} | |
} | |
module.Write (output); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment