From 402c22c02024ed784898c0b656960d3f771d3781 Mon Sep 17 00:00:00 2001 From: Walter Erquinigo Date: Tue, 1 Jul 2025 21:07:22 +0000 Subject: [PATCH 1/3] [LLDB][GPU] Decouple plugins and scaffold documentation - Add LLDB_ENABLE__PLUGIN variables to programmatically enable each GPU plugin - Make all the activation logic for all those plugins independent. This results in no `else` conditions, which makes rebases simpler. - Theoretically, this would allow multiple plugins to be running at the same time. This sounds nice even though I don't think we'll ever work on it. - Scaffold a tiny documentation file where we can put all the information related to the GPU plugins. --- lldb/docs/use/gpu-support.rst | 15 +++++++++++ .../GDBRemoteCommunicationServerLLGS.h | 6 +---- lldb/tools/lldb-server/CMakeLists.txt | 18 ++++++++++--- lldb/tools/lldb-server/Plugins/CMakeLists.txt | 7 +++-- lldb/tools/lldb-server/lldb-gdbserver.cpp | 26 +++++++++---------- 5 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 lldb/docs/use/gpu-support.rst diff --git a/lldb/docs/use/gpu-support.rst b/lldb/docs/use/gpu-support.rst new file mode 100644 index 0000000000000..d56526617aa53 --- /dev/null +++ b/lldb/docs/use/gpu-support.rst @@ -0,0 +1,15 @@ +GPU Support in LLDB +==================== + +AMD +------ + +System requirements +^^^^^^^^^^^^^^^^^^^ + +Mention how to set up ROMC. + +CMake +^^^^^ + +Mention something about the ROCM_PATH CMake variable and setting LLDB_ENABLE_AMDGPU_PLUGIN = ON. \ No newline at end of file diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h index 4fe653e232ec5..ad08f7ffd4f04 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h @@ -18,17 +18,13 @@ #include "lldb/Host/common/NativeProcessProtocol.h" #include "lldb/Utility/RegisterValue.h" #include "lldb/lldb-private-forward.h" +#include "LLDBServerPlugin.h" #include "GDBRemoteCommunicationServerCommon.h" class StringExtractorGDBRemote; namespace lldb_private { - -namespace lldb_server { - class LLDBServerPlugin; -} - namespace process_gdb_remote { class ProcessGDBRemote; diff --git a/lldb/tools/lldb-server/CMakeLists.txt b/lldb/tools/lldb-server/CMakeLists.txt index 3248063f0e8e6..ef086be5739c7 100644 --- a/lldb/tools/lldb-server/CMakeLists.txt +++ b/lldb/tools/lldb-server/CMakeLists.txt @@ -42,10 +42,23 @@ if(APPLE_EMBEDDED) endif() endif() -if(DEFINED ROCM_PATH) +option(LLDB_ENABLE_AMDGPU_PLUGIN "Enable support for the AMD GPU plugin" OFF) +option(LLDB_ENABLE_MOCK_GPU_PLUGIN "Enable support for the Mock GPU plugin" OFF) + +if(LLDB_ENABLE_AMDGPU_PLUGIN) + if(NOT DEFINED ROCM_PATH) + message(FATAL_ERROR "ROCM_PATH is not defined and LLDB_ENABLE_AMDGPU_PLUGIN expects it") + endif() + + if(NOT EXISTS ${ROCM_PATH}) + message(FATAL_ERROR "ROCM_PATH does not exist: ${ROCM_PATH}") + endif() + add_definitions(-DLLDB_ENABLE_AMDGPU_PLUGIN=1) list(APPEND LLDB_PLUGINS lldbServerPluginAMDGPU) -else() +endif() + +if(LLDB_ENABLE_MOCK_GPU_PLUGIN) add_definitions(-DLLDB_ENABLE_MOCKGPU_PLUGIN=1) list(APPEND LLDB_PLUGINS lldbServerPluginMockGPU) endif() @@ -71,7 +84,6 @@ add_lldb_tool(lldb-server lldbPluginInstructionMIPS64 lldbPluginInstructionRISCV ${LLDB_SYSTEM_LIBS} - lldbServerPluginMockGPU ) set_target_properties(lldb-server PROPERTIES ENABLE_EXPORTS 1) diff --git a/lldb/tools/lldb-server/Plugins/CMakeLists.txt b/lldb/tools/lldb-server/Plugins/CMakeLists.txt index e1431e20265f6..18e5f9af4b26d 100644 --- a/lldb/tools/lldb-server/Plugins/CMakeLists.txt +++ b/lldb/tools/lldb-server/Plugins/CMakeLists.txt @@ -1,4 +1,7 @@ -if(DEFINED ROCM_PATH AND EXISTS ${ROCM_PATH}) +if(LLDB_ENABLE_AMDGPU_PLUGIN) add_subdirectory(AMDGPU) endif() -add_subdirectory(MockGPU) + +if(LLDB_ENABLE_MOCK_GPU_PLUGIN) + add_subdirectory(MockGPU) +endif() diff --git a/lldb/tools/lldb-server/lldb-gdbserver.cpp b/lldb/tools/lldb-server/lldb-gdbserver.cpp index 4f818c15dbe5b..91876267cb49a 100644 --- a/lldb/tools/lldb-server/lldb-gdbserver.cpp +++ b/lldb/tools/lldb-server/lldb-gdbserver.cpp @@ -49,10 +49,10 @@ #if defined(LLDB_ENABLE_AMDGPU_PLUGIN) #include "Plugins/AMDGPU/LLDBServerPluginAMDGPU.h" -typedef lldb_private::lldb_server::LLDBServerPluginAMDGPU LLDBServerGPUPlugin; -#elif defined(LLDB_ENABLE_MOCKGPU_PLUGIN) +#endif + +#if defined(LLDB_ENABLE_MOCKGPU_PLUGIN) #include "Plugins/MockGPU/LLDBServerPluginMockGPU.h" -typedef lldb_private::lldb_server::LLDBServerPluginMockGPU LLDBServerGPUPlugin; #endif #ifndef LLGS_PROGRAM_NAME @@ -461,19 +461,19 @@ int main_gdbserver(int argc, char *argv[]) { NativeProcessManager manager(mainloop); GDBRemoteCommunicationServerLLGS gdb_server(mainloop, manager, "gdb-server"); -#if defined(LLDB_ENABLE_AMDGPU_PLUGIN) || defined(LLDB_ENABLE_MOCKGPU_PLUGIN) #if defined(LLDB_ENABLE_AMDGPU_PLUGIN) - // AMD GPU plugin requires to use the same mainloop as the native process. - // This is because AMD debug API has to be called from the same thread as the - // ptrace() thread. - MainLoop &gpu_mainloop = mainloop; -#else - // Any GPU plugins can use a separate mainloop. - MainLoop gpu_mainloop; + { + // AMD GPU plugin requires to use the same mainloop as the native process. + // This is because AMD debug API has to be called from the same thread as the + // ptrace() thread. + gdb_server.InstallPlugin( + std::make_unique(gdb_server, mainloop)); + } #endif - // Install GPU plugin. +#if defined(LLDB_ENABLE_MOCKGPU_PLUGIN) + MainLoop &gpu_mainloop = mainloop; gdb_server.InstallPlugin( - std::make_unique(gdb_server, gpu_mainloop)); + std::make_unique(gdb_server, gpu_mainloop)); #endif llvm::StringRef host_and_port; From 6abde2adb5597e0de127b71059eaf8095fe1edf0 Mon Sep 17 00:00:00 2001 From: Walter Erquinigo Date: Tue, 1 Jul 2025 21:45:12 +0000 Subject: [PATCH 2/3] fix header --- .../Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h | 6 +++++- lldb/tools/lldb-server/lldb-gdbserver.cpp | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h index ad08f7ffd4f04..4fe653e232ec5 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h @@ -18,13 +18,17 @@ #include "lldb/Host/common/NativeProcessProtocol.h" #include "lldb/Utility/RegisterValue.h" #include "lldb/lldb-private-forward.h" -#include "LLDBServerPlugin.h" #include "GDBRemoteCommunicationServerCommon.h" class StringExtractorGDBRemote; namespace lldb_private { + +namespace lldb_server { + class LLDBServerPlugin; +} + namespace process_gdb_remote { class ProcessGDBRemote; diff --git a/lldb/tools/lldb-server/lldb-gdbserver.cpp b/lldb/tools/lldb-server/lldb-gdbserver.cpp index 91876267cb49a..572282b823d34 100644 --- a/lldb/tools/lldb-server/lldb-gdbserver.cpp +++ b/lldb/tools/lldb-server/lldb-gdbserver.cpp @@ -36,6 +36,7 @@ #include "llvm/Support/Errno.h" #include "llvm/Support/Error.h" #include "llvm/Support/WithColor.h" +#include "Plugins/Process/gdb-remote/LLDBServerPlugin.h" #if defined(__linux__) #include "Plugins/Process/Linux/NativeProcessLinux.h" From aa18e8fb08cdf9a7410e4acf082292ed0ec72a87 Mon Sep 17 00:00:00 2001 From: Walter Erquinigo Date: Wed, 2 Jul 2025 13:58:11 -0700 Subject: [PATCH 3/3] ensure only one plugin is enabled at a time --- lldb/tools/lldb-server/CMakeLists.txt | 13 +++++++++++-- .../tools/lldb-server/Plugins/AMDGPU/CMakeLists.txt | 10 ---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lldb/tools/lldb-server/CMakeLists.txt b/lldb/tools/lldb-server/CMakeLists.txt index ef086be5739c7..8cdf5411dbd53 100644 --- a/lldb/tools/lldb-server/CMakeLists.txt +++ b/lldb/tools/lldb-server/CMakeLists.txt @@ -45,22 +45,31 @@ endif() option(LLDB_ENABLE_AMDGPU_PLUGIN "Enable support for the AMD GPU plugin" OFF) option(LLDB_ENABLE_MOCK_GPU_PLUGIN "Enable support for the Mock GPU plugin" OFF) +set(GPU_PLUGINS_ENABLED 0) + if(LLDB_ENABLE_AMDGPU_PLUGIN) if(NOT DEFINED ROCM_PATH) - message(FATAL_ERROR "ROCM_PATH is not defined and LLDB_ENABLE_AMDGPU_PLUGIN expects it") + message(FATAL_ERROR "ROCM_PATH is not defined and LLDB_ENABLE_AMDGPU_PLUGIN expects it. Use -DROCM_PATH=/path/to/rocm") endif() if(NOT EXISTS ${ROCM_PATH}) - message(FATAL_ERROR "ROCM_PATH does not exist: ${ROCM_PATH}") + message(FATAL_ERROR "ROCM_PATH does not exist: ${ROCM_PATH}. Use -DROCM_PATH=/path/to/rocm") endif() + message(STATUS "ROCM_PATH is set to: '${ROCM_PATH}'") add_definitions(-DLLDB_ENABLE_AMDGPU_PLUGIN=1) list(APPEND LLDB_PLUGINS lldbServerPluginAMDGPU) + math(EXPR GPU_PLUGINS_ENABLED "${GPU_PLUGINS_ENABLED} + 1") endif() if(LLDB_ENABLE_MOCK_GPU_PLUGIN) add_definitions(-DLLDB_ENABLE_MOCKGPU_PLUGIN=1) list(APPEND LLDB_PLUGINS lldbServerPluginMockGPU) + math(EXPR GPU_PLUGINS_ENABLED "${GPU_PLUGINS_ENABLED} + 1") +endif() + +if(GPU_PLUGINS_ENABLED GREATER 1) + message(FATAL_ERROR "Only one GPU plugin can be enabled at a time") endif() add_lldb_tool(lldb-server diff --git a/lldb/tools/lldb-server/Plugins/AMDGPU/CMakeLists.txt b/lldb/tools/lldb-server/Plugins/AMDGPU/CMakeLists.txt index 99493a3b03fb6..9a0c2eeac699d 100644 --- a/lldb/tools/lldb-server/Plugins/AMDGPU/CMakeLists.txt +++ b/lldb/tools/lldb-server/Plugins/AMDGPU/CMakeLists.txt @@ -1,13 +1,3 @@ - -message(STATUS "ROCM_PATH is set to: '${ROCM_PATH}'") -if(NOT ROCM_PATH) - message(FATAL_ERROR "ROCM_PATH must be specified. Use -DROCM_PATH=/path/to/rocm") -endif() - -if (NOT EXISTS ${ROCM_PATH}) - message(FATAL_ERROR "ROCM_PATH does not exist: '${ROCM_PATH}'") -endif() - # Find AMD-dbgapi package using the provided CMake config set(amd-dbgapi_DIR "${ROCM_PATH}/lib/cmake/amd-dbgapi" CACHE PATH "Path to amd-dbgapi cmake config") find_package(amd-dbgapi REQUIRED CONFIG)