Add file offsets to the source cache

Currently, gdb stores the number of lines and an array of file offsets
for the start of each line in struct symtab.  This patch moves this
information to the source cache.  This has two benefits.

First, it allows gdb to read a source file less frequently.
Currently, a source file may be read multiple times: once when
computing the file offsets, once when highlighting, and then pieces
may be read again while printing source lines.  With this change, the
file is read once for its source text and file offsets; and then
perhaps read again if it is evicted from the cache.

Second, if multiple symtabs cover the same source file, then this will
share the file offsets between them.  I'm not sure whether this
happens in practice.

gdb/ChangeLog
2019-08-06  Tom Tromey  <tromey@adacore.com>

	* annotate.c (annotate_source_line): Use g_source_cache.
	* source-cache.c (source_cache::get_plain_source_lines): Change
	parameters.  Populate m_offset_cache.
	(source_cache::ensure): New method.
	(source_cache::get_line_charpos): New method.
	(extract_lines): Move lower.  Change parameters.
	(source_cache::get_source_lines): Move lower.
	* source-cache.h (class source_cache): Update comment.
	<get_line_charpos>: New method.
	<get_source_lines>: Update comment.
	<clear>: Clear m_offset_cache.
	<get_plain_source_lines>: Change parameters.
	<ensure>: New method
	<m_offset_cache>: New member.
	* source.c (forget_cached_source_info_for_objfile): Update.
	(info_source_command): Use g_source_cache.
	(find_source_lines, open_source_file_with_line_charpos): Remove.
	(print_source_lines_base, search_command_helper): Use g_source_cache.
	* source.h (open_source_file_with_line_charpos): Don't declare.
	* symtab.h (struct symtab) <nlines, line_charpos>: Remove.
	* tui/tui-source.c (tui_source_window::do_scroll_vertical):
	Use g_source_cache.
This commit is contained in:
Tom Tromey
2019-07-22 14:31:43 -06:00
parent 872dceaaff
commit cb44333d99
8 changed files with 229 additions and 193 deletions

View File

@ -28,6 +28,7 @@
#include "top.h"
#include "source.h"
#include "objfiles.h"
#include "source-cache.h"
/* Prototypes for local functions. */
@ -440,15 +441,15 @@ annotate_source_line (struct symtab *s, int line, int mid_statement,
{
if (annotation_level > 0)
{
if (s->line_charpos == nullptr)
open_source_file_with_line_charpos (s);
if (s->fullname == nullptr)
return;
/* Don't index off the end of the line_charpos array. */
if (line > s->nlines)
const std::vector<off_t> *offsets;
if (!g_source_cache.get_line_charpos (s, &offsets))
return;
annotate_source (s->fullname, line, s->line_charpos[line - 1],
/* Don't index off the end of the line_charpos array. */
if (line > offsets->size ())
return;
annotate_source (s->fullname, line, (int) (*offsets)[line - 1],
mid_statement, get_objfile_arch (SYMTAB_OBJFILE (s)),
pc);
}