Next: , Up: Implementation Note


18.6.1 gnatmem Using GDB Mode

gnatmem executes the user program under the control of GDB using a script that sets breakpoints and gathers information on each dynamic allocation and deallocation. The output of the script is then analyzed by gnatmem in order to locate memory leaks and their origin in the program. Gnatmem works by recording each address returned by the allocation procedure (__gnat_malloc) along with the backtrace at the allocation point. On each deallocation, the deallocated address is matched with the corresponding allocation. At the end of the processing, the unmatched allocations are considered potential leaks. All the allocations associated with the same backtrace are grouped together and form an allocation root. The allocation roots are then sorted so that those with the biggest number of unmatched allocation are printed first. A delicate aspect of this technique is to distinguish between the data produced by the user program and the data produced by the gdb script. Currently, on systems that allow probing the terminal, the gdb command "tty" is used to force the program output to be redirected to the current terminal while the gdb output is directed to a file or to a pipe in order to be processed subsequently by gnatmem.