Update release making notes.
[deliverable/binutils-gdb.git] / gdb / gdbserver / thread-db.c
index 185b8edd3173e187ae505fec9320ac8b9172c7d7..2bb6d28820e7e529568391a7f2d188c791766fad 100644 (file)
@@ -1,5 +1,5 @@
 /* Thread management interface, for the remote server for GDB.
-   Copyright (C) 2002-2016 Free Software Foundation, Inc.
+   Copyright (C) 2002-2020 Free Software Foundation, Inc.
 
    Contributed by MontaVista Software.
 
 
 #include "linux-low.h"
 
-extern int debug_threads;
-
+#include "debug.h"
 #include "gdb_proc_service.h"
 #include "nat/gdb_thread_db.h"
-#include "gdb_vecs.h"
+#include "gdbsupport/gdb_vecs.h"
 #include "nat/linux-procfs.h"
+#include "gdbsupport/scoped_restore.h"
 
 #ifndef USE_LIBTHREAD_DB_DIRECTLY
 #include <dlfcn.h>
@@ -56,9 +56,6 @@ struct thread_db
 
   /* Addresses of libthread_db functions.  */
   td_ta_new_ftype *td_ta_new_p;
-  td_ta_event_getmsg_ftype * td_ta_event_getmsg_p;
-  td_ta_set_event_ftype *td_ta_set_event_p;
-  td_ta_event_addr_ftype *td_ta_event_addr_p;
   td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
   td_thr_get_info_ftype *td_thr_get_info_p;
   td_ta_thr_iter_ftype *td_ta_thr_iter_p;
@@ -158,19 +155,21 @@ thread_db_state_str (td_thr_state_e state)
 }
 #endif
 
+/* Get thread info about PTID, accessing memory via the current
+   thread.  */
+
 static int
 find_one_thread (ptid_t ptid)
 {
   td_thrhandle_t th;
   td_thrinfo_t ti;
   td_err_e err;
-  struct thread_info *inferior;
   struct lwp_info *lwp;
   struct thread_db *thread_db = current_process ()->priv->thread_db;
-  int lwpid = ptid_get_lwp (ptid);
+  int lwpid = ptid.lwp ();
 
-  inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
-  lwp = get_thread_lwp (inferior);
+  thread_info *thread = find_thread_ptid (ptid);
+  lwp = get_thread_lwp (thread);
   if (lwp->thread_known)
     return 1;
 
@@ -203,6 +202,7 @@ find_one_thread (ptid_t ptid)
 
   lwp->thread_known = 1;
   lwp->th = th;
+  lwp->thread_handle = ti.ti_tid;
 
   return 1;
 }
@@ -214,7 +214,7 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
 {
   struct process_info *proc = current_process ();
   int pid = pid_of (proc);
-  ptid_t ptid = ptid_build (pid, ti_p->ti_lid, 0);
+  ptid_t ptid = ptid_t (pid, ti_p->ti_lid, 0);
   struct lwp_info *lwp;
   int err;
 
@@ -224,9 +224,11 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
   err = linux_attach_lwp (ptid);
   if (err != 0)
     {
-      warning ("Could not attach to thread %ld (LWP %d): %s\n",
-              (unsigned long) ti_p->ti_tid, ti_p->ti_lid,
-              linux_ptrace_attach_fail_reason_string (ptid, err));
+      std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
+
+      warning ("Could not attach to thread %ld (LWP %d): %s",
+              (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ());
+
       return 0;
     }
 
@@ -234,6 +236,7 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
   gdb_assert (lwp != NULL);
   lwp->thread_known = 1;
   lwp->th = *th_p;
+  lwp->thread_handle = ti_p->ti_tid;
 
   return 1;
 }
@@ -248,7 +251,7 @@ maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
 {
   struct lwp_info *lwp;
 
-  lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
+  lwp = find_lwp_pid (ptid_t (ti_p->ti_lid));
   if (lwp != NULL)
     return 1;
 
@@ -404,7 +407,7 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
 
   lwp = get_thread_lwp (thread);
   if (!lwp->thread_known)
-    find_one_thread (thread->entry.id);
+    find_one_thread (thread->id);
   if (!lwp->thread_known)
     return TD_NOTHR;
 
@@ -442,6 +445,35 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
     return err;
 }
 
+/* See linux-low.h.  */
+
+bool
+thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len)
+{
+  struct thread_db *thread_db;
+  struct lwp_info *lwp;
+  thread_info *thread = find_thread_ptid (ptid);
+
+  if (thread == NULL)
+    return false;
+
+  thread_db = get_thread_process (thread)->priv->thread_db;
+
+  if (thread_db == NULL)
+    return false;
+
+  lwp = get_thread_lwp (thread);
+
+  if (!lwp->thread_known && !find_one_thread (thread->id))
+    return false;
+
+  gdb_assert (lwp->thread_known);
+
+  *handle = (gdb_byte *) &lwp->thread_handle;
+  *handle_len = sizeof (lwp->thread_handle);
+  return true;
+}
+
 #ifdef USE_LIBTHREAD_DB_DIRECTLY
 
 static int
