The attached patch adds support for debug info in C++ dynamic initializers. This is subject to -g and also __attribute__((nodebug)) which is now permitted on variables with static storage duration.<div><br></div><div>The driving reason for this change is to get useful information in backtraces. For instance, before:</div>
<div><br></div><div><div>Program received signal SIGSEGV, Segmentation fault.</div><div>0x00000000004004de in crash () at gi.cc:3</div><div>3 char x = *ptr;</div><div>(gdb) bt</div><div>#0 0x00000000004004de in crash () at gi.cc:3</div>
<div>#1 0x0000000000400505 in test () at gi.cc:7</div><div>#2 0x0000000000400519 in __cxx_global_var_init () at gi.cc:8</div><div>#3 0x0000000000400569 in global constructors keyed to a ()</div><div>#4 0x0000000000400636 in __do_global_ctors_aux ()</div>
<div>#5 0x00000000004003bb in _init ()</div><div>#6 0x00007fffffffe528 in ?? ()</div><div>#7 0x00000000004005c5 in __libc_csu_init ()</div><div>#8 0x00007ffff7a78be0 in __libc_start_main (main=<optimized out>, argc=<optimized out>, ubp_av=<optimized out>, </div>
<div> init=0x400580 <__libc_csu_init>, fini=0x7ffff7dd8300 <initial>, rtld_fini=0x7ffff7dec250 <_dl_fini>, stack_end=0x7fffffffe508)</div><div> at libc-start.c:185</div><div>#9 0x0000000000400409 in _start ()</div>
</div><div><br></div><div>which looks fine, but pay attention to the line number info for __cxx_global_var_init. After:</div><div><br></div><div><div>Program received signal SIGSEGV, Segmentation fault.</div><div>0x00000000004004de in crash () at gi.cc:3</div>
<div>3 char x = *ptr;</div><div>(gdb) bt</div><div>#0 0x00000000004004de in crash () at gi.cc:3</div><div>#1 0x00000000004004f9 in test () at gi.cc:7</div><div>#2 0x0000000000400519 in __cxx_global_var_init () at gi.cc:11</div>
<div>#3 0x0000000000400559 in global constructors keyed to a ()</div><div>#4 0x0000000000400626 in __do_global_ctors_aux ()</div><div>#5 0x00000000004003bb in _init ()</div><div>#6 0x00007fffffffe528 in ?? ()</div><div>
#7 0x00000000004005b5 in __libc_csu_init ()</div><div>#8 0x00007ffff7a78be0 in __libc_start_main (main=<optimized out>, argc=<optimized out>, ubp_av=<optimized out>, </div><div> init=0x400570 <__libc_csu_init>, fini=0x7ffff7dd8300 <initial>, rtld_fini=0x7ffff7dec250 <_dl_fini>, stack_end=0x7fffffffe508)</div>
<div> at libc-start.c:185</div><div>#9 0x0000000000400409 in _start ()</div></div><div><br></div><div>The before line was wrong. Here's the code for that program.</div><div><br></div><div><div> 1 void crash() {</div>
<div> 2 volatile char *ptr = 0;</div><div> 3 char x = *ptr;</div><div> 4 }</div><div> 5 </div><div> 6 int test() {</div><div> 7 crash();</div><div> 8 return 1;</div><div> 9 }</div>
<div> 10 </div><div> 11 static int i = test();</div><div> 12 __attribute__((nodebug)) static int j = test();</div><div> 13 </div><div> 14 int main(void) {}</div></div><div><br></div><div>It's more common to see __cxx_global_var_init given as location foo.cc:0 in the backtrace, but I wanted to show an example where that doesn't happen.</div>
<div><br></div><div>This patch includes a small cleanup to SemaDeclAttr.cpp. Tabs to spaces. "!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)" became "!isFunctionOrMethod(D)" because that's what the author actually meant. Richard Smith suggested that I change the "no address safety" attribute to use the same logic as nodebug (permitted on certain variables) but that turns out to be difficult to implement and should be in a separate patch.</div>
<div><br></div><div>Please review!</div><div><br></div><div>Nick</div>