<div dir="ltr"><div><p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Hello Everyone,</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">I need to have your thoughts on this.</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Consider the following test case --</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">-------------------------------------------</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> 1    int main(int Argc, char **Argv) {<br>
  2         int Local = 6;<br>
  3         printf("%d\n",Local);<br>
  4<br>
  5         {<br>
  6         printf("%d\n",Local);<br>
  7         int Local = 7;<br>
  8         printf("%d\n",Local);<br>
  9         }<br>
 10<br>
 11         return 0;<br>
 12  }</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">--------------------------------------------</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">When compiled in debug mode with compilers including (trunk
gcc and trunk clang) and debugging with GDB at Line No.6, the following
behavior is observed</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Breakpoint 1, main (Argc=1, Argv=0x7fffffffe458) at
MainScope.c:6<br>
6              
printf("%d\n",Local);<br>
(gdb) print Local<br>
$1 = 2102704   -- some Garbage value, </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">(gdb) info addr Local<br>
Symbol "Local" is a variable at frame base reg $rbp offset
0+-24.   -- <i>This is location of *Local* declared inside scope, but
as you may notice that the variable being referred here is from the outer
scope.</i></p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">This problem persists with both GDB and LLDB. Since we have
entered the Lexical Scope and when we try to print value of *Local*,  it
will look into the *current scope* and fetch the value if the variable exists
in scope(in case variable doesn't exist, GDB searches for it in the outer
scope). </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">This is regardless of whether the variable has actually came
into scope(or actually defined) at Line No. 7. Since DWARF already defined the
location(on stack) which will be valid for the lifetime of the variable, contrary
to when the variable is actually defined(or allocated) which is in this case
Line No. 7.</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">---------------------------------------------</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">  0x0000006d:     DW_TAG_lexical_block<br>
                  DW_AT_low_pc
 (0x00000000002016d1)<br>
                  DW_AT_high_pc
(0x000000000020170b)  </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">0x0000007a:       DW_TAG_variable<br>
                   
DW_AT_location      (DW_OP_fbreg -24)<br>
                   
DW_AT_name  ("Local")<br>
                   
DW_AT_decl_file     ("MainScope.c")<br>
                   
DW_AT_decl_line     (7)<br>
                   
DW_AT_type  (0x0000008a "int")</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">----------------------------------------------</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">The DWARF specification provides the DW_AT_start_scope
attribute to deal with this issue (Sec 3.9 Declarations with Reduced Scope
DWARFv5). This attribute aims at limiting the scope of variables within the
lexical scope in which it is defined to from where it has been declared/
defined.</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">In order to fix this issue, we want to modify llvm so that DW_AT_start_scope
is emitted for the variable in the inner block (in the above example). This
limits the scope of the inner block variable to start from the point of its
declaration. </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">For POC, we inserted DW_AT_start_scope in this inner *Local* variable, resultant dwarf after this.</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">-----------------------------</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">0x0000006d:     DW_TAG_lexical_block<br>
                  DW_AT_low_pc
 (0x00000000002016d1)<br>
                  DW_AT_high_pc
(0x000000000020170b)</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">0x0000007a:       DW_TAG_variable<br>                    <b><i> DW_AT_start_scope   (0x17) -- restricted within a subset(starting from the
point of definition(specified as an offset)) of entire ranges covered by Lex Block.</i></b><br>
                   
DW_AT_location      (DW_OP_fbreg -24)<br>
                   
DW_AT_name  ("Local")<br>
                   
DW_AT_decl_file     ("MainScope.c")<br>
                   
DW_AT_decl_line     (7)<br>
                   
DW_AT_type  (0x00000092 "int")</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">----------------------------</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">We also modified ‘gdb’ to interpret DW_AT_start_scope so
that the scope of the variable is limited from the PC where the value of
DW_AT_start_scope is. If the debugger is stopped at a point within the same
lexical block but at a PC before DW_AT_start_scope, then gdb follows the normal
search mechanism of searching in consecutive super blocks till it gets a match
or it reaches the global block. After the modification,  GDB is able to correctly show the value *6* in
our example.</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">After incorporating changes --</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">  Breakpoint 1, main (Argc=1, Argv=0x7fffffffe458) at
MainScope.c:6<br>
6              
printf("%d\n",Local);<br>
(gdb) print Local<br><b><i>
$1 = 6 --- Value retrieved from outer scope</i></b></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">(gdb)
info addr Local</p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">

</p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Symbol
"Local" is a variable at frame base reg $rbp offset 0+-20.</p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Could you guys please let us know your thoughts or suggestions
on this? Was/ Is there is an existing effort already going on to deal with this
problem? </p>

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"> </p>

<span style="font-size:11pt;font-family:Calibri,sans-serif">Even though location lists can be used to deal
with this scenario, in my experience, location lists are emitted at higher
optimization levels, and with the usage of location lists in this example, gdb
prints out <optimized out> (as expected) if it is stopped at a PC in the
same lexical block but before the point of declaration of the local variable.</span> <br></div><div><br></div><div>Thank You,<br>Sourabh Singh Tomar.</div></div>