diff mbox

[v4] Add debuginfod support to GDB

Message ID CAJDtP-TqxS=e34uDo_Gu_kdC9dO_bWej-fX-NPkCkkxEvQ1_KQ@mail.gmail.com
State New
Headers show

Commit Message

Aaron Merey Feb. 24, 2020, 4:42 a.m. UTC
On Fri, Feb 21, 2020 at 3:12 PM Simon Marchi <simark@simark.ca> wrote:
> I'll have to build debuginfod and configure it, so I can give it a try
> and see how it files.
>
> In the mean time: if we fail to parse the server's response as a bfd or
> if the build id doesn't match, I think we need to display a warning,
> because it probably means that the server sent us something bad, or there
> was some problem somewhere in the pipeline.
>
> Currently, if that happens, the user would see "Download successful", but
> no debug info loaded, so it's a bit misleading.

I added the warnings but removed the "Download successful" anyway
since it resulted in too much noise.

Aaron

Comments

Simon Marchi Feb. 24, 2020, 8:39 p.m. UTC | #1
On 2020-02-23 11:42 p.m., Aaron Merey wrote:
> On Fri, Feb 21, 2020 at 3:12 PM Simon Marchi <simark@simark.ca> wrote:
>> I'll have to build debuginfod and configure it, so I can give it a try
>> and see how it files.
>>
>> In the mean time: if we fail to parse the server's response as a bfd or
>> if the build id doesn't match, I think we need to display a warning,
>> because it probably means that the server sent us something bad, or there
>> was some problem somewhere in the pipeline.
>>
>> Currently, if that happens, the user would see "Download successful", but
>> no debug info loaded, so it's a bit misleading.
> 
> I added the warnings but removed the "Download successful" anyway
> since it resulted in too much noise.
> 
> Aaron
> 

I just noticed that the debuginfod_debuginfo_query definition is not in sync with
the declaration, so when we build without debug info support, we get:

  CXX    debuginfod-support.o
