MEMORY LEAK DETECTION  on HP-UNIX PLATFORMS
                                                                                                                                          
                                                                                                                                                                  
<< CONTENTS >>

Objective:
        To know about the memory leak detection on HP-UNIX Platforms with GDB.

Contents:
       1. Memory leak in a application program
       2. Memory leak in a library (shared / static)

Requirement: GDB with info leaks command availability check:
     
      # gdb -q
      (gdb) help info leaks  
      If you find the help for that command,then GDB is capable to debug the program for memory leaks.  Else the support of GDB to find the memory leaks is not available in that GDB version.

1. Memory leak in a application program                           
      Memory leak detection:
      Let us start the memory leaks detection with sample program.
      // leak.c
      #include <stdio.h>
      #include <string.h>
      main()
      {
          char *test;
          test=(char *)malloc(10*sizeof(char));
          strcpy(test,"gdbtest");
          (void)printf("Testname: %s\n",test);
      }

     Compile the program with preferred libraries and debugging symbols.
             cc -g -o leak leak.c -lrtc -lcl

     Debugging with GDB:   

# gdb -q
(gdb) file leak
Reading symbols from leak...done.
(gdb) b main
Breakpoint 1 at 0x4000950:1: file leak.c, line 6.
(gdb) set heap-check on
(gdb) r
Starting program: /muthu/leak/leak

Breakpoint 1, main () at leak.c:6
6                                      test=(char *)malloc(10*sizeof(char));
(gdb) s
warning: The Unwind info header section .IA_64_unwind_hdr is missing
 Skipping this library /usr/lib/hpux32/libcl.so.1.

7                                      strcpy(test,"gdbtest");
(gdb)
0x4000a20:0 in __milli_strcpy+0 ()+0 at leak.c:9
9                                }
(gdb)
0x4000a20:1 in __milli_strcpy+0x1 ()
(gdb)
Single stepping until exit from function __milli_strcpy,
which has no line number information.
main () at leak.c:8
8                                      (void)printf("Testname: %s\n",test);
(gdb)
Testname: gdbtest
9                                }
(gdb)
0x20000000777abcc0:0 in main_opd_entry+0x50 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function main_opd_entry,
which has no line number information.
0x20000000777abc50:0 in enter_main_opd+0x40 () from /usr/lib/hpux32/dld.so
(gdb) info leaks
Scanning for memory leaks...


10 bytes leaked in 1 blocks

No.   Total bytes     Blocks     Address     Function
0        10             1       0x40402850   main()
(gdb)

     The above memory leaks in the program is because of non-freeing the allocated memory with malloc() call.

2. Memory leak in a library (shared / static)
     Library memory leak detection:
         GDB is having the capability to detect the memory leak in the shared or static libraries. Libraries must have the debugging symbols. (ie. It must be compiled with -g option)
                 
         Let us see a example on this. A sample library as
   
         // test.c      
        #include <stdio.h>
        #include <sys/param.h>
        #include <string.h>

        /* Function which is used by other programs */
        void host()
        {
                char hostname[MAXHOSTNAMELEN];
                char *testname;

                /* Memory allocation and assigning to NULL */
                testname=(char *)malloc(1024*sizeof(char));
                bzero((void *)testname,1024*sizeof(char));

                gethostname(hostname,sizeof(hostname));
                (void)printf("\nHostname = %s\n",hostname);

                strcpy(testname,hostname);
                (void)printf("\nTestname = %s\n",testname);
        }
            
        Complation:
                cc -g -o test test.c
                While compiling this ,you will get warning messages for not having main(). Ignore the message.
       
        Static library creation:
                ar rcs libtest.a test.o
        Shared library creation:
                ld -o -b libtest.so test.o (or) ld -o -b libtest.sl test.o
                  
        Following program will use this library.
        // func.c
        #include <stdio.h>
        main()
        {
 /* Get the hostname using libtest.a library */
 char *test;
 test=malloc(100*sizeof(char));
 strcpy(test,"TESTHOST");
 host();
 free(test);
 }
            
        Compilation:
             cc -g -o func func.c $PWD/libtest.a -lrtc -lcl
                                    (or)
             cc -g -o func func.c $PWD/libtest.so -lrtc -lcl
                                    (or)
             cc -g -o func func.c $PWD/libtest.sl -lrtc -lcl      p;    
         
       Debugging with GDB:   

 
# gdb -q func
(gdb) b main
Breakpoint 1 at 0x4000880:2: file func.c, line 11.
(gdb) set heap-check on
(gdb) r
Starting program: /muthu/leak/func

Breakpoint 1, main () at func.c:11
11                      host();
(gdb) s
warning: The Unwind info header section .IA_64_unwind_hdr is missing
 Skipping this library /usr/lib/hpux32/libcl.so.1.

host () at test.c:17
17                      testname=(char *)malloc(1024*sizeof(char));
(gdb)
18                      bzero((void *)testname,1024*sizeof(char));
(gdb)
20                      gethostname(hostname,sizeof(hostname));
(gdb)
21                      (void)printf("\nHostname = %s\n",hostname);
(gdb)

Hostname = ntc63
23                      strcpy(testname,hostname);
(gdb)
24                      (void)printf("\nTestname = %s\n",testname);
(gdb)

Testname = ntc63
25              }
(gdb)
main () at func.c:12
12              }
(gdb) info leaks
Scanning for memory leaks...


1024 bytes leaked in 1 blocks

No.   Total bytes     Blocks     Address     Function
0        1024           1       0x404054a0   host()
(gdb) s
0x20000000777abcc0:0 in main_opd_entry+0x50 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function main_opd_entry,
which has no line number information.
0x20000000777abc50:0 in enter_main_opd+0x40 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function enter_main_opd,
which has no line number information.
0x20000000777b11d0:0 in dld_main_startup+0xf50 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function dld_main_startup,
which has no line number information.
warning: Cannot insert breakpoint 0: in /usr/lib/hpux32/dld.so
warning: dld.so is a shared library and will never be loaded
warning: as private, so breakpoint 0 has been ignored.
warning: Temporarily disabling shared library breakpoints:
0

Program exited normally.
(gdb)

                              
       
               I hope it is good to know the art of memory leak detection with GDB!!!      


MEMORY LEAK ANALYSIS                                                            Muthukumar Kandasamy <[email protected]>

Hosted by www.Geocities.ws

1