+/* 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);
+ thread_info *thread_info = find_thread_ptid (linux_target, ptid);
+ if (thread_info != NULL && thread_info->priv != NULL)
+ {
+ LOG ("; errno");
+
+ scoped_restore_current_thread restore_current_thread;
+ switch_to_thread (thread_info);
+
+ 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 (const gdb_exception_error &except)
+ {
+ if (warning_pre_print)
+ fputs_unfiltered (warning_pre_print, gdb_stderr);
+
+ exception_fprintf (gdb_stderr, except,
+ _("libthread_db integrity checks failed: "));
+
+ test_passed = false;
+ }
+
+ if (test_passed && log_progress)
+ debug_printf (_("libthread_db integrity checks passed.\n"));
+
+ tdb_testinfo = NULL;
+
+ linux_unstop_all_lwps ();
+
+ return test_passed;
+}
+