https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117647
Bug ID: 117647
Summary: Setting break point changes program output
Product: gcc
Version: 8.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: debug
Assignee: unassigned at gcc dot gnu.org
Reporter: wuz73 at hotmail dot com
Target Milestone: ---
OS: 4.18.0-477.13.1.el8_8.x86_64 #1 SMP Tue May 30 16:09:32 PDT 2023 x86_64
x86_64 x86_64 GNU/Linux
g++: 8.5.0 20210514 (Red Hat 8.5.0-18.0.2)
gdb: GNU gdb (GDB) Red Hat Enterprise Linux 8.2-19.0.1.el8
While debugging a program, I found that adding a break point can change the
program output! After some maneuver, I finally constructed this code to
reproduce:
<code>
#include <cstdio>
double a=39535.263;
double b=39535.263;
double c=39531.538;
double g=0.32;
int main()
{
double d=a-b;
double e=b-c;
double f=0;
double w=0.14;
if(d+e>1e-10)
f=(d-e)/(d+e);
g=f*(1-w)+g*w;
printf("%g\n",g);
}
</code>
After "g++ -g -O2 -mfma", you can check that the output should be -0.8152.
However, if you set a break point on this FMA instruction:
400520: c4 e2 f1 99 05 c7 01 vfmadd132sd 0x1c7(%rip),%xmm1,%xmm0
upon hitting the break point, continue, and you will see an incorrect output
0.0448. I tried hb (hardware-assisted break point) and it is the same. Latest
gdb 15.2 also yields the same error.
I suspect that gdb didn't calculate rip correctly when execute the vfmadd132sd
instruction, as it uses rip. I tried to break on some other instructions using
rip (vmovsd, vsubsd, vcomisd, vmulsd) and they seem to be OK.