/home/smarchi/src/binutils-gdb/gdb/debuginfod-support.c: In function ‘scoped_fd debuginfod_debuginfo_query(const unsigned char*, int, gdb::unique_xmalloc_ptr<char>*)’:
/home/smarchi/src/binutils-gdb/gdb/debuginfod-support.c:36:1: error: no previous declaration for ‘scoped_fd debuginfod_debuginfo_query(const unsigned char*, int, gdb::unique_xmalloc
_ptr<char>*)’ [-Werror=missing-declarations]
 debuginfod_debuginfo_query (const unsigned char *build_id __attribute__((unused)),
 ^~~~~~~~~~~~~~~~~~~~~~~~~~

I also think you could remove the __attribute__((unused)).  We don't use -Wunused-parameter
in GDB, and I don't think we plan to (there are a ton of places with unused parameters like
that, that don't hurt).

Simon
Aaron Merey Feb. 24, 2020, 8:45 p.m. UTC | #2
On Mon, Feb 24, 2020 at 3:39 PM Simon Marchi <simark@simark.ca> wrote:
> I just noticed that the debuginfod_debuginfo_query definition is not in sync with
> the declaration, so when we build without debug info support, we get:
>
>   CXX    debuginfod-support.o
> /home/smarchi/src/binutils-gdb/gdb/debuginfod-support.c: In function ‘scoped_fd debuginfod_debuginfo_query(const unsigned char*, int, gdb::unique_xmalloc_ptr<char>*)’:
> /home/smarchi/src/binutils-gdb/gdb/debuginfod-support.c:36:1: error: no previous declaration for ‘scoped_fd debuginfod_debuginfo_query(const unsigned char*, int, gdb::unique_xmalloc
> _ptr<char>*)’ [-Werror=missing-declarations]
>  debuginfod_debuginfo_query (const unsigned char *build_id __attribute__((unused)),
>  ^~~~~~~~~~~~~~~~~~~~~~~~~~
>
> I also think you could remove the __attribute__((unused)).  We don't use -Wunused-parameter
> in GDB, and I don't think we plan to (there are a ton of places with unused parameters like
> that, that don't hurt).

Ah ok I will change that on my end, I won't repost the patch over that unless
you'd like me to.

Aaron
Simon Marchi Feb. 24, 2020, 8:49 p.m. UTC | #3
On 2020-02-24 3:45 p.m., Aaron Merey wrote:
> On Mon, Feb 24, 2020 at 3:39 PM Simon Marchi <simark@simark.ca> wrote:
>> I just noticed that the debuginfod_debuginfo_query definition is not in sync with
>> the declaration, so when we build without debug info support, we get:
>>
>>   CXX    debuginfod-support.o
>> /home/smarchi/src/binutils-gdb/gdb/debuginfod-support.c: In function ‘scoped_fd debuginfod_debuginfo_query(const unsigned char*, int, gdb::unique_xmalloc_ptr<char>*)’:
>> /home/smarchi/src/binutils-gdb/gdb/debuginfod-support.c:36:1: error: no previous declaration for ‘scoped_fd debuginfod_debuginfo_query(const unsigned char*, int, gdb::unique_xmalloc
>> _ptr<char>*)’ [-Werror=missing-declarations]
>>  debuginfod_debuginfo_query (const unsigned char *build_id __attribute__((unused)),
>>  ^~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> I also think you could remove the __attribute__((unused)).  We don't use -Wunused-parameter
>> in GDB, and I don't think we plan to (there are a ton of places with unused parameters like
>> that, that don't hurt).
> 
> Ah ok I will change that on my end, I won't repost the patch over that unless
> you'd like me to.
> 
> Aaron

That's correct, keep the change locally for now.  I am trying to set up a debuginfod
server to try your patch at the moment.

Simon
diff mbox

Patch

From 819713b73c6d3eb5b55bfbfb070dfb6203a2048c Mon Sep 17 00:00:00 2001
From: Aaron Merey <amerey@redhat.com>
Date: Sun, 23 Feb 2020 23:29:02 -0500
Subject: [PATCH] Add debuginfod support to GDB

debuginfod is a lightweight web service that indexes ELF/DWARF debugging
resources by build-id and serves them over HTTP.

This patch enables GDB to query debuginfod servers for separate debug
files and source code when it is otherwise not able to find them.

GDB can be built with debuginfod using the --with-debuginfod configure
option.

This requires that libdebuginfod be installed and found at configure time.

debuginfod is packaged with elfutils, starting with version 0.178.

For more information see https://sourceware.org/elfutils/.

Tested on x86_64 Fedora 31.

gdb/ChangeLog:
2020-02-24  Aaron Merey  <amerey@redhat.com>

        * Makefile.in: Handle optional debuginfod support.
        * NEWS: Update.
        * README: Add --with-debuginfod summary.
        * config.in: Regenerate.
        * configure: Regenerate.
        * configure.ac: Handle optional debuginfod support.
        * debuginfod-support.c: debuginfod helper functions.
        * debuginfod-support.h: Ditto.
        * doc/gdb.texinfo: Add --with-debuginfod to configure options
        summary.
        * dwarf2/read.c (dwarf2_get_dwz_file): Query debuginfod servers
        when a dwz file cannot be found.
        * elfread.c (elf_symfile_read): Query debuginfod servers when a
        debuginfo file cannot be found.
        * source.c (open_source_file): Query debuginfod servers when a
        source file cannot be found.
        * top.c (print_gdb_configuration): Include
        --{with,without}-debuginfod in the output.

gdb/testsuite/ChangeLog:
2020-02-24  Aaron Merey  <amerey@redhat.com>

        * gdb.debuginfod: New directory for debuginfod tests.
        * gdb.debuginfod/main.c: New test file.
        * gdb.debuginfod/fetch_src_and_symbols.exp: New tests.
---
 gdb/Makefile.in                               |   7 +-
 gdb/NEWS                                      |  14 ++
 gdb/README                                    |   9 +
 gdb/config.in                                 |   3 +
 gdb/configure                                 |  95 +++++++-
 gdb/configure.ac                              |  34 ++-
 gdb/debuginfod-support.c                      | 129 +++++++++++
 gdb/debuginfod-support.h                      |  59 +++++
 gdb/doc/gdb.texinfo                           |   8 +
 gdb/dwarf2/read.c                             |  24 ++
 gdb/elfread.c                                 |  32 ++-
 gdb/source.c                                  |  30 +++
 .../gdb.debuginfod/fetch_src_and_symbols.exp  | 214 ++++++++++++++++++
 gdb/testsuite/gdb.debuginfod/main.c           |  25 ++
 gdb/top.c                                     |  10 +
 15 files changed, 687 insertions(+), 6 deletions(-)
 create mode 100644 gdb/debuginfod-support.c
 create mode 100644 gdb/debuginfod-support.h
 create mode 100644 gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
 create mode 100644 gdb/testsuite/gdb.debuginfod/main.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index f9606b8fc7..b63c0588b2 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -592,7 +592,8 @@  INTERNAL_CFLAGS_BASE = \
 	$(GDB_CFLAGS) $(OPCODES_CFLAGS) $(READLINE_CFLAGS) $(ZLIBINC) \
 	$(BFD_CFLAGS) $(INCLUDE_CFLAGS) $(LIBDECNUMBER_CFLAGS) \
 	$(INTL_CFLAGS) $(INCGNU) $(INCSUPPORT) $(ENABLE_CFLAGS) \
-	$(INTERNAL_CPPFLAGS) $(SRCHIGH_CFLAGS) $(TOP_CFLAGS) $(PTHREAD_CFLAGS)
+	$(INTERNAL_CPPFLAGS) $(SRCHIGH_CFLAGS) $(TOP_CFLAGS) $(PTHREAD_CFLAGS) \
+	@DEBUGINFOD_CFLAGS@
 INTERNAL_WARN_CFLAGS = $(INTERNAL_CFLAGS_BASE) $(GDB_WARN_CFLAGS)
 INTERNAL_CFLAGS = $(INTERNAL_WARN_CFLAGS) $(GDB_WERROR_CFLAGS)
 
@@ -617,7 +618,8 @@  CLIBS = $(SIM) $(READLINE) $(OPCODES) $(BFD) $(LIBCTF) $(ZLIB) \
 	@LIBS@ @GUILE_LIBS@ @PYTHON_LIBS@ \
 	$(LIBEXPAT) $(LIBLZMA) $(LIBBABELTRACE) $(LIBIPT) \
 	$(WIN32LIBS) $(LIBGNU) $(LIBICONV) \
-	$(LIBMPFR) $(SRCHIGH_LIBS) $(LIBXXHASH) $(PTHREAD_LIBS)
+	$(LIBMPFR) $(SRCHIGH_LIBS) $(LIBXXHASH) $(PTHREAD_LIBS) \
+	@DEBUGINFOD_LIBS@
 CDEPS = $(NAT_CDEPS) $(SIM) $(BFD) $(READLINE_DEPS) $(LIBCTF) \
 	$(OPCODES) $(INTL_DEPS) $(LIBIBERTY) $(CONFIG_DEPS) $(LIBGNU) \
 	$(LIBSUPPORT)
@@ -991,6 +993,7 @@  COMMON_SFILES = \
 	dbxread.c \
 	dcache.c \
 	debug.c \
+	debuginfod-support.c \
 	dictionary.c \
 	disasm.c \
 	disasm-selftests.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index e33d838dd1..d3a3605a58 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,20 @@ 
 
 *** Changes since GDB 9
 
+* GDB now supports debuginfod, an HTTP server for distributing ELF/DWARF
+  debugging information as well as source code.
+
+  When built with debuginfod, GDB can automatically query debuginfod
+  servers for the separate debug files and source code of the executable
+  being debugged.
+
+  To build GDB with debuginfod, pass --with-debuginfod to configure (this
+  requires libdebuginfod, the debuginfod client library).
+
+  debuginfod is distributed with elfutils, starting with version 0.178.
+
+  You can get the latest version from https://sourceware.org/elfutils.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on RISC-V GNU/Linux.
diff --git a/gdb/README b/gdb/README
index 3895758ece..0ec1605ce5 100644
--- a/gdb/README
+++ b/gdb/README
@@ -432,6 +432,15 @@  more obscure GDB `configure' options are not listed here.
      Use the curses library instead of the termcap library, for
      text-mode terminal operations.
 
+`--with-debuginfod'
+     Build GDB with libdebuginfod, the debuginfod client library.  Used
+     to automatically fetch source files and separate debug files from
+     debuginfod servers using the associated executable's build ID.
+     Enabled by default if libdebuginfod is installed and found at
+     configure time.  debuginfod is packaged with elfutils, starting
+     with version 0.178.  You can get the latest version from
+     'https://sourceware.org/elfutils/'.
+
 `--with-libunwind-ia64'
      Use the libunwind library for unwinding function call stack on ia64
      target platforms.
diff --git a/gdb/config.in b/gdb/config.in
index 9c5c1ce834..7a34b85e57 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -227,6 +227,9 @@ 
 /* Define if you have the babeltrace library. */
 #undef HAVE_LIBBABELTRACE
 
+/* Define to 1 if debuginfod is enabled. */
+#undef HAVE_LIBDEBUGINFOD
+
 /* Define if you have the expat library. */
 #undef HAVE_LIBEXPAT
 
diff --git a/gdb/configure b/gdb/configure
index e6b5a510f6..c50acbf4cf 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -719,7 +719,6 @@  GUILE_LIBS
 GUILE_CPPFLAGS
 GUILD_TARGET_FLAG
 GUILD
-pkg_config_prog_path
 HAVE_PYTHON_FALSE
 HAVE_PYTHON_TRUE
 PYTHON_LIBS
@@ -758,6 +757,9 @@  REPORT_BUGS_TEXI
 REPORT_BUGS_TO
 PKGVERSION
 CODESIGN_CERT
+DEBUGINFOD_CFLAGS
+DEBUGINFOD_LIBS
+pkg_config_prog_path
 HAVE_NATIVE_GCORE_TARGET
 TARGET_OBS
 subdirs
@@ -869,6 +871,7 @@  enable_64_bit_bfd
 enable_gdbmi
 enable_tui
 enable_gdbtk
+with_debuginfod
 with_libunwind_ia64
 with_curses
 enable_profiling
@@ -1602,6 +1605,8 @@  Optional Packages:
                           [--with-auto-load-dir]
   --without-auto-load-safe-path
                           do not restrict auto-loaded files locations
+  --with-debuginfod       Enable debuginfo lookups with debuginfod
+                          (auto/yes/no)
   --with-libunwind-ia64   use libunwind frame unwinding for ia64 targets
   --with-curses           use the curses library instead of the termcap
                           library
@@ -6836,8 +6841,94 @@  $as_echo "$as_me: WARNING: gdbtk isn't supported on $host; disabling" >&2;}
     enable_gdbtk=no ;;
 esac
 
-# Libunwind support for ia64.
+# Handle optional debuginfod support
+
+# Check whether --with-debuginfod was given.
+if test "${with_debuginfod+set}" = set; then :
+  withval=$with_debuginfod;
+else
+  with_debuginfod=auto
+fi
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to use debuginfod" >&5
+$as_echo_n "checking whether to use debuginfod... " >&6; }
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_debuginfod" >&5
+$as_echo "$with_debuginfod" >&6; }
+
+if test "x$with_debuginfod" != xno; then
+  # Extract the first word of "pkg-config", so it can be a program name with args.
+set dummy pkg-config; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_path_pkg_config_prog_path+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $pkg_config_prog_path in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_pkg_config_prog_path="$pkg_config_prog_path" # Let the user override the test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_path_pkg_config_prog_path="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  test -z "$ac_cv_path_pkg_config_prog_path" && ac_cv_path_pkg_config_prog_path="missing"
+  ;;
+esac
+fi
+pkg_config_prog_path=$ac_cv_path_pkg_config_prog_path
+if test -n "$pkg_config_prog_path"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $pkg_config_prog_path" >&5
+$as_echo "$pkg_config_prog_path" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+  if test "${pkg_config_prog_path}" != "missing"; then
+    ${pkg_config_prog_path} --exists --atleast-version=0.178 libdebuginfod
+    if test $? = 0; then
+      DEBUGINFOD_LIBS=`${pkg_config_prog_path} --libs libdebuginfod`
+
+      DEBUGINFOD_CFLAGS=`${pkg_config_prog_path} --cflags libdebuginfod`
+
+
+$as_echo "#define HAVE_LIBDEBUGINFOD 1" >>confdefs.h
+
+    else
+      if test "x$with_debuginfod" = xyes; then
+	as_fn_error $? "\"--with-debuginfod was given, but libdebuginfod is missing or unusable.\"" "$LINENO" 5
+      else
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: libdebuginfod is missing or unusable; some features may be unavailable." >&5
+$as_echo "$as_me: WARNING: libdebuginfod is missing or unusable; some features may be unavailable." >&2;}
+      fi
+    fi
+  else
+    if test "x$with_debuginfod" = xyes; then
+      as_fn_error $? "--with-debuginfod was given, but pkg-config missing or unusable; cannot find libdebuginfod" "$LINENO" 5
+    else
+      { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: pkg-config missing or unusable; debuginfod support disabled." >&5
+$as_echo "$as_me: WARNING: pkg-config missing or unusable; debuginfod support disabled." >&2;}
+    fi
+  fi
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: debuginfod support disabled; some features may be unavailable." >&5
+$as_echo "$as_me: WARNING: debuginfod support disabled; some features may be unavailable." >&2;}
+fi
+
+# Libunwind support for ia64.
 
 # Check whether --with-libunwind-ia64 was given.
 if test "${with_libunwind_ia64+set}" = set; then :
diff --git a/gdb/configure.ac b/gdb/configure.ac
index a51c5eda6b..980bc3047a 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -322,8 +322,40 @@  case $host_os in
     enable_gdbtk=no ;;
 esac
 
-# Libunwind support for ia64.
+# Handle optional debuginfod support
+AC_ARG_WITH([debuginfod],
+  AC_HELP_STRING([--with-debuginfod], [Enable debuginfo lookups with debuginfod (auto/yes/no)]),
+  [], [with_debuginfod=auto])
+AC_MSG_CHECKING([whether to use debuginfod])
+AC_MSG_RESULT([$with_debuginfod])
+
+if test "x$with_debuginfod" != xno; then
+  AC_PATH_PROG(pkg_config_prog_path, pkg-config, missing)
+  if test "${pkg_config_prog_path}" != "missing"; then
+    ${pkg_config_prog_path} --exists --atleast-version=0.178 libdebuginfod
+    if test $? = 0; then
+      AC_SUBST([DEBUGINFOD_LIBS], [`${pkg_config_prog_path} --libs libdebuginfod`])
+      AC_SUBST([DEBUGINFOD_CFLAGS], [`${pkg_config_prog_path} --cflags libdebuginfod`])
+      AC_DEFINE([HAVE_LIBDEBUGINFOD], [1], [Define to 1 if debuginfod is enabled.])
+    else
+      if test "x$with_debuginfod" = xyes; then
+	AC_MSG_ERROR(["--with-debuginfod was given, but libdebuginfod is missing or unusable."])
+      else
+	AC_MSG_WARN([libdebuginfod is missing or unusable; some features may be unavailable.])
+      fi
+    fi
+  else
+    if test "x$with_debuginfod" = xyes; then
+      AC_MSG_ERROR([--with-debuginfod was given, but pkg-config missing or unusable; cannot find libdebuginfod])
+    else
+      AC_MSG_WARN([pkg-config missing or unusable; debuginfod support disabled.])
+    fi
+  fi
+else
+  AC_MSG_WARN([debuginfod support disabled; some features may be unavailable.])
+fi
 
+# Libunwind support for ia64.
 AC_ARG_WITH(libunwind-ia64,
 AS_HELP_STRING([--with-libunwind-ia64],
 	       [use libunwind frame unwinding for ia64 targets]),,
diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
new file mode 100644
index 0000000000..6e0ee43c4f
--- /dev/null
+++ b/gdb/debuginfod-support.c
@@ -0,0 +1,129 @@ 
+/* debuginfod utilities for GDB.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include "defs.h"
+#include "cli/cli-style.h"
+#include "gdbsupport/scoped_fd.h"
+#include "debuginfod-support.h"
+
+#ifndef HAVE_LIBDEBUGINFOD
+scoped_fd
+debuginfod_source_query (const unsigned char *build_id __attribute__((unused)),
+			 int build_id_len __attribute__((unused)),
+			 const char *srcpath __attribute__((unused)),
+			 gdb::unique_xmalloc_ptr<char> *filename __attribute__((unused)))
+{
+  return scoped_fd (-ENOSYS);
+}
+
+scoped_fd
+debuginfod_debuginfo_query (const unsigned char *build_id __attribute__((unused)),
+			    int build_id_len __attribute__((unused)),
+			    gdb::unique_xmalloc_ptr<char> *filename __attribute__((unused)))
+{
+  return scoped_fd (-ENOSYS);
+}
+#else
+#include <elfutils/debuginfod.h>
+
+static int
+progressfn (debuginfod_client *c, long cur, long total)
+{
+  if (check_quit_flag ())
+    {
+      printf_filtered ("Cancelling download...\n");
+      return 1;
+    }
+
+  return 0;
+}
+
+static debuginfod_client *
+debuginfod_init ()
+{
+  debuginfod_client *c = debuginfod_begin ();
+
+  if (c != nullptr)
+    debuginfod_set_progressfn (c, progressfn);
+
+  return c;
+}
+
+/* See debuginfod-support.h  */
+
+scoped_fd
+debuginfod_source_query (const unsigned char *build_id,
+			 int build_id_len,
+			 const char *srcpath,
+			 gdb::unique_xmalloc_ptr<char> *destname)
+{
+  debuginfod_client *c = debuginfod_init ();
+
+  if (c == nullptr)
+    return scoped_fd (-ENOMEM);
+
+  printf_filtered (_("Debuginfod fetching source file %ps...\n"),
+		   styled_string (file_name_style.style (), srcpath));
+
+  scoped_fd fd (debuginfod_find_source (c,
+					build_id,
+					build_id_len,
+					srcpath,
+					nullptr));
+
+  if (fd.get () < 0)
+    {
+      printf_filtered (_("Download failed. Continuing without source file %ps.\n"),
+		       styled_string (file_name_style.style (), srcpath));
+    }
+  else
+    destname->reset (xstrdup (srcpath));
+
+  debuginfod_end (c);
+  return fd;
+}
+
+/* See debuginfod-support.h  */
+
+scoped_fd
+debuginfod_debuginfo_query (const unsigned char *build_id,
+			    int build_id_len,
+			    const char *filename,
+			    gdb::unique_xmalloc_ptr<char> *destname)
+{
+  debuginfod_client *c = debuginfod_init ();
+
+  if (c == nullptr)
+    return scoped_fd (-ENOMEM);
+
+  printf_filtered (_("Debuginfod fetching debug info for %ps...\n"),
+		   styled_string (file_name_style.style (), filename));
+
+  char *dname = nullptr;
+  scoped_fd fd (debuginfod_find_debuginfo (c, build_id, build_id_len, &dname));
+
+  if (fd.get () < 0)
+    printf_filtered (_("Download failed. Continuing without debug info for %ps.\n"),
+		     styled_string (file_name_style.style (),  filename));
+
+  destname->reset (dname);
+  debuginfod_end (c);
+  return fd;
+}
+#endif
diff --git a/gdb/debuginfod-support.h b/gdb/debuginfod-support.h
new file mode 100644
index 0000000000..94d4eba99b
--- /dev/null
+++ b/gdb/debuginfod-support.h
@@ -0,0 +1,59 @@ 
+/* debuginfod utilities for GDB.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef DEBUGINFOD_SUPPORT_H
+#define DEBUGINFOD_SUPPORT_H
+
+/* Query debuginfod servers for a source file associated with an
+   executable with BUILD_ID. BUILD_ID can be given as a binary blob or
+   a null-terminated string. If given as a binary blob, BUILD_ID_LEN
+   should be the number of bytes. If given as a null-terminated string,
+   BUILD_ID_LEN should be 0.
+
+   SRC_PATH should be the source file's absolute path that includes the
+   compilation directory of the CU associated with the source file.
+   For example if a CU's compilation directory is `/my/build` and the
+   source file path is `/my/source/foo.c`, then SRC_PATH should be
+   `/my/build/../source/foo.c`.
+
+   If the file is successfully retrieved, its path on the local machine
+   is stored in FILENAME. If GDB is not built with debuginfod, this
+   function returns -ENOSYS.  */
+
+extern scoped_fd
+debuginfod_source_query (const unsigned char *build_id,
+			 int build_id_len,
+			 const char *src_path,
+			 gdb::unique_xmalloc_ptr<char> *destname);
+
+/* Query debuginfod servers for a debuginfo file with BUILD_ID.
+   BUILD_ID can be given as a binary blob or a null-terminated string.
+   If given as a binary blob, BUILD_ID_LEN should be the number of bytes.
+   If given as a null-terminated string, BUILD_ID_LEN should be 0.
+
+   If the file is successfully retrieved, its path on the local machine
+   is stored in FILENAME. If GDB is not built with debuginfod, this
+   function returns -ENOSYS.  */
+
+extern scoped_fd
+debuginfod_debuginfo_query (const unsigned char *build_id,
+			    int build_id_len,
+			    const char *filename,
+			    gdb::unique_xmalloc_ptr<char> *destname);
+
+#endif /* DEBUGINFOD_SUPPORT_H */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 0645fb0594..30783b0e8c 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -37848,6 +37848,14 @@  supported).
 Use the curses library instead of the termcap library, for text-mode
 terminal operations.
 
+@item --with-debuginfod
+Build @value{GDBN} with libdebuginfod, the debuginfod client library.
+Used to automatically fetch source files and separate debug files from
+debuginfod servers using the associated executable's build ID. Enabled
+by default if libdebuginfod is installed and found at configure time.
+debuginfod is packaged with elfutils, starting with version 0.178. You
+can get the latest version from `https://sourceware.org/elfutils/'.
+
 @item --with-libunwind-ia64
 Use the libunwind library for unwinding function call stack on ia64
 target platforms.  See http://www.nongnu.org/libunwind/index.html for
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 46d510eb27..1ac23ff1b3 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -83,6 +83,7 @@ 
 #include "rust-lang.h"
 #include "gdbsupport/pathstuff.h"
 #include "count-one-bits.h"
+#include "debuginfod-support.h"
 
 /* When == 1, print basic high level tracing messages.
    When > 1, be more verbose.
@@ -2153,6 +2154,29 @@  dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
   if (dwz_bfd == NULL)
     dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
 
+  if (dwz_bfd == nullptr)
+    {
+      gdb::unique_xmalloc_ptr<char> alt_filename;
+      const char *origname = dwarf2_per_objfile->objfile->original_name;
+
+      scoped_fd fd (debuginfod_debuginfo_query (buildid,
+						buildid_len,
+						origname,
+						&alt_filename));
+
+      if (fd.get () >= 0)
+	{
+	  /* File successfully retrieved from server.  */
+	  dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
+
+	  if (dwz_bfd == nullptr)
+	    warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
+		     alt_filename.get ());
+	  else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
+	    dwz_bfd.reset (nullptr);
+	}
+    }
+
   if (dwz_bfd == NULL)
     error (_("could not find '.gnu_debugaltlink' file for %s"),
 	   objfile_name (dwarf2_per_objfile->objfile));
