From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OcxrMnOHOGFhVAAAWB0awg (envelope-from ) for ; Wed, 08 Sep 2021 05:50:43 -0400 Received: by simark.ca (Postfix, from userid 112) id BC7761EE22; Wed, 8 Sep 2021 05:50:43 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id EDA911EDF5 for ; Wed, 8 Sep 2021 05:50:41 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D71438618DF for ; Wed, 8 Sep 2021 09:50:41 +0000 (GMT) Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 20C503858415 for ; Wed, 8 Sep 2021 09:50:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 20C503858415 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x333.google.com with SMTP id m25-20020a7bcb99000000b002e751bcb5dbso1037427wmi.5 for ; Wed, 08 Sep 2021 02:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=19x+zSvoteijhx4yBglUWElJvggLQHcJbcahGfsP0Xk=; b=GLowYRiYNx/BLpAL2uZ5a1qraCnDrwIRzBQa4HzMxKyF4kxiLJJKT+KSrb7y9bllOJ GKNE7UP4SFqW3rTjDR3XTihN7AGjDw0ZVKNTauEu16EF0oZhkgoruat1Hg8flP54Q0fQ GcbjDjbtVzhV77nxDYAtgBNx4MbrzXbId87lGU3lwfLUGYfQm9woNhOIudeLHNlfaFka XUGULY3CisibzW+RyAgw2LjTZnN6Z8ia+xGZ+fQYjyld0HPvSO5HJQxgq9VaAOgFq5Us UfBDx74pSwKfVnUjUG24YuMdtlZaw5srzbDMOv3PGPERPdIMK2M5dKzkaQGL+65WujJ/ c4Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=19x+zSvoteijhx4yBglUWElJvggLQHcJbcahGfsP0Xk=; b=AC2titmbAHjB+SLv/Ygj8zB0olSH6RBBZhv0aLdk52OL6ymvILTVBSbQ4l/fXfmF2g tnv5xNt2IPjFg8DKJtf4e10bIJAoTihF3hHcrksiY9VHkGRgbHUthLGvSa0ZQJ1nUmLm DFXU2RD1EEx6aZg+9GBd7tt8GdjWUCwdvuL52yk5/wm1E7a9d7wSFLhpxipv5qf2kofi sHCJ68h5pgJhFyC0qOH8NwOB9kVumqpURUOurSpalMSWE7QR45hHo/l5uTp1CvYfs37M 0NT6PtBGdrYCASg0eJ+apGwWSezBVDq6ufhwnGzAlvjaIxLEZNe2noT4AGm53JC9K7la dw5w== X-Gm-Message-State: AOAM5326Y1Mhb3CWHWVED+yrU7d10YYj0tiQghnNz2J+2nZ7jpZLG/in eBs7/dUsJTbjSgdpHFjWcUpV1bSMemuY0Q== X-Google-Smtp-Source: ABdhPJw96CDd2B0vAioSy9cJv5oVwabb3+K4Z7o+atWkPykb3PAQSQz10JDXSeuWNvjZt5Y4jjYLfQ== X-Received: by 2002:a05:600c:4048:: with SMTP id j8mr2575116wmm.173.1631094629168; Wed, 08 Sep 2021 02:50:29 -0700 (PDT) Received: from localhost (host109-151-25-83.range109-151.btcentralplus.com. [109.151.25.83]) by smtp.gmail.com with ESMTPSA id q4sm1687518wra.43.2021.09.08.02.50.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Sep 2021 02:50:28 -0700 (PDT) Date: Wed, 8 Sep 2021 10:50:27 +0100 From: Andrew Burgess To: Simon Marchi Subject: Re: [PATCH 2/3] gdb: make thread_suspend_state::stop_pc optional Message-ID: <20210908095027.GV2581@embecosm.com> References: <8c0e958f-e5a6-8ed4-7326-99153e7c5faf@polymtl.ca> <20210907132103.GQ2581@embecosm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: Linux/5.8.18-100.fc31.x86_64 (x86_64) X-Uptime: 10:50:07 up 21 days, 22:46, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gdb-patches@sourceware.org Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" * Simon Marchi [2021-09-07 10:10:07 -0400]: > On 2021-09-07 9:21 a.m., Andrew Burgess wrote: > > * Simon Marchi [2021-09-01 10:23:32 -0400]: > > > >> > >> > >> On 2021-08-30 4:03 p.m., Andrew Burgess wrote: > >>> Currently the stop_pc field of thread_suspect_state is a CORE_ADDR and > >>> when we want to indicate that there is no stop_pc available we set > >>> this field back to a special value. > >>> > >>> There are actually two special values used, in post_create_inferior > >>> the stop_pc is set to 0. This is a little unfortunate, there are > >>> plenty of embedded targets where 0 is a valid pc address. The more > >>> common special value for stop_pc was set in > >>> thread_info::set_executing, where the value (~(CORE_ADDR) 0) was used. > >>> > >>> This commit changes things so that the stop_pc is instead a > >>> gdb::optional. We can now explicitly reset the field to an > >>> uninitialised state, we also have (when compiling with _GLIBCXX_DEBUG > >>> defined) asserts that we don't read the stop_pc when its in an > >>> uninitialised state (see gdbsupport/gdb_optional.h). > >> > >> Thanks, I think it's a good idea. > >> > >>> One situation where a thread will not have a stop_pc value is when the > >>> thread is stopped as a consequence of GDB being in all stop mode, and > >>> some other thread stopped at an interesting event. When GDB brings > >>> all the other threads to a stop those other threads will not have a > >>> stop_pc set (thus avoiding an unnecessary read of $pc). > >>> > >>> Previously, when GDB passed through handle_one (in infrun.c) the > >>> threads executing flag was set to false and the stop_pc field was left > >>> unchanged, i.e. it would (previous) have been left as ~0. > >>> > >>> Now, handle_one leaves the stop_pc with no value. > >>> > >>> This caused a problem when we later try to set these threads running > >>> again, in proceed() we compare the current pc with the cached > >>> stop_pc. If the thread was stopped in via handle_one then the stop_pc > >>> would have been left as ~0, and the compare (in proceed) > >>> would (likely) fail. Now however, this compare tries to read the > >>> stop_pc when it has no value, this would trigger an assert. > >>> > >>> To resolve this I've added thread_info::stop_pc_p() which returns true > >>> if the thread has a cached stop_pc. We should only ever call > >>> thread_info::stop_pc() if we know that there is a cached stop_pc. > >> > >> We could also make stop_pc return gdb::optional. I think it > >> would be slightly better, since anybody calling stop_pc would see that > >> it returns an optional and be forced to consider that. Otherwise, one > >> could call stop_pc and not know that stop_pc_p exists. But otherwise > >> it's the same. > > > > I did consider that initially, but most of the places where > > thread_info::stop_pc is called the value is being immediately passed > > through to some other function, here's an example pulled randomly from > > infrun.c: > > > > ecs->event_thread->control.stop_bpstat > > = bpstat_stop_status (get_current_regcache ()->aspace (), > > ecs->event_thread->stop_pc (), > > ecs->event_thread, &ecs->ws); > > > > if we are returned a gdb::optional<> then we might change the code to > > do this: > > > > ecs->event_thread->control.stop_bpstat > > = bpstat_stop_status (get_current_regcache ()->aspace (), > > *ecs->event_thread->stop_pc (), > > ecs->event_thread, &ecs->ws); > > > > Or maybe, like this: > > > > auto stop_pc = ecs->event_thread->stop_pc (); > > gdb_assert (stop_pc.has_value ()); > > ecs->event_thread->control.stop_bpstat > > = bpstat_stop_status (get_current_regcache ()->aspace (), > > *stop_pc, > > ecs->event_thread, &ecs->ws); > > > > In the first case, it doesn't feel like we've gained much over my > > patch, where thead_info::stop_pc() accesses the value for us. > > Further, once we've normalised the pattern of accessing the stop_pc as > > `*ecs->event_thread->stop_pc ()`, I worry people still wouldn't > > actually consider whether the stop_pc value was valid or not, they'd > > just duplicate the existing code. > > > > The second case seems excessively verbose, so much so, that you might > > even be tempted to write a wrapper, say thread_info::stop_pc_value(), > > which kind lands us back on my original patch... > > > > Initially, I'd relied on the asserts within gdb::optional to ensure > > that we didn't access the stop_pc when it had no value, but these > > asserts are only present when compiling with _GLIBCXX_DEBUG defined - > > I do this, but it's certainly not going to be standard in a release > > build of GDB. So, I wonder if this would be a good change: > > > > /* Return this thread's stop PC. This should only be called when it is > > known that stop_pc has a value. If this function is being used in a > > situation where a thread may not have had a stop_pc assigned, then > > stop_pc_p() can be used to check if the stop_pc is defined. */ > > > > CORE_ADDR stop_pc () const > > { > > gdb_assert (m_suspend.stop_pc.has_value ()); > > return *m_suspend.stop_pc; > > } > > That one looks good to me, stating preconditions in the documentation > and enforcing them using assertions. That's similar to how I designed > other things, like dynamic_prop. > > Simon Thanks, I pushed this patch with that change in place. Andrew