Add some more casts (1/2)
[deliverable/binutils-gdb.git] / gdb / gdbserver / thread-db.c
index 71bc98412ce5703c2c3e5712f136e39de15439c0..ffe722dcdd4f0780273706eb82743f3eab4c99cf 100644 (file)
@@ -29,12 +29,11 @@ static int thread_db_use_events;
 #include "gdb_proc_service.h"
 #include "nat/gdb_thread_db.h"
 #include "gdb_vecs.h"
+#include "nat/linux-procfs.h"
 
 #ifndef USE_LIBTHREAD_DB_DIRECTLY
 #include <dlfcn.h>
 #endif
-
-#include <stdint.h>
 #include <limits.h>
 #include <ctype.h>
 
@@ -68,30 +67,17 @@ struct thread_db
   struct breakpoint *td_create_bp;
 
   /* Addresses of libthread_db functions.  */
-  td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, td_thragent_t **ta);
-  td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
-                                   td_event_msg_t *msg);
-  td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
-                                td_thr_events_t *event);
-  td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
-                                 td_event_e event, td_notify_t *ptr);
-  td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid,
-                                  td_thrhandle_t *th);
-  td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
-                                td_thrinfo_t *infop);
-  td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
-  td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
-                               td_thr_iter_f *callback, void *cbdata_p,
-                               td_thr_state_e state, int ti_pri,
-                               sigset_t *ti_sigmask_p,
-                               unsigned int ti_user_flags);
-  td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
-                                    psaddr_t map_address,
-                                    size_t offset, psaddr_t *address);
-  td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
-                               unsigned long int modid,
-                               psaddr_t *base);
-  const char ** (*td_symbol_list_p) (void);
+  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_thr_event_enable_ftype *td_thr_event_enable_p;
+  td_ta_thr_iter_ftype *td_ta_thr_iter_p;
+  td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
+  td_thr_tlsbase_ftype *td_thr_tlsbase_p;
+  td_symbol_list_ftype *td_symbol_list_p;
 };
 
 static char *libthread_db_search_path;
@@ -191,7 +177,7 @@ thread_db_create_event (CORE_ADDR where)
   td_event_msg_t msg;
   td_err_e err;
   struct lwp_info *lwp;
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
 
   gdb_assert (thread_db->td_ta_event_getmsg_p != NULL);
 
@@ -227,7 +213,7 @@ thread_db_enable_reporting (void)
   td_thr_events_t events;
   td_notify_t notify;
   td_err_e err;
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
 
   if (thread_db->td_ta_set_event_p == NULL
       || thread_db->td_ta_event_addr_p == NULL
@@ -271,7 +257,7 @@ find_one_thread (ptid_t ptid)
   td_err_e err;
   struct thread_info *inferior;
   struct lwp_info *lwp;
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
   int lwpid = ptid_get_lwp (ptid);
 
   inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
@@ -339,7 +325,7 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
     {
       warning ("Could not attach to thread %ld (LWP %d): %s\n",
               ti_p->ti_tid, ti_p->ti_lid,
-              linux_attach_fail_reason_string (ptid, err));
+              linux_ptrace_attach_fail_reason_string (ptid, err));
       return 0;
     }
 
@@ -351,7 +337,7 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
   if (thread_db_use_events)
     {
       td_err_e err;
-      struct thread_db *thread_db = proc->private->thread_db;
+      struct thread_db *thread_db = proc->priv->thread_db;
 
       err = thread_db->td_thr_event_enable_p (th_p, 1);
       if (err != TD_OK)
@@ -390,12 +376,26 @@ find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
 {
   td_thrinfo_t ti;
   td_err_e err;
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
 
   err = thread_db->td_thr_get_info_p (th_p, &ti);
   if (err != TD_OK)
     error ("Cannot get thread info: %s", thread_db_err_str (err));
 
+  if (ti.ti_lid == -1)
+    {
+      /* A thread with kernel thread ID -1 is either a thread that
+        exited and was joined, or a thread that is being created but
+        hasn't started yet, and that is reusing the tcb/stack of a
+        thread that previously exited and was joined.  (glibc marks
+        terminated and joined threads with kernel thread ID -1.  See
+        glibc PR17707.  */
+      if (debug_threads)
+       debug_printf ("thread_db: skipping exited and "
+                     "joined thread (0x%lx)\n", ti.ti_tid);
+      return 0;
+    }
+
   /* Check for zombies.  */
   if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
     return 0;
@@ -415,7 +415,7 @@ thread_db_find_new_threads (void)
 {
   td_err_e err;
   ptid_t ptid = current_ptid;
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
   int loop, iteration;
 
   /* This function is only called when we first initialize thread_db.
@@ -461,7 +461,7 @@ thread_db_find_new_threads (void)
 static void
 thread_db_look_up_symbols (void)
 {
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
   const char **sym_list;
   CORE_ADDR unused;
 
@@ -476,7 +476,7 @@ thread_db_look_up_symbols (void)
 int
 thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
 {
-  struct thread_db *thread_db = current_process ()->private->thread_db;
+  struct thread_db *thread_db = current_process ()->priv->thread_db;
   int may_ask_gdb = !thread_db->all_symbols_looked_up;
 
   /* If we've passed the call to thread_db_look_up_symbols, then
@@ -499,7 +499,7 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
   struct thread_db *thread_db;
 
   proc = get_thread_process (thread);
-  thread_db = proc->private->thread_db;
+  thread_db = proc->priv->thread_db;
 
   /* If the thread layer is not (yet) initialized, fail.  */
   if (thread_db == NULL || !thread_db->all_symbols_looked_up)
@@ -560,10 +560,10 @@ thread_db_load_search (void)
   struct thread_db *tdb;
   struct process_info *proc = current_process ();
 
-  gdb_assert (proc->private->thread_db == NULL);
+  gdb_assert (proc->priv->thread_db == NULL);
 
-  tdb = xcalloc (1, sizeof (*tdb));
-  proc->private->thread_db = tdb;
+  tdb = XCNEW (struct thread_db);
+  proc->priv->thread_db = tdb;
 
   tdb->td_ta_new_p = &td_ta_new;
 
@@ -574,7 +574,7 @@ thread_db_load_search (void)
       if (debug_threads)
        debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
       free (tdb);
-      proc->private->thread_db = NULL;
+      proc->priv->thread_db = NULL;
       return 0;
     }
 
@@ -605,10 +605,10 @@ try_thread_db_load_1 (void *handle)
   struct thread_db *tdb;
   struct process_info *proc = current_process ();
 
-  gdb_assert (proc->private->thread_db == NULL);
+  gdb_assert (proc->priv->thread_db == NULL);
 
-  tdb = xcalloc (1, sizeof (*tdb));
-  proc->private->thread_db = tdb;
+  tdb = XCNEW (struct thread_db);
+  proc->priv->thread_db = tdb;
 
   tdb->handle = handle;
 
@@ -625,14 +625,17 @@ try_thread_db_load_1 (void *handle)
          if (required)                                         \
            {                                                   \
              free (tdb);                                       \
-             proc->private->thread_db = NULL;                  \
+             proc->priv->thread_db = NULL;                     \
              return 0;                                         \
            }                                                   \
        }                                                       \
     }                                                          \
   while (0)
 
-  CHK (1, tdb->td_ta_new_p = dlsym (handle, "td_ta_new"));
+#define TDB_DLSYM(tdb, func) \
+  tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
+
+  CHK (1, TDB_DLSYM (tdb, td_ta_new));
 
   /* Attempt to open a connection to the thread library.  */
   err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
@@ -641,27 +644,27 @@ try_thread_db_load_1 (void *handle)
       if (debug_threads)
        debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
       free (tdb);
-      proc->private->thread_db = NULL;
+      proc->priv->thread_db = NULL;
       return 0;
     }
 
