aboutsummaryrefslogtreecommitdiff
path: root/gdb/source.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2019-07-22 14:31:43 -0600
committerTom Tromey <tromey@adacore.com>2019-08-06 08:04:33 -0600
commitcb44333d99548bbbf7be06387a31877ee9322ab4 (patch)
tree8ac25ea16ebbc5556d5f1e24940e729169d08bb8 /gdb/source.c
parent872dceaaff9b54764b8f510b549497b9d904b136 (diff)
downloadgdb-cb44333d99548bbbf7be06387a31877ee9322ab4.zip
gdb-cb44333d99548bbbf7be06387a31877ee9322ab4.tar.gz
gdb-cb44333d99548bbbf7be06387a31877ee9322ab4.tar.bz2
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.
Diffstat (limited to 'gdb/source.c')
-rw-r--r--gdb/source.c120
1 files changed, 18 insertions, 102 deletions
diff --git a/gdb/source.c b/gdb/source.c
index a83e55e..e0050f1 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -357,11 +357,6 @@ forget_cached_source_info_for_objfile (struct objfile *objfile)
{
for (symtab *s : compunit_filetabs (cu))
{
- if (s->line_charpos != NULL)
- {
- xfree (s->line_charpos);
- s->line_charpos = NULL;
- }
if (s->fullname != NULL)
{
xfree (s->fullname);
@@ -642,9 +637,10 @@ info_source_command (const char *ignore, int from_tty)
printf_filtered (_("Compilation directory is %s\n"), SYMTAB_DIRNAME (s));
if (s->fullname)
printf_filtered (_("Located in %s\n"), s->fullname);
- if (s->nlines)
- printf_filtered (_("Contains %d line%s.\n"), s->nlines,
- s->nlines == 1 ? "" : "s");
+ const std::vector<off_t> *offsets;
+ if (g_source_cache.get_line_charpos (s, &offsets))
+ printf_filtered (_("Contains %d line%s.\n"), (int) offsets->size (),
+ offsets->size () == 1 ? "" : "s");
printf_filtered (_("Source language is %s.\n"), language_str (s->language));
printf_filtered (_("Producer is %s.\n"),
@@ -1123,92 +1119,6 @@ symtab_to_filename_for_display (struct symtab *symtab)
else
internal_error (__FILE__, __LINE__, _("invalid filename_display_string"));
}
-
-/* Create and initialize the table S->line_charpos that records
- the positions of the lines in the source file, which is assumed
- to be open on descriptor DESC.
- All set S->nlines to the number of such lines. */
-
-static void
-find_source_lines (struct symtab *s, int desc)
-{
- struct stat st;
- char *p, *end;
- int nlines = 0;
- int lines_allocated = 1000;
- int *line_charpos;
- long mtime = 0;
- int size;
-
- gdb_assert (s);
- line_charpos = XNEWVEC (int, lines_allocated);
- if (fstat (desc, &st) < 0)
- perror_with_name (symtab_to_filename_for_display (s));
-
- if (SYMTAB_OBJFILE (s) != NULL && SYMTAB_OBJFILE (s)->obfd != NULL)
- mtime = SYMTAB_OBJFILE (s)->mtime;
- else if (exec_bfd)
- mtime = exec_bfd_mtime;
-
- if (mtime && mtime < st.st_mtime)
- warning (_("Source file is more recent than executable."));
-
- {
- /* st_size might be a large type, but we only support source files whose
- size fits in an int. */
- size = (int) st.st_size;
-
- /* Use the heap, not the stack, because this may be pretty large,
- and we may run into various kinds of limits on stack size. */
- gdb::def_vector<char> data (size);
-
- /* Reassign `size' to result of read for systems where \r\n -> \n. */
- size = myread (desc, data.data (), size);
- if (size < 0)
- perror_with_name (symtab_to_filename_for_display (s));
- end = data.data () + size;
- p = &data[0];
- line_charpos[0] = 0;
- nlines = 1;
- while (p != end)
- {
- if (*p++ == '\n'
- /* A newline at the end does not start a new line. */
- && p != end)
- {
- if (nlines == lines_allocated)
- {
- lines_allocated *= 2;
- line_charpos =
- (int *) xrealloc ((char *) line_charpos,
- sizeof (int) * lines_allocated);
- }
- line_charpos[nlines++] = p - data.data ();
- }
- }
- }
-
- s->nlines = nlines;
- s->line_charpos =
- (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
-
-}
-
-
-
-/* See source.h. */
-
-scoped_fd
-open_source_file_with_line_charpos (struct symtab *s)
-{
- scoped_fd fd (open_source_file (s));
- if (fd.get () < 0)
- return fd;
-
- if (s->line_charpos == nullptr)
- find_source_lines (s, fd.get ());
- return fd;
-}
@@ -1308,8 +1218,13 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
std::string lines;
if (!g_source_cache.get_source_lines (s, line, stopline - 1, &lines))
- error (_("Line number %d out of range; %s has %d lines."),
- line, symtab_to_filename_for_display (s), s->nlines);
+ {
+ const std::vector<off_t> *offsets = nullptr;
+ g_source_cache.get_line_charpos (s, &offsets);
+ error (_("Line number %d out of range; %s has %d lines."),
+ line, symtab_to_filename_for_display (s),
+ offsets == nullptr ? 0 : (int) offsets->size ());
+ }
const char *iter = lines.c_str ();
while (nlines-- > 0 && *iter != '\0')
@@ -1524,7 +1439,7 @@ search_command_helper (const char *regex, int from_tty, bool forward)
if (current_source_symtab == 0)
select_source_symtab (0);
- scoped_fd desc (open_source_file_with_line_charpos (current_source_symtab));
+ scoped_fd desc (open_source_file (current_source_symtab));
if (desc.get () < 0)
perror_with_name (symtab_to_filename_for_display (current_source_symtab));
@@ -1532,11 +1447,13 @@ search_command_helper (const char *regex, int from_tty, bool forward)
? last_line_listed + 1
: last_line_listed - 1);
- if (line < 1 || line > current_source_symtab->nlines)
+ const std::vector<off_t> *offsets;
+ if (line < 1
+ || !g_source_cache.get_line_charpos (current_source_symtab, &offsets)
+ || line > offsets->size ())
error (_("Expression not found"));
- if (lseek (desc.get (), current_source_symtab->line_charpos[line - 1], 0)
- < 0)
+ if (lseek (desc.get (), (*offsets)[line - 1], 0) < 0)
perror_with_name (symtab_to_filename_for_display (current_source_symtab));
gdb_file_up stream = desc.to_file (FDOPEN_MODE);
@@ -1585,8 +1502,7 @@ search_command_helper (const char *regex, int from_tty, bool forward)
line--;
if (line < 1)
break;
- if (fseek (stream.get (),
- current_source_symtab->line_charpos[line - 1], 0) < 0)
+ if (fseek (stream.get (), (*offsets)[line - 1], 0) < 0)
{
const char *filename
= symtab_to_filename_for_display (current_source_symtab);