From patchwork Tue Jan 14 19:16:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 104781 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 00A7238560A8 for ; Tue, 14 Jan 2025 19:28:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00A7238560A8 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=default header.b=AssRAIQL X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 90807385627E for ; Tue, 14 Jan 2025 19:26:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90807385627E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 90807385627E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736882801; cv=none; b=WCbTi42/sIJVNblcirBRF2x0Ozc18acQHxY1vNanSouOxTro405X8YTN+a/+4Z3WsCmamX7E9Hh/NCyQGPRbx4+4dzOnniAbNYVEJ2EuXlcgyrN2uh10Zr1X6SrdZh7wki796Y2go0QZbh5Ea17u9ORkSR5MeWqzOl8bnbpkPCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736882801; c=relaxed/simple; bh=HzoXbexqiDnWFcUiGFsKWa0VLwoMcAJ/lo+G6S6qtks=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SoaEwPnSbkVPdehHYsnib3ATmmzmPkw0cx9X9JV1TqQFEZnz5vIrqqTRAFJRsJ7lAv1SpnaMUBQxLvmgD2szcmk/Jz0i+xOCG/8Jcmv2qAaHe0vr6y7CCHUdkTEXJPWwtqy/zml0s7vmAHq+awGvhjhGemMrRSq1sQuxWq2Ctp4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 90807385627E Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 50EJQaXZ068779 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 14 Jan 2025 14:26:41 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 50EJQaXZ068779 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=default; t=1736882801; bh=ENWX3eI2ez2+hRo7pngnwQSGkYc0lJWqgB7PHWRQXrg=; h=From:To:Cc:Subject:Date:In-Reply-To:From; b=AssRAIQLc1M1Mcq8aXA8yjOHJ+1PXPV0Mn3+Cpm31R4Pn7f0ixtMCMPKWtHb1lq4f loaGN4BCDXlu+TZuspTHKLT1Zxt7T4VHPi7vQdWNUAD8CgSSadQrTTzvPkOsYQMnfN ExivOwFEal+tvepBpqSRyYgzP9nax7PUfPumlprc= Received: by simark.ca (Postfix, from userid 112) id 2C6A61E116; Tue, 14 Jan 2025 14:18:26 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-Spam-Level: X-Spam-Status: No, score=-3188.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 Received: from simark.localdomain (modemcable238.237-201-24.mc.videotron.ca [24.201.237.238]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 9D7D81E105; Tue, 14 Jan 2025 14:18:19 -0500 (EST) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Lancelot SIX , Simon Marchi Subject: [PATCH 3/3] gdb/amd-dbgapi: add displaced stepping support Date: Tue, 14 Jan 2025 14:16:23 -0500 Message-ID: <20250114191818.2393132-4-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114191818.2393132-1-simon.marchi@polymtl.ca> References: <20250114191818.2393132-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Tue, 14 Jan 2025 19:26:36 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org From: Simon Marchi Implement the target_ops displaced stepping methods to add displaced stepping support when debugging AMD GPU programs. The knowledge of how to prepare and finish displaced steps is provided by the amd-dbgapi library, so the code here is relatively straightforward. No need to parse instructions or handle fixups, that is done by the lib We just need to remember, for each thread doing a displaced step, the displaced stepping id given by the library. Add a test to exercise the new functionality. The compiler generates DWARF that GDB doesn't understand yet [1], so trying to step over a breakpoint with DWARF present gives: (gdb) si Unhandled dwarf expression opcode 0xe9 The test purposefully builds the binary without DWARF info to circumvent this. [1] https://llvm.org/docs/AMDGPUDwarfExtensionsForHeterogeneousDebugging.html Change-Id: I53f459221a42d4b02a6041eadb8cf554500e2162 --- gdb/amd-dbgapi-target.c | 138 ++++++++++++++++++ gdb/testsuite/gdb.rocm/displaced-stepping.exp | 54 +++++++ 2 files changed, 192 insertions(+) create mode 100644 gdb/testsuite/gdb.rocm/displaced-stepping.exp diff --git a/gdb/amd-dbgapi-target.c b/gdb/amd-dbgapi-target.c index 153a35f788ed..996e16abf591 100644 --- a/gdb/amd-dbgapi-target.c +++ b/gdb/amd-dbgapi-target.c @@ -24,6 +24,7 @@ #include "cli/cli-cmds.h" #include "cli/cli-decode.h" #include "cli/cli-style.h" +#include "gdbcore.h" #include "gdbsupport/unordered_map.h" #include "inf-loop.h" #include "inferior.h" @@ -215,6 +216,12 @@ struct amd_dbgapi_inferior_info /* List of pending events the amd-dbgapi target retrieved from the dbgapi. */ std::list> wave_events; + /* Map of threads with ongoing displaced steps to corresponding amd-dbgapi + displaced stepping handles. */ + gdb::unordered_map + stepping_id_map; + /* Map of wave ID to wave_info. We cache wave_info objects because we need to access the info after the wave is gone, in the thread exit nofication. E.g.: @@ -291,6 +298,21 @@ struct amd_dbgapi_target final : public target_ops bool stopped_by_sw_breakpoint () override; bool stopped_by_hw_breakpoint () override; + bool supports_displaced_step (thread_info *thread) override + { + /* Handle displaced stepping for GPU threads only. */ + if (!ptid_is_gpu (thread->ptid)) + return beneath ()->supports_displaced_step (thread); + + return true; + } + + displaced_step_prepare_status displaced_step_prepare + (thread_info *thread, CORE_ADDR &displaced_pc) override; + + displaced_step_finish_status displaced_step_finish + (thread_info *thread, const target_waitstatus &status) override; + private: /* True if we must report thread events. */ bool m_report_thread_events = false; @@ -1897,6 +1919,122 @@ amd_dbgapi_target::update_thread_list () this->beneath ()->update_thread_list (); } +displaced_step_prepare_status +amd_dbgapi_target::displaced_step_prepare (thread_info *thread, + CORE_ADDR &displaced_pc) +{ + if (!ptid_is_gpu (thread->ptid)) + return beneath ()->displaced_step_prepare (thread, displaced_pc); + + gdb_assert (!thread->displaced_step_state.in_progress ()); + + /* Read the bytes that were overwritten by the breakpoint instruction being + stepped over. */ + CORE_ADDR original_pc = regcache_read_pc (get_thread_regcache (thread)); + gdbarch *arch = get_thread_regcache (thread)->arch (); + size_t size = get_amdgpu_gdbarch_tdep (arch)->breakpoint_instruction_size; + gdb::byte_vector overwritten_bytes (size); + + read_memory (original_pc, overwritten_bytes.data (), size); + + /* Ask dbgapi to start the displaced step. */ + amd_dbgapi_wave_id_t wave_id = get_amd_dbgapi_wave_id (thread->ptid); + amd_dbgapi_displaced_stepping_id_t stepping_id; + amd_dbgapi_status_t status + = amd_dbgapi_displaced_stepping_start (wave_id, overwritten_bytes.data (), + &stepping_id); + + switch (status) + { + case AMD_DBGAPI_STATUS_SUCCESS: + break; + + case AMD_DBGAPI_STATUS_ERROR_DISPLACED_STEPPING_BUFFER_NOT_AVAILABLE: + return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE; + + case AMD_DBGAPI_STATUS_ERROR_ILLEGAL_INSTRUCTION: + return DISPLACED_STEP_PREPARE_STATUS_CANT; + + default: + error (_("amd_dbgapi_displaced_stepping_start failed (%s)"), + get_status_string (status)); + } + + /* Save the displaced stepping id in the per-inferior info. */ + amd_dbgapi_inferior_info *info = get_amd_dbgapi_inferior_info (thread->inf); + + bool inserted + = info->stepping_id_map.emplace (thread, stepping_id.handle).second; + gdb_assert (inserted); + + /* Get the new (displaced) PC. */ + status = amd_dbgapi_wave_get_info (wave_id, AMD_DBGAPI_WAVE_INFO_PC, + sizeof (displaced_pc), &displaced_pc); + if (status != AMD_DBGAPI_STATUS_SUCCESS) + { + amd_dbgapi_displaced_stepping_complete (wave_id, stepping_id); + error (_("amd_dbgapi_wave_get_info failed (%s), could not get the " + "thread's displaced PC."), + get_status_string (status)); + } + + displaced_debug_printf ("selected buffer at %#lx", displaced_pc); + + /* We may have written some registers, so flush the register cache. */ + registers_changed_thread (thread); + + return DISPLACED_STEP_PREPARE_STATUS_OK; +} + +displaced_step_finish_status +amd_dbgapi_target::displaced_step_finish (thread_info *thread, + const target_waitstatus &ws) +{ + if (!ptid_is_gpu (thread->ptid)) + return beneath ()->displaced_step_finish (thread, ws); + + gdb_assert (thread->displaced_step_state.in_progress ()); + + /* Find the displaced stepping id for this thread. */ + amd_dbgapi_inferior_info *info = get_amd_dbgapi_inferior_info (thread->inf); + auto entry = info->stepping_id_map.extract (thread); + + gdb_assert (entry.has_value ()); + amd_dbgapi_displaced_stepping_id_t stepping_id {entry->second}; + + /* If the thread exited while stepping, we are done. The code above + cleared our associated resources. We don't want to call dbgapi + below: since the thread is gone, we wouldn't be able to find the + necessary wave ID. dbgapi already took care of releasing its + displaced-stepping-related resources when it deleted the + wave. */ + if (ws.kind () == TARGET_WAITKIND_THREAD_EXITED) + return DISPLACED_STEP_FINISH_STATUS_OK; + + amd_dbgapi_wave_id_t wave_id = get_amd_dbgapi_wave_id (thread->ptid); + amd_dbgapi_wave_stop_reasons_t stop_reason; + amd_dbgapi_status_t status + = amd_dbgapi_wave_get_info (wave_id, AMD_DBGAPI_WAVE_INFO_STOP_REASON, + sizeof (stop_reason), &stop_reason); + + if (status != AMD_DBGAPI_STATUS_SUCCESS) + error (_("wave_get_info for wave_%ld failed (%s)"), wave_id.handle, + get_status_string (status)); + + status = amd_dbgapi_displaced_stepping_complete (wave_id, stepping_id); + + if (status != AMD_DBGAPI_STATUS_SUCCESS) + error (_("amd_dbgapi_displaced_stepping_complete failed (%s)"), + get_status_string (status)); + + /* We may have written some registers, so flush the register cache. */ + registers_changed_thread (thread); + + return (stop_reason & AMD_DBGAPI_WAVE_STOP_REASON_SINGLE_STEP) != 0 + ? DISPLACED_STEP_FINISH_STATUS_OK + : DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED; +} + /* inferior_created observer. */ static void diff --git a/gdb/testsuite/gdb.rocm/displaced-stepping.exp b/gdb/testsuite/gdb.rocm/displaced-stepping.exp new file mode 100644 index 000000000000..6b26e1adfd7a --- /dev/null +++ b/gdb/testsuite/gdb.rocm/displaced-stepping.exp @@ -0,0 +1,54 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test displaced stepping on AMD GPUs. + +load_lib rocm.exp + +standard_testfile simple.cpp + +require allow_hipcc_tests + +# Since GDB doesn't yet understand DWARF expressions generated by the HIP +# compiler, purposefully generate the binary without debug info. +if {[build_executable "failed to prepare" $testfile $srcfile {hip}]} { + return +} + +proc do_test {} { + clean_restart $::binfile + + with_rocm_gpu_lock { + if ![runto_main] { + return + } + + gdb_test "with breakpoint pending on -- break do_an_addition" \ + "Breakpoint $::decimal \\(do_an_addition\\) pending." + + gdb_test "continue" \ + "Thread $::decimal hit Breakpoint $::decimal, $::hex in do_an_addition.*" + + gdb_test_no_output "set debug displaced on" + + gdb_test "stepi" "displaced_step_prepare_throw: prepared successfully.*$::hex in do_an_addition.*" + + gdb_test "continue" \ + "Inferior 1 .* exited normally.*" \ + "continue to end" + } +} + +do_test