[PATCH] Enable Access to containing scope variables from nested inlined function

Sharma, Alok Kumar AlokKumar.Sharma@amd.com
Sun Jul 24 18:16:23 GMT 2022


[Public]

Hi all,

Currently inlined functions are barred from accessing any containing
local scope variable, which makes sense for global scoped functions.

The gcc compiler supports nested (local scoped) function for end user,
the Clang compiler does not allow nested function for end user but
compiler itself generates such artificial functions internally ( in
case of Openmp). General barrier by gdb reduces debug experience,
current patch enhances debugging in such cases.

Please consider below program
---------------------
     1  #include <stdio.h>
     2
     3  __attribute__((always_inline)) inline int outer_fun (int arg) {
     4    // Inlined at (A) or not, this function should not have access to
     5    // variables caller_fun_var, caller_fun_other_var in non containing
     6    // scoped function caller_fun.
     7    int outer_local = 5;
     8    outer_local = arg;
     9    printf("Value of outer_local = %d\n", outer_local);
    10  }
    11
    12  void caller_fun() {
    13    static int caller_fun_var = 9;
    14    volatile int caller_fun_other_var = 7;
    15    __attribute__((always_inline)) inline void called_fun (int arg) {
    16      // Inlined at (B) or not, this function should have access to
    17    // containing scope variables caller_fun_var, caller_fun_other_var
    18      int local = 0;
    19      local = arg;
    20      printf("Value of local = %d\n", local);
    21    }
    22
    23    outer_fun(caller_fun_other_var);  // <--- (A)
    24    called_fun(caller_fun_other_var); // <--- (B)
    25    return;
    26  }
    27
    28  int main() {
    29    caller_fun();
    30    return 0;
    31  }
-----------------------
Which produces DWARF as below
-----------------------
0x00000094:   DW_TAG_subprogram
                DW_AT_name      ("main")
0x000000b2:   DW_TAG_subprogram
                DW_AT_name      ("caller_fun")
0x000000d0:     DW_TAG_variable
                  DW_AT_name    ("caller_fun_var")
0x000000dd:     DW_TAG_variable
                  DW_AT_name    ("caller_fun_other_var")
0x000000ec:     DW_TAG_subprogram
                  DW_AT_name    ("called_fun")
0x00000112:     DW_TAG_inlined_subroutine
                  DW_AT_abstract_origin (0x00000169 "outer_fun")
0x0000013f:     DW_TAG_inlined_subroutine
                  DW_AT_abstract_origin (0x000000ec "called_fun")
0x00000169:   DW_TAG_subprogram
                DW_AT_external  (true)
                DW_AT_name      ("outer_fun")
-----------------------
Please note concrete instances of abstract functions "called_fun" (0x000000ec)
and "outer_fun" (0x00000169) are DIE:0x00000112 and DIE:0x0000013f. Both
have DWARF tag DW_TAG_inlined_subroutine.
To stop "outer_fun" (0x00000112) from accessing scope of "caller_fun", gdb
generalize barrier for DW_TAG_inlined_subroutine.
GDB should instead check DW_AT_abstract_origin attribute to check actual scope
of corresponding abstract functions and allow "called_fun" to access scope of
"caller_fun".

Current patch stores this information into data structure DIE_BLOCK and uses
that to compute actual containing scope.

Please review the patch and let me know your comments.

Regards,
Alok
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Enable-Access-to-containing-scope-variables-from-nes.patch
Type: application/octet-stream
Size: 25669 bytes
Desc: 0001-Enable-Access-to-containing-scope-variables-from-nes.patch
URL: <https://sourceware.org/pipermail/gdb-patches/attachments/20220724/02a887b1/attachment-0001.obj>


More information about the Gdb-patches mailing list