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-11-22 07:39 UTC (History)
17 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 Sourceware Commits 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 Sourceware Commits 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.
Comment 4 Ahmed Sayeed 2021-06-27 17:59:57 UTC Comment hidden (spam)
Comment 5 Ucel Sani 2021-08-10 12:45:51 UTC Comment hidden (spam)
Comment 6 james rohan 2021-09-02 11:06:59 UTC Comment hidden (spam)
Comment 7 james robin 2021-09-06 09:09:15 UTC Comment hidden (spam)
Comment 8 Mehmet gelisin 2021-09-10 19:38:41 UTC Comment hidden (spam)
Comment 9 diheto 2021-09-22 10:11:37 UTC Comment hidden (spam)
Comment 10 Kylan 2021-09-26 13:31:28 UTC Comment hidden (spam)
Comment 11 Gulsen Engin 2021-10-09 11:00:50 UTC Comment hidden (spam)
Comment 13 progonsaytu 2021-10-19 07:15:26 UTC Comment hidden (spam)
Comment 14 glassmtech 2021-10-24 10:02:36 UTC Comment hidden (spam)
Comment 15 paneki 2021-11-06 21:13:01 UTC Comment hidden (spam)
Comment 16 tesaso8237@funboxcn.comxecana8007@funboxcn.com 2021-11-16 19:08:23 UTC Comment hidden (spam)
Comment 17 tesaso8237@funboxcn.comxecana8007@funboxcn.com 2021-11-16 19:12:25 UTC Comment hidden (spam)
Comment 18 tesaso8237@funboxcn.comxecana8007@funboxcn.com 2021-11-16 19:15:50 UTC Comment hidden (spam)
Comment 19 gexed96894 2021-11-22 07:39:35 UTC Comment hidden (spam)