public void DescribeCandidateProcesses(IList<IProcessInfo> candidates, ConsoleLog console)
        {
            var byArchitecture = candidates.ToLookup(c => c.Architecture);
            var self = ProcessArchitecture.FromCurrentProcess();

            if(self is ProcessArchitecture.x86)
            {
                if(!x86Thunk.Bootstrap.WasUsed)
                {
                    // Native x86. Write out everything.
                    WriteProcessList(candidates, console);
                }
                else
                {
                    // Write out only matching processes.
                    WriteProcessList(byArchitecture[self].ToList(), console);
                }
            }
            else
            {
                WriteProcessList(byArchitecture[self].ToList(), console);
                if(byArchitecture[new ProcessArchitecture.x86()].Any())
                {
                    // Go to 32-bit and render the rest of the process information.
                    Bootstrap.RecurseInto32BitProcess();
                }
            }
        }
示例#2
0
 public void LogRequestsTo(ConsoleLog log)
 {
     CancelRequested += (s, e) => log.WriteLine("Shutting down.");
     KillRequested += (s, e) => {
         log.WriteLine(e == TerminationReason.ControlBreak   ? "CTRL-Break pressed. Terminating."
                     : e == TerminationReason.ControlC       ? "CTRL-C pressed twice. Terminating."
                     : "Terminating.");
     };
 }
示例#3
0
        public static int Main(string[] args)
        {
            if (!AssertSufficientDotNetVersion()) return 255;

            var arguments = new Arguments();
            var options = CreateOptions(arguments);
            try
            {
                var remainingArgs = options.Parse(args).ToArray();
                arguments.ParseRemaining(ref remainingArgs);

                var console = new ConsoleLog(Console.Error, arguments.Verbose);
                var jobFactory = SelectFactory(arguments.JobType ?? JobType.DumpStacks);
                if (jobFactory == null) throw new ErrorWithExitCodeException(1, $"Unsupported operation: {arguments.JobType}") { ShowUsage = true };

                var configuredFactory = jobFactory.Configure(ref remainingArgs, arguments.ActivelyAttachToProcess);

                var process = ResolveTargetProcess(arguments, console);
                var job = configuredFactory.CreateJob(process);

                console.WriteLineVerbose($"Running as a {ProcessArchitecture.FromCurrentProcess().Describe()} process.");
                ExecuteJob(console, job);

                return 0;
            }
            catch (Requires32BitEnvironmentException)
            {
                return Bootstrap.RecurseInto32BitProcess();
            }
            catch (Requires64BitEnvironmentException ex)
            {
                Console.Error.WriteLine(ex.Message);
                return 64;
            }
            catch(ErrorWithExitCodeException ex)
            {
                if(!String.IsNullOrEmpty(ex.Message))
                {
                    Console.Error.WriteLine(ex.Message);
                }
                if(ex.ShowUsage)
                {
                    ShowUsage(arguments.JobType, options);
                }
                return ex.ExitCode;
            }
            catch (Exception ex)
            {
                // Otherwise-unhandled exception.
                Console.Error.WriteLine(ex);
                return 255;
            }
        }
        private void WriteProcessList(IList<IProcessInfo> processes, ConsoleLog console)
        {
            if(!processes.Any()) return;
            var tabulator = new Tabulator(
                new Column("Pid") { Width = 5, RightAlign = true},
                new Column("Name") { Width = 20},
                new Column("Memory") { Width = 12, RightAlign = true},
                new Column("CLR Versions"))
            { Defaults = { Padding = 2 } };

            console.WriteLine($"{processes.Count} matching {ProcessArchitecture.FromCurrentProcess().Describe()} processes were found:");
            console.WriteLine(tabulator.GetHeader());
            foreach (var candidate in processes.OrderBy(c => c.Pid))
            {
                console.WriteLine(tabulator.Tabulate(candidate.Pid, candidate.Name, $"{candidate.WorkingSetSizeBytes.InKilobytes()} KB", DescribeCLRVersions(candidate)));
            }
        }
示例#5
0
 public void Run(TextWriter output, ConsoleLog console)
 {
     var allHeapInfo = CollectHeapInfo();
        WriteHeapStatistics(allHeapInfo, output);
 }
示例#6
0
 private static IProcessInfo ResolveTargetProcess(Arguments arguments, ConsoleLog console)
 {
     var processResolver = new ProcessResolver(new ProcessFinder());
     try
     {
         return processResolver.ResolveTargetProcess(arguments.Pid, arguments.ProcessName);
     }
     catch(ProcessNotSpecifiedException ex)
     {
         throw new ErrorWithExitCodeException(1, ex.Message) { ShowUsage = true };
     }
     catch(ProcessNotFoundException ex) when(ex.Candidates.Any())
     {
         new ProcessListDescriber().DescribeCandidateProcesses(ex.Candidates.ToList(), console);
         if(Bootstrap.WasUsed) throw ErrorWithExitCodeException.Propagate(3);
         throw new ErrorWithExitCodeException(3, "Please specify a unique process Id using the -p switch.");
     }
     catch(ProcessNotFoundException ex)
     {
         throw new ErrorWithExitCodeException(3, ex.Message);
     }
 }
示例#7
0
 private static void ExecuteJob(ConsoleLog console, IDebugJob job)
 {
     try
     {
         job.Run(Console.Out, console);
     }
     catch (NoClrModulesFoundException ex)
     {
         throw new ErrorWithExitCodeException(2, ex);
     }
 }