binutils-gdb/gdb/testsuite/gdb.arch/riscv-default-tdesc.exp
Andrew Burgess 895b7b4e4b gdb/riscv: select rv32 target by default when requested
GDB for RISC-V always uses target descriptions.  When the target
doesn't provide a target description then a default is selected.
Usually this default is selected based on the properties of the
executable being debugged.  However, when there is no executable being
debugged we currently fallback to the riscv:rv64 target description as
the default.  This leads to strange behaviour like this:

  $ gdb
  (gdb) set architecture riscv:rv32
  (gdb) p sizeof ($pc)
  $1 = 8

Despite the users specifically setting the architecture to riscv:rv32
GDB still thinks that the target has riscv:rv64 register sizes.

The above is a bit of a contrived situation.  I actually ran into this
situation while trying to connect to a running riscv:rv32 target
without supplying an executable (the target didn't provide a target
description).  When I tried to set a register on the target I ran into
errors because GDB was passing 8 bytes to the target rather than the
expected 4.  Even when I manually specified the architecture (as
above) I couldn't convince GDB to only send 4 bytes.

This patch fixes this issue.  Now, when we selected a default target
description we will make use of the user selected architecture to
guide our choice.  In the above example we now get:

  $ gdb
  (gdb) set architecture riscv:rv32
  (gdb) p sizeof ($pc)
  $1 = 4

And my real world example of connecting to a remote without an
executable works fine.

I've used the fact that we can ask GDB about $pc even when no
executable is loaded as the basis for a test to cover this situation.

gdb/ChangeLog:

	* riscv-tdep.c (riscv_features_from_gdbarch_info): Rename to...
	(riscv_features_from_bfd): ...this.  Change parameter type to
	'bfd*', and update as required.
	(riscv_find_default_target_description): Update call to
	riscv_features_from_bfd.  Select a default xlen based on
	info.bfd_arch_info.
	(riscv_gdbarch_init): Update call to riscv_features_from_bfd.

gdb/testsuite/ChangeLog:

	* gdb.arch/riscv-default-tdesc.exp: New file.
2021-02-24 16:06:54 +00:00

60 lines
2.0 KiB
Plaintext

# Copyright 2021 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/>.
# Check the size of the $pc register as the user changes the selected
# architecture. As no executable is provided then the size of the $pc
# register is defined by the default target description selected by
# GDB.
#
# This test ensures that GDB is selecting an RV32 default if the user
# has forced the current architecture to be riscv:rv32.
#
# In all other cases the default will be RV64.
if {![istarget "riscv*-*-*"]} {
verbose "Skipping ${gdb_test_file_name}."
return
}
# Start GDB with no executable.
gdb_start
# The tests are defined by a list of architecture names to switch too
# and the expected size of $pc. The first list entry is special and
# has an empty architecture string, this checks GDB's default value on
# startup.
foreach data {{"" 8} {"riscv:rv32" 4} {"riscv:rv64" 8} {"riscv" 8} \
{"auto" 8}} {
set arch [lindex $data 0]
set size [lindex $data 1]
# Switch architecture.
if { $arch != "" && $arch != "auto" } {
gdb_test "set architecture $arch" \
"The target architecture is set to \"$arch\"\\."
} elseif { $arch == "auto" } {
gdb_test "set architecture $arch" \
"The target architecture is set to \"auto\" \\(currently \"riscv\"\\)\\."
} else {
set arch "default architecture"
}
# Check the size of $pc.
with_test_prefix "$arch" {
gdb_test "p sizeof (\$pc)" " = $size" \
"size of \$pc register is $size"
}
}