From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by sourceware.org (Postfix) with ESMTPS id B8CA6383907A for ; Fri, 30 Jun 2023 14:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8CA6383907A Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ggXW2gE9vvKUFWoFB682QZMCtaBCuy9w2rtCZBSkAdqZ+mnIpHaxhh6684EZfw1+GGrm3fvRLrQThb+I5/VDPcf1bAPW9E7mPFzRIlFuHwlq3uQIs98VAX5a2pQJCcMHi8FliWVzueNH4GZYA9CetgofvQjj+7Cw909s3eOI38cylA6m3yuLfQGdQlCSJm4uWaJPJBPYw0m/9ZNhjDM7JwCBeomP4LCmkxQEpgDYr1Dc7hZEeO71mRHrTHs3yxEaTtvu1Gm8fT2Of+Xjcwu80nN4kQJgoupINS217jCU6MvRZX3won1fwolcrx703JzwVb52cabt0BMTtzzFJkytdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ijartwDYTYtD/dslTSnV6hSXJFSFUxhfnIBKkdCDRCA=; b=coEjigEWS4Gc0eUG0GHNhjzw22uOaBDJ60xWprO0gBAoX+mn4L2ajj7UtB7GDiC0vInvgeBnnDBlzDK73Px8tuNc130pCdMt/NtoHLU1BTlu4pZQO6KjFXtjsWDtPkWLj1hanycenavR2gh9RmHI8VROck7kcN4RzUkRrJn7MgEscd/1pu/sbd/r3Gb8WPgtMCDHnGgxwoo78mzU3VhaVEEDYavtEPhm6ggd3W6KThPTuXMtJpP3AqyKmP0AUwE4G2XXQlQJT1e9tPSGkkmpDdQkaCCxFBjWxC7zy/fmfwNcgpx0AMpJLVudfQpqRa3r7PNCOWLBsLaUz73ARMrSRA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=sourceware.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ijartwDYTYtD/dslTSnV6hSXJFSFUxhfnIBKkdCDRCA=; b=V1ccx8BTksa3+yt8l4ZsQI1s6vkBXw8seCuSZaSBJZL2EGwqS48hHxGfXzBlhU+Jp8rD/pLXTEXts/8yiOaoW7EtzmDm4ftrnism1Wu2TU7eDE2tDthkgQhmngw5OYif/6zY2MEO5Ed51kyU/yDNz8yHYJ60gASmUgjs+v/6dj0= Received: from BN8PR07CA0016.namprd07.prod.outlook.com (2603:10b6:408:ac::29) by PH8PR12MB7423.namprd12.prod.outlook.com (2603:10b6:510:229::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6544.19; Fri, 30 Jun 2023 14:58:34 +0000 Received: from BN8NAM11FT067.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ac:cafe::16) by BN8PR07CA0016.outlook.office365.com (2603:10b6:408:ac::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6544.22 via Frontend Transport; Fri, 30 Jun 2023 14:58:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT067.mail.protection.outlook.com (10.13.177.159) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6521.43 via Frontend Transport; Fri, 30 Jun 2023 14:58:33 +0000 Received: from hpe6u-23.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Fri, 30 Jun 2023 09:58:29 -0500 From: Lancelot Six To: CC: , Lancelot Six Subject: [PATCH 2/2] gdb/amdgpu: Fix debugging multiple inferiors using the ROCm runtime Date: Fri, 30 Jun 2023 14:57:55 +0000 Message-ID: <20230630145755.6500-3-lancelot.six@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230630145755.6500-1-lancelot.six@amd.com> References: <20230630145755.6500-1-lancelot.six@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT067:EE_|PH8PR12MB7423:EE_ X-MS-Office365-Filtering-Correlation-Id: d3f46315-f6b6-4c82-4369-08db797a79da X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fRBbzSuejc3ggTlMyOrlxGyfYE4S2l3rb/RH9g2XYht5horw5ETCFsFCeMMNlOkuMFLOwj0y+1ult80mTNKGzN/8rX9N/mU6bvzAr9RBEf2ky+hz7ui4UNI2gLQtHY8iHC7DLI8v+rOZrTODb167E8l85hq1EMsrZHpTwOiHAlsO2cuBQM4uoRz9xFpl1f0tkN5yH4UUW/msS5ZvWpq1S/bvrYSxOiHOArf8/1Je7vNysjGrb47Ez8qORyUdrcecePRSaCaFYPE6mT6CdLH0nrmhCWV0Z4vwN5AkAwDW8X5LX6GwV7HK235spL9nivfvipH7fXnEGqX9qs7Wl8mdfkILOnN9G6nG+JGLAwuDm9DVnwhRQ/RO5fvtwnQ67PNA8q3Ozf12d9r8Lxyf7QSo6sn+MEdGn1UJp3RsWfEAgXX9UQ0tqO6yBa1FuSjA7q2laB3iSXSgdlGCC3+9pWwh7KzB5e18lKDUkv2gk6XdXRHsEfplXxZtGc4IUX0AZ+ch6vNMVI7Gv0h6/aRXnRy9IctRKU3vA+Liae3EXcEsEJxNbFL8D9rn9ucLoXaaQTCjP5BYALEbLxpicI37Wzy9MRFkFAZIBN6Wz3OiKYHHMHTxFxcXPoIPaxGQHjzqFJIXkWM4UlLulZykKCwSwPE+9sPZnjONk5OrKorcsH6i+goRFWg8mVskhW96A51YWMJQezN8ZK6rLQJn/FGj/jsY53kDx8UWJCxZ7ELtGxbjSjbhBpYYY6sO8K4jGGb1ooaAj+/rgU8ExNGYMvmB4qNmyQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(346002)(376002)(396003)(451199021)(46966006)(36840700001)(40470700004)(336012)(426003)(47076005)(40460700003)(83380400001)(30864003)(2616005)(2906002)(81166007)(356005)(82740400003)(36860700001)(40480700001)(36756003)(8936002)(8676002)(82310400005)(5660300002)(86362001)(19627235002)(54906003)(41300700001)(6916009)(4326008)(70206006)(70586007)(6666004)(316002)(7696005)(478600001)(16526019)(186003)(44832011)(1076003)(26005)(2004002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2023 14:58:33.9906 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3f46315-f6b6-4c82-4369-08db797a79da X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT067.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7423 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE,WEIRD_PORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: When debugging a multi-process application where a parent spawns multiple child processes using the ROCm runtime, I see the following assertion failure: ../../gdb/amd-dbgapi-target.c:1071: internal-error: process_one_event: Assertion `runtime_state == AMD_DBGAPI_RUNTIME_STATE_UNLOADED' failed. A problem internal to GDB has been detected, further debugging may prove unreliable. ----- Backtrace ----- 0x556e9a318540 gdb_internal_backtrace_1 ../../gdb/bt-utils.c:122 0x556e9a318540 _Z22gdb_internal_backtracev ../../gdb/bt-utils.c:168 0x556e9a730224 internal_vproblem ../../gdb/utils.c:396 0x556e9a7304e0 _Z15internal_verrorPKciS0_P13__va_list_tag ../../gdb/utils.c:476 0x556e9a87aeb4 _Z18internal_error_locPKciS0_z ../../gdbsupport/errors.cc:58 0x556e9a29f446 process_one_event ../../gdb/amd-dbgapi-target.c:1071 0x556e9a29f446 process_event_queue ../../gdb/amd-dbgapi-target.c:1156 0x556e9a29faf2 _ZN17amd_dbgapi_target4waitE6ptid_tP17target_waitstatus10enum_flagsI16target_wait_flagE ../../gdb/amd-dbgapi-target.c:1262 0x556e9a6b0965 _Z11target_wait6ptid_tP17target_waitstatus10enum_flagsI16target_wait_flagE ../../gdb/target.c:2586 0x556e9a4c221f do_target_wait_1 ../../gdb/infrun.c:3876 0x556e9a4d8489 operator() ../../gdb/infrun.c:3935 0x556e9a4d8489 do_target_wait ../../gdb/infrun.c:3964 0x556e9a4d8489 _Z20fetch_inferior_eventv ../../gdb/infrun.c:4365 0x556e9a87b915 gdb_wait_for_event ../../gdbsupport/event-loop.cc:694 0x556e9a87c3a9 gdb_wait_for_event ../../gdbsupport/event-loop.cc:593 0x556e9a87c3a9 _Z16gdb_do_one_eventi ../../gdbsupport/event-loop.cc:217 0x556e9a521689 start_event_loop ../../gdb/main.c:412 0x556e9a521689 captured_command_loop ../../gdb/main.c:476 0x556e9a523c04 captured_main ../../gdb/main.c:1320 0x556e9a523c04 _Z8gdb_mainP18captured_main_args ../../gdb/main.c:1339 0x556e9a24b1bf main ../../gdb/gdb.c:32 --------------------- ../../gdb/amd-dbgapi-target.c:1071: internal-error: process_one_event: Assertion `runtime_state == AMD_DBGAPI_RUNTIME_STATE_UNLOADED' failed. A problem internal to GDB has been detected, Before diving into why this error appears, let's explore how things are expected to work in normal circumstances. When a process being debugged starts using the ROCm runtime, the following happens: - The runtime registers itself to the driver. - The driver creates a "runtime loaded" event and notifies the debugger that a new event is available by writing to a file descriptor which is registered in GDB's main event loop. - GDB core calls the callback associated with this file descriptor (dbgapi_notifier_handler). Because the amd-dbgapi-target is not pushed at this point, the handler pulls the "runtime loaded" event from the driver (this is the only event which can be available at this point) and eventually pushes the amd-dbgapi-target on the inferior's target stack. In a nutshell, this is the expected AMDGPU runtime activation process. >From there, when new events are available regarding the GPU threads, the same file descriptor is written to. The callback sees that the amd-dbgapi-target is pushed so marks the amd_dbgapi_async_event_handler. This will later cause amd_dbgapi_target::wait to be called. The wait method pulls all the available events from the driver and handles them. The wait method returns the information conveyed by the first event, the other events are cached for later calls of the wait method. Note that because we are under the wait method, we know that the amd-dbgapi-target is pushed on the inferior target stack. This implies that the runtime activation event has been seen already. As a consequence, we cannot receive another event indicating that the runtime gets activated. This is what the failing assertion checks. In the case when we have multiple inferiors however, there is a flaw in what have been described above. If one inferior (let's call it inferior 1) already has the amd-dbgapi-target pushed to its target stack and another inferior (inferior 2) activates the ROCm runtime, here is what can happen: - The driver creates the runtime activation for inferior 2 and writes to the associated file descriptor. - GDB has inferior 1 selected and calls target_wait for some reason. - This prompts amd_dbgapi_target::wait to be called. The method pulls all events from the driver, including the runtime activation event for inferior 2, leading to the insertion failure. The fix for this problem is simple. To avoid such problem, we need to make sure that amd_dbgapi_target::wait only pulls events for the current inferior from the driver. This is what this patch implements. This patch also includes a testcase which could fail before this patch. This patch has been tested on a system with multiple GPUs which had more chances to reproduce the original bug. It has also been tested on top of the downstream ROCgdb port which has more AMDGPU related tests. The testcase have been tested with `make check check-read1 check-readmore`. --- gdb/amd-dbgapi-target.c | 6 +- gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp | 111 ++++++++++++++++++ gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp | 86 ++++++++++++++ 3 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp create mode 100644 gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp diff --git a/gdb/amd-dbgapi-target.c b/gdb/amd-dbgapi-target.c index 5565cf907fa..371f0683754 100644 --- a/gdb/amd-dbgapi-target.c +++ b/gdb/amd-dbgapi-target.c @@ -1255,8 +1255,10 @@ amd_dbgapi_target::wait (ptid_t ptid, struct target_waitstatus *ws, std::tie (event_ptid, gpu_waitstatus) = consume_one_event (ptid.pid ()); if (event_ptid == minus_one_ptid) { - /* Drain the events from the amd_dbgapi and preserve the ordering. */ - process_event_queue (); + /* Drain the events for the current inferior from the amd_dbgapi and + preserve the ordering. */ + auto info = get_amd_dbgapi_inferior_info (current_inferior ()); + process_event_queue (info->process_id, AMD_DBGAPI_EVENT_KIND_NONE); std::tie (event_ptid, gpu_waitstatus) = consume_one_event (ptid.pid ()); if (event_ptid == minus_one_ptid) diff --git a/gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp b/gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp new file mode 100644 index 00000000000..828dc0cf7d4 --- /dev/null +++ b/gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp @@ -0,0 +1,111 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 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 . */ + +#include +#include +#include +#include + +#define CHECK(cmd) \ + { \ + hipError_t error = cmd; \ + if (error != hipSuccess) \ + { \ + fprintf (stderr, "error: '%s'(%d) at %s:%d\n", \ + hipGetErrorString (error), error, __FILE__, __LINE__); \ + exit (EXIT_FAILURE); \ + } \ + } + +__global__ void +kern () +{ + asm ("s_sleep 1"); +} + +/* Spawn one child process per detected GPU. */ + +static int +parent (int argc, char **argv) +{ + /* Identify how many GPUs we have, and spawn one child for each. */ + int num_devices; + CHECK (hipGetDeviceCount (&num_devices)); + + /* Break here. */ + + for (int i = 0; i < num_devices; i++) + { + char n[32] = {}; + snprintf (n, sizeof (n), "%d", i); + pid_t pid = fork (); + if (pid == -1) + { + perror ("Fork failed"); + return -1; + } + + if (pid == 0) + { + /* Exec to be fore the child to re-initialize the ROCm runtime. */ + if (execl (argv[0], argv[0], n) == -1) + { + perror ("Failed to exec"); + return -1; + } + } + } + + /* Wait for all children. */ + int ws; + pid_t ret; + do + ret = waitpid (-1, &ws, 0); + while (!(ret == -1 && errno == ECHILD)); + + /* Last break here. */ + return 0; +} + +static int +child (int argc, char **argv) +{ + int dev_number; + if (sscanf (argv[1], "%d", &dev_number) != 1) + { + fprintf (stderr, "Invalid argument \"%s\"\n", argv[1]); + return -1; + } + + CHECK (hipSetDevice (dev_number)); + kern<<<1, 1>>> (); + hipDeviceSynchronize (); + return 0; +} + +/* When called with no argument, identify how many AMDGPU devices are + available on the system and spawn one worker process per GPU. If a + command-line argument is provided, it is the index of the GPU to use. */ + +int +main (int argc, char **argv) +{ + if (argc <= 1) + return parent (argc, argv); + else + return child (argc, argv); +} diff --git a/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp b/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp new file mode 100644 index 00000000000..3e8934645e6 --- /dev/null +++ b/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp @@ -0,0 +1,86 @@ +# Copyright 2023 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 . + +# This test checks that GDB can debug multiple inferior which uses all +# the ROCm runtime. + +load_lib rocm.exp + +standard_testfile .cpp + +require allow_hipcc_tests +require hip_devices_support_debug_multi_process + +if {[build_executable "failed to prepare" $testfile $srcfile {debug hip}]} { + return +} + +proc do_test {} { + clean_restart $::binfile + gdb_test_no_output "set non-stop on" + gdb_test_no_output "set detach-on-fork off" + gdb_test_no_output "set follow-fork parent" + + with_rocm_gpu_lock { + gdb_breakpoint [gdb_get_line_number "Break here"] + gdb_breakpoint kern allow-pending + gdb_breakpoint [gdb_get_line_number "Last break here"] + + # Run intil we reach the first breakpoint where we can figure + # out how many children will be spawned. + gdb_test "run" "hit Breakpoint.*" + + set num_childs [get_integer_valueof "num_devices" 0] + set bp_to_see $num_childs + set stopped_threads [list] + + gdb_test_multiple "continue -a" "continue to gpu breakpoints" { + -re "Thread ($::decimal\.$::decimal)\[^\r\n\]* hit Breakpoint\[^\r\n\]*, kern \(\)\[^\r\n\]*\r\n" { + lappend stopped_threads $expect_out(1,string) + incr bp_to_see -1 + if {$bp_to_see != 0} { + exp_continue + } else { + pass $gdb_test_name + } + } + -re "^\[^\r\n\]*\r\n" { + exp_continue + } + } + + # Continue all the children processes until they exit. + foreach thread $stopped_threads { + set infnumber [lindex [split $thread .] 0] + gdb_test "thread $thread" "Switching to thread.*" + gdb_test_multiple "continue $thread" "" { + -re "\\\[Inferior $infnumber \[^\n\r\]* exited normally\\]\r\n$::gdb_prompt " { + pass $gdb_test_name + } + } + } + + gdb_test_multiple "" "reach breakpoint in main" { + -re "hit Breakpoint.*parent" { + pass $gdb_test_name + } + } + # Select main inferior + gdb_test "inferior 1" "Switching to inferior 1.*" + gdb_continue_to_end "" "continue -a" 1 + } +} + +do_test -- 2.34.1