Debugging with GDB
Total Page:16
File Type:pdf, Size:1020Kb
Debugging with GDB The gnu Source-Level Debugger HP Seventeenth Edition, for GDB January 2007 Richard Stallman, Roland Pesch, Stan Shebs, et al. (Send bugs and comments on GDB to [email protected] with copy to [email protected] ) Debugging with GDB TEXinfo 2003-02-03.16 Copyright c 2007 Free Software Foundation, Inc. Published by the Free Software Foundation 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA ISBN 1-882114-77-9 Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another lan- guage, under the above conditions for modified versions. i Table of Contents Summary of GDB............................. 1 Free software ................................................ 1 Contributors to GDB......................................... 1 1 A Sample GDB Session .................... 5 1.1 Loading the Executable .................................. 5 1.2 Setting Display width.................................... 6 1.3 Setting Breakpoints ..................................... 6 1.4 Running the executable under GDB ...................... 6 1.5 Stepping to the next line in the source program............ 6 1.6 Stepping into a subroutine ............................... 6 1.7 Examining the Stack .................................... 7 1.8 Printing Variable Values ................................. 7 1.9 Listing Source Code ..................................... 7 1.10 Setting Variable Values During a Session ................. 8 2 Getting In and Out of GDB ............... 11 2.1 Invoking GDB ......................................... 11 2.1.1 Choosing files ................................. 11 2.1.2 Choosing modes ............................... 13 2.1.3 Redirecting WDB input and output to a file ..... 15 2.2 Quitting GDB ......................................... 15 2.3 Shell commands ........................................ 16 3 GDB Commands ......................... 17 3.1 Command syntax ...................................... 17 3.2 Command completion .................................. 17 3.3 Getting help ........................................... 19 4 Running Programs Under GDB ........... 23 4.1 Compiling for debugging ................................ 23 4.2 Starting your program .................................. 23 4.3 Arguments To Your Program............................ 24 4.4 Program Environment .................................. 25 4.5 Working directory ...................................... 26 4.6 Program Input and Output ............................. 26 4.7 Debugging a Running Process ........................... 27 4.8 Killing the child process ................................ 28 4.9 Debugging programs with multiple threads ............... 28 4.10 Debugging programs with multiple processes ............ 31 ii Debugging with GDB 5 Stopping and Continuing .................. 33 5.1 Breakpoints............................................ 33 5.1.1 Setting breakpoints ............................ 33 5.1.2 Setting catchpoints ............................ 37 5.1.3 Deleting breakpoints ........................... 38 5.1.4 Disabling breakpoints .......................... 39 5.1.5 Break conditions............................... 40 5.1.6 Breakpoint command lists ...................... 41 5.1.7 Breakpoint menus ............................. 42 5.1.8 “Cannot insert breakpoints” .................... 43 5.2 Continuing and stepping ................................ 43 5.3 Signals ................................................ 46 5.4 Stopping and starting multi-thread programs ............. 48 6 Examining the Stack ...................... 51 6.1 Stack frames ........................................... 51 6.2 Stacks Without frames ................................. 51 6.3 Commands for Examining the Stack ..................... 52 6.4 Backtraces............................................. 52 6.5 Selecting a frame ....................................... 53 6.6 Information about a frame .............................. 54 7 Examining Source Files ................... 57 7.1 Printing source lines .................................... 57 7.2 Searching source files ................................... 58 7.3 Specifying source directories ............................ 59 7.4 Source and machine code ............................... 59 8 Examining Data .......................... 63 8.1 Expressions ............................................ 63 8.2 Program variables ...................................... 64 8.3 Artificial arrays ........................................ 65 8.4 Output formats ........................................ 66 8.5 Examining memory..................................... 67 8.6 Automatic display...................................... 68 8.7 Print settings .......................................... 70 8.8 Value history .......................................... 74 8.9 Convenience variables .................................. 75 8.10 Registers ............................................. 76 8.11 Printing Floating Point Values ......................... 78 8.12 Floating point hardware ............................... 78 iii 9 Using GDB with Different Languages ...... 79 9.1 Switching between source languages ..................... 79 9.1.1 List of filename extensions and languages ........ 79 9.1.2 Setting the working language ................... 80 9.1.3 Having GDB infer the source language .......... 80 9.2 Displaying the language ................................ 80 9.3 Type and range checking................................ 81 9.3.1 An overview of type checking ................... 81 9.3.2 An overview of range checking .................. 82 9.4 Supported languages ................................... 83 9.4.1 C and C++ .................................... 83 9.4.1.1 C and C++ operators .................. 84 9.4.1.2 C and C++ constants .................. 85 9.4.1.3 C++ expressions ....................... 86 9.4.1.4 C and C++ defaults ................... 87 9.4.1.5 C and C++ type and range checks ...... 88 9.4.1.6 GDB and C .......................... 88 9.4.1.7 GDB features for C++ ................. 88 9.4.2 Fortran ....................................... 89 9.4.2.1 Fortran types ......................... 90 9.4.2.2 Fortran operators ..................... 90 9.4.2.3 Fortran special issues .................. 91 10 Examining the Symbol Table ............. 93 11 Altering Execution ...................... 97 11.1 Assignment to variables................................ 97 11.2 Continuing at a different address ....................... 98 11.3 Giving your program a signal .......................... 99 11.4 Returning from a function ............................. 99 11.5 Calling program functions ............................ 100 11.6 Patching programs ................................... 100 12 GDB Files ............................. 103 12.1 Commands to specify files ............................ 103 12.2 Specifying shared library locations..................... 106 12.3 Errors reading symbol files ............................ 107 13 Specifying a Debugging Target .......... 109 13.1 Active targets........................................ 109 13.2 Commands for managing targets ...................... 109 13.3 Choosing target byte order............................ 111 iv Debugging with GDB 14 HP-UX Configuration-Specific Information ....................................... 113 14.1 Summary of HP Enhancements to GDB ............... 113 14.2 HP-UX dependencies ................................. 115 14.2.1 Linker Dependencies ......................... 115 14.2.2 Dependent Standard Library Routines for Run Time Checking ................................. 116 14.3 Supported Platforms and Modes ...................... 117 14.4 HP-UX targets....................................... 118 14.5 Support for Alternate root ............................ 118 14.6 Specifying object file directories ....................... 118 14.7 Fix and continue debugging ........................... 120 14.7.1 Fix and Continue compiler dependencies ...... 120 14.7.2 Fix and Continue restrictions ................. 121 14.7.3 Using Fix and Continue ...................... 121 14.7.4 Example Fix and Continue session ............ 122 14.8 Inline Support ....................................... 124 14.8.1 Inline Debugging in HP 9000 Systems ......... 124 14.8.2 Inline Debugging in Integrity Systems ......... 125 14.8.2.1 Debugging Inline Functions in Integrity Systems ................................. 126 14.9 Debugging memory problems ......................... 128 14.9.1 When to suspect a memory leak .............. 128 14.9.2 Memory debugging restrictions ............... 128 14.9.3 Memory Debugging Methodologies ............ 128 14.9.4 Debugging Memory in Interactive Mode ....... 129 14.9.4.1 Commands for interactive memory debugging ............................... 129 14.9.4.2 Example for interactive debugging session ........................................ 132 14.9.5 Debugging Memory in Batch Mode ........... 133 14.9.5.1 Setting Configuration Options for Batch Mode ................................... 133 14.9.5.2 Environment variable setting for Batch mode debugging ........................