Bug 11836 - gdb cannot access variables that are subject to return value optimization
Summary: gdb cannot access variables that are subject to return value optimization
Status: RESOLVED INVALID
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.1
: P2 normal
Target Milestone: 7.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 9607 9640 12106 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-07-23 10:47 UTC by Andre'
Modified: 2014-05-28 19:43 UTC (History)
6 users (show)

See Also:
Host: i486-linux-gnu
Target: i486-linux-gnu
Build: i486-linux-gnu
Last reconfirmed:


Attachments
Test code and build instructions (649 bytes, text/plain)
2010-07-23 10:48 UTC, Andre'
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andre' 2010-07-23 10:47:19 UTC
#include <string>

std::string foo()
{
    std::string s;
    s = "hello";
    s += "world";
    return s;
}

std::string bar()
{
    std::string s;
    s = "hello";
    s += "world";
    return "something else";
}

int main()
{
    int a = foo().size();
    int b = bar().size();
    return a + b;
}

/*

Gcc seems to apply return value optimization (even at -O0) but there is
some problem with either the generated debug information or the way
gdb handles it. 

To reproduce it, save this file here as rvo.cpp and run

    g++ -g -O0 rvo.cpp -o rvo 

    gdb -ex 'set confirm off' -ex 'file ./rvo' \
        -ex 'b rvo.cpp:8' -ex 'b rvo.cpp:16' \
        -ex 'run' -ex 'print s'  \
        -ex 'cont' -ex 'print s' \
        -ex 'quit'


The result will look like:

    Breakpoint 1, foo () at rvo.cpp:8
    8           s += "world";
    $1 = {static npos = <optimized out>, 
      _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>>
    = {<No daa fields>}, <No data fields>}, _M_p = 0x1 <Address 0x1 out of
    bounds>}}

    Breakpoint 2, bar () at rvo.cpp:16
    16          s += "world";
    $2 = {static npos = <optimized out>, 
      _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>>
    = {<No data fields>}, <No data fields>}, _M_p = 0x804b014 "hello"}}

    Program exited with code 030.

RVO seems to be applied in the first case with "dangling" debug information.
If no RVO is used (second case), all is fine.


*/
Comment 1 Andre' 2010-07-23 10:48:22 UTC
Created attachment 4882 [details]
Test code and build instructions
Comment 2 Sami Wagiaalla 2010-07-23 14:54:34 UTC
Andre,

Thanks for the nice test case. 's' seems to have a location in the debug info
but it is either wrong or gdb is reading it wrong. I'll take a look at this when
I get a chance. If anyone else is interested, in the mean while, go for it :)

Sami
Comment 3 Andre' 2010-07-23 16:43:21 UTC
It turns out it is known on the gcc side: 

  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44731
Comment 4 Tom Tromey 2010-07-23 16:46:26 UTC
Possibly this gcc bug:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44731
Comment 5 Tom Tromey 2010-10-11 22:09:20 UTC
*** Bug 12106 has been marked as a duplicate of this bug. ***
Comment 6 Tom Tromey 2010-10-11 22:13:08 UTC
*** Bug 9640 has been marked as a duplicate of this bug. ***
Comment 7 Tom Tromey 2010-10-11 22:13:52 UTC
*** Bug 9607 has been marked as a duplicate of this bug. ***
Comment 8 Jan Kratochvil 2011-02-14 12:34:01 UTC
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44367

Breakpoint 1, foo () at rvo.C:8
8	    return s;
$1 = "helloworld"

Breakpoint 2, bar () at rvo.C:16
16	    return "something else";
$2 = "helloworld"

with /r:

Breakpoint 1, foo () at rvo.C:8
8	    return s;
$1 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
    _M_p = 0x601058 "helloworld"}}

Breakpoint 2, bar () at rvo.C:16
16	    return "something else";
$2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
    _M_p = 0x601028 "helloworld"}}

This should be a GCC-only bug which is already fixed:
FAIL g++ (GCC) 4.4.6 20110124 (prerelease)
PASS g++ (GCC) 4.5.3 20110124 (prerelease)
PASS g++ (GCC) 4.6.0 20110212 (experimental)
PASS gcc-c++-4.6.0-0.6.fc15.x86_64

Behaves the same with both:
GNU gdb (GDB) 7.2.50.20110213-cvs
gdb-7.2.50.20110206-18.fc15.x86_64

Is the bug still reproducible with recent GCC?
Comment 9 rdengler 2011-02-17 17:14:59 UTC
Hi Jan,

thanks for the information.
Sorry I couldn't test it yet, there only is g++ 4.4.3
on my system.
I suppose the confusing behaviour will have gone,
the test code is simple enough.

Regards
Richard



jan.kratochvil at redhat dot com schrieb:

>http://sourceware.org/bugzilla/show_bug.cgi?id=11836
>
>Jan Kratochvil <jan.kratochvil at redhat dot com> changed:
>
>           What    |Removed                     |Added
>----------------------------------------------------------------------------
>             Status|NEW                         |WAITING
>                 CC|                            |jan.kratochvil at redhat
>                   |                            |dot com
>
>--- Comment #8 from Jan Kratochvil <jan.kratochvil at redhat dot com> 2011-02-14 12:34:01 UTC ---
>http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44367
>
>Breakpoint 1, foo () at rvo.C:8
>8        return s;
>$1 = "helloworld"
>
>Breakpoint 2, bar () at rvo.C:16
>16        return "something else";
>$2 = "helloworld"
>
>with /r:
>
>Breakpoint 1, foo () at rvo.C:8
>8        return s;
>$1 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>>
>= {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
>    _M_p = 0x601058 "helloworld"}}
>
>Breakpoint 2, bar () at rvo.C:16
>16        return "something else";
>$2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>>
>= {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
>    _M_p = 0x601028 "helloworld"}}
>
>This should be a GCC-only bug which is already fixed:
>FAIL g++ (GCC) 4.4.6 20110124 (prerelease)
>PASS g++ (GCC) 4.5.3 20110124 (prerelease)
>PASS g++ (GCC) 4.6.0 20110212 (experimental)
>PASS gcc-c++-4.6.0-0.6.fc15.x86_64
>
>Behaves the same with both:
>GNU gdb (GDB) 7.2.50.20110213-cvs
>gdb-7.2.50.20110206-18.fc15.x86_64
>
>Is the bug still reproducible with recent GCC?
>
>  
>
Comment 10 Jan Kratochvil 2011-02-17 17:28:43 UTC
So let's say it has never been a GDB bug.
Comment 11 Jackie Rosen 2014-02-16 18:24:11 UTC Comment hidden (spam)