Bug 15231 - Local variables in lambdas are not accessible
Summary: Local variables in lambdas are not accessible
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Jan Kratochvil
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-03-05 22:07 UTC by Andre'
Modified: 2016-05-30 12:20 UTC (History)
3 users (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 Andre' 2013-03-05 22:07:46 UTC
The local variable x does not seem to be "visible" for the debugger in the following:


echo "
    void breaker() {}
    struct Z
    {
        int b;
     
        Z() {
            auto a = [&]() {
                int x = 32;
                breaker();
                return x;
            };
            b = a();
        }
     
    };
     
    int main()
    {
        Z z;
        return z.b;
    }
" | g++ -g -xc++ -std=c++11 -


gdb -ex 'b breaker' -ex 'run' -ex 'up' -ex 'p x' -ex 'bt full' ./a.out



->   GNU gdb (GDB) 7.5.50.20121013-cvs
->   [...]

->    10      in <stdin>
->    No symbol "x" in current context.


->    #0  breaker () at <stdin>:2
->    No locals.
->    #1  0x0804840e in Z::Z()::{lambda()#1}::operator()() const () at
->    <stdin>:10
->    No locals.
->    #2  0x08048425 in Z::Z (this=0xbffff34c) at <stdin>:13
->            a = {<No data fields>}
->    #3  0x080483f6 in main () at <stdin>:20
->            z = {b = -1208270848}


$ gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
Comment 1 Tom Tromey 2013-03-06 02:42:56 UTC
gdb ignores the outer function due to missing PC bounds:

 <1><5a>: Abbrev Number: 8 (DW_TAG_subprogram)
    <5b>   DW_AT_specification: <0x3d>	
    <5f>   DW_AT_inline      : 2	(declared as inline but ignored)
    <60>   DW_AT_object_pointer: <0x68>	
    <64>   DW_AT_sibling     : <0x15c>	

and

  /* Ignore functions with missing or invalid low and high pc attributes.  */
  if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
    {
      attr = dwarf2_attr (die, DW_AT_external, cu);
[...]
      return;

So the lambda's locals are never processed.
Comment 2 Jan Kratochvil 2016-04-21 21:22:50 UTC
I agree the bug is due to missing PC bounds but I find the difference elsewhere.

PASS: gcc-6.0.0-0.20.fc24.x86_64
FAIL: gcc-5.3.1-6.fc23.x86_64
Tested with gdb-7.10.1-31.fc23.x86_64 and even more recent GDBs.

GDB: read_lexical_block_scope
  /* Ignore blocks with missing or invalid low and high pc attributes.  */
[...]
  if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
    return;


PASS: gcc-6.0.0-0.20.fc24.x86_64 0x1c3->0x91->0xb7
    <c>   DW_AT_producer    : (indirect string, offset: 0xa): GNU C++11 6.0.0 20160406 (Red Hat 6.0.0-0.20) -mtune=generic -march=x86-64 -g -std=c++11
[...]
 <1><91>: Abbrev Number: 11 (DW_TAG_subprogram)
    <92>   DW_AT_specification: <0x3d>
    [...]
[...]
 <2><b7>: Abbrev Number: 15 (DW_TAG_structure_type)
    <b8>   DW_AT_name        : (indirect string, offset: 0x79): <lambda()>
    [...]
[...]
 <1><1c3>: Abbrev Number: 25 (DW_TAG_subprogram)
    <1c4>   DW_AT_abstract_origin: <0x91>
    <1c8>   DW_AT_linkage_name: (indirect string, offset: 0xbc): _ZN1ZC2Ev
    [...]

FAIL: gcc-5.3.1-6.fc23.x86_64 0x1aa->0x7b->0x92
    <c>   DW_AT_producer    : (indirect string, offset: 0x9f): GNU C++11 5.3.1 20160406 (Red Hat 5.3.1-6) -mtune=generic -march=x86-64 -g
[...]
 <1><7b>: Abbrev Number: 9 (DW_TAG_subprogram)
    <7c>   DW_AT_specification: <0x3d>
    [...]
 <2><92>: Abbrev Number: 11 (DW_TAG_lexical_block)
[...]
 <3><9c>: Abbrev Number: 13 (DW_TAG_structure_type)
    <9d>   DW_AT_name        : (indirect string, offset: 0x3c): <lambda()>
    [...]
[...]
 <1><1aa>: Abbrev Number: 25 (DW_TAG_subprogram)
    <1ab>   DW_AT_abstract_origin: <0x7b>
    <1af>   DW_AT_linkage_name: (indirect string, offset: 0x77): _ZN1ZC2Ev
    [...]

Keeping the Bug for GDB open as it could be compatible with older GCCs.
Comment 3 Jan Kratochvil 2016-05-14 19:31:15 UTC
[patch] PR 15231: import bare DW_TAG_lexical_block
https://sourceware.org/ml/gdb-patches/2016-05/msg00236.html
Comment 4 Sourceware Commits 2016-05-30 12:16:44 UTC
The master branch has been updated by Jan Kratochvil <jkratoch@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e385593eef98ac92be57159e141f4b805dadbbb3

commit e385593eef98ac92be57159e141f4b805dadbbb3
Author: Jan Kratochvil <jan.kratochvil@redhat.com>
Date:   Mon May 30 14:14:43 2016 +0200

    PR 15231: import bare DW_TAG_lexical_block
    
    Local variables in lambdas are not accessible
    https://sourceware.org/bugzilla/show_bug.cgi?id=15231
    
    GDB: read_lexical_block_scope
      /* Ignore blocks with missing or invalid low and high pc attributes.  */
    [...]
      if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
        return;
    
    But sometimes there is:
    
    FAIL: gcc-5.3.1-6.fc23.x86_64
     <2><92>: Abbrev Number: 11 (DW_TAG_lexical_block)
     <3><9c>: Abbrev Number: 13 (DW_TAG_structure_type)
        <9d>   DW_AT_name        : (indirect string, offset: 0x3c): <lambda()>
        [...]
    
    Where DW_TAG_lexical_block has no attributes.  Such whole subtree is currently
    dropped by GDB while I think it should just import all its children DIEs.
    
    It even XFAIL->XPASSes gdb.ada/out_of_line_in_inlined.exp:
    	commit 0fa7fe506c242b459c4c05d331e7c7d66fb52390
    	Author: Joel Brobecker <brobecker@adacore.com>
    	    out of line functions nested inside inline functions.
    So I have removed that xfail.
    
    gdb/ChangeLog
    2016-05-30  Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	PR c++/15231
    	* dwarf2read.c (enum pc_bounds_kind): Add PC_BOUNDS_INVALID.
    	(process_psymtab_comp_unit_reader, read_func_scope): Adjust callers.
    	(read_lexical_block_scope): Import DIEs from bare DW_TAG_lexical_block.
    	(read_call_site_scope): Adjust callers.
    	(dwarf2_get_pc_bounds): Implement pc_bounds_invalid.
    	(dwarf2_get_subprogram_pc_bounds, get_scope_pc_bounds): Adjust callers.
    
    gdb/testsuite/ChangeLog
    2016-05-30  Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	PR c++/15231
    	* gdb.ada/out_of_line_in_inlined.exp: Remove xfails.
    	* gdb.dwarf2/dw2-lexical-block-bare.exp: New file.
Comment 5 Jan Kratochvil 2016-05-30 12:20:51 UTC
Fixed in trunk.