public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Accessing std::cout and std::cin from within gdb.
@ 2011-10-20 17:24 Delcypher
  2011-10-21  0:47 ` Jan Kratochvil
  0 siblings, 1 reply; 3+ messages in thread
From: Delcypher @ 2011-10-20 17:24 UTC (permalink / raw)
  To: gdb

Hi I'm having problems accessing the cout and cin objects from within
gdb. If I take a simple example program:

#include <iostream>

using namespace std;
int main()
{
        ostream& mockCout = cout;
        istream& mockCin = cin;

        cout << "&std::cout is " << &cout << endl;
        cout << "&std::cin  is " << &cin << endl;

        cout << "&mockCin is " << &mockCin << endl;
        cout << "&mockCout  is " << &mockCout << endl;
        return 0;
}

And build with `g++ -Wall -g test.cpp' and then run `gdb a.out'

(gdb) b main
Breakpoint 1 at 0x40087c: file test.cpp, line 6.
(gdb) run
Starting program: /home/dan/src/a.out

Breakpoint 1, main () at test.cpp:6
6               ostream& mockCout = cout;
(gdb) n
7               istream& mockCin = cin;
(gdb) n
9               cout << "&std::cout is " << &cout << endl;
(gdb) n
&std::cout is 0x6013a0
10              cout << "&std::cin  is " << &cin << endl;
(gdb) n
&std::cin  is 0x601280
12              cout << "&mockCin is " << &mockCin << endl;
(gdb) n
&mockCin is 0x601280
13              cout << "&mockCout  is " << &mockCout << endl;
(gdb) n
&mockCout  is 0x6013a0
14              return 0;
(gdb) p &cout
$1 = (std::ostream *) 0x7ffff7dc9d60
(gdb) p &cin
$2 = (std::istream *) 0x7ffff7dc9e80
(gdb) p &mockCout
$3 = (std::ostream *) 0x6013a0
(gdb) p &mockCin
$4 = (std::istream *) 0x601280
(gdb)

What I see is that gdb has the completely wrong address for the cin
and cout objects. My references have the right address though.
Consequently calling any of cin's or cout's methods fails but using my
references works fine (except for the error messages I see).

(gdb) call cout.good()
Cannot access memory at address 0xffffffffffffffe8
(gdb) call mockCout.good()
warning: RTTI symbol not found for class 'std::ostream'
warning: RTTI symbol not found for class 'std::ostream'
warning: RTTI symbol not found for class 'std::ostream'
warning: RTTI symbol not found for class 'std::ostream'
warning: RTTI symbol not found for class 'std::ostream'
$6 = true
(gdb)

I found an old post
(http://sourceware.org/ml/gdb/2006-08/msg00244.html ) from over 5
years ago that demonstrates this behaviour and shows another way to
access cout or cin. In my case I could run

(gdb) call ( (std::ostream) *(0x6013a0)).good()



Is this behaviour of not being able to access cout and cin a bug or
have I done something wrong?

If needed here is some version information.
OS: Arch Linux x86_64
g++ version:  g++ (GCC) 4.6.1 20110819 (prerelease)
gdb version:  GNU gdb (GDB) 7.3.1

Thanks,
Dan.

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

* Re: Accessing std::cout and std::cin from within gdb.
  2011-10-20 17:24 Accessing std::cout and std::cin from within gdb Delcypher
@ 2011-10-21  0:47 ` Jan Kratochvil
  2011-10-22  1:18   ` Delcypher
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Kratochvil @ 2011-10-21  0:47 UTC (permalink / raw)
  To: Delcypher; +Cc: gdb

On Wed, 19 Oct 2011 23:14:24 +0200, Delcypher wrote:
> What I see is that gdb has the completely wrong address for the cin
> and cout objects.

$ nm a.out
00000000006013a0 B _ZSt4cout@@GLIBCXX_3.4
$ nm -C a.out
00000000006013a0 B std::cout@@GLIBCXX_3.4

std::cout symbol has mangled name _ZSt4cout.

$ readelf -Wr a.out | grep  _ZSt4cout
00000000006013a0  0000000c00000005 R_X86_64_COPY     00000000006013a0 _ZSt4cout + 0
$ readelf -Wr /usr/lib64/libstdc++.so.6 | grep  _ZSt4cout
00000000002ef688  00000a6500000006 R_X86_64_GLOB_DAT 00000000002f2d60 _ZSt4cout + 0

GDB does not handle the copy relocations well as GDB is not compliant with the
well defined ld.so symbols precedence/resolving.  GDB just searches symbols in
arbitrary order and when it finds one it succeeds.

This may be a wrong/dead copy in the case of copy relocations.  One should fix
it sooner it is pretty serious.

common/.bss variables from shared libraries not displayed correctly
http://sourceware.org/bugzilla/show_bug.cgi?id=11717


Thanks,
Jan

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

* Re: Accessing std::cout and std::cin from within gdb.
  2011-10-21  0:47 ` Jan Kratochvil
@ 2011-10-22  1:18   ` Delcypher
  0 siblings, 0 replies; 3+ messages in thread
From: Delcypher @ 2011-10-22  1:18 UTC (permalink / raw)
  To: gdb

Thanks for the reply. I'm not familiar with the inner workings of
linking but I will look into it when I have time.

Looks like this bug has been around for a while so I don't expect it
to fixed any time soon.

Thanks,
Dan.

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

end of thread, other threads:[~2011-10-21  9:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-20 17:24 Accessing std::cout and std::cin from within gdb Delcypher
2011-10-21  0:47 ` Jan Kratochvil
2011-10-22  1:18   ` Delcypher

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