-  CHK (1, tdb->td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"));
-  CHK (1, tdb->td_thr_get_info_p = dlsym (handle, "td_thr_get_info"));
-  CHK (1, tdb->td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"));
-  CHK (1, tdb->td_symbol_list_p = dlsym (handle, "td_symbol_list"));
+  CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
+  CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
+  CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
+  CHK (1, TDB_DLSYM (tdb, td_symbol_list));
 
   /* This is required only when thread_db_use_events is on.  */
-  CHK (thread_db_use_events,
-       tdb->td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
+  CHK (thread_db_use_events, TDB_DLSYM (tdb, td_thr_event_enable));
 
   /* These are not essential.  */
-  CHK (0, tdb->td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"));
-  CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
-  CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
-  CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
-  CHK (0, tdb->td_thr_tlsbase_p = dlsym (handle, "td_thr_tlsbase"));
+  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));
 
 #undef CHK
+#undef TDB_DLSYM
 
   return 1;
 }
@@ -746,7 +749,7 @@ try_thread_db_load_from_dir (const char *dir, size_t dir_len)
 
   if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
     {
-      char *cp = xmalloc (dir_len + 1);
+      char *cp = (char *) xmalloc (dir_len + 1);
 
       memcpy (cp, dir, dir_len);
       cp[dir_len] = '\0';
@@ -845,7 +848,22 @@ thread_db_init (int use_events)
          thread_db_mourn (proc);
          return 0;
        }
-      thread_db_find_new_threads ();
+
+      /* It's best to avoid td_ta_thr_iter if possible.  That walks
+        data structures in the inferior's address space that may be
+        corrupted, or, if the target is running, the list may change
+        while we walk it.  In the latter case, it's possible that a
+        thread exits just at the exact time that causes GDBserver to
+        get stuck in an infinite loop.  If the kernel supports clone
+        events, and /proc/PID/task/ exits, then we already know about
+        all threads in the process.  When we need info out of
+        thread_db on a given thread (e.g., for TLS), we'll use
+        find_one_thread then.  That uses thread_db entry points that
+        do not walk libpthread's thread list, so should be safe, as
+        well as more efficient.  */
+      if (use_events
+         || !linux_proc_task_list_dir_exists (pid_of (proc)))
+       thread_db_find_new_threads ();
       thread_db_look_up_symbols ();
       return 1;
     }
@@ -856,7 +874,7 @@ thread_db_init (int use_events)
 static int
 any_thread_of (struct inferior_list_entry *entry, void *args)
 {
-  int *pid_p = args;
+  int *pid_p = (int *) args;
 
   if (ptid_get_pid (entry->id) == *pid_p)
     return 1;
@@ -879,14 +897,16 @@ switch_to_process (struct process_info *proc)
 static void
 disable_thread_event_reporting (struct process_info *proc)
 {
-  struct thread_db *thread_db = proc->private->thread_db;
+  struct thread_db *thread_db = proc->priv->thread_db;
   if (thread_db)
     {
       td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
                                       td_thr_events_t *event);
 
 #ifndef USE_LIBTHREAD_DB_DIRECTLY
-      td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event");
+      td_ta_clear_event_p
+       = (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
+                                            "td_ta_clear_event");
 #else
       td_ta_clear_event_p = &td_ta_clear_event;
 #endif
@@ -911,7 +931,7 @@ disable_thread_event_reporting (struct process_info *proc)
 static void
 remove_thread_event_breakpoints (struct process_info *proc)
 {
-  struct thread_db *thread_db = proc->private->thread_db;
+  struct thread_db *thread_db = proc->priv->thread_db;
 
   if (thread_db->td_create_bp != NULL)
     {
@@ -929,7 +949,7 @@ remove_thread_event_breakpoints (struct process_info *proc)
 void
 thread_db_detach (struct process_info *proc)
 {
-  struct thread_db *thread_db = proc->private->thread_db;
+  struct thread_db *thread_db = proc->priv->thread_db;
 
   if (thread_db)
     {
@@ -943,13 +963,13 @@ thread_db_detach (struct process_info *proc)
 void
 thread_db_mourn (struct process_info *proc)
 {
-  struct thread_db *thread_db = proc->private->thread_db;
+  struct thread_db *thread_db = proc->priv->thread_db;
   if (thread_db)
     {
-      td_err_e (*td_ta_delete_p) (td_thragent_t *);
+      td_ta_delete_ftype *td_ta_delete_p;
 
 #ifndef USE_LIBTHREAD_DB_DIRECTLY
-      td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
+      td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
 #else
       td_ta_delete_p = &td_ta_delete;
 #endif
@@ -962,7 +982,7 @@ thread_db_mourn (struct process_info *proc)
 #endif  /* USE_LIBTHREAD_DB_DIRECTLY  */
 
       free (thread_db);
-      proc->private->thread_db = NULL;
+      proc->priv->thread_db = NULL;
     }
 }
 
This page took 0.031232 seconds and 4 git commands to generate.