mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2026-08-27 00:26:02 -04:00
When running test-case gdb.python/py-source-styling.exp with Tcl 9.0, I get:
...
python gdb.execute('list 26')^M
<SNIP>/* The following line contains a character that is non-utf-8. This is a
<SNIP> critical part of the test as Python 3 can't convert this into a string
<SNIIP> using its default mechanism. */
<SNIP>FAIL: $exp: test_source_cache_style_tracking: \
python gdb.execute('list 26') (timeout)
ERROR: i_read(spawn_id fd=9): invalid or incomplete multibyte or wide character
while executing
"expect {
-i exp9 -timeout 10
-re ".*A problem internal to GDB has been detected" {
fail "$message (GDB internal error)"
gdb_internal_error..."
("uplevel" body line 1)
invoked from within
"uplevel $body" POSIX EILSEQ {invalid or incomplete multibyte or wide \
character} i_read(spawn_id fd=9)
: invalid or incomplete multibyte or wide character
Couldn't send python gdb.execute('list 26', to_string=False, styling=True) to GDB.
...
I have tried a fix similar to the previous commit in default_gdb_spawn:
...
+ if {[tcl_version_at_least 9 0 0]} {
+ set fd [exp_open -leaveopen -i $res]
+ fconfigure $fd -encoding utf-8 -profile replace
+ }
+
...
but that doesn't seem to work.
Work around this by skipping the part that runs into this problem.
Approved-By: Tom Tromey <tom@tromey.com>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33948
190 lines
6.6 KiB
Text
190 lines
6.6 KiB
Text
# Copyright (C) 2022-2026 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 $::testfile
|
|
|
|
# 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 $::testfile
|
|
|
|
# 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 $::testfile
|
|
|
|
# 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
|
|
}
|
|
|
|
# With Tcl 9.0, something (dejagnu/expect/gdb testsuite infrastructure)
|
|
# has problems with gdb IO containing non-utf-8 chars. Work around this by
|
|
# requiring Tcl 8.6 for the following tests. PR testsuite/33987 is open to
|
|
# come up with a better solution.
|
|
require {expr {$::tcl_version == 8.6}}
|
|
|
|
# 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
|
|
}
|