Skip to content

Commit

Permalink
Use Environment.GetFolderPath to obtain home directory (#3087)
Browse files Browse the repository at this point in the history
* Use Environment.GetFolderPath to obtain home directory

* Format documents in changeset
  • Loading branch information
am11 authored May 24, 2022
1 parent 6f54867 commit eccaf50
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public string DefaultSymbolCache
}
else
{
_defaultSymbolCache = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".dotnet", "symbolcache");
_defaultSymbolCache = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".dotnet", "symbolcache");
}
}
return _defaultSymbolCache;
}
set
set
{
_defaultSymbolCache = value;
}
Expand Down Expand Up @@ -182,7 +182,7 @@ void ParseServer(int start)
symbolCachePaths.Add(DefaultSymbolCache);
}
}
else
else
{
symbolCachePaths.Add(parts[i]);
}
Expand Down Expand Up @@ -461,10 +461,10 @@ public ImmutableArray<byte> 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}");
Expand Down Expand Up @@ -493,7 +493,7 @@ public ISymbolFile OpenSymbolFile(string assemblyPath, bool isFileLayout, Stream
peStream = Utilities.TryOpenFile(assemblyPath);
if (peStream == null)
return null;

options = PEStreamOptions.Default;
}

Expand Down Expand Up @@ -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<PdbFileInfo> pdbInfos = module.GetPdbFileInfos();
Expand Down Expand Up @@ -647,7 +647,7 @@ private string DownloadPE(IModule module, KeyTypeFlags flags)
return null;
}
}
else
else
{
throw new ArgumentException($"Key flag not supported {flags}");
}
Expand Down Expand Up @@ -829,7 +829,7 @@ private SymbolFile TryOpenReaderFromCodeView(PEReader peReader, DebugDirectoryEn
string pdbPath = data.Path;
Stream pdbStream = null;

if (assemblyPath != null)
if (assemblyPath != null)
{
try
{
Expand Down Expand Up @@ -916,7 +916,7 @@ private SymbolFile TryOpenReaderFromEmbeddedPdb(PEReader peReader, DebugDirector
public override string ToString()
{
StringBuilder sb = new StringBuilder();
ForEachSymbolStore<Microsoft.SymbolStore.SymbolStores.SymbolStore>((symbolStore) =>
ForEachSymbolStore<Microsoft.SymbolStore.SymbolStores.SymbolStore>((symbolStore) =>
{
if (symbolStore is HttpSymbolStore httpSymbolStore)
{
Expand Down Expand Up @@ -962,7 +962,7 @@ private void SetSymbolStore(Microsoft.SymbolStore.SymbolStores.SymbolStore store
}
}

private bool IsDuplicateSymbolStore<T>(Microsoft.SymbolStore.SymbolStores.SymbolStore symbolStore, Func<T, bool> match)
private bool IsDuplicateSymbolStore<T>(Microsoft.SymbolStore.SymbolStores.SymbolStore symbolStore, Func<T, bool> match)
where T : Microsoft.SymbolStore.SymbolStores.SymbolStore
{
while (symbolStore != null)
Expand Down Expand Up @@ -1006,7 +1006,7 @@ public void ForEachSymbolStore<T>(Action<T> callback)
/// <returns>Last component of path</returns>
internal static string GetFileName(string pathName)
{
int pos = pathName.LastIndexOfAny(new char[] { '/', '\\'});
int pos = pathName.LastIndexOfAny(new char[] { '/', '\\' });
if (pos < 0)
{
return pathName;
Expand All @@ -1019,11 +1019,11 @@ internal static string GetFileName(string pathName)
/// </summary>
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);
}
Expand Down
56 changes: 29 additions & 27 deletions src/SOS/SOS.InstallHelper/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,10 @@ public InstallHelper(Action<string> 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"));
Expand All @@ -87,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");
}

Expand Down Expand Up @@ -139,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.");
}

Expand Down Expand Up @@ -207,7 +202,8 @@ public void Uninstall()
/// <exception cref="SOSInstallerException"></exception>
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;
Expand Down Expand Up @@ -243,7 +239,8 @@ public void Configure(bool remove = false)
}
}

if (markerFound) {
if (markerFound)
{
throw new SOSInstallerException(".lldbinit file end marker not found");
}
}
Expand All @@ -256,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);
Expand All @@ -266,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()));
Expand Down Expand Up @@ -303,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);
Expand All @@ -312,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);
Expand Down Expand Up @@ -378,4 +380,4 @@ public SOSInstallerException(string message, Exception inner)
{
}
}
}
}
46 changes: 23 additions & 23 deletions src/Tools/dotnet-dump/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public Analyzer()
_serviceProvider.AddService<IContextService>(_contextService);
_serviceProvider.AddServiceFactory<SOSLibrary>(() => SOSLibrary.Create(this));

_contextService.ServiceProvider.AddServiceFactory<ClrMDHelper>(() => {
_contextService.ServiceProvider.AddServiceFactory<ClrMDHelper>(() =>
{
ClrRuntime clrRuntime = _contextService.Services.GetService<ClrRuntime>();
return clrRuntime != null ? new ClrMDHelper(clrRuntime) : null;
});
Expand All @@ -64,23 +65,17 @@ public Task<int> 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
{
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)
{
}
Expand All @@ -89,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);
Expand All @@ -112,7 +108,8 @@ ex is NotSupportedException ||
foreach (string cmd in command)
{
_commandService.Execute(cmd, _contextService.Services);
if (_consoleProvider.Shutdown) {
if (_consoleProvider.Shutdown)
{
break;
}
}
Expand All @@ -123,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);
});
}
Expand Down Expand Up @@ -152,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)
{
}
Expand All @@ -177,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();
}
}
Expand Down Expand Up @@ -221,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}");
}
Expand Down

0 comments on commit eccaf50

Please sign in to comment.