Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add symbol tracing environment variable #1269

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class FileLocatorTests

internal static IFileLocator GetLocator()
{
return SymbolGroup.CreateFromSymbolPath($"srv*{Helpers.TestWorkingDirectory}*http://msdl.microsoft.com/download/symbols", null);
return SymbolGroup.CreateFromSymbolPath($"srv*{Helpers.TestWorkingDirectory}*http://msdl.microsoft.com/download/symbols", true, null);
}

[Fact(Skip = "Touches network, don't run regularly.")]
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Diagnostics.Runtime.Tests/src/TestTargets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private static DataTarget LoadDump(string path)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
DataTarget dataTarget = DataTarget.LoadDump(path);
dataTarget.FileLocator = SymbolGroup.CreateFromSymbolPath(string.Empty, null);
bool symTrace = CustomDataTarget.GetTraceEnvironmentVariable();
dataTarget.FileLocator = SymbolGroup.CreateFromSymbolPath(string.Empty, trace: symTrace, null);
return dataTarget;
}
else
Expand Down
17 changes: 17 additions & 0 deletions src/Microsoft.Diagnostics.Runtime/CustomDataTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace Microsoft.Diagnostics.Runtime
/// </summary>
public class CustomDataTarget : IDisposable
{
/// <summary>
/// The environment variable to set to true if.
/// </summary>
public const string TraceSymbolsEnvVariable = "ClrMD_TraceSymbolRequests";

/// <summary>
/// The data reader that ClrMD will use to read data from the target.
/// </summary>
Expand Down Expand Up @@ -80,6 +85,18 @@ protected virtual void Dispose(bool disposing)
}
}

internal static bool GetTraceEnvironmentVariable()
{
string? value = Environment.GetEnvironmentVariable(TraceSymbolsEnvVariable);
if (value is null)
return false;

if (bool.TryParse(value, out bool result))
return result;

return value.Equals("1", StringComparison.OrdinalIgnoreCase);
}

public override string ToString() => DataReader?.DisplayName ?? GetType().Name;
}
}
6 changes: 4 additions & 2 deletions src/Microsoft.Diagnostics.Runtime/DataTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public DataTarget(CustomDataTarget customTarget)
if (locator == null)
{
string sympath = Environment.GetEnvironmentVariable("_NT_SYMBOL_PATH") ?? "";
locator = SymbolGroup.CreateFromSymbolPath(sympath, _target.SymbolTokenCredential);
bool symTrace = CustomDataTarget.GetTraceEnvironmentVariable();
locator = SymbolGroup.CreateFromSymbolPath(sympath, trace:symTrace, _target.SymbolTokenCredential);
}

FileLocator = locator;
Expand All @@ -80,7 +81,8 @@ public void SetSymbolPath(string symbolPath)
if (symbolPath is null)
throw new ArgumentNullException(nameof(symbolPath));

FileLocator = SymbolGroup.CreateFromSymbolPath(symbolPath, _target.SymbolTokenCredential);
bool symTrace = CustomDataTarget.GetTraceEnvironmentVariable();
FileLocator = SymbolGroup.CreateFromSymbolPath(symbolPath, trace:symTrace, _target.SymbolTokenCredential);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static FileSymbolCache GetDefaultCache()
return s_cache!;
}

public static IFileLocator CreateFromSymbolPath(string symbolPath, TokenCredential? credential)
public static IFileLocator CreateFromSymbolPath(string symbolPath, bool trace, TokenCredential? credential)
{
FileSymbolCache defaultCache = GetDefaultCache();
List<IFileLocator> locators = new();
Expand All @@ -114,7 +114,7 @@ public static IFileLocator CreateFromSymbolPath(string symbolPath, TokenCredenti
{
if (IsUrl(server))
{
SymbolServer symSvr = new(cache, server, credential);
SymbolServer symSvr = new(cache, server, trace, credential);
locators.Add(symSvr);

if (first)
Expand Down Expand Up @@ -142,7 +142,7 @@ public static IFileLocator CreateFromSymbolPath(string symbolPath, TokenCredenti
return single;

if (locators.Count == 0)
return new SymbolServer(defaultCache, SymbolServer.Msdl, null);
return new SymbolServer(defaultCache, SymbolServer.Msdl, trace, null);

return new SymbolGroup(locators);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ internal sealed class SymbolServer : FileLocatorBase
private readonly TokenCredential? _tokenCredential;
private AccessToken _accessToken;
private readonly FileSymbolCache _cache;
private readonly bool _trace;
private readonly HttpClient _http = new();

public string Server { get; private set; }

internal SymbolServer(FileSymbolCache cache, string server, TokenCredential? credential)
internal SymbolServer(FileSymbolCache cache, string server, bool trace, TokenCredential? credential)
{
if (cache is null)
throw new ArgumentNullException(nameof(cache));

_cache = cache;
_trace = trace;
Server = server;

if (IsSymweb(server))
Expand Down Expand Up @@ -137,6 +139,10 @@ private static bool IsSymweb(string server)
_http.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

HttpResponseMessage response = await _http.GetAsync(fullPath).ConfigureAwait(false);

if (_trace)
Trace.WriteLine($"ClrMD symbol request: {fullPath} returned {response.StatusCode}");

if (response.IsSuccessStatusCode)
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

Expand Down