diff options
Diffstat (limited to 'libdwfl')
-rw-r--r-- | libdwfl/ChangeLog | 17 | ||||
-rw-r--r-- | libdwfl/Makefile.am | 6 | ||||
-rw-r--r-- | libdwfl/debuginfod-client.c | 131 | ||||
-rw-r--r-- | libdwfl/dwfl_build_id_find_elf.c | 14 | ||||
-rw-r--r-- | libdwfl/dwfl_end.c | 2 | ||||
-rw-r--r-- | libdwfl/dwfl_frame.c | 66 | ||||
-rw-r--r-- | libdwfl/find-debuginfo.c | 8 | ||||
-rw-r--r-- | libdwfl/libdwflP.h | 18 |
8 files changed, 209 insertions, 53 deletions
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 04a39637..b8222189 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,20 @@ +2019-10-28 Aaron Merey <[email protected]> + + * dwfl_build_id_find_elf.c (dwfl_build_id_find_elf): Call debuginfod + client functions via dlopen to look for elf/dwarf files as fallback. + * find-debuginfo.c (dwfl_standard_find_debuginfo): Ditto. + +2019-10-07 Omar Sandoval <[email protected]> + + * dwfl_frame.c (dwfl_getthreads): Get rid of unnecessary + thread_free_all_states calls. + (getthread): Ditto. + (state_free): Remove function. + (thread_free_all_states): Remove function. + (free_states): Add function. + (dwfl_thread_getframes): Don't update thread->unwound while unwinding. + * libdwflP.h (struct Dwfl_Thread): Update comment for unwound member. + 2019-08-12 Mark Wielaard <[email protected]> * gzip.c (open_stream): Return DWFL_E_ERRNO on bad file operation. diff --git a/libdwfl/Makefile.am b/libdwfl/Makefile.am index d9d13b28..af77bb0c 100644 --- a/libdwfl/Makefile.am +++ b/libdwfl/Makefile.am @@ -31,7 +31,7 @@ ## include $(top_srcdir)/config/eu.am AM_CPPFLAGS += -I$(srcdir) -I$(srcdir)/../libelf -I$(srcdir)/../libebl \ - -I$(srcdir)/../libdw -I$(srcdir)/../libdwelf + -I$(srcdir)/../libdw -I$(srcdir)/../libdwelf -I$(srcdir)/../debuginfod VERSION = 1 noinst_LIBRARIES = libdwfl.a @@ -39,7 +39,7 @@ noinst_LIBRARIES += libdwfl_pic.a pkginclude_HEADERS = libdwfl.h -libdwfl_a_CFLAGS = -fPIC -fvisibility=hidden $(AM_CFLAGS) +libdwfl_a_CFLAGS = -fPIC $(AM_CFLAGS) libdwfl_a_SOURCES = dwfl_begin.c dwfl_end.c dwfl_error.c dwfl_version.c \ dwfl_module.c dwfl_report_elf.c relocate.c \ dwfl_module_build_id.c dwfl_module_report_build_id.c \ @@ -70,7 +70,7 @@ libdwfl_a_SOURCES = dwfl_begin.c dwfl_end.c dwfl_error.c dwfl_version.c \ link_map.c core-file.c open.c image-header.c \ dwfl_frame.c frame_unwind.c dwfl_frame_pc.c \ linux-pid-attach.c linux-core-attach.c dwfl_frame_regs.c \ - gzip.c + gzip.c debuginfod-client.c if BZLIB libdwfl_a_SOURCES += bzip2.c diff --git a/libdwfl/debuginfod-client.c b/libdwfl/debuginfod-client.c new file mode 100644 index 00000000..ee604ad9 --- /dev/null +++ b/libdwfl/debuginfod-client.c @@ -0,0 +1,131 @@ +/* Try to get an ELF or debug file through the debuginfod. + Copyright (C) 2019 Red Hat, Inc. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "libdwflP.h" +#include <dlfcn.h> + +static __typeof__ (debuginfod_begin) *fp_debuginfod_begin; +static __typeof__ (debuginfod_find_executable) *fp_debuginfod_find_executable; +static __typeof__ (debuginfod_find_debuginfo) *fp_debuginfod_find_debuginfo; +static __typeof__ (debuginfod_end) *fp_debuginfod_end; + +/* NB: this is slightly thread-unsafe */ + +static debuginfod_client * +get_client (Dwfl *dwfl) +{ + if (dwfl->debuginfod != NULL) + return dwfl->debuginfod; + + if (fp_debuginfod_begin != NULL) + { + dwfl->debuginfod = (*fp_debuginfod_begin) (); + return dwfl->debuginfod; + } + + return NULL; +} + +int +__libdwfl_debuginfod_find_executable (Dwfl *dwfl, + const unsigned char *build_id_bits, + size_t build_id_len) +{ + int fd = -1; + if (build_id_len > 0) + { + debuginfod_client *c = get_client (dwfl); + if (c != NULL) + fd = (*fp_debuginfod_find_executable) (c, build_id_bits, + build_id_len, NULL); + } + + return fd; +} + +int +__libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl, + const unsigned char *build_id_bits, + size_t build_id_len) +{ + int fd = -1; + if (build_id_len > 0) + { + debuginfod_client *c = get_client (dwfl); + if (c != NULL) + fd = (*fp_debuginfod_find_debuginfo) (c, build_id_bits, + build_id_len, NULL); + } + + return fd; +} + +void +__libdwfl_debuginfod_end (debuginfod_client *c) +{ + if (c != NULL) + (*fp_debuginfod_end) (c); +} + +/* Try to get the libdebuginfod library functions to make sure + everything is initialized early. */ +void __attribute__ ((constructor)) +__libdwfl_debuginfod_init (void) +{ + void *debuginfod_so = dlopen("libdebuginfod-" VERSION ".so", RTLD_LAZY); + + if (debuginfod_so == NULL) + debuginfod_so = dlopen("libdebuginfod.so", RTLD_LAZY); + + if (debuginfod_so != NULL) + { + fp_debuginfod_begin = dlsym (debuginfod_so, "debuginfod_begin"); + fp_debuginfod_find_executable = dlsym (debuginfod_so, + "debuginfod_find_executable"); + fp_debuginfod_find_debuginfo = dlsym (debuginfod_so, + "debuginfod_find_debuginfo"); + fp_debuginfod_end = dlsym (debuginfod_so, "debuginfod_end"); + + /* We either get them all, or we get none. */ + if (fp_debuginfod_begin == NULL + || fp_debuginfod_find_executable == NULL + || fp_debuginfod_find_debuginfo == NULL + || fp_debuginfod_end == NULL) + { + fp_debuginfod_begin = NULL; + fp_debuginfod_find_executable = NULL; + fp_debuginfod_find_debuginfo = NULL; + fp_debuginfod_end = NULL; + dlclose (debuginfod_so); + } + } +} diff --git a/libdwfl/dwfl_build_id_find_elf.c b/libdwfl/dwfl_build_id_find_elf.c index cc6c3f62..4e56143f 100644 --- a/libdwfl/dwfl_build_id_find_elf.c +++ b/libdwfl/dwfl_build_id_find_elf.c @@ -1,5 +1,5 @@ /* Find an ELF file for a module from its build ID. - Copyright (C) 2007-2010, 2014, 2015 Red Hat, Inc. + Copyright (C) 2007-2010, 2014, 2015, 2019 Red Hat, Inc. This file is part of elfutils. This file is free software; you can redistribute it and/or modify @@ -187,7 +187,17 @@ dwfl_build_id_find_elf (Dwfl_Module *mod, free (*file_name); *file_name = NULL; } - else if (errno == 0 && mod->build_id_len > 0) + else + { + /* If all else fails and a build-id is available, query the + debuginfo-server if enabled. */ + if (fd < 0 && mod->build_id_len > 0) + fd = __libdwfl_debuginfod_find_executable (mod->dwfl, + mod->build_id_bits, + mod->build_id_len); + } + + if (fd < 0 && errno == 0 && mod->build_id_len > 0) /* Setting this with no file yet loaded is a marker that the build ID is authoritative even if we also know a putative *FILE_NAME. */ diff --git a/libdwfl/dwfl_end.c b/libdwfl/dwfl_end.c index 74ee9e07..4f6c722a 100644 --- a/libdwfl/dwfl_end.c +++ b/libdwfl/dwfl_end.c @@ -39,6 +39,8 @@ dwfl_end (Dwfl *dwfl) if (dwfl == NULL) return; + __libdwfl_debuginfod_end (dwfl->debuginfod); + if (dwfl->process) __libdwfl_process_free (dwfl->process); diff --git a/libdwfl/dwfl_frame.c b/libdwfl/dwfl_frame.c index 881f735a..5bbf850e 100644 --- a/libdwfl/dwfl_frame.c +++ b/libdwfl/dwfl_frame.c @@ -71,19 +71,14 @@ state_fetch_pc (Dwfl_Frame *state) /* Do not call it on your own, to be used by thread_* functions only. */ static void -state_free (Dwfl_Frame *state) +free_states (Dwfl_Frame *state) { - Dwfl_Thread *thread = state->thread; - assert (thread->unwound == state); - thread->unwound = state->unwound; - free (state); -} - -static void -thread_free_all_states (Dwfl_Thread *thread) -{ - while (thread->unwound) - state_free (thread->unwound); + while (state) + { + Dwfl_Frame *next = state->unwound; + free(state); + state = next; + } } static Dwfl_Frame * @@ -279,24 +274,15 @@ dwfl_getthreads (Dwfl *dwfl, int (*callback) (Dwfl_Thread *thread, void *arg), process->callbacks_arg, &thread.callbacks_arg); if (thread.tid < 0) - { - Dwfl_Error saved_errno = dwfl_errno (); - thread_free_all_states (&thread); - __libdwfl_seterrno (saved_errno); - return -1; - } + return -1; if (thread.tid == 0) { - thread_free_all_states (&thread); __libdwfl_seterrno (DWFL_E_NOERROR); return 0; } int err = callback (&thread, arg); if (err != DWARF_CB_OK) - { - thread_free_all_states (&thread); - return err; - } + return err; assert (thread.unwound == NULL); } /* NOTREACHED */ @@ -356,11 +342,8 @@ getthread (Dwfl *dwfl, pid_t tid, if (process->callbacks->get_thread (dwfl, tid, process->callbacks_arg, &thread.callbacks_arg)) { - int err; thread.tid = tid; - err = callback (&thread, arg); - thread_free_all_states (&thread); - return err; + return callback (&thread, arg); } return -1; @@ -411,12 +394,6 @@ dwfl_thread_getframes (Dwfl_Thread *thread, int (*callback) (Dwfl_Frame *state, void *arg), void *arg) { - if (thread->unwound != NULL) - { - /* We had to be called from inside CALLBACK. */ - __libdwfl_seterrno (DWFL_E_ATTACH_STATE_CONFLICT); - return -1; - } Ebl *ebl = thread->process->ebl; if (ebl_frame_nregs (ebl) == 0) { @@ -432,33 +409,34 @@ dwfl_thread_getframes (Dwfl_Thread *thread, if (! process->callbacks->set_initial_registers (thread, thread->callbacks_arg)) { - thread_free_all_states (thread); + free_states (thread->unwound); + thread->unwound = NULL; return -1; } - if (! state_fetch_pc (thread->unwound)) + Dwfl_Frame *state = thread->unwound; + thread->unwound = NULL; + if (! state_fetch_pc (state)) { if (process->callbacks->thread_detach) process->callbacks->thread_detach (thread, thread->callbacks_arg); - thread_free_all_states (thread); + free_states (state); return -1; } - - Dwfl_Frame *state; do { - state = thread->unwound; int err = callback (state, arg); if (err != DWARF_CB_OK) { if (process->callbacks->thread_detach) process->callbacks->thread_detach (thread, thread->callbacks_arg); - thread_free_all_states (thread); + free_states (state); return err; } __libdwfl_frame_unwind (state); + Dwfl_Frame *next = state->unwound; /* The old frame is no longer needed. */ - state_free (thread->unwound); - state = thread->unwound; + free (state); + state = next; } while (state && state->pc_state == DWFL_FRAME_STATE_PC_SET); @@ -467,12 +445,12 @@ dwfl_thread_getframes (Dwfl_Thread *thread, process->callbacks->thread_detach (thread, thread->callbacks_arg); if (state == NULL || state->pc_state == DWFL_FRAME_STATE_ERROR) { - thread_free_all_states (thread); + free_states (state); __libdwfl_seterrno (err); return -1; } assert (state->pc_state == DWFL_FRAME_STATE_PC_UNDEFINED); - thread_free_all_states (thread); + free_states (state); return 0; } INTDEF(dwfl_thread_getframes) diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c index 9267788d..40857645 100644 --- a/libdwfl/find-debuginfo.c +++ b/libdwfl/find-debuginfo.c @@ -1,5 +1,5 @@ /* Standard find_debuginfo callback for libdwfl. - Copyright (C) 2005-2010, 2014, 2015 Red Hat, Inc. + Copyright (C) 2005-2010, 2014, 2015, 2019 Red Hat, Inc. This file is part of elfutils. This file is free software; you can redistribute it and/or modify @@ -359,7 +359,8 @@ dwfl_standard_find_debuginfo (Dwfl_Module *mod, other than just by finding nothing, that's all we do. */ const unsigned char *bits; GElf_Addr vaddr; - if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0) + int bits_len; + if ((bits_len = INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr)) > 0) { /* Dropping most arguments means we cannot rely on them in dwfl_build_id_find_debuginfo. But leave it that way since @@ -397,6 +398,9 @@ dwfl_standard_find_debuginfo (Dwfl_Module *mod, free (canon); } + if (fd < 0 && bits_len > 0) + fd = __libdwfl_debuginfod_find_debuginfo (mod->dwfl, bits, bits_len); + return fd; } INTDEF (dwfl_standard_find_debuginfo) diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h index 941a8b66..f631f946 100644 --- a/libdwfl/libdwflP.h +++ b/libdwfl/libdwflP.h @@ -40,6 +40,7 @@ #include "../libdw/libdwP.h" /* We need its INTDECLs. */ #include "../libdwelf/libdwelfP.h" +#include "../debuginfod/debuginfod.h" typedef struct Dwfl_Process Dwfl_Process; @@ -114,6 +115,7 @@ struct Dwfl_User_Core struct Dwfl { const Dwfl_Callbacks *callbacks; + debuginfod_client *debuginfod; Dwfl_Module *modulelist; /* List in order used by full traversals. */ @@ -242,8 +244,7 @@ struct Dwfl_Thread { Dwfl_Process *process; pid_t tid; - /* The current frame being unwound. Initially it is the bottom frame. - Later the processed frames get freed and this pointer is updated. */ + /* Bottom (innermost) frame while we're initializing, NULL afterwards. */ Dwfl_Frame *unwound; void *callbacks_arg; }; @@ -635,6 +636,19 @@ extern Dwfl_Error __libdw_open_elf (int fd, Elf **elfp) internal_function; extern bool __libdwfl_dynamic_vaddr_get (Elf *elf, GElf_Addr *vaddrp) internal_function; +/* Internal interface to libdebuginfod (if installed). */ +int +__libdwfl_debuginfod_find_executable (Dwfl *dwfl, + const unsigned char *build_id_bits, + size_t build_id_len); +int +__libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl, + const unsigned char *build_id_bits, + size_t build_id_len); +void +__libdwfl_debuginfod_end (debuginfod_client *c); + + /* These are working nicely for --core, but are not ready to be exported interfaces quite yet. */ |