From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2061.outbound.protection.outlook.com [40.107.102.61]) by sourceware.org (Postfix) with ESMTPS id 3984D3857733 for ; Wed, 10 May 2023 18:46:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3984D3857733 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=ZB5OXCCCrHQHV7Ge2u38zRGyasZcNDLVuggsMvCobDYHAZEkJJJRZfVZRQdyTpbTIDi7gASUkGX29mZJU3ZnSocfHdrRJmWe+GzZWLMLOeVnfDJLhMULKYU79BaH/ZRLL8v6Y/jchGAYWgqODN8vH+AywdnhMnmTcXCfqHjuov5IRkCxRXfH2Bn0+StrcYTt7OwmfJzPVz6amPlZlgMfIZbi08txuunDWsJcW/GjRZbh6uEQhizSxII7nfwyu9/Y17jpVkq+oewKxJYUDlANzqbmC5xbv7kyLnzSQN1dc+14dGywpHoBLDGrULUSZ4ErzCMqSGB/zCv1f5bBrD9zLw== 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=cmhsgH/HEoGl3kP7J56/WsCUYn4x2WA0WEqVAwamZpg=; b=Z8LXgxcoP2z00brCm1NIrBTfor1TC4zPw2KvXErhwDw13QjRCAHgq88vxFC2pMhkBRbuUsvKKM9yGgxlyB5+fyZmJDC9HmhR4AyIQJYReBbaXfIh3mdO3xSV7bj/u7HXLthnN3R7b40p/qEh9u8RpiguPV4be+GbdpF4WX1jsv6xnRqsHx9orJqxOiR1Ec+Pe1+nZ4zvc0wDvE0K3QaEu8CBYm4JSLLZpBhY3PvwbH+EdS8/NVHB5lv+erb7RFTv+6jChP3EdUnz5wy3zSo0eArCq6ZPwSYn80Ug51aWXkKMNJRjfwFb2OsnHXXJHxYbMNvUCWzM8zb4H5RrMwvmMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=cmhsgH/HEoGl3kP7J56/WsCUYn4x2WA0WEqVAwamZpg=; b=5t8eJHTMmpX8nQrAXfnI42EdjsbXHUbI3QEJBKI3tN/vVKyzt3CwwsT9jC2b4LfA3CKyb32FRPSRs0QcfoYl5yarHff+hMAEQOyANP3pbVyfq1kRr6NpukMyzBry0g+0XBYPg2BxNM6vdDvzpv+fmgbuXw6dQjfGniCLRfoysHo= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from DM4PR12MB5745.namprd12.prod.outlook.com (2603:10b6:8:5c::7) by DS0PR12MB7851.namprd12.prod.outlook.com (2603:10b6:8:14a::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33; Wed, 10 May 2023 18:46:40 +0000 Received: from DM4PR12MB5745.namprd12.prod.outlook.com ([fe80::639b:9e9f:4d79:7ce8]) by DM4PR12MB5745.namprd12.prod.outlook.com ([fe80::639b:9e9f:4d79:7ce8%5]) with mapi id 15.20.6363.033; Wed, 10 May 2023 18:46:40 +0000 Message-ID: Date: Wed, 10 May 2023 19:46:36 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v6 3/3] gdb/gcore: interrupt all threads to generate core in non-stop targets Content-Language: en-US To: gdb-patches@sourceware.org Cc: lsix@lancelotsix.com References: <2daa1736-57cb-3c5a-f1cd-48f5aa03d010@simark.ca> <20230322163504.560986-1-lancelot.six@amd.com> <20230322163504.560986-4-lancelot.six@amd.com> From: Lancelot SIX In-Reply-To: <20230322163504.560986-4-lancelot.six@amd.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: FR2P281CA0038.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:92::11) To DM4PR12MB5745.namprd12.prod.outlook.com (2603:10b6:8:5c::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR12MB5745:EE_|DS0PR12MB7851:EE_ X-MS-Office365-Filtering-Correlation-Id: 25f49fbd-edd8-4c0e-f52d-08db5186e478 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1lYTo+jG43fmWD2GiVcwpEfUXyo1I+MBPYHA9B9ljGZdK7uhfP1fz9MSupD9knV+pZFW+J+3RS3f0IvcF4cF31MrBM90CTNwXiv3ht7gmIZ6u7o86BN4S/+c2iYLq97CGTt+gdmX1uKg/AfogCb56a3gqaqYV8hIU7090KhdyJZ7LvMV/yy8xK+aDmIU3IlI9hDdBxOzO8ktdKXYhmf6rnWvHSlgxBtn8A9edPC8uC3pFKDqxW/SB+fEQoC7gqxr3dVEo4klHouRILdFa267aK3rWuv0lv3D74hXsA44xCcMrdZ/o0lJXcRFjMkAhVMdR+ywSeciAiH2dydG0Egh0xb2QHEU3wfRO2wTjCDz2BPPkvereygoSK1v+bpLxr7Z9Eip0C8DdFiMS5bKl3ayeqsjnl9FKVo0ijimSz+0yyr4TNAZfuhq1Igi9p5J4cTAsPHxvvcHKyNG6L0HNrFXJzcd1RnRKBvQ2Jtlg3DA+UcMdGxXxOzNFur/7KY10D1aPWWoq0hLvd9IKWk4vXQvus36qCTRODEteqHdyQRMqDz4agOnCR6sboFVtOE4J9WGvsw11+sdmPYa4KGyavXviQxH4ZcSERvs3je6nLeNqw6aSjgUG+YApB9TizWbvEvcfsJD601iXoRNqLljbtlQHA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM4PR12MB5745.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(396003)(39860400002)(376002)(366004)(136003)(346002)(451199021)(31686004)(30864003)(2906002)(66476007)(41300700001)(8936002)(478600001)(6916009)(5660300002)(8676002)(316002)(4326008)(66899021)(6666004)(66556008)(66946007)(53546011)(6506007)(6486002)(6512007)(26005)(186003)(2616005)(36756003)(83380400001)(38100700002)(31696002)(86362001)(2004002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?eDI5MjFtSyttNjFxaVBrUW9wZWNaejFkaWdaTjhTaEwwTnNnYWlrZy9KQWlW?= =?utf-8?B?VWQvWWF3NHo0dkUyL1ErN283R2lLVi9Rd3BJUHpCd09FYzFqYXRMamN0QUdV?= =?utf-8?B?aDN5MXpQT3ZmdzU4MXVzMkdlWnk2RS9hWVJXWmpPZVZoaUdBaU1FekZFaGZD?= =?utf-8?B?RWd1MWhUVUFLNUxFd2lGQUJsV1lIQXpRL2pQa1ZxSEU0N25wRlRQZ3pwd0NR?= =?utf-8?B?cTEvQ2IyckdmbUN6d1ByZjArd2lrREZGaFJyNzhXNVFKR3R2NjNqdUNtTGhQ?= =?utf-8?B?TWFYd1kxYnRFeFN6WVhob0ZPS3BSVEE0VWZRbWlZenZIc3B2NXNJaXovNldv?= =?utf-8?B?WVRhbDBhaVRoeHhvaWptSzNlZ0VjMXErSnhsMS93dk5hdmhDaWEyS1BXRW1j?= =?utf-8?B?TGJqeWl0NzU4MmNocVhzTTZRMEFjSTl6OXQyanBTY0YzM05wNHdaSHZnRjZq?= =?utf-8?B?MzJLdkJEODgzbnlGUFdkdHR3RHZzNzlFVmlZQTExL090Ym1PWVh6RFY4R2ZV?= =?utf-8?B?dU1jY0tnSUt4bW95VjVkRG9ZSEUxYnYzU09kVlM3RHdXUHNtWVhZTTQvZVlS?= =?utf-8?B?Mjl5K09CbDlXbmlhc0dQaDhlVUE0cngyeGk2Vm9aUEY0bGNSWTJNeGJKVStH?= =?utf-8?B?NmdMYndNQURBMHFvRDhxVE5rbHJnRWNlOFZTVU5hS254UFZGcWI5Zk9CeHJW?= =?utf-8?B?M1dvMmo1UTZRMzJiMkNQa3hpdmRYLytSRnQyOHFVNVdUUS9DRU5YczE2KzZ2?= =?utf-8?B?dmR3YlJuRFZMd2lPTWdOSldZTG5GaFh1bGJpWUJIK0UwdSs4WnlaS1RZMExG?= =?utf-8?B?aDdYZ01aT0pMdXNGcHhibXdWQ055OEhZV0Z0VHVzZnZjbUN1NExWMWcxTFNX?= =?utf-8?B?ZkROdzkvUkRpdjZUN0ZnQWNZNjVBT1podFNiZGJTSks1TWhEazBDbEZBS3d0?= =?utf-8?B?T0Rybi9MNi81SDFHUDhZWlF1Smw4eXN4VTFoOC90UkExUVlodW9YUHpONkVK?= =?utf-8?B?ZzVTVnNCSUt6cndFamw5ajZPK0daWml4NFlLTnhvbnBNdzdYK0p5c3J3MHFX?= =?utf-8?B?d0tzQ2RESFVIRWZDREZacHRMbmt4N0NDUUhsUHdoYUxPSHJFcnNQYU5IVW1X?= =?utf-8?B?czRFOUlhcVFFMnZ0ZHdZcHIxR2s1OHltQlhWMFR2eWhHZnlXM2Vpd3RWS05W?= =?utf-8?B?aklQQmFHNytNMWh1RStabTRqQmJUVHd5V3Z0T1VwdmRWdnVkNEp0Ry9ZWDQy?= =?utf-8?B?MVpwUW1kalRINlJsVzhsRkVVa01kejJIaFozU2RzcUxlQlNSR0FpeHVtRFBH?= =?utf-8?B?UnRNcGovczlSSGVKdFJ1NVEvL2U5ZyszMDI1L0llUjdoSndEaTdueS9xYkhN?= =?utf-8?B?MjViSnNYbC9WZGNqTWVOekxITWZEWGF4U0N4OXgvSlRmbFYrcjNRdktsS1FH?= =?utf-8?B?b2UvUkVzcHBqbjZ5VTd1cWQ1QlFOb0VKbDZDT1ZhdC9MNkpoUVJ3UVZ2bEJn?= =?utf-8?B?ejZCU00zMndxMU04eFBUdnJTT1pmRWNQcTVWK0NUTDI4VmVUOEhDVWM0OENR?= =?utf-8?B?NHo1WWtJTFJGbzVCVjB0alV2NnVoR2hOREVmaTlJT00yNlAzcm5vdnU0dk5n?= =?utf-8?B?WTFsL0tlUUFhKzBDOHpsd1FId2tYSmwwTmlOVHNocmRVRkJISnA2NVNmSGNI?= =?utf-8?B?RW9laCtDY2k4T1Izbm5nY2JsdUxYdlM4c1ZTZ0xEQVpwdTVrMlN5c05lenVG?= =?utf-8?B?elp2RDBRVTFBcDRkWEZPeVZuN0FRZ2E1clZjVVRCblpXeXRKekZsVkUwWWow?= =?utf-8?B?Zmp0NVVvTzhkd2ZwMnFsRExpMjJNY2hSeHFVRDViSGNRUVk5aFhvMFQzNHFZ?= =?utf-8?B?Zm0xZzJFeFFscFdybGdDWnVoVlZxcDBJbVpXbUNmK09KRWpVaXY4aFkySEdR?= =?utf-8?B?dllVNjhMZ0RPS0FrNVFVVEdQRThBVXkxMGpFTk9LcmZTUUZlSjQrdGJaQ2gw?= =?utf-8?B?L0dHekF0TEhVZ2ZIVXladjBPbWNNVmxFb1F4N010VXFWSnI0b2FlRzR4TC9j?= =?utf-8?B?TER1UzRuRWdZc29pSHplS3lnSlRjbUNNUjhlUXdVeXRvNlQ1WVBCQkxQZkNw?= =?utf-8?Q?Pk4vCXZyzeHnydKPNjsOS7eLv?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 25f49fbd-edd8-4c0e-f52d-08db5186e478 X-MS-Exchange-CrossTenant-AuthSource: DM4PR12MB5745.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2023 18:46:40.6575 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: rYoDmCAGFngizD3w9fqAVgC4SKOYpMbv0fy0cRC8MlhaEhk5Ef4UqU+0thHTiCfrPkiydIxMj4X4WoVNWySIUg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7851 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: Hi, Kindly pinging on this patch. Best, Lancelot. On 22/03/2023 16:35, Lancelot SIX wrote: > If the user tries to generate a core dump (using the gcore command) > while some threads are running, a non-helpful error message is shown. > > Lets consider the following session as an example (debugging the test > program included in this patch): > > (gdb) set non-stop on > (gdb) b 37 > (gdb) r > Thread 1 "gcore-nonstop" hit Breakpoint 1, main () at gcore-nonstop.c:39 > (gdb) info thread > Id Target Id Frame > * 1 Thread 0x7ffff7d7a740 (LWP 431838) "gcore-nonstop" main () at gcore-nonstop.c:39 > 2 Thread 0x7ffff7d79640 (LWP 431841) "gcore-nonstop" (running) > (gdb) gcore > Couldn't get registers: No such process. > > The reported error ("No such process") does not help the user understand > what happens. This is due to the fact that we cannot access the > registers of a running thread. Even if we ignore this error, generating > a core dump while any thread might update memory would most likely > result in a core file with an inconsistent view of the process' memory. > > This patch proposes to change this. > > For non-stop targets change the gcore command so it first stops all > running threads (from the current inferior) before generating the > corefile, and then resumes them in their previous state. To achieve > this, this patch exposes the restart_threads function in infrun.h (used > to be local to infrun.c). We also allow the first parameter > (event_thread) to be nullptr as it is possible that the gcore command is > called while all threads are running, in which case we want all threads > to be restarted at the end of the procedure. > > Similar change for all-stop targets turns out to be less straight > forward. For this reason, when dealing with all-stop targets, this > patch proposes to error-out early with a meaningful message. > > Tested on x86_64 on native GDB and the native-extended-gdbserver board. > --- > gdb/NEWS | 8 +++ > gdb/doc/gdb.texinfo | 5 ++ > gdb/gcore.c | 20 ++++++ > gdb/infrun.c | 16 ++--- > gdb/infrun.h | 9 +++ > gdb/testsuite/gdb.base/gcore-nonstop.c | 44 +++++++++++++ > gdb/testsuite/gdb.base/gcore-nonstop.exp | 79 ++++++++++++++++++++++++ > 7 files changed, 170 insertions(+), 11 deletions(-) > create mode 100644 gdb/testsuite/gdb.base/gcore-nonstop.c > create mode 100644 gdb/testsuite/gdb.base/gcore-nonstop.exp > > diff --git a/gdb/NEWS b/gdb/NEWS > index cc262f1f8a6..65ac722bd2e 100644 > --- a/gdb/NEWS > +++ b/gdb/NEWS > @@ -91,6 +91,14 @@ show always-read-ctf > $2 = 1 > (gdb) break func if $_shell("some command") == 0 > > +* Changed commands > + > +gcore > +generate-core-file > + For non-stop targets, GDB ensures that all threads are stopped before > + generating a core dump. At the end of the command, threads are restored > + to their previous state. > + > * MI changes > > ** mi now reports 'no-history' as a stop reason when hitting the end of the > diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo > index 6c811b8be2e..574684d9e28 100644 > --- a/gdb/doc/gdb.texinfo > +++ b/gdb/doc/gdb.texinfo > @@ -13763,6 +13763,11 @@ Produce a core dump of the inferior process. The optional argument > specified, the file name defaults to @file{core.@var{pid}}, where > @var{pid} is the inferior process ID. > > +If the current inferior's target is non-stop, @value{GDBN} ensures that > +all the inferior's threads are stopped while generating the core dump. > +@value{GDBN} stops every running threads of the inferior before > +generating the core dump and resumes them when the command is done. > + > Note that this command is implemented only for some systems (as of > this writing, @sc{gnu}/Linux, FreeBSD, Solaris, and S390). > > diff --git a/gdb/gcore.c b/gdb/gcore.c > index 973abadb013..217976ac4e0 100644 > --- a/gdb/gcore.c > +++ b/gdb/gcore.c > @@ -34,6 +34,7 @@ > #include "regset.h" > #include "gdb_bfd.h" > #include "readline/tilde.h" > +#include "infrun.h" > #include > #include "gdbsupport/gdb_unlinker.h" > #include "gdbsupport/byte-vector.h" > @@ -131,6 +132,20 @@ gcore_command (const char *args, int from_tty) > if (!target_has_execution ()) > noprocess (); > > + scoped_restore_current_thread restore_current_thread; > + scoped_disable_commit_resumed disable_commit_resume ("generating coredump"); > + struct inferior *inf = current_inferior (); > + scoped_finish_thread_state finish_state (inf->process_target (), > + ptid_t (inf->pid)); > + > + if (target_is_non_stop_p (inf)) > + stop_all_threads ("generating coredump", inf); > + else > + for (thread_info *thread : inf->non_exited_threads ()) > + if (thread->executing ()) > + error (_("At least one thread is still running, cannot generate a " > + "core dump.")); > + > if (args && *args) > corefilename.reset (tilde_expand (args)); > else > @@ -161,6 +176,11 @@ gcore_command (const char *args, int from_tty) > } > > gdb_printf ("Saved corefile %s\n", corefilename.get ()); > + > + if (exists_non_stop_target ()) > + restart_threads (nullptr, inf); > + > + disable_commit_resume.reset_and_commit (); > } > > static enum bfd_architecture > diff --git a/gdb/infrun.c b/gdb/infrun.c > index a509e0d5bef..7364bfa623c 100644 > --- a/gdb/infrun.c > +++ b/gdb/infrun.c > @@ -97,9 +97,6 @@ static void resume (gdb_signal sig); > > static void wait_for_inferior (inferior *inf); > > -static void restart_threads (struct thread_info *event_thread, > - inferior *inf = nullptr); > - > static bool start_step_over (void); > > static bool step_over_info_valid_p (void); > @@ -6008,18 +6005,15 @@ handle_inferior_event (struct execution_control_state *ecs) > } > } > > -/* Restart threads back to what they were trying to do back when we > - paused them (because of an in-line step-over or vfork, for example). > - The EVENT_THREAD thread is ignored (not restarted). > - > - If INF is non-nullptr, only resume threads from INF. */ > +/* See infrun.h. */ > > -static void > +void > restart_threads (struct thread_info *event_thread, inferior *inf) > { > INFRUN_SCOPED_DEBUG_START_END ("event_thread=%s, inf=%d", > - event_thread->ptid.to_string ().c_str (), > - inf != nullptr ? inf->num : -1); > + (event_thread != nullptr > + ? event_thread->ptid.to_string ().c_str () > + : "None"), inf != nullptr ? inf->num : -1); > > gdb_assert (!step_over_info_valid_p ()); > > diff --git a/gdb/infrun.h b/gdb/infrun.h > index 5219063586d..4b2a04e503b 100644 > --- a/gdb/infrun.h > +++ b/gdb/infrun.h > @@ -182,6 +182,15 @@ extern void nullify_last_target_wait_ptid (); > all threads of all inferiors. */ > extern void stop_all_threads (const char *reason, inferior *inf = nullptr); > > +/* Restart threads back to what they were trying to do back when we > + paused them (because of an in-line step-over or vfork, for example). > + The EVENT_THREAD thread, if non-nullptr, is ignored (not restarted). > + > + If INF is non-nullptr, only resume threads from INF. */ > + > +extern void restart_threads (struct thread_info *event_thread, > + inferior *inf = nullptr); > + > extern void prepare_for_detach (void); > > extern void fetch_inferior_event (); > diff --git a/gdb/testsuite/gdb.base/gcore-nonstop.c b/gdb/testsuite/gdb.base/gcore-nonstop.c > new file mode 100644 > index 00000000000..a96c011ad14 > --- /dev/null > +++ b/gdb/testsuite/gdb.base/gcore-nonstop.c > @@ -0,0 +1,44 @@ > +/* This testcase is part of GDB, the GNU debugger. > + > + Copyright 2022-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 > + > +static pthread_barrier_t barrier; > + > +static void * > +worker_func (void *ignored) > +{ > + pthread_barrier_wait (&barrier); > + return NULL; > +} > + > +int > +main (void) > +{ > + pthread_t worker_thread; > + pthread_barrier_init (&barrier, NULL, 2); > + > + pthread_create (&worker_thread, NULL, worker_func, NULL); > + > + /* Break here. */ > + > + pthread_barrier_wait (&barrier); > + pthread_join (worker_thread, NULL); > + pthread_barrier_destroy (&barrier); > + > + return 0; > +} > diff --git a/gdb/testsuite/gdb.base/gcore-nonstop.exp b/gdb/testsuite/gdb.base/gcore-nonstop.exp > new file mode 100644 > index 00000000000..172dd760e73 > --- /dev/null > +++ b/gdb/testsuite/gdb.base/gcore-nonstop.exp > @@ -0,0 +1,79 @@ > +# Copyright 2022-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 . > + > +# Check that in non-stop mode, if some threads are running when the user > +# launches the "gcore" command, GDB suspends all threads, generates the core > +# file and resumes threads which where running before the "gcore" command > +# got issued. If running threads were not stopped, GDB would report errors > +# when trying to capture the thread's state. > + > +standard_testfile > + > +if { [build_executable "failed to build" \ > + ${testfile} ${srcfile} {pthreads debug}] } { > + return > +} > + > +save_vars { GDBFLAGS } { > + append GDBFLAGS " -ex \"set non-stop on\"" > + clean_restart ${binfile} > +} > + > +set lineno [gdb_get_line_number "Break here"] > +if { ![runto $lineno] } { > + return > +} > + > +# We should be stopped in thread 1 while thread 2 is running. > +gdb_test_sequence "info threads" "info threads" { > + {Id\s+Target Id\s+Frame} > + {\*\s+1[^\n]*\n} > + {\s+2\s+[^\n]*\(running\)[^\n]*\n} > +} > + > +set th1_pc "" > +gdb_test_multiple "p/x \$pc" "fetch thread 1 PC" { > + -wrap -re "$::decimal = ($::hex)" { > + set th1_pc $expect_out(1,string) > + pass $gdb_test_name > + } > +} > + > +set corefile [standard_output_file "corefile"] > +if {![gdb_gcore_cmd $corefile "generate corefile"]} { > + # gdb_gcore_cmd issues a "UNSUPPORTED". > + return > +} > + > +# After the core file is generated, thread 2 should be back running > +# and thread 1 should still be selected. > +gdb_test_sequence "info threads" "correct thread selection after gcore" { > + {Id\s+Target Id\s+Frame} > + {\*\s+1[^\n]*\n} > + {\s+2\s+[^\n]*\(running\)[^\n]*\n} > +} > + > +# Thread 1 is at the same PC it was before calling the gcore command. > +gdb_test "p/x \$pc" "\\\$$::decimal = $th1_pc" "thread 1 unchanged" > + > +clean_restart $binfile > +gdb_test "core-file $corefile" "Core was generated by.*" "load corefile" > + > +# The core file has the 2 threads. > +gdb_test_sequence "info threads" "threads in corefile" { > + {Id\s+Target Id\s+Frame} > + {\s+1\s+Thread[^\n]*\n} > + {\s+2\s+Thread[^\n]*\n} > +}