refactor: Move common debugger methods out of prelim directory

Related-To: NEO-9668
Signed-off-by: Jitendra Sharma <jitendra.sharma@intel.com>
This commit is contained in:
Jitendra Sharma
2024-02-06 10:47:36 +00:00
committed by Compute-Runtime-Automation
parent 82c5891a92
commit d69bb8ac15
7 changed files with 136 additions and 109 deletions

View File

@@ -7,14 +7,18 @@
#include "level_zero/tools/source/debug/debug_session.h" #include "level_zero/tools/source/debug/debug_session.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/sleep.h" #include "shared/source/helpers/sleep.h"
#include "shared/source/os_interface/linux/drm_allocation.h" #include "shared/source/os_interface/linux/drm_allocation.h"
#include "shared/source/os_interface/linux/drm_neo.h" #include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/sys_calls.h" #include "shared/source/os_interface/linux/sys_calls.h"
#include "level_zero/core/source/device/device.h" #include "level_zero/core/source/device/device.h"
#include "level_zero/core/source/gfx_core_helpers/l0_gfx_core_helper.h"
#include "level_zero/include/zet_intel_gpu_debug.h"
#include "level_zero/tools/source/debug/linux/debug_session.h" #include "level_zero/tools/source/debug/linux/debug_session.h"
#include "level_zero/tools/source/debug/linux/debug_session_factory.h" #include "level_zero/tools/source/debug/linux/debug_session_factory.h"
#include "level_zero/tools/source/debug/linux/drm_helper.h"
namespace L0 { namespace L0 {
DebugSessionLinuxAllocatorFn debugSessionLinuxFactory[DEBUG_SESSION_LINUX_TYPE_MAX] = {}; DebugSessionLinuxAllocatorFn debugSessionLinuxFactory[DEBUG_SESSION_LINUX_TYPE_MAX] = {};
@@ -121,4 +125,100 @@ void DebugSessionLinux::closeAsyncThread() {
internalEventThread.close(); internalEventThread.close();
} }
bool DebugSessionLinux::checkForceExceptionBit(uint64_t memoryHandle, EuThread::ThreadId threadId, uint32_t *cr0, const SIP::regset_desc *regDesc) {
auto gpuVa = getContextStateSaveAreaGpuVa(memoryHandle);
auto threadSlotOffset = calculateThreadSlotOffset(threadId);
auto startRegOffset = threadSlotOffset + calculateRegisterOffsetInThreadSlot(regDesc, 0);
[[maybe_unused]] int ret = readGpuMemory(memoryHandle, reinterpret_cast<char *>(cr0), 1 * regDesc->bytes, gpuVa + startRegOffset);
DEBUG_BREAK_IF(ret != ZE_RESULT_SUCCESS);
const uint32_t cr0ForcedExcpetionBitmask = 0x04000000;
if (cr0[1] & cr0ForcedExcpetionBitmask) {
return true;
}
return false;
}
void DebugSessionLinux::checkStoppedThreadsAndGenerateEvents(const std::vector<EuThread::ThreadId> &threads, uint64_t memoryHandle, uint32_t deviceIndex) {
std::vector<EuThread::ThreadId> threadsWithAttention;
std::vector<EuThread::ThreadId> stoppedThreadsToReport;
NEO::sleep(std::chrono::microseconds(1));
if (threads.size() > 1) {
auto hwInfo = connectedDevice->getHwInfo();
auto &l0GfxCoreHelper = connectedDevice->getL0GfxCoreHelper();
std::unique_ptr<uint8_t[]> bitmask;
size_t bitmaskSize;
[[maybe_unused]] auto attReadResult = threadControl(threads, deviceIndex, ThreadControlCmd::stopped, bitmask, bitmaskSize);
// error querying STOPPED threads - no threads available ( for example: threads have completed )
if (attReadResult != 0) {
PRINT_DEBUGGER_ERROR_LOG("checkStoppedThreadsAndGenerateEvents ATTENTION read failed: %d errno = %d \n", (int)attReadResult, DrmHelper::getErrno(connectedDevice));
return;
}
threadsWithAttention = l0GfxCoreHelper.getThreadsFromAttentionBitmask(hwInfo, deviceIndex, bitmask.get(), bitmaskSize);
if (threadsWithAttention.size() == 0) {
return;
}
}
const auto &threadsToCheck = threadsWithAttention.size() > 0 ? threadsWithAttention : threads;
stoppedThreadsToReport.reserve(threadsToCheck.size());
const auto regSize = std::max(getRegisterSize(ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU), 64u);
auto cr0 = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));
auto regDesc = typeToRegsetDesc(ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU);
for (auto &threadId : threadsToCheck) {
SIP::sr_ident srMagic = {{0}};
srMagic.count = 0;
if (readSystemRoutineIdent(allThreads[threadId].get(), memoryHandle, srMagic)) {
bool wasStopped = allThreads[threadId]->isStopped();
bool checkIfStopped = true;
if (srMagic.count % 2 == 1) {
memset(cr0.get(), 0, regSize);
checkIfStopped = !checkForceExceptionBit(memoryHandle, threadId, cr0.get(), regDesc);
}
if (checkIfStopped && allThreads[threadId]->verifyStopped(srMagic.count)) {
allThreads[threadId]->stopThread(memoryHandle);
if (!wasStopped) {
stoppedThreadsToReport.push_back(threadId);
}
}
} else {
break;
}
}
generateEventsForStoppedThreads(stoppedThreadsToReport);
}
ze_result_t DebugSessionLinux::resumeImp(const std::vector<EuThread::ThreadId> &threads, uint32_t deviceIndex) {
std::unique_ptr<uint8_t[]> bitmask;
size_t bitmaskSize;
auto result = threadControl(threads, deviceIndex, ThreadControlCmd::resume, bitmask, bitmaskSize);
return result == 0 ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_NOT_AVAILABLE;
}
ze_result_t DebugSessionLinux::interruptImp(uint32_t deviceIndex) {
std::unique_ptr<uint8_t[]> bitmask;
size_t bitmaskSize;
auto result = threadControl({}, deviceIndex, ThreadControlCmd::interruptAll, bitmask, bitmaskSize);
return result == 0 ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_NOT_AVAILABLE;
}
} // namespace L0 } // namespace L0

