diff mbox

[v2] arch-utils: Make the last endianness actually chosen sticky

Message ID alpine.DEB.2.00.1805301901390.10896@tp.orcam.me.uk
State Committed
Headers show

Commit Message

Maciej W. Rozycki May 30, 2018, 8:15 p.m. UTC
Use the last endianness explicitly selected, either by choosing a binary 
file or with the `set endian' command, for future automatic selection.

As observed with the `gdb.base/step-over-no-symbols.exp' test case when
discarding the binary file even while connected to a live target the
endianness automatically selected is reset to the GDB target's default,
even if it does not match the endianness of the target being talked to.

For example with a little-endian MIPS target and the default endianness
being big we get this:

(gdb) file .../gdb/testsuite/outputs/gdb.base/step-over-no-symbols/step-over-no-symbols
Reading symbols from .../gdb/testsuite/outputs/gdb.base/step-over-no-symbols/step-over-no-symbols...done.
(gdb) delete breakpoints
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) break main
Breakpoint 1 at 0x400840: file .../gdb/testsuite/gdb.base/start.c, line 34.
[...]
(gdb) continue
Continuing.

Breakpoint 1, main () at .../gdb/testsuite/gdb.base/start.c:34
34	  foo();
(gdb) delete breakpoints
Delete all breakpoints? (y or n) y
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) file
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
No executable file now.
Discard symbol table from `.../gdb/testsuite/outputs/gdb.base/step-over-no-symbols/step-over-no-symbols'? (y or n) y
No symbol file now.
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: purging symbols
p /x $pc
$1 = 0x40084000
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: get before PC
break *$pc
Breakpoint 2 at 0x40084000
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: break *$pc
set displaced-stepping off
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: set displaced-stepping off
stepi
Warning:
Cannot insert breakpoint 2.
Cannot access memory at address 0x40084000

Command aborted.
(gdb) FAIL: gdb.base/step-over-no-symbols.exp: displaced=off: stepi
p /x $pc
$2 = 0x40084000
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: get after PC
FAIL: gdb.base/step-over-no-symbols.exp: displaced=off: advanced
Remote debugging from host ...
monitor exit
(gdb) Killing process(es): ...
testcase .../gdb/testsuite/gdb.base/step-over-no-symbols.exp completed in 2 seconds

which shows that with the removal of the executable debugged the 
endianness of $pc still at `main' gets swapped and the value in that 
register is now incorrectly interpreted as 0x40084000 rather than 
0x400840 as shown earlier on with the `break' command.  Consequently the 
debug session no longer works as expected, until the endianness is 
overridden with an explicit `set endian little' command.

This will happen while working with any target hardware whose endianness 
does not match the default GDB target's endianness guessed and recorded 
for a later use in `initialize_current_architecture'.

Given that within a single run of GDB it is more likely that consecutive
target connections will use the same endianness than that the endianness
will be swapped between connections, it makes sense to preserve the last
endianness explicitly selected as the automatic default.  It will make a
session like above, where an executable is removed, work correctly and
will retain the endianness for a further reconnection to the target.  

And the new automatic default will still be overridden by subsequently 
choosing a binary to debug, or with an explicit `set endian' command.

With the change in place the test case above completes successfully:

(gdb) continue
Continuing.

