From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7DF1C3857C69 for ; Thu, 30 Nov 2023 16:30:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7DF1C3857C69 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7DF1C3857C69 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701361822; cv=none; b=iLYDM5eMfBDbQN63BIMMa2QzgOPnFdA4cQWeuzSUOVbKiJED0fRfEu0Z0cjSAeGSv1NFMM8U8bm6b2EqlN9eQVqMDco/yD6GEsH7f83DxdHJhNkCPGeVqXAUjgNdAOcMhh/MvVFaFxbtJY5szAAvMwjkJiXABrQrzIe9ah3yi9g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701361822; c=relaxed/simple; bh=45vSPCIyP+5j9Mpg8i31jSQLSOjlcG5Df02y7RNekLM=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=PHYaSWUiXq975WP6e4U8MrFvvgfnl/remOMn6KL02wLJZPTKMOwm8utqGQgDaYUMheIreazSbIUsn1zH+RA+Y4ETeFgyQ63gBSZF5hu1JXUQKJt1SYTP+7bv3KpFncN/gheWnfq1P4op/bmjan5toh/9JaWG3PFAKLtpOShPZYw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701361819; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EoIPFwVdB3TFjLY5+NX5rxUGp8zvsSxhAgxV/g6FG/c=; b=DtW8MokqUD619J/uKOwag/R+NMVOZD5Xf+aRer8zQ5U6EE2DvYMJ27buRntfFspZBXAOdv 9rSJRi2a6kNuGOI+qYOcLCbF9jrdm3D9QPO2C0wikiadhcbpLhOR6j8z2M7e2pPhiKLQRs aF9O74D59ykm+kjhDYvuyO2s2CW2C40= Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-568-bamW7B_PPV2cSog13A8u9Q-1; Thu, 30 Nov 2023 11:30:17 -0500 X-MC-Unique: bamW7B_PPV2cSog13A8u9Q-1 Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-5bddc607b45so1075796a12.0 for ; Thu, 30 Nov 2023 08:30:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701361816; x=1701966616; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EoIPFwVdB3TFjLY5+NX5rxUGp8zvsSxhAgxV/g6FG/c=; b=ejxs0ezCZYtdjjJj2+0IL8p+2a01gsuKSaZikpV1aINe+nokGL5l0KrSLCKAddBx+1 0U+AK8JJW+QWRtuspWelXGtZCfPXH1L4NFuM3yVubuVlY3m81usgTQoGVJJlzLCpv8rC uas30ORLipCjZe3kpTquWDC+YiP1iOwbAZ8rm7TA8mOGizuH8G3n5eh1pG6UAjJT9dGl c6aQdyYs6ZNyLNxJ4fW9LIWct2RngK51UZfVWuctLAUZIuAbhsBuJxO5WMGyAyQhhtc2 kRstiAIaVr7rSmVcUn7J90V6MAOcNgDhoTGxdl0F0j6ZZg9QqdUoQ9QLGdIk9hNaCpfx YIVQ== X-Gm-Message-State: AOJu0YxqFFBkV9t0oOnaUnLaiza6FfsNINV8HJpKFBoEPA/OiMU/VQtB M8G09IWQKwc/3ksK+iDf6pewhaGJjGJ7+ex+bB6nduJteFGUx/47j134k6in9eLQGLBKDKDO6ZD z5u0rHVM2ICJZGDlrW8X5xmhUsYzNDZQp85nm11Pt7i+ElSM= X-Received: by 2002:a17:90b:4b0f:b0:285:3444:94d2 with SMTP id lx15-20020a17090b4b0f00b00285344494d2mr26091028pjb.9.1701361816325; Thu, 30 Nov 2023 08:30:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IE2h3Z/0OvYTpXpoaVYm5BJOmQuBxIycihPRpyJmzsW0B0MZ6sCM8E4CuBQjW1+Tv6AIDjvu4d1M0WnUli9MuM= X-Received: by 2002:a17:90b:4b0f:b0:285:3444:94d2 with SMTP id lx15-20020a17090b4b0f00b00285344494d2mr26091002pjb.9.1701361815930; Thu, 30 Nov 2023 08:30:15 -0800 (PST) MIME-Version: 1.0 References: <20231028002008.1105723-1-amerey@redhat.com> <20231028002008.1105723-3-amerey@redhat.com> In-Reply-To: From: Aaron Merey Date: Thu, 30 Nov 2023 11:30:05 -0500 Message-ID: Subject: [PING*3][PATCH 2/4 v2] gdb/progspace: Add reverse safe iterator and template for unwrapping iterator To: gdb-patches@sourceware.org Cc: Andrew Burgess X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,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: Ping Thanks, Aaron On Mon, Nov 20, 2023 at 1:39=E2=80=AFPM Aaron Merey wro= te: > > Ping > > Thanks, > Aaron > > On Sun, Nov 12, 2023 at 3:20=E2=80=AFPM Aaron Merey w= rote: > > > > Ping > > > > Thanks, > > Aaron > > > > On Fri, Oct 27, 2023 at 8:20=E2=80=AFPM Aaron Merey = wrote: > > > > > > v1: https://sourceware.org/pipermail/gdb-patches/2023-June/199984.htm= l > > > > > > v2 removes unwrapping_reverse_objfile_iterator and adds > > > basic_safe_reverse_range and basic_safe_reverse_iterator. > > > > > > Commit message: > > > > > > This patch changes progspace objfile_list insertion so that separate > > > debug objfiles are placed into the list after the parent objfile, > > > instead of before. Additionally qf_require_partial_symbols now retur= ns > > > a safe_range. > > > > > > These changes are intended to prepare gdb for on-demand debuginfo > > > downloading and the downloading of .gdb_index sections. > > > > > > With on-demand downloading enabled, gdb might need to delete a > > > .gdb_index quick_symbol_functions from a parent objfile while looping > > > the objfile's list of quick_symbol_functions becasue the separate > > > debug objfile has just been downloaded. The use of a safe_range > > > prevents this removal from causing iterator invalidation. > > > > > > gdb might also download a debuginfo file during symtab expansion. > > > In this case an objfile will be added to the current progspace's > > > objfiles_list during iteration over the list (for example, in > > > iterate_over_symtabs). We want these loops to also iterate over > > > newly downloaded objfiles. So objfiles need to be inserted into > > > objfiles_list after their parent since it is during the search of > > > the parent objfile for some symbol or filename that the separate > > > debug objfile might be downloaded. > > > > > > To facilitate the safe deletion of objfiles, this patch also adds > > > basic_safe_reverse_range and basic_safe_reverse_iterator. This allow= s > > > objfiles to be removed from the objfiles_list in a loop without itera= tor > > > invalidation. > > > > > > If a forward safe iterator were to be used, the deletion of an > > > objfile could invalidate the safe iterator's reference to the next > > > objfile in the objfiles_list. This can happen when the deletion > > > of an objfile causes the deletion of a separate debug objfile that > > > happens to the be next element in the objfiles_list. > > > > > > The standard reverse iterator is not suitable for safe objfile deleti= on. > > > In order to safely delete the first objfile in the objfiles_list, the > > > standard reverse iterator's underlying begin iterator would have to b= e > > > decremented, resulting in undefined behavior. > > > > > > A small change was also made to a testcase in py-objfile.exp to > > > account for the new placement of separate debug objfiles in > > > objfiles_list. > > > --- > > > gdb/jit.c | 7 +- > > > gdb/objfiles.c | 8 +- > > > gdb/objfiles.h | 8 +- > > > gdb/progspace.c | 19 ++++- > > > gdb/progspace.h | 31 ++++--- > > > gdb/testsuite/gdb.python/py-objfile.exp | 2 +- > > > gdbsupport/safe-iterator.h | 106 ++++++++++++++++++++++= ++ > > > 7 files changed, 154 insertions(+), 27 deletions(-) > > > > > > diff --git a/gdb/jit.c b/gdb/jit.c > > > index 9e8325ab803..a39fdc5a96d 100644 > > > --- a/gdb/jit.c > > > +++ b/gdb/jit.c > > > @@ -1240,11 +1240,10 @@ jit_breakpoint_re_set (void) > > > static void > > > jit_inferior_exit_hook (struct inferior *inf) > > > { > > > - for (objfile *objf : current_program_space->objfiles_safe ()) > > > + current_program_space->unlink_objfiles_if ([&] (const objfile *obj= f) > > > { > > > - if (objf->jited_data !=3D nullptr && objf->jited_data->addr != =3D 0) > > > - objf->unlink (); > > > - } > > > + return (objf->jited_data !=3D nullptr) && (objf->jited_data->a= ddr !=3D 0); > > > + }); > > > } > > > > > > void > > > diff --git a/gdb/objfiles.c b/gdb/objfiles.c > > > index 8f085b1bb7c..9822c179962 100644 > > > --- a/gdb/objfiles.c > > > +++ b/gdb/objfiles.c > > > @@ -793,14 +793,12 @@ have_full_symbols (void) > > > void > > > objfile_purge_solibs (void) > > > { > > > - for (objfile *objf : current_program_space->objfiles_safe ()) > > > + current_program_space->unlink_objfiles_if ([&] (const objfile *obj= f) > > > { > > > /* We assume that the solib package has been purged already, o= r will > > > be soon. */ > > > - > > > - if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SH= ARED)) > > > - objf->unlink (); > > > - } > > > + return !(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF= _SHARED); > > > + }); > > > } > > > > > > > > > diff --git a/gdb/objfiles.h b/gdb/objfiles.h > > > index 4b8aa9bfcec..c20b63ceadf 100644 > > > --- a/gdb/objfiles.h > > > +++ b/gdb/objfiles.h > > > @@ -698,13 +698,17 @@ struct objfile > > > > > > private: > > > > > > + using qf_list =3D std::forward_list; > > > + using qf_range =3D iterator_range; > > > + using qf_safe_range =3D basic_safe_range; > > > + > > > /* Ensure that partial symbols have been read and return the "quic= k" (aka > > > partial) symbol functions for this symbol reader. */ > > > - const std::forward_list & > > > + qf_safe_range > > > qf_require_partial_symbols () > > > { > > > this->require_partial_symbols (true); > > > - return qf; > > > + return qf_safe_range (qf_range (qf.begin (), qf.end ())); > > > } > > > > > > public: > > > diff --git a/gdb/progspace.c b/gdb/progspace.c > > > index 839707e9d71..c0fca1dace7 100644 > > > --- a/gdb/progspace.c > > > +++ b/gdb/progspace.c > > > @@ -143,19 +143,19 @@ program_space::free_all_objfiles () > > > > > > void > > > program_space::add_objfile (std::unique_ptr &&objfile, > > > - struct objfile *before) > > > + struct objfile *after) > > > { > > > - if (before =3D=3D nullptr) > > > + if (after =3D=3D nullptr) > > > objfiles_list.push_back (std::move (objfile)); > > > else > > > { > > > auto iter =3D std::find_if (objfiles_list.begin (), objfiles_l= ist.end (), > > > [=3D] (const std::unique_ptr<::objfil= e> &objf) > > > { > > > - return objf.get () =3D=3D before; > > > + return objf.get () =3D=3D after; > > > }); > > > gdb_assert (iter !=3D objfiles_list.end ()); > > > - objfiles_list.insert (iter, std::move (objfile)); > > > + objfiles_list.insert (++iter, std::move (objfile)); > > > } > > > } > > > > > > @@ -184,6 +184,17 @@ program_space::remove_objfile (struct objfile *o= bjfile) > > > > > > /* See progspace.h. */ > > > > > > +void > > > +program_space::unlink_objfiles_if > > > + (gdb::function_view predicate) > > > +{ > > > + for (auto &it : objfiles_safe ()) > > > + if (predicate (it.get ())) > > > + it->unlink (); > > > +} > > > + > > > +/* See progspace.h. */ > > > + > > > struct objfile * > > > program_space::objfile_for_address (CORE_ADDR address) > > > { > > > diff --git a/gdb/progspace.h b/gdb/progspace.h > > > index a22e427400e..17bb1710ccf 100644 > > > --- a/gdb/progspace.h > > > +++ b/gdb/progspace.h > > > @@ -214,28 +214,32 @@ struct program_space > > > unwrapping_objfile_iterator (objfiles_list.end ())); > > > } > > > > > > - using objfiles_safe_range =3D basic_safe_range; > > > + using objfiles_safe_range =3D iterator_range; > > > + using objfiles_safe_reverse_range > > > + =3D basic_safe_reverse_range; > > > > > > /* An iterable object that can be used to iterate over all objfile= s. > > > The basic use is in a foreach, like: > > > > > > for (objfile *objf : pspace->objfiles_safe ()) { ... } > > > > > > - This variant uses a basic_safe_iterator so that objfiles can be > > > - deleted during iteration. */ > > > - objfiles_safe_range objfiles_safe () > > > + This variant uses a basic_safe_reverse_iterator so that objfile= s > > > + can be deleted during iteration. > > > + > > > + The use of a reverse iterator helps ensure that separate debug > > > + objfiles are deleted before their parent objfile. This prevent= s > > > + iterator invalidation due to the deletion of a parent objfile. = */ > > > + objfiles_safe_reverse_range objfiles_safe () > > > { > > > - return objfiles_safe_range > > > - (objfiles_range > > > - (unwrapping_objfile_iterator (objfiles_list.begin ()), > > > - unwrapping_objfile_iterator (objfiles_list.end ()))); > > > + return objfiles_safe_reverse_range > > > + (objfiles_safe_range (objfiles_list.begin (), objfiles_list.en= d ())); > > > } > > > > > > - /* Add OBJFILE to the list of objfiles, putting it just before > > > - BEFORE. If BEFORE is nullptr, it will go at the end of the > > > + /* Add OBJFILE to the list of objfiles, putting it just after > > > + AFTER. If AFTER is nullptr, it will go at the end of the > > > list. */ > > > void add_objfile (std::unique_ptr &&objfile, > > > - struct objfile *before); > > > + struct objfile *after); > > > > > > /* Remove OBJFILE from the list of objfiles. */ > > > void remove_objfile (struct objfile *objfile); > > > @@ -250,6 +254,11 @@ struct program_space > > > /* Free all the objfiles associated with this program space. */ > > > void free_all_objfiles (); > > > > > > + /* Unlink all objfiles associated with this program space for whic= h > > > + PREDICATE evaluates to true. */ > > > + void unlink_objfiles_if > > > + (gdb::function_view predicate); > > > + > > > /* Return the objfile containing ADDRESS, or nullptr if the addres= s > > > is outside all objfiles in this progspace. */ > > > struct objfile *objfile_for_address (CORE_ADDR address); > > > diff --git a/gdb/testsuite/gdb.python/py-objfile.exp b/gdb/testsuite/= gdb.python/py-objfile.exp > > > index 61b9942de79..0bf49976b73 100644 > > > --- a/gdb/testsuite/gdb.python/py-objfile.exp > > > +++ b/gdb/testsuite/gdb.python/py-objfile.exp > > > @@ -135,7 +135,7 @@ gdb_test "p main" "=3D {} $hex
" \ > > > gdb_py_test_silent_cmd "python objfile.add_separate_debug_file(\"${b= infile}\")" \ > > > "Add separate debug file file" 1 > > > > > > -gdb_py_test_silent_cmd "python sep_objfile =3D gdb.objfiles()\[0\]" = \ > > > +gdb_py_test_silent_cmd "python sep_objfile =3D gdb.objfiles()\[1\]" = \ > > > "Get separate debug info objfile" 1 > > > > > > gdb_test "python print (sep_objfile.owner.filename)" "${testfile}2" = \ > > > diff --git a/gdbsupport/safe-iterator.h b/gdbsupport/safe-iterator.h > > > index ccd772ca2a5..9f57c1543cf 100644 > > > --- a/gdbsupport/safe-iterator.h > > > +++ b/gdbsupport/safe-iterator.h > > > @@ -136,4 +136,110 @@ class basic_safe_range > > > Range m_range; > > > }; > > > > > > +/* A reverse basic_safe_iterator. See basic_safe_iterator for inten= ded use. */ > > > + > > > +template > > > +class basic_safe_reverse_iterator > > > +{ > > > +public: > > > + typedef basic_safe_reverse_iterator self_type; > > > + typedef typename Iterator::value_type value_type; > > > + typedef typename Iterator::reference reference; > > > + typedef typename Iterator::pointer pointer; > > > + typedef typename Iterator::iterator_category iterator_category; > > > + typedef typename Iterator::difference_type difference_type; > > > + > > > + /* Construct the iterator using ARG, and construct the end iterato= r > > > + using ARG2. */ > > > + template > > > + explicit basic_safe_reverse_iterator (Arg &&arg, Arg &&arg2) > > > + : m_begin (std::forward (arg)), > > > + m_end (std::forward (arg2)), > > > + m_it (m_end), > > > + m_next (m_end) > > > + { > > > + /* M_IT and M_NEXT are initialized as one-past-end. Set M_IT to= point > > > + to the last element and set M_NEXT to point to the second las= t element, > > > + if such elements exist. */ > > > + if (m_it !=3D m_begin) > > > + { > > > + --m_it; > > > + > > > + if (m_it !=3D m_begin) > > > + { > > > + --m_next; > > > + --m_next; > > > + } > > > + } > > > + } > > > + > > > + typename gdb::invoke_result::type > > > + operator* () const > > > + { return *m_it; } > > > + > > > + self_type &operator++ () > > > + { > > > + m_it =3D m_next; > > > + > > > + if (m_it !=3D m_end) > > > + { > > > + /* Use M_BEGIN only if we sure that it is valid. */ > > > + if (m_it =3D=3D m_begin) > > > + m_next =3D m_end; > > > + else > > > + --m_next; > > > + } > > > + > > > + return *this; > > > + } > > > + > > > + bool operator=3D=3D (const self_type &other) const > > > + { return m_it =3D=3D other.m_it; } > > > + > > > + bool operator!=3D (const self_type &other) const > > > + { return m_it !=3D other.m_it; } > > > + > > > +private: > > > + /* The first element. */ > > > + Iterator m_begin {}; > > > + > > > + /* A one-past-end iterator. */ > > > + Iterator m_end {}; > > > + > > > + /* The current element. */ > > > + Iterator m_it {}; > > > + > > > + /* The next element. Always one element ahead of M_IT. */ > > > + Iterator m_next {}; > > > +}; > > > + > > > +/* A range adapter that wraps a forward range, and then returns > > > + safe reverse iterators wrapping the original range's iterators. = */ > > > + > > > +template > > > +class basic_safe_reverse_range > > > +{ > > > +public: > > > + > > > + typedef basic_safe_reverse_iterator iter= ator; > > > + > > > + explicit basic_safe_reverse_range (Range range) > > > + : m_range (range) > > > + { > > > + } > > > + > > > + iterator begin () > > > + { > > > + return iterator (m_range.begin (), m_range.end ()); > > > + } > > > + > > > + iterator end () > > > + { > > > + return iterator (m_range.end (), m_range.end ()); > > > + } > > > + > > > +private: > > > + > > > + Range m_range; > > > +}; > > > #endif /* COMMON_SAFE_ITERATOR_H */ > > > -- > > > 2.41.0 > > >