PR29262, memory leak in pr_function_type
[binutils-gdb.git] / gdb / target.c
index 3653a7a98f805091fc7fedbc087b60a4ca88bc9e..18e53aa5d2768638513173243d62ec1ac459a5c8 100644 (file)
@@ -1,6 +1,6 @@
 /* Select target systems and architectures at runtime for GDB.
 
-   Copyright (C) 1990-2021 Free Software Foundation, Inc.
+   Copyright (C) 1990-2022 Free Software Foundation, Inc.
 
    Contributed by Cygnus Support.
 
@@ -26,6 +26,7 @@
 #include "symtab.h"
 #include "inferior.h"
 #include "infrun.h"
+#include "observable.h"
 #include "bfd.h"
 #include "symfile.h"
 #include "objfiles.h"
@@ -52,6 +53,7 @@
 #include <unordered_map>
 #include "target-connection.h"
 #include "valprint.h"
+#include "cli/cli-decode.h"
 
 static void generic_tls_error (void) ATTRIBUTE_NORETURN;
 
@@ -66,7 +68,7 @@ static int default_region_ok_for_hw_watchpoint (struct target_ops *,
 static void default_rcmd (struct target_ops *, const char *, struct ui_file *);
 
 static ptid_t default_get_ada_task_ptid (struct target_ops *self,
-                                        long lwp, long tid);
+                                        long lwp, ULONGEST tid);
 
 static void default_mourn_inferior (struct target_ops *self);
 
@@ -157,7 +159,7 @@ static void
 show_targetdebug (struct ui_file *file, int from_tty,
                  struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("Target debugging is %s.\n"), value);
+  gdb_printf (file, _("Target debugging is %s.\n"), value);
 }
 
 int
@@ -301,14 +303,6 @@ target_files_info ()
 
 /* See target.h.  */
 
-void
-target_post_startup_inferior (ptid_t ptid)
-{
-  return current_inferior ()->top_target ()->post_startup_inferior (ptid);
-}
-
-/* See target.h.  */
-
 int
 target_insert_fork_catchpoint (int pid)
 {
@@ -390,7 +384,17 @@ target_can_lock_scheduler ()
 bool
 target_can_async_p ()
 {
-  return current_inferior ()->top_target ()->can_async_p ();
+  return target_can_async_p (current_inferior ()->top_target ());
+}
+
+/* See target.h.  */
+
+bool
+target_can_async_p (struct target_ops *target)
+{
+  if (!target_async_permitted)
+    return false;
+  return target->can_async_p ();
 }
 
 /* See target.h.  */
@@ -398,7 +402,9 @@ target_can_async_p ()
 bool
 target_is_async_p ()
 {
-  return current_inferior ()->top_target ()->is_async_p ();
+  bool result = current_inferior ()->top_target ()->is_async_p ();
+  gdb_assert (target_async_permitted || !result);
+  return result;
 }
 
 exec_direction_kind
@@ -417,7 +423,7 @@ target_extra_thread_info (thread_info *tp)
 
 /* See target.h.  */
 
-char *
+const char *
 target_pid_to_exec_file (int pid)
 {
   return current_inferior ()->top_target ()->pid_to_exec_file (pid);
@@ -594,7 +600,7 @@ target_can_execute_reverse ()
 }
 
 ptid_t
-target_get_ada_task_ptid (long lwp, long tid)
+target_get_ada_task_ptid (long lwp, ULONGEST tid)
 {
   return current_inferior ()->top_target ()->get_ada_task_ptid (lwp, tid);
 }
@@ -837,18 +843,18 @@ target_log_command (const char *p)
 static void
 open_target (const char *args, int from_tty, struct cmd_list_element *command)
 {
-  auto *ti = static_cast<target_info *> (get_cmd_context (command));
+  auto *ti = static_cast<target_info *> (command->context ());
   target_open_ftype *func = target_factories[ti];
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog, "-> %s->open (...)\n",
-                       ti->shortname);
+    gdb_printf (gdb_stdlog, "-> %s->open (...)\n",
+               ti->shortname);
 
   func (args, from_tty);
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog, "<- %s->open (%s, %d)\n",
-                       ti->shortname, args, from_tty);
+    gdb_printf (gdb_stdlog, "<- %s->open (%s, %d)\n",
+               ti->shortname, args, from_tty);
 }
 
 /* See target.h.  */