diff --git a/gdb/elfread.c b/gdb/elfread.c
index 453bca527e..70001cbe08 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -49,6 +49,8 @@ 
 #include "mdebugread.h"
 #include "ctfread.h"
 #include "gdbsupport/gdb_string_view.h"
+#include "gdbsupport/scoped_fd.h"
+#include "debuginfod-support.h"
 
 /* Forward declarations.  */
 extern const struct sym_fns elf_sym_fns_gdb_index;
@@ -1316,8 +1318,36 @@  elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 	  symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
 				    symfile_flags, objfile);
 	}
-	else
+      else
+	{
 	  has_dwarf2 = false;
+	  const struct bfd_build_id *build_id = build_id_bfd_get (objfile->obfd);
+
+	  if (build_id != nullptr)
+	    {
+	      gdb::unique_xmalloc_ptr<char> symfile_path;
+	      scoped_fd fd (debuginfod_debuginfo_query (build_id->data,
+							build_id->size,
+							objfile->original_name,
+							&symfile_path));
+
+	      if (fd.get () >= 0)
+		{
+		  /* File successfully retrieved from server.  */
+		  gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (symfile_path.get ()));
+
+		  if (debug_bfd == nullptr)
+		    warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
+			     symfile_path.get ());
+		  else if (build_id_verify (debug_bfd.get (), build_id->size, build_id->data))
+		    {
+		      symbol_file_add_separate (debug_bfd.get (), symfile_path.get (),
+						symfile_flags, objfile);
+		      has_dwarf2 = true;
+		    }
+		}
+	    }
+	}
     }
 
   /* Read the CTF section only if there is no DWARF info.  */
