diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 144335a7efa..b766a0b2638 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2021-07-13 Pedro Alves + + PR gdb/28080 + * gdb_bfd.c (gdb_bfd_close_warning): New. + (gdb_bfd_iovec_fileio_close): Wrap target_fileio_close in + try/catch and print warning on exception. + (gdb_bfd_close_or_warn): Use gdb_bfd_close_warning. + 2021-07-13 Pedro Alves PR gdb/28080 diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index 3312197acd5..312442a466e 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -423,6 +423,15 @@ gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf, return pos; } +/* Warn that it wasn't possible to close a bfd for file NAME, because + of REASON. */ + +static void +gdb_bfd_close_warning (const char *name, const char *reason) +{ + warning (_("cannot close \"%s\": %s"), name, reason); +} + /* Wrapper for target_fileio_close suitable for passing as the CLOSE_FUNC argument to gdb_bfd_openr_iovec. */ @@ -436,7 +445,16 @@ gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream) /* Ignore errors on close. These may happen with remote targets if the connection has already been torn down. */ - target_fileio_close (fd, &target_errno); + try + { + target_fileio_close (fd, &target_errno); + } + catch (const gdb_exception &ex) + { + /* Also avoid crossing exceptions over bfd. */ + gdb_bfd_close_warning (bfd_get_filename (abfd), + ex.message->c_str ()); + } /* Zero means success. */ return 0; @@ -626,8 +644,8 @@ gdb_bfd_close_or_warn (struct bfd *abfd) ret = bfd_close (abfd); if (!ret) - warning (_("cannot close \"%s\": %s"), - name, bfd_errmsg (bfd_get_error ())); + gdb_bfd_close_warning (name, + bfd_errmsg (bfd_get_error ())); return ret; }