Breakpoint 1, main () at .../gdb/testsuite/gdb.base/start.c:34
34	  foo();
(gdb) delete breakpoints
Delete all breakpoints? (y or n) y
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) file
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
No executable file now.
Discard symbol table from `.../gdb/testsuite/outputs/gdb.base/step-over-no-symbols/step-over-no-symbols'? (y or n) y
No symbol file now.
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: purging symbols
p /x $pc
warning: GDB can't find the start of the function at 0x400840.

    GDB is unable to find the start of the function at 0x400840
and thus can't determine the size of that function's stack frame.
This means that GDB may be unable to access that stack frame, or
the frames below it.
    This problem is most likely caused by an invalid program counter or
stack pointer.
    However, if you think GDB should simply search farther back
from 0x400840 for code which looks like the beginning of a
function, you can increase the range of the search using the `set
heuristic-fence-post' command.
$1 = 0x400840
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: get before PC
break *$pc
Breakpoint 2 at 0x400840
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: break *$pc
set displaced-stepping off
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: set displaced-stepping off
stepi
warning: GDB can't find the start of the function at 0x4007f8.
0x004007f8 in ?? ()
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: stepi
p /x $pc
$2 = 0x4007f8
(gdb) PASS: gdb.base/step-over-no-symbols.exp: displaced=off: get after PC
PASS: gdb.base/step-over-no-symbols.exp: displaced=off: advanced
Remote debugging from host ...
monitor exit
(gdb) Killing process(es): ...
testcase .../gdb/testsuite/gdb.base/step-over-no-symbols.exp completed in 2 seconds

	gdb/
	* arch-utils.c (gdbarch_info_fill): Set `default_byte_order' to
	the endianness selected.
	* NEWS: Document `set endian auto' mode operation update.

	gdb/doc/
	* gdb.texinfo (Choosing Target Byte Order): Document endianness
	selection details with the `set endian auto' mode.

	gdb/testsuite
	* gdb.base/endian.exp: New test.
	* gdb.base/endian.c: New test source.
---
Hi Doug,

 Thanks for your review.  Meanwhile I lost this change from my radar due 
to distractions and I have only found it now as I have been processing the 
backlog.

On Tue, 8 Nov 2016, Doug Evans wrote:

> >>       gdb/
> >>       * arch-utils.c (gdbarch_info_fill): Set `default_byte_order' to
> >>       the endianness selected.
> >>
> >>       gdb/testsuite
> >>       * gdb.base/endian.exp: New test.
> >>       * gdb.base/endian.c: New test source.
> >
> >  Can I ask for <https://patchwork.sourceware.org/patch/16504/> to be
> > reviewed?
> 
> Hi.
> 
> This is fine with me.
> [I can imagine oddities if someone was doing multi-inferior debugging
> with different endiannesses, but that's a rare situation, and I don't
> think any oddities if they exist are killers.]

 While I agree there is room for oddities here, I think it's actually just 
a matter of which oddity to choose, between the sticky endianness and the 
default endianness, both posing a risk of a mismatch with one or more of 
the inferiors.

 Do we support mixing endiannesses in a multi-inferior scenario anyway?

> I tested it on amd64-linux just as a sanity check.
> 
> I think a NEWS entry is required.
> 
> I'm kinda thinking an addition to the "Choosing Target Byte Order"
> would be nice, but it's not mandatory. [If we do so, I think to do it
> right you would have to document a whole lot more than just
> this simple addition, and I don't like imposing on you that
> burden. If you want to take it on, great of course.
> But to me it's not necessary for this patch to go in.]

 Here's an update taking your suggestions into account.  No change to 
actual code.  I have verified that both info and PDF outputs render 
correctly with the manual update.  I had some general issues with PDF 
generation however, as noted in 
<https://sourceware.org/ml/gdb/2018-05/msg00030.html>.

 OK to apply?

  Maciej

Changes from v1:

- NEWS entry added.

- Manual updated with `set endian auto' details.

- Copyright year updated across new files.
---
 gdb/NEWS                          |    6 ++
 gdb/arch-utils.c                  |    2 
 gdb/doc/gdb.texinfo               |    9 +++
 gdb/testsuite/gdb.base/endian.c   |   22 ++++++++
 gdb/testsuite/gdb.base/endian.exp |   96 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 135 insertions(+)

gdb-default-byte-order.diff

Comments

Eli Zaretskii May 31, 2018, 2:35 a.m. UTC | #1
> Date: Wed, 30 May 2018 21:15:30 +0100
> From: "Maciej W. Rozycki" <macro@mips.com>
> CC: gdb-patches <gdb-patches@sourceware.org>
> 
> 	gdb/
> 	* arch-utils.c (gdbarch_info_fill): Set `default_byte_order' to
> 	the endianness selected.
> 	* NEWS: Document `set endian auto' mode operation update.
> 
> 	gdb/doc/
> 	* gdb.texinfo (Choosing Target Byte Order): Document endianness
> 	selection details with the `set endian auto' mode.
> 
> 	gdb/testsuite
> 	* gdb.base/endian.exp: New test.
> 	* gdb.base/endian.c: New test source.

