https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92386
Bug ID: 92386 Summary: gdb issue with variable-shadowing Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: debug Assignee: unassigned at gcc dot gnu.org Reporter: qinzhao at gcc dot gnu.org Target Milestone: --- gdb cannot print the correct value of variables if it's shadowed. please see the following small testing case for an example: 1 #include <stdio.h> 2 int 3 main () 4 { 5 volatile int v = 0; 6 int i; 7 for (i=0; i < 3; i++) 8 { 9 v++; 10 v++; 11 printf("i = %d outer v 1 is %d\n",i, v); 12 volatile int v = 4; 13 v++; 14 printf("i = %d inner v is %d\n",i, v); 15 } 16 printf("outer v 2 is %d\n", v); 17 } built it with the latest gcc10 and execute it, no problem: [qinzhao@localhost ~]$ gcc t.c [qinzhao@localhost ~]$ ./a.out i = 0 outer v 1 is 2 i = 0 inner v is 5 i = 1 outer v 1 is 4 i = 1 inner v is 5 i = 2 outer v 1 is 6 i = 2 inner v is 5 outer v 2 is 6 However, when use gdb to check the value of variable v, we will see that at line 9, 10, the value of "v" is incorrect: (gdb) run Starting program: /home/qinzhao/a.out Breakpoint 1, main () at t.c:9 9 v++; Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.ora28641867.1.el7.x86_64 (gdb) print v $1 = 32767 (gdb) n 10 v++; (gdb) print v $2 = 32767 (gdb) n 11 printf("i = %d outer v 1 is %d\n",i, v); (gdb) n i = 0 outer v 1 is 2 12 volatile int v = 4; (gdb) n 13 v++; (gdb) print v $3 = 4 (gdb) n 14 printf("i = %d inner v is %d\n",i, v); (gdb) print v $4 = 5 (gdb) n i = 0 inner v is 5 7 for (i=0; i < 3; i++) (gdb) n Breakpoint 1, main () at t.c:9 9 v++; (gdb) print v $5 = 5 (gdb) n 10 v++; (gdb) print v $6 = 5