Skip to content

Commit 985cb83

Browse files
author
Mike McLaughlin
authored
Add System.Diagnostics.StackFrame.GetMethodFromNativeIP API for VS4Mac (#61289)
* Add System.Diagnostics.StackFrame.GetMethodInfoFromNativeIP API for VS4Mac Issue: #61186
1 parent 61d603f commit 985cb83

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrame.CoreCLR.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Text;
5+
using System.Reflection;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
58

69
namespace System.Diagnostics
710
{
@@ -50,5 +53,25 @@ private void BuildStackFrame(int skipFrames, bool needFileInfo)
5053
}
5154

5255
private static bool AppendStackFrameWithoutMethodBase(StringBuilder sb) => false;
56+
57+
[DllImport(RuntimeHelpers.QCall, EntryPoint = "StackFrame_GetMethodDescFromNativeIP")]
58+
private static extern RuntimeMethodHandleInternal GetMethodDescFromNativeIP(IntPtr ip);
59+
60+
/// <summary>
61+
/// Returns the MethodBase instance for the managed code IP address.
62+
///
63+
/// Warning: The implementation of this method has race for dynamic and collectible methods.
64+
/// </summary>
65+
/// <param name="ip">code address</param>
66+
/// <returns>MethodBase instance for the method or null if IP not found</returns>
67+
internal static MethodBase? GetMethodFromNativeIP(IntPtr ip)
68+
{
69+
RuntimeMethodHandleInternal method = GetMethodDescFromNativeIP(ip);
70+
71+
if (method.Value == IntPtr.Zero)
72+
return null;
73+
74+
return RuntimeType.GetMethodBase(null, method);
75+
}
5376
}
5477
}

src/coreclr/vm/debugdebugger.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ FCIMPL0(FC_BOOL_RET, DebugDebugger::IsLogging)
297297
}
298298
FCIMPLEND
299299

300-
301300
FCIMPL4(void, DebugStackTrace::GetStackFramesInternal,
302301
StackFrameHelper* pStackFrameHelperUNSAFE,
303302
INT32 iSkip,
@@ -778,6 +777,28 @@ FCIMPL4(void, DebugStackTrace::GetStackFramesInternal,
778777
}
779778
FCIMPLEND
780779

780+
extern MethodDesc* QCALLTYPE StackFrame_GetMethodDescFromNativeIP(LPVOID ip)
781+
{
782+
QCALL_CONTRACT;
783+
784+
MethodDesc* pResult = nullptr;
785+
786+
BEGIN_QCALL;
787+
788+
// TODO: There is a race for dynamic and collectible methods here between getting
789+
// the MethodDesc here and when the managed wrapper converts it into a MethodBase
790+
// where the method could be collected.
791+
EECodeInfo codeInfo((PCODE)ip);
792+
if (codeInfo.IsValid())
793+
{
794+
pResult = codeInfo.GetMethodDesc();
795+
}
796+
797+
END_QCALL;
798+
799+
return pResult;
800+
}
801+
781802
FORCEINLINE void HolderDestroyStrongHandle(OBJECTHANDLE h) { if (h != NULL) DestroyStrongHandle(h); }
782803
typedef Wrapper<OBJECTHANDLE, DoNothing<OBJECTHANDLE>, HolderDestroyStrongHandle, NULL> StrongHandleHolder;
783804

src/coreclr/vm/debugdebugger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,6 @@ class DebugStackTrace
171171

172172
};
173173

174+
extern "C" MethodDesc* QCALLTYPE StackFrame_GetMethodDescFromNativeIP(LPVOID ip);
175+
174176
#endif // __DEBUG_DEBUGGER_h__

src/coreclr/vm/qcallentrypoints.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static const Entry s_QCall[] =
115115
DllImportEntry(RuntimeModule_GetType)
116116
DllImportEntry(RuntimeModule_GetScopeName)
117117
DllImportEntry(RuntimeModule_GetFullyQualifiedName)
118+
DllImportEntry(StackFrame_GetMethodDescFromNativeIP)
118119
DllImportEntry(ModuleBuilder_GetStringConstant)
119120
DllImportEntry(ModuleBuilder_GetTypeRef)
120121
DllImportEntry(ModuleBuilder_GetTokenFromTypeSpec)

src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.LibraryBuild.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
<!-- Internal API used by tests only. -->
55
<method name="GetICUVersion" />
66
</type>
7+
<type fullname="System.Diagnostics.StackFrame">
8+
<!-- Used by VS4Mac via reflection to symbolize stack traces -->
9+
<method name="GetMethodFromNativeIP" />
10+
</type>
711
</assembly>
812
</linker>

0 commit comments

Comments
 (0)