From 3778fb040a9bfa1dbda207eae01219231802c7a9 Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Mon, 23 May 2022 20:51:43 +0300 Subject: [PATCH 1/2] Use Environment.GetFolderPath to obtain home directory --- .../SymbolService.cs | 2 +- src/SOS/SOS.InstallHelper/InstallHelper.cs | 17 +++-------------- src/Tools/dotnet-dump/Analyzer.cs | 8 +------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs index 97b82ac458..2e978d981d 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs @@ -79,7 +79,7 @@ public string DefaultSymbolCache } else { - _defaultSymbolCache = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".dotnet", "symbolcache"); + _defaultSymbolCache = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".dotnet", "symbolcache"); } } return _defaultSymbolCache; diff --git a/src/SOS/SOS.InstallHelper/InstallHelper.cs b/src/SOS/SOS.InstallHelper/InstallHelper.cs index b9753f1a4f..20668459d0 100644 --- a/src/SOS/SOS.InstallHelper/InstallHelper.cs +++ b/src/SOS/SOS.InstallHelper/InstallHelper.cs @@ -57,21 +57,10 @@ public InstallHelper(Action writeLine, Architecture? architecture = null { m_writeLine = writeLine; string rid = GetRid(architecture); - string home; + string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - home = Environment.GetEnvironmentVariable("USERPROFILE"); - if (string.IsNullOrEmpty(home)) { - throw new SOSInstallerException("USERPROFILE environment variable not found"); - } - } - else - { - home = Environment.GetEnvironmentVariable("HOME"); - if (string.IsNullOrEmpty(home)) { - throw new SOSInstallerException("HOME environment variable not found"); - } LLDBInitFile = Path.Combine(home, ".lldbinit"); } InstallLocation = Path.GetFullPath(Path.Combine(home, ".dotnet", "sos")); @@ -378,4 +367,4 @@ public SOSInstallerException(string message, Exception inner) { } } -} \ No newline at end of file +} diff --git a/src/Tools/dotnet-dump/Analyzer.cs b/src/Tools/dotnet-dump/Analyzer.cs index 0e284c974c..960d018f25 100644 --- a/src/Tools/dotnet-dump/Analyzer.cs +++ b/src/Tools/dotnet-dump/Analyzer.cs @@ -64,13 +64,7 @@ public Task Analyze(FileInfo dump_path, string[] command) _consoleProvider.WriteLine($"Loading core dump: {dump_path} ..."); // Attempt to load the persisted command history - string dotnetHome; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - dotnetHome = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), ".dotnet"); - } - else { - dotnetHome = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".dotnet"); - } + string dotnetHome = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".dotnet"); string historyFileName = Path.Combine(dotnetHome, "dotnet-dump.history"); try { From d38c00f4fc650cff4b0fa65ba810354b407054ee Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Mon, 23 May 2022 20:58:51 +0300 Subject: [PATCH 2/2] Format documents in changeset --- .../SymbolService.cs | 30 +++++++------- src/SOS/SOS.InstallHelper/InstallHelper.cs | 41 ++++++++++++------- src/Tools/dotnet-dump/Analyzer.cs | 38 +++++++++-------- 3 files changed, 64 insertions(+), 45 deletions(-) diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs index 2e978d981d..b7fba25079 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/SymbolService.cs @@ -84,7 +84,7 @@ public string DefaultSymbolCache } return _defaultSymbolCache; } - set + set { _defaultSymbolCache = value; } @@ -182,7 +182,7 @@ void ParseServer(int start) symbolCachePaths.Add(DefaultSymbolCache); } } - else + else { symbolCachePaths.Add(parts[i]); } @@ -461,10 +461,10 @@ public ImmutableArray GetMetadata(string imagePath, uint imageTimestamp, u } } } - catch (Exception ex) when - (ex is UnauthorizedAccessException || - ex is BadImageFormatException || - ex is InvalidVirtualAddressException || + catch (Exception ex) when + (ex is UnauthorizedAccessException || + ex is BadImageFormatException || + ex is InvalidVirtualAddressException || ex is IOException) { Trace.TraceError($"GetMetaData: {ex.Message}"); @@ -493,7 +493,7 @@ public ISymbolFile OpenSymbolFile(string assemblyPath, bool isFileLayout, Stream peStream = Utilities.TryOpenFile(assemblyPath); if (peStream == null) return null; - + options = PEStreamOptions.Default; } @@ -606,7 +606,7 @@ private string DownloadPE(IModule module, KeyTypeFlags flags) Trace.TraceWarning($"DownLoadPE: no key generated for module {fileName} "); return null; } - } + } else if ((flags & KeyTypeFlags.SymbolKey) != 0) { IEnumerable pdbInfos = module.GetPdbFileInfos(); @@ -647,7 +647,7 @@ private string DownloadPE(IModule module, KeyTypeFlags flags) return null; } } - else + else { throw new ArgumentException($"Key flag not supported {flags}"); } @@ -829,7 +829,7 @@ private SymbolFile TryOpenReaderFromCodeView(PEReader peReader, DebugDirectoryEn string pdbPath = data.Path; Stream pdbStream = null; - if (assemblyPath != null) + if (assemblyPath != null) { try { @@ -916,7 +916,7 @@ private SymbolFile TryOpenReaderFromEmbeddedPdb(PEReader peReader, DebugDirector public override string ToString() { StringBuilder sb = new StringBuilder(); - ForEachSymbolStore((symbolStore) => + ForEachSymbolStore((symbolStore) => { if (symbolStore is HttpSymbolStore httpSymbolStore) { @@ -962,7 +962,7 @@ private void SetSymbolStore(Microsoft.SymbolStore.SymbolStores.SymbolStore store } } - private bool IsDuplicateSymbolStore(Microsoft.SymbolStore.SymbolStores.SymbolStore symbolStore, Func match) + private bool IsDuplicateSymbolStore(Microsoft.SymbolStore.SymbolStores.SymbolStore symbolStore, Func match) where T : Microsoft.SymbolStore.SymbolStores.SymbolStore { while (symbolStore != null) @@ -1006,7 +1006,7 @@ public void ForEachSymbolStore(Action callback) /// Last component of path internal static string GetFileName(string pathName) { - int pos = pathName.LastIndexOfAny(new char[] { '/', '\\'}); + int pos = pathName.LastIndexOfAny(new char[] { '/', '\\' }); if (pos < 0) { return pathName; @@ -1019,11 +1019,11 @@ internal static string GetFileName(string pathName) /// private static bool IsPathEqual(string path1, string path2) { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return StringComparer.OrdinalIgnoreCase.Equals(path1, path2); } - else + else { return string.Equals(path1, path2); } diff --git a/src/SOS/SOS.InstallHelper/InstallHelper.cs b/src/SOS/SOS.InstallHelper/InstallHelper.cs index 20668459d0..a89cfe5485 100644 --- a/src/SOS/SOS.InstallHelper/InstallHelper.cs +++ b/src/SOS/SOS.InstallHelper/InstallHelper.cs @@ -59,7 +59,7 @@ public InstallHelper(Action writeLine, Architecture? architecture = null string rid = GetRid(architecture); string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { LLDBInitFile = Path.Combine(home, ".lldbinit"); } @@ -76,16 +76,20 @@ public void Install() { WriteLine("Installing SOS to {0}", InstallLocation); - if (string.IsNullOrEmpty(SOSNativeSourcePath) || string.IsNullOrEmpty(SOSManagedSourcePath)) { + if (string.IsNullOrEmpty(SOSNativeSourcePath) || string.IsNullOrEmpty(SOSManagedSourcePath)) + { throw new SOSInstallerException("SOS source path not valid"); } - if (!Directory.Exists(SOSNativeSourcePath)) { + if (!Directory.Exists(SOSNativeSourcePath)) + { throw new SOSInstallerException($"Operating system or architecture not supported: installing from {SOSNativeSourcePath}"); } - if (!Directory.Exists(SOSManagedSourcePath)) { + if (!Directory.Exists(SOSManagedSourcePath)) + { throw new SOSInstallerException($"Invalid SOS source directory {SOSManagedSourcePath}"); } - if (string.IsNullOrEmpty(InstallLocation)) { + if (string.IsNullOrEmpty(InstallLocation)) + { throw new SOSInstallerException($"Installation path {InstallLocation} not valid"); } @@ -128,10 +132,12 @@ public void Install() }); // Configure lldb - if (LLDBInitFile != null) { + if (LLDBInitFile != null) + { Configure(); } - else { + else + { WriteLine($"Execute '.load {InstallLocation}\\sos.dll' to load SOS in your Windows debugger."); } @@ -196,7 +202,8 @@ public void Uninstall() /// public void Configure(bool remove = false) { - if (string.IsNullOrEmpty(LLDBInitFile)) { + if (string.IsNullOrEmpty(LLDBInitFile)) + { throw new SOSInstallerException("No lldb configuration file path"); } bool changed = false; @@ -232,7 +239,8 @@ public void Configure(bool remove = false) } } - if (markerFound) { + if (markerFound) + { throw new SOSInstallerException(".lldbinit file end marker not found"); } } @@ -245,7 +253,8 @@ public void Configure(bool remove = false) string extension = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so"; lines.Add($"plugin load {plugin}{extension}"); - if (EnableSymbolServer) { + if (EnableSymbolServer) + { lines.Add(string.Format("setsymbolserver -ms")); } lines.Add(InitFileEnd); @@ -255,10 +264,12 @@ public void Configure(bool remove = false) // If there is anything to write, write the lldb init file if (changed) { - if (remove) { + if (remove) + { WriteLine("Reverting {0} file - LLDB will no longer load SOS at startup", LLDBInitFile); } - else { + else + { WriteLine("{0} {1} file - LLDB will load SOS automatically at startup", existing ? "Updating existing" : "Creating new", LLDBInitFile); } RetryOperation($"Problem writing lldb init file {LLDBInitFile}", () => File.WriteAllLines(LLDBInitFile, lines.ToArray())); @@ -292,7 +303,8 @@ private void RetryOperation(string errorMessage, Action operation) } catch (Exception ex) when (ex is ArgumentException || ex is UnauthorizedAccessException || ex is SecurityException) { - if (errorMessage == null) { + if (errorMessage == null) + { return; } throw new SOSInstallerException($"{errorMessage}: {ex.Message}", ex); @@ -301,7 +313,8 @@ private void RetryOperation(string errorMessage, Action operation) if (lastfailure != null) { - if (errorMessage == null) { + if (errorMessage == null) + { return; } throw new SOSInstallerException($"{errorMessage}: {lastfailure.Message}", lastfailure); diff --git a/src/Tools/dotnet-dump/Analyzer.cs b/src/Tools/dotnet-dump/Analyzer.cs index 960d018f25..7532b870df 100644 --- a/src/Tools/dotnet-dump/Analyzer.cs +++ b/src/Tools/dotnet-dump/Analyzer.cs @@ -47,7 +47,8 @@ public Analyzer() _serviceProvider.AddService(_contextService); _serviceProvider.AddServiceFactory(() => SOSLibrary.Create(this)); - _contextService.ServiceProvider.AddServiceFactory(() => { + _contextService.ServiceProvider.AddServiceFactory(() => + { ClrRuntime clrRuntime = _contextService.Services.GetService(); return clrRuntime != null ? new ClrMDHelper(clrRuntime) : null; }); @@ -71,10 +72,10 @@ public Task Analyze(FileInfo dump_path, string[] command) string[] history = File.ReadAllLines(historyFileName); _consoleProvider.AddCommandHistory(history); } - catch (Exception ex) when - (ex is IOException || - ex is UnauthorizedAccessException || - ex is NotSupportedException || + catch (Exception ex) when + (ex is IOException || + ex is UnauthorizedAccessException || + ex is NotSupportedException || ex is SecurityException) { } @@ -83,11 +84,12 @@ ex is NotSupportedException || LoadExtensions(); try - { + { using DataTarget dataTarget = DataTarget.LoadDump(dump_path.FullName); OSPlatform targetPlatform = dataTarget.DataReader.TargetPlatform; - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || dataTarget.DataReader.EnumerateModules().Any((module) => Path.GetExtension(module.FileName) == ".dylib")) { + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || dataTarget.DataReader.EnumerateModules().Any((module) => Path.GetExtension(module.FileName) == ".dylib")) + { targetPlatform = OSPlatform.OSX; } _target = new TargetFromDataReader(dataTarget.DataReader, targetPlatform, this, _targetIdFactory++, dump_path.FullName); @@ -106,7 +108,8 @@ ex is NotSupportedException || foreach (string cmd in command) { _commandService.Execute(cmd, _contextService.Services); - if (_consoleProvider.Shutdown) { + if (_consoleProvider.Shutdown) + { break; } } @@ -117,7 +120,8 @@ ex is NotSupportedException || _consoleProvider.WriteLine("Ready to process analysis commands. Type 'help' to list available commands or 'help [command]' to get detailed help on a command."); _consoleProvider.WriteLine("Type 'quit' or 'exit' to exit the session."); - _consoleProvider.Start((string commandLine, CancellationToken cancellation) => { + _consoleProvider.Start((string commandLine, CancellationToken cancellation) => + { _commandService.Execute(commandLine, _contextService.Services); }); } @@ -146,10 +150,10 @@ ex is InvalidOperationException || { File.WriteAllLines(historyFileName, _consoleProvider.GetCommandHistory()); } - catch (Exception ex) when - (ex is IOException || - ex is UnauthorizedAccessException || - ex is NotSupportedException || + catch (Exception ex) when + (ex is IOException || + ex is UnauthorizedAccessException || + ex is NotSupportedException || ex is SecurityException) { } @@ -171,14 +175,16 @@ ex is NotSupportedException || public void DestroyTarget(ITarget target) { - if (target == null) { + if (target == null) + { throw new ArgumentNullException(nameof(target)); } if (target == _target) { _target = null; _contextService.ClearCurrentTarget(); - if (target is IDisposable disposable) { + if (target is IDisposable disposable) + { disposable.Dispose(); } } @@ -215,7 +221,7 @@ private void LoadExtension(string extensionPath) { assembly = Assembly.LoadFrom(extensionPath); } - catch (Exception ex) when (ex is IOException || ex is ArgumentException || ex is BadImageFormatException || ex is System.Security.SecurityException) + catch (Exception ex) when (ex is IOException || ex is ArgumentException || ex is BadImageFormatException || ex is System.Security.SecurityException) { _consoleProvider.WriteLineError($"Extension load {extensionPath} FAILED {ex.Message}"); }