forked from Imagelibrary/binutils-gdb
Currently GDB's source cache doesn't track whether the entries within
the cache are styled or not. This is pretty much fine, the assumption
is that any time we are fetching source code, we do so in order to
print it to the terminal, so where possible we always want styling
applied, and if styling is not applied, then it is because that file
cannot be styled for some reason.
Changes to 'set style enabled' cause the source cache to be flushed,
so future calls to fetch source code will regenerate the cache entries
with styling enabled or not as appropriate.
But this all assumes that styling is either on or off, and that
switching between these two states isn't done very often.
However, the Python API allows for individual commands to be executed
with styling turned off via gdb.execute(). See commit:
commit e5348a7ab3
Date: Thu Feb 13 15:39:31 2025 +0000
gdb/python: new styling argument to gdb.execute
Currently the source cache doesn't handle this case. Consider this:
(gdb) list main
... snip, styled source code displayed here ...
(gdb) python gdb.execute("list main", True, False, False)
... snip, styled source code is still shown here ...
In the second case, the final `False` passed to gdb.execute() is
asking for unstyled output.
The problem is that, `get_source_lines` calls `ensure` to prime the
cache for the file in question, then `extract_lines` just pulls the
lines of interest from the cached contents.
In `ensure`, if there is a cache entry for the desired filename, then
that is considered good enough. There is no consideration about
whether the cache entry is styled or not.
This commit aims to fix this, after this commit, the `ensure` function
will make sure that the cache entry used by `get_source_lines` is
styled correctly.
I think there are two approaches I could take:
1. Allow multiple cache entries for a single file, a styled, and
non-styled entry. The `ensure` function would then place the
correct cache entry into the last position so that
`get_source_lines` would use the correct entry, or
2. Have `ensure` recalculate entries if the required styling mode is
different to the styling mode of the current entry.
Approach #1 is better if we are rapidly switching between styling
modes, while #2 might be better if we want to keep more files in the
cache and we only rarely switch styling modes.
In the end I chose approach #2, but the good thing is that the changes
are all contained within the `ensure` function. If in the future we
wanted to change to strategy #1, this could be done transparently to
the rest of GDB.
So after this commit, the `ensure` function checks if styling is
currently possible or not. If it is not, and the current entry is
styled, then the current entry only is dropped from the cache, and a
new, unstyled entry is created. Likewise, if the current entry is
non-styled, but styling is required, we drop one entry and
recalculate.
With this change in place, I have updated set_style_enabled (in
cli/cli-style.c) so the source cache is no longer flushed when the
style settings are changed, the source cache will automatically handle
changes to the style settings now.
This problem was discovered in PR gdb/32676.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32676
Approved-By: Tom Tromey <tom@tromey.com>
110 lines
3.6 KiB
C++
110 lines
3.6 KiB
C++
/* Cache of styled source file text
|
|
Copyright (C) 2018-2024 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#ifndef GDB_SOURCE_CACHE_H
|
|
#define GDB_SOURCE_CACHE_H
|
|
|
|
#include "gdbsupport/unordered_map.h"
|
|
#include "gdbsupport/unordered_set.h"
|
|
|
|
/* 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.
|
|
|
|
Highlighting depends on the GNU Source Highlight library. When not
|
|
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. */
|
|
class source_cache
|
|
{
|
|
public:
|
|
|
|
source_cache ()
|
|
{
|
|
}
|
|
|
|
/* 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);
|
|
|
|
/* 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
|
|
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. */
|
|
bool get_source_lines (struct symtab *s, int first_line,
|
|
int last_line, std::string *lines_out);
|
|
|
|
/* Remove all the items from the source cache. */
|
|
void clear ()
|
|
{
|
|
m_source_map.clear ();
|
|
m_offset_cache.clear ();
|
|
m_no_styling_files.clear ();
|
|
}
|
|
|
|
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;
|
|
/* True if CONTENTS are styled. Otherwise, false. */
|
|
bool styled;
|
|
};
|
|
|
|
/* A helper function for get_source_lines reads a source file.
|
|
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);
|
|
|
|
/* 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. */
|
|
std::vector<source_text> m_source_map;
|
|
|
|
/* The file offset cache. The key is the full name of the source
|
|
file. */
|
|
gdb::unordered_map<std::string, std::vector<off_t>> m_offset_cache;
|
|
|
|
/* The list of files where styling failed. */
|
|
gdb::unordered_set<std::string> m_no_styling_files;
|
|
};
|
|
|
|
/* The global source cache. */
|
|
extern source_cache g_source_cache;
|
|
|
|
#endif /* GDB_SOURCE_CACHE_H */
|