View File

@@ -74,5 +74,18 @@ struct DebugSessionLinux : DebugSessionImp {
return NEO::SysCalls::munmap(addr, size); return NEO::SysCalls::munmap(addr, size);
} }
}; };
enum class ThreadControlCmd {
interrupt,
resume,
stopped,
interruptAll
};
virtual int threadControl(const std::vector<EuThread::ThreadId> &threads, uint32_t tile, ThreadControlCmd threadCmd, std::unique_ptr<uint8_t[]> &bitmask, size_t &bitmaskSize) = 0;
void checkStoppedThreadsAndGenerateEvents(const std::vector<EuThread::ThreadId> &threads, uint64_t memoryHandle, uint32_t deviceIndex) override;
MOCKABLE_VIRTUAL bool checkForceExceptionBit(uint64_t memoryHandle, EuThread::ThreadId threadId, uint32_t *cr0, const SIP::regset_desc *regDesc);
ze_result_t resumeImp(const std::vector<EuThread::ThreadId> &threads, uint32_t deviceIndex) override;
ze_result_t interruptImp(uint32_t deviceIndex) override;
}; };
} // namespace L0 } // namespace L0

View File

@@ -1506,102 +1506,6 @@ int DebugSessionLinuxi915::threadControl(const std::vector<EuThread::ThreadId> &
return euControlRetVal; return euControlRetVal;
} }
bool DebugSessionLinuxi915::checkForceExceptionBit(uint64_t memoryHandle, EuThread::ThreadId threadId, uint32_t *cr0, const SIP::regset_desc *regDesc) {
auto gpuVa = getContextStateSaveAreaGpuVa(memoryHandle);
auto threadSlotOffset = calculateThreadSlotOffset(threadId);
auto startRegOffset = threadSlotOffset + calculateRegisterOffsetInThreadSlot(regDesc, 0);
[[maybe_unused]] int ret = readGpuMemory(memoryHandle, reinterpret_cast<char *>(cr0), 1 * regDesc->bytes, gpuVa + startRegOffset);
DEBUG_BREAK_IF(ret != ZE_RESULT_SUCCESS);
const uint32_t cr0ForcedExcpetionBitmask = 0x04000000;
if (cr0[1] & cr0ForcedExcpetionBitmask) {
return true;
}
return false;
}
void DebugSessionLinuxi915::checkStoppedThreadsAndGenerateEvents(const std::vector<EuThread::ThreadId> &threads, uint64_t memoryHandle, uint32_t deviceIndex) {
std::vector<EuThread::ThreadId> threadsWithAttention;
std::vector<EuThread::ThreadId> stoppedThreadsToReport;
NEO::sleep(std::chrono::microseconds(1));
if (threads.size() > 1) {
auto hwInfo = connectedDevice->getHwInfo();
auto &l0GfxCoreHelper = connectedDevice->getL0GfxCoreHelper();
std::unique_ptr<uint8_t[]> bitmask;
size_t bitmaskSize;
[[maybe_unused]] auto attReadResult = threadControl(threads, deviceIndex, ThreadControlCmd::stopped, bitmask, bitmaskSize);
// error querying STOPPED threads - no threads available ( for example: threads have completed )
if (attReadResult != 0) {
PRINT_DEBUGGER_ERROR_LOG("checkStoppedThreadsAndGenerateEvents ATTENTION read failed: %d errno = %d \n", (int)attReadResult, DrmHelper::getErrno(connectedDevice));
return;
}
threadsWithAttention = l0GfxCoreHelper.getThreadsFromAttentionBitmask(hwInfo, deviceIndex, bitmask.get(), bitmaskSize);
if (threadsWithAttention.size() == 0) {
return;
}
}
const auto &threadsToCheck = threadsWithAttention.size() > 0 ? threadsWithAttention : threads;
stoppedThreadsToReport.reserve(threadsToCheck.size());
const auto regSize = std::max(getRegisterSize(ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU), 64u);
auto cr0 = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));
auto regDesc = typeToRegsetDesc(ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU);
for (auto &threadId : threadsToCheck) {
SIP::sr_ident srMagic = {{0}};
srMagic.count = 0;
if (readSystemRoutineIdent(allThreads[threadId].get(), memoryHandle, srMagic)) {
bool wasStopped = allThreads[threadId]->isStopped();
bool checkIfStopped = true;
if (srMagic.count % 2 == 1) {
memset(cr0.get(), 0, regSize);
checkIfStopped = !checkForceExceptionBit(memoryHandle, threadId, cr0.get(), regDesc);
}
if (checkIfStopped && allThreads[threadId]->verifyStopped(srMagic.count)) {
allThreads[threadId]->stopThread(memoryHandle);
if (!wasStopped) {
stoppedThreadsToReport.push_back(threadId);
}
}
} else {
break;
}
}
generateEventsForStoppedThreads(stoppedThreadsToReport);
}
ze_result_t DebugSessionLinuxi915::resumeImp(const std::vector<EuThread::ThreadId> &threads, uint32_t deviceIndex) {
std::unique_ptr<uint8_t[]> bitmask;
size_t bitmaskSize;
auto result = threadControl(threads, deviceIndex, ThreadControlCmd::resume, bitmask, bitmaskSize);
return result == 0 ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_NOT_AVAILABLE;
}
ze_result_t DebugSessionLinuxi915::interruptImp(uint32_t deviceIndex) {
std::unique_ptr<uint8_t[]> bitmask;
size_t bitmaskSize;
auto result = threadControl({}, deviceIndex, ThreadControlCmd::interruptAll, bitmask, bitmaskSize);
return result == 0 ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_NOT_AVAILABLE;
}
ze_result_t DebugSessionLinuxi915::getISAVMHandle(uint32_t deviceIndex, const zet_debug_memory_space_desc_t *desc, size_t size, uint64_t &vmHandle) { ze_result_t DebugSessionLinuxi915::getISAVMHandle(uint32_t deviceIndex, const zet_debug_memory_space_desc_t *desc, size_t size, uint64_t &vmHandle) {
auto gmmHelper = connectedDevice->getNEODevice()->getGmmHelper(); auto gmmHelper = connectedDevice->getNEODevice()->getGmmHelper();
auto accessVA = gmmHelper->decanonize(desc->address); auto accessVA = gmmHelper->decanonize(desc->address);

View File

@@ -153,21 +153,10 @@ struct DebugSessionLinuxi915 : DebugSessionLinux {
}; };
protected: protected:
enum class ThreadControlCmd {
interrupt,
resume,
stopped,
interruptAll
};
MOCKABLE_VIRTUAL void handleEvent(prelim_drm_i915_debug_event *event); MOCKABLE_VIRTUAL void handleEvent(prelim_drm_i915_debug_event *event);
bool checkAllEventsCollected(); bool checkAllEventsCollected();
std::unordered_map<uint64_t, std::unique_ptr<ClientConnection>> clientHandleToConnection; std::unordered_map<uint64_t, std::unique_ptr<ClientConnection>> clientHandleToConnection;
ze_result_t readEventImp(prelim_drm_i915_debug_event *drmDebugEvent); ze_result_t readEventImp(prelim_drm_i915_debug_event *drmDebugEvent);
ze_result_t resumeImp(const std::vector<EuThread::ThreadId> &threads, uint32_t deviceIndex) override;
ze_result_t interruptImp(uint32_t deviceIndex) override;
void checkStoppedThreadsAndGenerateEvents(const std::vector<EuThread::ThreadId> &threads, uint64_t memoryHandle, uint32_t deviceIndex) override;
MOCKABLE_VIRTUAL bool checkForceExceptionBit(uint64_t memoryHandle, EuThread::ThreadId threadId, uint32_t *cr0, const SIP::regset_desc *regDesc);
void enqueueApiEvent(zet_debug_event_t &debugEvent) override { void enqueueApiEvent(zet_debug_event_t &debugEvent) override {
pushApiEvent(debugEvent); pushApiEvent(debugEvent);
@@ -253,8 +242,7 @@ struct DebugSessionLinuxi915 : DebugSessionLinux {
ze_result_t writeDefaultMemory(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, ze_result_t writeDefaultMemory(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc,
size_t size, const void *buffer); size_t size, const void *buffer);
MOCKABLE_VIRTUAL int threadControl(const std::vector<EuThread::ThreadId> &threads, uint32_t tile, ThreadControlCmd threadCmd, std::unique_ptr<uint8_t[]> &bitmask, size_t &bitmaskSize); int threadControl(const std::vector<EuThread::ThreadId> &threads, uint32_t tile, ThreadControlCmd threadCmd, std::unique_ptr<uint8_t[]> &bitmask, size_t &bitmaskSize) override;
uint64_t getContextStateSaveAreaGpuVa(uint64_t memoryHandle) override; uint64_t getContextStateSaveAreaGpuVa(uint64_t memoryHandle) override;
size_t getContextStateSaveAreaSize(uint64_t memoryHandle) override; size_t getContextStateSaveAreaSize(uint64_t memoryHandle) override;
virtual uint64_t getSbaBufferGpuVa(uint64_t memoryHandle); virtual uint64_t getSbaBufferGpuVa(uint64_t memoryHandle);

View File

@@ -87,6 +87,9 @@ struct DebugSessionLinuxXe : DebugSessionLinux {
uint32_t xeDebuggerVersion = 0; uint32_t xeDebuggerVersion = 0;
protected: protected:
int threadControl(const std::vector<EuThread::ThreadId> &threads, uint32_t tile, ThreadControlCmd threadCmd, std::unique_ptr<uint8_t[]> &bitmask, size_t &bitmaskSize) override {
return -1;
}
void startAsyncThread() override; void startAsyncThread() override;
static void *asyncThreadFunction(void *arg); static void *asyncThreadFunction(void *arg);
void handleEventsAsync(); void handleEventsAsync();

View File

@@ -126,6 +126,11 @@ struct MockDebugSessionLinuxXe : public L0::DebugSessionLinuxXe {
return DebugSessionLinuxXe::initialize(); return DebugSessionLinuxXe::initialize();
} }
int threadControl(const std::vector<EuThread::ThreadId> &threads, uint32_t tile, ThreadControlCmd threadCmd, std::unique_ptr<uint8_t[]> &bitmask, size_t &bitmaskSize) override {
numThreadsPassedToThreadControl = threads.size();
return L0::DebugSessionLinuxXe::threadControl(threads, tile, threadCmd, bitmask, bitmaskSize);
}
std::unique_ptr<uint64_t[]> getInternalEvent() override { std::unique_ptr<uint64_t[]> getInternalEvent() override {
getInternalEventCounter++; getInternalEventCounter++;
if (synchronousInternalEventRead) { if (synchronousInternalEventRead) {
@@ -134,6 +139,7 @@ struct MockDebugSessionLinuxXe : public L0::DebugSessionLinuxXe {
return DebugSessionLinuxXe::getInternalEvent(); return DebugSessionLinuxXe::getInternalEvent();
} }
size_t numThreadsPassedToThreadControl = 0;
bool synchronousInternalEventRead = false; bool synchronousInternalEventRead = false;
std::atomic<int> getInternalEventCounter = 0; std::atomic<int> getInternalEventCounter = 0;
ze_result_t initializeRetVal = ZE_RESULT_FORCE_UINT32; ze_result_t initializeRetVal = ZE_RESULT_FORCE_UINT32;

View File

@@ -137,6 +137,19 @@ TEST(IoctlHandler, GivenHandlerWhenIoctlFailsWithEBUSYThenIoctlIsAgain) {
using DebugApiLinuxTestXe = Test<DebugApiLinuxXeFixture>; using DebugApiLinuxTestXe = Test<DebugApiLinuxXeFixture>;
TEST_F(DebugApiLinuxTestXe, WhenCallingThreadControlForInterruptThenErrorIsReturned) {
zet_debug_config_t config = {};
config.pid = 0x1234;
auto session = std::make_unique<MockDebugSessionLinuxXe>(config, device, 10);
EXPECT_NE(nullptr, session);
std::vector<EuThread::ThreadId> threads({});
std::unique_ptr<uint8_t[]> bitmaskOut;
size_t bitmaskSizeOut = 0;
EXPECT_EQ(-1, session->threadControl(threads, 0, MockDebugSessionLinuxXe::ThreadControlCmd::interrupt, bitmaskOut, bitmaskSizeOut));
}
TEST_F(DebugApiLinuxTestXe, GivenDebugSessionWhenCallingPollThenDefaultHandlerRedirectsToSysCall) { TEST_F(DebugApiLinuxTestXe, GivenDebugSessionWhenCallingPollThenDefaultHandlerRedirectsToSysCall) {
zet_debug_config_t config = {}; zet_debug_config_t config = {};
config.pid = 0x1234; config.pid = 0x1234;