Beispiel #1
0
        static ProfileData ReadProfileFromPort(ProfileReader reader)
        {
            ProfileData pd;

            if (AdbForward)
            {
                var cmdArgs = $"forward tcp:{Port} tcp:{Port}";
                if (Verbose)
                {
                    ColorWriteLine($"Calling 'adb {cmdArgs}'...", ConsoleColor.Yellow);
                }

                System.Diagnostics.Process.Start("adb", cmdArgs);
            }

            using (var client = new TcpClient("127.0.0.1", Port)) {
                using (var stream = client.GetStream()) {
                    var msgData = System.Text.Encoding.ASCII.GetBytes("save\n");

                    stream.Write(msgData, 0, msgData.Length);

                    if (Verbose)
                    {
                        ColorWriteLine($"Reading from '127.0.0.1:{Port}'...", ConsoleColor.Yellow);
                    }

                    using (var memoryStream = new MemoryStream(128 * 1024)) {
                        var data = new byte [4 * 1024];
                        int len;

                        while ((len = stream.Read(data, 0, data.Length)) > 0)
                        {
                            memoryStream.Write(data, 0, len);

                            if (Verbose)
                            {
                                ColorWrite($"Read {len} bytes...\r", ConsoleColor.Yellow);
                            }
                        }

                        if (Verbose)
                        {
                            ColorWriteLine($"Read total {memoryStream.Length} bytes...", ConsoleColor.Yellow);
                        }

                        memoryStream.Seek(0, SeekOrigin.Begin);

                        pd = reader.ReadAllData(memoryStream);
                    }
                }
            }

            return(pd);
        }
Beispiel #2
0
    public bool Execute()
    {
        var         reader = new ProfileReader();
        ProfileData data;

        using (FileStream stream = File.OpenRead(Input !))
        {
            data = reader.ReadAllData(stream);
            // ModuleRecord[] modules = data.Modules;
            var serializeOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                ReferenceHandler     = ReferenceHandler.Preserve,
                IgnoreNullValues     = true,
                WriteIndented        = true,
            };
            string s = JsonSerializer.Serialize(data, serializeOptions);
            File.WriteAllText(Output !, s);
        }
        return(true);
    }
    public bool Execute(string methodPath)
    {
        var         reader = new ProfileReader();
        ProfileData profile;

        string[] methodNames;
        using (FileStream stream = File.OpenRead(Input))
            profile = reader.ReadAllData(stream);
        if (ExcludeMethods != null)
        {
            var newMethods = new List <MethodRecord>();

            foreach (MethodRecord method in profile.Methods)
            {
                bool isFiltered = ExcludeMethods !.Any(e => method.Name == e);
                if (!isFiltered)
                {
                    newMethods.Add(method);
                }
            }
            profile.Methods = newMethods.ToArray();
        }
        if (Dump)
        {
            methodNames = DumpMethods(profile);
            File.WriteAllLines(methodPath, methodNames.ToArray());
        }
        if (Output != null)
        {
            var writer = new ProfileWriter();
            using (FileStream outStream = File.Create(Output !))
                writer.WriteAllData(outStream, profile);
        }

        return(true);
    }
Beispiel #4
0
        public static void Main(string [] args)
        {
            var path = ProcessArguments(args);

            if (args.Length == 1)
            {
                Modules = Types = Methods = true;
            }

            var         reader = new ProfileReader();
            ProfileData pd     = null;

            if (path == null)
            {
                if (Port < 0)
                {
                    Error($"You should specify path or -p PORT to read the profile.");
                    Environment.Exit(4);
                }
                else
                {
                    try {
                        pd = ReadProfileFromPort(reader);
                    } catch (Exception e) {
                        Error($"Unable to read profile through local port: {Port}.\n{e}");
                        Environment.Exit(5);
                    }
                }
            }
            else if (!File.Exists(path))
            {
                Error($"'{path}' doesn't exist.");
                Environment.Exit(3);
            }
            else
            {
                using (var stream = new FileStream(path, FileMode.Open)) {
                    if (Verbose)
                    {
                        ColorWriteLine($"Reading '{path}'...", ConsoleColor.Yellow);
                    }

                    pd = reader.ReadAllData(stream);
                }
            }

            List <MethodRecord>        methods = new List <MethodRecord> (pd.Methods);
            ICollection <TypeRecord>   types   = new List <TypeRecord> (pd.Types);
            ICollection <ModuleRecord> modules = new List <ModuleRecord> (pd.Modules);

            if (FilterMethod != null || FilterType != null || FilterModule != null)
            {
                methods = new List <MethodRecord> ();
                types   = new HashSet <TypeRecord> ();
                modules = new HashSet <ModuleRecord> ();

                foreach (var method in pd.Methods)
                {
                    var type   = method.Type;
                    var module = type.Module;

                    if (FilterModule != null)
                    {
                        var match = FilterModule.Match(module.ToString());

                        if (!match.Success)
                        {
                            continue;
                        }
                    }

                    if (FilterType != null)
                    {
                        var match = FilterType.Match(method.Type.ToString());

                        if (!match.Success)
                        {
                            continue;
                        }
                    }

                    if (FilterMethod != null)
                    {
                        var match = FilterMethod.Match(method.ToString());

                        if (!match.Success)
                        {
                            continue;
                        }
                    }

                    methods.Add(method);
                    types.Add(type);
                    modules.Add(module);
                }
            }

            if (FilterMethod == null && FilterType != null)
            {
                foreach (var type in pd.Types)
                {
                    if (types.Contains(type))
                    {
                        continue;
                    }

                    var match = FilterType.Match(type.ToString());

                    if (!match.Success)
                    {
                        continue;
                    }

                    types.Add(type);
                }
            }

            if (Modules)
            {
                ColorWriteLine($"Modules:", ConsoleColor.Green);

                foreach (var module in modules)
                {
                    WriteLine($"\t{module.Mvid} {module.ToString ()}");
                }
            }

            if (Types)
            {
                ColorWriteLine($"Types:", ConsoleColor.Green);

                foreach (var type in types)
                {
                    WriteLine($"\t{type}");
                }
            }

            if (Methods)
            {
                ColorWriteLine($"Methods:", ConsoleColor.Green);

                foreach (var method in methods)
                {
                    WriteLine($"\t{method}");
                }
            }

            if (Summary)
            {
                ColorWriteLine($"Summary:", ConsoleColor.Green);
                WriteLine($"\tModules: {modules.Count.ToString ("N0"),10}{(modules.Count != pd.Modules.Length ? $"  (of {pd.Modules.Length})" : "" )}");
                WriteLine($"\tTypes:   {types.Count.ToString ("N0"),10}{(types.Count != pd.Types.Length ? $"  (of {pd.Types.Length})" : "")}");
                WriteLine($"\tMethods: {methods.Count.ToString ("N0"),10}{(methods.Count != pd.Methods.Length ? $"  (of {pd.Methods.Length})" : "")}");
            }

            if (!string.IsNullOrEmpty(Output))
            {
                if (Verbose)
                {
                    ColorWriteLine($"Going to write the profile to '{Output}'", ConsoleColor.Yellow);
                }
                var modulesArray = new ModuleRecord [modules.Count];
                modules.CopyTo(modulesArray, 0);
                var typesArray = new TypeRecord [types.Count];
                types.CopyTo(typesArray, 0);
                var updatedPD = new ProfileData(modulesArray, typesArray, methods.ToArray());

                using (var stream = new FileStream(Output, FileMode.Create)) {
                    var writer = new ProfileWriter();
                    writer.WriteAllData(stream, updatedPD);
                }
            }
        }