Bug 27009 - [s390] GDB branches randomly for BC instruction while displaced stepping
Summary: [s390] GDB branches randomly for BC instruction while displaced stepping
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: breakpoints (show other bugs)
Version: 10.1
: P2 normal
Target Milestone: 10.2
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-03 23:07 UTC by Giancarlo Frix
Modified: 2021-01-31 06:26 UTC (History)
2 users (show)

See Also:
Host: s390x-ibm-linux-gnu
Target: s390x-ibm-linux-gnu
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Giancarlo Frix 2020-12-03 23:07:33 UTC
I noticed this problem while working an s390x port.

Minimal testcase:
	.align	8
	.globl main
	.globl insn
	.type  main, @function
	.type  insn, @function
	# This should return 0 on success.
main:
	basr	%r1, %r0
insn:	bc	15, win-insn(0,%r1)
	lghi	%r2,1
win:	lghi	%r2,0
	br	%r14

Assemble and link the above.

Turn on displaced stepping, set a breakpoint on `insn`, run, then try to step over the breakpoint with stepi.
(gdb) set displaced-stepping on
(gdb) b insn
(gdb) r
(gdb) stepi

instead of branching to `win`, gdb will branch to an apparently random nearby address, and the inferior will generally crash. This problem is present in all versions of GDB I've tested.
Comment 1 cvs-commit@gcc.gnu.org 2020-12-06 10:28:20 UTC
The master branch has been updated by Joel Brobecker <brobecke@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=296cfb88895e35d8bfc5ce43449e65a92d87354d

commit 296cfb88895e35d8bfc5ce43449e65a92d87354d
Author: Giancarlo Frix <gfrix@rocketsoftware.com>
Date:   Sun Dec 6 14:27:52 2020 +0400

    s390: Fix BC instruction breakpoint handling
    
    This fixes a long-lived bug in the s390 port.
    
    When trying to step over a breakpoint set on a BC (branch on condition)
    instruction with displaced stepping on IBM Z, gdb would incorrectly
    adjust the pc regardless of whether or not the branch was taken. Since
    the branch target is an absolute address, this would cause the inferior
    to jump around wildly whenever the branch was taken, either crashing it
    or causing it to behave unpredictably.
    
    It turns out that the logic to handle BC instructions correctly was in
    the code, but that the enum value representing its opcode has always
    been incorrect.
    
    This patch corrects the enum value to the actual opcode, fixing the
    stepping problem. The enum value is also used in the prologue analysis
    code, so this also fixes a minor bug where more of the prologue would
    be read than was necessary.
    
    gdb/ChangeLog:
    
            PR breakpoints/27009
            * s390-tdep.h (op_bc): Correct BC opcode value.
Comment 2 cvs-commit@gcc.gnu.org 2020-12-06 10:31:15 UTC
The gdb-10-branch branch has been updated by Joel Brobecker <brobecke@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=47c4623c7587a0008597a4a10a7f32bbac1f7ad4

commit 47c4623c7587a0008597a4a10a7f32bbac1f7ad4
Author: Giancarlo Frix <gfrix@rocketsoftware.com>
Date:   Sun Dec 6 14:30:53 2020 +0400

    s390: Fix BC instruction breakpoint handling
    
    This fixes a long-lived bug in the s390 port.
    
    When trying to step over a breakpoint set on a BC (branch on condition)
    instruction with displaced stepping on IBM Z, gdb would incorrectly
    adjust the pc regardless of whether or not the branch was taken. Since
    the branch target is an absolute address, this would cause the inferior
    to jump around wildly whenever the branch was taken, either crashing it
    or causing it to behave unpredictably.
    
    It turns out that the logic to handle BC instructions correctly was in
    the code, but that the enum value representing its opcode has always
    been incorrect.
    
    This patch corrects the enum value to the actual opcode, fixing the
    stepping problem. The enum value is also used in the prologue analysis
    code, so this also fixes a minor bug where more of the prologue would
    be read than was necessary.
    
    gdb/ChangeLog:
    
            PR breakpoints/27009
            * s390-tdep.h (op_bc): Correct BC opcode value.
Comment 3 Joel Brobecker 2021-01-31 06:26:22 UTC
patches pushed a while ago. Just forgot to close the PR.