#include "nat/linux-namespaces.h"
#include <algorithm>
#include "common/pathstuff.h"
+#include "valprint.h"
/* GNU/Linux libthread_db support.
by the "set auto-load libthread-db" command. */
static int auto_load_thread_db = 1;
+/* Set to non-zero if load-time libthread_db tests have been enabled
+ by the "maintenence set check-libthread-db" command. */
+static int check_thread_db_on_load = 0;
+
/* "show" command for the auto_load_thread_db configuration variable. */
static void
bookkeeping. */
struct thread_db_info *thread_db_list;
-static void thread_db_find_new_threads_1 (ptid_t ptid);
-static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new);
+static void thread_db_find_new_threads_1 (thread_info *stopped);
+static void thread_db_find_new_threads_2 (thread_info *stopped,
+ bool until_no_new);
static void check_thread_signals (void);
{
struct thread_db_info *info = XCNEW (struct thread_db_info);
- info->pid = ptid_get_pid (inferior_ptid);
+ info->pid = inferior_ptid.pid ();
info->handle = handle;
/* The workaround works by reading from /proc/pid/status, so it is
}
}
-/* Fetch the user-level thread id of PTID. */
+/* Fetch the user-level thread id of PTID. STOPPED is a stopped
+ thread that we can use to access memory. */
static struct thread_info *
-thread_from_lwp (ptid_t ptid)
+thread_from_lwp (thread_info *stopped, ptid_t ptid)
{
td_thrhandle_t th;
td_thrinfo_t ti;
/* This ptid comes from linux-nat.c, which should always fill in the
LWP. */
- gdb_assert (ptid_get_lwp (ptid) != 0);
+ gdb_assert (ptid.lwp () != 0);
- info = get_thread_db_info (ptid_get_pid (ptid));
+ info = get_thread_db_info (ptid.pid ());
/* Access an lwp we know is stopped. */
- info->proc_handle.ptid = ptid;
- err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid_get_lwp (ptid),
+ info->proc_handle.thread = stopped;
+ err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid.lwp (),
&th);
if (err != TD_OK)
error (_("Cannot find user-level thread for LWP %ld: %s"),
- ptid_get_lwp (ptid), thread_db_err_str (err));
+ ptid.lwp (), thread_db_err_str (err));
err = info->td_thr_get_info_p (&th, &ti);
if (err != TD_OK)
{
struct thread_db_info *info;
- info = get_thread_db_info (ptid_get_pid (child));
+ info = get_thread_db_info (child.pid ());
if (info == NULL)
return 0;
- thread_from_lwp (child);
+ thread_info *stopped = find_thread_ptid (parent);
+
+ thread_from_lwp (stopped, child);
- /* If we do not know about the main thread yet, this would be a good
- time to find it. */
- thread_from_lwp (parent);
+ /* If we do not know about the main thread's pthread info yet, this
+ would be a good time to find it. */
+ thread_from_lwp (stopped, parent);
return 1;
}
otherwise. */
static int
-thread_db_find_new_threads_silently (ptid_t ptid)
+thread_db_find_new_threads_silently (thread_info *stopped)
{
TRY
{
- thread_db_find_new_threads_2 (ptid, 1);
+ thread_db_find_new_threads_2 (stopped, true);
}
CATCH (except, RETURN_MASK_ERROR)
return NULL;
}
+/* State for check_thread_db_callback. */
+
+struct check_thread_db_info
+{
+ /* The libthread_db under test. */
+ struct thread_db_info *info;
+
+ /* True if progress should be logged. */
+ bool log_progress;
+
+ /* True if the callback was called. */
+ bool threads_seen;
+
+ /* Name of last libthread_db function called. */
+ const char *last_call;
+
+ /* Value returned by last libthread_db call. */
+ td_err_e last_result;
+};
+
+static struct check_thread_db_info *tdb_testinfo;
+
+/* Callback for check_thread_db. */
+
+static int
+check_thread_db_callback (const td_thrhandle_t *th, void *arg)
+{
+ gdb_assert (tdb_testinfo != NULL);
+ tdb_testinfo->threads_seen = true;
+
+#define LOG(fmt, args...) \
+ do \
+ { \
+ if (tdb_testinfo->log_progress) \
+ { \
+ debug_printf (fmt, ## args); \
+ gdb_flush (gdb_stdlog); \
+ } \
+ } \
+ while (0)
+
+#define CHECK_1(expr, args...) \
+ do \
+ { \
+ if (!(expr)) \
+ { \
+ LOG (" ... FAIL!\n"); \
+ error (args); \
+ } \
+ } \
+ while (0)
+
+#define CHECK(expr) \
+ CHECK_1 (expr, "(%s) == false", #expr)
+
+#define CALL_UNCHECKED(func, args...) \
+ do \
+ { \
+ tdb_testinfo->last_call = #func; \
+ tdb_testinfo->last_result \
+ = tdb_testinfo->info->func ## _p (args); \
+ } \
+ while (0)
+
+#define CHECK_CALL() \
+ CHECK_1 (tdb_testinfo->last_result == TD_OK, \
+ _("%s failed: %s"), \
+ tdb_testinfo->last_call, \
+ thread_db_err_str (tdb_testinfo->last_result)) \
+
+#define CALL(func, args...) \
+ do \
+ { \
+ CALL_UNCHECKED (func, args); \
+ CHECK_CALL (); \
+ } \
+ while (0)
+
+ LOG (" Got thread");
+
+ /* Check td_ta_thr_iter passed consistent arguments. */
+ CHECK (th != NULL);
+ CHECK (arg == (void *) tdb_testinfo);
+ CHECK (th->th_ta_p == tdb_testinfo->info->thread_agent);
+
+ LOG (" %s", core_addr_to_string_nz ((CORE_ADDR) th->th_unique));
+
+ /* Check td_thr_get_info. */
+ td_thrinfo_t ti;
+ CALL (td_thr_get_info, th, &ti);
+
+ LOG (" => %d", ti.ti_lid);
+
+ CHECK (ti.ti_ta_p == th->th_ta_p);
+ CHECK (ti.ti_tid == (thread_t) th->th_unique);
+
+ /* Check td_ta_map_lwp2thr. */
+ td_thrhandle_t th2;
+ 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)
+ {
+ /* Some platforms require execution for td_ta_map_lwp2thr. */
+ LOG (_("; can't map_lwp2thr"));
+ }
+ else
+ {
+ CHECK_CALL ();
+
+ LOG (" => %s", core_addr_to_string_nz ((CORE_ADDR) th2.th_unique));
+
+ CHECK (memcmp (th, &th2, sizeof (td_thrhandle_t)) == 0);
+ }
+
+ /* Attempt TLS access. Assuming errno is TLS, this calls
+ thread_db_get_thread_local_address, which in turn calls
+ td_thr_tls_get_addr for live inferiors or td_thr_tlsbase
+ for core files. This test is skipped if the thread has
+ not been recorded; proceeding in that case would result
+ in the test having the side-effect of noticing threads
+ which seems wrong.
+
+ Note that in glibc's libthread_db td_thr_tls_get_addr is
+ a thin wrapper around td_thr_tlsbase; this check always
+ hits the bulk of the code.
+
+ Note also that we don't actually check any libthread_db
+ calls are made, we just assume they were; future changes
+ to how GDB accesses TLS could result in this passing
+ without exercising the calls it's supposed to. */
+ ptid_t ptid = ptid_t (tdb_testinfo->info->pid, ti.ti_lid, 0);
+ struct thread_info *thread_info = find_thread_ptid (ptid);
+ if (thread_info != NULL && thread_info->priv != NULL)
+ {
+ LOG ("; errno");
+
+ scoped_restore_current_thread restore_current_thread;
+ switch_to_thread (ptid);
+
+ expression_up expr = parse_expression ("(int) errno");
+ struct value *val = evaluate_expression (expr.get ());
+
+ if (tdb_testinfo->log_progress)
+ {
+ struct value_print_options opts;
+
+ get_user_print_options (&opts);
+ LOG (" = ");
+ value_print (val, gdb_stdlog, &opts);
+ }
+ }
+
+ LOG (" ... OK\n");
+
+#undef LOG
+#undef CHECK_1
+#undef CHECK
+#undef CALL_UNCHECKED
+#undef CHECK_CALL
+#undef CALL
+
+ return 0;
+}
+
+/* Run integrity checks on the dlopen()ed libthread_db described by
+ INFO. Returns true on success, displays a warning and returns
+ false on failure. Logs progress messages to gdb_stdlog during
+ the test if LOG_PROGRESS is true. */
+
+static bool
+check_thread_db (struct thread_db_info *info, bool log_progress)
+{
+ bool test_passed = true;
+
+ if (log_progress)
+ debug_printf (_("Running libthread_db integrity checks:\n"));
+
+ /* GDB avoids using td_ta_thr_iter wherever possible (see comment
+ in try_thread_db_load_1 below) so in order to test it we may
+ have to locate it ourselves. */
+ td_ta_thr_iter_ftype *td_ta_thr_iter_p = info->td_ta_thr_iter_p;
+ if (td_ta_thr_iter_p == NULL)
+ {
+ void *thr_iter = verbose_dlsym (info->handle, "td_ta_thr_iter");
+ if (thr_iter == NULL)
+ return 0;
+
+ td_ta_thr_iter_p = (td_ta_thr_iter_ftype *) thr_iter;
+ }
+
+ /* Set up the test state we share with the callback. */
+ gdb_assert (tdb_testinfo == NULL);
+ struct check_thread_db_info tdb_testinfo_buf;
+ tdb_testinfo = &tdb_testinfo_buf;
+
+ memset (tdb_testinfo, 0, sizeof (struct check_thread_db_info));
+ tdb_testinfo->info = info;
+ tdb_testinfo->log_progress = log_progress;
+
+ /* td_ta_thr_iter shouldn't be used on running processes. Note that
+ it's possible the inferior will stop midway through modifying one
+ of its thread lists, in which case the check will spuriously
+ fail. */
+ linux_stop_and_wait_all_lwps ();
+
+ TRY
+ {
+ td_err_e err = td_ta_thr_iter_p (info->thread_agent,
+ check_thread_db_callback,
+ tdb_testinfo,
+ TD_THR_ANY_STATE,
+ TD_THR_LOWEST_PRIORITY,
+ TD_SIGNO_MASK,
+ TD_THR_ANY_USER_FLAGS);
+
+ if (err != TD_OK)
+ error (_("td_ta_thr_iter failed: %s"), thread_db_err_str (err));
+
+ if (!tdb_testinfo->threads_seen)
+ error (_("no threads seen"));
+ }
+ CATCH (except, RETURN_MASK_ERROR)
+ {
+ if (warning_pre_print)
+ fputs_unfiltered (warning_pre_print, gdb_stderr);
+
+ exception_fprintf (gdb_stderr, except,
+ _("libthread_db integrity checks failed: "));
+
+ test_passed = false;
+ }
+ END_CATCH
+
+ if (test_passed && log_progress)
+ debug_printf (_("libthread_db integrity checks passed.\n"));
+
+ tdb_testinfo = NULL;
+
+ linux_unstop_all_lwps ();
+
+ return test_passed;
+}
+
/* Attempt to initialize dlopen()ed libthread_db, described by INFO.
Return 1 on success.
Failure could happen if libthread_db does not have symbols we expect,
CHK (TDB_VERBOSE_DLSYM (info, td_ta_new));
/* Initialize the structure that identifies the child process. */
- info->proc_handle.ptid = inferior_ptid;
+ info->proc_handle.thread = inferior_thread ();
/* Now attempt to open a connection to the thread library. */
err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
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
- && linux_proc_task_list_dir_exists (ptid_get_pid (inferior_ptid)))
+ && linux_proc_task_list_dir_exists (inferior_ptid.pid ()))
info->td_ta_thr_iter_p = NULL;
else
CHK (TDB_VERBOSE_DLSYM (info, td_ta_thr_iter));
#undef TDB_DLSYM
#undef CHK
+ /* Run integrity checks if requested. */
+ if (check_thread_db_on_load)
+ {
+ if (!check_thread_db (info, libthread_db_debug))
+ return 0;
+ }
+
if (info->td_ta_thr_iter_p == NULL)
{
struct lwp_info *lp;
- int pid = ptid_get_pid (inferior_ptid);
+ int pid = inferior_ptid.pid ();
+ thread_info *curr_thread = inferior_thread ();
linux_stop_and_wait_all_lwps ();
ALL_LWPS (lp)
- if (ptid_get_pid (lp->ptid) == pid)
- thread_from_lwp (lp->ptid);
+ if (lp->ptid.pid () == pid)
+ thread_from_lwp (curr_thread, lp->ptid);
linux_unstop_all_lwps ();
}
- else if (thread_db_find_new_threads_silently (inferior_ptid) != 0)
+ 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
return 1;
/* This library "refused" to work on current inferior. */
- delete_thread_db_info (ptid_get_pid (inferior_ptid));
+ delete_thread_db_info (inferior_ptid.pid ());
return 0;
}
{
struct thread_db_info *info;
- info = get_thread_db_info (ptid_get_pid (inferior_ptid));
+ info = get_thread_db_info (inferior_ptid.pid ());
if (info != NULL)
return 1;
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 (ptid_get_pid (inferior_ptid), LINUX_NS_PID))
+ if (!linux_ns_same (inferior_ptid.pid (), LINUX_NS_PID))
{
warning (_ ("Target and debugger are in different PID "
"namespaces; thread lists and other data are "
void
thread_db_target::detach (inferior *inf, int from_tty)
{
- struct target_ops *target_beneath = find_target_beneath (this);
-
delete_thread_db_info (inf->pid);
- target_beneath->detach (inf, from_tty);
+ beneath ()->detach (inf, from_tty);
/* NOTE: From this point on, inferior_ptid is null_ptid. */
int options)
{
struct thread_db_info *info;
- struct target_ops *beneath = find_target_beneath (this);
- ptid = beneath->wait (ptid, ourstatus, options);
+ ptid = beneath ()->wait (ptid, ourstatus, options);
switch (ourstatus->kind)
{
return ptid;
}
- info = get_thread_db_info (ptid_get_pid (ptid));
+ info = get_thread_db_info (ptid.pid ());
/* If this process isn't using thread_db, we're done. */
if (info == NULL)
{
/* New image, it may or may not end up using thread_db. Assume
not unless we find otherwise. */
- delete_thread_db_info (ptid_get_pid (ptid));
+ delete_thread_db_info (ptid.pid ());
if (!thread_db_list)
unpush_target (&the_thread_db_target);
}
/* Fill in the thread's user-level thread id and status. */
- thread_from_lwp (ptid);
+ thread_from_lwp (find_thread_ptid (ptid), ptid);
return ptid;
}
void
thread_db_target::mourn_inferior ()
{
- struct target_ops *target_beneath = find_target_beneath (this);
-
- delete_thread_db_info (ptid_get_pid (inferior_ptid));
+ delete_thread_db_info (inferior_ptid.pid ());
- target_beneath->mourn_inferior ();
+ beneath ()->mourn_inferior ();
/* Detach thread_db target ops. */
if (!thread_db_list)
{
td_thrinfo_t ti;
td_err_e err;
- ptid_t ptid;
struct thread_info *tp;
struct callback_data *cb_data = (struct callback_data *) data;
struct thread_db_info *info = cb_data->info;
return 0;
}
- ptid = ptid_build (info->pid, ti.ti_lid, 0);
+ ptid_t ptid (info->pid, ti.ti_lid);
tp = find_thread_ptid (ptid);
if (tp == NULL || tp->priv == NULL)
record_thread (info, tp, ptid, th_p, &ti);
searches in a row do not discover any new threads. */
static void
-thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new)
+thread_db_find_new_threads_2 (thread_info *stopped, bool until_no_new)
{
td_err_e err = TD_OK;
struct thread_db_info *info;
int i, loop;
- info = get_thread_db_info (ptid_get_pid (ptid));
+ info = get_thread_db_info (stopped->ptid.pid ());
/* Access an lwp we know is stopped. */
- info->proc_handle.ptid = ptid;
+ info->proc_handle.thread = stopped;
if (until_no_new)
{
}
static void
-thread_db_find_new_threads_1 (ptid_t ptid)
+thread_db_find_new_threads_1 (thread_info *stopped)
{
- thread_db_find_new_threads_2 (ptid, 0);
+ thread_db_find_new_threads_2 (stopped, 0);
}
/* Implement the to_update_thread_list target method for this
if (info == NULL)
continue;
- thread = any_live_thread_of_process (inf->pid);
+ thread = any_live_thread_of_inferior (inf);
if (thread == NULL || thread->executing)
continue;
if (target_has_execution_1 (thread->ptid))
continue;
- thread_db_find_new_threads_1 (thread->ptid);
+ thread_db_find_new_threads_1 (thread);
}
/* Give the beneath target a chance to do extra processing. */
- this->beneath->update_thread_list ();
+ this->beneath ()->update_thread_list ();
}
const char *
thread_db_target::pid_to_str (ptid_t ptid)
{
struct thread_info *thread_info = find_thread_ptid (ptid);
- struct target_ops *beneath;
if (thread_info != NULL && thread_info->priv != NULL)
{
thread_db_thread_info *priv = get_thread_db_thread_info (thread_info);
snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
- (unsigned long) priv->tid, ptid_get_lwp (ptid));
+ (unsigned long) priv->tid, ptid.lwp ());
return buf;
}
- beneath = find_target_beneath (this);
- return beneath->pid_to_str (ptid);
+ return beneath ()->pid_to_str (ptid);
}
/* Return a string describing the state of the thread specified by
CORE_ADDR offset)
{
struct thread_info *thread_info;
- struct target_ops *beneath;
/* Find the matching thread. */
thread_info = find_thread_ptid (ptid);
/* We may not have discovered the thread yet. */
if (thread_info != NULL && thread_info->priv == NULL)
- thread_info = thread_from_lwp (ptid);
+ thread_info = thread_from_lwp (thread_info, ptid);
if (thread_info != NULL && thread_info->priv != NULL)
{
td_err_e err;
psaddr_t address;
- thread_db_info *info = get_thread_db_info (ptid_get_pid (ptid));
+ thread_db_info *info = get_thread_db_info (ptid.pid ());
thread_db_thread_info *priv = get_thread_db_thread_info (thread_info);
/* Finally, get the address of the variable. */
: (CORE_ADDR) (uintptr_t) address);
}
- beneath = find_target_beneath (this);
- return beneath->get_thread_local_address (ptid, lm, offset);
+ return beneath ()->get_thread_local_address (ptid, lm, offset);
}
/* Implement the to_get_ada_task_ptid target method for this target. */
thread_db_target::get_ada_task_ptid (long lwp, long thread)
{
/* NPTL uses a 1:1 model, so the LWP id suffices. */
- return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
+ return ptid_t (inferior_ptid.pid (), lwp, 0);
}
void
thread_db_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
{
- struct target_ops *beneath = find_target_beneath (this);
struct thread_db_info *info;
- if (ptid_equal (ptid, minus_one_ptid))
- info = get_thread_db_info (ptid_get_pid (inferior_ptid));
+ if (ptid == minus_one_ptid)
+ info = get_thread_db_info (inferior_ptid.pid ());
else
- info = get_thread_db_info (ptid_get_pid (ptid));
+ info = get_thread_db_info (ptid.pid ());
/* This workaround is only needed for child fork lwps stopped in a
PTRACE_O_TRACEFORK event. When the inferior is resumed, the
if (info)
info->need_stale_parent_threads_check = 0;
- beneath->resume (ptid, step, signo);
+ beneath ()->resume (ptid, step, signo);
}
/* std::sort helper function for info_auto_load_libthread_db, sort the
uiout->message (_("No auto-loaded libthread-db.\n"));
}
+/* Implement 'maintenance check libthread-db'. */
+
+static void
+maintenance_check_libthread_db (const char *args, int from_tty)
+{
+ int inferior_pid = inferior_ptid.pid ();
+ struct thread_db_info *info;
+
+ if (inferior_pid == 0)
+ error (_("No inferior running"));
+
+ info = get_thread_db_info (inferior_pid);
+ if (info == NULL)
+ error (_("No libthread_db loaded"));
+
+ check_thread_db (info, true);
+}
+
void
_initialize_thread_db (void)
{
Usage: info auto-load libthread-db"),
auto_load_info_cmdlist_get ());
+ add_cmd ("libthread-db", class_maintenance,
+ maintenance_check_libthread_db, _("\
+Run integrity checks on the current inferior's libthread_db."),
+ &maintenancechecklist);
+
+ add_setshow_boolean_cmd ("check-libthread-db",
+ class_maintenance,
+ &check_thread_db_on_load, _("\
+Set whether to check libthread_db at load time."), _("\
+Show whether to check libthread_db at load time."), _("\
+If enabled GDB will run integrity checks on inferior specific libthread_db\n\
+as they are loaded."),
+ NULL,
+ NULL,
+ &maintenance_set_cmdlist,
+ &maintenance_show_cmdlist);
+
/* Add ourselves to objfile event chain. */
gdb::observers::new_objfile.attach (thread_db_new_objfile);