From 0897f9aefdd706004819d9c39b5995093e34432f Mon Sep 17 00:00:00 2001 From: Mikhail Kurinnoi Date: Wed, 8 Nov 2023 04:17:14 +0300 Subject: [PATCH] [RISC-V] Implement SOS related code. (#4385) Implement ICorDebug related `clrstack -i`. Implement `clrstack -r` output. Related PR in runtime: https://github.com/dotnet/runtime/pull/94454 ``` > clrstack OS Thread Id: 0x40614 (0) Child SP IP Call Site 0000003FCBDD6F70 0000000000000000 [InlinedCallFrame: 0000003fcbdd6f70] Interop+Sys.g____PInvoke|44_0(Byte*, Int32) 0000003FCBDD6F70 0000003f32be5758 [InlinedCallFrame: 0000003fcbdd6f70] Interop+Sys.g____PInvoke|44_0(Byte*, Int32) 0000003FCBDD6F50 0000003F32BE5758 ILStubClass.IL_STUB_PInvoke(Byte*, Int32) 0000003FCBDD7050 0000003F32BE55BC Interop+Sys.ReadStdin(Byte*, Int32) [/home/runtime/src/libraries/System.Console/src/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @ 800] 0000003FCBDD7080 0000003F32BE5464 System.IO.StdInReader.ReadStdin(Byte*, Int32) [/home/runtime/src/libraries/System.Console/src/System/IO/StdInReader.cs @ 83] 0000003FCBDD70B0 0000003F32BE4FBC System.IO.StdInReader.ReadKey() [/home/runtime/src/libraries/System.Console/src/System/IO/StdInReader.cs @ 337] 0000003FCBDD7560 0000003F32BE3A24 System.IO.StdInReader.ReadLineCore(Boolean) [/home/runtime/src/libraries/System.Console/src/System/IO/StdInReader.cs @ 160] 0000003FCBDD7740 0000003F32BE3694 System.IO.StdInReader.ReadLine() [/home/runtime/src/libraries/System.Console/src/System/IO/StdInReader.cs @ 90] 0000003FCBDD77A0 0000003F32BE353C System.IO.SyncTextReader.ReadLine() [/home/runtime/src/libraries/System.Console/src/System/IO/SyncTextReader.cs @ 77] 0000003FCBDD77F0 0000003F32BE144C System.Console.ReadLine() [/home/runtime/src/libraries/System.Console/src/System/Console.cs @ 752] 0000003FCBDD7820 0000003F32B9DFD0 TestApp.Program.Main(System.String[]) [/home/viewizard/Desktop/projects_test/test_hr/Program.cs @ 11] ``` ``` > clrstack -i Dumping managed stack and managed variables using ICorDebug. ============================================================================= Child SP IP Call Site 0000003FCBDD6EF0 0000000000000000 [NativeStackFrame] 0000003FCBDD6F50 0000003f32be5758 0000003FCBDD6F70 (null) [Managed to Unmanaged transition: 0000003FCBDD6F70] 0000003FCBDD7050 0000003f32be55bc [DEFAULT] I4 Interop+Sys.ReadStdin(Ptr UI1,I4) (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD7080 0000003f32be5464 [DEFAULT] I4 System.IO.StdInReader.ReadStdin(Ptr UI1,I4) (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD70B0 0000003f32be4fbc [DEFAULT] [hasThis] ValueClass System.ConsoleKeyInfo System.IO.StdInReader.ReadKey() (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD7560 0000003f32be3a24 [DEFAULT] [hasThis] Boolean System.IO.StdInReader.ReadLineCore(Boolean) (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD7740 0000003f32be3694 [DEFAULT] [hasThis] String System.IO.StdInReader.ReadLine() (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD77A0 0000003f32be353c [DEFAULT] [hasThis] String System.IO.SyncTextReader.ReadLine() (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD77F0 0000003f32be144c [DEFAULT] String System.Console.ReadLine() (/home/mkurinnoi/dotnet/System.Console.dll) 0000003FCBDD7820 0000003f32b9dfd0 [DEFAULT] Void TestApp.Program.Main(SZArray String) (/home/mkurinnoi/test_hr.dll) 0000003FCBDD7850 0000003fb1e2307e [NativeStackFrame] Stack walk complete. ============================================================================= ``` CC @clamp03 @wscho77 @HJLeee @JongHeonChoi @t-mustafin @gbalykov --- src/SOS/SOS.Hosting/CorDebugDataTargetWrapper.cs | 3 +++ src/SOS/SOS.Hosting/CorDebugPlatform.cs | 4 +++- src/SOS/Strike/platform/cordebugdatatarget.h | 2 ++ src/SOS/Strike/strike.cpp | 16 ++++++++++++++-- src/SOS/Strike/util.h | 2 +- src/shared/pal/prebuilt/inc/cordebug.h | 10 +++++----- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/SOS/SOS.Hosting/CorDebugDataTargetWrapper.cs b/src/SOS/SOS.Hosting/CorDebugDataTargetWrapper.cs index 5e69c47532..c46e8f1591 100644 --- a/src/SOS/SOS.Hosting/CorDebugDataTargetWrapper.cs +++ b/src/SOS/SOS.Hosting/CorDebugDataTargetWrapper.cs @@ -107,6 +107,9 @@ private int GetPlatform( case Architecture.Arm64: platform = CorDebugPlatform.CORDB_PLATFORM_POSIX_ARM64; break; + case (Architecture)9 /* Architecture.RiscV64 */: + platform = CorDebugPlatform.CORDB_PLATFORM_POSIX_RISCV64; + break; default: return HResult.E_FAIL; } diff --git a/src/SOS/SOS.Hosting/CorDebugPlatform.cs b/src/SOS/SOS.Hosting/CorDebugPlatform.cs index 61111bb3f4..9609ea8b23 100644 --- a/src/SOS/SOS.Hosting/CorDebugPlatform.cs +++ b/src/SOS/SOS.Hosting/CorDebugPlatform.cs @@ -19,6 +19,8 @@ public enum CorDebugPlatform CORDB_PLATFORM_POSIX_AMD64 = 8, CORDB_PLATFORM_POSIX_X86 = 9, CORDB_PLATFORM_POSIX_ARM = 10, - CORDB_PLATFORM_POSIX_ARM64 = 11 + CORDB_PLATFORM_POSIX_ARM64 = 11, + CORDB_PLATFORM_POSIX_LOONGARCH64 = 12, + CORDB_PLATFORM_POSIX_RISCV64 = 13 } } diff --git a/src/SOS/Strike/platform/cordebugdatatarget.h b/src/SOS/Strike/platform/cordebugdatatarget.h index 25e75f2c75..a911769191 100644 --- a/src/SOS/Strike/platform/cordebugdatatarget.h +++ b/src/SOS/Strike/platform/cordebugdatatarget.h @@ -96,6 +96,8 @@ class CorDebugDataTarget : public ICorDebugMutableDataTarget, public ICorDebugMe *pPlatform = CORDB_PLATFORM_POSIX_ARM; else if (platformKind == IMAGE_FILE_MACHINE_ARM64) *pPlatform = CORDB_PLATFORM_POSIX_ARM64; + else if (platformKind == IMAGE_FILE_MACHINE_RISCV64) + *pPlatform = CORDB_PLATFORM_POSIX_RISCV64; else return E_FAIL; } diff --git a/src/SOS/Strike/strike.cpp b/src/SOS/Strike/strike.cpp index 6b693259e9..4ef4bfd0d5 100644 --- a/src/SOS/Strike/strike.cpp +++ b/src/SOS/Strike/strike.cpp @@ -10492,7 +10492,7 @@ class ClrStackImplWithICorDebug InternalFrameManager internalFrameManager; IfFailRet(internalFrameManager.Init(pThread3)); - #if defined(_AMD64_) || defined(_ARM64_) + #if defined(_AMD64_) || defined(_ARM64_) || defined(_RISCV64_) ExtOut("%-16s %-16s %s\n", "Child SP", "IP", "Call Site"); #elif defined(_X86_) || defined(_ARM_) ExtOut("%-8s %-8s %s\n", "Child SP", "IP", "Call Site"); @@ -10965,7 +10965,19 @@ class ClrStackImpl #if defined(SOS_TARGET_RISCV64) if (IsDbgTargetRiscV64()) { - ExtOut("RISCV64:NYI\n"); + foundPlatform = true; + String outputFormat3 = " %3s=%016llx %3s=%016llx %3s=%016llx\n"; + ExtOut(outputFormat3, "r0", context.RiscV64Context.R0, "ra", context.RiscV64Context.Ra, "sp", context.RiscV64Context.Sp); + ExtOut(outputFormat3, "gp", context.RiscV64Context.Gp, "tp", context.RiscV64Context.Tp, "t0", context.RiscV64Context.T0); + ExtOut(outputFormat3, "t1", context.RiscV64Context.T1, "t2", context.RiscV64Context.T2, "fp", context.RiscV64Context.Fp); + ExtOut(outputFormat3, "s1", context.RiscV64Context.S1, "a0", context.RiscV64Context.A0, "a1", context.RiscV64Context.A1); + ExtOut(outputFormat3, "a2", context.RiscV64Context.A2, "a3", context.RiscV64Context.A3, "a4", context.RiscV64Context.A4); + ExtOut(outputFormat3, "a5", context.RiscV64Context.A5, "a6", context.RiscV64Context.A6, "a7", context.RiscV64Context.A7); + ExtOut(outputFormat3, "s2", context.RiscV64Context.S2, "s3", context.RiscV64Context.S3, "s4", context.RiscV64Context.S4); + ExtOut(outputFormat3, "s5", context.RiscV64Context.S5, "s6", context.RiscV64Context.S6, "s7", context.RiscV64Context.S7); + ExtOut(outputFormat3, "s8", context.RiscV64Context.S8, "s9", context.RiscV64Context.S9, "s10", context.RiscV64Context.S10); + ExtOut(outputFormat3, "s11", context.RiscV64Context.S11, "t3", context.RiscV64Context.T3, "t4", context.RiscV64Context.T4); + ExtOut(outputFormat3, "t5", context.RiscV64Context.T5, "t6", context.RiscV64Context.T6, "pc", context.RiscV64Context.Pc); } #endif diff --git a/src/SOS/Strike/util.h b/src/SOS/Strike/util.h index 88a27f97c2..a0b039b888 100644 --- a/src/SOS/Strike/util.h +++ b/src/SOS/Strike/util.h @@ -1545,7 +1545,7 @@ BOOL IsMiniDumpFile(); void ReportOOM(); BOOL SafeReadMemory (TADDR offset, PVOID lpBuffer, ULONG cb, PULONG lpcbBytesRead); -#if !defined(_TARGET_WIN64_) && !defined(_ARM64_) && !defined(_MIPS64_) +#if !defined(_TARGET_WIN64_) && !defined(_ARM64_) && !defined(_MIPS64_) && !defined(_RISCV64_) // on 64-bit platforms TADDR and CLRDATA_ADDRESS are identical inline BOOL SafeReadMemory (CLRDATA_ADDRESS offset, PVOID lpBuffer, ULONG cb, PULONG lpcbBytesRead) { return SafeReadMemory(TO_TADDR(offset), lpBuffer, cb, lpcbBytesRead); } diff --git a/src/shared/pal/prebuilt/inc/cordebug.h b/src/shared/pal/prebuilt/inc/cordebug.h index 5feeaff766..0d4d04e81b 100644 --- a/src/shared/pal/prebuilt/inc/cordebug.h +++ b/src/shared/pal/prebuilt/inc/cordebug.h @@ -9195,15 +9195,15 @@ enum CorDebugRegister REGISTER_ARM64_V30 = ( REGISTER_ARM64_V29 + 1 ) , REGISTER_ARM64_V31 = ( REGISTER_ARM64_V30 + 1 ) , REGISTER_RISCV64_PC = 0, - REGISTER_RISCV64_RA = ( REGISTER_RISCV64_PC + 1), - REGISTER_RISCV64_SP = ( REGISTER_RISCV64_RA + 1), - REGISTER_RISCV64_GP = ( REGISTER_RISCV64_SP + 1), + REGISTER_RISCV64_SP = ( REGISTER_RISCV64_PC + 1 ), + REGISTER_RISCV64_FP = ( REGISTER_RISCV64_SP + 1 ), + REGISTER_RISCV64_RA = ( REGISTER_RISCV64_FP + 1 ), + REGISTER_RISCV64_GP = ( REGISTER_RISCV64_RA + 1 ), REGISTER_RISCV64_TP = ( REGISTER_RISCV64_GP + 1 ), REGISTER_RISCV64_T0 = ( REGISTER_RISCV64_TP + 1 ), REGISTER_RISCV64_T1 = ( REGISTER_RISCV64_T0 + 1 ), REGISTER_RISCV64_T2 = ( REGISTER_RISCV64_T1 + 1 ), - REGISTER_RISCV64_FP = ( REGISTER_RISCV64_T2 + 1 ), - REGISTER_RISCV64_S1 = ( REGISTER_RISCV64_FP + 1 ), + REGISTER_RISCV64_S1 = ( REGISTER_RISCV64_T2 + 1 ), REGISTER_RISCV64_A0 = ( REGISTER_RISCV64_S1 + 1 ), REGISTER_RISCV64_A1 = ( REGISTER_RISCV64_A0 + 1 ), REGISTER_RISCV64_A2 = ( REGISTER_RISCV64_A1 + 1 ),