@@ -872,10 +878,10 @@ The first argument is the type or protocol of the target machine.\n\
 Remaining arguments are interpreted by the target protocol.  For more\n\
 information on the arguments for a particular protocol, type\n\
 `help target ' followed by the protocol name."),
-                         &targetlist, "target ", 0, &cmdlist);
+                         &targetlist, 0, &cmdlist);
   c = add_cmd (t.shortname, no_class, t.doc, &targetlist);
-  set_cmd_context (c, (void *) &t);
-  set_cmd_sfunc (c, open_target);
+  c->set_context ((void *) &t);
+  c->func = open_target;
   if (completer != NULL)
     set_cmd_completer (c, completer);
 }
@@ -886,15 +892,15 @@ void
 add_deprecated_target_alias (const target_info &tinfo, const char *alias)
 {
   struct cmd_list_element *c;
-  char *alt;
 
   /* If we use add_alias_cmd, here, we do not get the deprecated warning,
      see PR cli/15104.  */
   c = add_cmd (alias, no_class, tinfo.doc, &targetlist);
-  set_cmd_sfunc (c, open_target);
-  set_cmd_context (c, (void *) &tinfo);
-  alt = xstrprintf ("target %s", tinfo.shortname);
-  deprecate_cmd (c, alt);
+  c->func = open_target;
+  c->set_context ((void *) &tinfo);
+  gdb::unique_xmalloc_ptr<char> alt
+    = xstrprintf ("target %s", tinfo.shortname);
+  deprecate_cmd (c, alt.release ());
 }
 
 /* Stub functions */
@@ -1122,7 +1128,7 @@ noprocess (void)
 static void
 default_terminal_info (struct target_ops *self, const char *args, int from_tty)
 {
-  printf_unfiltered (_("No saved terminal information.\n"));
+  gdb_printf (_("No saved terminal information.\n"));
 }
 
 /* A default implementation for the to_get_ada_task_ptid target method.
@@ -1132,7 +1138,7 @@ default_terminal_info (struct target_ops *self, const char *args, int from_tty)
    inferior_ptid.  */
 
 static ptid_t
-default_get_ada_task_ptid (struct target_ops *self, long lwp, long tid)
+default_get_ada_task_ptid (struct target_ops *self, long lwp, ULONGEST tid)
 {
   return ptid_t (inferior_ptid.pid (), lwp, tid);
 }
@@ -1214,7 +1220,7 @@ target_stack::unpush (target_ops *t)
   m_stack[stratum] = NULL;
 
   if (m_top == stratum)
-    m_top = t->beneath ()->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,
@@ -1234,9 +1240,9 @@ unpush_target_and_assert (struct target_ops *target)
 {
   if (!current_inferior ()->unpush_target (target))
     {
-      fprintf_unfiltered (gdb_stderr,
-                         "pop_all_targets couldn't find target %s\n",
-                         target->shortname ());
+      gdb_printf (gdb_stderr,
+                 "pop_all_targets couldn't find target %s\n",
+                 target->shortname ());
       internal_error (__FILE__, __LINE__,
                      _("failed internal consistency check"));
     }
@@ -1290,6 +1296,11 @@ target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
   struct target_ops *target = current_inferior ()->top_target ();
   struct gdbarch *gdbarch = target_gdbarch ();
 
+  /* If OBJFILE is a separate debug object file, look for the
+     original object file.  */
+  if (objfile->separate_debug_objfile_backlink != NULL)
+    objfile = objfile->separate_debug_objfile_backlink;
+
   if (gdbarch_fetch_tls_load_module_address_p (gdbarch))
     {
       ptid_t ptid = inferior_ptid;
@@ -1383,26 +1394,6 @@ target_xfer_status_to_string (enum target_xfer_status status)
 };
 
 
-/* See target.h.  */
-
-gdb::unique_xmalloc_ptr<char>
-target_read_string (CORE_ADDR memaddr, int len, int *bytes_read)
-{
-  gdb::unique_xmalloc_ptr<gdb_byte> buffer;
-
-  int ignore;
-  if (bytes_read == nullptr)
-    bytes_read = &ignore;
-
-  /* Note that the endian-ness does not matter here.  */
-  int errcode = read_string (memaddr, -1, 1, len, BFD_ENDIAN_LITTLE,
-                            &buffer, bytes_read);
-  if (errcode != 0)
-    return {};
-
-  return gdb::unique_xmalloc_ptr<char> ((char *) buffer.release ());
-}
-
 const target_section_table *
 target_get_section_table (struct target_ops *target)
 {
@@ -1743,17 +1734,17 @@ target_xfer_partial (struct target_ops *ops,
     {
       const unsigned char *myaddr = NULL;
 
-      fprintf_unfiltered (gdb_stdlog,
-                         "%s:target_xfer_partial "
-                         "(%d, %s, %s, %s, %s, %s) = %d, %s",
-                         ops->shortname (),
-                         (int) object,
-                         (annex ? annex : "(null)"),
-                         host_address_to_string (readbuf),
-                         host_address_to_string (writebuf),
-                         core_addr_to_string_nz (offset),
-                         pulongest (len), retval,
-                         pulongest (*xfered_len));
+      gdb_printf (gdb_stdlog,
+                 "%s:target_xfer_partial "
+                 "(%d, %s, %s, %s, %s, %s) = %d, %s",
+                 ops->shortname (),
+                 (int) object,
+                 (annex ? annex : "(null)"),
+                 host_address_to_string (readbuf),
+                 host_address_to_string (writebuf),
+                 core_addr_to_string_nz (offset),
+                 pulongest (len), retval,
+                 pulongest (*xfered_len));
 
       if (readbuf)
        myaddr = readbuf;
@@ -1763,24 +1754,24 @@ target_xfer_partial (struct target_ops *ops,
        {
          int i;
 
-         fputs_unfiltered (", bytes =", gdb_stdlog);
+         gdb_puts (", bytes =", gdb_stdlog);
          for (i = 0; i < *xfered_len; i++)
            {
              if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
                {
                  if (targetdebug < 2 && i > 0)
                    {
-                     fprintf_unfiltered (gdb_stdlog, " ...");
+                     gdb_printf (gdb_stdlog, " ...");
                      break;
                    }
-                 fprintf_unfiltered (gdb_stdlog, "\n");
+                 gdb_printf (gdb_stdlog, "\n");
                }
 
-             fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
+             gdb_printf (gdb_stdlog, " %02x", myaddr[i] & 0xff);
            }
        }
 
-      fputc_unfiltered ('\n', gdb_stdlog);
+      gdb_putc ('\n', gdb_stdlog);
     }
 
   /* Check implementations of to_xfer_partial update *XFERED_LEN
@@ -1956,9 +1947,9 @@ static void
 show_trust_readonly (struct ui_file *file, int from_tty,
                     struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file,
-                   _("Mode for reading from readonly sections is %s.\n"),
-                   value);
+  gdb_printf (file,
+             _("Mode for reading from readonly sections is %s.\n"),
+             value);
 }
 
 /* Target vector read/write partial wrapper functions.  */
@@ -2439,8 +2430,8 @@ info_target_command (const char *args, int from_tty)
   if (current_program_space->symfile_object_file != NULL)
     {
       objfile *objf = current_program_space->symfile_object_file;
-      printf_unfiltered (_("Symbols from \"%s\".\n"),
-                        objfile_name (objf));
+      gdb_printf (_("Symbols from \"%s\".\n"),
+                 objfile_name (objf));
     }
 
   for (target_ops *t = current_inferior ()->top_target ();
@@ -2453,9 +2444,9 @@ info_target_command (const char *args, int from_tty)
       if ((int) (t->stratum ()) <= (int) dummy_stratum)
        continue;
       if (has_all_mem)
-       printf_unfiltered (_("\tWhile running this, "
-                            "GDB does not access memory from...\n"));
-      printf_unfiltered ("%s:\n", t->longname ());
+       gdb_printf (_("\tWhile running this, "
+                     "GDB does not access memory from...\n"));
+      gdb_printf ("%s:\n", t->longname ());
       t->files_info ();
       has_all_mem = t->has_all_memory ();
     }
@@ -2597,11 +2588,25 @@ target_wait (ptid_t ptid, struct target_waitstatus *status,
             target_wait_flags options)
 {
   target_ops *target = current_inferior ()->top_target ();
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+
+  gdb_assert (!proc_target->commit_resumed_state);
 
-  if (!target->can_async_p ())
+  if (!target_can_async_p (target))
     gdb_assert ((options & TARGET_WNOHANG) == 0);
 
-  return target->wait (ptid, status, options);
+  try
+    {
+      gdb::observers::target_pre_wait.notify (ptid);
+      ptid_t event_ptid = target->wait (ptid, status, options);
+      gdb::observers::target_post_wait.notify (event_ptid);
+      return event_ptid;
+    }
+  catch (...)
+    {
+      gdb::observers::target_post_wait.notify (null_ptid);
+      throw;
+    }
 }
 
 /* See target.h.  */
@@ -2611,7 +2616,7 @@ default_target_wait (struct target_ops *ops,
                     ptid_t ptid, struct target_waitstatus *status,
                     target_wait_flags options)
 {
-  status->kind = TARGET_WAITKIND_IGNORE;
+  status->set_ignore ();
   return minus_one_ptid;
 }
 
@@ -2650,42 +2655,44 @@ target_thread_info_to_thread_handle (struct thread_info *tip)
 }
 
 void
-target_resume (ptid_t ptid, int step, enum gdb_signal signal)
+target_resume (ptid_t scope_ptid, int step, enum gdb_signal signal)
 {
   process_stratum_target *curr_target = current_inferior ()->process_target ();
+  gdb_assert (!curr_target->commit_resumed_state);
+
+  gdb_assert (inferior_ptid != null_ptid);
+  gdb_assert (inferior_ptid.matches (scope_ptid));
 
   target_dcache_invalidate ();
 
-  current_inferior ()->top_target ()->resume (ptid, step, signal);
+  current_inferior ()->top_target ()->resume (scope_ptid, step, signal);
 
-  registers_changed_ptid (curr_target, ptid);
+  registers_changed_ptid (curr_target, scope_ptid);
   /* We only set the internal executing state here.  The user/frontend
      running state is set at a higher level.  This also clears the
      thread's stop_pc as side effect.  */
-  set_executing (curr_target, ptid, true);
-  clear_inline_frame_state (curr_target, ptid);
-}
+  set_executing (curr_target, scope_ptid, true);
+  clear_inline_frame_state (curr_target, scope_ptid);
 
-/* If true, target_commit_resume is a nop.  */
-static int defer_target_commit_resume;
+  if (target_can_async_p ())
+    target_async (1);
+}
 
 /* See target.h.  */
 
 void
-target_commit_resume (void)
+target_commit_resumed ()
 {
-  if (defer_target_commit_resume)
-    return;
-
-  current_inferior ()->top_target ()->commit_resume ();
+  gdb_assert (current_inferior ()->process_target ()->commit_resumed_state);
+  current_inferior ()->top_target ()->commit_resumed ();
 }
 
 /* See target.h.  */
 
-scoped_restore_tmpl<int>
-make_scoped_defer_target_commit_resume ()
+bool
+target_has_pending_events ()
 {
-  return make_scoped_restore (&defer_target_commit_resume, 1);
+  return current_inferior ()->top_target ()->has_pending_events ();
 }
 
 void
@@ -2700,32 +2707,47 @@ target_program_signals (gdb::array_view<const unsigned char> program_signals)
   current_inferior ()->top_target ()->program_signals (program_signals);
 }
 
-static bool
-default_follow_fork (struct target_ops *self, bool follow_child,
-                    bool detach_fork)
+static void
+default_follow_fork (struct target_ops *self, inferior *child_inf,
+                    ptid_t child_ptid, target_waitkind fork_kind,
+                    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"));
 }
 
-/* Look through the list of possible targets for a target that can
-   follow forks.  */
+/* See target.h.  */
 
-bool
-target_follow_fork (bool follow_child, bool detach_fork)
+void
+target_follow_fork (inferior *child_inf, ptid_t child_ptid,
+                   target_waitkind fork_kind, bool follow_child,
+                   bool detach_fork)
 {
   target_ops *target = current_inferior ()->top_target ();
 
-  return target->follow_fork (follow_child, detach_fork);
+  /* Check consistency between CHILD_INF, CHILD_PTID, FOLLOW_CHILD and
+     DETACH_FORK.  */
+  if (child_inf != nullptr)
+    {
+      gdb_assert (follow_child || !detach_fork);
+      gdb_assert (child_inf->pid == child_ptid.pid ());
+    }
+  else
+    gdb_assert (!follow_child && detach_fork);
+
+  return target->follow_fork (child_inf, child_ptid, fork_kind, follow_child,
+                             detach_fork);
 }
 
-/* Target wrapper for follow exec hook.  */
+/* See target.h.  */
 
 void
-target_follow_exec (struct inferior *inf, const char *execd_pathname)
+target_follow_exec (inferior *follow_inf, ptid_t ptid,
+                   const char *execd_pathname)
 {
-  current_inferior ()->top_target ()->follow_exec (inf, execd_pathname);
+  current_inferior ()->top_target ()->follow_exec (follow_inf, ptid,
+                                                  execd_pathname);
 }
 
 static void
@@ -2838,10 +2860,10 @@ static void
 show_auto_connect_native_target (struct ui_file *file, int from_tty,
                                 struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file,
-                   _("Whether GDB may automatically connect to the "
-                     "native target is %s.\n"),
-                   value);
+  gdb_printf (file,
+             _("Whether GDB may automatically connect to the "
+               "native target is %s.\n"),
+             value);
 }
 
 /* A pointer to the target that can respond to "run" or "attach".
@@ -2949,8 +2971,8 @@ target_info_proc (const char *args, enum info_proc_what what)
       if (t->info_proc (args, what))
        {
          if (targetdebug)
-           fprintf_unfiltered (gdb_stdlog,
-                               "target_info_proc (\"%s\", %d)\n", args, what);
+           gdb_printf (gdb_stdlog,
+                       "target_info_proc (\"%s\", %d)\n", args, what);
 
          return 1;
        }
@@ -3120,13 +3142,9 @@ static std::vector<fileio_fh_t> fileio_fhandles;
    list each time a new file is opened.  */
 static int lowest_closed_fd;
 
-/* Invalidate the target associated with open handles that were open
-   on target TARG, since we're about to close (and maybe destroy) the
-   target.  The handles remain open from the client's perspective, but
-   trying to do anything with them other than closing them will fail
-   with EIO.  */
+/* See target.h.  */
 
-static void
+void
 fileio_handles_invalidate_target (target_ops *targ)
 {
   for (fileio_fh_t &fh : fileio_fhandles)
@@ -3260,13 +3278,13 @@ target_fileio_open (struct inferior *inf, const char *filename,
        fd = acquire_fileio_fd (t, fd);
 
       if (targetdebug)
-       fprintf_unfiltered (gdb_stdlog,
-                               "target_fileio_open (%d,%s,0x%x,0%o,%d)"
-                               " = %d (%d)\n",
-                               inf == NULL ? 0 : inf->num,
-                               filename, flags, mode,
-                               warn_if_slow, fd,
-                               fd != -1 ? 0 : *target_errno);
+       gdb_printf (gdb_stdlog,
+                   "target_fileio_open (%d,%s,0x%x,0%o,%d)"
+                   " = %d (%d)\n",
+                   inf == NULL ? 0 : inf->num,
+                   filename, flags, mode,
+                   warn_if_slow, fd,
+                   fd != -1 ? 0 : *target_errno);
       return fd;
     }
 
@@ -3292,11 +3310,11 @@ target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
                                     len, offset, target_errno);
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog,
-                       "target_fileio_pwrite (%d,...,%d,%s) "
-                       "= %d (%d)\n",
-                       fd, len, pulongest (offset),
-                       ret, ret != -1 ? 0 : *target_errno);
+    gdb_printf (gdb_stdlog,
+               "target_fileio_pwrite (%d,...,%d,%s) "
+               "= %d (%d)\n",
+               fd, len, pulongest (offset),
+               ret, ret != -1 ? 0 : *target_errno);
   return ret;
 }
 
@@ -3318,11 +3336,11 @@ target_fileio_pread (int fd, gdb_byte *read_buf, int len,
                                    len, offset, target_errno);
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog,
-                       "target_fileio_pread (%d,...,%d,%s) "
-                       "= %d (%d)\n",
-                       fd, len, pulongest (offset),
-                       ret, ret != -1 ? 0 : *target_errno);
+    gdb_printf (gdb_stdlog,
+               "target_fileio_pread (%d,...,%d,%s) "
+               "= %d (%d)\n",
+               fd, len, pulongest (offset),
+               ret, ret != -1 ? 0 : *target_errno);
   return ret;
 }
 
@@ -3342,9 +3360,9 @@ target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
     ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno);
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog,
-                       "target_fileio_fstat (%d) = %d (%d)\n",
-                       fd, ret, ret != -1 ? 0 : *target_errno);
+    gdb_printf (gdb_stdlog,
+               "target_fileio_fstat (%d) = %d (%d)\n",
+               fd, ret, ret != -1 ? 0 : *target_errno);
   return ret;
 }
 
@@ -3369,9 +3387,9 @@ target_fileio_close (int fd, int *target_errno)
     }
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog,
-                       "target_fileio_close (%d) = %d (%d)\n",
-                       fd, ret, ret != -1 ? 0 : *target_errno);
+    gdb_printf (gdb_stdlog,
+               "target_fileio_close (%d) = %d (%d)\n",
+               fd, ret, ret != -1 ? 0 : *target_errno);
   return ret;
 }
 
@@ -3389,11 +3407,11 @@ target_fileio_unlink (struct inferior *inf, const char *filename,
        continue;
 
       if (targetdebug)
-       fprintf_unfiltered (gdb_stdlog,
-                           "target_fileio_unlink (%d,%s)"
-                           " = %d (%d)\n",
-                           inf == NULL ? 0 : inf->num, filename,
-                           ret, ret != -1 ? 0 : *target_errno);
+       gdb_printf (gdb_stdlog,
+                   "target_fileio_unlink (%d,%s)"
+                   " = %d (%d)\n",
+                   inf == NULL ? 0 : inf->num, filename,
+                   ret, ret != -1 ? 0 : *target_errno);
       return ret;
     }
 
@@ -3416,12 +3434,12 @@ target_fileio_readlink (struct inferior *inf, const char *filename,
        continue;
 
       if (targetdebug)
-       fprintf_unfiltered (gdb_stdlog,
-                           "target_fileio_readlink (%d,%s)"
-                           " = %s (%d)\n",
-                           inf == NULL ? 0 : inf->num,
-                           filename, ret ? ret->c_str () : "(nil)",
-                           ret ? 0 : *target_errno);
+       gdb_printf (gdb_stdlog,
+                   "target_fileio_readlink (%d,%s)"
+                   " = %s (%d)\n",
+                   inf == NULL ? 0 : inf->num,
+                   filename, ret ? ret->c_str () : "(nil)",
+                   ret ? 0 : *target_errno);
       return ret;
     }
 
@@ -3613,13 +3631,32 @@ target_announce_detach (int from_tty)
   if (!from_tty)
     return;
 
+  pid = inferior_ptid.pid ();
   exec_file = get_exec_file (0);
-  if (exec_file == NULL)
-    exec_file = "";
+  if (exec_file == nullptr)
+    gdb_printf ("Detaching from pid %s\n",
+               target_pid_to_str (ptid_t (pid)).c_str ());
+  else
+    gdb_printf (_("Detaching from program: %s, %s\n"), exec_file,
+               target_pid_to_str (ptid_t (pid)).c_str ());
+}
 
-  pid = inferior_ptid.pid ();
-  printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
-                    target_pid_to_str (ptid_t (pid)).c_str ());
+/* See target.h  */
+
+void
+target_announce_attach (int from_tty, int pid)
+{
+  if (!from_tty)
+    return;
+
+  const char *exec_file = get_exec_file (0);
+
+  if (exec_file != nullptr)
+    gdb_printf ("Attaching to program: %s, %s\n", exec_file,
+               target_pid_to_str (ptid_t (pid)).c_str ());
+  else
+    gdb_printf ("Attaching to %s\n",
+               target_pid_to_str (ptid_t (pid)).c_str ());
 }
 
 /* The inferior process has died.  Long live the inferior!  */
@@ -3736,14 +3773,15 @@ debug_target::info () const
 void
 target_close (struct target_ops *targ)
 {
-  gdb_assert (!current_inferior ()->target_is_pushed (targ));
+  for (inferior *inf : all_inferiors ())
+    gdb_assert (!inf->target_is_pushed (targ));
 
   fileio_handles_invalidate_target (targ);
 
   targ->close ();
 
   if (targetdebug)
-    fprintf_unfiltered (gdb_stdlog, "target_close ()\n");
+    gdb_printf (gdb_stdlog, "target_close ()\n");
 }
 
 int
@@ -3761,6 +3799,10 @@ target_update_thread_list (void)
 void
 target_stop (ptid_t ptid)
 {
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+
+  gdb_assert (!proc_target->commit_resumed_state);
+
   if (!may_stop)
     {
       warning (_("May not interrupt or stop the target, ignoring attempt"));
@@ -3799,7 +3841,7 @@ target_pass_ctrlc (void)
        {
          /* A thread can be THREAD_STOPPED and executing, while
             running an infcall.  */
-         if (thr->state == THREAD_RUNNING || thr->executing)
+         if (thr->state == THREAD_RUNNING || thr->executing ())
            {
              /* We can get here quite deep in target layers.  Avoid
                 switching thread context or anything that would
@@ -3835,7 +3877,6 @@ target_stop_and_wait (ptid_t ptid)
   non_stop = true;
   target_stop (ptid);
 
-  memset (&status, 0, sizeof (status));
   target_wait (ptid, &status, 0);
 
   non_stop = was_non_stop;
@@ -4022,9 +4063,9 @@ target_ranged_break_num_registers (void)
 /* See target.h.  */
 
 struct btrace_target_info *
-target_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
+target_enable_btrace (thread_info *tp, const struct btrace_config *conf)
 {
-  return current_inferior ()->top_target ()->enable_btrace (ptid, conf);
+  return current_inferior ()->top_target ()->enable_btrace (tp, conf);
 }
 
 /* See target.h.  */
@@ -4296,7 +4337,7 @@ flash_erase_command (const char *cmd, int from_tty)
 static void
 maintenance_print_target_stack (const char *cmd, int from_tty)
 {
-  printf_filtered (_("The current target stack is:\n"));
+  gdb_printf (_("The current target stack is:\n"));
 
   for (target_ops *t = current_inferior ()->top_target ();
        t != NULL;
@@ -4304,7 +4345,7 @@ maintenance_print_target_stack (const char *cmd, int from_tty)
     {
       if (t->stratum () == debug_stratum)
        continue;
-      printf_filtered ("  - %s (%s)\n", t->shortname (), t->longname ());
+      gdb_printf ("  - %s (%s)\n", t->shortname (), t->longname ());
     }
 }
 
@@ -4313,6 +4354,9 @@ maintenance_print_target_stack (const char *cmd, int from_tty)
 void
 target_async (int enable)
 {
+  /* If we are trying to enable async mode then it must be the case that
+     async mode is possible for this target.  */
+  gdb_assert (!enable || target_can_async_p ());
   infrun_async (enable);
   current_inferior ()->top_target ()->async (enable);
 }
@@ -4329,31 +4373,28 @@ target_thread_events (int enable)
    just for maintainers to use when debugging gdb.  */
 bool target_async_permitted = true;
 
-/* The set command writes to this variable.  If the inferior is
-   executing, target_async_permitted is *not* updated.  */
-static bool target_async_permitted_1 = true;
-
 static void
-maint_set_target_async_command (const char *args, int from_tty,
-                               struct cmd_list_element *c)
+set_maint_target_async (bool permitted)
 {
   if (have_live_inferiors ())
-    {
-      target_async_permitted_1 = target_async_permitted;
-      error (_("Cannot change this setting while the inferior is running."));
-    }
+    error (_("Cannot change this setting while the inferior is running."));
+
+  target_async_permitted = permitted;
+}
 
-  target_async_permitted = target_async_permitted_1;
+static bool
+get_maint_target_async ()
+{
+  return target_async_permitted;
 }
 
 static void
-maint_show_target_async_command (struct ui_file *file, int from_tty,
-                                struct cmd_list_element *c,
-                                const char *value)
+show_maint_target_async (ui_file *file, int from_tty,
+                        cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file,
-                   _("Controlling the inferior in "
-                     "asynchronous mode is %s.\n"), value);
+  gdb_printf (file,
+             _("Controlling the inferior in "
+               "asynchronous mode is %s.\n"), value);
 }
 
 /* Return true if the target operates in non-stop mode even with "set
@@ -4370,10 +4411,11 @@ target_always_non_stop_p (void)
 bool
 target_is_non_stop_p ()
 {
-  return (non_stop
-         || target_non_stop_enabled == AUTO_BOOLEAN_TRUE
-         || (target_non_stop_enabled == AUTO_BOOLEAN_AUTO
-             && target_always_non_stop_p ()));
+  return ((non_stop
+          || target_non_stop_enabled == AUTO_BOOLEAN_TRUE
+          || (target_non_stop_enabled == AUTO_BOOLEAN_AUTO
+              && target_always_non_stop_p ()))
+         && target_can_async_p ());
 }
 
 /* See target.h.  */
@@ -4400,41 +4442,38 @@ exists_non_stop_target ()
    mode.  This is just for maintainers to use when debugging gdb.  */
 enum auto_boolean target_non_stop_enabled = AUTO_BOOLEAN_AUTO;
 
-/* The set command writes to this variable.  If the inferior is
-   executing, target_non_stop_enabled is *not* updated.  */
-static enum auto_boolean target_non_stop_enabled_1 = AUTO_BOOLEAN_AUTO;
-
-/* Implementation of "maint set target-non-stop".  */
+/* Set callback for maint target-non-stop setting.  */
 
 static void
-maint_set_target_non_stop_command (const char *args, int from_tty,
-                                  struct cmd_list_element *c)
+set_maint_target_non_stop (auto_boolean enabled)
 {
   if (have_live_inferiors ())
-    {
-      target_non_stop_enabled_1 = target_non_stop_enabled;
-      error (_("Cannot change this setting while the inferior is running."));
-    }
+    error (_("Cannot change this setting while the inferior is running."));
 
-  target_non_stop_enabled = target_non_stop_enabled_1;
+  target_non_stop_enabled = enabled;
 }
 
-/* Implementation of "maint show target-non-stop".  */
+/* Get callback for maint target-non-stop setting.  */
+
+static auto_boolean
+get_maint_target_non_stop ()
+{
+  return target_non_stop_enabled;
+}
 
 static void
-maint_show_target_non_stop_command (struct ui_file *file, int from_tty,
-                                   struct cmd_list_element *c,
-                                   const char *value)
+show_maint_target_non_stop (ui_file *file, int from_tty,
+                           cmd_list_element *c, const char *value)
 {
   if (target_non_stop_enabled == AUTO_BOOLEAN_AUTO)
-    fprintf_filtered (file,
-                     _("Whether the target is always in non-stop mode "
-                       "is %s (currently %s).\n"), value,
-                     target_always_non_stop_p () ? "on" : "off");
+    gdb_printf (file,
+               _("Whether the target is always in non-stop mode "
+                 "is %s (currently %s).\n"), value,
+               target_always_non_stop_p () ? "on" : "off");
   else
-    fprintf_filtered (file,
-                     _("Whether the target is always in non-stop mode "
-                       "is %s.\n"), value);
+    gdb_printf (file,
+               _("Whether the target is always in non-stop mode "
+                 "is %s.\n"), value);
 }
 
 /* Temporary copies of permission settings.  */
@@ -4530,22 +4569,24 @@ result in significant performance improvement for remote targets."),
           &maintenanceprintlist);
 
   add_setshow_boolean_cmd ("target-async", no_class,
-                          &target_async_permitted_1, _("\
+                          _("\
 Set whether gdb controls the inferior in asynchronous mode."), _("\
 Show whether gdb controls the inferior in asynchronous mode."), _("\
 Tells gdb whether to control the inferior in asynchronous mode."),
-                          maint_set_target_async_command,
-                          maint_show_target_async_command,
+                          set_maint_target_async,
+                          get_maint_target_async,
+                          show_maint_target_async,
                           &maintenance_set_cmdlist,
                           &maintenance_show_cmdlist);
 
   add_setshow_auto_boolean_cmd ("target-non-stop", no_class,
-                               &target_non_stop_enabled_1, _("\
+                               _("\
 Set whether gdb always controls the inferior in non-stop mode."), _("\
 Show whether gdb always controls the inferior in non-stop mode."), _("\
 Tells gdb whether to control the inferior in non-stop mode."),
-                          maint_set_target_non_stop_command,
-                          maint_show_target_non_stop_command,
+                          set_maint_target_non_stop,
+                          get_maint_target_non_stop,
+                          show_maint_target_non_stop,
                           &maintenance_set_cmdlist,
                           &maintenance_show_cmdlist);