OK for the documentation parts.

Thanks.
Maciej W. Rozycki May 31, 2018, 2:17 p.m. UTC | #2
On Wed, 30 May 2018, Eli Zaretskii wrote:

> > 	gdb/
> > 	* arch-utils.c (gdbarch_info_fill): Set `default_byte_order' to
> > 	the endianness selected.
> > 	* NEWS: Document `set endian auto' mode operation update.
> > 
> > 	gdb/doc/
> > 	* gdb.texinfo (Choosing Target Byte Order): Document endianness
> > 	selection details with the `set endian auto' mode.
> > 
> > 	gdb/testsuite
> > 	* gdb.base/endian.exp: New test.
> > 	* gdb.base/endian.c: New test source.
> 
> OK for the documentation parts.

 Applied now, thanks for your review.

  Maciej
Doug Evans via gdb-patches May 31, 2018, 4:19 p.m. UTC | #3
On Wed, May 30, 2018 at 1:15 PM, Maciej W. Rozycki <macro@mips.com> wrote:
>         gdb/
>         * arch-utils.c (gdbarch_info_fill): Set `default_byte_order' to
>         the endianness selected.
>         * NEWS: Document `set endian auto' mode operation update.
>
>         gdb/doc/
>         * gdb.texinfo (Choosing Target Byte Order): Document endianness
>         selection details with the `set endian auto' mode.
>
>         gdb/testsuite
>         * gdb.base/endian.exp: New test.
>         * gdb.base/endian.c: New test source.
> ---
> Hi Doug,
>
>  Thanks for your review.  Meanwhile I lost this change from my radar due
> to distractions and I have only found it now as I have been processing the
> backlog.

LGTM. Thanks!
diff mbox

Patch

Index: gdb/gdb/NEWS
===================================================================
--- gdb.orig/gdb/NEWS	2018-05-14 04:01:41.000000000 +0100
+++ gdb/gdb/NEWS	2018-05-30 20:58:03.772196107 +0100
@@ -3,6 +3,12 @@ 
 
 *** Changes since GDB 8.1
 
+* The endianness used with the 'set endian auto' mode in the absence of
+  an executable selected for debugging is now the last endianness chosen
+  either by one of the 'set endian big' and 'set endian little' commands
+  or by inferring from the last executable used, rather than the startup
+  default.
+
 * The commands 'info variables/functions/types' now show the source line
   numbers of symbol definitions when available.
 
