public void VisualizeMethodBody(MethodBodyBlock body, MethodHandle methodHandle, bool emitHeader = true) { StringBuilder builder = new StringBuilder(); // TODO: Inspect EncLog to find a containing type and display qualified name. var method = GetMethod(methodHandle); if (emitHeader) { builder.AppendFormat("Method {0} (0x{1:X8})", Literal(method.Name), MetadataTokens.GetToken(methodHandle)); builder.AppendLine(); } // TODO: decode signature if (!body.LocalSignature.IsNil) { var localSignature = GetLocalSignature(body.LocalSignature); builder.AppendFormat(" Locals: {0}", Literal(localSignature)); builder.AppendLine(); } ILVisualizerAsTokens.Instance.DumpMethod( builder, body.MaxStack, body.GetILBytes(), ImmutableArray.Create <ILVisualizer.LocalInfo>(), // TODO ImmutableArray.Create <ILVisualizer.HandlerSpan>()); // TOOD: ILVisualizer.GetHandlerSpans(body.ExceptionRegions) builder.AppendLine(); writer.Write(builder.ToString()); }
public override byte[] GetILBytes() { if (_ilBytes != null) { return(_ilBytes); } Interlocked.CompareExchange(ref _ilBytes, _methodBody.GetILBytes(), null); return(_ilBytes); }
public override byte[] GetILBytes() { if (_ilBytes != null) { return(_ilBytes); } byte[] ilBytes = _methodBody.GetILBytes(); return(_ilBytes = ilBytes); }
//<SnippetPrintMethods> static void PrintMethods(PEReader reader, MetadataReader mr, TypeDefinition tdef) { MethodDefinitionHandleCollection methods = tdef.GetMethods(); foreach (MethodDefinitionHandle mdefh in methods) { MethodDefinition mdef = mr.GetMethodDefinition(mdefh); string mname = mr.GetString(mdef.Name); Console.WriteLine($"Method: {mname}"); // Get the relative address of the method body in the executable int rva = mdef.RelativeVirtualAddress; if (rva == 0) { Console.WriteLine("Method body not found"); Console.WriteLine(); continue; } // Get method body information MethodBodyBlock mb = reader.GetMethodBody(rva); Console.WriteLine($" Maximum stack size: {mb.MaxStack}"); Console.WriteLine($" Local variables initialized: {mb.LocalVariablesInitialized}"); byte[] il = mb.GetILBytes(); Console.WriteLine($" Method body size: {il.Length}"); Console.WriteLine($" Exception regions: {mb.ExceptionRegions.Length}"); Console.WriteLine(); foreach (var region in mb.ExceptionRegions) { Console.WriteLine(region.Kind.ToString()); Console.WriteLine($" Try block offset: {region.TryOffset}"); Console.WriteLine($" Try block length: {region.TryLength}"); Console.WriteLine($" Handler offset: {region.HandlerOffset}"); Console.WriteLine($" Handler length: {region.HandlerLength}"); Console.WriteLine(); } } }