Created
June 20, 2014 16:17
-
-
Save lnicola/b48db1a6ff3617bdac2a to your computer and use it in GitHub Desktop.
some code to graph function dependencies, using Mono.Cecil and GLEE
This file contains hidden or 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 System.Drawing; | |
| using System.Drawing.Imaging; | |
| using System.Collections.Generic; | |
| using System.Collections.Specialized; | |
| using Microsoft.Glee.GraphViewerGdi; | |
| using Microsoft.Glee.Drawing; | |
| using Mono.Cecil; | |
| using Mono.Cecil.Cil; | |
| namespace CecilTest | |
| { | |
| class Program | |
| { | |
| static StringCollection visitedMethodList = new StringCollection(); | |
| static Graph graph; | |
| static void VisitMethodDefinition(MethodDefinition methodDefinition) | |
| { | |
| if (visitedMethodList.Contains(methodDefinition.Name)) | |
| return; | |
| visitedMethodList.Add(methodDefinition.Name); | |
| StringCollection visitedDestinationList = new StringCollection(); | |
| if (methodDefinition.Body == null) | |
| return; | |
| foreach (Instruction instruction in methodDefinition.Body.Instructions) | |
| { | |
| MethodDefinition calledMethodDefinition = instruction.Operand as MethodDefinition; | |
| if (instruction.OpCode.Name != "call" && instruction.OpCode.Name != "callvirt" || calledMethodDefinition == null) | |
| continue; | |
| //Console.WriteLine("{0} {1}", instruction.OpCode, instruction.Operand); | |
| bool skip = false; | |
| foreach (CustomAttribute customAttribute in calledMethodDefinition.CustomAttributes) | |
| { | |
| if (customAttribute.Constructor.DeclaringType.FullName == "InternalAttribute") | |
| { | |
| skip = true; | |
| break; | |
| } | |
| } | |
| if (skip) | |
| continue; | |
| string destination = calledMethodDefinition.Name; | |
| if (!visitedDestinationList.Contains(destination)) | |
| { | |
| visitedDestinationList.Add(destination); | |
| graph.AddEdge(methodDefinition.Name, destination); | |
| VisitMethodDefinition(calledMethodDefinition); | |
| } | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| graph = new Graph("Call tree"); | |
| AssemblyDefinition assemblyDefinition = AssemblyFactory.GetAssembly(@"..\..\..\Rascal\bin\Release\rascal.exe"); | |
| ModuleDefinition moduleDefinition = assemblyDefinition.MainModule; | |
| VisitMethodDefinition(moduleDefinition.Types["Rascal.Program"].Methods.GetMethod("Main")[0]); | |
| graph.FindNode("Main").Attr.Fillcolor = new Microsoft.Glee.Drawing.Color(61, 151, 255); | |
| /* | |
| foreach (TypeDefinition td in moduleDefinition.Types) | |
| foreach (MethodDefinition md in td.Methods) | |
| if (visitedMethodList.Contains(md.Name) == false) | |
| { | |
| VisitMethodDefinition(md); | |
| Node node = graph.FindNode(md.Name); | |
| if (node != null) | |
| node.Attr.Fillcolor = new Microsoft.Glee.Drawing.Color(61, 151, 255); | |
| } | |
| */ | |
| GraphRenderer graphRenderer = new GraphRenderer(graph); | |
| graphRenderer.CalculateLayout(); | |
| Bitmap b = new Bitmap((int) graph.Width, (int) graph.Height, PixelFormat.Format24bppRgb); | |
| graphRenderer.Render(b); | |
| b.Save(@"..\..\ct.png"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment