/* Thread management interface, for the remote server for GDB.
- Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
- Free Software Foundation, Inc.
+ Copyright (C) 2002-2015 Free Software Foundation, Inc.
Contributed by MontaVista Software.
static int thread_db_use_events;
#include "gdb_proc_service.h"
-#include "../gdb_thread_db.h"
+#include "nat/gdb_thread_db.h"
+#include "gdb_vecs.h"
#ifndef USE_LIBTHREAD_DB_DIRECTLY
#include <dlfcn.h>
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);
};
struct lwp_info *lwp;
struct thread_db *thread_db = current_process ()->private->thread_db;
- if (thread_db->td_ta_event_getmsg_p == NULL)
- fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
+ gdb_assert (thread_db->td_ta_event_getmsg_p != NULL);
if (debug_threads)
- fprintf (stderr, "Thread creation event.\n");
+ debug_printf ("Thread creation event.\n");
/* FIXME: This assumes we don't get another event.
In the LinuxThreads implementation, this is safe,
/* If we do not know about the main thread yet, this would be a good time to
find it. We need to do this to pick up the main thread before any newly
created threads. */
- lwp = get_thread_lwp (current_inferior);
+ lwp = get_thread_lwp (current_thread);
if (lwp->thread_known == 0)
- find_one_thread (lwp->head.id);
+ find_one_thread (current_thread->entry.id);
/* msg.event == TD_EVENT_CREATE */
lwpid, thread_db_err_str (err));
if (debug_threads)
- fprintf (stderr, "Found thread %ld (LWP %d)\n",
- ti.ti_tid, ti.ti_lid);
+ debug_printf ("Found thread %ld (LWP %d)\n",
+ ti.ti_tid, ti.ti_lid);
if (lwpid != ti.ti_lid)
{
static int
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);
struct lwp_info *lwp;
+ int err;
if (debug_threads)
- fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
- ti_p->ti_tid, ti_p->ti_lid);
- linux_attach_lwp (ti_p->ti_lid);
- lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
- if (lwp == NULL)
+ debug_printf ("Attaching to thread %ld (LWP %d)\n",
+ ti_p->ti_tid, ti_p->ti_lid);
+ err = linux_attach_lwp (ptid);
+ if (err != 0)
{
- warning ("Could not attach to thread %ld (LWP %d)\n",
- ti_p->ti_tid, ti_p->ti_lid);
+ warning ("Could not attach to thread %ld (LWP %d): %s\n",
+ ti_p->ti_tid, ti_p->ti_lid,
+ linux_ptrace_attach_fail_reason_string (ptid, err));
return 0;
}
+ lwp = find_lwp_pid (ptid);
+ gdb_assert (lwp != NULL);
lwp->thread_known = 1;
lwp->th = *th_p;
if (thread_db_use_events)
{
td_err_e err;
- struct thread_db *thread_db = current_process ()->private->thread_db;
+ struct thread_db *thread_db = proc->private->thread_db;
err = thread_db->td_thr_event_enable_p (th_p, 1);
if (err != TD_OK)
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;
thread_db_find_new_threads (void)
{
td_err_e err;
- ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id;
+ ptid_t ptid = current_ptid;
struct thread_db *thread_db = current_process ()->private->thread_db;
int loop, iteration;
TD_THR_LOWEST_PRIORITY,
TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
if (debug_threads)
- fprintf (stderr, "Found %d threads in iteration %d.\n",
- new_thread_count, iteration);
+ debug_printf ("Found %d threads in iteration %d.\n",
+ new_thread_count, iteration);
if (new_thread_count != 0)
{
psaddr_t addr;
td_err_e err;
struct lwp_info *lwp;
- struct thread_info *saved_inferior;
+ struct thread_info *saved_thread;
struct process_info *proc;
struct thread_db *thread_db;
thread_db = proc->private->thread_db;
/* If the thread layer is not (yet) initialized, fail. */
- if (!thread_db->all_symbols_looked_up)
+ if (thread_db == NULL || !thread_db->all_symbols_looked_up)
return TD_ERR;
- if (thread_db->td_thr_tls_get_addr_p == NULL)
+ /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
+ could work. */
+ if (thread_db->td_thr_tls_get_addr_p == NULL
+ || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
return -1;
lwp = get_thread_lwp (thread);
if (!lwp->thread_known)
- find_one_thread (lwp->head.id);
+ find_one_thread (thread->entry.id);
if (!lwp->thread_known)
return TD_NOTHR;
- saved_inferior = current_inferior;
- current_inferior = thread;
- /* Note the cast through uintptr_t: this interface only works if
- a target address fits in a psaddr_t, which is a host pointer.
- So a 32-bit debugger can not access 64-bit TLS through this. */
- err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
- (psaddr_t) (uintptr_t) load_module,
- offset, &addr);
- current_inferior = saved_inferior;
+ saved_thread = current_thread;
+ current_thread = thread;
+
+ if (load_module != 0)
+ {
+ /* Note the cast through uintptr_t: this interface only works if
+ a target address fits in a psaddr_t, which is a host pointer.
+ So a 32-bit debugger can not access 64-bit TLS through this. */
+ err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
+ (psaddr_t) (uintptr_t) load_module,
+ offset, &addr);
+ }
+ else
+ {
+ /* This code path handles the case of -static -pthread executables:
+ https://sourceware.org/ml/libc-help/2014-03/msg00024.html
+ For older GNU libc r_debug.r_map is NULL. For GNU libc after
+ PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
+ The constant number 1 depends on GNU __libc_setup_tls
+ initialization of l_tls_modid to 1. */
+ err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
+ addr = (char *) addr + offset;
+ }
+
+ current_thread = saved_thread;
if (err == TD_OK)
{
*address = (CORE_ADDR) (uintptr_t) addr;
struct thread_db *tdb;
struct process_info *proc = current_process ();
- if (proc->private->thread_db != NULL)
- fatal ("unexpected: proc->private->thread_db != NULL");
+ gdb_assert (proc->private->thread_db == NULL);
tdb = xcalloc (1, sizeof (*tdb));
proc->private->thread_db = tdb;
if (err != TD_OK)
{
if (debug_threads)
- fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
+ debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
free (tdb);
proc->private->thread_db = NULL;
return 0;
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;
return 1;
}
struct thread_db *tdb;
struct process_info *proc = current_process ();
- if (proc->private->thread_db != NULL)
- fatal ("unexpected: proc->private->thread_db != NULL");
+ gdb_assert (proc->private->thread_db == NULL);
tdb = xcalloc (1, sizeof (*tdb));
proc->private->thread_db = tdb;
if ((a) == NULL) \
{ \
if (debug_threads) \
- fprintf (stderr, "dlsym: %s\n", dlerror ()); \
+ debug_printf ("dlsym: %s\n", dlerror ()); \
if (required) \
{ \
free (tdb); \
if (err != TD_OK)
{
if (debug_threads)
- fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
+ debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
free (tdb);
proc->private->thread_db = NULL;
return 0;
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"));
#undef CHK
void *handle;
if (debug_threads)
- fprintf (stderr, "Trying host libthread_db library: %s.\n",
- library);
+ debug_printf ("Trying host libthread_db library: %s.\n",
+ library);
handle = dlopen (library, RTLD_NOW);
if (handle == NULL)
{
if (debug_threads)
- fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
+ debug_printf ("dlopen failed: %s.\n", dlerror ());
return 0;
}
static int
thread_db_load_search (void)
{
- const char *search_path;
- int rc = 0;
+ VEC (char_ptr) *dir_vec;
+ char *this_dir;
+ int i, rc = 0;
if (libthread_db_search_path == NULL)
libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
- search_path = libthread_db_search_path;
- while (*search_path)
+ dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
+
+ for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
{
- const char *end = strchr (search_path, ':');
- const char *this_dir = search_path;
+ const int pdir_len = sizeof ("$pdir") - 1;
size_t this_dir_len;
- if (end)
- {
- this_dir_len = end - search_path;
- search_path += this_dir_len + 1;
- }
- else
- {
- this_dir_len = strlen (this_dir);
- search_path += this_dir_len;
- }
+ this_dir_len = strlen (this_dir);
- if (this_dir_len == sizeof ("$pdir") - 1
- && strncmp (this_dir, "$pdir", this_dir_len) == 0)
+ if (strncmp (this_dir, "$pdir", pdir_len) == 0
+ && (this_dir[pdir_len] == '\0'
+ || this_dir[pdir_len] == '/'))
{
/* We don't maintain a list of loaded libraries so we don't know
where libpthread lives. We *could* fetch the info, but we don't
do that yet. Ignore it. */
}
- else if (this_dir_len == sizeof ("$sdir") - 1
- && strncmp (this_dir, "$sdir", this_dir_len) == 0)
+ else if (strcmp (this_dir, "$sdir") == 0)
{
if (try_thread_db_load_from_sdir ())
{
}
}
+ free_char_ptr_vec (dir_vec);
if (debug_threads)
- fprintf (stderr, "thread_db_load_search returning %d\n", rc);
+ debug_printf ("thread_db_load_search returning %d\n", rc);
return rc;
}
{
int pid = pid_of (proc);
- current_inferior =
+ current_thread =
(struct thread_info *) find_inferior (&all_threads,
any_thread_of, &pid);
}
if (td_ta_clear_event_p != NULL)
{
- struct thread_info *saved_inferior = current_inferior;
+ struct thread_info *saved_thread = current_thread;
td_thr_events_t events;
switch_to_process (proc);
td_event_fillset (&events);
(*td_ta_clear_event_p) (thread_db->thread_agent, &events);
- current_inferior = saved_inferior;
+ current_thread = saved_thread;
}
}
}
if (thread_db->td_create_bp != NULL)
{
- struct thread_info *saved_inferior = current_inferior;
+ struct thread_info *saved_thread = current_thread;
switch_to_process (proc);
delete_breakpoint (thread_db->td_create_bp);
thread_db->td_create_bp = NULL;
- current_inferior = saved_inferior;
+ current_thread = saved_thread;
}
}