Wrong debug info for argc at -O2

杨已彪 yangyibiao@hust.edu.cn
Fri May 29 15:58:48 GMT 2020

Consider test-case:
$ cat small.c
#include <stdio.h>

int main(int argc, char **argv) {
  char buf[6];
  char c[] = "abc";
  sprintf(buf, (char *)c, 1);

  return 0;

Stepping with step and print the values of arguments:
$ gcc -O2 -g small.c; gdb -q a.out
Reading symbols from a.out...
(gdb) b main
Breakpoint 1 at 0x401040: file small.c, line 5.
(gdb) r
Starting program: /home/yibiao/Debugger/a.out

Breakpoint 1, main (argc=1, argv=0x7fffffffdff8) at small.c:5
5      char c[] = "abc";
(gdb) info args argc
argc = 1
(gdb) step
6      sprintf(buf, (char *)c, 1);
(gdb) info args argc
argc = -8454

We can find that at line 5, the value of argc is 1.
When stepping to line 6 with step, the value of argc is changed to -8454.
However, When stepping with stepi, the value of argc is still 1 at line 6.

I am posting it here as I am not sure whether this is a gcc bug or a gdb bug.
By the way, I found it very difficult to determine whether a problem is caused by gdb or gcc?
Is there any suggestions?

$ gcc --version
gcc (GCC) 10.0.1 20200419 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

$ gdb --version
GNU gdb (GDB)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

More information about the Gdb mailing list