Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Rohr, Stephan" <stephan.rohr@intel.com>
To: Eduard-Petru Jipa <ejipa@undo.io>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH] gdb: don't abort shared library reload when one library fails to open
Date: Wed, 7 Oct 2026 12:45:35 +0000	[thread overview]
Message-ID: <DS7PR11MB62473DA8CF606000D4BA471A93942@DS7PR11MB6247.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20261007111849.4992-1-ejipa@undo.io>

Hi Eduard,

please see my feedback below:

thanks
stephan

> -----Original Message-----
> From: Eduard-Petru Jipa <ejipa@undo.io>
> Sent: Wednesday, 7 October 2026 13:19
> To: gdb-patches@sourceware.org
> Cc: Eduard Jipa <ejipa@undo.io>
> Subject: [PATCH] gdb: don't abort shared library reload when one library fails
> to open
> 
> From: Eduard Jipa <ejipa@undo.io>
> 
> When the set of shared libraries is reloaded, for example after
> 'set sysroot' or 'set solib-search-path', reload_shared_libraries_1
> calls solib_ops::bfd_open for each library in turn.  This can throw an
> error, for example if the file found is not in executable format, or
> its architecture is not compatible with the target.
> 
> Nothing in the loop catches that error, so a single bad library stops
> the whole reload.  Any libraries after it in the list are not reloaded,
> and the error propagates out of the command that triggered the reload.
> 
> Catch the error, print it as a warning, and carry on with the rest of
> the libraries.  This matches what is already done for errors from
> solib_map_sections later in the same loop.  The library that failed
> to open is handled as if it had not been found.
> 
> Add a test that builds a sysroot in which the first of two libraries
> is not a valid object file, and checks that the second library is
> still reloaded from the new sysroot after 'set sysroot'.
> 
> Tested on x86_64-linux with no regressions.
> ---
>  gdb/solib.c                                   | 13 ++-
>  .../gdb.base/solib-reload-error-lib.c         | 25 ++++++
>  gdb/testsuite/gdb.base/solib-reload-error.c   | 25 ++++++
>  gdb/testsuite/gdb.base/solib-reload-error.exp | 81 +++++++++++++++++++
>  4 files changed, 143 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.base/solib-reload-error-lib.c
>  create mode 100644 gdb/testsuite/gdb.base/solib-reload-error.c
>  create mode 100644 gdb/testsuite/gdb.base/solib-reload-error.exp
> 
> diff --git a/gdb/solib.c b/gdb/solib.c
> index 7ea3084bf48..ad964e0afe9 100644
> --- a/gdb/solib.c
> +++ b/gdb/solib.c
> @@ -1463,7 +1463,18 @@ reload_shared_libraries_1 (int from_tty)
>        gdb::unique_xmalloc_ptr<char> filename
>  	= gdb_rl_tilde_expand (so.original_name.c_str ());
> 
> -      gdb_bfd_ref_ptr abfd = so.ops ().bfd_open (filename.get ());
> +      gdb_bfd_ref_ptr abfd;
> +      try
> +	{
> +	  abfd = so.ops ().bfd_open (filename.get ());
> +	}
> +      catch (const gdb_exception_error &e)
> +	{
> +	  exception_fprintf (gdb_stderr, e,
> +			     _("Error while opening shared library `%ps':\n"),
> +			     styled_string (file_name_style.style (),
> +					    filename.get ()));

I think this prints the filename twice.  I tried it locally, e.g.,

Error while opening shared library [..]/gdb/testsuite/outputs/gdb.base/solib-reload-error/solib-reload-error-lib1.so':
`[..]/gdb/testsuite/outputs/gdb.base/solib-reload-error/sysroot/[..] \
  /gdb/testsuite/outputs/gdb.base/solib-reload-error/solib-reload-error-lib1.so': \
  not in executable format: file format not recognized

I think it would be nice to simply print

Error while opening shared library <path to lib>: <error>.

Would that be possible?  If not, we could drop the first mention of the failing lib?

Also, the commit message mentions "Catch the error, print it as warning [..]" but you use
"exception_printf" here.  Either reword the commit message or use "warning ()".

> +	}
>        if (abfd != NULL)
>  	found_pathname = bfd_get_filename (abfd.get ());
> 
> diff --git a/gdb/testsuite/gdb.base/solib-reload-error-lib.c
> b/gdb/testsuite/gdb.base/solib-reload-error-lib.c
> new file mode 100644
> index 00000000000..7aaa13fd697
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/solib-reload-error-lib.c
> @@ -0,0 +1,25 @@
> +/* This test program is part of GDB, the GNU debugger.
> +
> +   Copyright 2026 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 <http://www.gnu.org/licenses/>.  */
> +
> +/* This file is compiled twice, with LIB_FUNC defined to a different
> +   name each time.  */
> +
> +int
> +LIB_FUNC (void)
> +{
> +  return 0;
> +}
> diff --git a/gdb/testsuite/gdb.base/solib-reload-error.c
> b/gdb/testsuite/gdb.base/solib-reload-error.c
> new file mode 100644
> index 00000000000..465cf7e82e6
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/solib-reload-error.c
> @@ -0,0 +1,25 @@
> +/* This test program is part of GDB, the GNU debugger.
> +
> +   Copyright 2026 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 <http://www.gnu.org/licenses/>.  */
> +
> +extern int lib1_func (void);
> +extern int lib2_func (void);
> +
> +int
> +main (void)
> +{
> +  return lib1_func () + lib2_func ();
> +}
> diff --git a/gdb/testsuite/gdb.base/solib-reload-error.exp
> b/gdb/testsuite/gdb.base/solib-reload-error.exp
> new file mode 100644
> index 00000000000..adbafde1fbe
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/solib-reload-error.exp
> @@ -0,0 +1,81 @@
> +# Copyright 2026 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 <http://www.gnu.org/licenses/>.
> +
> +# Check that an error opening one shared library while reloading
> +# shared libraries (in this case, triggered by 'set sysroot') does not
> +# stop GDB from reloading the shared libraries that come after it.
> +
> +require allow_shlib_tests
> +require {!is_remote host}
> +
> +standard_testfile .c -lib.c
> +
> +set lib1 [standard_output_file ${testfile}-lib1.so]
> +set lib2 [standard_output_file ${testfile}-lib2.so]
> +
> +if { [gdb_compile_shlib $srcdir/$subdir/$srcfile2 $lib1 \
> +	  {debug additional_flags=-DLIB_FUNC=lib1_func}] != ""
> +     || [gdb_compile_shlib $srcdir/$subdir/$srcfile2 $lib2 \
> +	     {debug additional_flags=-DLIB_FUNC=lib2_func}] != "" } {
> +    untested "failed to compile shared libraries"
> +    return
> +}
> +
> +# Link against lib1 first, so that it is ahead of lib2 in the list of
> +# shared libraries that GDB reloads.
> +if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
> +	  [list debug shlib=$lib1 shlib=$lib2]] } {
> +    return
> +}
> +
> +gdb_load_shlib $lib1
> +gdb_load_shlib $lib2
> +
> +if { ![runto_main] } {
> +    return
> +}
> +
> +# The rest of the test relies on lib1 being reloaded before lib2.
> +if { [gdb_test "info sharedlibrary" \
> +	  "[string_to_regexp $lib1]\r\n.*[string_to_regexp $lib2]\r\n.*" \
> +	  "lib1 is ahead of lib2 in the list"] != 0 } {
> +    return
> +}
> +
> +# Build a sysroot in which lib1 is not a valid object file, but lib2
> +# is a good copy of the real library.
> +set sysroot [standard_output_file sysroot]
> +set sysroot_lib1 $sysroot$lib1
> +set sysroot_lib2 $sysroot$lib2
> +file delete -force $sysroot
> +file mkdir [file dirname $sysroot_lib1]
> +set fd [open $sysroot_lib1 w]
> +puts $fd "This is not an object file."
> +close $fd
> +file copy $lib2 $sysroot_lib2
> +
> +# Changing the sysroot reloads every shared library.  Opening lib1
> +# fails, and GDB should report that and carry on.
> +gdb_test "set sysroot $sysroot" \
> +    "Error while opening shared library `[string_to_regexp $lib1]':\r\n.*not in
> executable format.*" \
> +    "set sysroot with bad lib1"
> +
> +# lib2 comes after lib1 in the list, so it is only reloaded from the
> +# new sysroot if the error opening lib1 did not stop the reload.
> +gdb_test "info sharedlibrary" \
> +    "\r\n$hex\[ \t\]+$hex\[ \t\]+Yes\[^\r\n\]*[string_to_regexp

Could you also cover lib1, i.e., ensure it is not loaded?

> $sysroot_lib2]\r\n.*" \
> +    "lib2 is reloaded from the new sysroot"
> +
> +gdb_test "info symbol lib2_func" "lib2_func in section \\.text of
> [string_to_regexp $sysroot_lib2]"
> --
> 2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

  reply	other threads:[~2026-10-07 12:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-07 11:18 Eduard-Petru Jipa
2026-10-07 12:45 ` Rohr, Stephan [this message]
2026-10-07 13:36   ` [PATCH v2] " Eduard-Petru Jipa
2026-10-07 15:35     ` Rohr, Stephan
2026-10-08 13:06       ` [PATCH v3] " Eduard-Petru Jipa
2026-10-27 14:25         ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DS7PR11MB62473DA8CF606000D4BA471A93942@DS7PR11MB6247.namprd11.prod.outlook.com \
    --to=stephan.rohr@intel.com \
    --cc=ejipa@undo.io \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox