* [PATCH 1/5] Reject null pointers in program_space::set_exec_bfd
2026-11-11 2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
@ 2026-11-11 2:06 ` Tom Tromey
2026-11-11 2:06 ` [PATCH 2/5] Hoist call to gdb_bfd_check_format_matches in exec.c Tom Tromey
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2026-11-11 2:06 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
The sole caller of set_exec_bfd ensures that the passed-in BFD cannot
be nullptr. It seemed to me that the method itself should assert this
as well, as clearing the exec BFD requires a bit of extra work -- see
program_space::exec_close.
This also changes set_exec_bfd to require the precondition that there
is currently no exec BFD; again, if there was one, exec_close would
need to be called.
---
gdb/exec.c | 6 +++---
gdb/progspace.h | 2 ++
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/gdb/exec.c b/gdb/exec.c
index 806f9b0a67a..4f0a0ca23e3 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -465,13 +465,13 @@ exec_file_attach (const char *filename, int from_tty)
FOPEN_RUB, scratch_chan);
else
temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
- current_program_space->set_exec_bfd (std::move (temp));
-
- if (!current_program_space->exec_bfd ())
+ if (temp == nullptr)
error (_("\"%ps\": could not open as an executable file: %s."),
styled_string (file_name_style.style (), scratch_pathname),
bfd_errmsg (bfd_get_error ()));
+ current_program_space->set_exec_bfd (std::move (temp));
+
/* gdb_realpath_keepfile resolves symlinks on the local
filesystem and so cannot be used for "target:" files. */
gdb_assert (current_program_space->exec_filename () == nullptr);
diff --git a/gdb/progspace.h b/gdb/progspace.h
index a5ef3deb337..d0d8466a8e5 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -312,6 +312,8 @@ struct program_space
/* Set the exec BFD for this program space to ABFD. */
void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
{
+ gdb_assert (ebfd == nullptr);
+ gdb_assert (abfd != nullptr);
ebfd = std::move (abfd);
}
--
2.49.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/5] Hoist call to gdb_bfd_check_format_matches in exec.c
2026-11-11 2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
2026-11-11 2:06 ` [PATCH 1/5] Reject null pointers in program_space::set_exec_bfd Tom Tromey
@ 2026-11-11 2:06 ` Tom Tromey
2026-11-11 2:06 ` [PATCH 3/5] Have program_space::set_exec_bfd do more work Tom Tromey
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2026-11-11 2:06 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
Right now, exec.c calls gdb_bfd_check_format_matches after setting the
program space's exec_file. This means a call to exec_close is needed
before throwing an exception, to avoid leaving the exec BFD set in
this case.
It seems to me that hoisting this call is more straightforward, as now
the exec_close isn't needed.
---
gdb/exec.c | 30 +++++++++++++-----------------
1 file changed, 13 insertions(+), 17 deletions(-)
diff --git a/gdb/exec.c b/gdb/exec.c
index 4f0a0ca23e3..aa6fc42ebe7 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -459,18 +459,25 @@ exec_file_attach (const char *filename, int from_tty)
canonical_pathname = canonical_storage.get ();
}
- gdb_bfd_ref_ptr temp;
+ gdb_bfd_ref_ptr new_exec_bfd;
if (write_files && !load_via_target)
- temp = gdb_bfd_fopen (canonical_pathname, gnutarget,
- FOPEN_RUB, scratch_chan);
+ new_exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
+ FOPEN_RUB, scratch_chan);
else
- temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
- if (temp == nullptr)
+ new_exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget,
+ scratch_chan);
+ if (new_exec_bfd == nullptr)
error (_("\"%ps\": could not open as an executable file: %s."),
styled_string (file_name_style.style (), scratch_pathname),
bfd_errmsg (bfd_get_error ()));
- current_program_space->set_exec_bfd (std::move (temp));
+ if (!gdb_bfd_check_format_matches (new_exec_bfd.get (), bfd_object,
+ &matching))
+ error (_("\"%ps\": not in executable format: %s"),
+ styled_string (file_name_style.style (), scratch_pathname),
+ gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
+
+ current_program_space->set_exec_bfd (std::move (new_exec_bfd));
/* gdb_realpath_keepfile resolves symlinks on the local
filesystem and so cannot be used for "target:" files. */
@@ -484,17 +491,6 @@ exec_file_attach (const char *filename, int from_tty)
(make_unique_xstrdup (gdb_realpath_keepfile
(scratch_pathname).c_str ()));
- if (!gdb_bfd_check_format_matches (current_program_space->exec_bfd (),
- bfd_object, &matching))
- {
- /* Make sure to close exec_bfd, or else "run" might try to use
- it. */
- current_program_space->exec_close ();
- error (_("\"%ps\": not in executable format: %s"),
- styled_string (file_name_style.style (), scratch_pathname),
- gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
- }
-
std::vector<target_section> sections
= build_section_table (current_program_space->exec_bfd ());
--
2.49.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 3/5] Have program_space::set_exec_bfd do more work
2026-11-11 2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
2026-11-11 2:06 ` [PATCH 1/5] Reject null pointers in program_space::set_exec_bfd Tom Tromey
2026-11-11 2:06 ` [PATCH 2/5] Hoist call to gdb_bfd_check_format_matches in exec.c Tom Tromey
@ 2026-11-11 2:06 ` Tom Tromey
2026-11-11 2:06 ` [PATCH 4/5] Mark two program_space fields as private Tom Tromey
2026-11-11 2:06 ` [PATCH 5/5] Add some uses of bool to progspace and exec_file_attach Tom Tromey
4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2026-11-11 2:06 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
This changes program_space::set_exec_bfd to also set the exec
filename, and to compute the exec BFD mtime. This is better because
these things really ought to all be set at the same time.
program_space::set_exec_filename is not needed any more -- and should
not be needed in the future.
---
gdb/exec.c | 20 +++++++-------------
gdb/progspace.c | 14 ++++++++++++++
gdb/progspace.h | 14 ++++----------
3 files changed, 25 insertions(+), 23 deletions(-)
diff --git a/gdb/exec.c b/gdb/exec.c
index aa6fc42ebe7..125ba9d2c44 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -477,26 +477,20 @@ exec_file_attach (const char *filename, int from_tty)
styled_string (file_name_style.style (), scratch_pathname),
gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
- current_program_space->set_exec_bfd (std::move (new_exec_bfd));
-
- /* gdb_realpath_keepfile resolves symlinks on the local
- filesystem and so cannot be used for "target:" files. */
- gdb_assert (current_program_space->exec_filename () == nullptr);
+ gdb::unique_xmalloc_ptr<char> new_exec_filename;
if (load_via_target)
- current_program_space->set_exec_filename
- (make_unique_xstrdup
- (bfd_get_filename (current_program_space->exec_bfd ())));
+ new_exec_filename
+ = make_unique_xstrdup (bfd_get_filename (new_exec_bfd.get ()));
else
- current_program_space->set_exec_filename
- (make_unique_xstrdup (gdb_realpath_keepfile
+ new_exec_filename
+ = (make_unique_xstrdup (gdb_realpath_keepfile
(scratch_pathname).c_str ()));
+ current_program_space->set_exec_bfd (std::move (new_exec_bfd),
+ std::move (new_exec_filename));
std::vector<target_section> sections
= build_section_table (current_program_space->exec_bfd ());
- current_program_space->ebfd_mtime
- = gdb_bfd_get_mtime (current_program_space->exec_bfd ());
-
validate_files ();
set_gdbarch_from_file (current_program_space->exec_bfd ());
diff --git a/gdb/progspace.c b/gdb/progspace.c
index a9d13c91598..430629c57b3 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -277,6 +277,20 @@ program_space::objfile_for_address (CORE_ADDR address)
/* See progspace.h. */
+void
+program_space::set_exec_bfd (gdb_bfd_ref_ptr &&abfd,
+ gdb::unique_xmalloc_ptr<char> new_filename)
+{
+ gdb_assert (ebfd == nullptr);
+ gdb_assert (abfd != nullptr);
+ ebfd = std::move (abfd);
+ gdb_assert (m_exec_filename = nullptr);
+ m_exec_filename = std::move (new_filename);
+ ebfd_mtime = gdb_bfd_get_mtime (ebfd.get ());
+}
+
+/* See progspace.h. */
+
void
program_space::exec_close ()
{
diff --git a/gdb/progspace.h b/gdb/progspace.h
index d0d8466a8e5..f55444ce88c 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -298,9 +298,6 @@ struct program_space
const char *exec_filename () const
{ return m_exec_filename.get (); }
- void set_exec_filename (gdb::unique_xmalloc_ptr<char> filename)
- { m_exec_filename = std::move (filename); }
-
/* Close and clear exec_bfd. If we end up with no target sections
to read memory from, this unpushes the exec_ops target. */
void exec_close ();
@@ -309,13 +306,10 @@ struct program_space
bfd *exec_bfd () const
{ return ebfd.get (); }
- /* Set the exec BFD for this program space to ABFD. */
- void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
- {
- gdb_assert (ebfd == nullptr);
- gdb_assert (abfd != nullptr);
- ebfd = std::move (abfd);
- }
+ /* Set the exec BFD for this program space to ABFD. This also
+ updates the ebfd_mtime and set the exec filename. */
+ void set_exec_bfd (gdb_bfd_ref_ptr &&abfd,
+ gdb::unique_xmalloc_ptr<char> new_filename);
/* Reset saved solib data at the start of an solib event. This lets
us properly collect the data when calling solib_add, so it can then
--
2.49.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 4/5] Mark two program_space fields as private
2026-11-11 2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
` (2 preceding siblings ...)
2026-11-11 2:06 ` [PATCH 3/5] Have program_space::set_exec_bfd do more work Tom Tromey
@ 2026-11-11 2:06 ` Tom Tromey
2026-11-11 2:06 ` [PATCH 5/5] Add some uses of bool to progspace and exec_file_attach Tom Tromey
4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2026-11-11 2:06 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
This changes the 'ebfd' and 'ebfd_mtime' fields in program_space to be
private. A new method is added to access the mtime. This patch also
found one remaining spot that was directly accessing ebfd.
---
gdb/corefile.c | 4 ++--
gdb/exec.c | 2 +-
gdb/progspace.c | 20 ++++++++++----------
gdb/progspace.h | 23 ++++++++++++++---------
gdb/source-cache.c | 2 +-
5 files changed, 28 insertions(+), 23 deletions(-)
diff --git a/gdb/corefile.c b/gdb/corefile.c
index 37f04fa7794..ac488e8143f 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -55,8 +55,8 @@ reopen_exec_file (void)
int res = gdb_bfd_stat (exec_bfd, &st);
if (res == 0
- && current_program_space->ebfd_mtime != 0
- && current_program_space->ebfd_mtime != st.st_mtime)
+ && current_program_space->exec_bfd_mtime () != 0
+ && current_program_space->exec_bfd_mtime () != st.st_mtime)
exec_file_attach (bfd_get_filename (exec_bfd), 0);
}
\f
diff --git a/gdb/exec.c b/gdb/exec.c
index 125ba9d2c44..000b1170145 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -499,7 +499,7 @@ exec_file_attach (const char *filename, int from_tty)
list of sections. This possibly pushes the exec_ops
target. */
current_program_space->add_target_sections
- (current_program_space->ebfd.get (), sections);
+ (current_program_space->exec_bfd (), sections);
}
/* Are are loading the same executable? */
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 430629c57b3..619db5ef2fb 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -281,12 +281,12 @@ void
program_space::set_exec_bfd (gdb_bfd_ref_ptr &&abfd,
gdb::unique_xmalloc_ptr<char> new_filename)
{
- gdb_assert (ebfd == nullptr);
+ gdb_assert (m_ebfd == nullptr);
gdb_assert (abfd != nullptr);
- ebfd = std::move (abfd);
- gdb_assert (m_exec_filename = nullptr);
+ m_ebfd = std::move (abfd);
+ gdb_assert (m_exec_filename == nullptr);
m_exec_filename = std::move (new_filename);
- ebfd_mtime = gdb_bfd_get_mtime (ebfd.get ());
+ m_ebfd_mtime = gdb_bfd_get_mtime (m_ebfd.get ());
}
/* See progspace.h. */
@@ -294,15 +294,15 @@ program_space::set_exec_bfd (gdb_bfd_ref_ptr &&abfd,
void
program_space::exec_close ()
{
- if (ebfd != nullptr)
+ if (m_ebfd != nullptr)
{
/* Removing target sections may close the exec_ops target.
- Clear ebfd before doing so to prevent recursion. We
+ Clear m_ebfd before doing so to prevent recursion. We
move it to another ref_ptr instead of saving it to a raw
pointer to avoid it looking like possible use-after-free. */
- gdb_bfd_ref_ptr saved_ebfd = std::move (ebfd);
- ebfd.reset (nullptr);
- ebfd_mtime = 0;
+ gdb_bfd_ref_ptr saved_ebfd = std::move (m_ebfd);
+ m_ebfd.reset (nullptr);
+ m_ebfd_mtime = 0;
remove_target_sections (saved_ebfd.get ());
@@ -599,7 +599,7 @@ initialize_progspace ()
_initialize_foo routines may need to install their per-pspace
data keys. We can only allocate a progspace when all those
modules have done that. Do this before
- initialize_current_architecture, because that accesses the ebfd
+ initialize_current_architecture, because that accesses the m_ebfd
of current_program_space. */
current_program_space = new program_space (new_address_space ());
}
diff --git a/gdb/progspace.h b/gdb/progspace.h
index f55444ce88c..a5e5f0ea3f5 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -304,13 +304,19 @@ struct program_space
/* Return the exec BFD for this program space. */
bfd *exec_bfd () const
- { return ebfd.get (); }
+ { return m_ebfd.get (); }
/* Set the exec BFD for this program space to ABFD. This also
updates the ebfd_mtime and set the exec filename. */
void set_exec_bfd (gdb_bfd_ref_ptr &&abfd,
gdb::unique_xmalloc_ptr<char> new_filename);
+ /* Return the saved mtime of the exec BFD. */
+ long exec_bfd_mtime () const
+ {
+ return m_ebfd_mtime;
+ }
+
/* Reset saved solib data at the start of an solib event. This lets
us properly collect the data when calling solib_add, so it can then
later be printed. */
@@ -367,14 +373,6 @@ struct program_space
/* Unique ID number. */
int num = 0;
- /* The main executable loaded into this program space. This is
- managed by the exec target. */
-
- /* The BFD handle for the main executable. */
- gdb_bfd_ref_ptr ebfd;
- /* The last-modified time, from when the exec was brought in. */
- long ebfd_mtime = 0;
-
/* The address space attached to this program space. More than one
program space may be bound to the same address space. In the
traditional unix-like debugging scenario, this will usually
@@ -419,6 +417,13 @@ struct program_space
registry<program_space> registry_fields;
private:
+ /* The main executable loaded into this program space. This is
+ managed by the exec target. */
+ /* The BFD handle for the main executable. */
+ gdb_bfd_ref_ptr m_ebfd;
+ /* The last-modified time, from when the exec was brought in. */
+ long m_ebfd_mtime = 0;
+
/* All known objfiles are kept in a linked list. */
owning_intrusive_list<objfile> m_objfiles_list;
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index 804a6a08a8c..c2e98cae168 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -114,7 +114,7 @@ source_cache::get_plain_source_lines (struct symtab *s,
&& s->compunit ().objfile ()->obfd != NULL)
mtime = s->compunit ().objfile ()->mtime;
else if (current_program_space->exec_bfd ())
- mtime = current_program_space->ebfd_mtime;
+ mtime = current_program_space->exec_bfd_mtime ();
if (mtime && mtime < st.st_mtime)
warning (_("Source file is more recent than executable."));
--
2.49.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 5/5] Add some uses of bool to progspace and exec_file_attach
2026-11-11 2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
` (3 preceding siblings ...)
2026-11-11 2:06 ` [PATCH 4/5] Mark two program_space fields as private Tom Tromey
@ 2026-11-11 2:06 ` Tom Tromey
4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2026-11-11 2:06 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
This changes the program_space fields 'executing_startup' and
'breakpoints_not_allowed' to be bool; and changes exec_file_attach to
use bool. These are just some minor cleanups I noticed while working
on this series.
---
gdb/breakpoint.c | 4 ++--
gdb/exec.c | 4 ++--
gdb/infrun.c | 2 +-
gdb/progspace.h | 4 ++--
4 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index a23f5998b20..7a1be7f01f8 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -8693,14 +8693,14 @@ enable_watchpoints_after_interactive_call_stop (void)
void
disable_breakpoints_before_startup (void)
{
- current_program_space->executing_startup = 1;
+ current_program_space->executing_startup = true;
update_global_location_list (UGLL_DONT_INSERT);
}
void
enable_breakpoints_after_startup (void)
{
- current_program_space->executing_startup = 0;
+ current_program_space->executing_startup = false;
breakpoint_re_set ();
}
diff --git a/gdb/exec.c b/gdb/exec.c
index 000b1170145..9e5495ed953 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -400,7 +400,7 @@ exec_file_attach (const char *filename, int from_tty)
}
else
{
- int load_via_target = 0;
+ bool load_via_target = false;
const char *scratch_pathname, *canonical_pathname;
int scratch_chan;
char **matching;
@@ -410,7 +410,7 @@ exec_file_attach (const char *filename, int from_tty)
if (target_filesystem_is_local ())
filename += strlen (TARGET_SYSROOT_PREFIX);
else
- load_via_target = 1;
+ load_via_target = true;
}
gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 08fac30e9e8..a2ea1e39e58 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1235,7 +1235,7 @@ handle_vfork_done (thread_info *event_thread)
gdb_assert (event_thread->inf->thread_waiting_for_vfork_done == event_thread);
event_thread->inf->thread_waiting_for_vfork_done = nullptr;
- event_thread->inf->pspace->breakpoints_not_allowed = 0;
+ event_thread->inf->pspace->breakpoints_not_allowed = false;
/* On non-stop targets, we stopped all the inferior's threads in follow_fork,
resume them now. On all-stop targets, everything that needs to be resumed
diff --git a/gdb/progspace.h b/gdb/progspace.h
index a5e5f0ea3f5..9b97b932eac 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -392,11 +392,11 @@ struct program_space
targets, or for PIE executables, until we connect and ask the
target for the final relocation offsets, the symbols we've used
to set breakpoints point at the wrong addresses. */
- int executing_startup = 0;
+ bool executing_startup = false;
/* True if no breakpoints should be inserted in this program
space. */
- int breakpoints_not_allowed = 0;
+ bool breakpoints_not_allowed = false;
/* The object file that the main symbol table was loaded from
(e.g. the argument to the "symbol-file" or "file" command). */
--
2.49.0
^ permalink raw reply [flat|nested] 6+ messages in thread