// Executes file_name without creating a Window and without using the shell // with the parameters. Waits that it finishes it. Returns the stdout and stderr. private static Result runDo(string file_name, List <string> parameters) { Process process = new Process(); ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.FileName = file_name; string parameters_string = ""; foreach (string parameter in parameters) { parameters_string += CommandLineEncoder.EncodeArgText(parameter) + " "; } processStartInfo.Arguments = parameters_string; LogB.Debug("ExecuteProcess FileName: " + processStartInfo.FileName); LogB.Debug("ExecuteProcess Arguments: " + processStartInfo.Arguments); processStartInfo.CreateNoWindow = true; processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardInput = false; processStartInfo.RedirectStandardError = true; processStartInfo.RedirectStandardOutput = true; process.StartInfo = processStartInfo; try { process.Start(); } catch (Exception e) { string errorMessage = String.Format(Catalog.GetString("Cannot start:\n" + "{0}\n" + "with the parameters:" + "{1}\n" + "Exception:\n" + "{2}"), processStartInfo.FileName, parameters_string, e.Message); LogB.Warning(errorMessage); return(new Result("", "", Result.ERROR_CANT_START, errorMessage)); } string stdout = process.StandardOutput.ReadToEnd().TrimEnd('\n'); string stderr = process.StandardError.ReadToEnd().TrimEnd('\n'); process.WaitForExit(); if (stderr != "") { LogB.Warning(String.Format("Executed: {0} Parameters: {1} Stdout: {2} Stderr: {3}", processStartInfo.FileName, parameters_string, stdout, stderr)); } int exitCode = process.ExitCode; return(new Result(stdout, stderr, exitCode)); }
void _loadSourceInfo(ulong address) { var process = new Process { StartInfo = { FileName = "atos", Arguments = $"-o \"{CommandLineEncoder.EncodeArgText(_bin.Path)}\" {address:X16}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; process.Start(); process.WaitForExit(); if (process.ExitCode != 0) { throw new InvalidOperationException(); } while (!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine().Trim(); var match = Regex.Match(line, @"^([a-zA-Z0-9_]+) \((.*)\) (\((.*)\)|\+ [0-9]+)$"); var funcName = match.Groups[1].Value; var binName = match.Groups[2].Value; var fileName = match.Groups[4].Value; if (fileName.Contains(":")) { match = Regex.Match(fileName, @"^(.+):([0-9]+)$"); var filePath = match.Groups[1].Value; var lineNumber = uint.Parse(match.Groups[2].Value); _sourceLineInfo.Add(address, new SourceLineInfo { FilePath = filePath, LineNumber = lineNumber, FunctionName = funcName }); } else { _sourceLineInfo.Add(address, new SourceLineInfo { FunctionName = funcName }); } } }
/* * run a process on the background, eg: read rfid. * don't call WaitForExit(), kill it on Chronojump exit * returns false if there are problems calling it */ public static bool RunAtBackground(Process process, string file_name, List <string> parameters) { if (!File.Exists(parameters[0])) { LogB.Debug("ExecuteProcess does not exist parameter: " + parameters[0]); return(false); } ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.FileName = file_name; string parameters_string = ""; foreach (string parameter in parameters) { parameters_string += CommandLineEncoder.EncodeArgText(parameter) + " "; } processStartInfo.Arguments = parameters_string; LogB.Debug("ExecuteProcess FileName: " + processStartInfo.FileName); LogB.Debug("ExecuteProcess Arguments: " + processStartInfo.Arguments); processStartInfo.CreateNoWindow = true; processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardInput = false; processStartInfo.RedirectStandardError = true; processStartInfo.RedirectStandardOutput = true; process.StartInfo = processStartInfo; try { process.Start(); } catch (Exception e) { string errorMessage = String.Format(Catalog.GetString("Cannot start:\n" + "{0}\n" + "with the parameters:" + "{1}\n" + "Exception:\n" + "{2}"), processStartInfo.FileName, parameters_string, e.Message); LogB.Warning(errorMessage); return(false); } return(true); }
/* * run a process on the background, eg: read rfid. * don't call WaitForExit(), kill it on Chronojump exit * returns false if there are problems calling it */ public static bool RunAtBackground(ref Process process, string file_name, List <string> parameters, bool createNoWindow, bool useShellExecute, bool redirectInput, bool redirectOutput, bool redirectStderr) { ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.FileName = file_name; string parameters_string = ""; foreach (string parameter in parameters) { parameters_string += CommandLineEncoder.EncodeArgText(parameter) + " "; } processStartInfo.Arguments = parameters_string; LogB.Debug("ExecuteProcess FileName: " + processStartInfo.FileName); LogB.Debug("ExecuteProcess Arguments: " + processStartInfo.Arguments); processStartInfo.CreateNoWindow = createNoWindow; processStartInfo.UseShellExecute = useShellExecute; processStartInfo.RedirectStandardInput = redirectInput; //note UseShellExecute has to be false to be able to redirect processStartInfo.RedirectStandardError = redirectOutput; processStartInfo.RedirectStandardOutput = redirectStderr; process.StartInfo = processStartInfo; try { process.Start(); } catch (Exception e) { string errorMessage = String.Format(Catalog.GetString("Cannot start:\n" + "{0}\n" + "with the parameters:" + "{1}\n" + "Exception:\n" + "{2}"), processStartInfo.FileName, parameters_string, e.Message); LogB.Warning(errorMessage); return(false); } return(true); }
public static Dictionary <ulong, ISourceLineInfo> LoadSourceInfo(ELFBinInfo bin, IEnumerable <ulong> addresses, string toolPath, string toolPrefix, string toolName = "addr2line") { var addressBuff = new StringBuilder(); foreach (var address in addresses) { addressBuff.Append($" {address:X16}"); } var addr2linePath = Path.GetFullPath(Path.Combine(toolPath, toolPrefix + toolName)); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { addr2linePath = addr2linePath + ".exe"; } if (!File.Exists(addr2linePath)) { throw new Exception("addr2line missing: " + addr2linePath); } var process = new Process { StartInfo = { FileName = addr2linePath, Arguments = $"-a -p -f -e \"{CommandLineEncoder.EncodeArgText(bin.Path)}\" {addressBuff}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; process.Start(); process.WaitForExit(); if (process.ExitCode != 0) { throw new InvalidOperationException(); } var sourceLineInfo = new Dictionary <ulong, ISourceLineInfo>(); while (!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine().Trim(); if (Regex.IsMatch(line, @"^0x([0-9a-fA-F]{8,16}): .*")) { var addrMatch = Regex.Match(line, @"^0x([0-9a-fA-F]{8,16}): .*"); var address = ulong.Parse(addrMatch.Groups[1].Value, NumberStyles.AllowHexSpecifier); if (Regex.IsMatch(line, @"^0x([0-9a-fA-F]{8,16}): ([a-zA-Z0-9_]+) at (.*)$")) { var nameMatch = Regex.Match(line, @"^0x([0-9a-fA-F]{8,16}): ([a-zA-Z0-9_]+) at (.*)$"); var funcName = nameMatch.Groups[2].Value; var fileName = nameMatch.Groups[3].Value; if (fileName.Contains(":")) { nameMatch = Regex.Match(fileName, @"^(.+):(\?|[0-9]+)(.*)$"); var filePath = nameMatch.Groups[1].Value; filePath = new FileInfo(filePath).FullName; if (filePath.StartsWith(Directory.GetCurrentDirectory())) { filePath = filePath.Substring(Directory.GetCurrentDirectory().Length + 1); } if (nameMatch.Groups[2].Value.Length > 0 && nameMatch.Groups[2].Value != "?") { var lineNumber = uint.Parse(nameMatch.Groups[2].Value); sourceLineInfo.Add(address, new SourceLineInfo { FilePath = filePath, LineNumber = lineNumber, FunctionName = funcName }); } else { sourceLineInfo.Add(address, new SourceLineInfo { FilePath = filePath, FunctionName = funcName }); } } else { sourceLineInfo.Add(address, new SourceLineInfo { FilePath = fileName }); } } else { throw new KeyNotFoundException($"Cannot resolve {address:X16}"); } } } return(sourceLineInfo); }
public static Dictionary <string, List <ISymbolInfo> > LoadSymbolInfo(IBinInfo binPath, string toolPath = "", string toolPrefix = "", string toolName = "nm") { var nmPath = Path.GetFullPath(Path.Combine(toolPath, toolPrefix + toolName)); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { nmPath = nmPath + ".exe"; } Console.WriteLine(binPath.Path); if (!File.Exists(nmPath)) { throw new Exception("nm missing: " + nmPath); } var process = new Process { StartInfo = { FileName = nmPath, Arguments = $"-S \"{CommandLineEncoder.EncodeArgText(binPath.Path)}\"", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; process.Start(); var symbolInfo = new Dictionary <string, List <ISymbolInfo> >(); while (!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine()?.Trim(); var matchWithSize = Regex.Match(line, @"^([0-9a-fA-F]+) ([0-9a-fA-F]+) ([a-zA-Z]) ([a-zA-Z0-9_]+)$"); var matchWithoutSize = Regex.Match(line, @"^([0-9a-fA-F]+) ([a-zA-Z]) ([a-zA-Z0-9_]+)$"); if (matchWithSize.Success) { var address = ulong.Parse(matchWithSize.Groups[1].Value, NumberStyles.HexNumber); var size = ulong.Parse(matchWithSize.Groups[2].Value, NumberStyles.HexNumber); var funcName = matchWithSize.Groups[4].Value; if (!symbolInfo.ContainsKey(funcName)) { symbolInfo.Add(funcName, new List <ISymbolInfo>()); } symbolInfo[funcName].Add(new SymbolInfo { Address = address, Size = size, Name = funcName }); } else if (matchWithoutSize.Success) { var address = ulong.Parse(matchWithoutSize.Groups[1].Value, NumberStyles.HexNumber); var funcName = matchWithoutSize.Groups[3].Value; if (!symbolInfo.ContainsKey(funcName)) { symbolInfo.Add(funcName, new List <ISymbolInfo>()); } symbolInfo[funcName].Add(new SymbolInfo { Address = address, Size = 0, Name = funcName }); } } process.WaitForExit(); if (process.ExitCode != 0) { throw new InvalidOperationException(); } return(symbolInfo); }