private static void DoMain(string[] args) { var flags = new[] { "b", "bytes", "c", "children", "h", "?", "help" }; var parsedArgs = CommandLineHelper.ParseArgs(flags, args); if (parsedArgs.flags.Contains("h") || parsedArgs.flags.Contains("help") || parsedArgs.flags.Contains("?")) { ShowHelp(); return; } try { if (parsedArgs.theRest.Length == 0) { throw new CommandLineArgumentException( "You need to provide either a process ID or a path to the process executable."); } if (!parsedArgs.args.TryGetValue("filter", out var eventNameFilter)) { eventNameFilter = string.Empty; } var traceSession = new TraceSession(new ConsoleTraceOutput(eventNameFilter)); var traceOptions = new TraceSession.TraceOptions { PrintPacketBytes = parsedArgs.flags.Contains("b") || parsedArgs.flags.Contains("bytes"), }; SetConsoleCtrlCHook(traceSession); var procArgs = parsedArgs.theRest; if (!int.TryParse(procArgs[0], out var pid)) { traceSession.TraceNewProcess(procArgs, traceOptions); } else { traceSession.TraceRunningProcess(pid, traceOptions); } } catch (Exception ex) when(ex is CommandLineArgumentException || ex is ArgumentException) { Console.WriteLine($"[error] {ex.Message}"); Console.WriteLine(); Console.WriteLine($"{AppName.Name} -help to see usage info."); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"[critical] {ex.Message}"); Console.WriteLine("If this error persists, please report it at https://github.com/lowleveldesign/dotnet-netrace/issues, " + "providing the below details."); Console.WriteLine("=== Details ==="); Console.WriteLine($"{ex.GetType()}: {ex.Message}"); Console.WriteLine(); Console.WriteLine($"Command line: {Environment.CommandLine}"); Console.WriteLine($"OS: {Environment.OSVersion}"); Console.WriteLine($"x64 (OS): {Environment.Is64BitOperatingSystem}"); Console.WriteLine($"x64 (Process): {Environment.Is64BitProcess}"); Console.WriteLine($"Exception details: {ex}"); } }
private static void DoMain(string[] args) { List <string> procargs = null; var showhelp = false; var spawnNewConsoleWindow = false; var traceOptions = new TraceSession.TraceOptions(); string eventNameFilter = null; var p = new OptionSet { { "f|filter=", "Display only events which names contain the given keyword " + "(case insensitive). Does not impact the summary.", v => { eventNameFilter = v; } }, { "b|bytes", "Dump packet bytes to the console.", v => { traceOptions.PrintPacketBytes = v != null; } }, { "c|children", "Trace process and all its children.", v => { traceOptions.TraceChildProcesses = v != null; } }, { "newconsole", "Start the process in a new console window.", v => { spawnNewConsoleWindow = v != null; } }, { "h|help", "Show this message and exit.", v => showhelp = v != null }, { "?", "Show this message and exit.", v => showhelp = v != null } }; try { procargs = p.Parse(args); } catch (OptionException ex) { Console.Error.Write("ERROR: invalid argument"); Console.Error.WriteLine(ex.Message); Console.Error.WriteLine(); showhelp = true; } catch (FormatException) { Console.Error.WriteLine("ERROR: invalid number in one of the constraints"); Console.Error.WriteLine(); showhelp = true; } if (traceOptions.TraceChildProcesses && TraceEventSession.IsElevated() != true) { Console.Error.WriteLine("Must run elevated (Admin) to trace process children."); return; } if (!showhelp && procargs != null && procargs.Count == 0) { Console.Error.WriteLine("ERROR: please provide either process name or PID"); Console.Error.WriteLine(); showhelp = true; } if (showhelp) { ShowHelp(p); return; } // for diagnostics information #if DEBUG Trace.Listeners.Add(new ConsoleTraceListener()); #endif var traceSession = new TraceSession(new ConsoleTraceOutput(eventNameFilter)); SetConsoleCtrlCHook(traceSession); try { if (!int.TryParse(procargs[0], out var pid)) { traceSession.TraceNewProcess(procargs, spawnNewConsoleWindow, traceOptions); } else { traceSession.TraceRunningProcess(pid, traceOptions); } } catch (COMException ex) { if ((uint)ex.HResult == 0x800700B7) { Console.Error.WriteLine("ERROR: could not start the kernel logger - make sure it is not running."); } } catch (Win32Exception ex) { Console.Error.WriteLine( $"ERROR: an error occurred while trying to start or open the process, hr: 0x{ex.HResult:X8}, " + $"code: 0x{ex.NativeErrorCode:X8} ({ex.Message})."); } #if !DEBUG catch (Exception ex) { Console.Error.WriteLine($"ERROR: severe error happened when starting application: {ex.Message}"); } #endif }