HP WDB Debugging Dynamic Memory Usage Errors Using HP WDB HP Part Number: 5991-7502 Published: January 2007 Edition: 1.
© Copyright 2007 Hewlett-Packard Development Company, L.
Legal Notices The information in this document is subject to change without notice. Hewlett-Packard makes no warranty of any kind with regard to this manual, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. Hewlett-Packard shall not be held liable for errors contained herein or direct, indirect, special, incidental or consequential damages in connection with the furnishing, performance, or use of this material.
Table of Contents Introduction..........................................................................................................................................11 Intended Audience................................................................................................................................11 Typographic Conventions.....................................................................................................................11 Related Information...............................
Debugging Memory Using WDB GUI..................................................................................................65 Using WDB GUI to Debug Memory-Related Problems..................................................................66 Heap and Leak Profiling Using WDB GUI.....................................................................................66 Conclusion...................................................................................................................................
List of Tables 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Documentation for HP WDB..............................................................................................................12 Compiler Options for Memory Debugging.......................................................................................16 Generic Commands for Memory Checking.......................................................................................16 Commands for Heap Profiling...........................................
List of Examples 1 Filtered Heap Reporting for Allocations Exceeding at a Particular Call-Site.............................19 2 Incremental Heap Profile........................................................................................................................21 3 Monitoring memory usage in an arena...................................................................................................26 4 Simulating out-of-memory conditions after allocations ................................
Introduction HP Wildebeest Debugger (WDB) is an HP-supported implementation of the open source debugger GDB. Apart from the normal debugging functions, it also enables you to debug memory-related errors in a program. HP WDB supports memory-debugging (using Run Time Checking (RTC)) of source-level programs written in HP C, HP aC++, and Fortran 90 on Itanium®-based systems running HP-UX 11i v2, or HP-UX 11i v3, and PA-RISC systems running HP-UX 11.
Related Information The HP WDB documentation is available at the following location: /opt/langtools/wdb/doc/ Table 1 lists the documentation available for WDB. Table 1 Documentation for HP WDB Document Location Debugging with GDB /opt/langtools/wdb/doc/gdb.pdf GDB Quick Reference Card /opt/langtools/wdb/doc /refcard_a4.pdf /opt/langtools/wdb/doc /refcard_a3.df /opt/langtools/wdb/doc /refcard.pdf (Letter Format) Getting Started with WDB /opt/langtools/wdb/doc/html/wdb/C/GDBtutorial.
Prerequisites Following are the prerequisites for debugging memory-related problems in WDB: • • • • • • The memory-debugging feature in WDB is dependent on the availability of the dynamic Linker Version B.11.19. WDB uses the heap debugging library, librtc.[sl.so], to enable memory-debugging support. The librtc.[sl|so] library is a part of the HP WDB product.
Heap corruption occurs when a program tries to free memory that is not allocated to the program. Such instances include freeing un-initialized pointers where the pointer addresses memory outside the allocated memory. (Example 17 (page 68) illustrates how WDB detects such errors.) Accessing freed memory Accessing freed memory results in heap corruption. The scramble feature is a minimal aid to detect such errors. See “Scrambling a Heap Block” (page 54) for more information.
The typical causes for logical leaks are listed below: — Leaks caused by premature allocation of memory The application allocates the memory much ahead of the actual use of the allocated memory. — Leaks caused by delayed de-allocation The application delays the freeing the allocated block beyond the actual use of the allocated memory. — Leaks caused by failure to utilize allocated memory The application allocates memory, but fails to use the allocated memory.
Table 2 Compiler Options for Memory Debugging Compiler Option Description +check= [all|none|bounds|malloc|stack|uninit] The +check compiler options provide runtime checks to detect out-of-bounds array references (+check=bounds), memory leaks and heap corruption (+check=malloc), writing outside the stack frame(+check=stack), and un-initialized variables (+check=uninit). The +check=all option enables all the available runtime checks for the +check compiler option.
WDB supports the following heap-analysis profiles: • • • Snapshot Profile Incremental Heap Profile Arena Profile NOTE: Heap profiling must be enabled to view heap reports. The set heap-check on command enables heap profiling also. Snapshot Profile The snapshot profile displays the outstanding heap allocations at a specific instant (probe point) at runtime. It does not display the blocks that are already freed before the probe point. Table 4 lists the basic commands used for heap profiling.
Heap Start = 0x40408000 Heap End = 0x4041a900 Heap Size = 76288 bytes Outstanding Allocations: 41558 bytes allocated in 28 blocks No. Total bytes Blocks Address 0 34567 1 4096 2 1234 3 245 [...] 5. 1 1 1 8 0x40411000 0x7bd63000 0x40419710 0x404108b0 Function foo() bar() baz() boo() To view a specific allocation, specify the allocation number as an argument to the info heap command. For example: (gdb) info heap 1 4096 bytes at 0x7bd63000 (9.86% of all bytes allocated) in bar () at test.
Example 1 Filtered Heap Reporting for Allocations Exceeding at a Particular Call-Site Sample Program 1 2 3 4 5 6 7 8 9 10 11 12 #include #include main() { int i, *arr[1000]; for (i=0; i < 1000; i++) arr[i] = malloc (49); malloc (30); set_brkpt_here(0) exit(0); } Sample Debugging Session $ gdb minheap (gdb) b set_brkpt_here (gdb) set heap-check min-heap-size 31 (gdb) run (gdb) info heap Analyzing heap ... 49000 bytes allocated in 1000 blocks No.
Incremental Heap Profile The incremental profile displays the outstanding allocations at multiple probe points in an application at runtime. This profile is analogous to processing multiple snapshot profiles. Example 2 (page 21) illustrates this feature. Table 5 lists the commands for incremental heap-profiling. Table 5 Commands for Incremental Heap-Profiling 20 Command Description set heap-check interval Starts the incremental heap growth profile.
Example 2 Incremental Heap Profile Sample Program $ cat testincremental.c 1 #include 2 #include
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 /* 4 (3 old and 1 new) interval records; as the repeat count of 100 has been exceeded. */ marker1(); /* set repeat count to 500 */ for (repeat = 0; repeat < 2; repeat++) for (i = 0; i < 2; i++) { func3(); sleep (1); } marker1(); exit(0); } Sample Debugging Session (gdb) file testincremental Reading symbols from testincremental.. done.
Arena Profile WDB enables you to view the high-level memory-usage statistics of a running application. You can analyze the memory-usage statistics to understand the memory consumption, the allocation pattern, and the heap-fragmentation of the application.
can tune malloc() to use a specific ratio of small and ordinary blocks and reduce heap-fragmentation. The holding block headers and node blocks are used for the internal data-structure and book-keeping in malloc(). The sum of the total bytes in holding block headers and node blocks determines the efficiency of malloc(). The memory allocator is more efficient when it uses less memory for the internal data-structure and bookkeeping.
Number Number Number Number Number Number of of of of of of bytes in holding block header: 912 small blocks: 3500 ordinary blocks: 9 holding blocks: 35 free ordinary blocks: 1 free small blocks: 388 Memory-Debugging Features of WDB 25
Example 3 Monitoring memory usage in an arena Sample Program $ cat malloc_1.c 1 /* test large malloc. 2 * corruption. 3 */ 4 #include 5 #include
61 { 62 f4_small(); 63 f1_small(); 64 f2_small(); 65 f3_small(); 66 } 67 set_brkpt_here(); 68 69 for (int i=0; i<1000; i++) 70 { 71 f4(); 72 f1(); 73 f2(); 74 f3(); 75 76 } 77 set_brkpt_here(); 78 } Sample Debugging Session (gdb) -leaks malloc_1.
(gdb) info heap arenas num_arenas: 1 expansion: 4096 Arena ID: 0 Total number of blocks in arena: 47 Start address: 0x4001003c Ending address: 0x40480ffc Total space: 4657088 Number of bytes in free small blocks: 69216 Number of bytes in used small blocks: 199584 Number of bytes in free ordinary blocks: 2480 Number of bytes in used ordinary blocks: 4375600 Number of bytes in holding block header: 912 Number of small blocks: 3500 Number of ordinary blocks: 9 Number of holding blocks: 35 Number of free ordina
To view the leak profile, complete the following steps: 1. Run the debugger and load the program by entering the following command: $ gdb or $ gdb –leaks 2. Enable leak checking by entering the following command: (gdb) set heap-check leaks on (if the –leaks option is not used in Step 1) NOTE: Alternatively, you can use the set heap-check on command to automatically enable the detection of leaks by toggling the set heap-check leaks on command.
Error Injection WDB supports error injection features to debug out-of-memory events in an application. It enables you to simulate out-of-memory conditions in an application and analyze the behavior of the applications under such conditions. In addition, it enables you to gain control over program execution when an out-of-memory event occurs. To simulate an out-of-memory condition, you must use the set heap-check null-check command to force malloc() to return NULL after or a random number of allocations.
Example 4 Simulating out-of-memory conditions after allocations Sample Program bash-2.05b$ cat null-check.c 1 #include 2 #include
Continuing. Breakpoint 2, test_null_check () at null-check.c:19 19 printf("Out of memory scenario simulated\n"); (gdb) bt #0 test_null_check () at null-check.c:19 #1 0x2c70 in main () at null-check.c:25 #2 0x70ee3478 in _start+0xc0 () from /usr/lib/libc.2 (gdb) c Continuing. Out of memory scenario simulated Program exited normally.
Example 5 Simulating out-of-memory conditions after bytes are allocated Sample Program $ cat null-check.c 1 #include 2 #include
Breakpoint 2, test_null_check () at null-check.c:19 19 printf("Out of memory scenario simulated\n"); (gdb) p i $1 = 4 (gdb) c Continuing. warning: Malloc is returning simulated 0x00000000 value 0x70e78e8c in __rtc_nomem_event+0x4 () from /opt/langtools/lib/librtc.sl (gdb) bt #0 0x70e78e8c in __rtc_nomem_event+0x4 () from /opt/langtools/lib/librtc.sl #1 0x70e7b554 in handle_null_check+0x134 () from /opt/langtools/lib/librtc.sl #2 0x70e7b614 in malloc+0xb4 () from /opt/langtools/lib/librtc.
Example 6 Simulating out-of-memory conditions after a random number of allocations Sample Program bash-2.05b$ cat null-random.c 1 #include 2 #include 3 4 int main() 5 { 6 int i; 7 char *a; 8 for (i=0; i <= 500; i++) { 9 a = malloc(100); 10 if (a == NULL) 11 { 12 printf("Out of memory simulated\n"); 13 } 14 } 15 exit (0); 16 } Sample Debugging Session bash-2.05b$ /opt/langtools/bin/gdb HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00 and target hppa1.1-hp-hpux11.00.
Breakpoint 2, main () at null-random.c:12 12 printf("Out of memory simulated\n"); (gdb) p i $2 = 110 (gdb) c Continuing. Out of memory simulated warning: Malloc is returning simulated 0x00000000 value 0x70e78e8c in __rtc_nomem_event+0x4 () from /opt/langtools/lib/librtc.
Event Monitoring The event monitoring commands in WDB enable you to monitor specific heap events and heap-corruption problems in an application. Monitoring Heap Events WDB enables you to monitor specific events such as the size of memory allocations, the high water mark. Table 9 lists the commands for monitoring heap events.
Example 7 Monitoring a specific address Sample Program bash-2.05b$ cat watch-addr.c 1 #include 2 3 void enable_watch(char *cp) 4 { 5 6 } 7 8 int main() 9 { 10 char *cp = (char*)malloc(100); 11 enable_watch(cp); 12 free(cp); 13 exit(0); 14 } Sample Debugging Session bash-2.05b$ /opt/langtools/bin/gdb HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00 and target hppa1.1-hp-hpux11.00. Copyright 1986 - 2001 Free Software Foundation, Inc. Hewlett-Packard Wildebeest 5.5.
Monitoring Allocations Greater Than a Specified size The set heap-check block-size command instructs WDB to stop the program and transfer the execution control to the user when the program allocates a heap block whose size is greater than or equal to . Following is the syntax for the set heap-check block-size command: set heap-check block-size Example 8 (page 40) illustrates the use of the set heap-check block-size command.
Example 8 Monitoring allocations greater than a specified size Sample Program bash-2.05b$ cat block-size.c 1 #include 2 3 int main() 4 { 5 char * cp; 6 printf("Start of the program\n"); 7 cp = (char *)malloc(1024 *1024*10); 8 free (cp); 9 exit(0); 10 } Sample Debugging Session bash-2.05b$ /opt/langtools/bin/gdb HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00 and target hppa1.1-hp-hpux11.00. Copyright 1986 - 2001 Free Software Foundation, Inc. Hewlett-Packard Wildebeest 5.5.
Monitoring the Program Heap Growth. The set heap-check heap-size command instructs WDB to stop the program and transfer execution control to the user when the program attempts to increase the heap size of the program by or more. Example 9 (page 42) illustrates the use of this command.
Example 9 Monitoring program heap growth Sample Program bash-2.05b$ cat heap-size.c 1 #include 2 3 int main() 4 { 5 char * cp; 6 printf("Start of the program\n"); 7 cp = (char *)malloc(1024 *1024*10); 8 free (cp); 9 cp = (char *)malloc(1024 *1024*8); 10 free (cp); 11 exit(0); 12 } Sample Debugging Session bash-2.05b$ /opt/langtools/bin/gdb HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00 and target hppa1.1-hp-hpux11.00. Copyright 1986 - 2001 Free Software Foundation, Inc.
Monitoring Changes in Data Segment Space Allocation (High Water Mark Feature) The high water mark feature records the number of times the break value changes which is the number of times the heap grows. The high water mark feature monitors changes in the program break value. This value points to the end of the heap (which is also the end of the data segment). When memory is allocated using malloc() in excess of the available heap memory, the brk() call extends the heap. This changes the break value.
Example 10 High Water-Mark Feature Sample Program $cat high.c 1 #include 2 #include
Breakpoint 1, main () at high.c:40 40 }; (gdb) info heap high-mem Analyzing heap ... High memory mark stat High water mark updated count: 2 No. Total bytes 0 100 (gdb) Blocks 1 Address 0x4044ff20 Function func2() Case 2: The set heap-check high-mem-count command stops execution when the break value has moved of times. $ gdb high HP gdb 5.6 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x. Copyright 1986 - 2001 Free Software Foundation, Inc. Hewlett-Packard Wildebeest 5.5.
Monitoring De-allocations to Detect Double-Frees The set heap-check free command enables you to detect double-frees and frees with improper arguments. When this command is enabled, the free() calls are monitored to verify whether the parameters address valid heap blocks. If an erroneous free() is detected, the debugger stops execution and reports the error. You can analyze the stack trace to analyze where and how the error occurred.
Example 11 Monitoring heap-corruption caused by erroneous handling of string functions Sample Program bash-2.05b$ cat string.c 1 #include 2 3 int main() 4 { 5 char *ptr, *ptr1; 6 7 ptr = (char*)malloc(10); 8 ptr1 = (char *)malloc(20); 9 10 strcpy(ptr, "Hello"); 11 strcpy(ptr1, "Welcome to HP WDB"); 12 13 memcpy(ptr+5,ptr1,10); 14 } Sample Debugging Session bash-2.05b$ /opt/langtools/bin/gdb string HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00 and target hppa1.1-hp-hpux11.00.
Detecting Out-of-Bounds Writes with the Bounds-Checking Feature The set heap-check bounds command toggles the bounds-checking feature in WDB. When bounds-checking is enabled, WDB allocates extra space (guard bytes) at the beginning and end of a block during allocation and fills this space with a specific pattern. When the blocks are freed, the debugger verifies if the patterns are intact. If the patterns are corrupted, the debugger detects underflow or overflow errors and reports the corruption.
Example 12 Bounds-checking to detect out-of-bounds writes Sample Program bash-2.05b$ cat bounds.c 1 #include 2 3 int main() 4 { 5 char *cp = (char*)malloc(100); 6 cp[-1] = 100; 7 strcpy(cp,"Hello"); 8 cp[100] = 100; 9 free(cp); 10 exit(0); 11 } Sample Debugging Session bash-2.05b$ gdb bounds HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00 and target hppa1.1-hp-hpux11.00. Copyright 1986 - 2001 Free Software Foundation, Inc. Hewlett-Packard Wildebeest 5.5.
Ignore top 4 frames belonging to leak detection library of gdb. 0x70e78d7c in __rtc_event+0 () from /opt/langtools/lib/librtc.sl (gdb) f 4 #4 0x4000960:0 in main () at bounds.
Detecting Heap Corruption The info corruption command enables you to view the corruption profile of all the allocations that are corrupted at a specified probe-point in the program. Ensure that the bounds checking is enabled before using the info corruption command. The corruption information is written to a specified file if the is provided. Otherwise, it is written to stdout.
Example 13 Detecting heap corruption using the info corruption command Sample Program $cat infobounds.c 1 #include 2 #include
1 10 1 0x40012500 sm_malloc() 2 10 1 0x40012520 sm_malloc() (gdb) info corruption 2 10 bytes at 0x40012520 (33.33% of all bytes allocated) #0 sm_malloc() at infobounds.c:12 #1 main() at infobounds.c:21 #2 main_opd_entry() from /usr/lib/hpux32/dld.
Scrambling a Heap Block The set heap-check scramble command enables you to scramble a heap block and overwrite it with a specific pattern ("0xfeedface") when it is allocated or de-allocated. If the application continues to use (read) a freed block (incorrect memory usage), the application fails to find the expected data in the block. (This means that the data in the block is different from the initial data that was written in the block.
Settings to Manage Performance Degradation. Memory-debugging slows down the performance of an application by 20-40% because of stack unwinding. Reducing the number of stack frames the debugger collects for each allocation reduces the performance degradation. Table 12 lists the options for reducing the performance degradation. Table 12 Options for Performance Improvement Setting Command Description Stack Depth set heap-check frame-count Controls the depth of the call stack.
4. To generate a leak profile at the breakpoint, enter the following command: (gdb)info leaks 5. To generate a snapshot heap profile at the breakpoint, enter the following command: (gdb) info heap Debugging in Batch Mode In this mode, the user does not interactively issue commands in a debugger session. Instead, the memory-debugging commands are stored in a user-specified configuration file.
• For 64-bit applications running on PA-RISC LD_PRELOAD=/opt/langtools/lib/pa20_64/librtc.sl Overriding the Default Location for librtc.[sl.so] By default WDB uses the librtc.[sl|so], available at the location /opt/langtools/lib. If the application requires the use of librtc.[sl|so] at a different location, you must set the environment variable, LIBRTC_SERVER, to point to the location where the library is located.
Table 13 Supported Variables in the Batch Mode Configuration File (continued) Command Description set heap-check string Enables validation of calls to strcpy(), strncpy(), memcpy(), memccpy(),memset(), memmove(), bzero(), and, bcopy() set heap-check bounds Enables you to check for out-of-bounds corruption when the block is freed files= Enables you to specify the executables for which memory leak detection is enabled.
Table 14 The config_strings Options for RTC_MALLOC_CONFIG (continued) config_string Options Description abort_on_nomem=[01] RTC_NO_ABORT must not be set. If abort_on_nomem is set to 1, the batch mode aborts execution when an out-of-memory condition is detected. mem_logfile=stderr[+]filename heap_logfile=stderr[+]filename leak_logfile=stderr[+]filename The appropriate logfiles for the heap, leak, and corruption detection are displayed on stderr.
NOTE: If the application invokes calls such as system(3s), and popen(), which invoke a new shell, librtc.[sl|so] must not be loaded to the invoked shell. You must use LD_PRELOAD_ONCE, instead of LD_PRELOAD, to exclusively load thelibrtc.[sl|so] file to the calling process only. Following is the syntax for using LD_PRELOAD_ONCE: LD_PRELOAD_ONCE= /opt/langtools/lib/librtc.sl Example 15 (page 61) illustrates the batch mode debugging of the memtest.c program. The debugging results are stored in memtest.8494.
Example 15 Batch Mode Debugging for a 32-bit Application running on Itanium Sample Program $ cat memtest.c 1 #include 2 #include 3 #include 4 5 #include
(2) (3) (4) (5) 0x60000000cac22da0 rtc_record_free + 0x380 at ../../../Src/gnu/gdb/infrtc.c:2651 [/opt/langtools/lib/hpux32/librtc.sl] 0x60000000cac17200 __rtc_free + 0x160 at ../../../Src/gnu/gdb/infrtc.c:2977 [/opt/langtools/lib/hpux32/librtc.sl] 0x0000000004000bc0 main + 0x230 at memtest.c:25[./memtest] 0x60000000c0029000 main_opd_entry + 0x50[/usr/lib/hpux32/dld.so] $ cat .//memtest.8494.leaks 40 bytes leaked in 10 blocks No.
Debugging Multiple Applications in Batch Mode To debug multiple applications in the batch mode, complete the following steps: 1. 2. 3. Compile the source files. Set the following variables in the rtcconfig configuration file: Set the required environment variables as follows: export BATCH_RTC=on 4. To load the target application, set the environment variable, LD_PRELOAD as follows: • For 32-bit applications running on Itanium LD_PRELOAD=/opt/langtools/lib/hpux32/librtc.
LD_PRELOAD=/opt/langtools/lib/pa20_64/librtc.sl 2. Identify the required process (using the ps command) and attach the debugger to the process as follows. gdb -leaks 3. Insert breakpoints at suitable probe-points. When the breakpoints trigger, use the info heap and info leaks commands to display the heap and leak profile.
Table 15 Commonly Used Commands for Memory Debugging (continued) Description Interactive Mode/Attach Mode Batch Mode Enables the user to gain control over catch nomem an out-of-memory event. The user can step through program execution after the nomem event is detected.
• • • • Scrambles previous memory contents on malloc() or free() calls Stops if the following block address is allocated or de-allocated Stops program execution when an allocation causes heap growth exceeding bytes Collects memory leak data (equivalent to info leak) and memory-usage (equivalent to info heap) data WDB GUI supports both interactive and attach mode of memory-debugging. It does not support the batch mode debugging of applications.
Example 16 Detecting a double free error Sample Program $ cat double-free.c #include 1 2 int main() 3 { 4 5 printf("Starting program\n"); 6 char* han = (char*)malloc(sizeof(char)); 7 free(han); 8 printf("Now freeing a pointer twice...\n"); 9 free(han); 10 } Sample Debugging Session (gdb) set heap-check free on (gdb) file double-free Reading symbols from double-free...done. (gdb) b main Breakpoint 1 at 0x2be4: file double-free.c, line 5 from double-free.
Example 17 Detecting de-allocation of memory that has not been initialized Sample Program $ 1 2 3 4 5 6 7 8 cat unalloc.c #include int main() { printf("Starting program\n"); char* han; free(han); } Sample Debugging Session gdb) set heap-check on (gdb) file unalloc Reading symbols from unalloc...done. (gdb) b main Breakpoint 1 at 0x2bb4: file unalloc.c, line 5 from unalloc. (gdb) r Starting program: unalloc Breakpoint 1, main () at unalloc.
Example 18 Detecting de-allocation of un-allocated blocks Sample Program $ 1 2 3 4 5 6 7 8 9 cat unit.c #include int main() { printf("Starting program\n"); int *han = (int*)malloc(sizeof(int)); han++; free(han); } Sample Debugging Session (gdb) set heap-check on (gdb) file uninit Reading symbols from uninit...done. (gdb) b main Breakpoint 1 at 0x2bdc: file uninit.c, line 5 from uninit. (gdb) r Starting program: uninit Breakpoint 1, main () at uninit.
Example 19 Detecting memory leaks that are caused when an application overwrites a pointer that currently addresses a block of memory with another address or data Sample Program $ cat memleak1.c 1 #include 2 3 int main() { 4 5 printf("Starting program\n"); 6 int* han1 = (int*)malloc(sizeof(int)); 7 int* han2 = (int*)malloc(sizeof(int)); 8 han1 = han2; 9 free(han1); 10 } Sample Debugging Session (gdb) set heap-check on (gdb) file memleak1 Reading symbols from memleak1...done.
Example 20 Detecting memory leaks that are caused when a pointer variable in an application addresses memory that is out of the scope of the application Sample Program $ cat memleak2.c 1 #include
Example 21 Detecting memory leaks when you free a structure or an array that has pointers which are not freed. Sample Program $ cat memleak3.c 1 #include 2 3 struct stud 4 { 5 char* name; 6 int id; 7 }; 8 9 int main() { 10 11 struct stud *s1; 12 s1 = (struct stud*)malloc(sizeof(struct stud)); 13 s1->name = (char*)malloc(50); 14 strcpy(s1,"Annie"); 15 s1->id=10; 16 free(s1); 17 } Sample Debugging Session gdb) set heap-check on (gdb) file memleak3 Reading symbols from memleak3...done.
FAQ 1 Does WDB report all the leaks in a program? WDB uses a conservative leak detection algorithm. As a result, all leaks may not be reported, but all reported leaks are definite leaks. WDB reports leaks only in the code path exercised in the current run. 2 I wrote a small sample program that allocates a block using malloc() and leaks the block immediately, by assigning NULL to the pointer, but WDB does not report this block as a leak.
C catch nomem , 30 D Debugging Memory Using WDB GUI, 65 E Environment Variables for Batch Mode , 56 Error Injection, 30 Event Monitoring, 37 H Heap Corruption, 13 Heap Profiling, 17 I Incremental Heap Profiling, 20 info corruption, 51 info heap , 17 info heap arenas, 23 info heap high-mem , 43 info heap process, 23 info heap-interval , 20 info leaks, 28 Intended Audience, 11 Interactive Mode , 55 Interactive, Batch and Attach Mode , 55 L LD_PRELOAD, 56 Leak Profiling, 28 M Memory L
set heap-check reset, 20 set heap-check scramble , 54 set heap-check string , 46 set heap-check watch , 37 show heap-check, 16 Summary of Commands, 64 W WDB, 11 WDB Documentation, 12 FAQ 75