Debugging with Gdb the Gnu Source-Level Debugger
Total Page:16
File Type:pdf, Size:1020Kb
Debugging with gdb The gnu Source-Level Debugger Ninth Edition, for gdb version 6.4 Richard Stallman, Roland Pesch, Stan Shebs, et al. (Send bugs and comments on gdb to [email protected].) Debugging with gdb TEXinfo 2004-02-19.09 Copyright c 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 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 copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being \Free Software" and \Free Software Needs Free Documentation", with the Front-Cover Texts being \A GNU Manual," and with the Back-Cover Texts as in (a) below. (a) The Free Software Foundation's Back-Cover Text is: \You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development." i Table of Contents Summary of gdb .............................. 1 Free software .................................................... 1 Free Software Needs Free Documentation .......................... 1 Contributors to gdb.............................................. 3 1 A Sample gdb Session...................... 7 2 Getting In and Out of gdb ................ 11 2.1 Invoking gdb .............................................. 11 2.1.1 Choosing files ......................................... 12 2.1.2 Choosing modes ....................................... 13 2.1.3 What gdb does during startup ......................... 15 2.2 Quitting gdb .............................................. 16 2.3 Shell commands ............................................ 16 2.4 Logging output ............................................ 17 3 gdb Commands........................... 19 3.1 Command syntax .......................................... 19 3.2 Command completion ...................................... 19 3.3 Getting help ............................................... 21 4 Running Programs Under gdb ............. 25 4.1 Compiling for debugging .................................... 25 4.2 Starting your program ...................................... 26 4.3 Your program's arguments .................................. 27 4.4 Your program's environment ................................ 28 4.5 Your program's working directory ........................... 29 4.6 Your program's input and output............................ 29 4.7 Debugging an already-running process ....................... 30 4.8 Killing the child process .................................... 31 4.9 Debugging programs with multiple threads ................... 31 4.10 Debugging programs with multiple processes ................ 33 5 Stopping and Continuing .................. 35 5.1 Breakpoints, watchpoints, and catchpoints ................... 35 5.1.1 Setting breakpoints .................................... 36 5.1.2 Setting watchpoints .................................... 39 5.1.3 Setting catchpoints .................................... 42 5.1.4 Deleting breakpoints ................................... 43 5.1.5 Disabling breakpoints .................................. 44 5.1.6 Break conditions....................................... 44 ii Debugging with gdb 5.1.7 Breakpoint command lists .............................. 46 5.1.8 Breakpoint menus ..................................... 47 5.1.9 \Cannot insert breakpoints" ............................ 48 5.1.10 \Breakpoint address adjusted..." ....................... 48 5.2 Continuing and stepping .................................... 49 5.3 Signals .................................................... 52 5.4 Stopping and starting multi-thread programs ................. 54 6 Examining the Stack ...................... 57 6.1 Stack frames ............................................... 57 6.2 Backtraces ................................................. 58 6.3 Selecting a frame ........................................... 60 6.4 Information about a frame .................................. 61 7 Examining Source Files ................... 63 7.1 Printing source lines ........................................ 63 7.2 Editing source files ......................................... 64 7.2.1 Choosing your editor ................................... 65 7.3 Searching source files ....................................... 65 7.4 Specifying source directories................................. 65 7.5 Source and machine code ................................... 66 8 Examining Data .......................... 69 8.1 Expressions ................................................ 69 8.2 Program variables .......................................... 70 8.3 Artificial arrays ............................................ 71 8.4 Output formats ............................................ 72 8.5 Examining memory......................................... 73 8.6 Automatic display .......................................... 75 8.7 Print settings .............................................. 76 8.8 Value history .............................................. 81 8.9 Convenience variables ...................................... 82 8.10 Registers ................................................. 83 8.11 Floating point hardware ................................... 84 8.12 Vector Unit ............................................... 85 8.13 Operating system auxiliary information ..................... 85 8.14 Memory region attributes .................................. 85 8.14.1 Attributes............................................ 86 8.14.1.1 Memory Access Mode ............................ 86 8.14.1.2 Memory Access Size .............................. 86 8.14.1.3 Data Cache ...................................... 87 8.15 Copy between memory and a file ........................... 87 8.16 How to Produce a Core File from Your Program ............. 88 8.17 Character Sets ............................................ 88 8.18 Caching Data of Remote Targets ........................... 91 9 C Preprocessor Macros ................... 93 iii 10 Tracepoints ............................. 97 10.1 Commands to Set Tracepoints.............................. 97 10.1.1 Create and Delete Tracepoints ......................... 97 10.1.2 Enable and Disable Tracepoints........................ 98 10.1.3 Tracepoint Passcounts ................................ 98 10.1.4 Tracepoint Action Lists ............................... 99 10.1.5 Listing Tracepoints .................................. 100 10.1.6 Starting and Stopping Trace Experiment .............. 100 10.2 Using the collected data .................................. 101 10.2.1 tfind n ............................................ 101 10.2.2 tdump............................................... 103 10.2.3 save-tracepoints filename ........................ 104 10.3 Convenience Variables for Tracepoints ..................... 104 11 Debugging Programs That Use Overlays ....................................... 105 11.1 How Overlays Work ...................................... 105 11.2 Overlay Commands ...................................... 106 11.3 Automatic Overlay Debugging ............................ 108 11.4 Overlay Sample Program ................................. 109 12 Using gdb with Different Languages ..... 111 12.1 Switching between source languages ....................... 111 12.1.1 List of filename extensions and languages .............. 111 12.1.2 Setting the working language ......................... 112 12.1.3 Having gdb infer the source language ................. 112 12.2 Displaying the language .................................. 112 12.3 Type and range checking ................................. 113 12.3.1 An overview of type checking ......................... 113 12.3.2 An overview of range checking ........................ 114 12.4 Supported languages ..................................... 115 12.4.1 C and C++ .......................................... 115 12.4.1.1 C and C++ operators ............................ 116 12.4.1.2 C and C++ constants ............................ 117 12.4.1.3 C++ expressions ................................. 118 12.4.1.4 C and C++ defaults ............................. 119 12.4.1.5 C and C++ type and range checks ................ 119 12.4.1.6 gdb and C ..................................... 119 12.4.1.7 gdb features for C++ ............................ 120 12.4.2 Objective-C ......................................... 121 12.4.2.1 Method Names in Commands .................... 121 12.4.2.2 The Print Command With Objective-C ........... 122 12.4.3 Fortran ............................................. 122 12.4.3.1 Fortran operators and expressions ................ 122 12.4.3.2 Fortran Defaults ................................ 122 12.4.3.3 Special Fortran commands ....................... 122 12.4.4 Pascal .............................................. 123 iv Debugging with gdb 12.4.5 Modula-2 ........................................... 123 12.4.5.1 Operators ...................................... 123 12.4.5.2 Built-in functions and procedures................. 124 12.4.5.3 Constants ...................................... 125 12.4.5.4 Modula-2 defaults ............................... 126 12.4.5.5 Deviations from standard Modula-2 .............. 126 12.4.5.6 Modula-2 type and range checks.................. 126 12.4.5.7 The scope operators :: and . .................... 127 12.4.5.8