This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Global object constructors


> Is anyone aware of any facilities, or recommended techniques, for
> debugging global object constructors? Assume you have 500k of source
> code that you have been given, and must identify a problem causing
> heap corruption before main() is called. Ideally I'd like to be able
> to walk the global constructor list and thus find the objects and
> place appropriate checks in their constructors. I've been playing with
> __static_initializers_and_destructors_0, but gdb gives all sorts of
> spurious information from it. Identifying the global objects manually
> by browinsg source files is tedious at best.
> 
> Any information is greatly appreciated,

As a starting point, please share with us what platform you are
working on. Assuming it is ELFish, here is how you could approach the
problem:

- set a break point on __do_global_ctors_aux. Unfortunately, in the
  presence of shared libraries, you have more than one of those. In
  that case:
  - either link statically, or
  - using nm, find the address of the copy of __do_global_ctors_aux
    that you want to investigate. This works best for the main
    executable, because there won't be relocations. In gdb, do
    something like
    b *0x08048440

- read the code of __do_global_ctors_aux to see what it does. Even
  better, compile it with -g :-(

- Singe-step through that, with instruction steps (si). This will
  eventually get into
  __static_initializers_and_destructors_0. Single-step through that,
  too, until you see that source information is fine.

- When you leave a destructor, make sure you continue on a
  per-instruction basis, as line-wise stepping in these functions does
  not work.

Hope this helps,
Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]