Files
binutils-gdb/gdb/testsuite/gdb.python/py-source-styling.exp
Andrew Burgess 05f5f4f274 gdb: check styled status of source cache entries
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>
2025-03-21 15:20:26 +00:00

185 lines
6.3 KiB
Plaintext

# Copyright (C) 2022-2024 Free Software Foundation, Inc.
# 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/>.
# Test related to source code highlighting and Python. Includes a
# test for using the Pygments module as a fall back to GNU source
# highlight.
#
# This script also includes tests for handling a non-uft-8 character
# with both Pygments highlighting, and with gdb.execute (when using
# the list command).
require allow_python_tests
load_lib gdb-python.exp
standard_testfile
if { [build_executable "failed to build" ${testfile} ${srcfile}] == -1 } {
return
}
set line_number [gdb_get_line_number "List this line."]
# Helper proc. Run CMD, which should produce a source listing, and
# check if the source code is styled or not. EXPECT_STYLED indicates
# if we expect the source listing to be styled or not.
proc check_source_listing_styling { cmd expect_styled { testname "" } } {
if { $testname eq "" } {
set testname $cmd
}
set seen_style_escape false
gdb_test_multiple $cmd $testname {
-re -wrap "Python Exception.*" {
fail $gdb_test_name
return
}
-re "\033" {
set seen_style_escape true
exp_continue
}
-re "$::gdb_prompt $" {
gdb_assert { $seen_style_escape == $expect_styled } \
$gdb_test_name
}
}
}
# Check that the Python pygments module can be used for source
# highlighting when GNU source highlight is not available (or is
# disabled, as is done in this test).
proc test_pygments_styling {} {
clean_restart $::binfile
# Remote host boards disable styling via GDB's command line. Turn
# it back on now.
if {[is_remote host]} {
gdb_test "set style enabled on"
}
if { ![gdb_py_module_available "pygments"] } {
unsupported "pygments module not available"
return
}
if ![runto_main] {
return
}
gdb_test_no_output "maint set gnu-source-highlight enabled off"
gdb_test "maint flush source-cache" "Source cache flushed\\."
check_source_listing_styling "list $::line_number" true
}
# Use gdb.execute to list source code containing non-utf-8 character.
# Check that initially GDB fails to convert the source code to a
# string, then set the correct host encoding, and try again. This
# time the conversion should succeed.
proc test_gdb_execute_non_utf8_source {} {
clean_restart $::binfile
# The default host charset is utf-8, the source code contains a
# non-utf-8 character, so this will fail.
gdb_test \
"python source = gdb.execute('list $::line_number', True, True)" \
[multi_line \
"Python Exception <class 'UnicodeDecodeError'>: 'ascii' codec can't decode byte 0xc0 in position 250: ordinal not in range\\(128\\)" \
"Error occurred in Python: 'ascii' codec can't decode byte 0xc0 in position 250: ordinal not in range\\(128\\)"] \
"gdb.execute fails to convert result to string"
# Set the correct host charset, and try the conversion again.
gdb_test_no_output "set host-charset ISO-8859-1"
gdb_test_no_output \
"python source = gdb.execute('list $::line_number', True, True)" \
"gdb.execute does convert result to string"
# Check that we captured something that looks like the expected source.
gdb_test "python print(source)" ".*List this line.*"
}
# Use gdb.execute() to list source code. Alternate between asking for
# styled, and unstyled source code. In some cases we ask for the
# output to be returned via a string, and in other cases we ask for
# the output to be sent straight to stdout.
proc_with_prefix test_source_cache_style_tracking {} {
clean_restart $::binfile
# Remote host boards disable styling via GDB's command line. Turn
# it back on now.
if {[is_remote host]} {
gdb_test "set style enabled on"
}
gdb_test_no_output "set host-charset ISO-8859-1"
# Commands which return styled, and non-styled source code mixed
# together. This ensures that the source cache will need to keep
# discarding the entry with the wrong styling mode. All of these
# gdb.execute calls send their output via a string.
check_source_listing_styling \
"python print(gdb.execute('list $::line_number', to_string=True), end='')" \
false
check_source_listing_styling \
"python print(gdb.execute('list $::line_number', to_string=True, styling=True), end='')" \
true
foreach from_tty { True False } {
check_source_listing_styling \
"python print(gdb.execute('list $::line_number', $from_tty, True), end='')" \
false
check_source_listing_styling \
"python print(gdb.execute('list $::line_number', $from_tty, True, True), end='')" \
true
check_source_listing_styling \
"python print(gdb.execute('list $::line_number', $from_tty, True, False), end='')" \
false
}
# The same again, but this time the output is sent directly to
# stdout.
check_source_listing_styling \
"python gdb.execute('list $::line_number')" \
true
check_source_listing_styling \
"python gdb.execute('list $::line_number', to_string=False, styling=False)" \
false
check_source_listing_styling \
"python gdb.execute('list $::line_number', to_string=False, styling=True)" \
true
foreach from_tty { True False } {
check_source_listing_styling \
"python gdb.execute('list $::line_number', $from_tty, False, False)" \
false
check_source_listing_styling \
"python gdb.execute('list $::line_number', $from_tty, False, True)" \
true
check_source_listing_styling \
"python gdb.execute('list $::line_number', $from_tty, False)" \
true
}
}
# We need an ANSI-capable terminal to get the output, additionally we
# need to set LC_ALL so GDB knows the terminal is UTF-8 capable,
# otherwise we'll get a UnicodeEncodeError trying to encode the
# output.
with_ansi_styling_terminal {
test_pygments_styling
test_gdb_execute_non_utf8_source
test_source_cache_style_tracking
}