Beispiel #1
0
        static void Main(string[] args)
        {
            var retCode = ArgParser.Parse(args, new string[0], new string[] { "-pids" });

            if (retCode != ErrorCode.Success)
            {
                PrintSyntaxAndExit(retCode);
            }

            List <ProcessInfo> procInfos;

            if (ArgParser.SwitchesWithValues.ContainsKey("-pids"))
            {
                var pids = ParseAndValidatePids();
                procInfos = pids.Select(x => ProcessHelper.GetProcessDetailsByPid(x)).ToList();
            }
            else
            {
                procInfos = ProcessHelper.GetProcessDetails(args[0]);
            }

            Console.WriteLine($"Number of matching processes: {procInfos.Count}");
            Console.WriteLine();

            if (procInfos.Any())
            {
                Console.WriteLine($"Data collection started.");
                Console.WriteLine();
            }

            for (int i = 0; i < procInfos.Count; i++)
            {
                int pid = procInfos[i].PID;

                Console.WriteLine($"Process {i + 1}/{procInfos.Count}");

                if (string.IsNullOrWhiteSpace(procInfos[i].Name))
                {
                    Console.WriteLine($"WARNING: Process with pid '{pid}' does not exist.");
                    Console.WriteLine();
                    continue;
                }

                Console.WriteLine($"Process: {procInfos[i].Name} ({pid})");
                Console.WriteLine($"CmdLine: {procInfos[i].CmdLine}");
                var wrapper = ClrMdHelper.AttachToLiveProcess(pid);

                wrapper.TypesToDump.Add(TYPE_ServiceDescription);

                wrapper.ClrHeapIsNotWalkableCallback = () =>
                {
                    Console.WriteLine("PID: {0} - Cannot walk the heap!", pid);
                };

                wrapper.ClrObjectOfTypeFoundCallback = DumpTypes;

                wrapper.Process();

                Console.WriteLine();
            }

            if (procInfos.Any())
            {
                Console.WriteLine($"Data collection completed.");
                Console.WriteLine();
            }

            // Display results
            foreach (var group in RESULTS.GroupBy(x => x.Pid))
            {
                var proc = procInfos.First(x => x.PID == group.Key);
                Console.WriteLine($"Displaying data for:");
                Console.WriteLine($"\tProcess: {proc.Name} ({proc.PID})");
                Console.WriteLine($"\tCmdLine: {proc.CmdLine}");
                Console.WriteLine();

                int cnt    = 0;
                int cntAll = group.Count();

                foreach (var result in group)
                {
                    Console.WriteLine($"ServiceDescription {++cnt}/{cntAll}");
                    Console.WriteLine($"----------------------");
                    Console.WriteLine();
                    Console.WriteLine("ServiceBehaviors: ");

                    foreach (var svcBehavior in result.ServiceBehaviors)
                    {
                        Console.WriteLine($"\t{svcBehavior}");
                    }

                    Console.WriteLine();
                    Console.WriteLine("ServiceEndpoints:");

                    foreach (var svcEndpoint in result.ServiceEndpoints)
                    {
                        Console.WriteLine($"\t{proc.PID} | {svcEndpoint.Contract} | {svcEndpoint.CallbackContract ?? "<n/a>"} | {svcEndpoint.Uri}");
                        Console.WriteLine($"\t{svcEndpoint.BindingSecurity}");

                        if (svcEndpoint.EndpointBehaviors.Any())
                        {
                            Console.WriteLine();
                            Console.WriteLine("\tEndpointBehaviors:");

                            foreach (var endpointbehavior in svcEndpoint.EndpointBehaviors)
                            {
                                Console.WriteLine($"\t\t{endpointbehavior}");
                            }
                        }

                        if (svcEndpoint.ContractBehaviors.Any())
                        {
                            Console.WriteLine();
                            Console.WriteLine("\tContractBehaviors:");

                            foreach (var contractbehavior in svcEndpoint.ContractBehaviors)
                            {
                                Console.WriteLine($"\t\t{contractbehavior}");
                            }
                        }

                        if (svcEndpoint.ContractOperations.Any())
                        {
                            Console.WriteLine();
                            Console.WriteLine("\tOperations:");

                            foreach (var operation in svcEndpoint.ContractOperations)
                            {
                                Console.WriteLine($"\t\t{operation.OperationName}");
                                Console.WriteLine("\t\t\tOperationBehaviors:");

                                foreach (var opBehavior in operation.OperationBehaviors)
                                {
                                    Console.WriteLine($"\t\t\t\t{opBehavior}");
                                }
                            }
                        }
                    }

                    Console.WriteLine();
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var retCode = ArgParser.Parse(args, new string[] { "-h", "-?", "/?" }, new string[] { "-a", "-d", "-pid" });

            ValidateArguments(retCode);

            string switchArg = ArgParser.Switches.FirstOrDefault();

            if (switchArg != null)
            {
                switch (switchArg)
                {
                case "-h":
                case "-?":
                case "/?":
                {
                    PrintSyntaxAndExit(retCode);
                    break;
                }
                }
            }

            var kvp = ArgParser.SwitchesWithValues.FirstOrDefault();

            if (!kvp.Equals(default(KeyValuePair <string, string>)))
            {
                switch (kvp.Key)
                {
                case "-a":
                {
                    var assemblies = kvp.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var assembly in assemblies)
                    {
                        ValidateFile(assembly);
                    }

                    var aggrCat = new AggregateCatalog(assemblies.Select(x => new AssemblyCatalog(x)));

                    var RESULT = new List <ReflectionComposablePart>();

                    using (var container = new CompositionContainer(aggrCat))
                    {
                        foreach (var part in container.Catalog.Parts)
                        {
                            var rfc = new ReflectionComposablePart();
                            rfc.TypeName = part.ToString();

                            foreach (var import in part.ImportDefinitions)
                            {
                                var      impDef = new ImportDefinition();
                                string[] s      = ParseImportDefinition(import.ToString());
                                impDef.ContractName         = s[0].Substring(s[0].IndexOf("\t") + 1);
                                impDef.RequiredTypeIdentity = s[1].Substring(s[1].IndexOf("\t") + 1);
                                rfc.Imports.Add(impDef);
                            }

                            foreach (var export in part.ExportDefinitions)
                            {
                                var expDef = new ExportDefinition();
                                expDef.ContractName = export.ContractName;
                                expDef.TypeIdentity = (string)export.Metadata[CONST_ExportTypeIdentity];
                                rfc.Exports.Add(expDef);
                            }

                            RESULT.Add(rfc);
                        }
                    }

                    DgmlHelper.CreateDgml($"{Guid.NewGuid().ToString()}.dgml", RESULT);
                    break;
                }

                case "-d":
                {
                    string dumpFile = kvp.Value;
                    ValidateFile(dumpFile);

                    var wrapper = ClrMdHelper.LoadDumpFile(dumpFile);
                    InitAndStartProcessing(wrapper);
                    break;
                }

                case "-pid":
                {
                    if (int.TryParse(kvp.Value, out int pid))
                    {
                        var wrapper = ClrMdHelper.AttachToLiveProcess(pid);
                        InitAndStartProcessing(wrapper);
                    }
                    else
                    {
                        Console.WriteLine($"ERROR: Invalid process id.");
                        Environment.Exit(1);
                    }
                    break;
                }
                }
            }
        }