public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Wrong debug info for argc at -O2
@ 2020-05-29 15:58 杨已彪
  2020-06-22 18:14 ` Simon Marchi
  2020-06-22 18:53 ` Luis Machado
  0 siblings, 2 replies; 4+ messages in thread
From: 杨已彪 @ 2020-05-29 15:58 UTC (permalink / raw)
  To: gdb



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
(gdb)
...

/**************************************
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
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gdb --version
GNU gdb (GDB) 10.0.50.20200517-git
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.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Wrong debug info for argc at -O2
  2020-05-29 15:58 Wrong debug info for argc at -O2 杨已彪
@ 2020-06-22 18:14 ` Simon Marchi
  2020-06-22 18:53 ` Luis Machado
  1 sibling, 0 replies; 4+ messages in thread
From: Simon Marchi @ 2020-06-22 18:14 UTC (permalink / raw)
  To: 杨已彪, gdb

On 2020-05-29 11:58 a.m., 杨已彪 wrote:
> 
> 
> 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
> (gdb)
> ...
> 
> /**************************************
> 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
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> 
> $ gdb --version
> GNU gdb (GDB) 10.0.50.20200517-git
> 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.

Hi,

The only way to know is to inspect the DWARF information manually and see if it accurately
describe the program.  If not, it's a gcc bug.  It if does, it's a gdb bug.

To inspect the DWARF information, you can use `readelf --debug-dump` or `llvm-dwarfdump`
(although I am a big fan of GNU tools, I find the output of llvm-dwarfdump a bit more
readable).

In this particular case, you'd find the DIE (Debug Info Entry) for `argc` and see how the
location of that variable is described.  See the DWARF spec to understand how to interpret
the value.

Simon

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Wrong debug info for argc at -O2
  2020-05-29 15:58 Wrong debug info for argc at -O2 杨已彪
  2020-06-22 18:14 ` Simon Marchi
@ 2020-06-22 18:53 ` Luis Machado
  2020-06-30 15:30   ` 杨已彪
  1 sibling, 1 reply; 4+ messages in thread
From: Luis Machado @ 2020-06-22 18:53 UTC (permalink / raw)
  To: 杨已彪, gdb

On 5/29/20 12:58 PM, 杨已彪 wrote:
> 
> 
> 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
> (gdb)

What architecture is this?

If you want precise debug information, you should use -O0 instead. With 
O2 you are likely to run into situations where the debug info has been 
lost or is just incorrect.

With that said, GCC has improved over the past few years in terms of 
debug info generation for O2+.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Re: Wrong debug info for argc at -O2
  2020-06-22 18:53 ` Luis Machado
@ 2020-06-30 15:30   ` 杨已彪
  0 siblings, 0 replies; 4+ messages in thread
From: 杨已彪 @ 2020-06-30 15:30 UTC (permalink / raw)
  To: Luis Machado; +Cc: gdb

I am very sorry that I forgot to reply this. 

I am using Ubuntu 20.04 with x86_64. 

$ uname -a
Linux Lab 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

"Luis Machado" <luis.machado@linaro.org>wrote:
> On 5/29/20 12:58 PM, 杨已彪 wrote:
> > 
> > 
> > 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
> > (gdb)
> 
> What architecture is this?
> 
> If you want precise debug information, you should use -O0 instead. With 
> O2 you are likely to run into situations where the debug info has been 
> lost or is just incorrect.
> 
> With that said, GCC has improved over the past few years in terms of 
> debug info generation for O2+.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-06-30 15:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-29 15:58 Wrong debug info for argc at -O2 杨已彪
2020-06-22 18:14 ` Simon Marchi
2020-06-22 18:53 ` Luis Machado
2020-06-30 15:30   ` 杨已彪

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).