Skip to content

Commit 2a9c80b

Browse files
authored
[RISC-V] Implement emulate single step feature. (#94711)
* [RISC-V] Implement emulate single step feature. * [RISC-V] Fix clang16 build error. Error message /home/clamp/runtime/src/coreclr/debug/inc/riscv64/primitives.h:52:5: error: integer value -1 is outside the valid range of values [0, 255] for this enumeration type [-Wenum-constexpr-conversion] (CorDebugRegister)(-1), // X0 is zero register that is not a real register. We need padding here for proper mapping with ICorDebugInfo::RegNum. ^ * [RISC-V] Fix sign bit.
1 parent 6f30b75 commit 2a9c80b

File tree

10 files changed

+610
-21
lines changed

10 files changed

+610
-21
lines changed

src/coreclr/clrdefinitions.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ elseif (CLR_CMAKE_TARGET_ARCH_ARM)
1414
add_definitions(-D_ARM_WORKAROUND_)
1515
endif (CLR_CMAKE_HOST_WIN32 AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
1616
add_definitions(-DFEATURE_EMULATE_SINGLESTEP)
17+
elseif (CLR_CMAKE_TARGET_ARCH_RISCV64)
18+
add_definitions(-DFEATURE_EMULATE_SINGLESTEP)
1719
endif (CLR_CMAKE_TARGET_ARCH_ARM64)
1820

1921
if (CLR_CMAKE_TARGET_UNIX)

src/coreclr/debug/ee/controller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,7 +3202,7 @@ void DebuggerController::ApplyTraceFlag(Thread *thread)
32023202
g_pEEInterface->MarkThreadForDebugStepping(thread, true);
32033203
LOG((LF_CORDB,LL_INFO1000, "DC::ApplyTraceFlag marked thread for debug stepping\n"));
32043204

3205-
SetSSFlag(reinterpret_cast<DT_CONTEXT *>(context) ARM_ARG(thread) ARM64_ARG(thread));
3205+
SetSSFlag(reinterpret_cast<DT_CONTEXT *>(context) ARM_ARG(thread) ARM64_ARG(thread) RISCV64_ARG(thread));
32063206
}
32073207

32083208
//
@@ -3239,7 +3239,7 @@ void DebuggerController::UnapplyTraceFlag(Thread *thread)
32393239

32403240
// Always need to unmark for stepping
32413241
g_pEEInterface->MarkThreadForDebugStepping(thread, false);
3242-
UnsetSSFlag(reinterpret_cast<DT_CONTEXT *>(context) ARM_ARG(thread) ARM64_ARG(thread));
3242+
UnsetSSFlag(reinterpret_cast<DT_CONTEXT *>(context) ARM_ARG(thread) ARM64_ARG(thread) RISCV64_ARG(thread));
32433243
}
32443244

32453245
void DebuggerController::EnableExceptionHook()

src/coreclr/debug/ee/debugger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15770,7 +15770,7 @@ BOOL Debugger::IsThreadContextInvalid(Thread *pThread, CONTEXT *pCtx)
1577015770
if (success)
1577115771
{
1577215772
// Check single-step flag
15773-
if (IsSSFlagEnabled(reinterpret_cast<DT_CONTEXT *>(pCtx) ARM_ARG(pThread) ARM64_ARG(pThread)))
15773+
if (IsSSFlagEnabled(reinterpret_cast<DT_CONTEXT *>(pCtx) ARM_ARG(pThread) ARM64_ARG(pThread) RISCV64_ARG(pThread)))
1577415774
{
1577515775
// Can't hijack a thread whose SS-flag is set. This could lead to races
1577615776
// with the thread taking the SS-exception.

src/coreclr/debug/ee/riscv64/primitives.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,25 @@ void CopyREGDISPLAY(REGDISPLAY* pDst, REGDISPLAY* pSrc)
1212
CONTEXT tmp;
1313
CopyRegDisplay(pSrc, pDst, &tmp);
1414
}
15+
16+
void SetSSFlag(DT_CONTEXT *, Thread *pThread)
17+
{
18+
_ASSERTE(pThread != NULL);
19+
20+
pThread->EnableSingleStep();
21+
}
22+
23+
void UnsetSSFlag(DT_CONTEXT *, Thread *pThread)
24+
{
25+
_ASSERTE(pThread != NULL);
26+
27+
pThread->DisableSingleStep();
28+
}
29+
30+
// Check if single stepping is enabled.
31+
bool IsSSFlagEnabled(DT_CONTEXT *, Thread *pThread)
32+
{
33+
_ASSERTE(pThread != NULL);
34+
35+
return pThread->IsSingleStepEnabled();
36+
}

src/coreclr/debug/inc/riscv64/primitives.h

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ inline CORDB_ADDRESS GetPatchEndAddr(CORDB_ADDRESS patchAddr)
4949

5050
constexpr CorDebugRegister g_JITToCorDbgReg[] =
5151
{
52-
(CorDebugRegister)(-1), // X0 is zero register that is not a real register. We need padding here for proper mapping with ICorDebugInfo::RegNum.
52+
(CorDebugRegister)(255), // X0 is zero register that is not a real register. We need padding here for proper mapping with ICorDebugInfo::RegNum.
5353
REGISTER_RISCV64_RA,
5454
REGISTER_RISCV64_SP,
5555
REGISTER_RISCV64_GP,
@@ -221,24 +221,15 @@ inline bool AddressIsBreakpoint(CORDB_ADDRESS_TYPE* address)
221221
return CORDbgGetInstruction(address) == CORDbg_BREAK_INSTRUCTION;
222222
}
223223

224-
inline void SetSSFlag(DT_CONTEXT *pContext)
225-
{
226-
// TODO-RISCV64: RISCV64 doesn't support cpsr.
227-
_ASSERTE(!"unimplemented on RISCV64 yet");
228-
}
224+
class Thread;
225+
// Enable single stepping.
226+
void SetSSFlag(DT_CONTEXT *pCtx, Thread *pThread);
229227

230-
inline void UnsetSSFlag(DT_CONTEXT *pContext)
231-
{
232-
// TODO-RISCV64: RISCV64 doesn't support cpsr.
233-
_ASSERTE(!"unimplemented on RISCV64 yet");
234-
}
228+
// Disable single stepping
229+
void UnsetSSFlag(DT_CONTEXT *pCtx, Thread *pThread);
235230

236-
inline bool IsSSFlagEnabled(DT_CONTEXT * pContext)
237-
{
238-
// TODO-RISCV64: RISCV64 doesn't support cpsr.
239-
_ASSERTE(!"unimplemented on RISCV64 yet");
240-
return false;
241-
}
231+
// Check if single stepping is enabled.
232+
bool IsSSFlagEnabled(DT_CONTEXT *pCtx, Thread *pThread);
242233

243234

244235
inline bool PRDIsEqual(PRD_TYPE p1, PRD_TYPE p2)

src/coreclr/inc/stdmacros.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@
125125
#define NOT_LOONGARCH64_ARG(x) , x
126126
#endif
127127

128+
#ifdef TARGET_RISCV64
129+
#define RISCV64_FIRST_ARG(x) x ,
130+
#define RISCV64_ARG(x) , x
131+
#define RISCV64_ONLY(x) x
132+
#define NOT_RISCV64(x)
133+
#define NOT_RISCV64_ARG(x)
134+
#else
135+
#define RISCV64_FIRST_ARG(x)
136+
#define RISCV64_ARG(x)
137+
#define RISCV64_ONLY(x)
138+
#define NOT_RISCV64(x) x
139+
#define NOT_RISCV64_ARG(x) , x
140+
#endif
141+
128142
#ifdef TARGET_64BIT
129143
#define LOG2_PTRSIZE 3
130144
#else

src/coreclr/vm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ elseif(CLR_CMAKE_TARGET_ARCH_RISCV64)
882882

883883
set(VM_SOURCES_WKS_ARCH
884884
${ARCH_SOURCES_DIR}/profiler.cpp
885+
${ARCH_SOURCES_DIR}/singlestepper.cpp
885886
gcinfodecoder.cpp
886887
)
887888
endif()

0 commit comments

Comments
 (0)