Skip to content

Commit

Permalink
Make it possible to construct StackFrame without MethodBase
Browse files Browse the repository at this point in the history
Contributes to dotnet#80165.

Resolving a `MethodBase` is a pretty expensive operation and currently even a hello world needs it because of `Exception.ToString()`. This pull request is trying to get `MethodBase` out of the picture when constructing the exception string.

We currently only need `_method` for two things - the public `GetMethod` API, and `StackFrame.ToString`. The `StackFrame.ToString` can already deal with `_method` being null (and that codepath exists solely for NativeAOT). This is the minimal change. We could potentially explore other approaches - leave `MethodBase` always at null, or unshare the `StackFrame` class.
  • Loading branch information
MichalStrehovsky committed Jan 16, 2023
1 parent 51a8dd5 commit e66d92b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -28,6 +30,36 @@ public partial class StackFrame
/// </summary>
private bool _needFileInfo;

/// <summary>
/// Will be true if we attempted to retrieve the associated MethodBase but couldn't.
/// </summary>
private bool _noMethodBaseAvailable;

/// <summary>
/// Returns the method the frame is executing
/// </summary>
[RequiresUnreferencedCode("Metadata for the method might be incomplete or removed")]
public virtual MethodBase? GetMethod()
{
TryInitializeMethodBase();
return _method;
}

private bool TryInitializeMethodBase()
{
if (_noMethodBaseAvailable || _ipAddress == IntPtr.Zero)
return false;

IntPtr methodStartAddress = RuntimeImports.RhFindMethodStartAddress(_ipAddress);
DeveloperExperience.Default.TryGetMethodBase(methodStartAddress, out MethodBase _method);
if (_method == null)
{
_noMethodBaseAvailable = true;
return false;
}
return true;
}

/// <summary>
/// Constructs a StackFrame corresponding to a given IP address.
/// </summary>
Expand Down Expand Up @@ -55,7 +87,6 @@ private void InitializeForIpAddress(IntPtr ipAddress, bool needFileInfo)
_nativeOffset = (int)((nint)_ipAddress - (nint)methodStartAddress);

DeveloperExperience.Default.TryGetILOffsetWithinMethod(_ipAddress, out _ilOffset);
DeveloperExperience.Default.TryGetMethodBase(methodStartAddress, out _method);

if (needFileInfo)
{
Expand Down Expand Up @@ -98,7 +129,7 @@ internal IntPtr GetNativeIPAddress()
/// </summary>
internal bool HasMethod()
{
return _method != null;
return TryInitializeMethodBase();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public StackFrame(string? fileName, int lineNumber, int colNumber)

internal bool IsLastFrameFromForeignExceptionStackTrace => _isLastFrameFromForeignExceptionStackTrace;

#if !NATIVEAOT
/// <summary>
/// Returns the method the frame is executing
/// </summary>
Expand All @@ -142,6 +143,7 @@ public StackFrame(string? fileName, int lineNumber, int colNumber)
{
return _method;
}
#endif

/// <summary>
/// Returns the offset from the start of the native (jitted) code for the
Expand Down

0 comments on commit e66d92b

Please sign in to comment.