commit:     cb3146b4d97606591ae881ec19edebb9164b804c
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Wed Aug 10 07:43:14 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Aug 10 07:43:39 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=cb3146b4

sys-devel/gdb: backport core file detach fix

Signed-off-by: Sam James <sam <AT> gentoo.org>

 .../gdb/files/gdb-12.1-core-file-detach.patch      | 155 +++++++++++
 sys-devel/gdb/gdb-12.1-r2.ebuild                   | 297 +++++++++++++++++++++
 2 files changed, 452 insertions(+)

diff --git a/sys-devel/gdb/files/gdb-12.1-core-file-detach.patch 
b/sys-devel/gdb/files/gdb-12.1-core-file-detach.patch
new file mode 100644
index 000000000000..c0f8a8ee0269
--- /dev/null
+++ b/sys-devel/gdb/files/gdb-12.1-core-file-detach.patch
@@ -0,0 +1,155 @@
+https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=0fe74cb9ad35add9c6da4df5c9879f254d918a6a
+
+From: Pedro Alves <[email protected]>
+Date: Wed, 22 Jun 2022 18:44:37 +0100
+Subject: [PATCH] Fix core-file -> detach -> crash (corefiles/29275)
+
+After loading a core file, you're supposed to be able to use "detach"
+to unload the core file.  That unfortunately regressed starting with
+GDB 11, with these commits:
+
+ 1192f124a308 - gdb: generalize commit_resume, avoid commit-resuming when 
threads have pending statuses
+ 408f66864a1a - detach in all-stop with threads running
+
+resulting in a GDB crash:
+
+ ...
+ Thread 1 "gdb" received signal SIGSEGV, Segmentation fault.
+ 0x0000555555e842bf in maybe_set_commit_resumed_all_targets () at 
../../src/gdb/infrun.c:2899
+ 2899          if (proc_target->commit_resumed_state)
+ (top-gdb) bt
+ #0  0x0000555555e842bf in maybe_set_commit_resumed_all_targets () at 
../../src/gdb/infrun.c:2899
+ #1  0x0000555555e848bf in scoped_disable_commit_resumed::reset 
(this=0x7fffffffd440) at ../../src/gdb/infrun.c:3023
+ #2  0x0000555555e84a0c in scoped_disable_commit_resumed::reset_and_commit 
(this=0x7fffffffd440) at ../../src/gdb/infrun.c:3049
+ #3  0x0000555555e739cd in detach_command (args=0x0, from_tty=1) at 
../../src/gdb/infcmd.c:2791
+ #4  0x0000555555c0ba46 in do_simple_func (args=0x0, from_tty=1, 
c=0x55555662a600) at ../../src/gdb/cli/cli-decode.c:95
+ #5  0x0000555555c112b0 in cmd_func (cmd=0x55555662a600, args=0x0, from_tty=1) 
at ../../src/gdb/cli/cli-decode.c:2514
+ #6  0x0000555556173b1f in execute_command (p=0x5555565c5916 "", from_tty=1) 
at ../../src/gdb/top.c:699
+
+The code that crashes looks like:
+
+ static void
+ maybe_set_commit_resumed_all_targets ()
+ {
+   scoped_restore_current_thread restore_thread;
+
+   for (inferior *inf : all_non_exited_inferiors ())
+     {
+       process_stratum_target *proc_target = inf->process_target ();
+
+       if (proc_target->commit_resumed_state)
+           ^^^^^^^^^^^
+
+With 'proc_target' above being null.  all_non_exited_inferiors filters
+out inferiors that have pid==0.  We get here at the end of
+detach_command, after core_target::detach has already run, at which
+point the inferior _should_ have pid==0 and no process target.  It is
+clear it no longer has a process target, but, it still has a pid!=0
+somehow.
+
+The reason the inferior still has pid!=0, is that core_target::detach
+just unpushes, and relies on core_target::close to actually do the
+getting rid of the core and exiting the inferior.  The problem with
+that is that detach_command grabs an extra strong reference to the
+process stratum target, so the unpush_target inside
+core_target::detach doesn't actually result in a call to
+core_target::close.
+
+Fix this my moving the cleaning up the core inferior to a shared
+routine called by both core_target::close and core_target::detach.  We
+still need to cleanup the inferior from within core_file::close
+because there are paths to it that want to get rid of the core without
+going through detach.  E.g., "core-file" -> "run".
+
+This commit includes a new test added to gdb.base/corefile.exp to
+cover the "core-file core" -> "detach" scenario.
+
+Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29275
+
+Change-Id: Ic42bdd03182166b19f598428b0dbc2ce6f67c893
+--- a/gdb/corelow.c
++++ b/gdb/corelow.c
+@@ -120,6 +120,9 @@ public:
+ 
+ private: /* per-core data */
+ 
++  /* Get rid of the core inferior.  */
++  void clear_core ();
++
+   /* The core's section table.  Note that these target sections are
+      *not* mapped in the current address spaces' set of target
+      sections --- those should come only from pure executable or
+@@ -290,10 +293,8 @@ core_target::build_file_mappings ()
+ /* An arbitrary identifier for the core inferior.  */
+ #define CORELOW_PID 1
+ 
+-/* Close the core target.  */
+-
+ void
+-core_target::close ()
++core_target::clear_core ()
+ {
+   if (core_bfd)
+     {
+@@ -307,6 +308,14 @@ core_target::close ()
+ 
+       current_program_space->cbfd.reset (nullptr);
+     }
++}
++
++/* Close the core target.  */
++
++void
++core_target::close ()
++{
++  clear_core ();
+ 
+   /* Core targets are heap-allocated (see core_target_open), so here
+      we delete ourselves.  */
+@@ -592,9 +601,15 @@ core_target_open (const char *arg, int from_tty)
+ void
+ core_target::detach (inferior *inf, int from_tty)
+ {
+-  /* Note that 'this' is dangling after this call.  unpush_target
+-     closes the target, and our close implementation deletes
+-     'this'.  */
++  /* Get rid of the core.  Don't rely on core_target::close doing it,
++     because target_detach may be called with core_target's refcount > 1,
++     meaning core_target::close may not be called yet by the
++     unpush_target call below.  */
++  clear_core ();
++
++  /* Note that 'this' may be dangling after this call.  unpush_target
++     closes the target if the refcount reaches 0, and our close
++     implementation deletes 'this'.  */
+   inf->unpush_target (this);
+ 
+   /* Clear the register cache and the frame cache.  */
+--- a/gdb/testsuite/gdb.base/corefile.exp
++++ b/gdb/testsuite/gdb.base/corefile.exp
+@@ -207,6 +207,16 @@ gdb_test "up" "#\[0-9\]* *\[0-9xa-fH'\]* in .* 
\\(.*\\).*" "up in corefile.exp (
+ 
+ gdb_test "core" "No core file now."
+ 
++# Test that we can unload the core with the "detach" command.
++
++proc_with_prefix corefile_detach {} {
++    clean_restart $::binfile
++
++    gdb_test "core-file $::corefile" "Core was generated by .*" "load core"
++    gdb_test "detach" "No core file now\\." "detach core"
++}
++
++corefile_detach
+ 
+ # Test a run (start) command will clear any loaded core file.
+ 
+@@ -222,6 +232,8 @@ proc corefile_test_run {} {
+       return
+     }
+ 
++    clean_restart $::binfile
++
+     gdb_test "core-file $corefile" "Core was generated by .*" "run: load core 
again"
+     gdb_test "info files" "\r\nLocal core dump file:\r\n.*" "run: sanity 
check we see the core file"
+ 

diff --git a/sys-devel/gdb/gdb-12.1-r2.ebuild b/sys-devel/gdb/gdb-12.1-r2.ebuild
new file mode 100644
index 000000000000..9da927c79e77
--- /dev/null
+++ b/sys-devel/gdb/gdb-12.1-r2.ebuild
@@ -0,0 +1,297 @@
+# Copyright 1999-2022 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+
+PYTHON_COMPAT=( python3_{8..11} )
+inherit flag-o-matic python-single-r1 strip-linguas toolchain-funcs
+
+export CTARGET=${CTARGET:-${CHOST}}
+
+if [[ ${CTARGET} == ${CHOST} ]] ; then
+       if [[ ${CATEGORY} == cross-* ]] ; then
+               export CTARGET=${CATEGORY#cross-}
+       fi
+fi
+
+is_cross() { [[ ${CHOST} != ${CTARGET} ]] ; }
+
+case ${PV} in
+       9999*)
+               # live git tree
+               EGIT_REPO_URI="https://sourceware.org/git/binutils-gdb.git";
+               inherit git-r3
+               SRC_URI=""
+               ;;
+       *.*.50.2???????)
+               # weekly snapshots
+               
SRC_URI="ftp://sourceware.org/pub/gdb/snapshots/current/gdb-weekly-${PV}.tar.xz";
+               ;;
+       *)
+               # Normal upstream release
+               SRC_URI="mirror://gnu/gdb/${P}.tar.xz
+                       ftp://sourceware.org/pub/gdb/releases/${P}.tar.xz";
+               ;;
+esac
+
+PATCH_VER=""
+PATCH_DEV=""
+DESCRIPTION="GNU debugger"
+HOMEPAGE="https://sourceware.org/gdb/";
+SRC_URI="${SRC_URI}
+       
${PATCH_DEV:+https://dev.gentoo.org/~${PATCH_DEV}/distfiles/${P}-patches-${PATCH_VER}.tar.xz}
+       ${PATCH_VER:+mirror://gentoo/${P}-patches-${PATCH_VER}.tar.xz}
+"
+
+LICENSE="GPL-3+ LGPL-2.1+"
+SLOT="0"
+
+if [[ ${PV} != 9999* ]] ; then
+       KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 
~riscv ~s390 ~sparc ~x86 ~x64-cygwin ~amd64-linux ~x86-linux ~x64-macos 
~sparc-solaris ~sparc64-solaris ~x64-solaris ~x86-solaris"
+fi
+
+IUSE="cet guile lzma multitarget nls +python +server source-highlight test 
vanilla xml xxhash"
+REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
+
+# In fact, gdb's test suite needs some work to get passing.
+# See e.g. https://sourceware.org/gdb/wiki/TestingGDB.
+# As of 11.2, on amd64: "# of unexpected failures    8600"
+# ia64 kernel crashes when gdb testsuite is running
+# in fact, gdb's test suite needs some work to get passing.
+# See e.g. https://sourceware.org/gdb/wiki/TestingGDB.
+# As of 11.2, on amd64: "# of unexpected failures    8600"
+RESTRICT="
+       ia64? ( test )
+       !test? ( test )
+       test
+"
+
+RDEPEND="
+       dev-libs/mpfr:0=
+       dev-libs/gmp:=
+       >=sys-libs/ncurses-5.2-r2:0=
+       >=sys-libs/readline-7:0=
+       sys-libs/zlib
+       elibc_glibc? ( net-libs/libnsl:= )
+       lzma? ( app-arch/xz-utils )
+       python? ( ${PYTHON_DEPS} )
+       guile? ( >=dev-scheme/guile-2.0 )
+       xml? ( dev-libs/expat )
+       source-highlight? (
+               dev-util/source-highlight
+       )
+       xxhash? (
+               dev-libs/xxhash
+       )
+"
+DEPEND="${RDEPEND}"
+BDEPEND="
+       app-arch/xz-utils
+       sys-apps/texinfo
+       virtual/yacc
+       nls? ( sys-devel/gettext )
+       source-highlight? ( virtual/pkgconfig )
+       test? ( dev-util/dejagnu )
+"
+
+PATCHES=(
+       "${FILESDIR}"/${PN}-8.3.1-verbose-build.patch
+       "${FILESDIR}"/${P}-readline-8.2-build.patch
+       "${FILESDIR}"/${P}-core-file-detach.patch
+)
+
+pkg_setup() {
+       use python && python-single-r1_pkg_setup
+}
+
+src_prepare() {
+       default
+
+       strip-linguas -u bfd/po opcodes/po
+
+       # Avoid using ancient termcap from host on Prefix systems
+       sed -i -e 's/termcap tinfow/tinfow/g' \
+               gdb/configure{.ac,} || die
+}
+
+gdb_branding() {
+       printf "Gentoo ${PV} "
+
+       if ! use vanilla && [[ -n ${PATCH_VER} ]] ; then
+               printf "p${PATCH_VER}"
+       else
+               printf "vanilla"
+       fi
+
+       [[ -n ${EGIT_COMMIT} ]] && printf " ${EGIT_COMMIT}"
+}
+
+src_configure() {
+       strip-unsupported-flags
+
+       # See 
https://www.gnu.org/software/make/manual/html_node/Parallel-Output.html
+       # Avoid really confusing logs from subconfigure spam, makes logs far
+       # more legible.
+       MAKEOPTS="--output-sync=line ${MAKEOPTS}"
+
+       local myconf=(
+               # portage's econf() does not detect presence of --d-d-t
+               # because it greps only top-level ./configure. But not
+               # gnulib's or gdb's configure.
+               --disable-dependency-tracking
+
+               --with-pkgversion="$(gdb_branding)"
+               --with-bugurl='https://bugs.gentoo.org/'
+               --disable-werror
+               # Disable modules that are in a combined binutils/gdb tree. bug 
#490566
+               --disable-{binutils,etc,gas,gold,gprof,ld}
+
+               # avoid automagic dependency on (currently prefix) systems
+               # systems with debuginfod library, bug #754753
+               --without-debuginfod
+
+               $(use_enable test unit-tests)
+
+               # Allow user to opt into CET for host libraries.
+               # Ideally we would like automagic-or-disabled here.
+               # But the check does not quite work on i686: bug #760926.
+               $(use_enable cet)
+
+               # We need to set both configure options, --with-sysroot and 
--libdir,
+               # to fix cross build issues that happen when configuring gmp.
+               # We explicitly need --libdir. Having only --with-sysroot 
without
+               # --libdir would not fix the build issues.
+               # For some reason, it is not enough to set only --with-sysroot,
+               # also not enough to pass --with-gmp-xxx options.
+               --with-sysroot="${ESYSROOT}"
+               --libdir="${ESYSROOT}/usr/$(get_libdir)"
+       )
+
+       local sysroot="${EPREFIX}/usr/${CTARGET}"
+
+       is_cross && myconf+=(
+               --with-sysroot="${sysroot}"
+               --includedir="${sysroot}/usr/include"
+               --with-gdb-datadir="\${datadir}/gdb/${CTARGET}"
+       )
+
+       # gdbserver only works for native targets (CHOST==CTARGET).
+       # it also doesn't support all targets, so rather than duplicate
+       # the target list (which changes between versions), use the
+       # "auto" value when things are turned on, which is triggered
+       # whenever no --enable or --disable is given
+       if is_cross || use !server ; then
+               myconf+=( --disable-gdbserver )
+       fi
+
+       myconf+=(
+               --enable-64-bit-bfd
+               --disable-install-libbfd
+               --disable-install-libiberty
+               --enable-obsolete
+               # This only disables building in the readline subdir.
+               # For gdb itself, it'll use the system version.
+               --disable-readline
+               --with-system-readline
+               # This only disables building in the zlib subdir.
+               # For gdb itself, it'll use the system version.
+               --without-zlib
+               --with-system-zlib
+               --with-separate-debug-dir="${EPREFIX}"/usr/lib/debug
+               $(use_with xml expat)
+               $(use_with lzma)
+               $(use_enable nls)
+               $(use_enable source-highlight)
+               $(use multitarget && echo --enable-targets=all)
+               $(use_with python python "${EPYTHON}")
+               $(use_with xxhash)
+               $(use_with guile)
+       )
+
+       if use sparc-solaris || use x86-solaris ; then
+               # Disable largefile support
+               # https://sourceware.org/ml/gdb-patches/2014-12/msg00058.html
+               myconf+=( --disable-largefile )
+       fi
+
+       # source-highlight is detected with pkg-config: bug #716558
+       export ac_cv_path_pkg_config_prog_path="$(tc-getPKG_CONFIG)"
+
+       export CC_FOR_BUILD="$(tc-getBUILD_CC)"
+
+       # ensure proper compiler is detected for Clang builds: bug #831202
+       export GCC_FOR_TARGET="${CC_FOR_TARGET:-$(tc-getCC)}"
+
+       econf "${myconf[@]}"
+}
+
+src_compile() {
+       emake V=1
+}
+
+src_install() {
+       emake V=1 DESTDIR="${D}" install
+
+       find "${ED}"/usr -name libiberty.a -delete || die
+
+       # Delete translations that conflict with binutils-libs. bug #528088
+       # Note: Should figure out how to store these in an internal gdb dir.
+       if use nls ; then
+               find "${ED}" \
+                       -regextype posix-extended -regex 
'.*/(bfd|opcodes)[.]g?mo$' \
+                       -delete || die
+       fi
+
+       # Don't install docs when building a cross-gdb
+       if [[ ${CTARGET} != ${CHOST} ]] ; then
+               rm -rf "${ED}"/usr/share/{doc,info,locale} || die
+               local f
+               for f in "${ED}"/usr/share/man/*/* ; do
+                       if [[ ${f##*/} != ${CTARGET}-* ]] ; then
+                               mv "${f}" "${f%/*}/${CTARGET}-${f##*/}" || die
+                       fi
+               done
+               return 0
+       fi
+
+       # Install it by hand for now:
+       # https://sourceware.org/ml/gdb-patches/2011-12/msg00915.html
+       # Only install if it exists due to the twisted behavior (see
+       # notes in src_configure above).
+       [[ -e gdbserver/gdbreplay ]] && dobin gdbserver/gdbreplay
+
+       docinto gdb
+       dodoc gdb/CONTRIBUTE gdb/README gdb/MAINTAINERS \
+               gdb/NEWS gdb/PROBLEMS
+       docinto sim
+       dodoc sim/{MAINTAINERS,README-HACKING}
+
+       if use server ; then
+               docinto gdbserver
+               dodoc gdbserver/README
+       fi
+
+       if [[ -n ${PATCH_VER} ]] ; then
+               dodoc "${WORKDIR}"/extra/gdbinit.sample
+       fi
+
+       # Remove shared info pages
+       rm -f 
"${ED}"/usr/share/info/{annotate,bfd,configure,ctf-spec,standards}.info*
+
+       if use python ; then
+               python_optimize "${ED}"/usr/share/gdb/python/gdb
+       fi
+}
+
+pkg_postinst() {
+       # Portage doesn't unmerge files in /etc
+       rm -vf "${EROOT}"/etc/skel/.gdbinit
+
+       if use prefix && [[ ${CHOST} == *-darwin* ]] ; then
+               ewarn "gdb is unable to get a mach task port when installed by 
Prefix"
+               ewarn "Portage, unprivileged.  To make gdb fully functional 
you'll"
+               ewarn "have to perform the following steps:"
+               ewarn "  % sudo chgrp procmod ${EPREFIX}/usr/bin/gdb"
+               ewarn "  % sudo chmod g+s ${EPREFIX}/usr/bin/gdb"
+       fi
+}

Reply via email to