Skip to content

Commit

Permalink
[RISC-V] Implement SOS related code. (#4385)
Browse files Browse the repository at this point in the history
Implement ICorDebug related `clrstack -i`.
Implement `clrstack -r` output.

Related PR in runtime: dotnet/runtime#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
  • Loading branch information
viewizard authored Nov 8, 2023
1 parent d76b189 commit 0897f9a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/SOS/SOS.Hosting/CorDebugDataTargetWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/SOS/SOS.Hosting/CorDebugPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 2 additions & 0 deletions src/SOS/Strike/platform/cordebugdatatarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 14 additions & 2 deletions src/SOS/Strike/strike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/SOS/Strike/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
10 changes: 5 additions & 5 deletions src/shared/pal/prebuilt/inc/cordebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down

0 comments on commit 0897f9a

Please sign in to comment.