gdb: set only inferior_ptid in sparc_{fetch,store}_inferior_registers
[binutils-gdb.git] / gdb / linux-thread-db.c
index b3cda05cd6e0bfc6a602044ab5150bf22d3c2f55..9925b02e7780258c611a2ee9e8a9a90bb73ae947 100644 (file)
@@ -1,6 +1,6 @@
 /* libthread_db assisted debugging support, generic parts.
 
-   Copyright (C) 1999-2020 Free Software Foundation, Inc.
+   Copyright (C) 1999-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -92,9 +92,10 @@ public:
   strata stratum () const override { return thread_stratum; }
 
   void detach (inferior *, int) override;
-  ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
+  ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
   void resume (ptid_t, int, enum gdb_signal) override;
   void mourn_inferior () override;
+  void follow_exec (inferior *, ptid_t, const char *) override;
   void update_thread_list () override;
   std::string pid_to_str (ptid_t) override;
   CORE_ADDR get_thread_local_address (ptid_t ptid,
@@ -161,8 +162,6 @@ static thread_db_target the_thread_db_target;
 /* Non-zero if we have determined the signals used by the threads
    library.  */
 static int thread_signals;
-static sigset_t thread_stop_set;
-static sigset_t thread_print_set;
 
 struct thread_db_info
 {
@@ -209,7 +208,7 @@ struct thread_db_info
 
 /* List of known processes using thread_db, and the required
    bookkeeping.  */
-struct thread_db_info *thread_db_list;
+static thread_db_info *thread_db_list;
 
 static void thread_db_find_new_threads_1 (thread_info *stopped);
 static void thread_db_find_new_threads_2 (thread_info *stopped,
@@ -237,7 +236,7 @@ add_thread_db_info (void *handle)
 
   /* The workaround works by reading from /proc/pid/status, so it is
      disabled for core files.  */
-  if (target_has_execution)
+  if (target_has_execution ())
     info->need_stale_parent_threads_check = 1;
 
   info->next = thread_db_list;
@@ -531,7 +530,7 @@ thread_db_find_new_threads_silently (thread_info *stopped)
         corrupted.  For core files it does not apply, no 'later enumeration'
         is possible.  */
 
-      if (!target_has_execution || !inferior_has_bug ("nptl_version", 2, 7))
+      if (!target_has_execution () || !inferior_has_bug ("nptl_version", 2, 7))
        {
          exception_fprintf (gdb_stderr, except,
                             _("Warning: couldn't activate thread debugging "
@@ -658,7 +657,7 @@ check_thread_db_callback (const td_thrhandle_t *th, void *arg)
   memset (&th2, 23, sizeof (td_thrhandle_t));
   CALL_UNCHECKED (td_ta_map_lwp2thr, th->th_ta_p, ti.ti_lid, &th2);
 
-  if (tdb_testinfo->last_result == TD_ERR && !target_has_execution)
+  if (tdb_testinfo->last_result == TD_ERR && !target_has_execution ())
     {
       /* Some platforms require execution for td_ta_map_lwp2thr.  */
       LOG (_("; can't map_lwp2thr"));
@@ -850,19 +849,19 @@ try_thread_db_load_1 (struct thread_db_info *info)
        fprintf_unfiltered (gdb_stdlog, _("td_ta_new failed: %s\n"),
                            thread_db_err_str (err));
       else
-        switch (err)
-          {
-            case TD_NOLIBTHREAD:
+       switch (err)
+         {
+           case TD_NOLIBTHREAD:
 #ifdef THREAD_DB_HAS_TD_VERSION
-            case TD_VERSION:
+           case TD_VERSION:
 #endif
-              /* The errors above are not unexpected and silently ignored:
-                 they just mean we haven't found correct version of
-                 libthread_db yet.  */
-              break;
-            default:
-              warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
-          }
+             /* The errors above are not unexpected and silently ignored:
+                they just mean we haven't found correct version of
+                libthread_db yet.  */
+             break;
+           default:
+             warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
+         }
       return false;
     }
 
@@ -884,7 +883,7 @@ try_thread_db_load_1 (struct thread_db_info *info)
 
      td_ta_map_lwp2thr uses ps_get_thread_area, but we can't use that
      currently on core targets, as it uses ptrace directly.  */
-  if (target_has_execution
+  if (target_has_execution ()
       && linux_proc_task_list_dir_exists (inferior_ptid.pid ()))
     info->td_ta_thr_iter_p = NULL;
   else
@@ -918,8 +917,8 @@ try_thread_db_load_1 (struct thread_db_info *info)
   else if (thread_db_find_new_threads_silently (inferior_thread ()) != 0)
     {
       /* Even if libthread_db initializes, if the thread list is
-         corrupted, we'd not manage to list any threads.  Better reject this
-         thread_db, and fall back to at least listing LWPs.  */
+        corrupted, we'd not manage to list any threads.  Better reject this
+        thread_db, and fall back to at least listing LWPs.  */
       return false;
     }
 
@@ -946,7 +945,7 @@ try_thread_db_load_1 (struct thread_db_info *info)
 
   /* The thread library was detected.  Activate the thread_db target
      for this process.  */
-  push_target (&the_thread_db_target);
+  current_inferior ()->push_target (&the_thread_db_target);
   return true;
 }
 
@@ -976,10 +975,11 @@ try_thread_db_load (const char *library, bool check_auto_load_safe)
          return false;
        }
 
-      if (!file_is_auto_load_safe (library, _("auto-load: Loading libthread-db "
-                                             "library \"%s\" from explicit "
-                                             "directory.\n"),
-                                  library))
+      auto_load_debug_printf
+       ("Loading libthread-db library \"%s\" from explicit directory.",
+        library);
+
+      if (!file_is_auto_load_safe (library))
        return false;
     }
 
@@ -997,13 +997,13 @@ try_thread_db_load (const char *library, bool check_auto_load_safe)
 
       td_init = dlsym (handle, "td_init");
       if (td_init != NULL)
-        {
-          const char *const libpath = dladdr_to_soname (td_init);
+       {
+         const char *const libpath = dladdr_to_soname (td_init);
 
-          if (libpath != NULL)
-            fprintf_unfiltered (gdb_stdlog, _("Host %s resolved to: %s.\n"),
-                               library, libpath);
-        }
+         if (libpath != NULL)
+           fprintf_unfiltered (gdb_stdlog, _("Host %s resolved to: %s.\n"),
+                              library, libpath);
+       }
     }
 
   info = add_thread_db_info (handle);
@@ -1194,7 +1194,7 @@ thread_db_load (void)
 
   /* Don't attempt to use thread_db on executables not running
      yet.  */
-  if (!target_has_registers)
+  if (!target_has_registers ())
     return false;
 
   /* Don't attempt to use thread_db for remote targets.  */
@@ -1224,23 +1224,14 @@ check_thread_signals (void)
 {
   if (!thread_signals)
     {
-      sigset_t mask;
       int i;
 
-      lin_thread_get_thread_signals (&mask);
-      sigemptyset (&thread_stop_set);
-      sigemptyset (&thread_print_set);
-
-      for (i = 1; i < NSIG; i++)
+      for (i = 0; i < lin_thread_get_thread_signal_num (); i++)
        {
-         if (sigismember (&mask, i))
-           {
-             if (signal_stop_update (gdb_signal_from_host (i), 0))
-               sigaddset (&thread_stop_set, i);
-             if (signal_print_update (gdb_signal_from_host (i), 0))
-               sigaddset (&thread_print_set, i);
-             thread_signals = 1;
-           }
+         int sig = lin_thread_get_thread_signal (i);
+         signal_stop_update (gdb_signal_from_host (sig), 0);
+         signal_print_update (gdb_signal_from_host (sig), 0);
+         thread_signals = 1;
        }
     }
 }
@@ -1286,7 +1277,7 @@ thread_db_new_objfile (struct objfile *objfile)
 }
 
 static void
-check_pid_namespace_match (void)
+check_pid_namespace_match (inferior *inf)
 {
   /* Check is only relevant for local targets targets.  */
   if (target_can_run ())
@@ -1296,7 +1287,7 @@ check_pid_namespace_match (void)
         child's thread list, we'll mistakenly think it has no threads
         since the thread PID fields won't match the PID we give to
         libthread_db.  */
-      if (!linux_ns_same (inferior_ptid.pid (), LINUX_NS_PID))
+      if (!linux_ns_same (inf->pid, LINUX_NS_PID))
        {
          warning (_ ("Target and debugger are in different PID "
                      "namespaces; thread lists and other data are "
@@ -1310,9 +1301,9 @@ check_pid_namespace_match (void)
    This handles the case of debugging statically linked executables.  */
 
 static void
-thread_db_inferior_created (struct target_ops *target, int from_tty)
+thread_db_inferior_created (inferior *inf)
 {
-  check_pid_namespace_match ();
+  check_pid_namespace_match (inf);
   check_for_thread_db ();
 }
 
@@ -1358,7 +1349,7 @@ record_thread (struct thread_db_info *info,
   else
     tp->priv.reset (priv);
 
-  if (target_has_execution)
+  if (target_has_execution ())
     check_thread_signals ();
 
   return tp;
@@ -1374,12 +1365,12 @@ thread_db_target::detach (inferior *inf, int from_tty)
   /* NOTE: From this point on, inferior_ptid is null_ptid.  */
 
   /* Detach the thread_db target from this inferior.  */
-  unpush_target (this);
+  inf->unpush_target (this);
 }
 
 ptid_t
 thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
-                       int options)
+                       target_wait_flags options)
 {
   struct thread_db_info *info;
 
@@ -1394,6 +1385,7 @@ thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
     case TARGET_WAITKIND_EXITED:
     case TARGET_WAITKIND_THREAD_EXITED:
     case TARGET_WAITKIND_SIGNALLED:
+    case TARGET_WAITKIND_EXECD:
       return ptid;
     }
 
@@ -1403,16 +1395,6 @@ thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
   if (info == NULL)
     return ptid;
 
-  if (ourstatus->kind == TARGET_WAITKIND_EXECD)
-    {
-      /* New image, it may or may not end up using thread_db.  Assume
-        not unless we find otherwise.  */
-      delete_thread_db_info (beneath, ptid.pid ());
-      unpush_target (this);
-
-      return ptid;
-    }
-
   /* Fill in the thread's user-level thread id and status.  */
   thread_from_lwp (find_thread_ptid (beneath, ptid), ptid);
 
@@ -1430,7 +1412,20 @@ thread_db_target::mourn_inferior ()
   target_beneath->mourn_inferior ();
 
   /* Detach the thread_db target from this inferior.  */
-  unpush_target (this);
+  current_inferior ()->unpush_target (this);
+}
+
+void
+thread_db_target::follow_exec (inferior *follow_inf, ptid_t ptid,
+                              const char *execd_pathname)
+{
+  process_stratum_target *beneath
+    = as_process_stratum_target (this->beneath ());
+
+  delete_thread_db_info (beneath, ptid.pid ());
+
+  current_inferior ()->unpush_target (this);
+  beneath->follow_exec (follow_inf, ptid, execd_pathname);
 }
 
 struct callback_data
@@ -1701,7 +1696,7 @@ thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
       thread_db_thread_info *priv = get_thread_db_thread_info (tp);
 
       if (priv != NULL && handle_tid == priv->tid)
-        return tp;
+       return tp;
     }
 
   return NULL;
@@ -1789,19 +1784,19 @@ thread_db_target::get_thread_local_address (ptid_t ptid,
          /* Now, if libthread_db provided the initialization image's
             address, we *could* try to build a non-lvalue value from
             the initialization image.  */
-        throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
-                     _("TLS not allocated yet"));
+       throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
+                    _("TLS not allocated yet"));
 #endif
 
       /* Something else went wrong.  */
       if (err != TD_OK)
