Re: [patch] Allow gdbserver to dynamically lookup libthread_db.so.1

Paul Pluzhnikov
2009-10-16T00:41:31Z

[-- Attachment #1: Type: text/plain, Size: 992 bytes --]

On Thu, Oct 8, 2009 at 12:42 PM, Daniel Jacobowitz <drow@false.org> wrote:
> On Thu, Oct 08, 2009 at 12:20:53PM -0700, Paul Pluzhnikov wrote:

>> I would prefer to do it as a follow-up patch though.
>> Would that be ok?
>
> Oh, absolutely. This isn't a common use case, just one I've found
> handy. As usual, I appreciate how helpful you are!

Here is the promised follow-up patch.

If gdbserver is configured with
--with-libthread-db=/path/to/libthread.db.{a,so}
then we link with the given library instead of libdl, and directly import
all the symbols from it.

Tested by configuring --with-libthread-db=/lib64/libthread_db.so.1 and
running 'make check RUNTESTFLAGS="--target_board native-gdbserver"'.
No regressions.

Thanks,
--
Paul Pluzhnikov

2009-10-15 Paul Pluzhnikov <ppluzhnikov@google.com>

* configure.ac: New --with-libthread-db option.
* thread-db.c: Allow direct dependence on libthread_db.
(thread_db_free): Adjust.
* config.in: Regenerate.
* configure: Likewise.

[-- Attachment #2: gdb-gdbserver-threaddb-search-20091015.txt --]
[-- Type: text/plain, Size: 5680 bytes --]

Index: configure.ac
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/configure.ac,v
retrieving revision 1.27
diff -u -p -u -r1.27 configure.ac
--- configure.ac 9 Oct 2009 00:31:01 -0000 1.27
+++ configure.ac 16 Oct 2009 00:13:49 -0000
@@ -158,6 +158,13 @@ if test "$srv_linux_thread_db" = "yes";
fi
fi

+AC_ARG_WITH(libthread-db,
+AS_HELP_STRING([--with-libthread-db=PATH], [use given libthread_db directly]),
+[srv_libthread_db_path="${withval}"
+ AC_DEFINE(USE_LIBTHREAD_DB_DIRECTLY, 1, [Define if we should use libthread_db directly.])
+ srv_libs="$srv_libthread_db_path"
+])
+
if test "$srv_xmlfiles" != ""; then
srv_xmlbuiltin="xml-builtin.o"
AC_DEFINE(USE_XML, 1, [Define if an XML target description is available.])
Index: thread-db.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/thread-db.c,v
retrieving revision 1.24
diff -u -p -u -r1.24 thread-db.c
--- thread-db.c 9 Oct 2009 00:31:01 -0000 1.24
+++ thread-db.c 16 Oct 2009 00:38:43 -0000
@@ -30,7 +30,10 @@ static int thread_db_use_events;
#include "gdb_proc_service.h"
#include "../gdb_thread_db.h"

+#ifndef USE_LIBTHREAD_DB_DIRECTLY
#include <dlfcn.h>
+#endif
+
#include <stdint.h>
#include <limits.h>
#include <ctype.h>
@@ -44,8 +47,10 @@ struct thread_db
/* Connection to the libthread_db library. */
td_thragent_t *thread_agent;

+#ifndef USE_LIBTHREAD_DB_DIRECTLY
/* Handle of the libthread_db from dlopen. */
void *handle;
+#endif

/* Addresses of libthread_db functions. */
td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, td_thragent_t **ta);
@@ -435,6 +440,51 @@ thread_db_get_tls_address (struct thread
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 ();
+
+ if (proc->private->thread_db != NULL)
+ fatal ("unexpected: proc->private->thread_db != NULL");
+
+ tdb.td_ta_new_p = &td_ta_new;
+
+ /* Attempt to open a connection to the thread library. */
+ err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
+ if (err != TD_OK)
+ {
+ if (debug_threads)
+ fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
+ 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;
+
+ /* This is required only when thread_db_use_events is on. */
+ tdb.td_thr_event_enable_p = &td_thr_event_enable;
+
+ /* These are not essential. */
+ 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;
+
+ proc->private->thread_db = xmalloc (sizeof (tdb));
+ memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
+
+ return 1;
+}
+
+#else
+
static int
try_thread_db_load_1 (void *handle)
{
@@ -613,6 +663,8 @@ thread_db_load_search (void)
return rc;
}

+#endif /* USE_LIBTHREAD_DB_DIRECTLY */
+
int
thread_db_init (int use_events)
{
@@ -656,6 +708,7 @@ thread_db_free (struct process_info *pro
struct thread_db *thread_db = proc->private->thread_db;
if (thread_db)
{
+#ifndef USE_LIBTHREAD_DB_DIRECTLY
td_err_e (*td_ta_delete_p) (td_thragent_t *);

td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
@@ -663,6 +716,10 @@ thread_db_free (struct process_info *pro
(*td_ta_delete_p) (thread_db->thread_agent);

dlclose (thread_db->handle);
+#else
+ td_ta_delete (thread_db->thread_agent);
+#endif /* USE_LIBTHREAD_DB_DIRECTLY */
+
free (thread_db);
proc->private->thread_db = NULL;
}
Index: config.in
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/config.in,v
retrieving revision 1.24
diff -u -p -u -r1.24 config.in
--- config.in 9 Oct 2009 00:31:01 -0000 1.24
+++ config.in 16 Oct 2009 00:13:49 -0000
@@ -175,6 +175,9 @@
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS

+/* Define if we should use libthread_db directly. */
+#undef USE_LIBTHREAD_DB_DIRECTLY
+
/* Enable extensions on AIX 3, Interix. */
#ifndef _ALL_SOURCE
# undef _ALL_SOURCE
Index: configure
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/configure,v
retrieving revision 1.41
diff -u -p -u -r1.41 configure
--- configure 9 Oct 2009 00:31:01 -0000 1.41
+++ configure 16 Oct 2009 00:13:49 -0000
@@ -668,6 +668,7 @@ ac_user_opts='
enable_option_checking
with_pkgversion
with_bugurl
+with_libthread_db
'
ac_precious_vars='build_alias
host_alias
@@ -1297,6 +1298,8 @@ Optional Packages:
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-pkgversion=PKG Use PKG in the version string in place of "GDB"
--with-bugurl=URL Direct users to URL to report a bug
+ --with-libthread-db=PATH
+ use given libthread_db directly

Some influential environment variables:
CC C compiler command
@@ -4268,6 +4271,18 @@ $as_echo "#define HAVE_TD_VERSION 1" >>c
fi
fi

+
+# Check whether --with-libthread-db was given.
+if test "${with_libthread_db+set}" = set; then :
+ withval=$with_libthread_db; srv_libthread_db_path="${withval}"
+
+$as_echo "#define USE_LIBTHREAD_DB_DIRECTLY 1" >>confdefs.h
+
+ srv_libs="$srv_libthread_db_path"
+
+fi
+
+
if test "$srv_xmlfiles" != ""; then
srv_xmlbuiltin="xml-builtin.o"