diff --git a/gdb/source.c b/gdb/source.c
index 4f889e4b95..d091c81a1a 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -48,6 +48,8 @@ 
 #include "source-cache.h"
 #include "cli/cli-style.h"
 #include "observable.h"
+#include "build-id.h"
+#include "debuginfod-support.h"
 
 #define OPEN_MODE (O_RDONLY | O_BINARY)
 #define FDOPEN_MODE FOPEN_RB
@@ -1148,6 +1150,34 @@  open_source_file (struct symtab *s)
   s->fullname = NULL;
   scoped_fd fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s),
 				       &fullname);
+
+  if (fd.get () < 0)
+    {
+      if (SYMTAB_COMPUNIT (s) != nullptr)
+	{
+	  std::string srcpath;
+
+	  if (IS_ABSOLUTE_PATH (s->filename))
+	    srcpath = s->filename;
+	  else
+	    {
+	      srcpath = SYMTAB_DIRNAME (s);
+	      srcpath += SLASH_STRING;
+	      srcpath += s->filename;
+	    }
+
+	  const objfile *ofp = COMPUNIT_OBJFILE (SYMTAB_COMPUNIT (s));
+	  const struct bfd_build_id *build_id = build_id_bfd_get (ofp->obfd);
+
+	  /* Query debuginfod for the source file.  */
+	  if (build_id != nullptr)
+	    fd = debuginfod_source_query (build_id->data,
+					  build_id->size,
+					  srcpath.c_str (),
+					  &fullname);
+	}
+    }
+
   s->fullname = fullname.release ();
   return fd;
 }
