2018-10-10 12:21:05 +08:00
|
|
|
/* Cache of styled source file text
|
2024-01-12 23:30:44 +08:00
|
|
|
Copyright (C) 2018-2024 Free Software Foundation, Inc.
|
2018-10-10 12:21:05 +08:00
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#ifndef SOURCE_CACHE_H
|
|
|
|
#define SOURCE_CACHE_H
|
|
|
|
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
/* This caches two things related to source files.
|
|
|
|
|
|
|
|
First, it caches highlighted source text, keyed by the source
|
|
|
|
file's full name. A size-limited LRU cache is used.
|
2018-10-10 12:21:05 +08:00
|
|
|
|
|
|
|
Highlighting depends on the GNU Source Highlight library. When not
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
available or when highlighting fails for some reason, this cache
|
|
|
|
will instead store the un-highlighted source text.
|
|
|
|
|
|
|
|
Second, this will cache the file offsets corresponding to the start
|
|
|
|
of each line of a source file. This cache is not size-limited. */
|
2018-10-10 12:21:05 +08:00
|
|
|
class source_cache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
source_cache ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
/* This returns the vector of file offsets for the symtab S,
|
|
|
|
computing the vector first if needed.
|
|
|
|
|
|
|
|
On failure, returns false.
|
|
|
|
|
|
|
|
On success, returns true and sets *OFFSETS. This pointer is not
|
|
|
|
guaranteed to remain valid across other calls to get_source_lines
|
|
|
|
or get_line_charpos. */
|
|
|
|
bool get_line_charpos (struct symtab *s,
|
|
|
|
const std::vector<off_t> **offsets);
|
|
|
|
|
2018-10-10 12:21:05 +08:00
|
|
|
/* Get the source text for the source file in symtab S. FIRST_LINE
|
|
|
|
and LAST_LINE are the first and last lines to return; line
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
numbers are 1-based. If the file cannot be read, or if the line
|
|
|
|
numbers are out of range, false is returned. Otherwise,
|
|
|
|
LINES_OUT is set to the desired text. The returned text may
|
|
|
|
include ANSI terminal escapes. */
|
2018-10-10 12:21:05 +08:00
|
|
|
bool get_source_lines (struct symtab *s, int first_line,
|
2019-01-22 05:17:32 +08:00
|
|
|
int last_line, std::string *lines_out);
|
2018-10-10 12:21:05 +08:00
|
|
|
|
|
|
|
/* Remove all the items from the source cache. */
|
|
|
|
void clear ()
|
|
|
|
{
|
|
|
|
m_source_map.clear ();
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
m_offset_cache.clear ();
|
2023-10-17 17:38:06 +08:00
|
|
|
m_no_styling_files.clear ();
|
2018-10-10 12:21:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/* One element in the cache. */
|
|
|
|
struct source_text
|
|
|
|
{
|
|
|
|
/* The full name of the file. */
|
|
|
|
std::string fullname;
|
|
|
|
/* The contents of the file. */
|
|
|
|
std::string contents;
|
|
|
|
};
|
|
|
|
|
2019-07-23 02:41:23 +08:00
|
|
|
/* A helper function for get_source_lines reads a source file.
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
Returns the contents of the file; or throws an exception on
|
|
|
|
error. This also updates m_offset_cache. */
|
|
|
|
std::string get_plain_source_lines (struct symtab *s,
|
|
|
|
const std::string &fullname);
|
2018-10-10 12:21:05 +08:00
|
|
|
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
/* A helper function that the data for the given symtab is entered
|
|
|
|
into both caches. Returns false on error. */
|
|
|
|
bool ensure (struct symtab *s);
|
|
|
|
|
|
|
|
/* The contents of the source text cache. */
|
2018-10-10 12:21:05 +08:00
|
|
|
std::vector<source_text> m_source_map;
|
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.
2019-07-23 04:31:43 +08:00
|
|
|
|
|
|
|
/* The file offset cache. The key is the full name of the source
|
|
|
|
file. */
|
|
|
|
std::unordered_map<std::string, std::vector<off_t>> m_offset_cache;
|
2023-10-17 17:38:06 +08:00
|
|
|
|
|
|
|
/* The list of files where styling failed. */
|
|
|
|
std::unordered_set<std::string> m_no_styling_files;
|
2018-10-10 12:21:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The global source cache. */
|
|
|
|
extern source_cache g_source_cache;
|
|
|
|
|
|
|
|
#endif /* SOURCE_CACHE_H */
|