private static string GetStackInfo(Exception ex) { StringBuilder builder = new StringBuilder(); int num = 0; foreach (StackFrame frame in new StackTrace(ex).GetFrames()) { builder.Append(" -"); int iLOffset = frame.GetILOffset(); MethodBase method = frame.GetMethod(); if ((method != null) && (method.DeclaringType != null)) { builder.Append(method.DeclaringType.FullName); } if (method != null) { builder.Append(" " + method.ToString()); } builder.Append(" offset: 0x" + iLOffset.ToString("X")); builder.AppendLine(); if ((((iLOffset > 0) && (method != null)) && (method.ReflectedType != null)) && !method.ReflectedType.Namespace.StartsWith("System", StringComparison.Ordinal)) { try { builder.AppendLine(" " + Disassembler.Disassemble(method, iLOffset - 0x19, iLOffset + 0x19).Replace("\n", "\n ")); } catch (Exception exception) { builder.AppendLine(" <" + exception.Message + ex.StackTrace + ">"); } } builder.AppendLine(); num++; } return(builder.ToString()); }
private static void DisassembleQuery() { Assembly assembly; string data = (string) AppDomain.CurrentDomain.GetData("path"); string[] refs = (string[]) AppDomain.CurrentDomain.GetData("refs"); AppDomain.CurrentDomain.AssemblyResolve += delegate (object sender, ResolveEventArgs args) { string simpleName = new AssemblyName(args.Name).Name.ToLowerInvariant(); string assemblyFile = refs.FirstOrDefault<string>(r => Path.GetFileNameWithoutExtension(r).ToLowerInvariant() == simpleName); if (assemblyFile == null) { return null; } return Assembly.LoadFrom(assemblyFile); }; try { assembly = Assembly.LoadFrom(data); } catch (Exception exception) { Log.Write("File: " + data + " could not be loaded: " + exception.Message); return; } string directoryName = Path.GetDirectoryName(data); foreach (AssemblyName name in assembly.GetReferencedAssemblies()) { if (name.Name.StartsWith("TypedDataContext", StringComparison.Ordinal)) { Assembly.LoadFrom(Path.Combine(directoryName, name.Name + ".dll")); } } string[] strArray = refs; int index = 0; while (true) { if (index >= strArray.Length) { break; } string path = strArray[index]; if (Path.IsPathRooted(path)) { try { Assembly.LoadFrom(path); } catch { } } index++; } Type type = assembly.GetType("UserQuery"); MethodInfo method = null; if (assembly.EntryPoint != null) { method = assembly.EntryPoint; } else { try { method = type.GetMethod("Main", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); } catch { } } if (method == null) { method = type.GetMethod("RunUserAuthoredQuery", BindingFlags.NonPublic | BindingFlags.Instance); } byte[] iLAsByteArray = method.GetMethodBody().GetILAsByteArray(); int offsetFrom = 0; int num3 = iLAsByteArray.Length - 1; if (iLAsByteArray[0] == OpCodes.Nop.Value) { offsetFrom++; } if ((num3 > 0) && (iLAsByteArray[num3] == OpCodes.Ret.Value)) { num3--; } if ((num3 > 0) && (iLAsByteArray[num3] == OpCodes.Nop.Value)) { num3--; } if ((num3 > 0) && (iLAsByteArray[num3] == OpCodes.Pop.Value)) { num3--; } StringBuilder builder = new Disassembler(method, new HtmlILStyler(false)).Dis(offsetFrom, num3); Type type2 = (assembly.EntryPoint == null) ? type : assembly.GetType(assembly.GetName().Name, false, true); if (type2 != null) { foreach (MethodInfo info2 in type2.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { if (((info2 != method) && (info2.Name != "RunUserAuthoredQuery")) && (info2 != method)) { builder.AppendLine(); builder.AppendLine("<b>" + new HtmlILStyler(true).StyleIdentifier(info2.Name) + ":</b>"); builder.Append(new Disassembler(info2, new HtmlILStyler(false)).Dis()); } } foreach (Type type3 in type2.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public)) { foreach (MethodBase base2 in type3.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly).Concat<MethodBase>(type3.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))) { builder.AppendLine(); builder.AppendLine("<b>" + new HtmlILStyler(true).StyleIdentifier(type3.Name + "." + base2.Name) + ":</b>"); builder.Append(new Disassembler(base2, new HtmlILStyler(false)).Dis()); } } } string str4 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html>\r\n <head>\r\n <style type=\"text/css\">body\r\n{\r\n\tmargin: 0.3em 0.3em 0.4em 0.5em;\r\n}\r\npre\r\n{\r\n\tmargin:0;\r\n\tpadding:0;\r\n\tfont-family: Consolas;\r\n}\r\nspan.opcode\r\n{\r\n\tcolor: #0000FF;\r\n}\r\nspan.ident\r\n{\r\n\tcolor: #008080;\r\n}\r\nspan.string\r\n{\r\n\tcolor: #DC1414;\r\n}\r\na:link, a:visited\r\n{\r\n\ttext-decoration:none;\r\n\tfont-weight:bold;\r\n}\r\na:hover\r\n{\r\n\ttext-decoration:underline;\r\n\tfont-weight:bold;\r\n}\r\n</style> \r\n </head>\r\n <body>\r\n <pre>" + builder.ToString() + "</pre>\r\n </body>\r\n</html>"; AppDomain.CurrentDomain.SetData("result", str4); }
private static void DisassembleQuery() { Assembly assembly; string data = (string)AppDomain.CurrentDomain.GetData("path"); string[] refs = (string[])AppDomain.CurrentDomain.GetData("refs"); AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs args) { string simpleName = new AssemblyName(args.Name).Name.ToLowerInvariant(); string assemblyFile = refs.FirstOrDefault <string>(r => Path.GetFileNameWithoutExtension(r).ToLowerInvariant() == simpleName); if (assemblyFile == null) { return(null); } return(Assembly.LoadFrom(assemblyFile)); }; try { assembly = Assembly.LoadFrom(data); } catch (Exception exception) { Log.Write("File: " + data + " could not be loaded: " + exception.Message); return; } string directoryName = Path.GetDirectoryName(data); foreach (AssemblyName name in assembly.GetReferencedAssemblies()) { if (name.Name.StartsWith("TypedDataContext", StringComparison.Ordinal)) { Assembly.LoadFrom(Path.Combine(directoryName, name.Name + ".dll")); } } string[] strArray = refs; int index = 0; while (true) { if (index >= strArray.Length) { break; } string path = strArray[index]; if (Path.IsPathRooted(path)) { try { Assembly.LoadFrom(path); } catch { } } index++; } Type type = assembly.GetType("UserQuery"); MethodInfo method = null; if (assembly.EntryPoint != null) { method = assembly.EntryPoint; } else { try { method = type.GetMethod("Main", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); } catch { } } if (method == null) { method = type.GetMethod("RunUserAuthoredQuery", BindingFlags.NonPublic | BindingFlags.Instance); } byte[] iLAsByteArray = method.GetMethodBody().GetILAsByteArray(); int offsetFrom = 0; int num3 = iLAsByteArray.Length - 1; if (iLAsByteArray[0] == OpCodes.Nop.Value) { offsetFrom++; } if ((num3 > 0) && (iLAsByteArray[num3] == OpCodes.Ret.Value)) { num3--; } if ((num3 > 0) && (iLAsByteArray[num3] == OpCodes.Nop.Value)) { num3--; } if ((num3 > 0) && (iLAsByteArray[num3] == OpCodes.Pop.Value)) { num3--; } StringBuilder builder = new Disassembler(method, new HtmlILStyler(false)).Dis(offsetFrom, num3); Type type2 = (assembly.EntryPoint == null) ? type : assembly.GetType(assembly.GetName().Name, false, true); if (type2 != null) { foreach (MethodInfo info2 in type2.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { if (((info2 != method) && (info2.Name != "RunUserAuthoredQuery")) && (info2 != method)) { builder.AppendLine(); builder.AppendLine("<b>" + new HtmlILStyler(true).StyleIdentifier(info2.Name) + ":</b>"); builder.Append(new Disassembler(info2, new HtmlILStyler(false)).Dis()); } } foreach (Type type3 in type2.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public)) { foreach (MethodBase base2 in type3.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly).Concat <MethodBase>(type3.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))) { builder.AppendLine(); builder.AppendLine("<b>" + new HtmlILStyler(true).StyleIdentifier(type3.Name + "." + base2.Name) + ":</b>"); builder.Append(new Disassembler(base2, new HtmlILStyler(false)).Dis()); } } } string str4 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html>\r\n <head>\r\n <style type=\"text/css\">body\r\n{\r\n\tmargin: 0.3em 0.3em 0.4em 0.5em;\r\n}\r\npre\r\n{\r\n\tmargin:0;\r\n\tpadding:0;\r\n\tfont-family: Consolas;\r\n}\r\nspan.opcode\r\n{\r\n\tcolor: #0000FF;\r\n}\r\nspan.ident\r\n{\r\n\tcolor: #008080;\r\n}\r\nspan.string\r\n{\r\n\tcolor: #DC1414;\r\n}\r\na:link, a:visited\r\n{\r\n\ttext-decoration:none;\r\n\tfont-weight:bold;\r\n}\r\na:hover\r\n{\r\n\ttext-decoration:underline;\r\n\tfont-weight:bold;\r\n}\r\n</style> \r\n </head>\r\n <body>\r\n <pre>" + builder.ToString() + "</pre>\r\n </body>\r\n</html>"; AppDomain.CurrentDomain.SetData("result", str4); }
public static string Disassemble(this MethodBase method) { return(Disassembler.Disassemble(method)); }