+/* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
+ relative, or just LIBTHREAD_DB. */
+
+static int
+try_thread_db_load (const char *library)
+{
+ void *handle;
+ struct thread_db_info *info;
+
+ if (info_verbose)
+ printf_unfiltered (_("Trying host libthread_db library: %s.\n"),
+ library);
+ handle = dlopen (library, RTLD_NOW);
+ if (handle == NULL)
+ {
+ if (info_verbose)
+ printf_unfiltered (_("dlopen failed: %s.\n"), dlerror ());
+ return 0;
+ }
+
+ if (info_verbose && 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)
+ printf_unfiltered (_("Host %s resolved to: %s.\n"),
+ library, libpath);
+ }
+ }
+
+ info = add_thread_db_info (handle);
+
+ if (try_thread_db_load_1 (info))
+ return 1;
+
+ /* This library "refused" to work on current inferior. */
+ delete_thread_db_info (GET_PID (inferior_ptid));
+ return 0;
+}
+
+
+/* Search libthread_db_search_path for libthread_db which "agrees"
+ to work on current inferior. */
+
+static int
+thread_db_load_search (void)
+{
+ char path[PATH_MAX];
+ const char *search_path = libthread_db_search_path;
+ int rc = 0;
+
+ while (*search_path)
+ {
+ const char *end = strchr (search_path, ':');
+ if (end)
+ {
+ size_t len = end - search_path;
+ if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
+ {
+ char *cp = xmalloc (len + 1);
+ memcpy (cp, search_path, len);
+ cp[len] = '\0';
+ warning (_("libthread_db_search_path component too long,"
+ " ignored: %s."), cp);
+ xfree (cp);
+ search_path += len + 1;
+ continue;
+ }
+ memcpy (path, search_path, len);
+ path[len] = '\0';
+ search_path += len + 1;
+ }
+ else
+ {
+ size_t len = strlen (search_path);
+
+ if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
+ {
+ warning (_("libthread_db_search_path component too long,"
+ " ignored: %s."), search_path);
+ break;
+ }
+ memcpy (path, search_path, len + 1);
+ search_path += len;
+ }
+ strcat (path, "/");
+ strcat (path, LIBTHREAD_DB_SO);
+ if (try_thread_db_load (path))
+ {
+ rc = 1;
+ break;
+ }
+ }
+ if (rc == 0)
+ rc = try_thread_db_load (LIBTHREAD_DB_SO);
+ return rc;
+}
+
+/* Attempt to load and initialize libthread_db.
+ Return 1 on success.
+ */
+
+static int
+thread_db_load (void)
+{
+ struct objfile *obj;
+ struct thread_db_info *info;
+
+ info = get_thread_db_info (GET_PID (inferior_ptid));
+
+ if (info != NULL)
+ return 1;
+
+ /* Don't attempt to use thread_db on targets which can not run
+ (executables not running yet, core files) for now. */
+ if (!target_has_execution)
+ return 0;
+
+ /* Don't attempt to use thread_db for remote targets. */
+ if (!target_can_run (¤t_target))
+ return 0;
+
+ if (thread_db_load_search ())
+ return 1;
+
+ /* None of the libthread_db's on our search path, not the system default
+ ones worked. If the executable is dynamically linked against
+ libpthread, try loading libthread_db from the same directory. */
+
+ ALL_OBJFILES (obj)
+ if (libpthread_name_p (obj->name))
+ {
+ char path[PATH_MAX], *cp;
+
+ gdb_assert (strlen (obj->name) < sizeof (path));
+ strcpy (path, obj->name);
+ cp = strrchr (path, '/');
+
+ if (cp == NULL)
+ {
+ warning (_("Expected absolute pathname for libpthread in the"
+ " inferior, but got %s."), path);
+ }
+ else if (cp + 1 + strlen (LIBTHREAD_DB_SO) + 1 > path + sizeof (path))
+ {
+ warning (_("Unexpected: path to libpthread in the inferior is"
+ " too long: %s"), path);
+ }
+ else
+ {
+ strcpy (cp + 1, LIBTHREAD_DB_SO);
+ if (try_thread_db_load (path))
+ return 1;
+ }
+ warning (_("Unable to find libthread_db matching inferior's thread"
+ " library, thread debugging will not be available."));
+ return 0;
+ }
+ /* Either this executable isn't using libpthread at all, or it is
+ statically linked. Since we can't easily distinguish these two cases,
+ no warning is issued. */
+ return 0;
+}
+
+static void
+disable_thread_event_reporting (struct thread_db_info *info)
+{
+ if (info->td_ta_clear_event_p != NULL)
+ {
+ td_thr_events_t events;
+
+ /* Set the process wide mask saying we aren't interested in any
+ events anymore. */
+ td_event_fillset (&events);
+ info->td_ta_clear_event_p (info->thread_agent, &events);
+ }
+
+ info->td_create_bp_addr = 0;
+ info->td_death_bp_addr = 0;
+}
+
+static void
+check_thread_signals (void)
+{
+#ifdef GET_THREAD_SIGNALS
+ if (!thread_signals)
+ {
+ sigset_t mask;
+ int i;
+
+ GET_THREAD_SIGNALS (&mask);
+ sigemptyset (&thread_stop_set);
+ sigemptyset (&thread_print_set);
+
+ for (i = 1; i < NSIG; i++)
+ {
+ if (sigismember (&mask, i))
+ {
+ if (signal_stop_update (target_signal_from_host (i), 0))
+ sigaddset (&thread_stop_set, i);
+ if (signal_print_update (target_signal_from_host (i), 0))
+ sigaddset (&thread_print_set, i);
+ thread_signals = 1;
+ }
+ }
+ }
+#endif
+}
+
+/* Check whether thread_db is usable. This function is called when
+ an inferior is created (or otherwise acquired, e.g. attached to)
+ and when new shared libraries are loaded into a running process. */
+
+void
+check_for_thread_db (void)
+{
+ td_err_e err;
+ static void *last_loaded;
+
+ /* Do nothing if we couldn't load libthread_db.so.1. */
+ if (!thread_db_load ())
+ return;
+}