From 6356de6511877dd20daafde6678b485d2ff934ac Mon Sep 17 00:00:00 2001 From: Mateusz Hoppe Date: Wed, 3 Mar 2021 12:09:39 +0000 Subject: [PATCH] Add debug api handlers 2 Related-To: NEO-4554 Signed-off-by: Mateusz Hoppe --- level_zero/api/tools/zet_debug.cpp | 22 +++++------ level_zero/core/source/device/device.h | 1 + level_zero/core/source/device/device_imp.h | 1 + .../core/test/unit_tests/mocks/mock_device.h | 2 + .../tools/source/debug/debug_handlers.cpp | 33 ++++++++++++++++ .../tools/source/debug/debug_handlers.h | 14 ++++++- level_zero/tools/source/debug/debug_session.h | 7 +++- .../sources/debug/mock_debug_session.h | 1 + .../sources/debug/test_debug_api.cpp | 39 +++++++++++++++++++ .../sources/debug/test_debug_api.inl | 28 +++++++++++++ 10 files changed, 135 insertions(+), 13 deletions(-) diff --git a/level_zero/api/tools/zet_debug.cpp b/level_zero/api/tools/zet_debug.cpp index 1dab89d575..296fd7fa3d 100644 --- a/level_zero/api/tools/zet_debug.cpp +++ b/level_zero/api/tools/zet_debug.cpp @@ -21,13 +21,13 @@ zetDebugAttach( zet_device_handle_t hDevice, const zet_debug_config_t *config, zet_debug_session_handle_t *phDebug) { - return L0::debugAttach(hDevice, config, phDebug); + return L0::DebugApiHandlers::debugAttach(hDevice, config, phDebug); } ZE_APIEXPORT ze_result_t ZE_APICALL zetDebugDetach( zet_debug_session_handle_t hDebug) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugDetach(hDebug); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -35,21 +35,21 @@ zetDebugReadEvent( zet_debug_session_handle_t hDebug, uint64_t timeout, zet_debug_event_t *event) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugReadEvent(hDebug, timeout, event); } ZE_APIEXPORT ze_result_t ZE_APICALL zetDebugInterrupt( zet_debug_session_handle_t hDebug, ze_device_thread_t thread) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugInterrupt(hDebug, thread); } ZE_APIEXPORT ze_result_t ZE_APICALL zetDebugResume( zet_debug_session_handle_t hDebug, ze_device_thread_t thread) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugResume(hDebug, thread); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -59,7 +59,7 @@ zetDebugReadMemory( const zet_debug_memory_space_desc_t *desc, size_t size, void *buffer) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugReadMemory(hDebug, thread, desc, size, buffer); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -69,14 +69,14 @@ zetDebugWriteMemory( const zet_debug_memory_space_desc_t *desc, size_t size, const void *buffer) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugWriteMemory(hDebug, thread, desc, size, buffer); } ZE_APIEXPORT ze_result_t ZE_APICALL zetDebugAcknowledgeEvent( zet_debug_session_handle_t hDebug, const zet_debug_event_t *event) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugAcknowledgeEvent(hDebug, event); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -84,7 +84,7 @@ zetDebugGetRegisterSetProperties( zet_device_handle_t hDevice, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugGetRegisterSetProperties(hDevice, pCount, pRegisterSetProperties); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -95,7 +95,7 @@ zetDebugReadRegisters( uint32_t start, uint32_t count, void *pRegisterValues) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugReadRegisters(hDebug, thread, type, start, count, pRegisterValues); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -106,5 +106,5 @@ zetDebugWriteRegisters( uint32_t start, uint32_t count, void *pRegisterValues) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::DebugApiHandlers::debugWriteRegisters(hDebug, thread, type, start, count, pRegisterValues); } \ No newline at end of file diff --git a/level_zero/core/source/device/device.h b/level_zero/core/source/device/device.h index f085ea7e3a..59d5aeda5e 100644 --- a/level_zero/core/source/device/device.h +++ b/level_zero/core/source/device/device.h @@ -91,6 +91,7 @@ struct Device : _ze_device_handle_t { virtual uint32_t getPlatformInfo() const = 0; virtual MetricContext &getMetricContext() = 0; virtual DebugSession *getDebugSession(const zet_debug_config_t &config) = 0; + virtual void removeDebugSession() = 0; virtual ze_result_t activateMetricGroups(uint32_t count, zet_metric_group_handle_t *phMetricGroups) = 0; diff --git a/level_zero/core/source/device/device_imp.h b/level_zero/core/source/device/device_imp.h index eef3153242..c18afe7182 100644 --- a/level_zero/core/source/device/device_imp.h +++ b/level_zero/core/source/device/device_imp.h @@ -59,6 +59,7 @@ struct DeviceImp : public Device { uint32_t getPlatformInfo() const override; MetricContext &getMetricContext() override; DebugSession *getDebugSession(const zet_debug_config_t &config) override; + void removeDebugSession() override { debugSession.release(); } uint32_t getMaxNumHwThreads() const override; ze_result_t activateMetricGroups(uint32_t count, diff --git a/level_zero/core/test/unit_tests/mocks/mock_device.h b/level_zero/core/test/unit_tests/mocks/mock_device.h index fa0850377a..a3949e211b 100644 --- a/level_zero/core/test/unit_tests/mocks/mock_device.h +++ b/level_zero/core/test/unit_tests/mocks/mock_device.h @@ -250,6 +250,8 @@ struct Mock : public Device { DebugSession *getDebugSession(const zet_debug_config_t &config) override { return nullptr; } + + void removeDebugSession() override {} }; template <> diff --git a/level_zero/tools/source/debug/debug_handlers.cpp b/level_zero/tools/source/debug/debug_handlers.cpp index 6cdf7aa1d3..ed7a793f3e 100644 --- a/level_zero/tools/source/debug/debug_handlers.cpp +++ b/level_zero/tools/source/debug/debug_handlers.cpp @@ -5,6 +5,8 @@ * */ +#include "level_zero/tools/source/debug/debug_handlers.h" + #include "level_zero/tools/source/debug/debug_session.h" namespace L0 { @@ -13,8 +15,39 @@ DebugSession *DebugSession::create(const zet_debug_config_t &config, Device *dev return nullptr; } +namespace DebugApiHandlers { ze_result_t debugAttach(zet_device_handle_t hDevice, const zet_debug_config_t *config, zet_debug_session_handle_t *phDebug) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } +ze_result_t debugDetach(zet_debug_session_handle_t hDebug) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } + +ze_result_t debugReadEvent(zet_debug_session_handle_t hDebug, uint64_t timeout, zet_debug_event_t *event) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } + +ze_result_t debugInterrupt(zet_debug_session_handle_t hDebug, ze_device_thread_t thread) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } + +ze_result_t debugResume(zet_debug_session_handle_t hDebug, ze_device_thread_t thread) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } + +ze_result_t debugReadMemory(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, void *buffer) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t debugWriteMemory(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, const void *buffer) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t debugAcknowledgeEvent(zet_debug_session_handle_t hDebug, const zet_debug_event_t *event) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } + +ze_result_t debugGetRegisterSetProperties(zet_device_handle_t hDevice, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t debugReadRegisters(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, zet_debug_regset_type_t type, uint32_t start, uint32_t count, void *pRegisterValues) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t debugWriteRegisters(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, zet_debug_regset_type_t type, uint32_t start, uint32_t count, void *pRegisterValues) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} +} // namespace DebugApiHandlers } // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/source/debug/debug_handlers.h b/level_zero/tools/source/debug/debug_handlers.h index e631e957dc..f63670feb5 100644 --- a/level_zero/tools/source/debug/debug_handlers.h +++ b/level_zero/tools/source/debug/debug_handlers.h @@ -9,5 +9,17 @@ #include namespace L0 { +namespace DebugApiHandlers { ze_result_t debugAttach(zet_device_handle_t hDevice, const zet_debug_config_t *config, zet_debug_session_handle_t *phDebug); -} \ No newline at end of file +ze_result_t debugDetach(zet_debug_session_handle_t hDebug); +ze_result_t debugReadEvent(zet_debug_session_handle_t hDebug, uint64_t timeout, zet_debug_event_t *event); +ze_result_t debugInterrupt(zet_debug_session_handle_t hDebug, ze_device_thread_t thread); +ze_result_t debugResume(zet_debug_session_handle_t hDebug, ze_device_thread_t thread); +ze_result_t debugReadMemory(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, void *buffer); +ze_result_t debugWriteMemory(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, const void *buffer); +ze_result_t debugAcknowledgeEvent(zet_debug_session_handle_t hDebug, const zet_debug_event_t *event); +ze_result_t debugGetRegisterSetProperties(zet_device_handle_t hDevice, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties); +ze_result_t debugReadRegisters(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, zet_debug_regset_type_t type, uint32_t start, uint32_t count, void *pRegisterValues); +ze_result_t debugWriteRegisters(zet_debug_session_handle_t hDebug, ze_device_thread_t thread, zet_debug_regset_type_t type, uint32_t start, uint32_t count, void *pRegisterValues); +} // namespace DebugApiHandlers +} // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/source/debug/debug_session.h b/level_zero/tools/source/debug/debug_session.h index f3c31ce42e..321da4d096 100644 --- a/level_zero/tools/source/debug/debug_session.h +++ b/level_zero/tools/source/debug/debug_session.h @@ -23,8 +23,13 @@ struct DebugSession : _zet_debug_session_handle_t { static DebugSession *fromHandle(zet_debug_session_handle_t handle) { return static_cast(handle); } inline zet_debug_session_handle_t toHandle() { return this; } + virtual bool closeConnection() = 0; + + Device *getConnectedDevice() { return connectedDevice; } + protected: - DebugSession(const zet_debug_config_t &config, Device *device){}; + DebugSession(const zet_debug_config_t &config, Device *device) : connectedDevice(device){}; + Device *connectedDevice = nullptr; }; } // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/test/unit_tests/sources/debug/mock_debug_session.h b/level_zero/tools/test/unit_tests/sources/debug/mock_debug_session.h index 0b6e23fbf7..e242b81ec3 100644 --- a/level_zero/tools/test/unit_tests/sources/debug/mock_debug_session.h +++ b/level_zero/tools/test/unit_tests/sources/debug/mock_debug_session.h @@ -22,6 +22,7 @@ class OsInterfaceWithDebugAttach : public NEO::OSInterface { struct DebugSessionMock : public L0::DebugSession { DebugSessionMock(const zet_debug_config_t &config, L0::Device *device) : DebugSession(config, device){}; + bool closeConnection() override { return true; } }; } // namespace ult diff --git a/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.cpp b/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.cpp index 950027e10f..923c29d140 100644 --- a/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.cpp +++ b/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.cpp @@ -9,6 +9,8 @@ #include "test.h" +#include "level_zero/tools/source/debug/debug_handlers.h" + namespace L0 { namespace ult { @@ -61,5 +63,42 @@ TEST(DebugSessionTest, WhenDebugSessionCreateIsCalledThenNullptrReturned) { EXPECT_EQ(nullptr, session); } +TEST(DebugSessionTest, WhenUnsupportedFunctionCalledThenErrorIsReturned) { + zet_debug_session_handle_t session = {}; + + auto result = L0::DebugApiHandlers::debugDetach(session); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + result = L0::DebugApiHandlers::debugReadEvent(session, 0, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + ze_device_thread_t thread = {}; + result = L0::DebugApiHandlers::debugInterrupt(session, thread); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + result = L0::DebugApiHandlers::debugResume(session, thread); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + result = L0::DebugApiHandlers::debugReadMemory(session, thread, nullptr, 0, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + result = L0::DebugApiHandlers::debugWriteMemory(session, thread, nullptr, 0, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + result = L0::DebugApiHandlers::debugAcknowledgeEvent(session, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + zet_device_handle_t hDevice = {}; + result = L0::DebugApiHandlers::debugGetRegisterSetProperties(hDevice, nullptr, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + zet_debug_regset_type_t type = {ZET_DEBUG_REGSET_TYPE_INVALID}; + result = L0::DebugApiHandlers::debugReadRegisters(session, thread, type, 0, 0, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + result = L0::DebugApiHandlers::debugWriteRegisters(session, thread, type, 0, 0, nullptr); + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); +} + } // namespace ult } // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.inl b/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.inl index 601c5c8ffa..6872e0b2b3 100644 --- a/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.inl +++ b/level_zero/tools/test/unit_tests/sources/debug/test_debug_api.inl @@ -107,5 +107,33 @@ TEST(DebugSessionTest, givenDebugSessionWhenConvertingToAndFromHandleCorrectHand EXPECT_EQ(session, sessionFromHandle); } +TEST(DebugSessionTest, givenDebugSessionWhenGettingConnectedDeviceThenCorrectDeviceIsReturned) { + zet_debug_config_t config = {}; + config.pid = 0x1234; + + NEO::Device *neoDevice(NEO::MockDevice::createWithNewExecutionEnvironment(NEO::defaultHwInfo.get(), 0)); + Mock deviceImp(neoDevice, neoDevice->getExecutionEnvironment()); + auto debugSession = std::make_unique(config, &deviceImp); + L0::DebugSession *session = debugSession.get(); + + auto device = session->getConnectedDevice(); + + EXPECT_EQ(&deviceImp, device); +} + +TEST(DebugSessionTest, givenDeviceWithDebugSessionWhenRemoveCalledThenSessionIsNotDeleted) { + zet_debug_config_t config = {}; + config.pid = 0x1234; + + NEO::Device *neoDevice(NEO::MockDevice::createWithNewExecutionEnvironment(NEO::defaultHwInfo.get(), 0)); + Mock deviceImp(neoDevice, neoDevice->getExecutionEnvironment()); + auto debugSession = std::make_unique(config, &deviceImp); + L0::DebugSession *session = debugSession.get(); + deviceImp.debugSession.reset(session); + deviceImp.removeDebugSession(); + + EXPECT_EQ(nullptr, deviceImp.debugSession.get()); +} + } // namespace ult } // namespace L0