[RISC-V] Implement SOS related code. (#4385)
authorMikhail Kurinnoi <m.kurinnoi@samsung.com>
Wed, 8 Nov 2023 01:17:14 +0000 (04:17 +0300)
committerGitHub <noreply@github.com>
Wed, 8 Nov 2023 01:17:14 +0000 (17:17 -0800)
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.<ReadStdin>g____PInvoke|44_0(Byte*, Int32)
0000003FCBDD6F70 0000003f32be5758 [InlinedCallFrame: 0000003fcbdd6f70] Interop+Sys.<ReadStdin>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
src/SOS/SOS.Hosting/CorDebugPlatform.cs
src/SOS/Strike/platform/cordebugdatatarget.h
src/SOS/Strike/strike.cpp
src/SOS/Strike/util.h
src/shared/pal/prebuilt/inc/cordebug.h

index 5e69c4753205a786d133800da513b17000e1c05b..c46e8f1591884eb80d18b897f47ae7585ec459e4 100644 (file)
@@ -107,6 +107,9 @@ namespace SOS.Hosting
                     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;
                 }
index 61111bb3f4aa5a62612841d51f3d13dbb833e470..9609ea8b23764b056506d72e18dc6cb5c731b43a 100644 (file)
@@ -19,6 +19,8 @@ namespace SOS.Hosting
         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
     }
 }
index 25e75f2c75e8647d0ecf8fec5e992a461dba6f0d..a911769191a79912e6f2c20de006cf4eb2d6696c 100644 (file)
@@ -96,6 +96,8 @@ public:
                 *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;
         }
index 6b693259e97f7986fe0515e669ee86e5eef43cc5..4ef4bfd0d58528f5b3021897d76c6bbc9a2f7b34 100644 (file)
@@ -10492,7 +10492,7 @@ public:
         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 @@ public:
 #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
 
index 88a27f97c24a2f875c08622f311223fb8ab4f866..a0b039b88895fa050c881b4c4fc5bf3e637e1afb 100644 (file)
@@ -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); }
index 5feeaff766dc2752c0bb97f65606385f9c297b91..0d4d04e81bdd5cd6b84aee92ce2c328996ba9adf 100644 (file)
@@ -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 ),