Skip to content

VS4Mac needs a runtime API to symbolize managed IPs #61186

Closed
@mikem8361

Description

@mikem8361

Background and motivation

VS4Mac needs a way to symbolize managed IPs when rethrowing a native NSException from Objective C++. They do the symbolizing in the telemetry upload on the unhandled NSException crash. They don't currently build a StackTrace/StackFrames when the managed NSException wrapper is thrown so all they need currently is a way to convert a IP address of a managed method into a full method name string.

This is a short term API needed for the next 6.0 service release discoverable only through reflection.

API Proposal

namespace  System.Diagnostics
{
    public class StackFrame
    {
         /// <summary>
        /// Returns the method info instance for the managed code IP address.
        /// </summary>
        /// <param name="ip">code address</param>
        /// <returns>MethodBase instance or null if IP not found</returns>
        internal static MethodBase? GetMethodFromNativeIP(IntPtr ip);
    }
}

API Usage

Type stackFrameType = typeof(System.Diagnostics.StackFrame);
MethodInfo? getMethodFromNativeIP = stackFrameType.GetMethod("GetMethodFromNativeIP", BindingFlags.NonPublic | BindingFlags.Static);
if (getMethodFromNativeIP is null)
{
    Console.WriteLine("GetMethodFromNativeIP not found");
    return;
}

long ip = 0;
MethodBase? testFunction = getMethodFromNativeIP.Invoke(null, new object[] { new IntPtr(ip) }) as MethodBase;
if (testFunction is null)
{
    Console.WriteLine("Test function not found");
    return;
}

Alternative Designs

For .NET 7.0, we could replace this temporary API with this public one:

public class StackFrame
{
    public static StackFrame? CreateFromNativeIP(IntPtr nativeIP, bool needFileInfo = false); 
}

Risks

The .NET 6.0 API has the following limitations that will need to be addressed:

  1. There is a race condition for dynamic and collectable methods between when the MethodDesc is found and where it is wrapped in the MethodBase.
  2. MethodBase isn't a good design choice for working with NativeAOT.

Metadata

Metadata

Assignees

Labels

area-Diagnostics-coreclrenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions