Bug 11076 - Printing character arrays
Summary: Printing character arrays
Status: RESOLVED WORKSFORME
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: unknown
: P2 normal
Target Milestone: 7.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-10 10:39 UTC by sabrown256
Modified: 2009-12-11 04:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sabrown256 2009-12-10 10:39:20 UTC
GDB 7.0 has problem printing character arrays.

If you have declaration in source such as:
char a[100];

print a -> ""
print &a[0] -> "foo bar baz"

On the other hand if you have:
char *b;

print b -> "foo bar baz"

Character arrays in structs have the same problem.
Comment 1 Paul Pluzhnikov 2009-12-10 17:13:05 UTC
This works just fine for me:

int main()
{
  char a[100] = "foo bar baz";
  return 0;
}

gcc -g t.c -o t
gdb-7.0/build/gdb/gdb ./t
GNU gdb (GDB) 7.0
...
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /tmp/t...done.
(gdb) list
1       int main()
2       {
3         char a[100] = "foo bar baz";
4         return 0;
5       }
(gdb) break 4
Breakpoint 1 at 0x400546: file t.c, line 4.
(gdb) run

Breakpoint 1, main () at t.c:4
4         return 0;
(gdb) print a
$1 = "foo bar baz", '\000' <repeats 88 times>

Please provide a complete test case, as well as complete log of your GDB session
(as I have done above).
Comment 2 sabrown256 2009-12-10 19:13:19 UTC
Subject: Re:  Printing character arrays

ppluzhnikov at google dot com wrote:
> ------- Additional Comments From ppluzhnikov at google dot com  2009-12-10 17:13 -------
> This works just fine for me:
>
> int main()
> {
>   char a[100] = "foo bar baz";
>   return 0;
> }
>
> gcc -g t.c -o t
> gdb-7.0/build/gdb/gdb ./t
> GNU gdb (GDB) 7.0
> ...
> This GDB was configured as "x86_64-unknown-linux-gnu".
> For bug reporting instructions, please see:
> <http://www.gnu.org/software/gdb/bugs/>...
> Reading symbols from /tmp/t...done.
> (gdb) list
> 1       int main()
> 2       {
> 3         char a[100] = "foo bar baz";
> 4         return 0;
> 5       }
> (gdb) break 4
> Breakpoint 1 at 0x400546: file t.c, line 4.
> (gdb) run
>
> Breakpoint 1, main () at t.c:4
> 4         return 0;
> (gdb) print a
> $1 = "foo bar baz", '\000' <repeats 88 times>
>
> Please provide a complete test case, as well as complete log of your GDB session
> (as I have done above).
>
>   

Here is an example from a typical debug session:

GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 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.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from 
/store/apps/codes/dobby/dev/lnx-2.10-o/bin/dbman...done.
Breakpoint 1 at 0x40cfac: file ../../dbplace.c, line 343.
                       ...

(gdb) p data[0]
$1 = {id = 1, country = "", state = '\000' <repeats 127 times>,
  city = "", name = "", comment = "",
  start = "\000\000\000\000\000\000\000\000\000",
  end = "\000\000\000\000\000\000\000\000\000", longitude = 0,
  latitude = 0, elevation = 0}
(gdb) whatis data[0].country
type = char [128]
(gdb) p data[0].country
$2 = ""
(gdb) p &data[0].country[0]
$3 = 0x7ffff7ee8054 "Canada"
(gdb)


I have been seeing this consistently across a broad range of codes.
They are in development, I have bugs, I use gdb - so I cannot say
there is no problem with my codes.  They are mainly clean under valgrind.
In any event, the debugger should not have this kind of problem.

I will try your example to see if it is Ubuntu's build that is funny.

Thanks for the reply.

Stewart


Comment 3 sabrown256 2009-12-10 19:15:11 UTC
Subject: Re:  Printing character arrays

ppluzhnikov at google dot com wrote:
> ------- Additional Comments From ppluzhnikov at google dot com  2009-12-10 17:13 -------
> This works just fine for me:
>
> int main()
> {
>   char a[100] = "foo bar baz";
>   return 0;
> }
>
> gcc -g t.c -o t
> gdb-7.0/build/gdb/gdb ./t
> GNU gdb (GDB) 7.0
> ...
> This GDB was configured as "x86_64-unknown-linux-gnu".
> For bug reporting instructions, please see:
> <http://www.gnu.org/software/gdb/bugs/>...
> Reading symbols from /tmp/t...done.
> (gdb) list
> 1       int main()
> 2       {
> 3         char a[100] = "foo bar baz";
> 4         return 0;
> 5       }
> (gdb) break 4
> Breakpoint 1 at 0x400546: file t.c, line 4.
> (gdb) run
>
> Breakpoint 1, main () at t.c:4
> 4         return 0;
> (gdb) print a
> $1 = "foo bar baz", '\000' <repeats 88 times>
>
> Please provide a complete test case, as well as complete log of your GDB session
> (as I have done above).
>
>   
I ran your example and got:

Current directory is /gapps/codes/dobby/db/
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 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.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /store/apps/codes/dobby/db/foo...done.
Breakpoint 1 at 0x40058e: file foo.c, line 4.
(gdb) r
Starting program: /store/apps/codes/dobby/db/foo

Breakpoint 1, main () at foo.c:4
(gdb) p a
$1 = ""
(gdb) p &a[0]
$2 = 0x7fffffffe0b0 "foo bar baz"
(gdb)

I will submit this to the Ubuntu folks.

Stewart

Comment 4 Paul Pluzhnikov 2009-12-11 04:51:24 UTC
The problem appears to be in either GCC or GDB as distributed by Ubuntu.
Both gdb-7.0 (official) and current CVS Head work fine.