diff --git a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
new file mode 100644
index 0000000000..ec6cd4cf4e
--- /dev/null
+++ b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
@@ -0,0 +1,214 @@ 
+# Copyright 2020 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test debuginfod functionality
+
+standard_testfile main.c
+
+load_lib dwarf.exp
+
+if { [which debuginfod] == 0 } {
+    untested "cannot find debuginfod"
+    return -1
+}
+
+if { [which curl] == 0 } {
+    untested "cannot find curl"
+    return -1
+}
+
+# Skip testing if gdb was not configured with debuginfod
+if { [string first "with-debuginfod" [exec $GDB --configuration]] == -1 } {
+    untested "gdb not configured with debuginfod"
+    return -1
+}
+
+set cache [standard_output_file ".client_cache"]
+set db [standard_output_file ".debuginfod.db"]
+
+# Delete any preexisting test files
+file delete -force $cache
+file delete -force $db
+
+set sourcetmp [standard_output_file tmp-${srcfile}]
+set outputdir [standard_output_file {}]
+
+# Make a copy source file that we can move around
+if { [catch {file copy -force ${srcdir}/${subdir}/${srcfile} \
+	     [standard_output_file ${sourcetmp}]}] != 0 } {
+    error "create temporary file"
+    return -1
+}
+
+if { [gdb_compile "$sourcetmp" "$binfile" executable {debug}] != "" } {
+    fail "compile"
+    return -1
+}
+
+setenv DEBUGINFOD_URLS ""
+setenv DEBUGINFOD_TIMEOUT 30
+setenv DEBUGINFOD_CACHE_PATH $cache
+
+# Test that gdb cannot find source without debuginfod
+clean_restart $binfile
+gdb_test_no_output "set substitute-path $outputdir /dev/null"
+gdb_test "list" ".*No such file or directory.*"
+
+# Strip symbols into separate file and move it so gdb cannot find it without debuginfod
+if { [gdb_gnu_strip_debug $binfile ""] != 0 } {
+    fail "strip debuginfo"
+    return -1
+}
+
+set debugdir [standard_output_file "debug"]
+set debuginfo [standard_output_file "fetch_src_and_symbols.debug"]
+
+file mkdir $debugdir
+file rename -force $debuginfo $debugdir
+
+# Test that gdb cannot find symbols without debuginfod
+clean_restart $binfile
+gdb_test "file" ".*No symbol file.*"
+
+# Write some assembly that just has a .gnu_debugaltlink section.
+# Copied from testsuite/gdb.dwarf2/dwzbuildid.exp.
+proc write_just_debugaltlink {filename dwzname buildid} {
+    set asm_file [standard_output_file $filename]
+
+    Dwarf::assemble $asm_file {
+	upvar dwzname dwzname
+	upvar buildid buildid
+
+	gnu_debugaltlink $dwzname $buildid
+
+	# Only the DWARF reader checks .gnu_debugaltlink, so make sure
+	# there is a bit of DWARF in here.
+	cu {} {
+	    compile_unit {{language @DW_LANG_C}} {
+	    }
+	}
+    }
+}
+
+# Write some DWARF that also sets the buildid.
+# Copied from testsuite/gdb.dwarf2/dwzbuildid.exp.
+proc write_dwarf_file {filename buildid {value 99}} {
+    set asm_file [standard_output_file $filename]
+
+    Dwarf::assemble $asm_file {
+	declare_labels int_label int_label2
+
+	upvar buildid buildid
+	upvar value value
+
+	build_id $buildid
+
+	cu {} {
+	    compile_unit {{language @DW_LANG_C}} {
+	        int_label2: base_type {
+		    {name int}
+		    {byte_size 4 sdata}
+		    {encoding @DW_ATE_signed}
+		}
+
+		constant {
+		    {name the_int}
+		    {type :$int_label2}
+		    {const_value $value data1}
+		}
+	    }
+	}
+    }
+}
+
+set buildid "01234567890abcdef0123456"
+
+write_just_debugaltlink ${binfile}_has_altlink.S ${binfile}_dwz.o $buildid
+write_dwarf_file ${binfile}_dwz.S $buildid
+
+if {[gdb_compile ${binfile}_has_altlink.S ${binfile}_alt.o object nodebug] != ""} {
+    fail "compile main with altlink"
+    return -1
+}
+
+if {[gdb_compile ${binfile}_dwz.S ${binfile}_dwz.o object nodebug] != ""} {
+    fail "compile altlink"
+    return -1
+}
+
+file rename -force ${binfile}_dwz.o $debugdir
+
+# Test that gdb cannot find dwz without debuginfod.
+clean_restart
+gdb_test "file ${binfile}_alt.o" ".*could not find '.gnu_debugaltlink'.*"
+
+# Find an unused port
+set port 7999
+set found 0
+while { ! $found } {
+  incr port
+  if { $port == 65536 } {
+    fail "no available ports"
+    return -1
+  }
+
+  spawn debuginfod -vvvv -d $db -p $port -F $debugdir
+  expect {
+    "started http server on IPv4 IPv6 port=$port" { set found 1 }
+    "Failed to bind to port" { kill_wait_spawned_process $spawn_id }
+    timeout {
+      fail "find port timeout"
+      return -1
+    }
+  }
+}
+
+set metrics [list "ready 1" \
+	     "thread_work_total{role=\"traverse\"} 1" \
+	     "thread_work_pending{role=\"scan\"} 0" \
+	     "thread_busy{role=\"scan\"} 0"]
+
+# Check server metrics to confirm init has completed.
+foreach m $metrics {
+  set timelim 20
+  while { $timelim != 0 } {
+    sleep 0.5
+    catch {exec curl -s http://127.0.0.1:$port/metrics} got
+
+    if { [regexp $m $got] } {
+      break
+    }
+
+    incr timelim -1
+  }
+
+  if { $timelim == 0 } {
+    fail "server init timeout"
+    return -1
+  }
+}
+
+# Point the client to the server
+setenv DEBUGINFOD_URLS http://127.0.0.1:$port
+
+# gdb should now find the symbol and source files
+clean_restart $binfile
+gdb_test_no_output "set substitute-path $outputdir /dev/null"
+gdb_test "br main" "Breakpoint 1 at.*file.*"
+gdb_test "l" ".*This program is distributed in the hope.*"
+
+# gdb should now find the debugaltlink file
+clean_restart
+gdb_test "file ${binfile}_alt.o" ".*Reading symbols from ${binfile}_alt.o\.\.\.*"
diff --git a/gdb/testsuite/gdb.debuginfod/main.c b/gdb/testsuite/gdb.debuginfod/main.c
new file mode 100644
index 0000000000..73abaf58b1
--- /dev/null
+++ b/gdb/testsuite/gdb.debuginfod/main.c
@@ -0,0 +1,25 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Dummy main function.  */
+
+int
+main()
+{
+  asm ("main_label: .globl main_label");
+  return 0;
+}
diff --git a/gdb/top.c b/gdb/top.c
index f702af9acd..1a98ae198c 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1528,6 +1528,16 @@  This GDB was configured as follows:\n\
 "));
 #endif
 
+#if HAVE_LIBDEBUGINFOD
+  fprintf_filtered (stream, _("\
+             --with-debuginfod\n\
+"));
+#else
+   fprintf_filtered (stream, _("\
+             --without-debuginfod\n\
+"));
+#endif
+
 #if HAVE_GUILE
   fprintf_filtered (stream, _("\
              --with-guile\n\
-- 
2.24.1