Index: gdb/gdb/arch-utils.c
===================================================================
--- gdb.orig/gdb/arch-utils.c	2018-05-14 04:01:42.045923841 +0100
+++ gdb/gdb/arch-utils.c	2018-05-30 17:13:21.295622268 +0100
@@ -768,6 +768,8 @@  gdbarch_info_fill (struct gdbarch_info *
   if (info->byte_order == BFD_ENDIAN_UNKNOWN)
     info->byte_order = default_byte_order;
   info->byte_order_for_code = info->byte_order;
+  /* Wire the default to the last selected byte order.  */
+  default_byte_order = info->byte_order;
 
   /* "(gdb) set osabi ...".  Handled by gdbarch_lookup_osabi.  */
   /* From the manual override, or from file.  */
Index: gdb/gdb/doc/gdb.texinfo
===================================================================
--- gdb.orig/gdb/doc/gdb.texinfo	2018-05-30 17:11:24.000000000 +0100
+++ gdb/gdb/doc/gdb.texinfo	2018-05-30 17:49:24.224267380 +0100
@@ -20308,6 +20308,15 @@  Display @value{GDBN}'s current idea of t
 
 @end table
 
+If the @code{set endian auto} mode is in effect and no executable has
+been selected, then the endianness used is the last one chosen either
+by one of the @code{set endian big} and @code{set endian little}
+commands or by inferring from the last executable used.  If no
+endianness has been previously chosen, then the default for this mode
+is inferred from the target @value{GDBN} has been built for, and is
+@code{little} if the name of the target CPU has an @code{el} suffix
+and @code{big} otherwise.
+
 Note that these commands merely adjust interpretation of symbolic
 data on the host, and that they have absolutely no effect on the
 target system.
Index: gdb/gdb/testsuite/gdb.base/endian.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gdb/gdb/testsuite/gdb.base/endian.c	2018-05-30 21:12:39.342971368 +0100
@@ -0,0 +1,22 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright (C) 2018 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/>.  */
+
+int
+main (void)
+{
+  return 0;
+}
Index: gdb/gdb/testsuite/gdb.base/endian.exp
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gdb/gdb/testsuite/gdb.base/endian.exp	2018-05-30 21:12:46.798719867 +0100
@@ -0,0 +1,96 @@ 
+# Copyright (C) 2018 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/>.
+
+# Contributed by Imagination Technologies, written by Maciej W. Rozycki.
+
+# Test automatic endianness selection.
+
+standard_testfile
+
+set en_auto "The target endianness is set automatically"
+set en_set "The target is assumed to be"
+
+clean_restart
+
+# First check that the automatic endianness is updated
+# with the `set endian' command.
+gdb_test "set endian auto" \
+    "$en_auto \\\(currently \(\?:big\|little\) endian\\\)" \
+    "default target endianness"
+gdb_test "set endian big" "$en_set big endian" \
+    "set target endianness"
+gdb_test "set endian auto" "$en_auto \\\(currently big endian\\\)" \
+    "auto target endianness"
+gdb_test "set endian little" "$en_set little endian" \
+    "set target endianness little"
+gdb_test "set endian auto" "$en_auto \\\(currently little endian\\\)" \
+    "auto target endianness little"
+gdb_test "set endian big" "$en_set big endian" \
+    "set target endianness big"
+gdb_test "set endian auto" "$en_auto \\\(currently big endian\\\)" \
+    "auto target endianness big"
+
+if { [build_executable ${testfile}.exp $testfile] } {
+    gdb_suppress_entire_file "$pf_prefix cannot build executable"
+}
+
+if { [gdb_file_cmd $binfile] } {
+    gdb_suppress_entire_file "$pf_prefix cannot select executable"
+}
+set test "get target endianness"
+if { [gdb_test_multiple "show endian" "$test" {
+    -re "$en_auto \\\(currently \(big\|little\) endian\\\).*$gdb_prompt" {
+	set endian $expect_out(1,string)
+	pass "$test"
+    }
+}] } {
+    gdb_suppress_entire_file \
+	"$pf_prefix cannot determine executable endianness"
+    set endian ""
+}
+
+# Now check that the automatic endianness is updated
+# according to the executable selected.
+if { [gdb_unload] } {
+    gdb_suppress_entire_file "$pf_prefix cannot unselect executable"
+}
+gdb_test "set endian big" "$en_set big endian" \
+    "override target endianness big"
+gdb_test "set endian auto" "$en_auto \\\(currently big endian\\\)" \
+    "override auto target endianness big"
+if { [gdb_file_cmd $binfile] } {
+    gdb_suppress_entire_file "$pf_prefix cannot select executable"
+}
+gdb_test "show endian" "$en_auto \\\(currently $endian endian\\\)" \
+    "previously big default executable endianness"
+if { [gdb_unload] } {
+    gdb_suppress_entire_file "$pf_prefix cannot unselect executable"
+}
+gdb_test "show endian" "$en_auto \\\(currently $endian endian\\\)" \
+    "previously big default no executable endianness"
+gdb_test "set endian little" "$en_set little endian" \
+    "override target endianness little"
+gdb_test "set endian auto" "$en_auto \\\(currently little endian\\\)" \
+    "override auto target endianness little"
+if { [gdb_file_cmd $binfile] } {
+    gdb_suppress_entire_file "$pf_prefix cannot select executable"
+}
+gdb_test "show endian" "$en_auto \\\(currently $endian endian\\\)" \
+    "previously little default executable endianness"
+if { [gdb_unload] } {
+    gdb_suppress_entire_file "$pf_prefix cannot unselect executable"
+}
+gdb_test "show endian" "$en_auto \\\(currently $endian endian\\\)" \
+    "previously little default no executable endianness"