gdb/gdbserver/thread-db.c - gdb
Global variables defined
Data types defined
Functions defined
Macros defined
Source code
- #include "server.h"
- #include "linux-low.h"
- extern int debug_threads;
- static int thread_db_use_events;
- #include "gdb_proc_service.h"
- #include "nat/gdb_thread_db.h"
- #include "gdb_vecs.h"
- #ifndef USE_LIBTHREAD_DB_DIRECTLY
- #include <dlfcn.h>
- #endif
- #include <stdint.h>
- #include <limits.h>
- #include <ctype.h>
- struct thread_db
- {
-
- struct ps_prochandle proc_handle;
-
- td_thragent_t *thread_agent;
-
- int all_symbols_looked_up;
- #ifndef USE_LIBTHREAD_DB_DIRECTLY
-
- void *handle;
- #endif
-
- struct breakpoint *td_create_bp;
-
- 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);
- };
- static char *libthread_db_search_path;
- static int find_one_thread (ptid_t);
- static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
- static const char *
- thread_db_err_str (td_err_e err)
- {
- static char buf[64];
- switch (err)
- {
- case TD_OK:
- return "generic 'call succeeded'";
- case TD_ERR:
- return "generic error";
- case TD_NOTHR:
- return "no thread to satisfy query";
- case TD_NOSV:
- return "no sync handle to satisfy query";
- case TD_NOLWP:
- return "no LWP to satisfy query";
- case TD_BADPH:
- return "invalid process handle";
- case TD_BADTH:
- return "invalid thread handle";
- case TD_BADSH:
- return "invalid synchronization handle";
- case TD_BADTA:
- return "invalid thread agent";
- case TD_BADKEY:
- return "invalid key";
- case TD_NOMSG:
- return "no event message for getmsg";
- case TD_NOFPREGS:
- return "FPU register set not available";
- case TD_NOLIBTHREAD:
- return "application not linked with libthread";
- case TD_NOEVENT:
- return "requested event is not supported";
- case TD_NOCAPAB:
- return "capability not available";
- case TD_DBERR:
- return "debugger service failed";
- case TD_NOAPLIC:
- return "operation not applicable to";
- case TD_NOTSD:
- return "no thread-specific data for this thread";
- case TD_MALLOC:
- return "malloc failed";
- case TD_PARTIALREG:
- return "only part of register set was written/read";
- case TD_NOXREGS:
- return "X register set not available for this thread";
- #ifdef HAVE_TD_VERSION
- case TD_VERSION:
- return "version mismatch between libthread_db and libpthread";
- #endif
- default:
- xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
- return buf;
- }
- }
- #if 0
- #endif
- static int
- 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;
- gdb_assert (thread_db->td_ta_event_getmsg_p != NULL);
- if (debug_threads)
- debug_printf ("Thread creation event.\n");
- FIXME
- err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
- if (err != TD_OK)
- fprintf (stderr, "thread getmsg err: %s\n",
- thread_db_err_str (err));
-
- lwp = get_thread_lwp (current_thread);
- if (lwp->thread_known == 0)
- find_one_thread (current_thread->entry.id);
-
- find_new_threads_callback (msg.th_p, NULL);
- return 0;
- }
- static int
- 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;
- if (thread_db->td_ta_set_event_p == NULL
- || thread_db->td_ta_event_addr_p == NULL
- || thread_db->td_ta_event_getmsg_p == NULL)
-
- return 0;
-
- td_event_emptyset (&events);
- td_event_addset (&events, TD_CREATE);
- err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
- if (err != TD_OK)
- {
- warning ("Unable to set global thread event mask: %s",
- thread_db_err_str (err));
- return 0;
- }
-
- err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
- ¬ify);
- if (err != TD_OK)
- {
- warning ("Unable to get location for thread creation breakpoint: %s",
- thread_db_err_str (err));
- return 0;
- }
- thread_db->td_create_bp
- = set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
- thread_db_create_event);
- return 1;
- }
- 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 ()->private->thread_db;
- int lwpid = ptid_get_lwp (ptid);
- inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
- lwp = get_thread_lwp (inferior);
- if (lwp->thread_known)
- return 1;
-
- err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
- if (err != TD_OK)
- error ("Cannot get thread handle for LWP %d: %s",
- lwpid, thread_db_err_str (err));
- err = thread_db->td_thr_get_info_p (&th, &ti);
- if (err != TD_OK)
- error ("Cannot get thread info for LWP %d: %s",
- lwpid, thread_db_err_str (err));
- if (debug_threads)
- debug_printf ("Found thread %ld (LWP %d)\n",
- ti.ti_tid, ti.ti_lid);
- if (lwpid != ti.ti_lid)
- {
- warning ("PID mismatch! Expected %ld, got %ld",
- (long) lwpid, (long) ti.ti_lid);
- return 0;
- }
- if (thread_db_use_events)
- {
- err = thread_db->td_thr_event_enable_p (&th, 1);
- if (err != TD_OK)
- error ("Cannot enable thread event reporting for %d: %s",
- ti.ti_lid, thread_db_err_str (err));
- }
-
- if (ti.ti_tid == 0)
- return 0;
- lwp->thread_known = 1;
- lwp->th = th;
- return 1;
- }
- 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)
- 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): %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 = proc->private->thread_db;
- err = thread_db->td_thr_event_enable_p (th_p, 1);
- if (err != TD_OK)
- error ("Cannot enable thread event reporting for %d: %s",
- ti_p->ti_lid, thread_db_err_str (err));
- }
- return 1;
- }
- static int
- maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
- int *counter)
- {
- struct lwp_info *lwp;
- lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
- if (lwp != NULL)
- return 1;
- if (!attach_thread (th_p, ti_p))
- return 0;
- if (counter != NULL)
- *counter += 1;
- return 1;
- }
- static int
- 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;
- 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)
- {
-
- return 0;
- }
-
- if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
- return 0;
- if (!maybe_attach_thread (th_p, &ti, (int *) data))
- {
-
- return 1;
- }
- return 0;
- }
- static void
- 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;
- int loop, iteration;
-
- if (find_one_thread (ptid) == 0)
- return;
-
- for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
- {
- int new_thread_count = 0;
-
- err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
- find_new_threads_callback,
- &new_thread_count,
- TD_THR_ANY_STATE,
- TD_THR_LOWEST_PRIORITY,
- TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
- if (debug_threads)
- debug_printf ("Found %d threads in iteration %d.\n",
- new_thread_count, iteration);
- if (new_thread_count != 0)
- {
-
- loop = -1;
- }
- }
- if (err != TD_OK)
- error ("Cannot find new threads: %s", thread_db_err_str (err));
- }
- static void
- thread_db_look_up_symbols (void)
- {
- struct thread_db *thread_db = current_process ()->private->thread_db;
- const char **sym_list;
- CORE_ADDR unused;
- for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
- look_up_one_symbol (*sym_list, &unused, 1);
-
- thread_db->all_symbols_looked_up = 1;
- }
- int
- thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
- {
- struct thread_db *thread_db = current_process ()->private->thread_db;
- int may_ask_gdb = !thread_db->all_symbols_looked_up;
-
- return look_up_one_symbol (name, addrp, may_ask_gdb);
- }
- int
- thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
- CORE_ADDR load_module, CORE_ADDR *address)
- {
- psaddr_t addr;
- td_err_e err;
- struct lwp_info *lwp;
- struct thread_info *saved_thread;
- struct process_info *proc;
- struct thread_db *thread_db;
- proc = get_thread_process (thread);
- thread_db = proc->private->thread_db;
-
- if (thread_db == NULL || !thread_db->all_symbols_looked_up)
- return TD_ERR;
-
- 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 (thread->entry.id);
- if (!lwp->thread_known)
- return TD_NOTHR;
- saved_thread = current_thread;
- current_thread = thread;
- if (load_module != 0)
- {
-
- err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
- (psaddr_t) (uintptr_t) load_module,
- offset, &addr);
- }
- else
- {
-
- 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;
- return 0;
- }
- else
- return err;
- }
- #ifdef USE_LIBTHREAD_DB_DIRECTLY
- static int
- thread_db_load_search (void)
- {
- td_err_e err;
- struct thread_db *tdb;
- struct process_info *proc = current_process ();
- gdb_assert (proc->private->thread_db == NULL);
- tdb = xcalloc (1, sizeof (*tdb));
- proc->private->thread_db = tdb;
- tdb->td_ta_new_p = &td_ta_new;
-
- err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
- if (err != TD_OK)
- {
- if (debug_threads)
- debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
- free (tdb);
- proc->private->thread_db = NULL;
- return 0;
- }
- tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
- tdb->td_thr_get_info_p = &td_thr_get_info;
- tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
- tdb->td_symbol_list_p = &td_symbol_list;
-
- tdb->td_thr_event_enable_p = &td_thr_event_enable;
-
- 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;
- return 1;
- }
- #else
- static int
- try_thread_db_load_1 (void *handle)
- {
- td_err_e err;
- struct thread_db *tdb;
- struct process_info *proc = current_process ();
- gdb_assert (proc->private->thread_db == NULL);
- tdb = xcalloc (1, sizeof (*tdb));
- proc->private->thread_db = tdb;
- tdb->handle = handle;
-
- #define CHK(required, a) \
- do \
- { \
- if ((a) == NULL) \
- { \
- if (debug_threads) \
- debug_printf ("dlsym: %s\n", dlerror ()); \
- if (required) \
- { \
- free (tdb); \
- proc->private->thread_db = NULL; \
- return 0; \
- } \
- } \
- } \
- while (0)
- CHK (1, tdb->td_ta_new_p = dlsym (handle, "td_ta_new"));
-
- err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
- if (err != TD_OK)
- {
- if (debug_threads)
- debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
- free (tdb);
- proc->private->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 (thread_db_use_events,
- tdb->td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
-
- 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"));
- #undef CHK
- return 1;
- }
- #ifdef HAVE_DLADDR
- static const char *
- dladdr_to_soname (const void *addr)
- {
- Dl_info info;
- if (dladdr (addr, &info) != 0)
- return info.dli_fname;
- return NULL;
- }
- #endif
- static int
- try_thread_db_load (const char *library)
- {
- void *handle;
- if (debug_threads)
- debug_printf ("Trying host libthread_db library: %s.\n",
- library);
- handle = dlopen (library, RTLD_NOW);
- if (handle == NULL)
- {
- if (debug_threads)
- debug_printf ("dlopen failed: %s.\n", dlerror ());
- return 0;
- }
- #ifdef HAVE_DLADDR
- if (debug_threads && strchr (library, '/') == NULL)
- {
- void *td_init;
- td_init = dlsym (handle, "td_init");
- if (td_init != NULL)
- {
- const char *const libpath = dladdr_to_soname (td_init);
- if (libpath != NULL)
- fprintf (stderr, "Host %s resolved to: %s.\n",
- library, libpath);
- }
- }
- #endif
- if (try_thread_db_load_1 (handle))
- return 1;
-
- dlclose (handle);
- return 0;
- }
- static int
- try_thread_db_load_from_sdir (void)
- {
- return try_thread_db_load (LIBTHREAD_DB_SO);
- }
- static int
- try_thread_db_load_from_dir (const char *dir, size_t dir_len)
- {
- char path[PATH_MAX];
- if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
- {
- char *cp = xmalloc (dir_len + 1);
- memcpy (cp, dir, dir_len);
- cp[dir_len] = '\0';
- warning (_("libthread-db-search-path component too long,"
- " ignored: %s."), cp);
- free (cp);
- return 0;
- }
- memcpy (path, dir, dir_len);
- path[dir_len] = '/';
- strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
- return try_thread_db_load (path);
- }
- static int
- thread_db_load_search (void)
- {
- 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);
- 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 int pdir_len = sizeof ("$pdir") - 1;
- size_t this_dir_len;
- this_dir_len = strlen (this_dir);
- if (strncmp (this_dir, "$pdir", pdir_len) == 0
- && (this_dir[pdir_len] == '\0'
- || this_dir[pdir_len] == '/'))
- {
-
- }
- else if (strcmp (this_dir, "$sdir") == 0)
- {
- if (try_thread_db_load_from_sdir ())
- {
- rc = 1;
- break;
- }
- }
- else
- {
- if (try_thread_db_load_from_dir (this_dir, this_dir_len))
- {
- rc = 1;
- break;
- }
- }
- }
- free_char_ptr_vec (dir_vec);
- if (debug_threads)
- debug_printf ("thread_db_load_search returning %d\n", rc);
- return rc;
- }
- #endif
- int
- thread_db_init (int use_events)
- {
- struct process_info *proc = current_process ();
- FIXME
- thread_db_use_events = use_events;
- if (thread_db_load_search ())
- {
- if (use_events && thread_db_enable_reporting () == 0)
- {
-
- thread_db_mourn (proc);
- return 0;
- }
- thread_db_find_new_threads ();
- thread_db_look_up_symbols ();
- return 1;
- }
- return 0;
- }
- static int
- any_thread_of (struct inferior_list_entry *entry, void *args)
- {
- int *pid_p = 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);
- }
- static void
- disable_thread_event_reporting (struct process_info *proc)
- {
- struct thread_db *thread_db = proc->private->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");
- #else
- td_ta_clear_event_p = &td_ta_clear_event;
- #endif
- if (td_ta_clear_event_p != NULL)
- {
- 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_thread = saved_thread;
- }
- }
- }
- static void
- remove_thread_event_breakpoints (struct process_info *proc)
- {
- struct thread_db *thread_db = proc->private->thread_db;
- if (thread_db->td_create_bp != NULL)
- {
- 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_thread = saved_thread;
- }
- }
- void
- thread_db_detach (struct process_info *proc)
- {
- struct thread_db *thread_db = proc->private->thread_db;
- if (thread_db)
- {
- disable_thread_event_reporting (proc);
- remove_thread_event_breakpoints (proc);
- }
- }
- void
- thread_db_mourn (struct process_info *proc)
- {
- struct thread_db *thread_db = proc->private->thread_db;
- if (thread_db)
- {
- td_err_e (*td_ta_delete_p) (td_thragent_t *);
- #ifndef USE_LIBTHREAD_DB_DIRECTLY
- td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
- #else
- td_ta_delete_p = &td_ta_delete;
- #endif
- if (td_ta_delete_p != NULL)
- (*td_ta_delete_p) (thread_db->thread_agent);
- #ifndef USE_LIBTHREAD_DB_DIRECTLY
- dlclose (thread_db->handle);
- #endif
- free (thread_db);
- proc->private->thread_db = NULL;
- }
- }
- int
- thread_db_handle_monitor_command (char *mon)
- {
- const char *cmd = "set libthread-db-search-path";
- size_t cmd_len = strlen (cmd);
- if (strncmp (mon, cmd, cmd_len) == 0
- && (mon[cmd_len] == '\0'
- || mon[cmd_len] == ' '))
- {
- const char *cp = mon + cmd_len;
- if (libthread_db_search_path != NULL)
- free (libthread_db_search_path);
-
- while (isspace (*cp))
- ++cp;
- if (*cp == '\0')
- cp = LIBTHREAD_DB_SEARCH_PATH;
- libthread_db_search_path = xstrdup (cp);
- monitor_output ("libthread-db-search-path set to `");
- monitor_output (libthread_db_search_path);
- monitor_output ("'\n");
- return 1;
- }
-
- return 0;
- }