/* Select target systems and architectures at runtime for GDB.
- Copyright (C) 1990-2022 Free Software Foundation, Inc.
+ Copyright (C) 1990-2023 Free Software Foundation, Inc.
Contributed by Cygnus Support.
auto &func_slot = target_factories[&t];
if (func_slot != nullptr)
- internal_error (__FILE__, __LINE__,
- _("target already added (\"%s\")."), t.shortname);
+ internal_error (_("target already added (\"%s\")."), t.shortname);
func_slot = func;
if (targetlist == NULL)
void
target_kill (void)
{
+
+ /* If the commit_resume_state of the to-be-killed-inferior's process stratum
+ is true, and this inferior is the last live inferior with resumed threads
+ of that target, then we want to leave commit_resume_state to false, as the
+ target won't have any resumed threads anymore. We achieve this with
+ this scoped_disable_commit_resumed. On construction, it will set the flag
+ to false. On destruction, it will only set it to true if there are resumed
+ threads left. */
+ scoped_disable_commit_resumed disable ("killing");
current_inferior ()->top_target ()->kill ();
}
/* See target.h. */
void
-decref_target (target_ops *t)
+target_ops_ref_policy::decref (target_ops *t)
{
t->decref ();
if (t->refcount () == 0)
void
target_stack::push (target_ops *t)
{
- t->incref ();
+ /* We must create a new reference first. It is possible that T is
+ already pushed on this target stack, in which case we will first
+ unpush it below, before re-pushing it. If we don't increment the
+ reference count now, then when we unpush it, we might end up deleting
+ T, which is not good. */
+ auto ref = target_ops_ref::new_reference (t);
strata stratum = t->stratum ();
- if (stratum == process_stratum)
- connection_list_add (as_process_stratum_target (t));
-
/* If there's already a target at this stratum, remove it. */
- if (m_stack[stratum] != NULL)
- unpush (m_stack[stratum]);
+ if (m_stack[stratum].get () != nullptr)
+ unpush (m_stack[stratum].get ());
/* Now add the new one. */
- m_stack[stratum] = t;
+ m_stack[stratum] = std::move (ref);
if (m_top < stratum)
m_top = stratum;
+
+ if (stratum == process_stratum)
+ connection_list_add (as_process_stratum_target (t));
}
/* See target.h. */
strata stratum = t->stratum ();
if (stratum == dummy_stratum)
- internal_error (__FILE__, __LINE__,
- _("Attempt to unpush the dummy target"));
+ internal_error (_("Attempt to unpush the dummy target"));
/* Look for the specified target. Note that a target can only occur
once in the target stack. */
return false;
}
- /* Unchain the target. */
- m_stack[stratum] = NULL;
-
if (m_top == stratum)
m_top = this->find_beneath (t)->stratum ();
- /* Finally close the target, if there are no inferiors
- referencing this target still. Note we do this after unchaining,
- so any target method calls from within the target_close
- implementation don't end up in T anymore. Do leave the target
- open if we have are other inferiors referencing this target
- still. */
- decref_target (t);
-
- return true;
-}
-
-/* Unpush TARGET and assert that it worked. */
-
-static void
-unpush_target_and_assert (struct target_ops *target)
-{
- if (!current_inferior ()->unpush_target (target))
- {
- gdb_printf (gdb_stderr,
- "pop_all_targets couldn't find target %s\n",
- target->shortname ());
- internal_error (__FILE__, __LINE__,
- _("failed internal consistency check"));
- }
-}
-
-void
-pop_all_targets_above (enum strata above_stratum)
-{
- while ((int) (current_inferior ()->top_target ()->stratum ())
- > (int) above_stratum)
- unpush_target_and_assert (current_inferior ()->top_target ());
-}
-
-/* See target.h. */
+ /* Move the target reference off the target stack, this sets the pointer
+ held in m_stack to nullptr, and places the reference in ref. When
+ ref goes out of scope its reference count will be decremented, which
+ might cause the target to close.
-void
-pop_all_targets_at_and_above (enum strata stratum)
-{
- while ((int) (current_inferior ()->top_target ()->stratum ())
- >= (int) stratum)
- unpush_target_and_assert (current_inferior ()->top_target ());
-}
+ We have to do it this way, and not just set the value in m_stack to
+ nullptr directly, because doing so would decrement the reference
+ count first, which might close the target, and closing the target
+ does a check that the target is not on any inferiors target_stack. */
+ auto ref = std::move (m_stack[stratum]);
-void
-pop_all_targets (void)
-{
- pop_all_targets_above (dummy_stratum);
+ return true;
}
void
if (len == 0)
return TARGET_XFER_EOF;
- memaddr = address_significant (target_gdbarch (), memaddr);
+ memaddr = gdbarch_remove_non_address_bits (target_gdbarch (), memaddr);
/* Fill in READBUF with breakpoint shadows, or WRITEBUF with
breakpoint insns, thus hiding out from higher layers whether
/* attach_flag may be set if the previous process associated with
the inferior was attached to. */
- current_inferior ()->attach_flag = 0;
+ current_inferior ()->attach_flag = false;
current_inferior ()->highest_thread_num = 0;
it doesn't (which seems like a win for UDI), remove it now. */
/* Leave the exec target, though. The user may be switching from a
live process to a core of the same program. */
- pop_all_targets_above (file_stratum);
+ current_inferior ()->pop_all_targets_above (file_stratum);
target_pre_inferior (from_tty);
}
void
target_detach (inferior *inf, int from_tty)
{
+ /* Thread's don't need to be resumed until the end of this function. */
+ scoped_disable_commit_resumed disable_commit_resumed ("detaching");
+
/* After we have detached, we will clear the register cache for this inferior
by calling registers_changed_ptid. We must save the pid_ptid before
detaching, as the target detach method will clear inf->pid. */
inferior_ptid matches save_pid_ptid, but in our case, it does not
call it, as inferior_ptid has been reset. */
reinit_frame_cache ();
+
+ disable_commit_resumed.reset_and_commit ();
}
void
bool follow_child, bool detach_fork)
{
/* Some target returned a fork event, but did not know how to follow it. */
- internal_error (__FILE__, __LINE__,
- _("could not find a target to follow fork"));
+ internal_error (_("could not find a target to follow fork"));
}
/* See target.h. */
static void
default_mourn_inferior (struct target_ops *self)
{
- internal_error (__FILE__, __LINE__,
- _("could not find a target to follow mourn inferior"));
+ internal_error (_("could not find a target to follow mourn inferior"));
}
void
/* This function is only called if the target is running. In that
case there should have been a process_stratum target and it
should either know how to create inferiors, or not... */
- internal_error (__FILE__, __LINE__, _("No targets found"));
+ internal_error (_("No targets found"));
}
/* Whether GDB is allowed to fall back to the default run target for
set_native_target (target_ops *target)
{
if (the_native_target != NULL)
- internal_error (__FILE__, __LINE__,
- _("native target already set (\"%s\")."),
+ internal_error (_("native target already set (\"%s\")."),
the_native_target->longname ());
the_native_target = target;
int
target_ops::fileio_open (struct inferior *inf, const char *filename,
int flags, int mode, int warn_if_slow,
- int *target_errno)
+ fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return -1;
int
target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
- ULONGEST offset, int *target_errno)
+ ULONGEST offset, fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return -1;
int
target_ops::fileio_pread (int fd, gdb_byte *read_buf, int len,
- ULONGEST offset, int *target_errno)
+ ULONGEST offset, fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return -1;
}
int
-target_ops::fileio_fstat (int fd, struct stat *sb, int *target_errno)
+target_ops::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return -1;
}
int
-target_ops::fileio_close (int fd, int *target_errno)
+target_ops::fileio_close (int fd, fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return -1;
int
target_ops::fileio_unlink (struct inferior *inf, const char *filename,
- int *target_errno)
+ fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return -1;
gdb::optional<std::string>
target_ops::fileio_readlink (struct inferior *inf, const char *filename,
- int *target_errno)
+ fileio_error *target_errno)
{
*target_errno = FILEIO_ENOSYS;
return {};
int
target_fileio_open (struct inferior *inf, const char *filename,
- int flags, int mode, bool warn_if_slow, int *target_errno)
+ int flags, int mode, bool warn_if_slow, fileio_error *target_errno)
{
for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
{
int
target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
- ULONGEST offset, int *target_errno)
+ ULONGEST offset, fileio_error *target_errno)
{
fileio_fh_t *fh = fileio_fd_to_fh (fd);
int ret = -1;
if (fh->is_closed ())
- *target_errno = EBADF;
+ *target_errno = FILEIO_EBADF;
else if (fh->target == NULL)
- *target_errno = EIO;
+ *target_errno = FILEIO_EIO;
else
ret = fh->target->fileio_pwrite (fh->target_fd, write_buf,
len, offset, target_errno);
int
target_fileio_pread (int fd, gdb_byte *read_buf, int len,
- ULONGEST offset, int *target_errno)
+ ULONGEST offset, fileio_error *target_errno)
{
fileio_fh_t *fh = fileio_fd_to_fh (fd);
int ret = -1;
if (fh->is_closed ())
- *target_errno = EBADF;
+ *target_errno = FILEIO_EBADF;
else if (fh->target == NULL)
- *target_errno = EIO;
+ *target_errno = FILEIO_EIO;
else
ret = fh->target->fileio_pread (fh->target_fd, read_buf,
len, offset, target_errno);
/* See target.h. */
int
-target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
+target_fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
{
fileio_fh_t *fh = fileio_fd_to_fh (fd);
int ret = -1;
if (fh->is_closed ())
- *target_errno = EBADF;
+ *target_errno = FILEIO_EBADF;
else if (fh->target == NULL)
- *target_errno = EIO;
+ *target_errno = FILEIO_EIO;
else
ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno);
/* See target.h. */
int
-target_fileio_close (int fd, int *target_errno)
+target_fileio_close (int fd, fileio_error *target_errno)
{
fileio_fh_t *fh = fileio_fd_to_fh (fd);
int ret = -1;
if (fh->is_closed ())
- *target_errno = EBADF;
+ *target_errno = FILEIO_EBADF;
else
{
if (fh->target != NULL)
int
target_fileio_unlink (struct inferior *inf, const char *filename,
- int *target_errno)
+ fileio_error *target_errno)
{
for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
{
gdb::optional<std::string>
target_fileio_readlink (struct inferior *inf, const char *filename,
- int *target_errno)
+ fileio_error *target_errno)
{
for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
{
{
if (m_fd >= 0)
{
- int target_errno;
+ fileio_error target_errno;
target_fileio_close (m_fd, &target_errno);
}
size_t buf_alloc, buf_pos;
gdb_byte *buf;
LONGEST n;
- int target_errno;
+ fileio_error target_errno;
scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY,
0700, false, &target_errno));
{
/* Look for a non-empty slot at stratum levels beneath T's. */
for (int stratum = t->stratum () - 1; stratum >= 0; --stratum)
- if (m_stack[stratum] != NULL)
- return m_stack[stratum];
+ if (m_stack[stratum].get () != NULL)
+ return m_stack[stratum].get ();
return NULL;
}