Accessing std::cout and std::cin from within gdb.

Delcypher
2011-10-19T21:14:44Z

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.