@@ -475,9 +507,6 @@ thread_db_load_search (void)
   tdb->td_symbol_list_p = &td_symbol_list;
 
   /* These are not essential.  */
-  tdb->td_ta_event_addr_p = &td_ta_event_addr;
-  tdb->td_ta_set_event_p = &td_ta_set_event;
-  tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
   tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
   tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
 
@@ -542,9 +571,6 @@ try_thread_db_load_1 (void *handle)
   CHK (1, TDB_DLSYM (tdb, td_symbol_list));
 
   /* These are not essential.  */
-  CHK (0, TDB_DLSYM (tdb, td_ta_event_addr));
-  CHK (0, TDB_DLSYM (tdb, td_ta_set_event));
-  CHK (0, TDB_DLSYM (tdb, td_ta_event_getmsg));
   CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
   CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
 
@@ -599,8 +625,7 @@ try_thread_db_load (const char *library)
          const char *const libpath = dladdr_to_soname (td_init);
 
          if (libpath != NULL)
-           fprintf (stderr, "Host %s resolved to: %s.\n",
-                    library, libpath);
+           debug_printf ("Host %s resolved to: %s.\n", library, libpath);
        }
     }
 #endif
@@ -657,17 +682,17 @@ try_thread_db_load_from_dir (const char *dir, size_t dir_len)
 static int
 thread_db_load_search (void)
 {
-  VEC (char_ptr) *dir_vec;
-  char *this_dir;
-  int i, rc = 0;
+  int rc = 0;
 
   if (libthread_db_search_path == NULL)
     libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
 
-  dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
+  std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
+    = dirnames_to_char_ptr_vec (libthread_db_search_path);
 
-  for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
+  for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec)
     {
+      char *this_dir = this_dir_up.get ();
       const int pdir_len = sizeof ("$pdir") - 1;
       size_t this_dir_len;
 
@@ -699,7 +724,6 @@ thread_db_load_search (void)
        }
     }
 
-  free_char_ptr_vec (dir_vec);
   if (debug_threads)
     debug_printf ("thread_db_load_search returning %d\n", rc);
   return rc;
@@ -743,25 +767,12 @@ thread_db_init (void)
   return 0;
 }
 
-static int
-any_thread_of (struct inferior_list_entry *entry, void *args)
-{
-  int *pid_p = (int *) args;
-
-  if (ptid_get_pid (entry->id) == *pid_p)
-    return 1;
-
-  return 0;
-}
-
 static void
 switch_to_process (struct process_info *proc)
 {
   int pid = pid_of (proc);
 
-  current_thread =
-    (struct thread_info *) find_inferior (&all_threads,
-                                         any_thread_of, &pid);
+  current_thread = find_any_thread_of_pid (pid);
 }
 
 /* Disconnect from libthread_db and free resources.  */
@@ -874,3 +885,26 @@ thread_db_handle_monitor_command (char *mon)
   /* Tell server.c to perform default processing.  */
   return 0;
 }
+
+/* See linux-low.h.  */
+
+void
+thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid)
+{
+  process_info *parent_proc = get_thread_process (parent_thr);
+  struct thread_db *thread_db = parent_proc->priv->thread_db;
+
+  /* If the thread layer isn't initialized, return.  It may just
+     be that the program uses clone, but does not use libthread_db.  */
+  if (thread_db == NULL || !thread_db->all_symbols_looked_up)
+    return;
+
+  /* find_one_thread calls into libthread_db which accesses memory via
+     the current thread.  Temporarily switch to a thread we know is
+     stopped.  */
+  scoped_restore restore_current_thread
+    = make_scoped_restore (&current_thread, parent_thr);
+
+  if (!find_one_thread (child_ptid))
+    warning ("Cannot find thread after clone.");
+}
This page took 0.027754 seconds and 4 git commands to generate.