[4/4,v5] gdb/debuginfod: Add .debug_line downloading

Message ID 20231028002008.1105723-5-amerey@redhat.com
State New
Headers
Series On-demand debuginfo downloading |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Aaron Merey Oct. 28, 2023, 12:20 a.m. UTC
  v4: https://sourceware.org/pipermail/gdb-patches/2023-August/201651.html

v5 adds prefix_state to progress_update objects to track when
a newline prefix needs to be added to a download progress message.
This is used to correctly format progress messages that occur during
command autocompletion.

Commit message:

ELF/DWARF section downloading allows gdb to download .gdb_index files in
order to defer full debuginfo downloads.  However .gdb_index does not
contain any information regarding source filenames.  When a gdb command
includes a filename argument (ex. 'break main.c:50'), this results in
the mass downloading of all deferred debuginfo so that gdb can search the
debuginfo for matching source filenames.  This can result in unnecessary
downloads.

To improve this, have gdb instead download each debuginfo's .debug_line
(and .debug_line_str if using DWARF5) when executing these commands.
Download full debuginfo only when its .debug_line contains a matching
filename.

Since the combined size of .debug_line and .debug_line_str is only about
1% the size of the corresponding debuginfo, significant time can be saved
by checking these sections before choosing to download an entire debuginfo.

This patch also redirects stdout and stderr of the debuginfod server
used by testsuite/gdb.debuginfod tests to a server_log standard output
file.  While adding tests for this patch I ran into an issue where the
test server would block when logging to stderr, presumably because the
stderr buffer filled up and wasn't being read from.  Redirecting the
log to a file fixes this and also makes the server log more accessible
when debugging test failures.
---
 gdb/cli-out.c                            |  11 +-
 gdb/completer.c                          |  18 +-
 gdb/dwarf2/line-header.c                 | 215 +++++++++++++++--------
 gdb/dwarf2/line-header.h                 |  10 ++
 gdb/dwarf2/read-gdb-index.c              |  60 +++++++
 gdb/dwarf2/read.c                        | 208 ++++++++++++++++++++++
 gdb/dwarf2/read.h                        |  37 ++++
 gdb/mi/mi-out.c                          |   9 +-
 gdb/testsuite/gdb.debuginfod/section.exp |  21 +++
 gdb/ui-out.c                             |   3 +
 gdb/ui-out.h                             |  20 +++
 11 files changed, 531 insertions(+), 81 deletions(-)
  

Comments

Aaron Merey Nov. 12, 2023, 8:21 p.m. UTC | #1
Ping

Thanks,
Aaron