-        throw_error (TLS_GENERIC_ERROR,
-                     (("%s")), thread_db_err_str (err));
+       throw_error (TLS_GENERIC_ERROR,
+                    (("%s")), thread_db_err_str (err));
 
       /* Cast assuming host == target.  Joy.  */
       /* Do proper sign extension for the target.  */
-      gdb_assert (exec_bfd);
-      return (bfd_get_sign_extend_vma (exec_bfd) > 0
+      gdb_assert (current_program_space->exec_bfd ());
+      return (bfd_get_sign_extend_vma (current_program_space->exec_bfd ()) > 0
              ? (CORE_ADDR) (intptr_t) address
              : (CORE_ADDR) (uintptr_t) address);
     }
@@ -1940,7 +1935,7 @@ info_auto_load_libthread_db (const char *args, int from_tty)
            i++;
          }
 
-       uiout->field_string ("pids", pids.c_str ());
+       uiout->field_string ("pids", pids);
 
        uiout->text ("\n");
       }
@@ -2038,10 +2033,11 @@ as they are loaded."),
                           &maintenance_show_cmdlist);
 
   /* Add ourselves to objfile event chain.  */
-  gdb::observers::new_objfile.attach (thread_db_new_objfile);
+  gdb::observers::new_objfile.attach (thread_db_new_objfile, "linux-thread-db");
 
   /* Add ourselves to inferior_created event chain.
      This is needed to handle debugging statically linked programs where
      the new_objfile observer won't get called for libpthread.  */
-  gdb::observers::inferior_created.attach (thread_db_inferior_created);
+  gdb::observers::inferior_created.attach (thread_db_inferior_created,
+                                          "linux-thread-db");
 }