On Fri, Oct 27, 2023 at 8:20 PM Aaron Merey <amerey@redhat.com> wrote:
>
> v4: https://sourceware.org/pipermail/gdb-patches/2023-August/201651.html
>
> v5 adds prefix_state to progress_update objects to track when
> a newline prefix needs to be added to a download progress message.
> This is used to correctly format progress messages that occur during
> command autocompletion.
>
> Commit message:
>
> ELF/DWARF section downloading allows gdb to download .gdb_index files in
> order to defer full debuginfo downloads.  However .gdb_index does not
> contain any information regarding source filenames.  When a gdb command
> includes a filename argument (ex. 'break main.c:50'), this results in
> the mass downloading of all deferred debuginfo so that gdb can search the
> debuginfo for matching source filenames.  This can result in unnecessary
> downloads.
>
> To improve this, have gdb instead download each debuginfo's .debug_line
> (and .debug_line_str if using DWARF5) when executing these commands.
> Download full debuginfo only when its .debug_line contains a matching
> filename.
>
> Since the combined size of .debug_line and .debug_line_str is only about
> 1% the size of the corresponding debuginfo, significant time can be saved
> by checking these sections before choosing to download an entire debuginfo.
>
> This patch also redirects stdout and stderr of the debuginfod server
> used by testsuite/gdb.debuginfod tests to a server_log standard output
> file.  While adding tests for this patch I ran into an issue where the
> test server would block when logging to stderr, presumably because the
> stderr buffer filled up and wasn't being read from.  Redirecting the
> log to a file fixes this and also makes the server log more accessible
> when debugging test failures.
> ---
>  gdb/cli-out.c                            |  11 +-
>  gdb/completer.c                          |  18 +-
>  gdb/dwarf2/line-header.c                 | 215 +++++++++++++++--------
>  gdb/dwarf2/line-header.h                 |  10 ++
>  gdb/dwarf2/read-gdb-index.c              |  60 +++++++
>  gdb/dwarf2/read.c                        | 208 ++++++++++++++++++++++
>  gdb/dwarf2/read.h                        |  37 ++++
>  gdb/mi/mi-out.c                          |   9 +-
>  gdb/testsuite/gdb.debuginfod/section.exp |  21 +++
>  gdb/ui-out.c                             |   3 +
>  gdb/ui-out.h                             |  20 +++
>  11 files changed, 531 insertions(+), 81 deletions(-)
>
> diff --git a/gdb/cli-out.c b/gdb/cli-out.c
> index c919622d418..a570a2d939d 100644
> --- a/gdb/cli-out.c
> +++ b/gdb/cli-out.c
> @@ -307,16 +307,23 @@ cli_ui_out::do_progress_notify (const std::string &msg,
>
>    if (info.state == progress_update::START)
>      {
> +      std::string prefix;
> +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> +       {
> +         prefix = "\n";
> +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> +       }
> +
>        if (stream->isatty ()
>           && current_ui->input_interactive_p ()
>           && chars_per_line >= MIN_CHARS_PER_LINE)
>         {
> -         gdb_printf (stream, "%s\n", msg.c_str ());
> +         gdb_printf (stream, "%s\n", (prefix + msg).c_str ());
>           info.state = progress_update::BAR;
>         }
>        else
>         {
> -         gdb_printf (stream, "%s...\n", msg.c_str ());
> +         gdb_printf (stream, "%s...\n", (prefix + msg).c_str ());
>           info.state = progress_update::WORKING;
>         }
>      }
> diff --git a/gdb/completer.c b/gdb/completer.c
> index 2abf3998345..9c299f3eb65 100644
> --- a/gdb/completer.c
> +++ b/gdb/completer.c
> @@ -1345,6 +1345,10 @@ complete_line_internal_1 (completion_tracker &tracker,
>      {
>        /* We've recognized a full command.  */
>
> +      /* Disable pagination since responding to the pagination prompt
> +        overwrites rl_line_buffer.  */
> +      scoped_restore pag_restore = make_scoped_restore (&pagination_enabled, false);
> +
>        if (p == tmp_command + point)
>         {
>           /* There is no non-whitespace in the line beyond the
> @@ -1444,7 +1448,8 @@ complete_line_internal_1 (completion_tracker &tracker,
>  }
>
>  /* Wrapper around complete_line_internal_1 to handle
> -   MAX_COMPLETIONS_REACHED_ERROR.  */
> +   MAX_COMPLETIONS_REACHED_ERROR and possible progress update
> +   interactions.  */
>
>  static void
>  complete_line_internal (completion_tracker &tracker,
> @@ -1452,6 +1457,11 @@ complete_line_internal (completion_tracker &tracker,
>                         const char *line_buffer, int point,
>                         complete_line_internal_reason reason)
>  {
> +  scoped_restore restore_prefix_state
> +    = make_scoped_restore
> +      (&cur_prefix_state,
> +       ui_out::progress_update::prefix_state::NEWLINE_NEEDED);
> +
>    try
>      {
>        complete_line_internal_1 (tracker, text, line_buffer, point, reason);
> @@ -1461,6 +1471,12 @@ complete_line_internal (completion_tracker &tracker,
>        if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
>         throw;
>      }
> +
> +  /* If progress update messages printed, then the text being completed
> +     needs to be printed again.  */
> +  if (cur_prefix_state
> +      == ui_out::progress_update::prefix_state::NEWLINE_PRINTED)
> +    rl_forced_update_display ();
>  }
>
>  /* See completer.h.  */
> diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
> index d072a91bac9..b9210d84f6b 100644
> --- a/gdb/dwarf2/line-header.c
> +++ b/gdb/dwarf2/line-header.c
> @@ -102,50 +102,57 @@ read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
>  {
>    LONGEST length = read_initial_length (abfd, buf, bytes_read);
>
> -  gdb_assert (cu_header->initial_length_size == 4
> -             || cu_header->initial_length_size == 8
> -             || cu_header->initial_length_size == 12);
> +  if (cu_header != nullptr)
> +    {
> +      gdb_assert (cu_header->initial_length_size == 4
> +                 || cu_header->initial_length_size == 8
> +                 || cu_header->initial_length_size == 12);
>
> -  if (cu_header->initial_length_size != *bytes_read)
> -    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> +      if (cu_header->initial_length_size != *bytes_read)
> +       complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> +    }
>
>    *offset_size = (*bytes_read == 4) ? 4 : 8;
>    return length;
>  }
>
> -/* Read directory or file name entry format, starting with byte of
> -   format count entries, ULEB128 pairs of entry formats, ULEB128 of
> -   entries count and the entries themselves in the described entry
> -   format.  */
> +
> +/* Like read_formatted_entries but the .debug_line and .debug_line_str
> +   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
> +   where these sections are read from separate files without necessarily
> +   having access to the entire debuginfo file they originate from.  */
>
>  static void
> -read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> -                       const gdb_byte **bufp, struct line_header *lh,
> -                       unsigned int offset_size,
> -                       void (*callback) (struct line_header *lh,
> -                                         const char *name,
> -                                         dir_index d_index,
> -                                         unsigned int mod_time,
> -                                         unsigned int length))
> +read_formatted_entries
> +  (bfd *parent_bfd, const gdb_byte **line_bufp,
> +   const gdb::array_view<const gdb_byte> line_str_data,
> +   struct line_header *lh,
> +   unsigned int offset_size,
> +   void (*callback) (struct line_header *lh,
> +                    const char *name,
> +                    dir_index d_index,
> +                    unsigned int mod_time,
> +                    unsigned int length))
>  {
>    gdb_byte format_count, formati;
>    ULONGEST data_count, datai;
> -  const gdb_byte *buf = *bufp;
> +  const gdb_byte *buf = *line_bufp;
> +  const gdb_byte *str_buf = line_str_data.data ();
>    const gdb_byte *format_header_data;
>    unsigned int bytes_read;
>
> -  format_count = read_1_byte (abfd, buf);
> +  format_count = read_1_byte (parent_bfd, buf);
>    buf += 1;
>    format_header_data = buf;
>    for (formati = 0; formati < format_count; formati++)
>      {
> -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
>        buf += bytes_read;
> -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
>        buf += bytes_read;
>      }
>
> -  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
> +  data_count = read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
>    buf += bytes_read;
>    for (datai = 0; datai < data_count; datai++)
>      {
> @@ -154,10 +161,10 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
>
>        for (formati = 0; formati < format_count; formati++)
>         {
> -         ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
> +         ULONGEST content_type = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
>           format += bytes_read;
>
> -         ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
> +         ULONGEST form  = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
>           format += bytes_read;
>
>           gdb::optional<const char *> string;
> @@ -166,36 +173,48 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
>           switch (form)
>             {
>             case DW_FORM_string:
> -             string.emplace (read_direct_string (abfd, buf, &bytes_read));
> +             string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
>               buf += bytes_read;
>               break;
>
>             case DW_FORM_line_strp:
>               {
> -               const char *str
> -                 = per_objfile->read_line_string (buf, offset_size);
> +               if (line_str_data.empty ())
> +                 error (_("Dwarf Error: DW_FORM_line_strp used without " \
> +                          "required section"));
> +               if (line_str_data.size () <= offset_size)
> +                 error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
> +                          "of section .debug_line"));
> +
> +               ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
> +
> +               const char *str;
> +               if (str_buf[str_offset] == '\0')
> +                 str = nullptr;
> +               else
> +                 str = (const char *) (str_buf + str_offset);
>                 string.emplace (str);
>                 buf += offset_size;
> +               break;
>               }
> -             break;
>
>             case DW_FORM_data1:
> -             uint.emplace (read_1_byte (abfd, buf));
> +             uint.emplace (read_1_byte (parent_bfd, buf));
>               buf += 1;
>               break;
>
>             case DW_FORM_data2:
> -             uint.emplace (read_2_bytes (abfd, buf));
> +             uint.emplace (read_2_bytes (parent_bfd, buf));
>               buf += 2;
>               break;
>
>             case DW_FORM_data4:
> -             uint.emplace (read_4_bytes (abfd, buf));
> +             uint.emplace (read_4_bytes (parent_bfd, buf));
>               buf += 4;
>               break;
>
>             case DW_FORM_data8:
> -             uint.emplace (read_8_bytes (abfd, buf));
> +             uint.emplace (read_8_bytes (parent_bfd, buf));
>               buf += 8;
>               break;
>
> @@ -205,7 +224,7 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
>               break;
>
>             case DW_FORM_udata:
> -             uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
> +             uint.emplace (read_unsigned_leb128 (parent_bfd, buf, &bytes_read));
>               buf += bytes_read;
>               break;
>
> @@ -248,28 +267,30 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
>        callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
>      }
>
> -  *bufp = buf;
> +  *line_bufp = buf;
>  }
>
>  /* See line-header.h.  */
>
>  line_header_up
> -dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> -                          dwarf2_per_objfile *per_objfile,
> -                          struct dwarf2_section_info *section,
> -                          const struct comp_unit_head *cu_header,
> -                          const char *comp_dir)
> +dwarf_decode_line_header (bfd *parent_bfd,
> +                         gdb::array_view<const gdb_byte> line_data,
> +                         gdb::array_view<const gdb_byte> line_str_data,
> +                         const gdb_byte **debug_line_ptr,
> +                         bool is_dwz,
> +                         const struct comp_unit_head *cu_header,
> +                         const char *comp_dir)
>  {
> -  const gdb_byte *line_ptr;
> +  const gdb_byte *line_ptr, *buf;
>    unsigned int bytes_read, offset_size;
>    int i;
>    const char *cur_dir, *cur_file;
>
> -  bfd *abfd = section->get_bfd_owner ();
> +  buf = *debug_line_ptr;
>
>    /* Make sure that at least there's room for the total_length field.
>       That could be 12 bytes long, but we're just going to fudge that.  */
> -  if (to_underlying (sect_off) + 4 >= section->size)
> +  if (buf + 4 >= line_data.data () + line_data.size ())
>      {
>        dwarf2_statement_list_fits_in_line_number_section_complaint ();
>        return 0;
> @@ -277,62 +298,65 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
>
>    line_header_up lh (new line_header (comp_dir));
>
> -  lh->sect_off = sect_off;
> +  lh->sect_off = (sect_offset) (buf - line_data.data ());
>    lh->offset_in_dwz = is_dwz;
>
> -  line_ptr = section->buffer + to_underlying (sect_off);
> +  line_ptr = buf;
>
>    /* Read in the header.  */
>    LONGEST unit_length
> -    = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
> +    = read_checked_initial_length_and_offset (parent_bfd, buf, cu_header,
>                                               &bytes_read, &offset_size);
> -  line_ptr += bytes_read;
>
> -  const gdb_byte *start_here = line_ptr;
> +  line_ptr += bytes_read;
>
> -  if (line_ptr + unit_length > (section->buffer + section->size))
> +  if (line_ptr + unit_length > buf + line_data.size ())
>      {
>        dwarf2_statement_list_fits_in_line_number_section_complaint ();
>        return 0;
>      }
> +
> +  const gdb_byte *start_here = line_ptr;
> +
>    lh->statement_program_end = start_here + unit_length;
> -  lh->version = read_2_bytes (abfd, line_ptr);
> +  lh->version = read_2_bytes (parent_bfd, line_ptr);
>    line_ptr += 2;
>    if (lh->version > 5)
>      {
>        /* This is a version we don't understand.  The format could have
>          changed in ways we don't handle properly so just punt.  */
>        complaint (_("unsupported version in .debug_line section"));
> -      return NULL;
> +      return nullptr;
>      }
>    if (lh->version >= 5)
>      {
>        gdb_byte segment_selector_size;
>
>        /* Skip address size.  */
> -      read_1_byte (abfd, line_ptr);
> +      read_1_byte (parent_bfd, line_ptr);
>        line_ptr += 1;
>
> -      segment_selector_size = read_1_byte (abfd, line_ptr);
> +      segment_selector_size = read_1_byte (parent_bfd, line_ptr);
>        line_ptr += 1;
>        if (segment_selector_size != 0)
>         {
>           complaint (_("unsupported segment selector size %u "
>                        "in .debug_line section"),
>                      segment_selector_size);
> -         return NULL;
> +         return nullptr;
>         }
>      }
>
> -  LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
> +  LONGEST header_length = read_offset (parent_bfd, line_ptr, offset_size);
>    line_ptr += offset_size;
>    lh->statement_program_start = line_ptr + header_length;
> -  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
> +
> +  lh->minimum_instruction_length = read_1_byte (parent_bfd, line_ptr);
>    line_ptr += 1;
>
>    if (lh->version >= 4)
>      {
> -      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
> +      lh->maximum_ops_per_instruction = read_1_byte (parent_bfd, line_ptr);
>        line_ptr += 1;
>      }
>    else
> @@ -345,41 +369,47 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
>                    "in `.debug_line' section"));
>      }
>
> -  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
> +  lh->default_is_stmt = read_1_byte (parent_bfd, line_ptr);
>    line_ptr += 1;
> -  lh->line_base = read_1_signed_byte (abfd, line_ptr);
> +
> +  lh->line_base = read_1_signed_byte (parent_bfd, line_ptr);
>    line_ptr += 1;
> -  lh->line_range = read_1_byte (abfd, line_ptr);
> +
> +  lh->line_range = read_1_byte (parent_bfd, line_ptr);
>    line_ptr += 1;
> -  lh->opcode_base = read_1_byte (abfd, line_ptr);
> +
> +  lh->opcode_base = read_1_byte (parent_bfd, line_ptr);
>    line_ptr += 1;
> +
>    lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
>
>    lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
>    for (i = 1; i < lh->opcode_base; ++i)
>      {
> -      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
> +      lh->standard_opcode_lengths[i] = read_1_byte (parent_bfd, line_ptr);
>        line_ptr += 1;
>      }
>
>    if (lh->version >= 5)
>      {
>        /* Read directory table.  */
> -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> -                             offset_size,
> -                             [] (struct line_header *header, const char *name,
> -                                 dir_index d_index, unsigned int mod_time,
> -                                 unsigned int length)
> +      read_formatted_entries
> +       (parent_bfd, &line_ptr, line_str_data,
> +        lh.get (), offset_size,
> +        [] (struct line_header *header, const char *name,
> +            dir_index d_index, unsigned int mod_time,
> +            unsigned int length)
>         {
>           header->add_include_dir (name);
>         });
>
>        /* Read file name table.  */
> -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> -                             offset_size,
> -                             [] (struct line_header *header, const char *name,
> -                                 dir_index d_index, unsigned int mod_time,
> -                                 unsigned int length)
> +      read_formatted_entries
> +       (parent_bfd, &line_ptr, line_str_data,
> +        lh.get (), offset_size,
> +        [] (struct line_header *header, const char *name,
> +            dir_index d_index, unsigned int mod_time,
> +            unsigned int length)
>         {
>           header->add_file_name (name, d_index, mod_time, length);
>         });
> @@ -387,7 +417,7 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
>    else
>      {
>        /* Read directory table.  */
> -      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> +      while ((cur_dir = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
>         {
>           line_ptr += bytes_read;
>           lh->add_include_dir (cur_dir);
> @@ -395,17 +425,17 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
>        line_ptr += bytes_read;
>
>        /* Read file name table.  */
> -      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> +      while ((cur_file = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
>         {
>           unsigned int mod_time, length;
>           dir_index d_index;
>
>           line_ptr += bytes_read;
> -         d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> +         d_index = (dir_index) read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
>           line_ptr += bytes_read;
> -         mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> +         mod_time = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
>           line_ptr += bytes_read;
> -         length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> +         length = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
>           line_ptr += bytes_read;
>
>           lh->add_file_name (cur_file, d_index, mod_time, length);
> @@ -413,9 +443,40 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
>        line_ptr += bytes_read;
>      }
>
> -  if (line_ptr > (section->buffer + section->size))
> +  if (line_ptr > (buf + line_data.size ()))
>      complaint (_("line number info header doesn't "
>                  "fit in `.debug_line' section"));
>
> +  *debug_line_ptr += unit_length + offset_size;
>    return lh;
>  }
> +
> +line_header_up
> +dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> +                          dwarf2_per_objfile *per_objfile,
> +                          struct dwarf2_section_info *section,
> +                          const struct comp_unit_head *cu_header,
> +                          const char *comp_dir)
> +{
> +  struct objfile *objfile = per_objfile->objfile;
> +  struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
> +
> +  /* Read .debug_line.  */
> +  dwarf2_section_info *line_sec = &per_bfd->line;
> +  bfd_size_type line_size = line_sec->get_size (objfile);
> +
> +  gdb::array_view<const gdb_byte> line (line_sec->buffer, line_size);
> +
> +  /* Read .debug_line_str.  */
> +  dwarf2_section_info *line_str_sec = &per_bfd->line_str;
> +  bfd_size_type line_str_size = line_str_sec->get_size (objfile);
> +
> +  gdb::array_view<const gdb_byte> line_str (line_str_sec->buffer,
> +                                           line_str_size);
> +
> +  const gdb_byte *line_ptr = line.data () + to_underlying (sect_off);
> +
> +  return dwarf_decode_line_header
> +    (per_bfd->obfd, line, line_str, &line_ptr,
> +     is_dwz, cu_header, comp_dir);
> +}
> diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
> index 06d2eec573b..22db9f9aa78 100644
> --- a/gdb/dwarf2/line-header.h
> +++ b/gdb/dwarf2/line-header.h
> @@ -217,4 +217,14 @@ extern line_header_up dwarf_decode_line_header
>     struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
>     const char *comp_dir);
>
> +/* Like above but the .debug_line and .debug_line_str are stored in
> +   LINE_DATA and LINE_STR_DATA. *DEBUG_LINE_PTR should point to a
> +   statement program header within LINE_DATA.  */
> +
> +extern line_header_up dwarf_decode_line_header
> +  (bfd *parent_bfd, gdb::array_view<const gdb_byte> line_data,
> +   gdb::array_view<const gdb_byte> line_str_data,
> +   const gdb_byte **debug_line_ptr, bool is_dwz,
> +  const comp_unit_head *cu_header, const char *comp_dir);
> +
>  #endif /* DWARF2_LINE_HEADER_H */
> diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
> index da88a8b405c..c0e51357ce2 100644
> --- a/gdb/dwarf2/read-gdb-index.c
> +++ b/gdb/dwarf2/read-gdb-index.c
> @@ -131,6 +131,9 @@ struct mapped_gdb_index final : public mapped_index_base
>    }
>  };
>
> +struct mapped_debug_line;
> +typedef std::unique_ptr<mapped_debug_line> mapped_debug_line_up;
> +
>  struct dwarf2_gdb_index : public dwarf2_base_index_functions
>  {
>    /* This dumps minimal information about the index.
> @@ -175,6 +178,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
>       domain_enum domain,
>       enum search_domain kind);
>
> + /* If OBJFILE's debuginfo download has been deferred, use a mapped_debug_line
> +    to generate filenames.
> +
> +    Otherwise call dwarf2_base_index_functions::map_symbol_filenames.  */
> +
> +  void map_symbol_filenames (struct objfile *objfile,
> +                            gdb::function_view<symbol_filename_ftype> fun,
> +                            bool need_fullname) override;
> +
>    /* Calls dwarf2_base_index_functions::expand_all_symtabs and downloads
>       debuginfo if necessary.  */
>    void expand_all_symtabs (struct objfile *objfile) override;
> @@ -182,6 +194,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
>    /* Calls dwarf2_base_index_functions::find_last_source_symtab and downloads
>       debuginfo if necessary.  */
>    struct symtab *find_last_source_symtab (struct objfile *objfile) override;
> +
> +  /* Filename information related to this .gdb_index.  */
> +  mapped_debug_line_up mdl;
> +
> +  /* Return true if any of the filenames in this .gdb_index's .debug_line
> +     mapping match FILE_MATCHER.  Initializes the mapping if necessary.  */
> +  bool filename_in_debug_line
> +    (objfile *objfile,
> +     gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
>  };
>
>  void
> @@ -217,6 +238,30 @@ dwarf2_gdb_index::find_last_source_symtab (struct objfile *objfile)
>      }
>  }
>
> +void
> +dwarf2_gdb_index::map_symbol_filenames
> +     (struct objfile *objfile,
> +      gdb::function_view<symbol_filename_ftype> fun,
> +      bool need_fullname)
> +{
> +  try
> +    {
> +      dwarf2_base_index_functions::map_symbol_filenames (objfile, fun,
> +                                                        need_fullname);
> +    }
> +  catch (const gdb_exception &e)
> +    {
> +      if ((objfile->flags & OBJF_DOWNLOAD_DEFERRED) == 0)
> +       exception_print (gdb_stderr, e);
> +      else
> +       {
> +         if (mdl == nullptr)
> +           mdl.reset (new mapped_debug_line (objfile));
> +         mdl->map_filenames (fun, need_fullname);
> +       }
> +    }
> +}
> +
>  /* This dumps minimal information about the index.
>     It is called via "mt print objfiles".
>     One use is to verify .gdb_index has been loaded by the
> @@ -590,6 +635,17 @@ dwarf2_gdb_index::do_expand_symtabs_matching
>    return result;
>  }
>
> +bool
> +dwarf2_gdb_index::filename_in_debug_line
> +  (objfile *objfile,
> +   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> +{
> +  if (mdl == nullptr)
> +    mdl.reset (new mapped_debug_line (objfile));
> +
> +  return mdl->contains_matching_filename (file_matcher);
> +}
> +
>  bool
>  dwarf2_gdb_index::expand_symtabs_matching
>      (struct objfile *objfile,
> @@ -618,6 +674,10 @@ dwarf2_gdb_index::expand_symtabs_matching
>           return false;
>         }
>
> +      if (file_matcher != nullptr
> +         && !filename_in_debug_line (objfile, file_matcher))
> +       return true;
> +
>        read_full_dwarf_from_debuginfod (objfile, this);
>        return true;
>      }
> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> index 0c5689c63ef..876e3aedcf1 100644
> --- a/gdb/dwarf2/read.c
> +++ b/gdb/dwarf2/read.c
> @@ -81,6 +81,7 @@
>  #include "gdbsupport/gdb_optional.h"
>  #include "gdbsupport/underlying.h"
>  #include "gdbsupport/hash_enum.h"
> +#include "gdbsupport/scoped_mmap.h"
>  #include "filename-seen-cache.h"
>  #include "producer.h"
>  #include <fcntl.h>
> @@ -2135,6 +2136,213 @@ dw2_get_file_names (dwarf2_per_cu_data *this_cu,
>    return this_cu->file_names;
>  }
>
> +#if !HAVE_SYS_MMAN_H
> +
> +bool
> +mapped_debug_line::contains_matching_filename
> +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> +{
> +  return false;
> +}
> +
> +gdb::array_view<const gdb_byte>
> +mapped_debug_line::read_debug_line_separate
> +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> +{
> +  return {};
> +}
> +
> +bool
> +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> +{
> +  return false;
> +}
> +
> +void
> +mapped_debug_line::map_filenames
> +  (gdb::function_view<symbol_filename_ftype> fun,
> +   bool need_fullname)
> +{
> +  return;
> +}
> +
> +#else /* !HAVE_SYS_MMAN_H */
> +
> +struct line_resource_mmap final : public index_cache_resource
> +{
> +  /* Try to mmap FILENAME.  Throw an exception on failure, including if the
> +     file doesn't exist. */
> +  line_resource_mmap (const char *filename)
> +    : mapping (mmap_file (filename))
> +  {}
> +
> +  scoped_mmap mapping;
> +};
> +
> +/* See read.h.  */
> +
> +bool
> +mapped_debug_line::contains_matching_filename
> +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> +{
> +  for (line_header_up &lh : line_headers)
> +    for (file_entry &fe : lh->file_names ())
> +      {
> +       const char *filename = fe.name;
> +
> +       if (file_matcher (fe.name, false))
> +         return true;
> +
> +       bool basename_match = file_matcher (lbasename (fe.name), true);
> +
> +       if (!basenames_may_differ && !basename_match)
> +         continue;
> +
> +       /* DW_AT_comp_dir is not explicitly mentioned in the .debug_line
> +          until DWARF5.  Since we don't have access to the CU at this
> +          point we just check for a partial match on the filename.
> +          If there is a match, the full debuginfo will be downloaded
> +          ane the match will be re-evalute with DW_AT_comp_dir.  */
> +       if (lh->version < 5 && fe.d_index == 0)
> +         return basename_match;
> +
> +       const char *dirname = fe.include_dir (&*lh);
> +       std::string fullname;
> +
> +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> +         fullname = filename;
> +       else
> +         fullname = std::string (dirname) + SLASH_STRING + filename;
> +
> +       gdb::unique_xmalloc_ptr<char> rewritten
> +         = rewrite_source_path (fullname.c_str ());
> +       if (rewritten != nullptr)
> +         fullname = rewritten.release ();
> +
> +       if (file_matcher (fullname.c_str (), false))
> +         return true;
> +      }
> +
> +  return false;
> +}
> +
> +/* See read.h.  */
> +
> +void
> +mapped_debug_line::map_filenames
> +  (gdb::function_view<symbol_filename_ftype> fun,
> +   bool need_fullname)
> +{
> +  for (line_header_up &lh : line_headers)
> +    for (file_entry &fe : lh->file_names ())
> +      {
> +       const char *filename = fe.name;
> +
> +       if (!need_fullname)
> +         {
> +           fun (filename, nullptr);
> +           continue;
> +         }
> +
> +       const char *dirname = fe.include_dir (&*lh);
> +       std::string fullname;
> +
> +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> +         fullname = filename;
> +       else
> +         fullname = std::string (dirname) + SLASH_STRING + filename;
> +
> +       gdb::unique_xmalloc_ptr<char> rewritten
> +         = rewrite_source_path (fullname.c_str ());
> +       if (rewritten != nullptr)
> +         fullname = rewritten.release ();
> +
> +       fun (filename, fullname.c_str ());
> +      }
> +}
> +
> +/* See read.h.  */
> +
> +gdb::array_view<const gdb_byte>
> +mapped_debug_line::read_debug_line_separate
> +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> +{
> +  if (filename == nullptr)
> +    return {};
> +
> +  try
> +  {
> +    line_resource_mmap *mmap_resource
> +      = new line_resource_mmap (filename);
> +
> +    resource->reset (mmap_resource);
> +
> +    return gdb::array_view<const gdb_byte>
> +      ((const gdb_byte *) mmap_resource->mapping.get (),
> +       mmap_resource->mapping.size ());
> +  }
> +  catch (const gdb_exception &except)
> +  {
> +    exception_print (gdb_stderr, except);
> +  }
> +
> +  return {};
> +}
> +
> +/* See read.h.  */
> +
> +bool
> +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> +{
> +  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
> +  if (build_id == nullptr)
> +    return false;
> +
> +  gdb::unique_xmalloc_ptr<char> line_path;
> +  scoped_fd line_fd = debuginfod_section_query (build_id->data,
> +                                               build_id->size,
> +                                               bfd_get_filename
> +                                                 (objfile->obfd.get ()),
> +                                               ".debug_line",
> +                                               &line_path);
> +
> +  if (line_fd.get () < 0)
> +    return false;
> +
> +  gdb::unique_xmalloc_ptr<char> line_str_path;
> +  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
> +                                                   build_id->size,
> +                                                   bfd_get_filename
> +                                                     (objfile->obfd.get ()),
> +                                                   ".debug_line_str",
> +                                                   &line_str_path);
> +
> +  line_data = read_debug_line_separate (line_path.get (), &line_resource);
> +  line_str_data = read_debug_line_separate (line_str_path.get (),
> +                                           &line_str_resource);
> +
> +  const gdb_byte *line_ptr = line_data.data ();
> +
> +  while (line_ptr < line_data.data () + line_data.size ())
> +    {
> +      line_header_up lh
> +       = dwarf_decode_line_header (objfile->obfd.get (),
> +                                   line_data, line_str_data,
> +                                   &line_ptr, false,
> +                                   nullptr, nullptr);
> +      line_headers.emplace_back (lh.release ());
> +    }
> +
> +  return true;
> +}
> +#endif /* !HAVE_SYS_MMAN_H */
> +
> +mapped_debug_line::mapped_debug_line (objfile *objfile)
> +{
> +  if (!read_debug_line_from_debuginfod (objfile))
> +    line_headers.clear ();
> +}
> +
>  /* A helper for the "quick" functions which computes and caches the
>     real path for a given file name from the line table.  */
>
> diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
> index 6ed0be7203b..49fea22c092 100644
> --- a/gdb/dwarf2/read.h
> +++ b/gdb/dwarf2/read.h
> @@ -33,6 +33,7 @@
>  #include "gdbsupport/hash_enum.h"
>  #include "gdbsupport/function-view.h"
>  #include "gdbsupport/packed.h"
> +#include "dwarf2/line-header.h"
>
>  /* Hold 'maintenance (set|show) dwarf' commands.  */
>  extern struct cmd_list_element *set_dwarf_cmdlist;
> @@ -969,4 +970,40 @@ extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
>  extern void read_full_dwarf_from_debuginfod (struct objfile *,
>                                              dwarf2_base_index_functions *);
>
> +struct mapped_debug_line
> +{
> +  mapped_debug_line (objfile *objfile);
> +
> +  /* Return true if any of the mapped .debug_line's filenames match
> +     FILE_MATCHER.  */
> +
> +  bool contains_matching_filename
> +    (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> +
> +  /* Call FUN with each filename in this mapped .debug_line.  Include
> +     each file's fullname if NEED_FULLNAME is true.  */
> +
> +  void map_filenames (gdb::function_view<symbol_filename_ftype> fun,
> +                     bool need_fullname);
> +
> +private:
> +  std::vector<line_header_up> line_headers;
> +
> +  gdb::array_view<const gdb_byte> line_data;
> +  gdb::array_view<const gdb_byte> line_str_data;
> +
> +  std::unique_ptr<index_cache_resource> line_resource;
> +  std::unique_ptr<index_cache_resource> line_str_resource;
> +
> +  /* Download the .debug_line and .debug_line_str associated with OBJFILE
> +     and populate line_headers.  */
> +
> +  bool read_debug_line_from_debuginfod (objfile *objfile);
> +
> +  /* Initialize line_data and line_str_data with the .debug_line and
> +    .debug_line_str downloaded read_debug_line_from_debuginfod.  */
> +
> +  gdb::array_view<const gdb_byte> read_debug_line_separate
> +    (char *filename, std::unique_ptr<index_cache_resource> *resource);
> +};
>  #endif /* DWARF2READ_H */
> diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
> index bbd21287b28..110864adac3 100644
> --- a/gdb/mi/mi-out.c
> +++ b/gdb/mi/mi-out.c
> @@ -278,7 +278,14 @@ mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
>
>    if (info.state == progress_update::START)
>      {
> -      gdb_printf ("%s...\n", msg.c_str ());
> +      std::string prefix;
> +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> +       {
> +         prefix = "\n";
> +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> +       }
> +
> +      gdb_printf ("%s...\n", (prefix + msg).c_str ());
>        info.state = progress_update::WORKING;
>      }
>  }
> diff --git a/gdb/testsuite/gdb.debuginfod/section.exp b/gdb/testsuite/gdb.debuginfod/section.exp
> index ff57c6e32b7..b5c6929fcf7 100644
> --- a/gdb/testsuite/gdb.debuginfod/section.exp
> +++ b/gdb/testsuite/gdb.debuginfod/section.exp
> @@ -171,6 +171,27 @@ proc_with_prefix local_url { } {
>      # during backtrace.
>      set res ".*debug info for $lib_sl1\.\.\.\r\n\#1.*"
>      gdb_test "bt" $res "break backtrace"
> +
> +    clean_restart_with_prompt $sectexec "" "line 1"
> +
> +    # List source file using .debug_line download.
> +    set res ".*\.debug_line.*$lib_sl1.*21.*extern void libsection2_test.*"
> +    gdb_test "list $libsrc1:21" $res "line 1 list"
> +
> +    clean_restart_with_prompt $sectexec "" "line 2"
> +
> +    # Set breakpoint using .debug_line download.
> +    set res ".*section \.debug_line for $lib_sl1.*Breakpoint 2 at.*$libsrc1.*"
> +    gdb_test "br $libsrc1:37" $res "line 2 br"
> +
> +    # Continue to breakpoint.
> +    set res "Breakpoint 2, libsection1_test.*\"Cancelling thread\\\\n\".*"
> +    gdb_test "c" $res "line 2 continue"
> +
> +    # Check that download progress message is correctly formatted
> +    # when printing threads.
> +    set res ".*separate debug info for $lib_sl2\.\.\.\r\n.* 2    Thread.*"
> +    gdb_test "info thr" $res "line thread"
>  }
>
>  # Create CACHE and DB directories ready for debuginfod to use.
> diff --git a/gdb/ui-out.c b/gdb/ui-out.c
> index 9f643b1ce95..fde46bfbe94 100644
> --- a/gdb/ui-out.c
> +++ b/gdb/ui-out.c
> @@ -32,6 +32,9 @@
>  #include <memory>
>  #include <string>
>
> +/* Current state of newline prefixing for progress update messages.  */
> +prefix_state_t cur_prefix_state = prefix_state_t::NEWLINE_OFF;
> +
>  namespace {
>
>  /* A header of a ui_out_table.  */
> diff --git a/gdb/ui-out.h b/gdb/ui-out.h
> index 70a7145741f..7de8796aee0 100644
> --- a/gdb/ui-out.h
> +++ b/gdb/ui-out.h
> @@ -296,6 +296,21 @@ class ui_out
>        BAR
>      };
>
> +    /* Used to communicate the status of a newline prefix for the next progress
> +       update message.  */
> +    enum prefix_state
> +    {
> +      /* Do not modify the next progress update message.  */
> +      NEWLINE_OFF,
> +
> +      /* The next progress update message should include a newline prefix.  */
> +      NEWLINE_NEEDED,
> +
> +      /* A newline prefix was included in a debuginfod progress update
> +        message.  */
> +      NEWLINE_PRINTED
> +    };
> +
>      /* SHOULD_PRINT indicates whether something should be printed for a tty.  */
>      progress_update ()
>      {
> @@ -393,6 +408,11 @@ class ui_out
>    ui_out_level *current_level () const;
>  };
>
> +typedef ui_out::progress_update::prefix_state prefix_state_t;
> +
> +/* Current state of the newline prefix.  */
> +extern prefix_state_t cur_prefix_state;
> +
>  /* Start a new tuple or list on construction, and end it on
>     destruction.  Normally this is used via the typedefs
>     ui_out_emit_tuple and ui_out_emit_list.  */
> --
> 2.41.0
>
  
Aaron Merey Nov. 20, 2023, 6:40 p.m. UTC | #2
Ping

Thanks,
Aaron

On Sun, Nov 12, 2023 at 3:21 PM Aaron Merey <amerey@redhat.com> wrote:
>
> Ping
>
> Thanks,
> Aaron
>
> On Fri, Oct 27, 2023 at 8:20 PM Aaron Merey <amerey@redhat.com> wrote:
> >
> > v4: https://sourceware.org/pipermail/gdb-patches/2023-August/201651.html
> >
> > v5 adds prefix_state to progress_update objects to track when
> > a newline prefix needs to be added to a download progress message.
> > This is used to correctly format progress messages that occur during
> > command autocompletion.
> >
> > Commit message:
> >
> > ELF/DWARF section downloading allows gdb to download .gdb_index files in
> > order to defer full debuginfo downloads.  However .gdb_index does not
> > contain any information regarding source filenames.  When a gdb command
> > includes a filename argument (ex. 'break main.c:50'), this results in
> > the mass downloading of all deferred debuginfo so that gdb can search the
> > debuginfo for matching source filenames.  This can result in unnecessary
> > downloads.
> >
> > To improve this, have gdb instead download each debuginfo's .debug_line
> > (and .debug_line_str if using DWARF5) when executing these commands.
> > Download full debuginfo only when its .debug_line contains a matching
> > filename.
> >
> > Since the combined size of .debug_line and .debug_line_str is only about
> > 1% the size of the corresponding debuginfo, significant time can be saved
> > by checking these sections before choosing to download an entire debuginfo.
> >
> > This patch also redirects stdout and stderr of the debuginfod server
> > used by testsuite/gdb.debuginfod tests to a server_log standard output
> > file.  While adding tests for this patch I ran into an issue where the
> > test server would block when logging to stderr, presumably because the
> > stderr buffer filled up and wasn't being read from.  Redirecting the
> > log to a file fixes this and also makes the server log more accessible
> > when debugging test failures.
> > ---
> >  gdb/cli-out.c                            |  11 +-
> >  gdb/completer.c                          |  18 +-
> >  gdb/dwarf2/line-header.c                 | 215 +++++++++++++++--------
> >  gdb/dwarf2/line-header.h                 |  10 ++
> >  gdb/dwarf2/read-gdb-index.c              |  60 +++++++
> >  gdb/dwarf2/read.c                        | 208 ++++++++++++++++++++++
> >  gdb/dwarf2/read.h                        |  37 ++++
> >  gdb/mi/mi-out.c                          |   9 +-
> >  gdb/testsuite/gdb.debuginfod/section.exp |  21 +++
> >  gdb/ui-out.c                             |   3 +
> >  gdb/ui-out.h                             |  20 +++
> >  11 files changed, 531 insertions(+), 81 deletions(-)
> >
> > diff --git a/gdb/cli-out.c b/gdb/cli-out.c
> > index c919622d418..a570a2d939d 100644
> > --- a/gdb/cli-out.c
> > +++ b/gdb/cli-out.c
> > @@ -307,16 +307,23 @@ cli_ui_out::do_progress_notify (const std::string &msg,
> >
> >    if (info.state == progress_update::START)
> >      {
> > +      std::string prefix;
> > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > +       {
> > +         prefix = "\n";
> > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > +       }
> > +
> >        if (stream->isatty ()
> >           && current_ui->input_interactive_p ()
> >           && chars_per_line >= MIN_CHARS_PER_LINE)
> >         {
> > -         gdb_printf (stream, "%s\n", msg.c_str ());
> > +         gdb_printf (stream, "%s\n", (prefix + msg).c_str ());
> >           info.state = progress_update::BAR;
> >         }
> >        else
> >         {
> > -         gdb_printf (stream, "%s...\n", msg.c_str ());
> > +         gdb_printf (stream, "%s...\n", (prefix + msg).c_str ());
> >           info.state = progress_update::WORKING;
> >         }
> >      }
> > diff --git a/gdb/completer.c b/gdb/completer.c
> > index 2abf3998345..9c299f3eb65 100644
> > --- a/gdb/completer.c
> > +++ b/gdb/completer.c
> > @@ -1345,6 +1345,10 @@ complete_line_internal_1 (completion_tracker &tracker,
> >      {
> >        /* We've recognized a full command.  */
> >
> > +      /* Disable pagination since responding to the pagination prompt
> > +        overwrites rl_line_buffer.  */
> > +      scoped_restore pag_restore = make_scoped_restore (&pagination_enabled, false);
> > +
> >        if (p == tmp_command + point)
> >         {
> >           /* There is no non-whitespace in the line beyond the
> > @@ -1444,7 +1448,8 @@ complete_line_internal_1 (completion_tracker &tracker,
> >  }
> >
> >  /* Wrapper around complete_line_internal_1 to handle
> > -   MAX_COMPLETIONS_REACHED_ERROR.  */
> > +   MAX_COMPLETIONS_REACHED_ERROR and possible progress update
> > +   interactions.  */
> >
> >  static void
> >  complete_line_internal (completion_tracker &tracker,
> > @@ -1452,6 +1457,11 @@ complete_line_internal (completion_tracker &tracker,
> >                         const char *line_buffer, int point,
> >                         complete_line_internal_reason reason)
> >  {
> > +  scoped_restore restore_prefix_state
> > +    = make_scoped_restore
> > +      (&cur_prefix_state,
> > +       ui_out::progress_update::prefix_state::NEWLINE_NEEDED);
> > +
> >    try
> >      {
> >        complete_line_internal_1 (tracker, text, line_buffer, point, reason);
> > @@ -1461,6 +1471,12 @@ complete_line_internal (completion_tracker &tracker,
> >        if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
> >         throw;
> >      }
> > +
> > +  /* If progress update messages printed, then the text being completed
> > +     needs to be printed again.  */
> > +  if (cur_prefix_state
> > +      == ui_out::progress_update::prefix_state::NEWLINE_PRINTED)
> > +    rl_forced_update_display ();
> >  }
> >
> >  /* See completer.h.  */
> > diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
> > index d072a91bac9..b9210d84f6b 100644
> > --- a/gdb/dwarf2/line-header.c
> > +++ b/gdb/dwarf2/line-header.c
> > @@ -102,50 +102,57 @@ read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
> >  {
> >    LONGEST length = read_initial_length (abfd, buf, bytes_read);
> >
> > -  gdb_assert (cu_header->initial_length_size == 4
> > -             || cu_header->initial_length_size == 8
> > -             || cu_header->initial_length_size == 12);
> > +  if (cu_header != nullptr)
> > +    {
> > +      gdb_assert (cu_header->initial_length_size == 4
> > +                 || cu_header->initial_length_size == 8
> > +                 || cu_header->initial_length_size == 12);
> >
> > -  if (cu_header->initial_length_size != *bytes_read)
> > -    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > +      if (cu_header->initial_length_size != *bytes_read)
> > +       complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > +    }
> >
> >    *offset_size = (*bytes_read == 4) ? 4 : 8;
> >    return length;
> >  }
> >
> > -/* Read directory or file name entry format, starting with byte of
> > -   format count entries, ULEB128 pairs of entry formats, ULEB128 of
> > -   entries count and the entries themselves in the described entry
> > -   format.  */
> > +
> > +/* Like read_formatted_entries but the .debug_line and .debug_line_str
> > +   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
> > +   where these sections are read from separate files without necessarily
> > +   having access to the entire debuginfo file they originate from.  */
> >
> >  static void
> > -read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > -                       const gdb_byte **bufp, struct line_header *lh,
> > -                       unsigned int offset_size,
> > -                       void (*callback) (struct line_header *lh,
> > -                                         const char *name,
> > -                                         dir_index d_index,
> > -                                         unsigned int mod_time,
> > -                                         unsigned int length))
> > +read_formatted_entries
> > +  (bfd *parent_bfd, const gdb_byte **line_bufp,
> > +   const gdb::array_view<const gdb_byte> line_str_data,
> > +   struct line_header *lh,
> > +   unsigned int offset_size,
> > +   void (*callback) (struct line_header *lh,
> > +                    const char *name,
> > +                    dir_index d_index,
> > +                    unsigned int mod_time,
> > +                    unsigned int length))
> >  {
> >    gdb_byte format_count, formati;
> >    ULONGEST data_count, datai;
> > -  const gdb_byte *buf = *bufp;
> > +  const gdb_byte *buf = *line_bufp;
> > +  const gdb_byte *str_buf = line_str_data.data ();
> >    const gdb_byte *format_header_data;
> >    unsigned int bytes_read;
> >
> > -  format_count = read_1_byte (abfd, buf);
> > +  format_count = read_1_byte (parent_bfd, buf);
> >    buf += 1;
> >    format_header_data = buf;
> >    for (formati = 0; formati < format_count; formati++)
> >      {
> > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> >        buf += bytes_read;
> > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> >        buf += bytes_read;
> >      }
> >
> > -  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
> > +  data_count = read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> >    buf += bytes_read;
> >    for (datai = 0; datai < data_count; datai++)
> >      {
> > @@ -154,10 +161,10 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> >
> >        for (formati = 0; formati < format_count; formati++)
> >         {
> > -         ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
> > +         ULONGEST content_type = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> >           format += bytes_read;
> >
> > -         ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
> > +         ULONGEST form  = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> >           format += bytes_read;
> >
> >           gdb::optional<const char *> string;
> > @@ -166,36 +173,48 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> >           switch (form)
> >             {
> >             case DW_FORM_string:
> > -             string.emplace (read_direct_string (abfd, buf, &bytes_read));
> > +             string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
> >               buf += bytes_read;
> >               break;
> >
> >             case DW_FORM_line_strp:
> >               {
> > -               const char *str
> > -                 = per_objfile->read_line_string (buf, offset_size);
> > +               if (line_str_data.empty ())
> > +                 error (_("Dwarf Error: DW_FORM_line_strp used without " \
> > +                          "required section"));
> > +               if (line_str_data.size () <= offset_size)
> > +                 error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
> > +                          "of section .debug_line"));
> > +
> > +               ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
> > +
> > +               const char *str;
> > +               if (str_buf[str_offset] == '\0')
> > +                 str = nullptr;
> > +               else
> > +                 str = (const char *) (str_buf + str_offset);
> >                 string.emplace (str);
> >                 buf += offset_size;
> > +               break;
> >               }
> > -             break;
> >
> >             case DW_FORM_data1:
> > -             uint.emplace (read_1_byte (abfd, buf));
> > +             uint.emplace (read_1_byte (parent_bfd, buf));
> >               buf += 1;
> >               break;
> >
> >             case DW_FORM_data2:
> > -             uint.emplace (read_2_bytes (abfd, buf));
> > +             uint.emplace (read_2_bytes (parent_bfd, buf));
> >               buf += 2;
> >               break;
> >
> >             case DW_FORM_data4:
> > -             uint.emplace (read_4_bytes (abfd, buf));
> > +             uint.emplace (read_4_bytes (parent_bfd, buf));
> >               buf += 4;
> >               break;
> >
> >             case DW_FORM_data8:
> > -             uint.emplace (read_8_bytes (abfd, buf));
> > +             uint.emplace (read_8_bytes (parent_bfd, buf));
> >               buf += 8;
> >               break;
> >
> > @@ -205,7 +224,7 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> >               break;
> >
> >             case DW_FORM_udata:
> > -             uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
> > +             uint.emplace (read_unsigned_leb128 (parent_bfd, buf, &bytes_read));
> >               buf += bytes_read;
> >               break;
> >
> > @@ -248,28 +267,30 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> >        callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
> >      }
> >
> > -  *bufp = buf;
> > +  *line_bufp = buf;
> >  }
> >
> >  /* See line-header.h.  */
> >
> >  line_header_up
> > -dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > -                          dwarf2_per_objfile *per_objfile,
> > -                          struct dwarf2_section_info *section,
> > -                          const struct comp_unit_head *cu_header,
> > -                          const char *comp_dir)
> > +dwarf_decode_line_header (bfd *parent_bfd,
> > +                         gdb::array_view<const gdb_byte> line_data,
> > +                         gdb::array_view<const gdb_byte> line_str_data,
> > +                         const gdb_byte **debug_line_ptr,
> > +                         bool is_dwz,
> > +                         const struct comp_unit_head *cu_header,
> > +                         const char *comp_dir)
> >  {
> > -  const gdb_byte *line_ptr;
> > +  const gdb_byte *line_ptr, *buf;
> >    unsigned int bytes_read, offset_size;
> >    int i;
> >    const char *cur_dir, *cur_file;
> >
> > -  bfd *abfd = section->get_bfd_owner ();
> > +  buf = *debug_line_ptr;
> >
> >    /* Make sure that at least there's room for the total_length field.
> >       That could be 12 bytes long, but we're just going to fudge that.  */
> > -  if (to_underlying (sect_off) + 4 >= section->size)
> > +  if (buf + 4 >= line_data.data () + line_data.size ())
> >      {
> >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> >        return 0;
> > @@ -277,62 +298,65 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> >
> >    line_header_up lh (new line_header (comp_dir));
> >
> > -  lh->sect_off = sect_off;
> > +  lh->sect_off = (sect_offset) (buf - line_data.data ());
> >    lh->offset_in_dwz = is_dwz;
> >
> > -  line_ptr = section->buffer + to_underlying (sect_off);
> > +  line_ptr = buf;
> >
> >    /* Read in the header.  */
> >    LONGEST unit_length
> > -    = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
> > +    = read_checked_initial_length_and_offset (parent_bfd, buf, cu_header,
> >                                               &bytes_read, &offset_size);
> > -  line_ptr += bytes_read;
> >
> > -  const gdb_byte *start_here = line_ptr;
> > +  line_ptr += bytes_read;
> >
> > -  if (line_ptr + unit_length > (section->buffer + section->size))
> > +  if (line_ptr + unit_length > buf + line_data.size ())
> >      {
> >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> >        return 0;
> >      }
> > +
> > +  const gdb_byte *start_here = line_ptr;
> > +
> >    lh->statement_program_end = start_here + unit_length;
> > -  lh->version = read_2_bytes (abfd, line_ptr);
> > +  lh->version = read_2_bytes (parent_bfd, line_ptr);
> >    line_ptr += 2;
> >    if (lh->version > 5)
> >      {
> >        /* This is a version we don't understand.  The format could have
> >          changed in ways we don't handle properly so just punt.  */
> >        complaint (_("unsupported version in .debug_line section"));
> > -      return NULL;
> > +      return nullptr;
> >      }
> >    if (lh->version >= 5)
> >      {
> >        gdb_byte segment_selector_size;
> >
> >        /* Skip address size.  */
> > -      read_1_byte (abfd, line_ptr);
> > +      read_1_byte (parent_bfd, line_ptr);
> >        line_ptr += 1;
> >
> > -      segment_selector_size = read_1_byte (abfd, line_ptr);
> > +      segment_selector_size = read_1_byte (parent_bfd, line_ptr);
> >        line_ptr += 1;
> >        if (segment_selector_size != 0)
> >         {
> >           complaint (_("unsupported segment selector size %u "
> >                        "in .debug_line section"),
> >                      segment_selector_size);
> > -         return NULL;
> > +         return nullptr;
> >         }
> >      }
> >
> > -  LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
> > +  LONGEST header_length = read_offset (parent_bfd, line_ptr, offset_size);
> >    line_ptr += offset_size;
> >    lh->statement_program_start = line_ptr + header_length;
> > -  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
> > +
> > +  lh->minimum_instruction_length = read_1_byte (parent_bfd, line_ptr);
> >    line_ptr += 1;
> >
> >    if (lh->version >= 4)
> >      {
> > -      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
> > +      lh->maximum_ops_per_instruction = read_1_byte (parent_bfd, line_ptr);
> >        line_ptr += 1;
> >      }
> >    else
> > @@ -345,41 +369,47 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> >                    "in `.debug_line' section"));
> >      }
> >
> > -  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
> > +  lh->default_is_stmt = read_1_byte (parent_bfd, line_ptr);
> >    line_ptr += 1;
> > -  lh->line_base = read_1_signed_byte (abfd, line_ptr);
> > +
> > +  lh->line_base = read_1_signed_byte (parent_bfd, line_ptr);
> >    line_ptr += 1;
> > -  lh->line_range = read_1_byte (abfd, line_ptr);
> > +
> > +  lh->line_range = read_1_byte (parent_bfd, line_ptr);
> >    line_ptr += 1;
> > -  lh->opcode_base = read_1_byte (abfd, line_ptr);
> > +
> > +  lh->opcode_base = read_1_byte (parent_bfd, line_ptr);
> >    line_ptr += 1;
> > +
> >    lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
> >
> >    lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
> >    for (i = 1; i < lh->opcode_base; ++i)
> >      {
> > -      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
> > +      lh->standard_opcode_lengths[i] = read_1_byte (parent_bfd, line_ptr);
> >        line_ptr += 1;
> >      }
> >
> >    if (lh->version >= 5)
> >      {
> >        /* Read directory table.  */
> > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > -                             offset_size,
> > -                             [] (struct line_header *header, const char *name,
> > -                                 dir_index d_index, unsigned int mod_time,
> > -                                 unsigned int length)
> > +      read_formatted_entries
> > +       (parent_bfd, &line_ptr, line_str_data,
> > +        lh.get (), offset_size,
> > +        [] (struct line_header *header, const char *name,
> > +            dir_index d_index, unsigned int mod_time,
> > +            unsigned int length)
> >         {
> >           header->add_include_dir (name);
> >         });
> >
> >        /* Read file name table.  */
> > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > -                             offset_size,
> > -                             [] (struct line_header *header, const char *name,
> > -                                 dir_index d_index, unsigned int mod_time,
> > -                                 unsigned int length)
> > +      read_formatted_entries
> > +       (parent_bfd, &line_ptr, line_str_data,
> > +        lh.get (), offset_size,
> > +        [] (struct line_header *header, const char *name,
> > +            dir_index d_index, unsigned int mod_time,
> > +            unsigned int length)
> >         {
> >           header->add_file_name (name, d_index, mod_time, length);
> >         });
> > @@ -387,7 +417,7 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> >    else
> >      {
> >        /* Read directory table.  */
> > -      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > +      while ((cur_dir = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> >         {
> >           line_ptr += bytes_read;
> >           lh->add_include_dir (cur_dir);
> > @@ -395,17 +425,17 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> >        line_ptr += bytes_read;
> >
> >        /* Read file name table.  */
> > -      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > +      while ((cur_file = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> >         {
> >           unsigned int mod_time, length;
> >           dir_index d_index;
> >
> >           line_ptr += bytes_read;
> > -         d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > +         d_index = (dir_index) read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> >           line_ptr += bytes_read;
> > -         mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > +         mod_time = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> >           line_ptr += bytes_read;
> > -         length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > +         length = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> >           line_ptr += bytes_read;
> >
> >           lh->add_file_name (cur_file, d_index, mod_time, length);
> > @@ -413,9 +443,40 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> >        line_ptr += bytes_read;
> >      }
> >
> > -  if (line_ptr > (section->buffer + section->size))
> > +  if (line_ptr > (buf + line_data.size ()))
> >      complaint (_("line number info header doesn't "
> >                  "fit in `.debug_line' section"));
> >
> > +  *debug_line_ptr += unit_length + offset_size;
> >    return lh;
> >  }
> > +
> > +line_header_up
> > +dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > +                          dwarf2_per_objfile *per_objfile,
> > +                          struct dwarf2_section_info *section,
> > +                          const struct comp_unit_head *cu_header,
> > +                          const char *comp_dir)
> > +{
> > +  struct objfile *objfile = per_objfile->objfile;
> > +  struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
> > +
> > +  /* Read .debug_line.  */
> > +  dwarf2_section_info *line_sec = &per_bfd->line;
> > +  bfd_size_type line_size = line_sec->get_size (objfile);
> > +
> > +  gdb::array_view<const gdb_byte> line (line_sec->buffer, line_size);
> > +
> > +  /* Read .debug_line_str.  */
> > +  dwarf2_section_info *line_str_sec = &per_bfd->line_str;
> > +  bfd_size_type line_str_size = line_str_sec->get_size (objfile);
> > +
> > +  gdb::array_view<const gdb_byte> line_str (line_str_sec->buffer,
> > +                                           line_str_size);
> > +
> > +  const gdb_byte *line_ptr = line.data () + to_underlying (sect_off);
> > +
> > +  return dwarf_decode_line_header
> > +    (per_bfd->obfd, line, line_str, &line_ptr,
> > +     is_dwz, cu_header, comp_dir);
> > +}
> > diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
> > index 06d2eec573b..22db9f9aa78 100644
> > --- a/gdb/dwarf2/line-header.h
> > +++ b/gdb/dwarf2/line-header.h
> > @@ -217,4 +217,14 @@ extern line_header_up dwarf_decode_line_header
> >     struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
> >     const char *comp_dir);
> >
> > +/* Like above but the .debug_line and .debug_line_str are stored in
> > +   LINE_DATA and LINE_STR_DATA. *DEBUG_LINE_PTR should point to a
> > +   statement program header within LINE_DATA.  */
> > +
> > +extern line_header_up dwarf_decode_line_header
> > +  (bfd *parent_bfd, gdb::array_view<const gdb_byte> line_data,
> > +   gdb::array_view<const gdb_byte> line_str_data,
> > +   const gdb_byte **debug_line_ptr, bool is_dwz,
> > +  const comp_unit_head *cu_header, const char *comp_dir);
> > +
> >  #endif /* DWARF2_LINE_HEADER_H */
> > diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
> > index da88a8b405c..c0e51357ce2 100644
> > --- a/gdb/dwarf2/read-gdb-index.c
> > +++ b/gdb/dwarf2/read-gdb-index.c
> > @@ -131,6 +131,9 @@ struct mapped_gdb_index final : public mapped_index_base
> >    }
> >  };
> >
> > +struct mapped_debug_line;
> > +typedef std::unique_ptr<mapped_debug_line> mapped_debug_line_up;
> > +
> >  struct dwarf2_gdb_index : public dwarf2_base_index_functions
> >  {
> >    /* This dumps minimal information about the index.
> > @@ -175,6 +178,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> >       domain_enum domain,
> >       enum search_domain kind);
> >
> > + /* If OBJFILE's debuginfo download has been deferred, use a mapped_debug_line
> > +    to generate filenames.
> > +
> > +    Otherwise call dwarf2_base_index_functions::map_symbol_filenames.  */
> > +
> > +  void map_symbol_filenames (struct objfile *objfile,
> > +                            gdb::function_view<symbol_filename_ftype> fun,
> > +                            bool need_fullname) override;
> > +
> >    /* Calls dwarf2_base_index_functions::expand_all_symtabs and downloads
> >       debuginfo if necessary.  */
> >    void expand_all_symtabs (struct objfile *objfile) override;
> > @@ -182,6 +194,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> >    /* Calls dwarf2_base_index_functions::find_last_source_symtab and downloads
> >       debuginfo if necessary.  */
> >    struct symtab *find_last_source_symtab (struct objfile *objfile) override;
> > +
> > +  /* Filename information related to this .gdb_index.  */
> > +  mapped_debug_line_up mdl;
> > +
> > +  /* Return true if any of the filenames in this .gdb_index's .debug_line
> > +     mapping match FILE_MATCHER.  Initializes the mapping if necessary.  */
> > +  bool filename_in_debug_line
> > +    (objfile *objfile,
> > +     gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> >  };
> >
> >  void
> > @@ -217,6 +238,30 @@ dwarf2_gdb_index::find_last_source_symtab (struct objfile *objfile)
> >      }
> >  }
> >
> > +void
> > +dwarf2_gdb_index::map_symbol_filenames
> > +     (struct objfile *objfile,
> > +      gdb::function_view<symbol_filename_ftype> fun,
> > +      bool need_fullname)
> > +{
> > +  try
> > +    {
> > +      dwarf2_base_index_functions::map_symbol_filenames (objfile, fun,
> > +                                                        need_fullname);
> > +    }
> > +  catch (const gdb_exception &e)
> > +    {
> > +      if ((objfile->flags & OBJF_DOWNLOAD_DEFERRED) == 0)
> > +       exception_print (gdb_stderr, e);
> > +      else
> > +       {
> > +         if (mdl == nullptr)
> > +           mdl.reset (new mapped_debug_line (objfile));
> > +         mdl->map_filenames (fun, need_fullname);
> > +       }
> > +    }
> > +}
> > +
> >  /* This dumps minimal information about the index.
> >     It is called via "mt print objfiles".
> >     One use is to verify .gdb_index has been loaded by the
> > @@ -590,6 +635,17 @@ dwarf2_gdb_index::do_expand_symtabs_matching
> >    return result;
> >  }
> >
> > +bool
> > +dwarf2_gdb_index::filename_in_debug_line
> > +  (objfile *objfile,
> > +   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > +{
> > +  if (mdl == nullptr)
> > +    mdl.reset (new mapped_debug_line (objfile));
> > +
> > +  return mdl->contains_matching_filename (file_matcher);
> > +}
> > +
> >  bool
> >  dwarf2_gdb_index::expand_symtabs_matching
> >      (struct objfile *objfile,
> > @@ -618,6 +674,10 @@ dwarf2_gdb_index::expand_symtabs_matching
> >           return false;
> >         }
> >
> > +      if (file_matcher != nullptr
> > +         && !filename_in_debug_line (objfile, file_matcher))
> > +       return true;
> > +
> >        read_full_dwarf_from_debuginfod (objfile, this);
> >        return true;
> >      }
> > diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> > index 0c5689c63ef..876e3aedcf1 100644
> > --- a/gdb/dwarf2/read.c
> > +++ b/gdb/dwarf2/read.c
> > @@ -81,6 +81,7 @@
> >  #include "gdbsupport/gdb_optional.h"
> >  #include "gdbsupport/underlying.h"
> >  #include "gdbsupport/hash_enum.h"
> > +#include "gdbsupport/scoped_mmap.h"
> >  #include "filename-seen-cache.h"
> >  #include "producer.h"
> >  #include <fcntl.h>
> > @@ -2135,6 +2136,213 @@ dw2_get_file_names (dwarf2_per_cu_data *this_cu,
> >    return this_cu->file_names;
> >  }
> >
> > +#if !HAVE_SYS_MMAN_H
> > +
> > +bool
> > +mapped_debug_line::contains_matching_filename
> > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > +{
> > +  return false;
> > +}
> > +
> > +gdb::array_view<const gdb_byte>
> > +mapped_debug_line::read_debug_line_separate
> > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > +{
> > +  return {};
> > +}
> > +
> > +bool
> > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > +{
> > +  return false;
> > +}
> > +
> > +void
> > +mapped_debug_line::map_filenames
> > +  (gdb::function_view<symbol_filename_ftype> fun,
> > +   bool need_fullname)
> > +{
> > +  return;
> > +}
> > +
> > +#else /* !HAVE_SYS_MMAN_H */
> > +
> > +struct line_resource_mmap final : public index_cache_resource
> > +{
> > +  /* Try to mmap FILENAME.  Throw an exception on failure, including if the
> > +     file doesn't exist. */
> > +  line_resource_mmap (const char *filename)
> > +    : mapping (mmap_file (filename))
> > +  {}
> > +
> > +  scoped_mmap mapping;
> > +};
> > +
> > +/* See read.h.  */
> > +
> > +bool
> > +mapped_debug_line::contains_matching_filename
> > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > +{
> > +  for (line_header_up &lh : line_headers)
> > +    for (file_entry &fe : lh->file_names ())
> > +      {
> > +       const char *filename = fe.name;
> > +
> > +       if (file_matcher (fe.name, false))
> > +         return true;
> > +
> > +       bool basename_match = file_matcher (lbasename (fe.name), true);
> > +
> > +       if (!basenames_may_differ && !basename_match)
> > +         continue;
> > +
> > +       /* DW_AT_comp_dir is not explicitly mentioned in the .debug_line
> > +          until DWARF5.  Since we don't have access to the CU at this
> > +          point we just check for a partial match on the filename.
> > +          If there is a match, the full debuginfo will be downloaded
> > +          ane the match will be re-evalute with DW_AT_comp_dir.  */
> > +       if (lh->version < 5 && fe.d_index == 0)
> > +         return basename_match;
> > +
> > +       const char *dirname = fe.include_dir (&*lh);
> > +       std::string fullname;
> > +
> > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > +         fullname = filename;
> > +       else
> > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > +
> > +       gdb::unique_xmalloc_ptr<char> rewritten
> > +         = rewrite_source_path (fullname.c_str ());
> > +       if (rewritten != nullptr)
> > +         fullname = rewritten.release ();
> > +
> > +       if (file_matcher (fullname.c_str (), false))
> > +         return true;
> > +      }
> > +
> > +  return false;
> > +}
> > +
> > +/* See read.h.  */
> > +
> > +void
> > +mapped_debug_line::map_filenames
> > +  (gdb::function_view<symbol_filename_ftype> fun,
> > +   bool need_fullname)
> > +{
> > +  for (line_header_up &lh : line_headers)
> > +    for (file_entry &fe : lh->file_names ())
> > +      {
> > +       const char *filename = fe.name;
> > +
> > +       if (!need_fullname)
> > +         {
> > +           fun (filename, nullptr);
> > +           continue;
> > +         }
> > +
> > +       const char *dirname = fe.include_dir (&*lh);
> > +       std::string fullname;
> > +
> > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > +         fullname = filename;
> > +       else
> > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > +
> > +       gdb::unique_xmalloc_ptr<char> rewritten
> > +         = rewrite_source_path (fullname.c_str ());
> > +       if (rewritten != nullptr)
> > +         fullname = rewritten.release ();
> > +
> > +       fun (filename, fullname.c_str ());
> > +      }
> > +}
> > +
> > +/* See read.h.  */
> > +
> > +gdb::array_view<const gdb_byte>
> > +mapped_debug_line::read_debug_line_separate
> > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > +{
> > +  if (filename == nullptr)
> > +    return {};
> > +
> > +  try
> > +  {
> > +    line_resource_mmap *mmap_resource
> > +      = new line_resource_mmap (filename);
> > +
> > +    resource->reset (mmap_resource);
> > +
> > +    return gdb::array_view<const gdb_byte>
> > +      ((const gdb_byte *) mmap_resource->mapping.get (),
> > +       mmap_resource->mapping.size ());
> > +  }
> > +  catch (const gdb_exception &except)
> > +  {
> > +    exception_print (gdb_stderr, except);
> > +  }
> > +
> > +  return {};
> > +}
> > +
> > +/* See read.h.  */
> > +
> > +bool
> > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > +{
> > +  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
> > +  if (build_id == nullptr)
> > +    return false;
> > +
> > +  gdb::unique_xmalloc_ptr<char> line_path;
> > +  scoped_fd line_fd = debuginfod_section_query (build_id->data,
> > +                                               build_id->size,
> > +                                               bfd_get_filename
> > +                                                 (objfile->obfd.get ()),
> > +                                               ".debug_line",
> > +                                               &line_path);
> > +
> > +  if (line_fd.get () < 0)
> > +    return false;
> > +
> > +  gdb::unique_xmalloc_ptr<char> line_str_path;
> > +  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
> > +                                                   build_id->size,
> > +                                                   bfd_get_filename
> > +                                                     (objfile->obfd.get ()),
> > +                                                   ".debug_line_str",
> > +                                                   &line_str_path);
> > +
> > +  line_data = read_debug_line_separate (line_path.get (), &line_resource);
> > +  line_str_data = read_debug_line_separate (line_str_path.get (),
> > +                                           &line_str_resource);
> > +
> > +  const gdb_byte *line_ptr = line_data.data ();
> > +
> > +  while (line_ptr < line_data.data () + line_data.size ())
> > +    {
> > +      line_header_up lh
> > +       = dwarf_decode_line_header (objfile->obfd.get (),
> > +                                   line_data, line_str_data,
> > +                                   &line_ptr, false,
> > +                                   nullptr, nullptr);
> > +      line_headers.emplace_back (lh.release ());
> > +    }
> > +
> > +  return true;
> > +}
> > +#endif /* !HAVE_SYS_MMAN_H */
> > +
> > +mapped_debug_line::mapped_debug_line (objfile *objfile)
> > +{
> > +  if (!read_debug_line_from_debuginfod (objfile))
> > +    line_headers.clear ();
> > +}
> > +
> >  /* A helper for the "quick" functions which computes and caches the
> >     real path for a given file name from the line table.  */
> >
> > diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
> > index 6ed0be7203b..49fea22c092 100644
> > --- a/gdb/dwarf2/read.h
> > +++ b/gdb/dwarf2/read.h
> > @@ -33,6 +33,7 @@
> >  #include "gdbsupport/hash_enum.h"
> >  #include "gdbsupport/function-view.h"
> >  #include "gdbsupport/packed.h"
> > +#include "dwarf2/line-header.h"
> >
> >  /* Hold 'maintenance (set|show) dwarf' commands.  */
> >  extern struct cmd_list_element *set_dwarf_cmdlist;
> > @@ -969,4 +970,40 @@ extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
> >  extern void read_full_dwarf_from_debuginfod (struct objfile *,
> >                                              dwarf2_base_index_functions *);
> >
> > +struct mapped_debug_line
> > +{
> > +  mapped_debug_line (objfile *objfile);
> > +
> > +  /* Return true if any of the mapped .debug_line's filenames match
> > +     FILE_MATCHER.  */
> > +
> > +  bool contains_matching_filename
> > +    (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > +
> > +  /* Call FUN with each filename in this mapped .debug_line.  Include
> > +     each file's fullname if NEED_FULLNAME is true.  */
> > +
> > +  void map_filenames (gdb::function_view<symbol_filename_ftype> fun,
> > +                     bool need_fullname);
> > +
> > +private:
> > +  std::vector<line_header_up> line_headers;
> > +
> > +  gdb::array_view<const gdb_byte> line_data;
> > +  gdb::array_view<const gdb_byte> line_str_data;
> > +
> > +  std::unique_ptr<index_cache_resource> line_resource;
> > +  std::unique_ptr<index_cache_resource> line_str_resource;
> > +
> > +  /* Download the .debug_line and .debug_line_str associated with OBJFILE
> > +     and populate line_headers.  */
> > +
> > +  bool read_debug_line_from_debuginfod (objfile *objfile);
> > +
> > +  /* Initialize line_data and line_str_data with the .debug_line and
> > +    .debug_line_str downloaded read_debug_line_from_debuginfod.  */
> > +
> > +  gdb::array_view<const gdb_byte> read_debug_line_separate
> > +    (char *filename, std::unique_ptr<index_cache_resource> *resource);
> > +};
> >  #endif /* DWARF2READ_H */
> > diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
> > index bbd21287b28..110864adac3 100644
> > --- a/gdb/mi/mi-out.c
> > +++ b/gdb/mi/mi-out.c
> > @@ -278,7 +278,14 @@ mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
> >
> >    if (info.state == progress_update::START)
> >      {
> > -      gdb_printf ("%s...\n", msg.c_str ());
> > +      std::string prefix;
> > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > +       {
> > +         prefix = "\n";
> > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > +       }
> > +
> > +      gdb_printf ("%s...\n", (prefix + msg).c_str ());
> >        info.state = progress_update::WORKING;
> >      }
> >  }
> > diff --git a/gdb/testsuite/gdb.debuginfod/section.exp b/gdb/testsuite/gdb.debuginfod/section.exp
> > index ff57c6e32b7..b5c6929fcf7 100644
> > --- a/gdb/testsuite/gdb.debuginfod/section.exp
> > +++ b/gdb/testsuite/gdb.debuginfod/section.exp
> > @@ -171,6 +171,27 @@ proc_with_prefix local_url { } {
> >      # during backtrace.
> >      set res ".*debug info for $lib_sl1\.\.\.\r\n\#1.*"
> >      gdb_test "bt" $res "break backtrace"
> > +
> > +    clean_restart_with_prompt $sectexec "" "line 1"
> > +
> > +    # List source file using .debug_line download.
> > +    set res ".*\.debug_line.*$lib_sl1.*21.*extern void libsection2_test.*"
> > +    gdb_test "list $libsrc1:21" $res "line 1 list"
> > +
> > +    clean_restart_with_prompt $sectexec "" "line 2"
> > +
> > +    # Set breakpoint using .debug_line download.
> > +    set res ".*section \.debug_line for $lib_sl1.*Breakpoint 2 at.*$libsrc1.*"
> > +    gdb_test "br $libsrc1:37" $res "line 2 br"
> > +
> > +    # Continue to breakpoint.
> > +    set res "Breakpoint 2, libsection1_test.*\"Cancelling thread\\\\n\".*"
> > +    gdb_test "c" $res "line 2 continue"
> > +
> > +    # Check that download progress message is correctly formatted
> > +    # when printing threads.
> > +    set res ".*separate debug info for $lib_sl2\.\.\.\r\n.* 2    Thread.*"
> > +    gdb_test "info thr" $res "line thread"
> >  }
> >
> >  # Create CACHE and DB directories ready for debuginfod to use.
> > diff --git a/gdb/ui-out.c b/gdb/ui-out.c
> > index 9f643b1ce95..fde46bfbe94 100644
> > --- a/gdb/ui-out.c
> > +++ b/gdb/ui-out.c
> > @@ -32,6 +32,9 @@
> >  #include <memory>
> >  #include <string>
> >
> > +/* Current state of newline prefixing for progress update messages.  */
> > +prefix_state_t cur_prefix_state = prefix_state_t::NEWLINE_OFF;
> > +
> >  namespace {
> >
> >  /* A header of a ui_out_table.  */
> > diff --git a/gdb/ui-out.h b/gdb/ui-out.h
> > index 70a7145741f..7de8796aee0 100644
> > --- a/gdb/ui-out.h
> > +++ b/gdb/ui-out.h
> > @@ -296,6 +296,21 @@ class ui_out
> >        BAR
> >      };
> >
> > +    /* Used to communicate the status of a newline prefix for the next progress
> > +       update message.  */
> > +    enum prefix_state
> > +    {
> > +      /* Do not modify the next progress update message.  */
> > +      NEWLINE_OFF,
> > +
> > +      /* The next progress update message should include a newline prefix.  */
> > +      NEWLINE_NEEDED,
> > +
> > +      /* A newline prefix was included in a debuginfod progress update
> > +        message.  */
> > +      NEWLINE_PRINTED
> > +    };
> > +
> >      /* SHOULD_PRINT indicates whether something should be printed for a tty.  */
> >      progress_update ()
> >      {
> > @@ -393,6 +408,11 @@ class ui_out
> >    ui_out_level *current_level () const;
> >  };
> >
> > +typedef ui_out::progress_update::prefix_state prefix_state_t;
> > +
> > +/* Current state of the newline prefix.  */
> > +extern prefix_state_t cur_prefix_state;
> > +
> >  /* Start a new tuple or list on construction, and end it on
> >     destruction.  Normally this is used via the typedefs
> >     ui_out_emit_tuple and ui_out_emit_list.  */
> > --
> > 2.41.0
> >
  
Aaron Merey Nov. 30, 2023, 4:30 p.m. UTC | #3
Ping

Thanks,
Aaron

On Mon, Nov 20, 2023 at 1:40 PM Aaron Merey <amerey@redhat.com> wrote:
>
> Ping
>
> Thanks,
> Aaron
>
> On Sun, Nov 12, 2023 at 3:21 PM Aaron Merey <amerey@redhat.com> wrote:
> >
> > Ping
> >
> > Thanks,
> > Aaron
> >
> > On Fri, Oct 27, 2023 at 8:20 PM Aaron Merey <amerey@redhat.com> wrote:
> > >
> > > v4: https://sourceware.org/pipermail/gdb-patches/2023-August/201651.html
> > >
> > > v5 adds prefix_state to progress_update objects to track when
> > > a newline prefix needs to be added to a download progress message.
> > > This is used to correctly format progress messages that occur during
> > > command autocompletion.
> > >
> > > Commit message:
> > >
> > > ELF/DWARF section downloading allows gdb to download .gdb_index files in
> > > order to defer full debuginfo downloads.  However .gdb_index does not
> > > contain any information regarding source filenames.  When a gdb command
> > > includes a filename argument (ex. 'break main.c:50'), this results in
> > > the mass downloading of all deferred debuginfo so that gdb can search the
> > > debuginfo for matching source filenames.  This can result in unnecessary
> > > downloads.
> > >
> > > To improve this, have gdb instead download each debuginfo's .debug_line
> > > (and .debug_line_str if using DWARF5) when executing these commands.
> > > Download full debuginfo only when its .debug_line contains a matching
> > > filename.
> > >
> > > Since the combined size of .debug_line and .debug_line_str is only about
> > > 1% the size of the corresponding debuginfo, significant time can be saved
> > > by checking these sections before choosing to download an entire debuginfo.
> > >
> > > This patch also redirects stdout and stderr of the debuginfod server
> > > used by testsuite/gdb.debuginfod tests to a server_log standard output
> > > file.  While adding tests for this patch I ran into an issue where the
> > > test server would block when logging to stderr, presumably because the
> > > stderr buffer filled up and wasn't being read from.  Redirecting the
> > > log to a file fixes this and also makes the server log more accessible
> > > when debugging test failures.
> > > ---
> > >  gdb/cli-out.c                            |  11 +-
> > >  gdb/completer.c                          |  18 +-
> > >  gdb/dwarf2/line-header.c                 | 215 +++++++++++++++--------
> > >  gdb/dwarf2/line-header.h                 |  10 ++
> > >  gdb/dwarf2/read-gdb-index.c              |  60 +++++++
> > >  gdb/dwarf2/read.c                        | 208 ++++++++++++++++++++++
> > >  gdb/dwarf2/read.h                        |  37 ++++
> > >  gdb/mi/mi-out.c                          |   9 +-
> > >  gdb/testsuite/gdb.debuginfod/section.exp |  21 +++
> > >  gdb/ui-out.c                             |   3 +
> > >  gdb/ui-out.h                             |  20 +++
> > >  11 files changed, 531 insertions(+), 81 deletions(-)
> > >
> > > diff --git a/gdb/cli-out.c b/gdb/cli-out.c
> > > index c919622d418..a570a2d939d 100644
> > > --- a/gdb/cli-out.c
> > > +++ b/gdb/cli-out.c
> > > @@ -307,16 +307,23 @@ cli_ui_out::do_progress_notify (const std::string &msg,
> > >
> > >    if (info.state == progress_update::START)
> > >      {
> > > +      std::string prefix;
> > > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > > +       {
> > > +         prefix = "\n";
> > > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > > +       }
> > > +
> > >        if (stream->isatty ()
> > >           && current_ui->input_interactive_p ()
> > >           && chars_per_line >= MIN_CHARS_PER_LINE)
> > >         {
> > > -         gdb_printf (stream, "%s\n", msg.c_str ());
> > > +         gdb_printf (stream, "%s\n", (prefix + msg).c_str ());
> > >           info.state = progress_update::BAR;
> > >         }
> > >        else
> > >         {
> > > -         gdb_printf (stream, "%s...\n", msg.c_str ());
> > > +         gdb_printf (stream, "%s...\n", (prefix + msg).c_str ());
> > >           info.state = progress_update::WORKING;
> > >         }
> > >      }
> > > diff --git a/gdb/completer.c b/gdb/completer.c
> > > index 2abf3998345..9c299f3eb65 100644
> > > --- a/gdb/completer.c
> > > +++ b/gdb/completer.c
> > > @@ -1345,6 +1345,10 @@ complete_line_internal_1 (completion_tracker &tracker,
> > >      {
> > >        /* We've recognized a full command.  */
> > >
> > > +      /* Disable pagination since responding to the pagination prompt
> > > +        overwrites rl_line_buffer.  */
> > > +      scoped_restore pag_restore = make_scoped_restore (&pagination_enabled, false);
> > > +
> > >        if (p == tmp_command + point)
> > >         {
> > >           /* There is no non-whitespace in the line beyond the
> > > @@ -1444,7 +1448,8 @@ complete_line_internal_1 (completion_tracker &tracker,
> > >  }
> > >
> > >  /* Wrapper around complete_line_internal_1 to handle
> > > -   MAX_COMPLETIONS_REACHED_ERROR.  */
> > > +   MAX_COMPLETIONS_REACHED_ERROR and possible progress update
> > > +   interactions.  */
> > >
> > >  static void
> > >  complete_line_internal (completion_tracker &tracker,
> > > @@ -1452,6 +1457,11 @@ complete_line_internal (completion_tracker &tracker,
> > >                         const char *line_buffer, int point,
> > >                         complete_line_internal_reason reason)
> > >  {
> > > +  scoped_restore restore_prefix_state
> > > +    = make_scoped_restore
> > > +      (&cur_prefix_state,
> > > +       ui_out::progress_update::prefix_state::NEWLINE_NEEDED);
> > > +
> > >    try
> > >      {
> > >        complete_line_internal_1 (tracker, text, line_buffer, point, reason);
> > > @@ -1461,6 +1471,12 @@ complete_line_internal (completion_tracker &tracker,
> > >        if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
> > >         throw;
> > >      }
> > > +
> > > +  /* If progress update messages printed, then the text being completed
> > > +     needs to be printed again.  */
> > > +  if (cur_prefix_state
> > > +      == ui_out::progress_update::prefix_state::NEWLINE_PRINTED)
> > > +    rl_forced_update_display ();
> > >  }
> > >
> > >  /* See completer.h.  */
> > > diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
> > > index d072a91bac9..b9210d84f6b 100644
> > > --- a/gdb/dwarf2/line-header.c
> > > +++ b/gdb/dwarf2/line-header.c
> > > @@ -102,50 +102,57 @@ read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
> > >  {
> > >    LONGEST length = read_initial_length (abfd, buf, bytes_read);
> > >
> > > -  gdb_assert (cu_header->initial_length_size == 4
> > > -             || cu_header->initial_length_size == 8
> > > -             || cu_header->initial_length_size == 12);
> > > +  if (cu_header != nullptr)
> > > +    {
> > > +      gdb_assert (cu_header->initial_length_size == 4
> > > +                 || cu_header->initial_length_size == 8
> > > +                 || cu_header->initial_length_size == 12);
> > >
> > > -  if (cu_header->initial_length_size != *bytes_read)
> > > -    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > > +      if (cu_header->initial_length_size != *bytes_read)
> > > +       complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > > +    }
> > >
> > >    *offset_size = (*bytes_read == 4) ? 4 : 8;
> > >    return length;
> > >  }
> > >
> > > -/* Read directory or file name entry format, starting with byte of
> > > -   format count entries, ULEB128 pairs of entry formats, ULEB128 of
> > > -   entries count and the entries themselves in the described entry
> > > -   format.  */
> > > +
> > > +/* Like read_formatted_entries but the .debug_line and .debug_line_str
> > > +   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
> > > +   where these sections are read from separate files without necessarily
> > > +   having access to the entire debuginfo file they originate from.  */
> > >
> > >  static void
> > > -read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > -                       const gdb_byte **bufp, struct line_header *lh,
> > > -                       unsigned int offset_size,
> > > -                       void (*callback) (struct line_header *lh,
> > > -                                         const char *name,
> > > -                                         dir_index d_index,
> > > -                                         unsigned int mod_time,
> > > -                                         unsigned int length))
> > > +read_formatted_entries
> > > +  (bfd *parent_bfd, const gdb_byte **line_bufp,
> > > +   const gdb::array_view<const gdb_byte> line_str_data,
> > > +   struct line_header *lh,
> > > +   unsigned int offset_size,
> > > +   void (*callback) (struct line_header *lh,
> > > +                    const char *name,
> > > +                    dir_index d_index,
> > > +                    unsigned int mod_time,
> > > +                    unsigned int length))
> > >  {
> > >    gdb_byte format_count, formati;
> > >    ULONGEST data_count, datai;
> > > -  const gdb_byte *buf = *bufp;
> > > +  const gdb_byte *buf = *line_bufp;
> > > +  const gdb_byte *str_buf = line_str_data.data ();
> > >    const gdb_byte *format_header_data;
> > >    unsigned int bytes_read;
> > >
> > > -  format_count = read_1_byte (abfd, buf);
> > > +  format_count = read_1_byte (parent_bfd, buf);
> > >    buf += 1;
> > >    format_header_data = buf;
> > >    for (formati = 0; formati < format_count; formati++)
> > >      {
> > > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > >        buf += bytes_read;
> > > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > >        buf += bytes_read;
> > >      }
> > >
> > > -  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > +  data_count = read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > >    buf += bytes_read;
> > >    for (datai = 0; datai < data_count; datai++)
> > >      {
> > > @@ -154,10 +161,10 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > >
> > >        for (formati = 0; formati < format_count; formati++)
> > >         {
> > > -         ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
> > > +         ULONGEST content_type = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> > >           format += bytes_read;
> > >
> > > -         ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
> > > +         ULONGEST form  = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> > >           format += bytes_read;
> > >
> > >           gdb::optional<const char *> string;
> > > @@ -166,36 +173,48 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > >           switch (form)
> > >             {
> > >             case DW_FORM_string:
> > > -             string.emplace (read_direct_string (abfd, buf, &bytes_read));
> > > +             string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
> > >               buf += bytes_read;
> > >               break;
> > >
> > >             case DW_FORM_line_strp:
> > >               {
> > > -               const char *str
> > > -                 = per_objfile->read_line_string (buf, offset_size);
> > > +               if (line_str_data.empty ())
> > > +                 error (_("Dwarf Error: DW_FORM_line_strp used without " \
> > > +                          "required section"));
> > > +               if (line_str_data.size () <= offset_size)
> > > +                 error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
> > > +                          "of section .debug_line"));
> > > +
> > > +               ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
> > > +
> > > +               const char *str;
> > > +               if (str_buf[str_offset] == '\0')
> > > +                 str = nullptr;
> > > +               else
> > > +                 str = (const char *) (str_buf + str_offset);
> > >                 string.emplace (str);
> > >                 buf += offset_size;
> > > +               break;
> > >               }
> > > -             break;
> > >
> > >             case DW_FORM_data1:
> > > -             uint.emplace (read_1_byte (abfd, buf));
> > > +             uint.emplace (read_1_byte (parent_bfd, buf));
> > >               buf += 1;
> > >               break;
> > >
> > >             case DW_FORM_data2:
> > > -             uint.emplace (read_2_bytes (abfd, buf));
> > > +             uint.emplace (read_2_bytes (parent_bfd, buf));
> > >               buf += 2;
> > >               break;
> > >
> > >             case DW_FORM_data4:
> > > -             uint.emplace (read_4_bytes (abfd, buf));
> > > +             uint.emplace (read_4_bytes (parent_bfd, buf));
> > >               buf += 4;
> > >               break;
> > >
> > >             case DW_FORM_data8:
> > > -             uint.emplace (read_8_bytes (abfd, buf));
> > > +             uint.emplace (read_8_bytes (parent_bfd, buf));
> > >               buf += 8;
> > >               break;
> > >
> > > @@ -205,7 +224,7 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > >               break;
> > >
> > >             case DW_FORM_udata:
> > > -             uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
> > > +             uint.emplace (read_unsigned_leb128 (parent_bfd, buf, &bytes_read));
> > >               buf += bytes_read;
> > >               break;
> > >
> > > @@ -248,28 +267,30 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > >        callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
> > >      }
> > >
> > > -  *bufp = buf;
> > > +  *line_bufp = buf;
> > >  }
> > >
> > >  /* See line-header.h.  */
> > >
> > >  line_header_up
> > > -dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > -                          dwarf2_per_objfile *per_objfile,
> > > -                          struct dwarf2_section_info *section,
> > > -                          const struct comp_unit_head *cu_header,
> > > -                          const char *comp_dir)
> > > +dwarf_decode_line_header (bfd *parent_bfd,
> > > +                         gdb::array_view<const gdb_byte> line_data,
> > > +                         gdb::array_view<const gdb_byte> line_str_data,
> > > +                         const gdb_byte **debug_line_ptr,
> > > +                         bool is_dwz,
> > > +                         const struct comp_unit_head *cu_header,
> > > +                         const char *comp_dir)
> > >  {
> > > -  const gdb_byte *line_ptr;
> > > +  const gdb_byte *line_ptr, *buf;
> > >    unsigned int bytes_read, offset_size;
> > >    int i;
> > >    const char *cur_dir, *cur_file;
> > >
> > > -  bfd *abfd = section->get_bfd_owner ();
> > > +  buf = *debug_line_ptr;
> > >
> > >    /* Make sure that at least there's room for the total_length field.
> > >       That could be 12 bytes long, but we're just going to fudge that.  */
> > > -  if (to_underlying (sect_off) + 4 >= section->size)
> > > +  if (buf + 4 >= line_data.data () + line_data.size ())
> > >      {
> > >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> > >        return 0;
> > > @@ -277,62 +298,65 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > >
> > >    line_header_up lh (new line_header (comp_dir));
> > >
> > > -  lh->sect_off = sect_off;
> > > +  lh->sect_off = (sect_offset) (buf - line_data.data ());
> > >    lh->offset_in_dwz = is_dwz;
> > >
> > > -  line_ptr = section->buffer + to_underlying (sect_off);
> > > +  line_ptr = buf;
> > >
> > >    /* Read in the header.  */
> > >    LONGEST unit_length
> > > -    = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
> > > +    = read_checked_initial_length_and_offset (parent_bfd, buf, cu_header,
> > >                                               &bytes_read, &offset_size);
> > > -  line_ptr += bytes_read;
> > >
> > > -  const gdb_byte *start_here = line_ptr;
> > > +  line_ptr += bytes_read;
> > >
> > > -  if (line_ptr + unit_length > (section->buffer + section->size))
> > > +  if (line_ptr + unit_length > buf + line_data.size ())
> > >      {
> > >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> > >        return 0;
> > >      }
> > > +
> > > +  const gdb_byte *start_here = line_ptr;
> > > +
> > >    lh->statement_program_end = start_here + unit_length;
> > > -  lh->version = read_2_bytes (abfd, line_ptr);
> > > +  lh->version = read_2_bytes (parent_bfd, line_ptr);
> > >    line_ptr += 2;
> > >    if (lh->version > 5)
> > >      {
> > >        /* This is a version we don't understand.  The format could have
> > >          changed in ways we don't handle properly so just punt.  */
> > >        complaint (_("unsupported version in .debug_line section"));
> > > -      return NULL;
> > > +      return nullptr;
> > >      }
> > >    if (lh->version >= 5)
> > >      {
> > >        gdb_byte segment_selector_size;
> > >
> > >        /* Skip address size.  */
> > > -      read_1_byte (abfd, line_ptr);
> > > +      read_1_byte (parent_bfd, line_ptr);
> > >        line_ptr += 1;
> > >
> > > -      segment_selector_size = read_1_byte (abfd, line_ptr);
> > > +      segment_selector_size = read_1_byte (parent_bfd, line_ptr);
> > >        line_ptr += 1;
> > >        if (segment_selector_size != 0)
> > >         {
> > >           complaint (_("unsupported segment selector size %u "
> > >                        "in .debug_line section"),
> > >                      segment_selector_size);
> > > -         return NULL;
> > > +         return nullptr;
> > >         }
> > >      }
> > >
> > > -  LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
> > > +  LONGEST header_length = read_offset (parent_bfd, line_ptr, offset_size);
> > >    line_ptr += offset_size;
> > >    lh->statement_program_start = line_ptr + header_length;
> > > -  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
> > > +
> > > +  lh->minimum_instruction_length = read_1_byte (parent_bfd, line_ptr);
> > >    line_ptr += 1;
> > >
> > >    if (lh->version >= 4)
> > >      {
> > > -      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
> > > +      lh->maximum_ops_per_instruction = read_1_byte (parent_bfd, line_ptr);
> > >        line_ptr += 1;
> > >      }
> > >    else
> > > @@ -345,41 +369,47 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > >                    "in `.debug_line' section"));
> > >      }
> > >
> > > -  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
> > > +  lh->default_is_stmt = read_1_byte (parent_bfd, line_ptr);
> > >    line_ptr += 1;
> > > -  lh->line_base = read_1_signed_byte (abfd, line_ptr);
> > > +
> > > +  lh->line_base = read_1_signed_byte (parent_bfd, line_ptr);
> > >    line_ptr += 1;
> > > -  lh->line_range = read_1_byte (abfd, line_ptr);
> > > +
> > > +  lh->line_range = read_1_byte (parent_bfd, line_ptr);
> > >    line_ptr += 1;
> > > -  lh->opcode_base = read_1_byte (abfd, line_ptr);
> > > +
> > > +  lh->opcode_base = read_1_byte (parent_bfd, line_ptr);
> > >    line_ptr += 1;
> > > +
> > >    lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
> > >
> > >    lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
> > >    for (i = 1; i < lh->opcode_base; ++i)
> > >      {
> > > -      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
> > > +      lh->standard_opcode_lengths[i] = read_1_byte (parent_bfd, line_ptr);
> > >        line_ptr += 1;
> > >      }
> > >
> > >    if (lh->version >= 5)
> > >      {
> > >        /* Read directory table.  */
> > > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > > -                             offset_size,
> > > -                             [] (struct line_header *header, const char *name,
> > > -                                 dir_index d_index, unsigned int mod_time,
> > > -                                 unsigned int length)
> > > +      read_formatted_entries
> > > +       (parent_bfd, &line_ptr, line_str_data,
> > > +        lh.get (), offset_size,
> > > +        [] (struct line_header *header, const char *name,
> > > +            dir_index d_index, unsigned int mod_time,
> > > +            unsigned int length)
> > >         {
> > >           header->add_include_dir (name);
> > >         });
> > >
> > >        /* Read file name table.  */
> > > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > > -                             offset_size,
> > > -                             [] (struct line_header *header, const char *name,
> > > -                                 dir_index d_index, unsigned int mod_time,
> > > -                                 unsigned int length)
> > > +      read_formatted_entries
> > > +       (parent_bfd, &line_ptr, line_str_data,
> > > +        lh.get (), offset_size,
> > > +        [] (struct line_header *header, const char *name,
> > > +            dir_index d_index, unsigned int mod_time,
> > > +            unsigned int length)
> > >         {
> > >           header->add_file_name (name, d_index, mod_time, length);
> > >         });
> > > @@ -387,7 +417,7 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > >    else
> > >      {
> > >        /* Read directory table.  */
> > > -      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > > +      while ((cur_dir = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> > >         {
> > >           line_ptr += bytes_read;
> > >           lh->add_include_dir (cur_dir);
> > > @@ -395,17 +425,17 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > >        line_ptr += bytes_read;
> > >
> > >        /* Read file name table.  */
> > > -      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > > +      while ((cur_file = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> > >         {
> > >           unsigned int mod_time, length;
> > >           dir_index d_index;
> > >
> > >           line_ptr += bytes_read;
> > > -         d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > +         d_index = (dir_index) read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > >           line_ptr += bytes_read;
> > > -         mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > +         mod_time = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > >           line_ptr += bytes_read;
> > > -         length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > +         length = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > >           line_ptr += bytes_read;
> > >
> > >           lh->add_file_name (cur_file, d_index, mod_time, length);
> > > @@ -413,9 +443,40 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > >        line_ptr += bytes_read;
> > >      }
> > >
> > > -  if (line_ptr > (section->buffer + section->size))
> > > +  if (line_ptr > (buf + line_data.size ()))
> > >      complaint (_("line number info header doesn't "
> > >                  "fit in `.debug_line' section"));
> > >
> > > +  *debug_line_ptr += unit_length + offset_size;
> > >    return lh;
> > >  }
> > > +
> > > +line_header_up
> > > +dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > +                          dwarf2_per_objfile *per_objfile,
> > > +                          struct dwarf2_section_info *section,
> > > +                          const struct comp_unit_head *cu_header,
> > > +                          const char *comp_dir)
> > > +{
> > > +  struct objfile *objfile = per_objfile->objfile;
> > > +  struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
> > > +
> > > +  /* Read .debug_line.  */
> > > +  dwarf2_section_info *line_sec = &per_bfd->line;
> > > +  bfd_size_type line_size = line_sec->get_size (objfile);
> > > +
> > > +  gdb::array_view<const gdb_byte> line (line_sec->buffer, line_size);
> > > +
> > > +  /* Read .debug_line_str.  */
> > > +  dwarf2_section_info *line_str_sec = &per_bfd->line_str;
> > > +  bfd_size_type line_str_size = line_str_sec->get_size (objfile);
> > > +
> > > +  gdb::array_view<const gdb_byte> line_str (line_str_sec->buffer,
> > > +                                           line_str_size);
> > > +
> > > +  const gdb_byte *line_ptr = line.data () + to_underlying (sect_off);
> > > +
> > > +  return dwarf_decode_line_header
> > > +    (per_bfd->obfd, line, line_str, &line_ptr,
> > > +     is_dwz, cu_header, comp_dir);
> > > +}
> > > diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
> > > index 06d2eec573b..22db9f9aa78 100644
> > > --- a/gdb/dwarf2/line-header.h
> > > +++ b/gdb/dwarf2/line-header.h
> > > @@ -217,4 +217,14 @@ extern line_header_up dwarf_decode_line_header
> > >     struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
> > >     const char *comp_dir);
> > >
> > > +/* Like above but the .debug_line and .debug_line_str are stored in
> > > +   LINE_DATA and LINE_STR_DATA. *DEBUG_LINE_PTR should point to a
> > > +   statement program header within LINE_DATA.  */
> > > +
> > > +extern line_header_up dwarf_decode_line_header
> > > +  (bfd *parent_bfd, gdb::array_view<const gdb_byte> line_data,
> > > +   gdb::array_view<const gdb_byte> line_str_data,
> > > +   const gdb_byte **debug_line_ptr, bool is_dwz,
> > > +  const comp_unit_head *cu_header, const char *comp_dir);
> > > +
> > >  #endif /* DWARF2_LINE_HEADER_H */
> > > diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
> > > index da88a8b405c..c0e51357ce2 100644
> > > --- a/gdb/dwarf2/read-gdb-index.c
> > > +++ b/gdb/dwarf2/read-gdb-index.c
> > > @@ -131,6 +131,9 @@ struct mapped_gdb_index final : public mapped_index_base
> > >    }
> > >  };
> > >
> > > +struct mapped_debug_line;
> > > +typedef std::unique_ptr<mapped_debug_line> mapped_debug_line_up;
> > > +
> > >  struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > >  {
> > >    /* This dumps minimal information about the index.
> > > @@ -175,6 +178,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > >       domain_enum domain,
> > >       enum search_domain kind);
> > >
> > > + /* If OBJFILE's debuginfo download has been deferred, use a mapped_debug_line
> > > +    to generate filenames.
> > > +
> > > +    Otherwise call dwarf2_base_index_functions::map_symbol_filenames.  */
> > > +
> > > +  void map_symbol_filenames (struct objfile *objfile,
> > > +                            gdb::function_view<symbol_filename_ftype> fun,
> > > +                            bool need_fullname) override;
> > > +
> > >    /* Calls dwarf2_base_index_functions::expand_all_symtabs and downloads
> > >       debuginfo if necessary.  */
> > >    void expand_all_symtabs (struct objfile *objfile) override;
> > > @@ -182,6 +194,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > >    /* Calls dwarf2_base_index_functions::find_last_source_symtab and downloads
> > >       debuginfo if necessary.  */
> > >    struct symtab *find_last_source_symtab (struct objfile *objfile) override;
> > > +
> > > +  /* Filename information related to this .gdb_index.  */
> > > +  mapped_debug_line_up mdl;
> > > +
> > > +  /* Return true if any of the filenames in this .gdb_index's .debug_line
> > > +     mapping match FILE_MATCHER.  Initializes the mapping if necessary.  */
> > > +  bool filename_in_debug_line
> > > +    (objfile *objfile,
> > > +     gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > >  };
> > >
> > >  void
> > > @@ -217,6 +238,30 @@ dwarf2_gdb_index::find_last_source_symtab (struct objfile *objfile)
> > >      }
> > >  }
> > >
> > > +void
> > > +dwarf2_gdb_index::map_symbol_filenames
> > > +     (struct objfile *objfile,
> > > +      gdb::function_view<symbol_filename_ftype> fun,
> > > +      bool need_fullname)
> > > +{
> > > +  try
> > > +    {
> > > +      dwarf2_base_index_functions::map_symbol_filenames (objfile, fun,
> > > +                                                        need_fullname);
> > > +    }
> > > +  catch (const gdb_exception &e)
> > > +    {
> > > +      if ((objfile->flags & OBJF_DOWNLOAD_DEFERRED) == 0)
> > > +       exception_print (gdb_stderr, e);
> > > +      else
> > > +       {
> > > +         if (mdl == nullptr)
> > > +           mdl.reset (new mapped_debug_line (objfile));
> > > +         mdl->map_filenames (fun, need_fullname);
> > > +       }
> > > +    }
> > > +}
> > > +
> > >  /* This dumps minimal information about the index.
> > >     It is called via "mt print objfiles".
> > >     One use is to verify .gdb_index has been loaded by the
> > > @@ -590,6 +635,17 @@ dwarf2_gdb_index::do_expand_symtabs_matching
> > >    return result;
> > >  }
> > >
> > > +bool
> > > +dwarf2_gdb_index::filename_in_debug_line
> > > +  (objfile *objfile,
> > > +   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > +{
> > > +  if (mdl == nullptr)
> > > +    mdl.reset (new mapped_debug_line (objfile));
> > > +
> > > +  return mdl->contains_matching_filename (file_matcher);
> > > +}
> > > +
> > >  bool
> > >  dwarf2_gdb_index::expand_symtabs_matching
> > >      (struct objfile *objfile,
> > > @@ -618,6 +674,10 @@ dwarf2_gdb_index::expand_symtabs_matching
> > >           return false;
> > >         }
> > >
> > > +      if (file_matcher != nullptr
> > > +         && !filename_in_debug_line (objfile, file_matcher))
> > > +       return true;
> > > +
> > >        read_full_dwarf_from_debuginfod (objfile, this);
> > >        return true;
> > >      }
> > > diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> > > index 0c5689c63ef..876e3aedcf1 100644
> > > --- a/gdb/dwarf2/read.c
> > > +++ b/gdb/dwarf2/read.c
> > > @@ -81,6 +81,7 @@
> > >  #include "gdbsupport/gdb_optional.h"
> > >  #include "gdbsupport/underlying.h"
> > >  #include "gdbsupport/hash_enum.h"
> > > +#include "gdbsupport/scoped_mmap.h"
> > >  #include "filename-seen-cache.h"
> > >  #include "producer.h"
> > >  #include <fcntl.h>
> > > @@ -2135,6 +2136,213 @@ dw2_get_file_names (dwarf2_per_cu_data *this_cu,
> > >    return this_cu->file_names;
> > >  }
> > >
> > > +#if !HAVE_SYS_MMAN_H
> > > +
> > > +bool
> > > +mapped_debug_line::contains_matching_filename
> > > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > +{
> > > +  return false;
> > > +}
> > > +
> > > +gdb::array_view<const gdb_byte>
> > > +mapped_debug_line::read_debug_line_separate
> > > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > > +{
> > > +  return {};
> > > +}
> > > +
> > > +bool
> > > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > > +{
> > > +  return false;
> > > +}
> > > +
> > > +void
> > > +mapped_debug_line::map_filenames
> > > +  (gdb::function_view<symbol_filename_ftype> fun,
> > > +   bool need_fullname)
> > > +{
> > > +  return;
> > > +}
> > > +
> > > +#else /* !HAVE_SYS_MMAN_H */
> > > +
> > > +struct line_resource_mmap final : public index_cache_resource
> > > +{
> > > +  /* Try to mmap FILENAME.  Throw an exception on failure, including if the
> > > +     file doesn't exist. */
> > > +  line_resource_mmap (const char *filename)
> > > +    : mapping (mmap_file (filename))
> > > +  {}
> > > +
> > > +  scoped_mmap mapping;
> > > +};
> > > +
> > > +/* See read.h.  */
> > > +
> > > +bool
> > > +mapped_debug_line::contains_matching_filename
> > > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > +{
> > > +  for (line_header_up &lh : line_headers)
> > > +    for (file_entry &fe : lh->file_names ())
> > > +      {
> > > +       const char *filename = fe.name;
> > > +
> > > +       if (file_matcher (fe.name, false))
> > > +         return true;
> > > +
> > > +       bool basename_match = file_matcher (lbasename (fe.name), true);
> > > +
> > > +       if (!basenames_may_differ && !basename_match)
> > > +         continue;
> > > +
> > > +       /* DW_AT_comp_dir is not explicitly mentioned in the .debug_line
> > > +          until DWARF5.  Since we don't have access to the CU at this
> > > +          point we just check for a partial match on the filename.
> > > +          If there is a match, the full debuginfo will be downloaded
> > > +          ane the match will be re-evalute with DW_AT_comp_dir.  */
> > > +       if (lh->version < 5 && fe.d_index == 0)
> > > +         return basename_match;
> > > +
> > > +       const char *dirname = fe.include_dir (&*lh);
> > > +       std::string fullname;
> > > +
> > > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > > +         fullname = filename;
> > > +       else
> > > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > > +
> > > +       gdb::unique_xmalloc_ptr<char> rewritten
> > > +         = rewrite_source_path (fullname.c_str ());
> > > +       if (rewritten != nullptr)
> > > +         fullname = rewritten.release ();
> > > +
> > > +       if (file_matcher (fullname.c_str (), false))
> > > +         return true;
> > > +      }
> > > +
> > > +  return false;
> > > +}
> > > +
> > > +/* See read.h.  */
> > > +
> > > +void
> > > +mapped_debug_line::map_filenames
> > > +  (gdb::function_view<symbol_filename_ftype> fun,
> > > +   bool need_fullname)
> > > +{
> > > +  for (line_header_up &lh : line_headers)
> > > +    for (file_entry &fe : lh->file_names ())
> > > +      {
> > > +       const char *filename = fe.name;
> > > +
> > > +       if (!need_fullname)
> > > +         {
> > > +           fun (filename, nullptr);
> > > +           continue;
> > > +         }
> > > +
> > > +       const char *dirname = fe.include_dir (&*lh);
> > > +       std::string fullname;
> > > +
> > > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > > +         fullname = filename;
> > > +       else
> > > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > > +
> > > +       gdb::unique_xmalloc_ptr<char> rewritten
> > > +         = rewrite_source_path (fullname.c_str ());
> > > +       if (rewritten != nullptr)
> > > +         fullname = rewritten.release ();
> > > +
> > > +       fun (filename, fullname.c_str ());
> > > +      }
> > > +}
> > > +
> > > +/* See read.h.  */
> > > +
> > > +gdb::array_view<const gdb_byte>
> > > +mapped_debug_line::read_debug_line_separate
> > > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > > +{
> > > +  if (filename == nullptr)
> > > +    return {};
> > > +
> > > +  try
> > > +  {
> > > +    line_resource_mmap *mmap_resource
> > > +      = new line_resource_mmap (filename);
> > > +
> > > +    resource->reset (mmap_resource);
> > > +
> > > +    return gdb::array_view<const gdb_byte>
> > > +      ((const gdb_byte *) mmap_resource->mapping.get (),
> > > +       mmap_resource->mapping.size ());
> > > +  }
> > > +  catch (const gdb_exception &except)
> > > +  {
> > > +    exception_print (gdb_stderr, except);
> > > +  }
> > > +
> > > +  return {};
> > > +}
> > > +
> > > +/* See read.h.  */
> > > +
> > > +bool
> > > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > > +{
> > > +  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
> > > +  if (build_id == nullptr)
> > > +    return false;
> > > +
> > > +  gdb::unique_xmalloc_ptr<char> line_path;
> > > +  scoped_fd line_fd = debuginfod_section_query (build_id->data,
> > > +                                               build_id->size,
> > > +                                               bfd_get_filename
> > > +                                                 (objfile->obfd.get ()),
> > > +                                               ".debug_line",
> > > +                                               &line_path);
> > > +
> > > +  if (line_fd.get () < 0)
> > > +    return false;
> > > +
> > > +  gdb::unique_xmalloc_ptr<char> line_str_path;
> > > +  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
> > > +                                                   build_id->size,
> > > +                                                   bfd_get_filename
> > > +                                                     (objfile->obfd.get ()),
> > > +                                                   ".debug_line_str",
> > > +                                                   &line_str_path);
> > > +
> > > +  line_data = read_debug_line_separate (line_path.get (), &line_resource);
> > > +  line_str_data = read_debug_line_separate (line_str_path.get (),
> > > +                                           &line_str_resource);
> > > +
> > > +  const gdb_byte *line_ptr = line_data.data ();
> > > +
> > > +  while (line_ptr < line_data.data () + line_data.size ())
> > > +    {
> > > +      line_header_up lh
> > > +       = dwarf_decode_line_header (objfile->obfd.get (),
> > > +                                   line_data, line_str_data,
> > > +                                   &line_ptr, false,
> > > +                                   nullptr, nullptr);
> > > +      line_headers.emplace_back (lh.release ());
> > > +    }
> > > +
> > > +  return true;
> > > +}
> > > +#endif /* !HAVE_SYS_MMAN_H */
> > > +
> > > +mapped_debug_line::mapped_debug_line (objfile *objfile)
> > > +{
> > > +  if (!read_debug_line_from_debuginfod (objfile))
> > > +    line_headers.clear ();
> > > +}
> > > +
> > >  /* A helper for the "quick" functions which computes and caches the
> > >     real path for a given file name from the line table.  */
> > >
> > > diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
> > > index 6ed0be7203b..49fea22c092 100644
> > > --- a/gdb/dwarf2/read.h
> > > +++ b/gdb/dwarf2/read.h
> > > @@ -33,6 +33,7 @@
> > >  #include "gdbsupport/hash_enum.h"
> > >  #include "gdbsupport/function-view.h"
> > >  #include "gdbsupport/packed.h"
> > > +#include "dwarf2/line-header.h"
> > >
> > >  /* Hold 'maintenance (set|show) dwarf' commands.  */
> > >  extern struct cmd_list_element *set_dwarf_cmdlist;
> > > @@ -969,4 +970,40 @@ extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
> > >  extern void read_full_dwarf_from_debuginfod (struct objfile *,
> > >                                              dwarf2_base_index_functions *);
> > >
> > > +struct mapped_debug_line
> > > +{
> > > +  mapped_debug_line (objfile *objfile);
> > > +
> > > +  /* Return true if any of the mapped .debug_line's filenames match
> > > +     FILE_MATCHER.  */
> > > +
> > > +  bool contains_matching_filename
> > > +    (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > > +
> > > +  /* Call FUN with each filename in this mapped .debug_line.  Include
> > > +     each file's fullname if NEED_FULLNAME is true.  */
> > > +
> > > +  void map_filenames (gdb::function_view<symbol_filename_ftype> fun,
> > > +                     bool need_fullname);
> > > +
> > > +private:
> > > +  std::vector<line_header_up> line_headers;
> > > +
> > > +  gdb::array_view<const gdb_byte> line_data;
> > > +  gdb::array_view<const gdb_byte> line_str_data;
> > > +
> > > +  std::unique_ptr<index_cache_resource> line_resource;
> > > +  std::unique_ptr<index_cache_resource> line_str_resource;
> > > +
> > > +  /* Download the .debug_line and .debug_line_str associated with OBJFILE
> > > +     and populate line_headers.  */
> > > +
> > > +  bool read_debug_line_from_debuginfod (objfile *objfile);
> > > +
> > > +  /* Initialize line_data and line_str_data with the .debug_line and
> > > +    .debug_line_str downloaded read_debug_line_from_debuginfod.  */
> > > +
> > > +  gdb::array_view<const gdb_byte> read_debug_line_separate
> > > +    (char *filename, std::unique_ptr<index_cache_resource> *resource);
> > > +};
> > >  #endif /* DWARF2READ_H */
> > > diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
> > > index bbd21287b28..110864adac3 100644
> > > --- a/gdb/mi/mi-out.c
> > > +++ b/gdb/mi/mi-out.c
> > > @@ -278,7 +278,14 @@ mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
> > >
> > >    if (info.state == progress_update::START)
> > >      {
> > > -      gdb_printf ("%s...\n", msg.c_str ());
> > > +      std::string prefix;
> > > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > > +       {
> > > +         prefix = "\n";
> > > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > > +       }
> > > +
> > > +      gdb_printf ("%s...\n", (prefix + msg).c_str ());
> > >        info.state = progress_update::WORKING;
> > >      }
> > >  }
> > > diff --git a/gdb/testsuite/gdb.debuginfod/section.exp b/gdb/testsuite/gdb.debuginfod/section.exp
> > > index ff57c6e32b7..b5c6929fcf7 100644
> > > --- a/gdb/testsuite/gdb.debuginfod/section.exp
> > > +++ b/gdb/testsuite/gdb.debuginfod/section.exp
> > > @@ -171,6 +171,27 @@ proc_with_prefix local_url { } {
> > >      # during backtrace.
> > >      set res ".*debug info for $lib_sl1\.\.\.\r\n\#1.*"
> > >      gdb_test "bt" $res "break backtrace"
> > > +
> > > +    clean_restart_with_prompt $sectexec "" "line 1"
> > > +
> > > +    # List source file using .debug_line download.
> > > +    set res ".*\.debug_line.*$lib_sl1.*21.*extern void libsection2_test.*"
> > > +    gdb_test "list $libsrc1:21" $res "line 1 list"
> > > +
> > > +    clean_restart_with_prompt $sectexec "" "line 2"
> > > +
> > > +    # Set breakpoint using .debug_line download.
> > > +    set res ".*section \.debug_line for $lib_sl1.*Breakpoint 2 at.*$libsrc1.*"
> > > +    gdb_test "br $libsrc1:37" $res "line 2 br"
> > > +
> > > +    # Continue to breakpoint.
> > > +    set res "Breakpoint 2, libsection1_test.*\"Cancelling thread\\\\n\".*"
> > > +    gdb_test "c" $res "line 2 continue"
> > > +
> > > +    # Check that download progress message is correctly formatted
> > > +    # when printing threads.
> > > +    set res ".*separate debug info for $lib_sl2\.\.\.\r\n.* 2    Thread.*"
> > > +    gdb_test "info thr" $res "line thread"
> > >  }
> > >
> > >  # Create CACHE and DB directories ready for debuginfod to use.
> > > diff --git a/gdb/ui-out.c b/gdb/ui-out.c
> > > index 9f643b1ce95..fde46bfbe94 100644
> > > --- a/gdb/ui-out.c
> > > +++ b/gdb/ui-out.c
> > > @@ -32,6 +32,9 @@
> > >  #include <memory>
> > >  #include <string>
> > >
> > > +/* Current state of newline prefixing for progress update messages.  */
> > > +prefix_state_t cur_prefix_state = prefix_state_t::NEWLINE_OFF;
> > > +
> > >  namespace {
> > >
> > >  /* A header of a ui_out_table.  */
> > > diff --git a/gdb/ui-out.h b/gdb/ui-out.h
> > > index 70a7145741f..7de8796aee0 100644
> > > --- a/gdb/ui-out.h
> > > +++ b/gdb/ui-out.h
> > > @@ -296,6 +296,21 @@ class ui_out
> > >        BAR
> > >      };
> > >
> > > +    /* Used to communicate the status of a newline prefix for the next progress
> > > +       update message.  */
> > > +    enum prefix_state
> > > +    {
> > > +      /* Do not modify the next progress update message.  */
> > > +      NEWLINE_OFF,
> > > +
> > > +      /* The next progress update message should include a newline prefix.  */
> > > +      NEWLINE_NEEDED,
> > > +
> > > +      /* A newline prefix was included in a debuginfod progress update
> > > +        message.  */
> > > +      NEWLINE_PRINTED
> > > +    };
> > > +
> > >      /* SHOULD_PRINT indicates whether something should be printed for a tty.  */
> > >      progress_update ()
> > >      {
> > > @@ -393,6 +408,11 @@ class ui_out
> > >    ui_out_level *current_level () const;
> > >  };
> > >
> > > +typedef ui_out::progress_update::prefix_state prefix_state_t;
> > > +
> > > +/* Current state of the newline prefix.  */
> > > +extern prefix_state_t cur_prefix_state;
> > > +
> > >  /* Start a new tuple or list on construction, and end it on
> > >     destruction.  Normally this is used via the typedefs
> > >     ui_out_emit_tuple and ui_out_emit_list.  */
> > > --
> > > 2.41.0
> > >
  
Aaron Merey Dec. 12, 2023, 3:08 p.m. UTC | #4
Ping

Thanks,
Aaron

On Thu, Nov 30, 2023 at 11:30 AM Aaron Merey <amerey@redhat.com> wrote:
>
> Ping
>
> Thanks,
> Aaron
>
> On Mon, Nov 20, 2023 at 1:40 PM Aaron Merey <amerey@redhat.com> wrote:
> >
> > Ping
> >
> > Thanks,
> > Aaron
> >
> > On Sun, Nov 12, 2023 at 3:21 PM Aaron Merey <amerey@redhat.com> wrote:
> > >
> > > Ping
> > >
> > > Thanks,
> > > Aaron
> > >
> > > On Fri, Oct 27, 2023 at 8:20 PM Aaron Merey <amerey@redhat.com> wrote:
> > > >
> > > > v4: https://sourceware.org/pipermail/gdb-patches/2023-August/201651.html
> > > >
> > > > v5 adds prefix_state to progress_update objects to track when
> > > > a newline prefix needs to be added to a download progress message.
> > > > This is used to correctly format progress messages that occur during
> > > > command autocompletion.
> > > >
> > > > Commit message:
> > > >
> > > > ELF/DWARF section downloading allows gdb to download .gdb_index files in
> > > > order to defer full debuginfo downloads.  However .gdb_index does not
> > > > contain any information regarding source filenames.  When a gdb command
> > > > includes a filename argument (ex. 'break main.c:50'), this results in
> > > > the mass downloading of all deferred debuginfo so that gdb can search the
> > > > debuginfo for matching source filenames.  This can result in unnecessary
> > > > downloads.
> > > >
> > > > To improve this, have gdb instead download each debuginfo's .debug_line
> > > > (and .debug_line_str if using DWARF5) when executing these commands.
> > > > Download full debuginfo only when its .debug_line contains a matching
> > > > filename.
> > > >
> > > > Since the combined size of .debug_line and .debug_line_str is only about
> > > > 1% the size of the corresponding debuginfo, significant time can be saved
> > > > by checking these sections before choosing to download an entire debuginfo.
> > > >
> > > > This patch also redirects stdout and stderr of the debuginfod server
> > > > used by testsuite/gdb.debuginfod tests to a server_log standard output
> > > > file.  While adding tests for this patch I ran into an issue where the
> > > > test server would block when logging to stderr, presumably because the
> > > > stderr buffer filled up and wasn't being read from.  Redirecting the
> > > > log to a file fixes this and also makes the server log more accessible
> > > > when debugging test failures.
> > > > ---
> > > >  gdb/cli-out.c                            |  11 +-
> > > >  gdb/completer.c                          |  18 +-
> > > >  gdb/dwarf2/line-header.c                 | 215 +++++++++++++++--------
> > > >  gdb/dwarf2/line-header.h                 |  10 ++
> > > >  gdb/dwarf2/read-gdb-index.c              |  60 +++++++
> > > >  gdb/dwarf2/read.c                        | 208 ++++++++++++++++++++++
> > > >  gdb/dwarf2/read.h                        |  37 ++++
> > > >  gdb/mi/mi-out.c                          |   9 +-
> > > >  gdb/testsuite/gdb.debuginfod/section.exp |  21 +++
> > > >  gdb/ui-out.c                             |   3 +
> > > >  gdb/ui-out.h                             |  20 +++
> > > >  11 files changed, 531 insertions(+), 81 deletions(-)
> > > >
> > > > diff --git a/gdb/cli-out.c b/gdb/cli-out.c
> > > > index c919622d418..a570a2d939d 100644
> > > > --- a/gdb/cli-out.c
> > > > +++ b/gdb/cli-out.c
> > > > @@ -307,16 +307,23 @@ cli_ui_out::do_progress_notify (const std::string &msg,
> > > >
> > > >    if (info.state == progress_update::START)
> > > >      {
> > > > +      std::string prefix;
> > > > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > > > +       {
> > > > +         prefix = "\n";
> > > > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > > > +       }
> > > > +
> > > >        if (stream->isatty ()
> > > >           && current_ui->input_interactive_p ()
> > > >           && chars_per_line >= MIN_CHARS_PER_LINE)
> > > >         {
> > > > -         gdb_printf (stream, "%s\n", msg.c_str ());
> > > > +         gdb_printf (stream, "%s\n", (prefix + msg).c_str ());
> > > >           info.state = progress_update::BAR;
> > > >         }
> > > >        else
> > > >         {
> > > > -         gdb_printf (stream, "%s...\n", msg.c_str ());
> > > > +         gdb_printf (stream, "%s...\n", (prefix + msg).c_str ());
> > > >           info.state = progress_update::WORKING;
> > > >         }
> > > >      }
> > > > diff --git a/gdb/completer.c b/gdb/completer.c
> > > > index 2abf3998345..9c299f3eb65 100644
> > > > --- a/gdb/completer.c
> > > > +++ b/gdb/completer.c
> > > > @@ -1345,6 +1345,10 @@ complete_line_internal_1 (completion_tracker &tracker,
> > > >      {
> > > >        /* We've recognized a full command.  */
> > > >
> > > > +      /* Disable pagination since responding to the pagination prompt
> > > > +        overwrites rl_line_buffer.  */
> > > > +      scoped_restore pag_restore = make_scoped_restore (&pagination_enabled, false);
> > > > +
> > > >        if (p == tmp_command + point)
> > > >         {
> > > >           /* There is no non-whitespace in the line beyond the
> > > > @@ -1444,7 +1448,8 @@ complete_line_internal_1 (completion_tracker &tracker,
> > > >  }
> > > >
> > > >  /* Wrapper around complete_line_internal_1 to handle
> > > > -   MAX_COMPLETIONS_REACHED_ERROR.  */
> > > > +   MAX_COMPLETIONS_REACHED_ERROR and possible progress update
> > > > +   interactions.  */
> > > >
> > > >  static void
> > > >  complete_line_internal (completion_tracker &tracker,
> > > > @@ -1452,6 +1457,11 @@ complete_line_internal (completion_tracker &tracker,
> > > >                         const char *line_buffer, int point,
> > > >                         complete_line_internal_reason reason)
> > > >  {
> > > > +  scoped_restore restore_prefix_state
> > > > +    = make_scoped_restore
> > > > +      (&cur_prefix_state,
> > > > +       ui_out::progress_update::prefix_state::NEWLINE_NEEDED);
> > > > +
> > > >    try
> > > >      {
> > > >        complete_line_internal_1 (tracker, text, line_buffer, point, reason);
> > > > @@ -1461,6 +1471,12 @@ complete_line_internal (completion_tracker &tracker,
> > > >        if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
> > > >         throw;
> > > >      }
> > > > +
> > > > +  /* If progress update messages printed, then the text being completed
> > > > +     needs to be printed again.  */
> > > > +  if (cur_prefix_state
> > > > +      == ui_out::progress_update::prefix_state::NEWLINE_PRINTED)
> > > > +    rl_forced_update_display ();
> > > >  }
> > > >
> > > >  /* See completer.h.  */
> > > > diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
> > > > index d072a91bac9..b9210d84f6b 100644
> > > > --- a/gdb/dwarf2/line-header.c
> > > > +++ b/gdb/dwarf2/line-header.c
> > > > @@ -102,50 +102,57 @@ read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
> > > >  {
> > > >    LONGEST length = read_initial_length (abfd, buf, bytes_read);
> > > >
> > > > -  gdb_assert (cu_header->initial_length_size == 4
> > > > -             || cu_header->initial_length_size == 8
> > > > -             || cu_header->initial_length_size == 12);
> > > > +  if (cu_header != nullptr)
> > > > +    {
> > > > +      gdb_assert (cu_header->initial_length_size == 4
> > > > +                 || cu_header->initial_length_size == 8
> > > > +                 || cu_header->initial_length_size == 12);
> > > >
> > > > -  if (cu_header->initial_length_size != *bytes_read)
> > > > -    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > > > +      if (cu_header->initial_length_size != *bytes_read)
> > > > +       complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > > > +    }
> > > >
> > > >    *offset_size = (*bytes_read == 4) ? 4 : 8;
> > > >    return length;
> > > >  }
> > > >
> > > > -/* Read directory or file name entry format, starting with byte of
> > > > -   format count entries, ULEB128 pairs of entry formats, ULEB128 of
> > > > -   entries count and the entries themselves in the described entry
> > > > -   format.  */
> > > > +
> > > > +/* Like read_formatted_entries but the .debug_line and .debug_line_str
> > > > +   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
> > > > +   where these sections are read from separate files without necessarily
> > > > +   having access to the entire debuginfo file they originate from.  */
> > > >
> > > >  static void
> > > > -read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > > -                       const gdb_byte **bufp, struct line_header *lh,
> > > > -                       unsigned int offset_size,
> > > > -                       void (*callback) (struct line_header *lh,
> > > > -                                         const char *name,
> > > > -                                         dir_index d_index,
> > > > -                                         unsigned int mod_time,
> > > > -                                         unsigned int length))
> > > > +read_formatted_entries
> > > > +  (bfd *parent_bfd, const gdb_byte **line_bufp,
> > > > +   const gdb::array_view<const gdb_byte> line_str_data,
> > > > +   struct line_header *lh,
> > > > +   unsigned int offset_size,
> > > > +   void (*callback) (struct line_header *lh,
> > > > +                    const char *name,
> > > > +                    dir_index d_index,
> > > > +                    unsigned int mod_time,
> > > > +                    unsigned int length))
> > > >  {
> > > >    gdb_byte format_count, formati;
> > > >    ULONGEST data_count, datai;
> > > > -  const gdb_byte *buf = *bufp;
> > > > +  const gdb_byte *buf = *line_bufp;
> > > > +  const gdb_byte *str_buf = line_str_data.data ();
> > > >    const gdb_byte *format_header_data;
> > > >    unsigned int bytes_read;
> > > >
> > > > -  format_count = read_1_byte (abfd, buf);
> > > > +  format_count = read_1_byte (parent_bfd, buf);
> > > >    buf += 1;
> > > >    format_header_data = buf;
> > > >    for (formati = 0; formati < format_count; formati++)
> > > >      {
> > > > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > > >        buf += bytes_read;
> > > > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > > >        buf += bytes_read;
> > > >      }
> > > >
> > > > -  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > > +  data_count = read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > > >    buf += bytes_read;
> > > >    for (datai = 0; datai < data_count; datai++)
> > > >      {
> > > > @@ -154,10 +161,10 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > >
> > > >        for (formati = 0; formati < format_count; formati++)
> > > >         {
> > > > -         ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
> > > > +         ULONGEST content_type = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> > > >           format += bytes_read;
> > > >
> > > > -         ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
> > > > +         ULONGEST form  = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> > > >           format += bytes_read;
> > > >
> > > >           gdb::optional<const char *> string;
> > > > @@ -166,36 +173,48 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > >           switch (form)
> > > >             {
> > > >             case DW_FORM_string:
> > > > -             string.emplace (read_direct_string (abfd, buf, &bytes_read));
> > > > +             string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
> > > >               buf += bytes_read;
> > > >               break;
> > > >
> > > >             case DW_FORM_line_strp:
> > > >               {
> > > > -               const char *str
> > > > -                 = per_objfile->read_line_string (buf, offset_size);
> > > > +               if (line_str_data.empty ())
> > > > +                 error (_("Dwarf Error: DW_FORM_line_strp used without " \
> > > > +                          "required section"));
> > > > +               if (line_str_data.size () <= offset_size)
> > > > +                 error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
> > > > +                          "of section .debug_line"));
> > > > +
> > > > +               ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
> > > > +
> > > > +               const char *str;
> > > > +               if (str_buf[str_offset] == '\0')
> > > > +                 str = nullptr;
> > > > +               else
> > > > +                 str = (const char *) (str_buf + str_offset);
> > > >                 string.emplace (str);
> > > >                 buf += offset_size;
> > > > +               break;
> > > >               }
> > > > -             break;
> > > >
> > > >             case DW_FORM_data1:
> > > > -             uint.emplace (read_1_byte (abfd, buf));
> > > > +             uint.emplace (read_1_byte (parent_bfd, buf));
> > > >               buf += 1;
> > > >               break;
> > > >
> > > >             case DW_FORM_data2:
> > > > -             uint.emplace (read_2_bytes (abfd, buf));
> > > > +             uint.emplace (read_2_bytes (parent_bfd, buf));
> > > >               buf += 2;
> > > >               break;
> > > >
> > > >             case DW_FORM_data4:
> > > > -             uint.emplace (read_4_bytes (abfd, buf));
> > > > +             uint.emplace (read_4_bytes (parent_bfd, buf));
> > > >               buf += 4;
> > > >               break;
> > > >
> > > >             case DW_FORM_data8:
> > > > -             uint.emplace (read_8_bytes (abfd, buf));
> > > > +             uint.emplace (read_8_bytes (parent_bfd, buf));
> > > >               buf += 8;
> > > >               break;
> > > >
> > > > @@ -205,7 +224,7 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > >               break;
> > > >
> > > >             case DW_FORM_udata:
> > > > -             uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
> > > > +             uint.emplace (read_unsigned_leb128 (parent_bfd, buf, &bytes_read));
> > > >               buf += bytes_read;
> > > >               break;
> > > >
> > > > @@ -248,28 +267,30 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > >        callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
> > > >      }
> > > >
> > > > -  *bufp = buf;
> > > > +  *line_bufp = buf;
> > > >  }
> > > >
> > > >  /* See line-header.h.  */
> > > >
> > > >  line_header_up
> > > > -dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > -                          dwarf2_per_objfile *per_objfile,
> > > > -                          struct dwarf2_section_info *section,
> > > > -                          const struct comp_unit_head *cu_header,
> > > > -                          const char *comp_dir)
> > > > +dwarf_decode_line_header (bfd *parent_bfd,
> > > > +                         gdb::array_view<const gdb_byte> line_data,
> > > > +                         gdb::array_view<const gdb_byte> line_str_data,
> > > > +                         const gdb_byte **debug_line_ptr,
> > > > +                         bool is_dwz,
> > > > +                         const struct comp_unit_head *cu_header,
> > > > +                         const char *comp_dir)
> > > >  {
> > > > -  const gdb_byte *line_ptr;
> > > > +  const gdb_byte *line_ptr, *buf;
> > > >    unsigned int bytes_read, offset_size;
> > > >    int i;
> > > >    const char *cur_dir, *cur_file;
> > > >
> > > > -  bfd *abfd = section->get_bfd_owner ();
> > > > +  buf = *debug_line_ptr;
> > > >
> > > >    /* Make sure that at least there's room for the total_length field.
> > > >       That could be 12 bytes long, but we're just going to fudge that.  */
> > > > -  if (to_underlying (sect_off) + 4 >= section->size)
> > > > +  if (buf + 4 >= line_data.data () + line_data.size ())
> > > >      {
> > > >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> > > >        return 0;
> > > > @@ -277,62 +298,65 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > >
> > > >    line_header_up lh (new line_header (comp_dir));
> > > >
> > > > -  lh->sect_off = sect_off;
> > > > +  lh->sect_off = (sect_offset) (buf - line_data.data ());
> > > >    lh->offset_in_dwz = is_dwz;
> > > >
> > > > -  line_ptr = section->buffer + to_underlying (sect_off);
> > > > +  line_ptr = buf;
> > > >
> > > >    /* Read in the header.  */
> > > >    LONGEST unit_length
> > > > -    = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
> > > > +    = read_checked_initial_length_and_offset (parent_bfd, buf, cu_header,
> > > >                                               &bytes_read, &offset_size);
> > > > -  line_ptr += bytes_read;
> > > >
> > > > -  const gdb_byte *start_here = line_ptr;
> > > > +  line_ptr += bytes_read;
> > > >
> > > > -  if (line_ptr + unit_length > (section->buffer + section->size))
> > > > +  if (line_ptr + unit_length > buf + line_data.size ())
> > > >      {
> > > >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> > > >        return 0;
> > > >      }
> > > > +
> > > > +  const gdb_byte *start_here = line_ptr;
> > > > +
> > > >    lh->statement_program_end = start_here + unit_length;
> > > > -  lh->version = read_2_bytes (abfd, line_ptr);
> > > > +  lh->version = read_2_bytes (parent_bfd, line_ptr);
> > > >    line_ptr += 2;
> > > >    if (lh->version > 5)
> > > >      {
> > > >        /* This is a version we don't understand.  The format could have
> > > >          changed in ways we don't handle properly so just punt.  */
> > > >        complaint (_("unsupported version in .debug_line section"));
> > > > -      return NULL;
> > > > +      return nullptr;
> > > >      }
> > > >    if (lh->version >= 5)
> > > >      {
> > > >        gdb_byte segment_selector_size;
> > > >
> > > >        /* Skip address size.  */
> > > > -      read_1_byte (abfd, line_ptr);
> > > > +      read_1_byte (parent_bfd, line_ptr);
> > > >        line_ptr += 1;
> > > >
> > > > -      segment_selector_size = read_1_byte (abfd, line_ptr);
> > > > +      segment_selector_size = read_1_byte (parent_bfd, line_ptr);
> > > >        line_ptr += 1;
> > > >        if (segment_selector_size != 0)
> > > >         {
> > > >           complaint (_("unsupported segment selector size %u "
> > > >                        "in .debug_line section"),
> > > >                      segment_selector_size);
> > > > -         return NULL;
> > > > +         return nullptr;
> > > >         }
> > > >      }
> > > >
> > > > -  LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
> > > > +  LONGEST header_length = read_offset (parent_bfd, line_ptr, offset_size);
> > > >    line_ptr += offset_size;
> > > >    lh->statement_program_start = line_ptr + header_length;
> > > > -  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
> > > > +
> > > > +  lh->minimum_instruction_length = read_1_byte (parent_bfd, line_ptr);
> > > >    line_ptr += 1;
> > > >
> > > >    if (lh->version >= 4)
> > > >      {
> > > > -      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
> > > > +      lh->maximum_ops_per_instruction = read_1_byte (parent_bfd, line_ptr);
> > > >        line_ptr += 1;
> > > >      }
> > > >    else
> > > > @@ -345,41 +369,47 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > >                    "in `.debug_line' section"));
> > > >      }
> > > >
> > > > -  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
> > > > +  lh->default_is_stmt = read_1_byte (parent_bfd, line_ptr);
> > > >    line_ptr += 1;
> > > > -  lh->line_base = read_1_signed_byte (abfd, line_ptr);
> > > > +
> > > > +  lh->line_base = read_1_signed_byte (parent_bfd, line_ptr);
> > > >    line_ptr += 1;
> > > > -  lh->line_range = read_1_byte (abfd, line_ptr);
> > > > +
> > > > +  lh->line_range = read_1_byte (parent_bfd, line_ptr);
> > > >    line_ptr += 1;
> > > > -  lh->opcode_base = read_1_byte (abfd, line_ptr);
> > > > +
> > > > +  lh->opcode_base = read_1_byte (parent_bfd, line_ptr);
> > > >    line_ptr += 1;
> > > > +
> > > >    lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
> > > >
> > > >    lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
> > > >    for (i = 1; i < lh->opcode_base; ++i)
> > > >      {
> > > > -      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
> > > > +      lh->standard_opcode_lengths[i] = read_1_byte (parent_bfd, line_ptr);
> > > >        line_ptr += 1;
> > > >      }
> > > >
> > > >    if (lh->version >= 5)
> > > >      {
> > > >        /* Read directory table.  */
> > > > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > > > -                             offset_size,
> > > > -                             [] (struct line_header *header, const char *name,
> > > > -                                 dir_index d_index, unsigned int mod_time,
> > > > -                                 unsigned int length)
> > > > +      read_formatted_entries
> > > > +       (parent_bfd, &line_ptr, line_str_data,
> > > > +        lh.get (), offset_size,
> > > > +        [] (struct line_header *header, const char *name,
> > > > +            dir_index d_index, unsigned int mod_time,
> > > > +            unsigned int length)
> > > >         {
> > > >           header->add_include_dir (name);
> > > >         });
> > > >
> > > >        /* Read file name table.  */
> > > > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > > > -                             offset_size,
> > > > -                             [] (struct line_header *header, const char *name,
> > > > -                                 dir_index d_index, unsigned int mod_time,
> > > > -                                 unsigned int length)
> > > > +      read_formatted_entries
> > > > +       (parent_bfd, &line_ptr, line_str_data,
> > > > +        lh.get (), offset_size,
> > > > +        [] (struct line_header *header, const char *name,
> > > > +            dir_index d_index, unsigned int mod_time,
> > > > +            unsigned int length)
> > > >         {
> > > >           header->add_file_name (name, d_index, mod_time, length);
> > > >         });
> > > > @@ -387,7 +417,7 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > >    else
> > > >      {
> > > >        /* Read directory table.  */
> > > > -      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > > > +      while ((cur_dir = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> > > >         {
> > > >           line_ptr += bytes_read;
> > > >           lh->add_include_dir (cur_dir);
> > > > @@ -395,17 +425,17 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > >        line_ptr += bytes_read;
> > > >
> > > >        /* Read file name table.  */
> > > > -      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > > > +      while ((cur_file = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> > > >         {
> > > >           unsigned int mod_time, length;
> > > >           dir_index d_index;
> > > >
> > > >           line_ptr += bytes_read;
> > > > -         d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > > +         d_index = (dir_index) read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > > >           line_ptr += bytes_read;
> > > > -         mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > > +         mod_time = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > > >           line_ptr += bytes_read;
> > > > -         length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > > +         length = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > > >           line_ptr += bytes_read;
> > > >
> > > >           lh->add_file_name (cur_file, d_index, mod_time, length);
> > > > @@ -413,9 +443,40 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > >        line_ptr += bytes_read;
> > > >      }
> > > >
> > > > -  if (line_ptr > (section->buffer + section->size))
> > > > +  if (line_ptr > (buf + line_data.size ()))
> > > >      complaint (_("line number info header doesn't "
> > > >                  "fit in `.debug_line' section"));
> > > >
> > > > +  *debug_line_ptr += unit_length + offset_size;
> > > >    return lh;
> > > >  }
> > > > +
> > > > +line_header_up
> > > > +dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > +                          dwarf2_per_objfile *per_objfile,
> > > > +                          struct dwarf2_section_info *section,
> > > > +                          const struct comp_unit_head *cu_header,
> > > > +                          const char *comp_dir)
> > > > +{
> > > > +  struct objfile *objfile = per_objfile->objfile;
> > > > +  struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
> > > > +
> > > > +  /* Read .debug_line.  */
> > > > +  dwarf2_section_info *line_sec = &per_bfd->line;
> > > > +  bfd_size_type line_size = line_sec->get_size (objfile);
> > > > +
> > > > +  gdb::array_view<const gdb_byte> line (line_sec->buffer, line_size);
> > > > +
> > > > +  /* Read .debug_line_str.  */
> > > > +  dwarf2_section_info *line_str_sec = &per_bfd->line_str;
> > > > +  bfd_size_type line_str_size = line_str_sec->get_size (objfile);
> > > > +
> > > > +  gdb::array_view<const gdb_byte> line_str (line_str_sec->buffer,
> > > > +                                           line_str_size);
> > > > +
> > > > +  const gdb_byte *line_ptr = line.data () + to_underlying (sect_off);
> > > > +
> > > > +  return dwarf_decode_line_header
> > > > +    (per_bfd->obfd, line, line_str, &line_ptr,
> > > > +     is_dwz, cu_header, comp_dir);
> > > > +}
> > > > diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
> > > > index 06d2eec573b..22db9f9aa78 100644
> > > > --- a/gdb/dwarf2/line-header.h
> > > > +++ b/gdb/dwarf2/line-header.h
> > > > @@ -217,4 +217,14 @@ extern line_header_up dwarf_decode_line_header
> > > >     struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
> > > >     const char *comp_dir);
> > > >
> > > > +/* Like above but the .debug_line and .debug_line_str are stored in
> > > > +   LINE_DATA and LINE_STR_DATA. *DEBUG_LINE_PTR should point to a
> > > > +   statement program header within LINE_DATA.  */
> > > > +
> > > > +extern line_header_up dwarf_decode_line_header
> > > > +  (bfd *parent_bfd, gdb::array_view<const gdb_byte> line_data,
> > > > +   gdb::array_view<const gdb_byte> line_str_data,
> > > > +   const gdb_byte **debug_line_ptr, bool is_dwz,
> > > > +  const comp_unit_head *cu_header, const char *comp_dir);
> > > > +
> > > >  #endif /* DWARF2_LINE_HEADER_H */
> > > > diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
> > > > index da88a8b405c..c0e51357ce2 100644
> > > > --- a/gdb/dwarf2/read-gdb-index.c
> > > > +++ b/gdb/dwarf2/read-gdb-index.c
> > > > @@ -131,6 +131,9 @@ struct mapped_gdb_index final : public mapped_index_base
> > > >    }
> > > >  };
> > > >
> > > > +struct mapped_debug_line;
> > > > +typedef std::unique_ptr<mapped_debug_line> mapped_debug_line_up;
> > > > +
> > > >  struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > > >  {
> > > >    /* This dumps minimal information about the index.
> > > > @@ -175,6 +178,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > > >       domain_enum domain,
> > > >       enum search_domain kind);
> > > >
> > > > + /* If OBJFILE's debuginfo download has been deferred, use a mapped_debug_line
> > > > +    to generate filenames.
> > > > +
> > > > +    Otherwise call dwarf2_base_index_functions::map_symbol_filenames.  */
> > > > +
> > > > +  void map_symbol_filenames (struct objfile *objfile,
> > > > +                            gdb::function_view<symbol_filename_ftype> fun,
> > > > +                            bool need_fullname) override;
> > > > +
> > > >    /* Calls dwarf2_base_index_functions::expand_all_symtabs and downloads
> > > >       debuginfo if necessary.  */
> > > >    void expand_all_symtabs (struct objfile *objfile) override;
> > > > @@ -182,6 +194,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > > >    /* Calls dwarf2_base_index_functions::find_last_source_symtab and downloads
> > > >       debuginfo if necessary.  */
> > > >    struct symtab *find_last_source_symtab (struct objfile *objfile) override;
> > > > +
> > > > +  /* Filename information related to this .gdb_index.  */
> > > > +  mapped_debug_line_up mdl;
> > > > +
> > > > +  /* Return true if any of the filenames in this .gdb_index's .debug_line
> > > > +     mapping match FILE_MATCHER.  Initializes the mapping if necessary.  */
> > > > +  bool filename_in_debug_line
> > > > +    (objfile *objfile,
> > > > +     gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > > >  };
> > > >
> > > >  void
> > > > @@ -217,6 +238,30 @@ dwarf2_gdb_index::find_last_source_symtab (struct objfile *objfile)
> > > >      }
> > > >  }
> > > >
> > > > +void
> > > > +dwarf2_gdb_index::map_symbol_filenames
> > > > +     (struct objfile *objfile,
> > > > +      gdb::function_view<symbol_filename_ftype> fun,
> > > > +      bool need_fullname)
> > > > +{
> > > > +  try
> > > > +    {
> > > > +      dwarf2_base_index_functions::map_symbol_filenames (objfile, fun,
> > > > +                                                        need_fullname);
> > > > +    }
> > > > +  catch (const gdb_exception &e)
> > > > +    {
> > > > +      if ((objfile->flags & OBJF_DOWNLOAD_DEFERRED) == 0)
> > > > +       exception_print (gdb_stderr, e);
> > > > +      else
> > > > +       {
> > > > +         if (mdl == nullptr)
> > > > +           mdl.reset (new mapped_debug_line (objfile));
> > > > +         mdl->map_filenames (fun, need_fullname);
> > > > +       }
> > > > +    }
> > > > +}
> > > > +
> > > >  /* This dumps minimal information about the index.
> > > >     It is called via "mt print objfiles".
> > > >     One use is to verify .gdb_index has been loaded by the
> > > > @@ -590,6 +635,17 @@ dwarf2_gdb_index::do_expand_symtabs_matching
> > > >    return result;
> > > >  }
> > > >
> > > > +bool
> > > > +dwarf2_gdb_index::filename_in_debug_line
> > > > +  (objfile *objfile,
> > > > +   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > > +{
> > > > +  if (mdl == nullptr)
> > > > +    mdl.reset (new mapped_debug_line (objfile));
> > > > +
> > > > +  return mdl->contains_matching_filename (file_matcher);
> > > > +}
> > > > +
> > > >  bool
> > > >  dwarf2_gdb_index::expand_symtabs_matching
> > > >      (struct objfile *objfile,
> > > > @@ -618,6 +674,10 @@ dwarf2_gdb_index::expand_symtabs_matching
> > > >           return false;
> > > >         }
> > > >
> > > > +      if (file_matcher != nullptr
> > > > +         && !filename_in_debug_line (objfile, file_matcher))
> > > > +       return true;
> > > > +
> > > >        read_full_dwarf_from_debuginfod (objfile, this);
> > > >        return true;
> > > >      }
> > > > diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> > > > index 0c5689c63ef..876e3aedcf1 100644
> > > > --- a/gdb/dwarf2/read.c
> > > > +++ b/gdb/dwarf2/read.c
> > > > @@ -81,6 +81,7 @@
> > > >  #include "gdbsupport/gdb_optional.h"
> > > >  #include "gdbsupport/underlying.h"
> > > >  #include "gdbsupport/hash_enum.h"
> > > > +#include "gdbsupport/scoped_mmap.h"
> > > >  #include "filename-seen-cache.h"
> > > >  #include "producer.h"
> > > >  #include <fcntl.h>
> > > > @@ -2135,6 +2136,213 @@ dw2_get_file_names (dwarf2_per_cu_data *this_cu,
> > > >    return this_cu->file_names;
> > > >  }
> > > >
> > > > +#if !HAVE_SYS_MMAN_H
> > > > +
> > > > +bool
> > > > +mapped_debug_line::contains_matching_filename
> > > > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > > +{
> > > > +  return false;
> > > > +}
> > > > +
> > > > +gdb::array_view<const gdb_byte>
> > > > +mapped_debug_line::read_debug_line_separate
> > > > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > > > +{
> > > > +  return {};
> > > > +}
> > > > +
> > > > +bool
> > > > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > > > +{
> > > > +  return false;
> > > > +}
> > > > +
> > > > +void
> > > > +mapped_debug_line::map_filenames
> > > > +  (gdb::function_view<symbol_filename_ftype> fun,
> > > > +   bool need_fullname)
> > > > +{
> > > > +  return;
> > > > +}
> > > > +
> > > > +#else /* !HAVE_SYS_MMAN_H */
> > > > +
> > > > +struct line_resource_mmap final : public index_cache_resource
> > > > +{
> > > > +  /* Try to mmap FILENAME.  Throw an exception on failure, including if the
> > > > +     file doesn't exist. */
> > > > +  line_resource_mmap (const char *filename)
> > > > +    : mapping (mmap_file (filename))
> > > > +  {}
> > > > +
> > > > +  scoped_mmap mapping;
> > > > +};
> > > > +
> > > > +/* See read.h.  */
> > > > +
> > > > +bool
> > > > +mapped_debug_line::contains_matching_filename
> > > > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > > +{
> > > > +  for (line_header_up &lh : line_headers)
> > > > +    for (file_entry &fe : lh->file_names ())
> > > > +      {
> > > > +       const char *filename = fe.name;
> > > > +
> > > > +       if (file_matcher (fe.name, false))
> > > > +         return true;
> > > > +
> > > > +       bool basename_match = file_matcher (lbasename (fe.name), true);
> > > > +
> > > > +       if (!basenames_may_differ && !basename_match)
> > > > +         continue;
> > > > +
> > > > +       /* DW_AT_comp_dir is not explicitly mentioned in the .debug_line
> > > > +          until DWARF5.  Since we don't have access to the CU at this
> > > > +          point we just check for a partial match on the filename.
> > > > +          If there is a match, the full debuginfo will be downloaded
> > > > +          ane the match will be re-evalute with DW_AT_comp_dir.  */
> > > > +       if (lh->version < 5 && fe.d_index == 0)
> > > > +         return basename_match;
> > > > +
> > > > +       const char *dirname = fe.include_dir (&*lh);
> > > > +       std::string fullname;
> > > > +
> > > > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > > > +         fullname = filename;
> > > > +       else
> > > > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > > > +
> > > > +       gdb::unique_xmalloc_ptr<char> rewritten
> > > > +         = rewrite_source_path (fullname.c_str ());
> > > > +       if (rewritten != nullptr)
> > > > +         fullname = rewritten.release ();
> > > > +
> > > > +       if (file_matcher (fullname.c_str (), false))
> > > > +         return true;
> > > > +      }
> > > > +
> > > > +  return false;
> > > > +}
> > > > +
> > > > +/* See read.h.  */
> > > > +
> > > > +void
> > > > +mapped_debug_line::map_filenames
> > > > +  (gdb::function_view<symbol_filename_ftype> fun,
> > > > +   bool need_fullname)
> > > > +{
> > > > +  for (line_header_up &lh : line_headers)
> > > > +    for (file_entry &fe : lh->file_names ())
> > > > +      {
> > > > +       const char *filename = fe.name;
> > > > +
> > > > +       if (!need_fullname)
> > > > +         {
> > > > +           fun (filename, nullptr);
> > > > +           continue;
> > > > +         }
> > > > +
> > > > +       const char *dirname = fe.include_dir (&*lh);
> > > > +       std::string fullname;
> > > > +
> > > > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > > > +         fullname = filename;
> > > > +       else
> > > > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > > > +
> > > > +       gdb::unique_xmalloc_ptr<char> rewritten
> > > > +         = rewrite_source_path (fullname.c_str ());
> > > > +       if (rewritten != nullptr)
> > > > +         fullname = rewritten.release ();
> > > > +
> > > > +       fun (filename, fullname.c_str ());
> > > > +      }
> > > > +}
> > > > +
> > > > +/* See read.h.  */
> > > > +
> > > > +gdb::array_view<const gdb_byte>
> > > > +mapped_debug_line::read_debug_line_separate
> > > > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > > > +{
> > > > +  if (filename == nullptr)
> > > > +    return {};
> > > > +
> > > > +  try
> > > > +  {
> > > > +    line_resource_mmap *mmap_resource
> > > > +      = new line_resource_mmap (filename);
> > > > +
> > > > +    resource->reset (mmap_resource);
> > > > +
> > > > +    return gdb::array_view<const gdb_byte>
> > > > +      ((const gdb_byte *) mmap_resource->mapping.get (),
> > > > +       mmap_resource->mapping.size ());
> > > > +  }
> > > > +  catch (const gdb_exception &except)
> > > > +  {
> > > > +    exception_print (gdb_stderr, except);
> > > > +  }
> > > > +
> > > > +  return {};
> > > > +}
> > > > +
> > > > +/* See read.h.  */
> > > > +
> > > > +bool
> > > > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > > > +{
> > > > +  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
> > > > +  if (build_id == nullptr)
> > > > +    return false;
> > > > +
> > > > +  gdb::unique_xmalloc_ptr<char> line_path;
> > > > +  scoped_fd line_fd = debuginfod_section_query (build_id->data,
> > > > +                                               build_id->size,
> > > > +                                               bfd_get_filename
> > > > +                                                 (objfile->obfd.get ()),
> > > > +                                               ".debug_line",
> > > > +                                               &line_path);
> > > > +
> > > > +  if (line_fd.get () < 0)
> > > > +    return false;
> > > > +
> > > > +  gdb::unique_xmalloc_ptr<char> line_str_path;
> > > > +  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
> > > > +                                                   build_id->size,
> > > > +                                                   bfd_get_filename
> > > > +                                                     (objfile->obfd.get ()),
> > > > +                                                   ".debug_line_str",
> > > > +                                                   &line_str_path);
> > > > +
> > > > +  line_data = read_debug_line_separate (line_path.get (), &line_resource);
> > > > +  line_str_data = read_debug_line_separate (line_str_path.get (),
> > > > +                                           &line_str_resource);
> > > > +
> > > > +  const gdb_byte *line_ptr = line_data.data ();
> > > > +
> > > > +  while (line_ptr < line_data.data () + line_data.size ())
> > > > +    {
> > > > +      line_header_up lh
> > > > +       = dwarf_decode_line_header (objfile->obfd.get (),
> > > > +                                   line_data, line_str_data,
> > > > +                                   &line_ptr, false,
> > > > +                                   nullptr, nullptr);
> > > > +      line_headers.emplace_back (lh.release ());
> > > > +    }
> > > > +
> > > > +  return true;
> > > > +}
> > > > +#endif /* !HAVE_SYS_MMAN_H */
> > > > +
> > > > +mapped_debug_line::mapped_debug_line (objfile *objfile)
> > > > +{
> > > > +  if (!read_debug_line_from_debuginfod (objfile))
> > > > +    line_headers.clear ();
> > > > +}
> > > > +
> > > >  /* A helper for the "quick" functions which computes and caches the
> > > >     real path for a given file name from the line table.  */
> > > >
> > > > diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
> > > > index 6ed0be7203b..49fea22c092 100644
> > > > --- a/gdb/dwarf2/read.h
> > > > +++ b/gdb/dwarf2/read.h
> > > > @@ -33,6 +33,7 @@
> > > >  #include "gdbsupport/hash_enum.h"
> > > >  #include "gdbsupport/function-view.h"
> > > >  #include "gdbsupport/packed.h"
> > > > +#include "dwarf2/line-header.h"
> > > >
> > > >  /* Hold 'maintenance (set|show) dwarf' commands.  */
> > > >  extern struct cmd_list_element *set_dwarf_cmdlist;
> > > > @@ -969,4 +970,40 @@ extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
> > > >  extern void read_full_dwarf_from_debuginfod (struct objfile *,
> > > >                                              dwarf2_base_index_functions *);
> > > >
> > > > +struct mapped_debug_line
> > > > +{
> > > > +  mapped_debug_line (objfile *objfile);
> > > > +
> > > > +  /* Return true if any of the mapped .debug_line's filenames match
> > > > +     FILE_MATCHER.  */
> > > > +
> > > > +  bool contains_matching_filename
> > > > +    (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > > > +
> > > > +  /* Call FUN with each filename in this mapped .debug_line.  Include
> > > > +     each file's fullname if NEED_FULLNAME is true.  */
> > > > +
> > > > +  void map_filenames (gdb::function_view<symbol_filename_ftype> fun,
> > > > +                     bool need_fullname);
> > > > +
> > > > +private:
> > > > +  std::vector<line_header_up> line_headers;
> > > > +
> > > > +  gdb::array_view<const gdb_byte> line_data;
> > > > +  gdb::array_view<const gdb_byte> line_str_data;
> > > > +
> > > > +  std::unique_ptr<index_cache_resource> line_resource;
> > > > +  std::unique_ptr<index_cache_resource> line_str_resource;
> > > > +
> > > > +  /* Download the .debug_line and .debug_line_str associated with OBJFILE
> > > > +     and populate line_headers.  */
> > > > +
> > > > +  bool read_debug_line_from_debuginfod (objfile *objfile);
> > > > +
> > > > +  /* Initialize line_data and line_str_data with the .debug_line and
> > > > +    .debug_line_str downloaded read_debug_line_from_debuginfod.  */
> > > > +
> > > > +  gdb::array_view<const gdb_byte> read_debug_line_separate
> > > > +    (char *filename, std::unique_ptr<index_cache_resource> *resource);
> > > > +};
> > > >  #endif /* DWARF2READ_H */
> > > > diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
> > > > index bbd21287b28..110864adac3 100644
> > > > --- a/gdb/mi/mi-out.c
> > > > +++ b/gdb/mi/mi-out.c
> > > > @@ -278,7 +278,14 @@ mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
> > > >
> > > >    if (info.state == progress_update::START)
> > > >      {
> > > > -      gdb_printf ("%s...\n", msg.c_str ());
> > > > +      std::string prefix;
> > > > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > > > +       {
> > > > +         prefix = "\n";
> > > > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > > > +       }
> > > > +
> > > > +      gdb_printf ("%s...\n", (prefix + msg).c_str ());
> > > >        info.state = progress_update::WORKING;
> > > >      }
> > > >  }
> > > > diff --git a/gdb/testsuite/gdb.debuginfod/section.exp b/gdb/testsuite/gdb.debuginfod/section.exp
> > > > index ff57c6e32b7..b5c6929fcf7 100644
> > > > --- a/gdb/testsuite/gdb.debuginfod/section.exp
> > > > +++ b/gdb/testsuite/gdb.debuginfod/section.exp
> > > > @@ -171,6 +171,27 @@ proc_with_prefix local_url { } {
> > > >      # during backtrace.
> > > >      set res ".*debug info for $lib_sl1\.\.\.\r\n\#1.*"
> > > >      gdb_test "bt" $res "break backtrace"
> > > > +
> > > > +    clean_restart_with_prompt $sectexec "" "line 1"
> > > > +
> > > > +    # List source file using .debug_line download.
> > > > +    set res ".*\.debug_line.*$lib_sl1.*21.*extern void libsection2_test.*"
> > > > +    gdb_test "list $libsrc1:21" $res "line 1 list"
> > > > +
> > > > +    clean_restart_with_prompt $sectexec "" "line 2"
> > > > +
> > > > +    # Set breakpoint using .debug_line download.
> > > > +    set res ".*section \.debug_line for $lib_sl1.*Breakpoint 2 at.*$libsrc1.*"
> > > > +    gdb_test "br $libsrc1:37" $res "line 2 br"
> > > > +
> > > > +    # Continue to breakpoint.
> > > > +    set res "Breakpoint 2, libsection1_test.*\"Cancelling thread\\\\n\".*"
> > > > +    gdb_test "c" $res "line 2 continue"
> > > > +
> > > > +    # Check that download progress message is correctly formatted
> > > > +    # when printing threads.
> > > > +    set res ".*separate debug info for $lib_sl2\.\.\.\r\n.* 2    Thread.*"
> > > > +    gdb_test "info thr" $res "line thread"
> > > >  }
> > > >
> > > >  # Create CACHE and DB directories ready for debuginfod to use.
> > > > diff --git a/gdb/ui-out.c b/gdb/ui-out.c
> > > > index 9f643b1ce95..fde46bfbe94 100644
> > > > --- a/gdb/ui-out.c
> > > > +++ b/gdb/ui-out.c
> > > > @@ -32,6 +32,9 @@
> > > >  #include <memory>
> > > >  #include <string>
> > > >
> > > > +/* Current state of newline prefixing for progress update messages.  */
> > > > +prefix_state_t cur_prefix_state = prefix_state_t::NEWLINE_OFF;
> > > > +
> > > >  namespace {
> > > >
> > > >  /* A header of a ui_out_table.  */
> > > > diff --git a/gdb/ui-out.h b/gdb/ui-out.h
> > > > index 70a7145741f..7de8796aee0 100644
> > > > --- a/gdb/ui-out.h
> > > > +++ b/gdb/ui-out.h
> > > > @@ -296,6 +296,21 @@ class ui_out
> > > >        BAR
> > > >      };
> > > >
> > > > +    /* Used to communicate the status of a newline prefix for the next progress
> > > > +       update message.  */
> > > > +    enum prefix_state
> > > > +    {
> > > > +      /* Do not modify the next progress update message.  */
> > > > +      NEWLINE_OFF,
> > > > +
> > > > +      /* The next progress update message should include a newline prefix.  */
> > > > +      NEWLINE_NEEDED,
> > > > +
> > > > +      /* A newline prefix was included in a debuginfod progress update
> > > > +        message.  */
> > > > +      NEWLINE_PRINTED
> > > > +    };
> > > > +
> > > >      /* SHOULD_PRINT indicates whether something should be printed for a tty.  */
> > > >      progress_update ()
> > > >      {
> > > > @@ -393,6 +408,11 @@ class ui_out
> > > >    ui_out_level *current_level () const;
> > > >  };
> > > >
> > > > +typedef ui_out::progress_update::prefix_state prefix_state_t;
> > > > +
> > > > +/* Current state of the newline prefix.  */
> > > > +extern prefix_state_t cur_prefix_state;
> > > > +
> > > >  /* Start a new tuple or list on construction, and end it on
> > > >     destruction.  Normally this is used via the typedefs
> > > >     ui_out_emit_tuple and ui_out_emit_list.  */
> > > > --
> > > > 2.41.0
> > > >
  
Aaron Merey Dec. 20, 2023, 2:58 p.m. UTC | #5
Ping

Thanks,
Aaron

On Tue, Dec 12, 2023 at 10:08 AM Aaron Merey <amerey@redhat.com> wrote:
>
> Ping
>
> Thanks,
> Aaron
>
> On Thu, Nov 30, 2023 at 11:30 AM Aaron Merey <amerey@redhat.com> wrote:
> >
> > Ping
> >
> > Thanks,
> > Aaron
> >
> > On Mon, Nov 20, 2023 at 1:40 PM Aaron Merey <amerey@redhat.com> wrote:
> > >
> > > Ping
> > >
> > > Thanks,
> > > Aaron
> > >
> > > On Sun, Nov 12, 2023 at 3:21 PM Aaron Merey <amerey@redhat.com> wrote:
> > > >
> > > > Ping
> > > >
> > > > Thanks,
> > > > Aaron
> > > >
> > > > On Fri, Oct 27, 2023 at 8:20 PM Aaron Merey <amerey@redhat.com> wrote:
> > > > >
> > > > > v4: https://sourceware.org/pipermail/gdb-patches/2023-August/201651.html
> > > > >
> > > > > v5 adds prefix_state to progress_update objects to track when
> > > > > a newline prefix needs to be added to a download progress message.
> > > > > This is used to correctly format progress messages that occur during
> > > > > command autocompletion.
> > > > >
> > > > > Commit message:
> > > > >
> > > > > ELF/DWARF section downloading allows gdb to download .gdb_index files in
> > > > > order to defer full debuginfo downloads.  However .gdb_index does not
> > > > > contain any information regarding source filenames.  When a gdb command
> > > > > includes a filename argument (ex. 'break main.c:50'), this results in
> > > > > the mass downloading of all deferred debuginfo so that gdb can search the
> > > > > debuginfo for matching source filenames.  This can result in unnecessary
> > > > > downloads.
> > > > >
> > > > > To improve this, have gdb instead download each debuginfo's .debug_line
> > > > > (and .debug_line_str if using DWARF5) when executing these commands.
> > > > > Download full debuginfo only when its .debug_line contains a matching
> > > > > filename.
> > > > >
> > > > > Since the combined size of .debug_line and .debug_line_str is only about
> > > > > 1% the size of the corresponding debuginfo, significant time can be saved
> > > > > by checking these sections before choosing to download an entire debuginfo.
> > > > >
> > > > > This patch also redirects stdout and stderr of the debuginfod server
> > > > > used by testsuite/gdb.debuginfod tests to a server_log standard output
> > > > > file.  While adding tests for this patch I ran into an issue where the
> > > > > test server would block when logging to stderr, presumably because the
> > > > > stderr buffer filled up and wasn't being read from.  Redirecting the
> > > > > log to a file fixes this and also makes the server log more accessible
> > > > > when debugging test failures.
> > > > > ---
> > > > >  gdb/cli-out.c                            |  11 +-
> > > > >  gdb/completer.c                          |  18 +-
> > > > >  gdb/dwarf2/line-header.c                 | 215 +++++++++++++++--------
> > > > >  gdb/dwarf2/line-header.h                 |  10 ++
> > > > >  gdb/dwarf2/read-gdb-index.c              |  60 +++++++
> > > > >  gdb/dwarf2/read.c                        | 208 ++++++++++++++++++++++
> > > > >  gdb/dwarf2/read.h                        |  37 ++++
> > > > >  gdb/mi/mi-out.c                          |   9 +-
> > > > >  gdb/testsuite/gdb.debuginfod/section.exp |  21 +++
> > > > >  gdb/ui-out.c                             |   3 +
> > > > >  gdb/ui-out.h                             |  20 +++
> > > > >  11 files changed, 531 insertions(+), 81 deletions(-)
> > > > >
> > > > > diff --git a/gdb/cli-out.c b/gdb/cli-out.c
> > > > > index c919622d418..a570a2d939d 100644
> > > > > --- a/gdb/cli-out.c
> > > > > +++ b/gdb/cli-out.c
> > > > > @@ -307,16 +307,23 @@ cli_ui_out::do_progress_notify (const std::string &msg,
> > > > >
> > > > >    if (info.state == progress_update::START)
> > > > >      {
> > > > > +      std::string prefix;
> > > > > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > > > > +       {
> > > > > +         prefix = "\n";
> > > > > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > > > > +       }
> > > > > +
> > > > >        if (stream->isatty ()
> > > > >           && current_ui->input_interactive_p ()
> > > > >           && chars_per_line >= MIN_CHARS_PER_LINE)
> > > > >         {
> > > > > -         gdb_printf (stream, "%s\n", msg.c_str ());
> > > > > +         gdb_printf (stream, "%s\n", (prefix + msg).c_str ());
> > > > >           info.state = progress_update::BAR;
> > > > >         }
> > > > >        else
> > > > >         {
> > > > > -         gdb_printf (stream, "%s...\n", msg.c_str ());
> > > > > +         gdb_printf (stream, "%s...\n", (prefix + msg).c_str ());
> > > > >           info.state = progress_update::WORKING;
> > > > >         }
> > > > >      }
> > > > > diff --git a/gdb/completer.c b/gdb/completer.c
> > > > > index 2abf3998345..9c299f3eb65 100644
> > > > > --- a/gdb/completer.c
> > > > > +++ b/gdb/completer.c
> > > > > @@ -1345,6 +1345,10 @@ complete_line_internal_1 (completion_tracker &tracker,
> > > > >      {
> > > > >        /* We've recognized a full command.  */
> > > > >
> > > > > +      /* Disable pagination since responding to the pagination prompt
> > > > > +        overwrites rl_line_buffer.  */
> > > > > +      scoped_restore pag_restore = make_scoped_restore (&pagination_enabled, false);
> > > > > +
> > > > >        if (p == tmp_command + point)
> > > > >         {
> > > > >           /* There is no non-whitespace in the line beyond the
> > > > > @@ -1444,7 +1448,8 @@ complete_line_internal_1 (completion_tracker &tracker,
> > > > >  }
> > > > >
> > > > >  /* Wrapper around complete_line_internal_1 to handle
> > > > > -   MAX_COMPLETIONS_REACHED_ERROR.  */
> > > > > +   MAX_COMPLETIONS_REACHED_ERROR and possible progress update
> > > > > +   interactions.  */
> > > > >
> > > > >  static void
> > > > >  complete_line_internal (completion_tracker &tracker,
> > > > > @@ -1452,6 +1457,11 @@ complete_line_internal (completion_tracker &tracker,
> > > > >                         const char *line_buffer, int point,
> > > > >                         complete_line_internal_reason reason)
> > > > >  {
> > > > > +  scoped_restore restore_prefix_state
> > > > > +    = make_scoped_restore
> > > > > +      (&cur_prefix_state,
> > > > > +       ui_out::progress_update::prefix_state::NEWLINE_NEEDED);
> > > > > +
> > > > >    try
> > > > >      {
> > > > >        complete_line_internal_1 (tracker, text, line_buffer, point, reason);
> > > > > @@ -1461,6 +1471,12 @@ complete_line_internal (completion_tracker &tracker,
> > > > >        if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
> > > > >         throw;
> > > > >      }
> > > > > +
> > > > > +  /* If progress update messages printed, then the text being completed
> > > > > +     needs to be printed again.  */
> > > > > +  if (cur_prefix_state
> > > > > +      == ui_out::progress_update::prefix_state::NEWLINE_PRINTED)
> > > > > +    rl_forced_update_display ();
> > > > >  }
> > > > >
> > > > >  /* See completer.h.  */
> > > > > diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
> > > > > index d072a91bac9..b9210d84f6b 100644
> > > > > --- a/gdb/dwarf2/line-header.c
> > > > > +++ b/gdb/dwarf2/line-header.c
> > > > > @@ -102,50 +102,57 @@ read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
> > > > >  {
> > > > >    LONGEST length = read_initial_length (abfd, buf, bytes_read);
> > > > >
> > > > > -  gdb_assert (cu_header->initial_length_size == 4
> > > > > -             || cu_header->initial_length_size == 8
> > > > > -             || cu_header->initial_length_size == 12);
> > > > > +  if (cu_header != nullptr)
> > > > > +    {
> > > > > +      gdb_assert (cu_header->initial_length_size == 4
> > > > > +                 || cu_header->initial_length_size == 8
> > > > > +                 || cu_header->initial_length_size == 12);
> > > > >
> > > > > -  if (cu_header->initial_length_size != *bytes_read)
> > > > > -    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > > > > +      if (cu_header->initial_length_size != *bytes_read)
> > > > > +       complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
> > > > > +    }
> > > > >
> > > > >    *offset_size = (*bytes_read == 4) ? 4 : 8;
> > > > >    return length;
> > > > >  }
> > > > >
> > > > > -/* Read directory or file name entry format, starting with byte of
> > > > > -   format count entries, ULEB128 pairs of entry formats, ULEB128 of
> > > > > -   entries count and the entries themselves in the described entry
> > > > > -   format.  */
> > > > > +
> > > > > +/* Like read_formatted_entries but the .debug_line and .debug_line_str
> > > > > +   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
> > > > > +   where these sections are read from separate files without necessarily
> > > > > +   having access to the entire debuginfo file they originate from.  */
> > > > >
> > > > >  static void
> > > > > -read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > > > -                       const gdb_byte **bufp, struct line_header *lh,
> > > > > -                       unsigned int offset_size,
> > > > > -                       void (*callback) (struct line_header *lh,
> > > > > -                                         const char *name,
> > > > > -                                         dir_index d_index,
> > > > > -                                         unsigned int mod_time,
> > > > > -                                         unsigned int length))
> > > > > +read_formatted_entries
> > > > > +  (bfd *parent_bfd, const gdb_byte **line_bufp,
> > > > > +   const gdb::array_view<const gdb_byte> line_str_data,
> > > > > +   struct line_header *lh,
> > > > > +   unsigned int offset_size,
> > > > > +   void (*callback) (struct line_header *lh,
> > > > > +                    const char *name,
> > > > > +                    dir_index d_index,
> > > > > +                    unsigned int mod_time,
> > > > > +                    unsigned int length))
> > > > >  {
> > > > >    gdb_byte format_count, formati;
> > > > >    ULONGEST data_count, datai;
> > > > > -  const gdb_byte *buf = *bufp;
> > > > > +  const gdb_byte *buf = *line_bufp;
> > > > > +  const gdb_byte *str_buf = line_str_data.data ();
> > > > >    const gdb_byte *format_header_data;
> > > > >    unsigned int bytes_read;
> > > > >
> > > > > -  format_count = read_1_byte (abfd, buf);
> > > > > +  format_count = read_1_byte (parent_bfd, buf);
> > > > >    buf += 1;
> > > > >    format_header_data = buf;
> > > > >    for (formati = 0; formati < format_count; formati++)
> > > > >      {
> > > > > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > > > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > > > >        buf += bytes_read;
> > > > > -      read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > > > +      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > > > >        buf += bytes_read;
> > > > >      }
> > > > >
> > > > > -  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
> > > > > +  data_count = read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
> > > > >    buf += bytes_read;
> > > > >    for (datai = 0; datai < data_count; datai++)
> > > > >      {
> > > > > @@ -154,10 +161,10 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > > >
> > > > >        for (formati = 0; formati < format_count; formati++)
> > > > >         {
> > > > > -         ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
> > > > > +         ULONGEST content_type = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> > > > >           format += bytes_read;
> > > > >
> > > > > -         ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
> > > > > +         ULONGEST form  = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
> > > > >           format += bytes_read;
> > > > >
> > > > >           gdb::optional<const char *> string;
> > > > > @@ -166,36 +173,48 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > > >           switch (form)
> > > > >             {
> > > > >             case DW_FORM_string:
> > > > > -             string.emplace (read_direct_string (abfd, buf, &bytes_read));
> > > > > +             string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
> > > > >               buf += bytes_read;
> > > > >               break;
> > > > >
> > > > >             case DW_FORM_line_strp:
> > > > >               {
> > > > > -               const char *str
> > > > > -                 = per_objfile->read_line_string (buf, offset_size);
> > > > > +               if (line_str_data.empty ())
> > > > > +                 error (_("Dwarf Error: DW_FORM_line_strp used without " \
> > > > > +                          "required section"));
> > > > > +               if (line_str_data.size () <= offset_size)
> > > > > +                 error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
> > > > > +                          "of section .debug_line"));
> > > > > +
> > > > > +               ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
> > > > > +
> > > > > +               const char *str;
> > > > > +               if (str_buf[str_offset] == '\0')
> > > > > +                 str = nullptr;
> > > > > +               else
> > > > > +                 str = (const char *) (str_buf + str_offset);
> > > > >                 string.emplace (str);
> > > > >                 buf += offset_size;
> > > > > +               break;
> > > > >               }
> > > > > -             break;
> > > > >
> > > > >             case DW_FORM_data1:
> > > > > -             uint.emplace (read_1_byte (abfd, buf));
> > > > > +             uint.emplace (read_1_byte (parent_bfd, buf));
> > > > >               buf += 1;
> > > > >               break;
> > > > >
> > > > >             case DW_FORM_data2:
> > > > > -             uint.emplace (read_2_bytes (abfd, buf));
> > > > > +             uint.emplace (read_2_bytes (parent_bfd, buf));
> > > > >               buf += 2;
> > > > >               break;
> > > > >
> > > > >             case DW_FORM_data4:
> > > > > -             uint.emplace (read_4_bytes (abfd, buf));
> > > > > +             uint.emplace (read_4_bytes (parent_bfd, buf));
> > > > >               buf += 4;
> > > > >               break;
> > > > >
> > > > >             case DW_FORM_data8:
> > > > > -             uint.emplace (read_8_bytes (abfd, buf));
> > > > > +             uint.emplace (read_8_bytes (parent_bfd, buf));
> > > > >               buf += 8;
> > > > >               break;
> > > > >
> > > > > @@ -205,7 +224,7 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > > >               break;
> > > > >
> > > > >             case DW_FORM_udata:
> > > > > -             uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
> > > > > +             uint.emplace (read_unsigned_leb128 (parent_bfd, buf, &bytes_read));
> > > > >               buf += bytes_read;
> > > > >               break;
> > > > >
> > > > > @@ -248,28 +267,30 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> > > > >        callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
> > > > >      }
> > > > >
> > > > > -  *bufp = buf;
> > > > > +  *line_bufp = buf;
> > > > >  }
> > > > >
> > > > >  /* See line-header.h.  */
> > > > >
> > > > >  line_header_up
> > > > > -dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > > -                          dwarf2_per_objfile *per_objfile,
> > > > > -                          struct dwarf2_section_info *section,
> > > > > -                          const struct comp_unit_head *cu_header,
> > > > > -                          const char *comp_dir)
> > > > > +dwarf_decode_line_header (bfd *parent_bfd,
> > > > > +                         gdb::array_view<const gdb_byte> line_data,
> > > > > +                         gdb::array_view<const gdb_byte> line_str_data,
> > > > > +                         const gdb_byte **debug_line_ptr,
> > > > > +                         bool is_dwz,
> > > > > +                         const struct comp_unit_head *cu_header,
> > > > > +                         const char *comp_dir)
> > > > >  {
> > > > > -  const gdb_byte *line_ptr;
> > > > > +  const gdb_byte *line_ptr, *buf;
> > > > >    unsigned int bytes_read, offset_size;
> > > > >    int i;
> > > > >    const char *cur_dir, *cur_file;
> > > > >
> > > > > -  bfd *abfd = section->get_bfd_owner ();
> > > > > +  buf = *debug_line_ptr;
> > > > >
> > > > >    /* Make sure that at least there's room for the total_length field.
> > > > >       That could be 12 bytes long, but we're just going to fudge that.  */
> > > > > -  if (to_underlying (sect_off) + 4 >= section->size)
> > > > > +  if (buf + 4 >= line_data.data () + line_data.size ())
> > > > >      {
> > > > >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> > > > >        return 0;
> > > > > @@ -277,62 +298,65 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > >
> > > > >    line_header_up lh (new line_header (comp_dir));
> > > > >
> > > > > -  lh->sect_off = sect_off;
> > > > > +  lh->sect_off = (sect_offset) (buf - line_data.data ());
> > > > >    lh->offset_in_dwz = is_dwz;
> > > > >
> > > > > -  line_ptr = section->buffer + to_underlying (sect_off);
> > > > > +  line_ptr = buf;
> > > > >
> > > > >    /* Read in the header.  */
> > > > >    LONGEST unit_length
> > > > > -    = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
> > > > > +    = read_checked_initial_length_and_offset (parent_bfd, buf, cu_header,
> > > > >                                               &bytes_read, &offset_size);
> > > > > -  line_ptr += bytes_read;
> > > > >
> > > > > -  const gdb_byte *start_here = line_ptr;
> > > > > +  line_ptr += bytes_read;
> > > > >
> > > > > -  if (line_ptr + unit_length > (section->buffer + section->size))
> > > > > +  if (line_ptr + unit_length > buf + line_data.size ())
> > > > >      {
> > > > >        dwarf2_statement_list_fits_in_line_number_section_complaint ();
> > > > >        return 0;
> > > > >      }
> > > > > +
> > > > > +  const gdb_byte *start_here = line_ptr;
> > > > > +
> > > > >    lh->statement_program_end = start_here + unit_length;
> > > > > -  lh->version = read_2_bytes (abfd, line_ptr);
> > > > > +  lh->version = read_2_bytes (parent_bfd, line_ptr);
> > > > >    line_ptr += 2;
> > > > >    if (lh->version > 5)
> > > > >      {
> > > > >        /* This is a version we don't understand.  The format could have
> > > > >          changed in ways we don't handle properly so just punt.  */
> > > > >        complaint (_("unsupported version in .debug_line section"));
> > > > > -      return NULL;
> > > > > +      return nullptr;
> > > > >      }
> > > > >    if (lh->version >= 5)
> > > > >      {
> > > > >        gdb_byte segment_selector_size;
> > > > >
> > > > >        /* Skip address size.  */
> > > > > -      read_1_byte (abfd, line_ptr);
> > > > > +      read_1_byte (parent_bfd, line_ptr);
> > > > >        line_ptr += 1;
> > > > >
> > > > > -      segment_selector_size = read_1_byte (abfd, line_ptr);
> > > > > +      segment_selector_size = read_1_byte (parent_bfd, line_ptr);
> > > > >        line_ptr += 1;
> > > > >        if (segment_selector_size != 0)
> > > > >         {
> > > > >           complaint (_("unsupported segment selector size %u "
> > > > >                        "in .debug_line section"),
> > > > >                      segment_selector_size);
> > > > > -         return NULL;
> > > > > +         return nullptr;
> > > > >         }
> > > > >      }
> > > > >
> > > > > -  LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
> > > > > +  LONGEST header_length = read_offset (parent_bfd, line_ptr, offset_size);
> > > > >    line_ptr += offset_size;
> > > > >    lh->statement_program_start = line_ptr + header_length;
> > > > > -  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
> > > > > +
> > > > > +  lh->minimum_instruction_length = read_1_byte (parent_bfd, line_ptr);
> > > > >    line_ptr += 1;
> > > > >
> > > > >    if (lh->version >= 4)
> > > > >      {
> > > > > -      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
> > > > > +      lh->maximum_ops_per_instruction = read_1_byte (parent_bfd, line_ptr);
> > > > >        line_ptr += 1;
> > > > >      }
> > > > >    else
> > > > > @@ -345,41 +369,47 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > >                    "in `.debug_line' section"));
> > > > >      }
> > > > >
> > > > > -  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
> > > > > +  lh->default_is_stmt = read_1_byte (parent_bfd, line_ptr);
> > > > >    line_ptr += 1;
> > > > > -  lh->line_base = read_1_signed_byte (abfd, line_ptr);
> > > > > +
> > > > > +  lh->line_base = read_1_signed_byte (parent_bfd, line_ptr);
> > > > >    line_ptr += 1;
> > > > > -  lh->line_range = read_1_byte (abfd, line_ptr);
> > > > > +
> > > > > +  lh->line_range = read_1_byte (parent_bfd, line_ptr);
> > > > >    line_ptr += 1;
> > > > > -  lh->opcode_base = read_1_byte (abfd, line_ptr);
> > > > > +
> > > > > +  lh->opcode_base = read_1_byte (parent_bfd, line_ptr);
> > > > >    line_ptr += 1;
> > > > > +
> > > > >    lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
> > > > >
> > > > >    lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
> > > > >    for (i = 1; i < lh->opcode_base; ++i)
> > > > >      {
> > > > > -      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
> > > > > +      lh->standard_opcode_lengths[i] = read_1_byte (parent_bfd, line_ptr);
> > > > >        line_ptr += 1;
> > > > >      }
> > > > >
> > > > >    if (lh->version >= 5)
> > > > >      {
> > > > >        /* Read directory table.  */
> > > > > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > > > > -                             offset_size,
> > > > > -                             [] (struct line_header *header, const char *name,
> > > > > -                                 dir_index d_index, unsigned int mod_time,
> > > > > -                                 unsigned int length)
> > > > > +      read_formatted_entries
> > > > > +       (parent_bfd, &line_ptr, line_str_data,
> > > > > +        lh.get (), offset_size,
> > > > > +        [] (struct line_header *header, const char *name,
> > > > > +            dir_index d_index, unsigned int mod_time,
> > > > > +            unsigned int length)
> > > > >         {
> > > > >           header->add_include_dir (name);
> > > > >         });
> > > > >
> > > > >        /* Read file name table.  */
> > > > > -      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
> > > > > -                             offset_size,
> > > > > -                             [] (struct line_header *header, const char *name,
> > > > > -                                 dir_index d_index, unsigned int mod_time,
> > > > > -                                 unsigned int length)
> > > > > +      read_formatted_entries
> > > > > +       (parent_bfd, &line_ptr, line_str_data,
> > > > > +        lh.get (), offset_size,
> > > > > +        [] (struct line_header *header, const char *name,
> > > > > +            dir_index d_index, unsigned int mod_time,
> > > > > +            unsigned int length)
> > > > >         {
> > > > >           header->add_file_name (name, d_index, mod_time, length);
> > > > >         });
> > > > > @@ -387,7 +417,7 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > >    else
> > > > >      {
> > > > >        /* Read directory table.  */
> > > > > -      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > > > > +      while ((cur_dir = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> > > > >         {
> > > > >           line_ptr += bytes_read;
> > > > >           lh->add_include_dir (cur_dir);
> > > > > @@ -395,17 +425,17 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > >        line_ptr += bytes_read;
> > > > >
> > > > >        /* Read file name table.  */
> > > > > -      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
> > > > > +      while ((cur_file = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
> > > > >         {
> > > > >           unsigned int mod_time, length;
> > > > >           dir_index d_index;
> > > > >
> > > > >           line_ptr += bytes_read;
> > > > > -         d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > > > +         d_index = (dir_index) read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > > > >           line_ptr += bytes_read;
> > > > > -         mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > > > +         mod_time = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > > > >           line_ptr += bytes_read;
> > > > > -         length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
> > > > > +         length = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
> > > > >           line_ptr += bytes_read;
> > > > >
> > > > >           lh->add_file_name (cur_file, d_index, mod_time, length);
> > > > > @@ -413,9 +443,40 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > >        line_ptr += bytes_read;
> > > > >      }
> > > > >
> > > > > -  if (line_ptr > (section->buffer + section->size))
> > > > > +  if (line_ptr > (buf + line_data.size ()))
> > > > >      complaint (_("line number info header doesn't "
> > > > >                  "fit in `.debug_line' section"));
> > > > >
> > > > > +  *debug_line_ptr += unit_length + offset_size;
> > > > >    return lh;
> > > > >  }
> > > > > +
> > > > > +line_header_up
> > > > > +dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
> > > > > +                          dwarf2_per_objfile *per_objfile,
> > > > > +                          struct dwarf2_section_info *section,
> > > > > +                          const struct comp_unit_head *cu_header,
> > > > > +                          const char *comp_dir)
> > > > > +{
> > > > > +  struct objfile *objfile = per_objfile->objfile;
> > > > > +  struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
> > > > > +
> > > > > +  /* Read .debug_line.  */
> > > > > +  dwarf2_section_info *line_sec = &per_bfd->line;
> > > > > +  bfd_size_type line_size = line_sec->get_size (objfile);
> > > > > +
> > > > > +  gdb::array_view<const gdb_byte> line (line_sec->buffer, line_size);
> > > > > +
> > > > > +  /* Read .debug_line_str.  */
> > > > > +  dwarf2_section_info *line_str_sec = &per_bfd->line_str;
> > > > > +  bfd_size_type line_str_size = line_str_sec->get_size (objfile);
> > > > > +
> > > > > +  gdb::array_view<const gdb_byte> line_str (line_str_sec->buffer,
> > > > > +                                           line_str_size);
> > > > > +
> > > > > +  const gdb_byte *line_ptr = line.data () + to_underlying (sect_off);
> > > > > +
> > > > > +  return dwarf_decode_line_header
> > > > > +    (per_bfd->obfd, line, line_str, &line_ptr,
> > > > > +     is_dwz, cu_header, comp_dir);
> > > > > +}
> > > > > diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
> > > > > index 06d2eec573b..22db9f9aa78 100644
> > > > > --- a/gdb/dwarf2/line-header.h
> > > > > +++ b/gdb/dwarf2/line-header.h
> > > > > @@ -217,4 +217,14 @@ extern line_header_up dwarf_decode_line_header
> > > > >     struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
> > > > >     const char *comp_dir);
> > > > >
> > > > > +/* Like above but the .debug_line and .debug_line_str are stored in
> > > > > +   LINE_DATA and LINE_STR_DATA. *DEBUG_LINE_PTR should point to a
> > > > > +   statement program header within LINE_DATA.  */
> > > > > +
> > > > > +extern line_header_up dwarf_decode_line_header
> > > > > +  (bfd *parent_bfd, gdb::array_view<const gdb_byte> line_data,
> > > > > +   gdb::array_view<const gdb_byte> line_str_data,
> > > > > +   const gdb_byte **debug_line_ptr, bool is_dwz,
> > > > > +  const comp_unit_head *cu_header, const char *comp_dir);
> > > > > +
> > > > >  #endif /* DWARF2_LINE_HEADER_H */
> > > > > diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
> > > > > index da88a8b405c..c0e51357ce2 100644
> > > > > --- a/gdb/dwarf2/read-gdb-index.c
> > > > > +++ b/gdb/dwarf2/read-gdb-index.c
> > > > > @@ -131,6 +131,9 @@ struct mapped_gdb_index final : public mapped_index_base
> > > > >    }
> > > > >  };
> > > > >
> > > > > +struct mapped_debug_line;
> > > > > +typedef std::unique_ptr<mapped_debug_line> mapped_debug_line_up;
> > > > > +
> > > > >  struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > > > >  {
> > > > >    /* This dumps minimal information about the index.
> > > > > @@ -175,6 +178,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > > > >       domain_enum domain,
> > > > >       enum search_domain kind);
> > > > >
> > > > > + /* If OBJFILE's debuginfo download has been deferred, use a mapped_debug_line
> > > > > +    to generate filenames.
> > > > > +
> > > > > +    Otherwise call dwarf2_base_index_functions::map_symbol_filenames.  */
> > > > > +
> > > > > +  void map_symbol_filenames (struct objfile *objfile,
> > > > > +                            gdb::function_view<symbol_filename_ftype> fun,
> > > > > +                            bool need_fullname) override;
> > > > > +
> > > > >    /* Calls dwarf2_base_index_functions::expand_all_symtabs and downloads
> > > > >       debuginfo if necessary.  */
> > > > >    void expand_all_symtabs (struct objfile *objfile) override;
> > > > > @@ -182,6 +194,15 @@ struct dwarf2_gdb_index : public dwarf2_base_index_functions
> > > > >    /* Calls dwarf2_base_index_functions::find_last_source_symtab and downloads
> > > > >       debuginfo if necessary.  */
> > > > >    struct symtab *find_last_source_symtab (struct objfile *objfile) override;
> > > > > +
> > > > > +  /* Filename information related to this .gdb_index.  */
> > > > > +  mapped_debug_line_up mdl;
> > > > > +
> > > > > +  /* Return true if any of the filenames in this .gdb_index's .debug_line
> > > > > +     mapping match FILE_MATCHER.  Initializes the mapping if necessary.  */
> > > > > +  bool filename_in_debug_line
> > > > > +    (objfile *objfile,
> > > > > +     gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > > > >  };
> > > > >
> > > > >  void
> > > > > @@ -217,6 +238,30 @@ dwarf2_gdb_index::find_last_source_symtab (struct objfile *objfile)
> > > > >      }
> > > > >  }
> > > > >
> > > > > +void
> > > > > +dwarf2_gdb_index::map_symbol_filenames
> > > > > +     (struct objfile *objfile,
> > > > > +      gdb::function_view<symbol_filename_ftype> fun,
> > > > > +      bool need_fullname)
> > > > > +{
> > > > > +  try
> > > > > +    {
> > > > > +      dwarf2_base_index_functions::map_symbol_filenames (objfile, fun,
> > > > > +                                                        need_fullname);
> > > > > +    }
> > > > > +  catch (const gdb_exception &e)
> > > > > +    {
> > > > > +      if ((objfile->flags & OBJF_DOWNLOAD_DEFERRED) == 0)
> > > > > +       exception_print (gdb_stderr, e);
> > > > > +      else
> > > > > +       {
> > > > > +         if (mdl == nullptr)
> > > > > +           mdl.reset (new mapped_debug_line (objfile));
> > > > > +         mdl->map_filenames (fun, need_fullname);
> > > > > +       }
> > > > > +    }
> > > > > +}
> > > > > +
> > > > >  /* This dumps minimal information about the index.
> > > > >     It is called via "mt print objfiles".
> > > > >     One use is to verify .gdb_index has been loaded by the
> > > > > @@ -590,6 +635,17 @@ dwarf2_gdb_index::do_expand_symtabs_matching
> > > > >    return result;
> > > > >  }
> > > > >
> > > > > +bool
> > > > > +dwarf2_gdb_index::filename_in_debug_line
> > > > > +  (objfile *objfile,
> > > > > +   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > > > +{
> > > > > +  if (mdl == nullptr)
> > > > > +    mdl.reset (new mapped_debug_line (objfile));
> > > > > +
> > > > > +  return mdl->contains_matching_filename (file_matcher);
> > > > > +}
> > > > > +
> > > > >  bool
> > > > >  dwarf2_gdb_index::expand_symtabs_matching
> > > > >      (struct objfile *objfile,
> > > > > @@ -618,6 +674,10 @@ dwarf2_gdb_index::expand_symtabs_matching
> > > > >           return false;
> > > > >         }
> > > > >
> > > > > +      if (file_matcher != nullptr
> > > > > +         && !filename_in_debug_line (objfile, file_matcher))
> > > > > +       return true;
> > > > > +
> > > > >        read_full_dwarf_from_debuginfod (objfile, this);
> > > > >        return true;
> > > > >      }
> > > > > diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> > > > > index 0c5689c63ef..876e3aedcf1 100644
> > > > > --- a/gdb/dwarf2/read.c
> > > > > +++ b/gdb/dwarf2/read.c
> > > > > @@ -81,6 +81,7 @@
> > > > >  #include "gdbsupport/gdb_optional.h"
> > > > >  #include "gdbsupport/underlying.h"
> > > > >  #include "gdbsupport/hash_enum.h"
> > > > > +#include "gdbsupport/scoped_mmap.h"
> > > > >  #include "filename-seen-cache.h"
> > > > >  #include "producer.h"
> > > > >  #include <fcntl.h>
> > > > > @@ -2135,6 +2136,213 @@ dw2_get_file_names (dwarf2_per_cu_data *this_cu,
> > > > >    return this_cu->file_names;
> > > > >  }
> > > > >
> > > > > +#if !HAVE_SYS_MMAN_H
> > > > > +
> > > > > +bool
> > > > > +mapped_debug_line::contains_matching_filename
> > > > > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > > > +{
> > > > > +  return false;
> > > > > +}
> > > > > +
> > > > > +gdb::array_view<const gdb_byte>
> > > > > +mapped_debug_line::read_debug_line_separate
> > > > > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > > > > +{
> > > > > +  return {};
> > > > > +}
> > > > > +
> > > > > +bool
> > > > > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > > > > +{
> > > > > +  return false;
> > > > > +}
> > > > > +
> > > > > +void
> > > > > +mapped_debug_line::map_filenames
> > > > > +  (gdb::function_view<symbol_filename_ftype> fun,
> > > > > +   bool need_fullname)
> > > > > +{
> > > > > +  return;
> > > > > +}
> > > > > +
> > > > > +#else /* !HAVE_SYS_MMAN_H */
> > > > > +
> > > > > +struct line_resource_mmap final : public index_cache_resource
> > > > > +{
> > > > > +  /* Try to mmap FILENAME.  Throw an exception on failure, including if the
> > > > > +     file doesn't exist. */
> > > > > +  line_resource_mmap (const char *filename)
> > > > > +    : mapping (mmap_file (filename))
> > > > > +  {}
> > > > > +
> > > > > +  scoped_mmap mapping;
> > > > > +};
> > > > > +
> > > > > +/* See read.h.  */
> > > > > +
> > > > > +bool
> > > > > +mapped_debug_line::contains_matching_filename
> > > > > +  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
> > > > > +{
> > > > > +  for (line_header_up &lh : line_headers)
> > > > > +    for (file_entry &fe : lh->file_names ())
> > > > > +      {
> > > > > +       const char *filename = fe.name;
> > > > > +
> > > > > +       if (file_matcher (fe.name, false))
> > > > > +         return true;
> > > > > +
> > > > > +       bool basename_match = file_matcher (lbasename (fe.name), true);
> > > > > +
> > > > > +       if (!basenames_may_differ && !basename_match)
> > > > > +         continue;
> > > > > +
> > > > > +       /* DW_AT_comp_dir is not explicitly mentioned in the .debug_line
> > > > > +          until DWARF5.  Since we don't have access to the CU at this
> > > > > +          point we just check for a partial match on the filename.
> > > > > +          If there is a match, the full debuginfo will be downloaded
> > > > > +          ane the match will be re-evalute with DW_AT_comp_dir.  */
> > > > > +       if (lh->version < 5 && fe.d_index == 0)
> > > > > +         return basename_match;
> > > > > +
> > > > > +       const char *dirname = fe.include_dir (&*lh);
> > > > > +       std::string fullname;
> > > > > +
> > > > > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > > > > +         fullname = filename;
> > > > > +       else
> > > > > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > > > > +
> > > > > +       gdb::unique_xmalloc_ptr<char> rewritten
> > > > > +         = rewrite_source_path (fullname.c_str ());
> > > > > +       if (rewritten != nullptr)
> > > > > +         fullname = rewritten.release ();
> > > > > +
> > > > > +       if (file_matcher (fullname.c_str (), false))
> > > > > +         return true;
> > > > > +      }
> > > > > +
> > > > > +  return false;
> > > > > +}
> > > > > +
> > > > > +/* See read.h.  */
> > > > > +
> > > > > +void
> > > > > +mapped_debug_line::map_filenames
> > > > > +  (gdb::function_view<symbol_filename_ftype> fun,
> > > > > +   bool need_fullname)
> > > > > +{
> > > > > +  for (line_header_up &lh : line_headers)
> > > > > +    for (file_entry &fe : lh->file_names ())
> > > > > +      {
> > > > > +       const char *filename = fe.name;
> > > > > +
> > > > > +       if (!need_fullname)
> > > > > +         {
> > > > > +           fun (filename, nullptr);
> > > > > +           continue;
> > > > > +         }
> > > > > +
> > > > > +       const char *dirname = fe.include_dir (&*lh);
> > > > > +       std::string fullname;
> > > > > +
> > > > > +       if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
> > > > > +         fullname = filename;
> > > > > +       else
> > > > > +         fullname = std::string (dirname) + SLASH_STRING + filename;
> > > > > +
> > > > > +       gdb::unique_xmalloc_ptr<char> rewritten
> > > > > +         = rewrite_source_path (fullname.c_str ());
> > > > > +       if (rewritten != nullptr)
> > > > > +         fullname = rewritten.release ();
> > > > > +
> > > > > +       fun (filename, fullname.c_str ());
> > > > > +      }
> > > > > +}
> > > > > +
> > > > > +/* See read.h.  */
> > > > > +
> > > > > +gdb::array_view<const gdb_byte>
> > > > > +mapped_debug_line::read_debug_line_separate
> > > > > +  (char *filename, std::unique_ptr<index_cache_resource> *resource)
> > > > > +{
> > > > > +  if (filename == nullptr)
> > > > > +    return {};
> > > > > +
> > > > > +  try
> > > > > +  {
> > > > > +    line_resource_mmap *mmap_resource
> > > > > +      = new line_resource_mmap (filename);
> > > > > +
> > > > > +    resource->reset (mmap_resource);
> > > > > +
> > > > > +    return gdb::array_view<const gdb_byte>
> > > > > +      ((const gdb_byte *) mmap_resource->mapping.get (),
> > > > > +       mmap_resource->mapping.size ());
> > > > > +  }
> > > > > +  catch (const gdb_exception &except)
> > > > > +  {
> > > > > +    exception_print (gdb_stderr, except);
> > > > > +  }
> > > > > +
> > > > > +  return {};
> > > > > +}
> > > > > +
> > > > > +/* See read.h.  */
> > > > > +
> > > > > +bool
> > > > > +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> > > > > +{
> > > > > +  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
> > > > > +  if (build_id == nullptr)
> > > > > +    return false;
> > > > > +
> > > > > +  gdb::unique_xmalloc_ptr<char> line_path;
> > > > > +  scoped_fd line_fd = debuginfod_section_query (build_id->data,
> > > > > +                                               build_id->size,
> > > > > +                                               bfd_get_filename
> > > > > +                                                 (objfile->obfd.get ()),
> > > > > +                                               ".debug_line",
> > > > > +                                               &line_path);
> > > > > +
> > > > > +  if (line_fd.get () < 0)
> > > > > +    return false;
> > > > > +
> > > > > +  gdb::unique_xmalloc_ptr<char> line_str_path;
> > > > > +  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
> > > > > +                                                   build_id->size,
> > > > > +                                                   bfd_get_filename
> > > > > +                                                     (objfile->obfd.get ()),
> > > > > +                                                   ".debug_line_str",
> > > > > +                                                   &line_str_path);
> > > > > +
> > > > > +  line_data = read_debug_line_separate (line_path.get (), &line_resource);
> > > > > +  line_str_data = read_debug_line_separate (line_str_path.get (),
> > > > > +                                           &line_str_resource);
> > > > > +
> > > > > +  const gdb_byte *line_ptr = line_data.data ();
> > > > > +
> > > > > +  while (line_ptr < line_data.data () + line_data.size ())
> > > > > +    {
> > > > > +      line_header_up lh
> > > > > +       = dwarf_decode_line_header (objfile->obfd.get (),
> > > > > +                                   line_data, line_str_data,
> > > > > +                                   &line_ptr, false,
> > > > > +                                   nullptr, nullptr);
> > > > > +      line_headers.emplace_back (lh.release ());
> > > > > +    }
> > > > > +
> > > > > +  return true;
> > > > > +}
> > > > > +#endif /* !HAVE_SYS_MMAN_H */
> > > > > +
> > > > > +mapped_debug_line::mapped_debug_line (objfile *objfile)
> > > > > +{
> > > > > +  if (!read_debug_line_from_debuginfod (objfile))
> > > > > +    line_headers.clear ();
> > > > > +}
> > > > > +
> > > > >  /* A helper for the "quick" functions which computes and caches the
> > > > >     real path for a given file name from the line table.  */
> > > > >
> > > > > diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
> > > > > index 6ed0be7203b..49fea22c092 100644
> > > > > --- a/gdb/dwarf2/read.h
> > > > > +++ b/gdb/dwarf2/read.h
> > > > > @@ -33,6 +33,7 @@
> > > > >  #include "gdbsupport/hash_enum.h"
> > > > >  #include "gdbsupport/function-view.h"
> > > > >  #include "gdbsupport/packed.h"
> > > > > +#include "dwarf2/line-header.h"
> > > > >
> > > > >  /* Hold 'maintenance (set|show) dwarf' commands.  */
> > > > >  extern struct cmd_list_element *set_dwarf_cmdlist;
> > > > > @@ -969,4 +970,40 @@ extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
> > > > >  extern void read_full_dwarf_from_debuginfod (struct objfile *,
> > > > >                                              dwarf2_base_index_functions *);
> > > > >
> > > > > +struct mapped_debug_line
> > > > > +{
> > > > > +  mapped_debug_line (objfile *objfile);
> > > > > +
> > > > > +  /* Return true if any of the mapped .debug_line's filenames match
> > > > > +     FILE_MATCHER.  */
> > > > > +
> > > > > +  bool contains_matching_filename
> > > > > +    (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
> > > > > +
> > > > > +  /* Call FUN with each filename in this mapped .debug_line.  Include
> > > > > +     each file's fullname if NEED_FULLNAME is true.  */
> > > > > +
> > > > > +  void map_filenames (gdb::function_view<symbol_filename_ftype> fun,
> > > > > +                     bool need_fullname);
> > > > > +
> > > > > +private:
> > > > > +  std::vector<line_header_up> line_headers;
> > > > > +
> > > > > +  gdb::array_view<const gdb_byte> line_data;
> > > > > +  gdb::array_view<const gdb_byte> line_str_data;
> > > > > +
> > > > > +  std::unique_ptr<index_cache_resource> line_resource;
> > > > > +  std::unique_ptr<index_cache_resource> line_str_resource;
> > > > > +
> > > > > +  /* Download the .debug_line and .debug_line_str associated with OBJFILE
> > > > > +     and populate line_headers.  */
> > > > > +
> > > > > +  bool read_debug_line_from_debuginfod (objfile *objfile);
> > > > > +
> > > > > +  /* Initialize line_data and line_str_data with the .debug_line and
> > > > > +    .debug_line_str downloaded read_debug_line_from_debuginfod.  */
> > > > > +
> > > > > +  gdb::array_view<const gdb_byte> read_debug_line_separate
> > > > > +    (char *filename, std::unique_ptr<index_cache_resource> *resource);
> > > > > +};
> > > > >  #endif /* DWARF2READ_H */
> > > > > diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
> > > > > index bbd21287b28..110864adac3 100644
> > > > > --- a/gdb/mi/mi-out.c
> > > > > +++ b/gdb/mi/mi-out.c
> > > > > @@ -278,7 +278,14 @@ mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
> > > > >
> > > > >    if (info.state == progress_update::START)
> > > > >      {
> > > > > -      gdb_printf ("%s...\n", msg.c_str ());
> > > > > +      std::string prefix;
> > > > > +      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
> > > > > +       {
> > > > > +         prefix = "\n";
> > > > > +         cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
> > > > > +       }
> > > > > +
> > > > > +      gdb_printf ("%s...\n", (prefix + msg).c_str ());
> > > > >        info.state = progress_update::WORKING;
> > > > >      }
> > > > >  }
> > > > > diff --git a/gdb/testsuite/gdb.debuginfod/section.exp b/gdb/testsuite/gdb.debuginfod/section.exp
> > > > > index ff57c6e32b7..b5c6929fcf7 100644
> > > > > --- a/gdb/testsuite/gdb.debuginfod/section.exp
> > > > > +++ b/gdb/testsuite/gdb.debuginfod/section.exp
> > > > > @@ -171,6 +171,27 @@ proc_with_prefix local_url { } {
> > > > >      # during backtrace.
> > > > >      set res ".*debug info for $lib_sl1\.\.\.\r\n\#1.*"
> > > > >      gdb_test "bt" $res "break backtrace"
> > > > > +
> > > > > +    clean_restart_with_prompt $sectexec "" "line 1"
> > > > > +
> > > > > +    # List source file using .debug_line download.
> > > > > +    set res ".*\.debug_line.*$lib_sl1.*21.*extern void libsection2_test.*"
> > > > > +    gdb_test "list $libsrc1:21" $res "line 1 list"
> > > > > +
> > > > > +    clean_restart_with_prompt $sectexec "" "line 2"
> > > > > +
> > > > > +    # Set breakpoint using .debug_line download.
> > > > > +    set res ".*section \.debug_line for $lib_sl1.*Breakpoint 2 at.*$libsrc1.*"
> > > > > +    gdb_test "br $libsrc1:37" $res "line 2 br"
> > > > > +
> > > > > +    # Continue to breakpoint.
> > > > > +    set res "Breakpoint 2, libsection1_test.*\"Cancelling thread\\\\n\".*"
> > > > > +    gdb_test "c" $res "line 2 continue"
> > > > > +
> > > > > +    # Check that download progress message is correctly formatted
> > > > > +    # when printing threads.
> > > > > +    set res ".*separate debug info for $lib_sl2\.\.\.\r\n.* 2    Thread.*"
> > > > > +    gdb_test "info thr" $res "line thread"
> > > > >  }
> > > > >
> > > > >  # Create CACHE and DB directories ready for debuginfod to use.
> > > > > diff --git a/gdb/ui-out.c b/gdb/ui-out.c
> > > > > index 9f643b1ce95..fde46bfbe94 100644
> > > > > --- a/gdb/ui-out.c
> > > > > +++ b/gdb/ui-out.c
> > > > > @@ -32,6 +32,9 @@
> > > > >  #include <memory>
> > > > >  #include <string>
> > > > >
> > > > > +/* Current state of newline prefixing for progress update messages.  */
> > > > > +prefix_state_t cur_prefix_state = prefix_state_t::NEWLINE_OFF;
> > > > > +
> > > > >  namespace {
> > > > >
> > > > >  /* A header of a ui_out_table.  */
> > > > > diff --git a/gdb/ui-out.h b/gdb/ui-out.h
> > > > > index 70a7145741f..7de8796aee0 100644
> > > > > --- a/gdb/ui-out.h
> > > > > +++ b/gdb/ui-out.h
> > > > > @@ -296,6 +296,21 @@ class ui_out
> > > > >        BAR
> > > > >      };
> > > > >
> > > > > +    /* Used to communicate the status of a newline prefix for the next progress
> > > > > +       update message.  */
> > > > > +    enum prefix_state
> > > > > +    {
> > > > > +      /* Do not modify the next progress update message.  */
> > > > > +      NEWLINE_OFF,
> > > > > +
> > > > > +      /* The next progress update message should include a newline prefix.  */
> > > > > +      NEWLINE_NEEDED,
> > > > > +
> > > > > +      /* A newline prefix was included in a debuginfod progress update
> > > > > +        message.  */
> > > > > +      NEWLINE_PRINTED
> > > > > +    };
> > > > > +
> > > > >      /* SHOULD_PRINT indicates whether something should be printed for a tty.  */
> > > > >      progress_update ()
> > > > >      {
> > > > > @@ -393,6 +408,11 @@ class ui_out
> > > > >    ui_out_level *current_level () const;
> > > > >  };
> > > > >
> > > > > +typedef ui_out::progress_update::prefix_state prefix_state_t;
> > > > > +
> > > > > +/* Current state of the newline prefix.  */
> > > > > +extern prefix_state_t cur_prefix_state;
> > > > > +
> > > > >  /* Start a new tuple or list on construction, and end it on
> > > > >     destruction.  Normally this is used via the typedefs
> > > > >     ui_out_emit_tuple and ui_out_emit_list.  */
> > > > > --
> > > > > 2.41.0
> > > > >
  
Thiago Jung Bauermann Dec. 27, 2023, 12:30 a.m. UTC | #6
Aaron Merey <amerey@redhat.com> writes:

> -/* Read directory or file name entry format, starting with byte of
> -   format count entries, ULEB128 pairs of entry formats, ULEB128 of
> -   entries count and the entries themselves in the described entry
> -   format.  */
> +
> +/* Like read_formatted_entries but the .debug_line and .debug_line_str

This function is also called read_formatted_entries, so the comment is a
bit confusing to me. Perhaps start with "Like the other
read_formatted_entries, but ..."?

> +   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
> +   where these sections are read from separate files without necessarily
> +   having access to the entire debuginfo file they originate from.  */
>  
>  static void
> -read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
> -			const gdb_byte **bufp, struct line_header *lh,
> -			unsigned int offset_size,
> -			void (*callback) (struct line_header *lh,
> -					  const char *name,
> -					  dir_index d_index,
> -					  unsigned int mod_time,
> -					  unsigned int length))
> +read_formatted_entries
> +  (bfd *parent_bfd, const gdb_byte **line_bufp,
> +   const gdb::array_view<const gdb_byte> line_str_data,
> +   struct line_header *lh,
> +   unsigned int offset_size,
> +   void (*callback) (struct line_header *lh,
> +		     const char *name,
> +		     dir_index d_index,
> +		     unsigned int mod_time,
> +		     unsigned int length))
>  {

<snip>

> @@ -166,36 +173,48 @@ read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
>  	  switch (form)
>  	    {
>  	    case DW_FORM_string:
> -	      string.emplace (read_direct_string (abfd, buf, &bytes_read));
> +	      string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
>  	      buf += bytes_read;
>  	      break;
>  
>  	    case DW_FORM_line_strp:
>  	      {
> -		const char *str
> -		  = per_objfile->read_line_string (buf, offset_size);
> +		if (line_str_data.empty ())
> +		  error (_("Dwarf Error: DW_FORM_line_strp used without " \
> +			   "required section"));

Is error the correct response here? Or should the bad form just be
complained about and ignored?

> +		if (line_str_data.size () <= offset_size)

I don't understand this check. Shouldn't it be the following?

  if (line_str_data.size () <= str_offset)

> +		  error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
> +			   "of section .debug_line"));

IIUC, the message should say ".debug_line_str" instead of ".debug_line".
Also, same question about error vs complaint.

> +
> +		ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
> +
> +		const char *str;
> +		if (str_buf[str_offset] == '\0')
> +		  str = nullptr;
> +		else
> +		  str = (const char *) (str_buf + str_offset);
>  		string.emplace (str);
>  		buf += offset_size;
> +		break;
>  	      }
> -	      break;

<snip>

> +gdb::array_view<const gdb_byte>
> +mapped_debug_line::read_debug_line_separate
> +  (char *filename, std::unique_ptr<index_cache_resource> *resource)

filename can be const char *.

> +{
> +  if (filename == nullptr)
> +    return {};
> +
> +  try
> +  {
> +    line_resource_mmap *mmap_resource
> +      = new line_resource_mmap (filename);
> +
> +    resource->reset (mmap_resource);
> +
> +    return gdb::array_view<const gdb_byte>
> +      ((const gdb_byte *) mmap_resource->mapping.get (),
> +       mmap_resource->mapping.size ());
> +  }
> +  catch (const gdb_exception &except)
> +  {
> +    exception_print (gdb_stderr, except);
> +  }
> +
> +  return {};
> +}
> +
> +/* See read.h.  */
> +
> +bool
> +mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
> +{
> +  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
> +  if (build_id == nullptr)
> +    return false;
> +
> +  gdb::unique_xmalloc_ptr<char> line_path;
> +  scoped_fd line_fd = debuginfod_section_query (build_id->data,
> +						build_id->size,
> +						bfd_get_filename
> +						  (objfile->obfd.get ()),
> +						".debug_line",
> +						&line_path);
> +
> +  if (line_fd.get () < 0)
> +    return false;
> +
> +  gdb::unique_xmalloc_ptr<char> line_str_path;
> +  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
> +						    build_id->size,
> +						    bfd_get_filename
> +						      (objfile->obfd.get ()),
> +						    ".debug_line_str",
> +						    &line_str_path);
> +
> +  line_data = read_debug_line_separate (line_path.get (), &line_resource);
> +  line_str_data = read_debug_line_separate (line_str_path.get (),
> +					    &line_str_resource);
> +
> +  const gdb_byte *line_ptr = line_data.data ();
> +
> +  while (line_ptr < line_data.data () + line_data.size ())
> +    {
> +      line_header_up lh
> +	= dwarf_decode_line_header (objfile->obfd.get (),
> +				    line_data, line_str_data,
> +				    &line_ptr, false,
> +				    nullptr, nullptr);
> +      line_headers.emplace_back (lh.release ());

This line is destroying a line_header_up just to construct a new one to
put in the vector. I believe you can use std::move here instead to avoid
the extra work.

> +    }
> +
> +  return true;
> +}
> +#endif /* !HAVE_SYS_MMAN_H */
  

Patch

diff --git a/gdb/cli-out.c b/gdb/cli-out.c
index c919622d418..a570a2d939d 100644
--- a/gdb/cli-out.c
+++ b/gdb/cli-out.c
@@ -307,16 +307,23 @@  cli_ui_out::do_progress_notify (const std::string &msg,
 
   if (info.state == progress_update::START)
     {
+      std::string prefix;
+      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
+	{
+	  prefix = "\n";
+	  cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
+	}
+
       if (stream->isatty ()
 	  && current_ui->input_interactive_p ()
 	  && chars_per_line >= MIN_CHARS_PER_LINE)
 	{
-	  gdb_printf (stream, "%s\n", msg.c_str ());
+	  gdb_printf (stream, "%s\n", (prefix + msg).c_str ());
 	  info.state = progress_update::BAR;
 	}
       else
 	{
-	  gdb_printf (stream, "%s...\n", msg.c_str ());
+	  gdb_printf (stream, "%s...\n", (prefix + msg).c_str ());
 	  info.state = progress_update::WORKING;
 	}
     }
diff --git a/gdb/completer.c b/gdb/completer.c
index 2abf3998345..9c299f3eb65 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -1345,6 +1345,10 @@  complete_line_internal_1 (completion_tracker &tracker,
     {
       /* We've recognized a full command.  */
 
+      /* Disable pagination since responding to the pagination prompt
+	 overwrites rl_line_buffer.  */
+      scoped_restore pag_restore = make_scoped_restore (&pagination_enabled, false);
+
       if (p == tmp_command + point)
 	{
 	  /* There is no non-whitespace in the line beyond the
@@ -1444,7 +1448,8 @@  complete_line_internal_1 (completion_tracker &tracker,
 }
 
 /* Wrapper around complete_line_internal_1 to handle
-   MAX_COMPLETIONS_REACHED_ERROR.  */
+   MAX_COMPLETIONS_REACHED_ERROR and possible progress update
+   interactions.  */
 
 static void
 complete_line_internal (completion_tracker &tracker,
@@ -1452,6 +1457,11 @@  complete_line_internal (completion_tracker &tracker,
 			const char *line_buffer, int point,
 			complete_line_internal_reason reason)
 {
+  scoped_restore restore_prefix_state
+    = make_scoped_restore
+      (&cur_prefix_state,
+       ui_out::progress_update::prefix_state::NEWLINE_NEEDED);
+
   try
     {
       complete_line_internal_1 (tracker, text, line_buffer, point, reason);
@@ -1461,6 +1471,12 @@  complete_line_internal (completion_tracker &tracker,
       if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
 	throw;
     }
+
+  /* If progress update messages printed, then the text being completed
+     needs to be printed again.  */
+  if (cur_prefix_state
+      == ui_out::progress_update::prefix_state::NEWLINE_PRINTED)
+    rl_forced_update_display ();
 }
 
 /* See completer.h.  */
diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
index d072a91bac9..b9210d84f6b 100644
--- a/gdb/dwarf2/line-header.c
+++ b/gdb/dwarf2/line-header.c
@@ -102,50 +102,57 @@  read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
 {
   LONGEST length = read_initial_length (abfd, buf, bytes_read);
 
-  gdb_assert (cu_header->initial_length_size == 4
-	      || cu_header->initial_length_size == 8
-	      || cu_header->initial_length_size == 12);
+  if (cu_header != nullptr)
+    {
+      gdb_assert (cu_header->initial_length_size == 4
+		  || cu_header->initial_length_size == 8
+		  || cu_header->initial_length_size == 12);
 
-  if (cu_header->initial_length_size != *bytes_read)
-    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
+      if (cu_header->initial_length_size != *bytes_read)
+	complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
+    }
 
   *offset_size = (*bytes_read == 4) ? 4 : 8;
   return length;
 }
 
-/* Read directory or file name entry format, starting with byte of
-   format count entries, ULEB128 pairs of entry formats, ULEB128 of
-   entries count and the entries themselves in the described entry
-   format.  */
+
+/* Like read_formatted_entries but the .debug_line and .debug_line_str
+   are stored in LINE_BUFP and LINE_STR_DATA.  This is used for cases
+   where these sections are read from separate files without necessarily
+   having access to the entire debuginfo file they originate from.  */
 
 static void
-read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
-			const gdb_byte **bufp, struct line_header *lh,
-			unsigned int offset_size,
-			void (*callback) (struct line_header *lh,
-					  const char *name,
-					  dir_index d_index,
-					  unsigned int mod_time,
-					  unsigned int length))
+read_formatted_entries
+  (bfd *parent_bfd, const gdb_byte **line_bufp,
+   const gdb::array_view<const gdb_byte> line_str_data,
+   struct line_header *lh,
+   unsigned int offset_size,
+   void (*callback) (struct line_header *lh,
+		     const char *name,
+		     dir_index d_index,
+		     unsigned int mod_time,
+		     unsigned int length))
 {
   gdb_byte format_count, formati;
   ULONGEST data_count, datai;
-  const gdb_byte *buf = *bufp;
+  const gdb_byte *buf = *line_bufp;
+  const gdb_byte *str_buf = line_str_data.data ();
   const gdb_byte *format_header_data;
   unsigned int bytes_read;
 
-  format_count = read_1_byte (abfd, buf);
+  format_count = read_1_byte (parent_bfd, buf);
   buf += 1;
   format_header_data = buf;
   for (formati = 0; formati < format_count; formati++)
     {
-      read_unsigned_leb128 (abfd, buf, &bytes_read);
+      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
       buf += bytes_read;
-      read_unsigned_leb128 (abfd, buf, &bytes_read);
+      read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
       buf += bytes_read;
     }
 
-  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
+  data_count = read_unsigned_leb128 (parent_bfd, buf, &bytes_read);
   buf += bytes_read;
   for (datai = 0; datai < data_count; datai++)
     {
@@ -154,10 +161,10 @@  read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
 
       for (formati = 0; formati < format_count; formati++)
 	{
-	  ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
+	  ULONGEST content_type = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
 	  format += bytes_read;
 
-	  ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
+	  ULONGEST form  = read_unsigned_leb128 (parent_bfd, format, &bytes_read);
 	  format += bytes_read;
 
 	  gdb::optional<const char *> string;
@@ -166,36 +173,48 @@  read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
 	  switch (form)
 	    {
 	    case DW_FORM_string:
-	      string.emplace (read_direct_string (abfd, buf, &bytes_read));
+	      string.emplace (read_direct_string (parent_bfd, buf, &bytes_read));
 	      buf += bytes_read;
 	      break;
 
 	    case DW_FORM_line_strp:
 	      {
-		const char *str
-		  = per_objfile->read_line_string (buf, offset_size);
+		if (line_str_data.empty ())
+		  error (_("Dwarf Error: DW_FORM_line_strp used without " \
+			   "required section"));
+		if (line_str_data.size () <= offset_size)
+		  error (_("Dwarf Error: DW_FORM_line_strp pointing outside " \
+			   "of section .debug_line"));
+
+		ULONGEST str_offset = read_offset (parent_bfd, buf, offset_size);
+
+		const char *str;
+		if (str_buf[str_offset] == '\0')
+		  str = nullptr;
+		else
+		  str = (const char *) (str_buf + str_offset);
 		string.emplace (str);
 		buf += offset_size;
+		break;
 	      }
-	      break;
 
 	    case DW_FORM_data1:
-	      uint.emplace (read_1_byte (abfd, buf));
+	      uint.emplace (read_1_byte (parent_bfd, buf));
 	      buf += 1;
 	      break;
 
 	    case DW_FORM_data2:
-	      uint.emplace (read_2_bytes (abfd, buf));
+	      uint.emplace (read_2_bytes (parent_bfd, buf));
 	      buf += 2;
 	      break;
 
 	    case DW_FORM_data4:
-	      uint.emplace (read_4_bytes (abfd, buf));
+	      uint.emplace (read_4_bytes (parent_bfd, buf));
 	      buf += 4;
 	      break;
 
 	    case DW_FORM_data8:
-	      uint.emplace (read_8_bytes (abfd, buf));
+	      uint.emplace (read_8_bytes (parent_bfd, buf));
 	      buf += 8;
 	      break;
 
@@ -205,7 +224,7 @@  read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
 	      break;
 
 	    case DW_FORM_udata:
-	      uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
+	      uint.emplace (read_unsigned_leb128 (parent_bfd, buf, &bytes_read));
 	      buf += bytes_read;
 	      break;
 
@@ -248,28 +267,30 @@  read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
       callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
     }
 
-  *bufp = buf;
+  *line_bufp = buf;
 }
 
 /* See line-header.h.  */
 
 line_header_up
-dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
-			   dwarf2_per_objfile *per_objfile,
-			   struct dwarf2_section_info *section,
-			   const struct comp_unit_head *cu_header,
-			   const char *comp_dir)
+dwarf_decode_line_header (bfd *parent_bfd,
+			  gdb::array_view<const gdb_byte> line_data,
+			  gdb::array_view<const gdb_byte> line_str_data,
+			  const gdb_byte **debug_line_ptr,
+			  bool is_dwz,
+			  const struct comp_unit_head *cu_header,
+			  const char *comp_dir)
 {
-  const gdb_byte *line_ptr;
+  const gdb_byte *line_ptr, *buf;
   unsigned int bytes_read, offset_size;
   int i;
   const char *cur_dir, *cur_file;
 
-  bfd *abfd = section->get_bfd_owner ();
+  buf = *debug_line_ptr;
 
   /* Make sure that at least there's room for the total_length field.
      That could be 12 bytes long, but we're just going to fudge that.  */
-  if (to_underlying (sect_off) + 4 >= section->size)
+  if (buf + 4 >= line_data.data () + line_data.size ())
     {
       dwarf2_statement_list_fits_in_line_number_section_complaint ();
       return 0;
@@ -277,62 +298,65 @@  dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
 
   line_header_up lh (new line_header (comp_dir));
 
-  lh->sect_off = sect_off;
+  lh->sect_off = (sect_offset) (buf - line_data.data ());
   lh->offset_in_dwz = is_dwz;
 
-  line_ptr = section->buffer + to_underlying (sect_off);
+  line_ptr = buf;
 
   /* Read in the header.  */
   LONGEST unit_length
-    = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
+    = read_checked_initial_length_and_offset (parent_bfd, buf, cu_header,
 					      &bytes_read, &offset_size);
-  line_ptr += bytes_read;
 
-  const gdb_byte *start_here = line_ptr;
+  line_ptr += bytes_read;
 
-  if (line_ptr + unit_length > (section->buffer + section->size))
+  if (line_ptr + unit_length > buf + line_data.size ())
     {
       dwarf2_statement_list_fits_in_line_number_section_complaint ();
       return 0;
     }
+
+  const gdb_byte *start_here = line_ptr;
+
   lh->statement_program_end = start_here + unit_length;
-  lh->version = read_2_bytes (abfd, line_ptr);
+  lh->version = read_2_bytes (parent_bfd, line_ptr);
   line_ptr += 2;
   if (lh->version > 5)
     {
       /* This is a version we don't understand.  The format could have
 	 changed in ways we don't handle properly so just punt.  */
       complaint (_("unsupported version in .debug_line section"));
-      return NULL;
+      return nullptr;
     }
   if (lh->version >= 5)
     {
       gdb_byte segment_selector_size;
 
       /* Skip address size.  */
-      read_1_byte (abfd, line_ptr);
+      read_1_byte (parent_bfd, line_ptr);
       line_ptr += 1;
 
-      segment_selector_size = read_1_byte (abfd, line_ptr);
+      segment_selector_size = read_1_byte (parent_bfd, line_ptr);
       line_ptr += 1;
       if (segment_selector_size != 0)
 	{
 	  complaint (_("unsupported segment selector size %u "
 		       "in .debug_line section"),
 		     segment_selector_size);
-	  return NULL;
+	  return nullptr;
 	}
     }
 
-  LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
+  LONGEST header_length = read_offset (parent_bfd, line_ptr, offset_size);
   line_ptr += offset_size;
   lh->statement_program_start = line_ptr + header_length;
-  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
+
+  lh->minimum_instruction_length = read_1_byte (parent_bfd, line_ptr);
   line_ptr += 1;
 
   if (lh->version >= 4)
     {
-      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
+      lh->maximum_ops_per_instruction = read_1_byte (parent_bfd, line_ptr);
       line_ptr += 1;
     }
   else
@@ -345,41 +369,47 @@  dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
 		   "in `.debug_line' section"));
     }
 
-  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
+  lh->default_is_stmt = read_1_byte (parent_bfd, line_ptr);
   line_ptr += 1;
-  lh->line_base = read_1_signed_byte (abfd, line_ptr);
+
+  lh->line_base = read_1_signed_byte (parent_bfd, line_ptr);
   line_ptr += 1;
-  lh->line_range = read_1_byte (abfd, line_ptr);
+
+  lh->line_range = read_1_byte (parent_bfd, line_ptr);
   line_ptr += 1;
-  lh->opcode_base = read_1_byte (abfd, line_ptr);
+
+  lh->opcode_base = read_1_byte (parent_bfd, line_ptr);
   line_ptr += 1;
+
   lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
 
   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
   for (i = 1; i < lh->opcode_base; ++i)
     {
-      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
+      lh->standard_opcode_lengths[i] = read_1_byte (parent_bfd, line_ptr);
       line_ptr += 1;
     }
 
   if (lh->version >= 5)
     {
       /* Read directory table.  */
-      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
-			      offset_size,
-			      [] (struct line_header *header, const char *name,
-				  dir_index d_index, unsigned int mod_time,
-				  unsigned int length)
+      read_formatted_entries
+	(parent_bfd, &line_ptr, line_str_data,
+	 lh.get (), offset_size,
+	 [] (struct line_header *header, const char *name,
+	     dir_index d_index, unsigned int mod_time,
+	     unsigned int length)
 	{
 	  header->add_include_dir (name);
 	});
 
       /* Read file name table.  */
-      read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
-			      offset_size,
-			      [] (struct line_header *header, const char *name,
-				  dir_index d_index, unsigned int mod_time,
-				  unsigned int length)
+      read_formatted_entries
+	(parent_bfd, &line_ptr, line_str_data,
+	 lh.get (), offset_size,
+	 [] (struct line_header *header, const char *name,
+	     dir_index d_index, unsigned int mod_time,
+	     unsigned int length)
 	{
 	  header->add_file_name (name, d_index, mod_time, length);
 	});
@@ -387,7 +417,7 @@  dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
   else
     {
       /* Read directory table.  */
-      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
+      while ((cur_dir = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
 	{
 	  line_ptr += bytes_read;
 	  lh->add_include_dir (cur_dir);
@@ -395,17 +425,17 @@  dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
       line_ptr += bytes_read;
 
       /* Read file name table.  */
-      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
+      while ((cur_file = read_direct_string (parent_bfd, line_ptr, &bytes_read)) != nullptr)
 	{
 	  unsigned int mod_time, length;
 	  dir_index d_index;
 
 	  line_ptr += bytes_read;
-	  d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
+	  d_index = (dir_index) read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
 	  line_ptr += bytes_read;
-	  mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
+	  mod_time = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
 	  line_ptr += bytes_read;
-	  length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
+	  length = read_unsigned_leb128 (parent_bfd, line_ptr, &bytes_read);
 	  line_ptr += bytes_read;
 
 	  lh->add_file_name (cur_file, d_index, mod_time, length);
@@ -413,9 +443,40 @@  dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
       line_ptr += bytes_read;
     }
 
-  if (line_ptr > (section->buffer + section->size))
+  if (line_ptr > (buf + line_data.size ()))
     complaint (_("line number info header doesn't "
 		 "fit in `.debug_line' section"));
 
+  *debug_line_ptr += unit_length + offset_size;
   return lh;
 }
+
+line_header_up
+dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
+			   dwarf2_per_objfile *per_objfile,
+			   struct dwarf2_section_info *section,
+			   const struct comp_unit_head *cu_header,
+			   const char *comp_dir)
+{
+  struct objfile *objfile = per_objfile->objfile;
+  struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
+
+  /* Read .debug_line.  */
+  dwarf2_section_info *line_sec = &per_bfd->line;
+  bfd_size_type line_size = line_sec->get_size (objfile);
+
+  gdb::array_view<const gdb_byte> line (line_sec->buffer, line_size);
+
+  /* Read .debug_line_str.  */
+  dwarf2_section_info *line_str_sec = &per_bfd->line_str;
+  bfd_size_type line_str_size = line_str_sec->get_size (objfile);
+
+  gdb::array_view<const gdb_byte> line_str (line_str_sec->buffer,
+					    line_str_size);
+
+  const gdb_byte *line_ptr = line.data () + to_underlying (sect_off);
+
+  return dwarf_decode_line_header
+    (per_bfd->obfd, line, line_str, &line_ptr,
+     is_dwz, cu_header, comp_dir);
+}
diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
index 06d2eec573b..22db9f9aa78 100644
--- a/gdb/dwarf2/line-header.h
+++ b/gdb/dwarf2/line-header.h
@@ -217,4 +217,14 @@  extern line_header_up dwarf_decode_line_header
    struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
    const char *comp_dir);
 
+/* Like above but the .debug_line and .debug_line_str are stored in
+   LINE_DATA and LINE_STR_DATA. *DEBUG_LINE_PTR should point to a
+   statement program header within LINE_DATA.  */
+
+extern line_header_up dwarf_decode_line_header
+  (bfd *parent_bfd, gdb::array_view<const gdb_byte> line_data,
+   gdb::array_view<const gdb_byte> line_str_data,
+   const gdb_byte **debug_line_ptr, bool is_dwz,
+  const comp_unit_head *cu_header, const char *comp_dir);
+
 #endif /* DWARF2_LINE_HEADER_H */
diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
index da88a8b405c..c0e51357ce2 100644
--- a/gdb/dwarf2/read-gdb-index.c
+++ b/gdb/dwarf2/read-gdb-index.c
@@ -131,6 +131,9 @@  struct mapped_gdb_index final : public mapped_index_base
   }
 };
 
+struct mapped_debug_line;
+typedef std::unique_ptr<mapped_debug_line> mapped_debug_line_up;
+
 struct dwarf2_gdb_index : public dwarf2_base_index_functions
 {
   /* This dumps minimal information about the index.
@@ -175,6 +178,15 @@  struct dwarf2_gdb_index : public dwarf2_base_index_functions
      domain_enum domain,
      enum search_domain kind);
 
+ /* If OBJFILE's debuginfo download has been deferred, use a mapped_debug_line
+    to generate filenames.
+
+    Otherwise call dwarf2_base_index_functions::map_symbol_filenames.  */
+
+  void map_symbol_filenames (struct objfile *objfile,
+			     gdb::function_view<symbol_filename_ftype> fun,
+			     bool need_fullname) override;
+
   /* Calls dwarf2_base_index_functions::expand_all_symtabs and downloads
      debuginfo if necessary.  */
   void expand_all_symtabs (struct objfile *objfile) override;
@@ -182,6 +194,15 @@  struct dwarf2_gdb_index : public dwarf2_base_index_functions
   /* Calls dwarf2_base_index_functions::find_last_source_symtab and downloads
      debuginfo if necessary.  */
   struct symtab *find_last_source_symtab (struct objfile *objfile) override;
+
+  /* Filename information related to this .gdb_index.  */
+  mapped_debug_line_up mdl;
+
+  /* Return true if any of the filenames in this .gdb_index's .debug_line
+     mapping match FILE_MATCHER.  Initializes the mapping if necessary.  */
+  bool filename_in_debug_line
+    (objfile *objfile,
+     gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
 };
 
 void
@@ -217,6 +238,30 @@  dwarf2_gdb_index::find_last_source_symtab (struct objfile *objfile)
     }
 }
 
+void
+dwarf2_gdb_index::map_symbol_filenames
+     (struct objfile *objfile,
+      gdb::function_view<symbol_filename_ftype> fun,
+      bool need_fullname)
+{
+  try
+    {
+      dwarf2_base_index_functions::map_symbol_filenames (objfile, fun,
+							 need_fullname);
+    }
+  catch (const gdb_exception &e)
+    {
+      if ((objfile->flags & OBJF_DOWNLOAD_DEFERRED) == 0)
+	exception_print (gdb_stderr, e);
+      else
+	{
+	  if (mdl == nullptr)
+	    mdl.reset (new mapped_debug_line (objfile));
+	  mdl->map_filenames (fun, need_fullname);
+	}
+    }
+}
+
 /* This dumps minimal information about the index.
    It is called via "mt print objfiles".
    One use is to verify .gdb_index has been loaded by the
@@ -590,6 +635,17 @@  dwarf2_gdb_index::do_expand_symtabs_matching
   return result;
 }
 
+bool
+dwarf2_gdb_index::filename_in_debug_line
+  (objfile *objfile,
+   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
+{
+  if (mdl == nullptr)
+    mdl.reset (new mapped_debug_line (objfile));
+
+  return mdl->contains_matching_filename (file_matcher);
+}
+
 bool
 dwarf2_gdb_index::expand_symtabs_matching
     (struct objfile *objfile,
@@ -618,6 +674,10 @@  dwarf2_gdb_index::expand_symtabs_matching
 	  return false;
 	}
 
+      if (file_matcher != nullptr
+	  && !filename_in_debug_line (objfile, file_matcher))
+	return true;
+
       read_full_dwarf_from_debuginfod (objfile, this);
       return true;
     }
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 0c5689c63ef..876e3aedcf1 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -81,6 +81,7 @@ 
 #include "gdbsupport/gdb_optional.h"
 #include "gdbsupport/underlying.h"
 #include "gdbsupport/hash_enum.h"
+#include "gdbsupport/scoped_mmap.h"
 #include "filename-seen-cache.h"
 #include "producer.h"
 #include <fcntl.h>
@@ -2135,6 +2136,213 @@  dw2_get_file_names (dwarf2_per_cu_data *this_cu,
   return this_cu->file_names;
 }
 
+#if !HAVE_SYS_MMAN_H
+
+bool
+mapped_debug_line::contains_matching_filename
+  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
+{
+  return false;
+}
+
+gdb::array_view<const gdb_byte>
+mapped_debug_line::read_debug_line_separate
+  (char *filename, std::unique_ptr<index_cache_resource> *resource)
+{
+  return {};
+}
+
+bool
+mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
+{
+  return false;
+}
+
+void
+mapped_debug_line::map_filenames
+  (gdb::function_view<symbol_filename_ftype> fun,
+   bool need_fullname)
+{
+  return;
+}
+
+#else /* !HAVE_SYS_MMAN_H */
+
+struct line_resource_mmap final : public index_cache_resource
+{
+  /* Try to mmap FILENAME.  Throw an exception on failure, including if the
+     file doesn't exist. */
+  line_resource_mmap (const char *filename)
+    : mapping (mmap_file (filename))
+  {}
+
+  scoped_mmap mapping;
+};
+
+/* See read.h.  */
+
+bool
+mapped_debug_line::contains_matching_filename
+  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
+{
+  for (line_header_up &lh : line_headers)
+    for (file_entry &fe : lh->file_names ())
+      {
+	const char *filename = fe.name;
+
+	if (file_matcher (fe.name, false))
+	  return true;
+
+	bool basename_match = file_matcher (lbasename (fe.name), true);
+
+	if (!basenames_may_differ && !basename_match)
+	  continue;
+
+	/* DW_AT_comp_dir is not explicitly mentioned in the .debug_line
+	   until DWARF5.  Since we don't have access to the CU at this
+	   point we just check for a partial match on the filename.
+	   If there is a match, the full debuginfo will be downloaded
+	   ane the match will be re-evalute with DW_AT_comp_dir.  */
+	if (lh->version < 5 && fe.d_index == 0)
+	  return basename_match;
+
+	const char *dirname = fe.include_dir (&*lh);
+	std::string fullname;
+
+	if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
+	  fullname = filename;
+	else
+	  fullname = std::string (dirname) + SLASH_STRING + filename;
+
+	gdb::unique_xmalloc_ptr<char> rewritten
+	  = rewrite_source_path (fullname.c_str ());
+	if (rewritten != nullptr)
+	  fullname = rewritten.release ();
+
+	if (file_matcher (fullname.c_str (), false))
+	  return true;
+      }
+
+  return false;
+}
+
+/* See read.h.  */
+
+void
+mapped_debug_line::map_filenames
+  (gdb::function_view<symbol_filename_ftype> fun,
+   bool need_fullname)
+{
+  for (line_header_up &lh : line_headers)
+    for (file_entry &fe : lh->file_names ())
+      {
+	const char *filename = fe.name;
+
+	if (!need_fullname)
+	  {
+	    fun (filename, nullptr);
+	    continue;
+	  }
+
+	const char *dirname = fe.include_dir (&*lh);
+	std::string fullname;
+
+	if (dirname == nullptr || IS_ABSOLUTE_PATH (filename))
+	  fullname = filename;
+	else
+	  fullname = std::string (dirname) + SLASH_STRING + filename;
+
+	gdb::unique_xmalloc_ptr<char> rewritten
+	  = rewrite_source_path (fullname.c_str ());
+	if (rewritten != nullptr)
+	  fullname = rewritten.release ();
+
+	fun (filename, fullname.c_str ());
+      }
+}
+
+/* See read.h.  */
+
+gdb::array_view<const gdb_byte>
+mapped_debug_line::read_debug_line_separate
+  (char *filename, std::unique_ptr<index_cache_resource> *resource)
+{
+  if (filename == nullptr)
+    return {};
+
+  try
+  {
+    line_resource_mmap *mmap_resource
+      = new line_resource_mmap (filename);
+
+    resource->reset (mmap_resource);
+
+    return gdb::array_view<const gdb_byte>
+      ((const gdb_byte *) mmap_resource->mapping.get (),
+       mmap_resource->mapping.size ());
+  }
+  catch (const gdb_exception &except)
+  {
+    exception_print (gdb_stderr, except);
+  }
+
+  return {};
+}
+
+/* See read.h.  */
+
+bool
+mapped_debug_line::read_debug_line_from_debuginfod (objfile *objfile)
+{
+  const bfd_build_id *build_id = build_id_bfd_get (objfile->obfd.get ());
+  if (build_id == nullptr)
+    return false;
+
+  gdb::unique_xmalloc_ptr<char> line_path;
+  scoped_fd line_fd = debuginfod_section_query (build_id->data,
+						build_id->size,
+						bfd_get_filename
+						  (objfile->obfd.get ()),
+						".debug_line",
+						&line_path);
+
+  if (line_fd.get () < 0)
+    return false;
+
+  gdb::unique_xmalloc_ptr<char> line_str_path;
+  scoped_fd line_str_fd = debuginfod_section_query (build_id->data,
+						    build_id->size,
+						    bfd_get_filename
+						      (objfile->obfd.get ()),
+						    ".debug_line_str",
+						    &line_str_path);
+
+  line_data = read_debug_line_separate (line_path.get (), &line_resource);
+  line_str_data = read_debug_line_separate (line_str_path.get (),
+					    &line_str_resource);
+
+  const gdb_byte *line_ptr = line_data.data ();
+
+  while (line_ptr < line_data.data () + line_data.size ())
+    {
+      line_header_up lh
+	= dwarf_decode_line_header (objfile->obfd.get (),
+				    line_data, line_str_data,
+				    &line_ptr, false,
+				    nullptr, nullptr);
+      line_headers.emplace_back (lh.release ());
+    }
+
+  return true;
+}
+#endif /* !HAVE_SYS_MMAN_H */
+
+mapped_debug_line::mapped_debug_line (objfile *objfile)
+{
+  if (!read_debug_line_from_debuginfod (objfile))
+    line_headers.clear ();
+}
+
 /* A helper for the "quick" functions which computes and caches the
    real path for a given file name from the line table.  */
 
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index 6ed0be7203b..49fea22c092 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -33,6 +33,7 @@ 
 #include "gdbsupport/hash_enum.h"
 #include "gdbsupport/function-view.h"
 #include "gdbsupport/packed.h"
+#include "dwarf2/line-header.h"
 
 /* Hold 'maintenance (set|show) dwarf' commands.  */
 extern struct cmd_list_element *set_dwarf_cmdlist;
@@ -969,4 +970,40 @@  extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
 extern void read_full_dwarf_from_debuginfod (struct objfile *,
 					     dwarf2_base_index_functions *);
 
+struct mapped_debug_line
+{
+  mapped_debug_line (objfile *objfile);
+
+  /* Return true if any of the mapped .debug_line's filenames match
+     FILE_MATCHER.  */
+
+  bool contains_matching_filename
+    (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
+
+  /* Call FUN with each filename in this mapped .debug_line.  Include
+     each file's fullname if NEED_FULLNAME is true.  */
+
+  void map_filenames (gdb::function_view<symbol_filename_ftype> fun,
+		      bool need_fullname);
+
+private:
+  std::vector<line_header_up> line_headers;
+
+  gdb::array_view<const gdb_byte> line_data;
+  gdb::array_view<const gdb_byte> line_str_data;
+
+  std::unique_ptr<index_cache_resource> line_resource;
+  std::unique_ptr<index_cache_resource> line_str_resource;
+
+  /* Download the .debug_line and .debug_line_str associated with OBJFILE
+     and populate line_headers.  */
+
+  bool read_debug_line_from_debuginfod (objfile *objfile);
+
+  /* Initialize line_data and line_str_data with the .debug_line and
+    .debug_line_str downloaded read_debug_line_from_debuginfod.  */
+
+  gdb::array_view<const gdb_byte> read_debug_line_separate
+    (char *filename, std::unique_ptr<index_cache_resource> *resource);
+};
 #endif /* DWARF2READ_H */
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index bbd21287b28..110864adac3 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -278,7 +278,14 @@  mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
 
   if (info.state == progress_update::START)
     {
-      gdb_printf ("%s...\n", msg.c_str ());
+      std::string prefix;
+      if (cur_prefix_state == prefix_state_t::NEWLINE_NEEDED)
+	{
+	  prefix = "\n";
+	  cur_prefix_state = prefix_state_t::NEWLINE_PRINTED;
+	}
+
+      gdb_printf ("%s...\n", (prefix + msg).c_str ());
       info.state = progress_update::WORKING;
     }
 }
diff --git a/gdb/testsuite/gdb.debuginfod/section.exp b/gdb/testsuite/gdb.debuginfod/section.exp
index ff57c6e32b7..b5c6929fcf7 100644
--- a/gdb/testsuite/gdb.debuginfod/section.exp
+++ b/gdb/testsuite/gdb.debuginfod/section.exp
@@ -171,6 +171,27 @@  proc_with_prefix local_url { } {
     # during backtrace.
     set res ".*debug info for $lib_sl1\.\.\.\r\n\#1.*"
     gdb_test "bt" $res "break backtrace"
+
+    clean_restart_with_prompt $sectexec "" "line 1"
+
+    # List source file using .debug_line download.
+    set res ".*\.debug_line.*$lib_sl1.*21.*extern void libsection2_test.*"
+    gdb_test "list $libsrc1:21" $res "line 1 list"
+
+    clean_restart_with_prompt $sectexec "" "line 2"
+
+    # Set breakpoint using .debug_line download.
+    set res ".*section \.debug_line for $lib_sl1.*Breakpoint 2 at.*$libsrc1.*"
+    gdb_test "br $libsrc1:37" $res "line 2 br"
+
+    # Continue to breakpoint.
+    set res "Breakpoint 2, libsection1_test.*\"Cancelling thread\\\\n\".*"
+    gdb_test "c" $res "line 2 continue"
+
+    # Check that download progress message is correctly formatted
+    # when printing threads.
+    set res ".*separate debug info for $lib_sl2\.\.\.\r\n.* 2    Thread.*"
+    gdb_test "info thr" $res "line thread"
 }
 
 # Create CACHE and DB directories ready for debuginfod to use.
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 9f643b1ce95..fde46bfbe94 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -32,6 +32,9 @@ 
 #include <memory>
 #include <string>
 
+/* Current state of newline prefixing for progress update messages.  */
+prefix_state_t cur_prefix_state = prefix_state_t::NEWLINE_OFF;
+
 namespace {
 
 /* A header of a ui_out_table.  */
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 70a7145741f..7de8796aee0 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -296,6 +296,21 @@  class ui_out
       BAR
     };
 
+    /* Used to communicate the status of a newline prefix for the next progress
+       update message.  */
+    enum prefix_state
+    {
+      /* Do not modify the next progress update message.  */
+      NEWLINE_OFF,
+
+      /* The next progress update message should include a newline prefix.  */
+      NEWLINE_NEEDED,
+
+      /* A newline prefix was included in a debuginfod progress update
+	 message.  */
+      NEWLINE_PRINTED
+    };
+
     /* SHOULD_PRINT indicates whether something should be printed for a tty.  */
     progress_update ()
     {
@@ -393,6 +408,11 @@  class ui_out
   ui_out_level *current_level () const;
 };
 
+typedef ui_out::progress_update::prefix_state prefix_state_t;
+
+/* Current state of the newline prefix.  */
+extern prefix_state_t cur_prefix_state;
+
 /* Start a new tuple or list on construction, and end it on
    destruction.  Normally this is used via the typedefs
    ui_out_emit_tuple and ui_out_emit_list.  */