HP 5992-4701 User Manual

Debugging with GDB Manual

The GNU Source-Level Debugger
HP Part Number: 5992-4701 Published: February 2009 Edition: 19
© Copyright 2009 Hewlett-Packard Development Company, L.P.
Confidential computer software. Valid license from HP required for possession, use or copying. Consistent with FAR 12.211 and
12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are
The information contained herein is subject to change without notice. The only warranties for HP products and services are set
forth in the express warranty statements accompanying such products and services. Nothing herein should be construed as
constituting an additional warranty. HP shall not be liable for technical or editorial errors or omissions contained herein.
UNIX is a registered trademark of The Open Group.
Java is a registered trademark of Sun Microsystems, Inc.

Table of Contents

Summary of GDB........................................................................................................................15
Free Software......................................................................................................................15
Contributors to GDB...........................................................................................................15
1 A Sample GDB Session.............................................................................................................19
1.1 Loading the Executable.................................................................................................19
1.2 Setting Display width....................................................................................................20
1.3 Setting Breakpoints.......................................................................................................20
1.4 Running the executable under GDB.............................................................................20
1.5 Stepping to the next line in the source program...........................................................20
1.6 Stepping into a subroutine............................................................................................21
1.7 Examining the Stack......................................................................................................21
1.8 Printing Variable Values................................................................................................21
1.9 Listing Source Code......................................................................................................22
1.10 Setting Variable Values During a Session....................................................................22
2 Getting In and Out of GDB.......................................................................................................25
2.1 Invoking GDB................................................................................................................25
2.1.1 Choosing files........................................................................................................26
2.1.2 Choosing modes....................................................................................................27
2.1.3 Redirecting WDB input and output to a file.........................................................30
2.2 Quitting GDB................................................................................................................30
2.3 Shell commands............................................................................................................31
3 GDB Commands......................................................................................................................33
3.1 Command syntax..........................................................................................................33
3.2 Command completion...................................................................................................33
3.3 Getting help...................................................................................................................35
4 Running Programs Under GDB...................................................................................................39
4.1 Compiling for debugging..............................................................................................39
4.2 Starting your program..................................................................................................39
4.3 Arguments To Your Program........................................................................................41
4.4 Program Environment..................................................................................................41
4.5 Working directory.........................................................................................................43
4.6 Program Input and Output...........................................................................................43
4.7 Debugging a Running Process......................................................................................44
4.8 Killing the child process................................................................................................45
Table of Contents 3
4.9 Debugging programs with multiple threads................................................................46
4.10 Debugging programs with multiple processes...........................................................49
5 Stopping and Continuing..........................................................................................................51
5.1 Breakpoints....................................................................................................................51
5.1.1 Setting breakpoints...............................................................................................52
5.1.2 Setting catchpoints................................................................................................56
5.1.3 Deleting breakpoints.............................................................................................58
5.1.4 Disabling breakpoints...........................................................................................58
5.1.5 Break conditions....................................................................................................59
5.1.6 Breakpoint command lists....................................................................................61
5.1.7 Breakpoint menus.................................................................................................62
5.1.8 “Cannot insert breakpoints”.................................................................................63
5.2 Continuing and stepping..............................................................................................64
5.3 Signals...........................................................................................................................67
5.4 Stopping and starting multi-thread programs..............................................................69
6 Examining the Stack.................................................................................................................71
6.1 Stack frames..................................................................................................................71
6.2 Stacks Without frames...................................................................................................72
6.3 Commands for Examining the Stack.............................................................................72
6.4 Backtraces......................................................................................................................72
6.5 Selecting a frame...........................................................................................................73
6.6 Information about a frame............................................................................................74
7 Examining Source Files.............................................................................................................77
7.1 Printing source lines......................................................................................................77
7.2 Searching source files....................................................................................................78
7.3 Specifying source directories........................................................................................79
7.4 Source and machine code..............................................................................................80
8 Examining Data.......................................................................................................................83
8.1 Expressions....................................................................................................................83
8.2 Program variables.........................................................................................................84
8.3 Artificial arrays.............................................................................................................85
8.4 Output formats..............................................................................................................86
8.5 Examining memory.......................................................................................................87
8.6 Automatic display.........................................................................................................89
8.7 Print settings..................................................................................................................90
8.8 Value history.................................................................................................................95
8.9 Convenience variables...................................................................................................96
8.10 Registers......................................................................................................................98
4 Table of Contents
8.11 Printing Floating Point Values....................................................................................99
8.12 Floating point hardware..............................................................................................99
9 Using GDB with Different Languages........................................................................................101
9.1 Switching between source languages..........................................................................101
9.1.1 List of filename extensions and languages.........................................................101
9.1.2 Setting the working language.............................................................................102
9.1.3 Having GDB infer the source language..............................................................102
9.2 Displaying the language..............................................................................................103
9.3 Type and range checking.............................................................................................103
9.3.1 An overview of type checking............................................................................103
9.3.2 An overview of range checking..........................................................................104
9.4 Supported languages...................................................................................................105
9.4.1 C and C++............................................................................................................106
9.4.1.1 C and C++ operators....................................................................................106
9.4.1.2 C and C++ constants....................................................................................108
9.4.1.3 C++ expressions...........................................................................................109
9.4.1.4 C and C++ defaults......................................................................................110
9.4.1.5 C and C++ type and range checks...............................................................110
9.4.1.6 GDB and C..................................................................................................111
9.4.1.7 GDB features for C++..................................................................................111
9.4.2 Fortran.................................................................................................................112
9.4.2.1 Fortran types...............................................................................................112
9.4.2.2 Fortran operators.........................................................................................113
9.4.2.3 Fortran special issues..................................................................................114
10 Examining the Symbol Table..................................................................................................115
11 Altering Execution.................................................................................................................119
11.1 Assignment to variables............................................................................................119
11.2 Continuing at a different address..............................................................................120
11.3 Giving your program a signal...................................................................................121
11.4 Returning from a function.........................................................................................121
11.5 Calling program functions........................................................................................122
11.6 Patching programs.....................................................................................................122
12 GDB Files.............................................................................................................................125
12.1 Commands to specify files........................................................................................125
12.2 Specifying shared library locations...........................................................................130
12.3 Errors reading symbol files.......................................................................................131
Table of Contents 5
13 Specifying a Debugging Target..............................................................................................133
13.1 Active targets.............................................................................................................133
13.2 Commands for managing targets..............................................................................133
13.3 Choosing target byte order........................................................................................135
14 HP-UX Configuration-Specific Information.................................................................................137
14.1 Summary of HP Enhancements to GDB....................................................................137
14.2 HP-UX dependencies................................................................................................140
14.2.1 Linker Dependencies.........................................................................................140
14.2.2 Dependent Standard Library Routines for Run Time Checking......................140
14.3 Supported Platforms and Modes..............................................................................142
14.4 HP-UX targets............................................................................................................143
14.5 Support for Alternate root.........................................................................................143
14.6 Specifying object file directories................................................................................144
14.7 Fix and continue debugging......................................................................................145
14.7.1 Fix and Continue compiler dependencies.........................................................146
14.7.2 Fix and Continue restrictions............................................................................146
14.7.3 Using Fix and Continue.....................................................................................147
14.7.4 Example Fix and Continue session...................................................................148
14.8 Inline Support............................................................................................................150
14.8.1 Inline Debugging in HP 9000 Systems..............................................................150
14.8.2 Inline Debugging in Integrity Systems.............................................................151
14.8.2.1 Debugging Inline Functions in Integrity Systems.....................................152
14.9 Debugging Macros....................................................................................................153
14.9.1 Viewing and Evaluating Macro Definitions......................................................153
14.9.1.1 Compiler Options to Enable Macro Debugging.......................................154
14.9.2 Examples for Macro Debugging........................................................................155
14.10 Debugging Memory Problems................................................................................157
14.10.1 When to suspect a memory leak......................................................................158
14.10.2 Memory debugging restrictions......................................................................158
14.10.3 Memory Debugging Methodologies...............................................................158
14.10.4 Debugging Memory in Interactive Mode........................................................159
14.10.4.1 Commands for interactive memory debugging......................................159
14.10.4.2 Example for interactive debugging session.............................................163
14.10.5 Debugging Memory in Batch Mode................................................................164
14.10.5.1 Setting Configuration Options for Batch Mode......................................164
14.10.5.2 Environment variable setting for Batch mode debugging......................167
14.10.5.3 Example for Batch Mode RTC.................................................................169
14.10.6 Debugging Memory Interactively After Attaching to a Running Process......171
14.10.7 Configuring memory debugging settings.......................................................173
14.10.7.1 Specifying the stack depth.......................................................................173
14.10.7.2 Specifying minimum leak size................................................................173
14.10.7.3 Specifying minimum block size..............................................................174
6 Table of Contents
14.10.8 Scenarios in memory debugging.....................................................................174
14.10.8.1 Stop when freeing unallocated or deallocated blocks.............................174
14.10.8.2 Stop when freeing a block if bad writes occurred outside block
boundary................................................................................................................174
14.10.8.3 Stop when a specified block address is allocated or deallocated............175
14.10.8.4 Scramble previous memory contents at malloc/free calls.......................175
14.10.8.5 Detect dangling pointers and dangling blocks.......................................175
14.10.8.6 Detect in-block corruption of freed blocks..............................................176
14.10.8.7 Specify the amount of guard bytes for every block of allocated
memory..................................................................................................................176
14.10.9 Comparison of Memory Debugging Commands in Interactive Mode and
Batch Mode..................................................................................................................176
14.10.10 Heap Profiling................................................................................................178
14.10.10.1 Commands for heap profiling...............................................................178
14.10.10.2 info heap arena...............................................................................179
14.10.10.3 info heap arena [0 |1|2|..] blocks stacks..............................179
14.10.10.4 info module ADDRESS.....................................................................179
14.10.10.5 info heap process..........................................................................179
14.10.10.6 Example for heap profiling....................................................................179
14.10.11 Memory Checking Analysis for User Defined Memory Management
Routines.......................................................................................................................180
14.10.12 Commands to track the change in data segment value.................................180
14.11 Thread Debugging Support.....................................................................................181
14.11.1 Support for Enabling and Disabling Specific Threads....................................181
14.11.2 Backtrace Support for Thread Debugging.......................................................182
14.11.3 Advanced Thread Debugging Support...........................................................182
14.11.3.1 Pre-requisites for Advanced Thread Debugging....................................183
14.11.3.2 Enabling and Disabling Advanced Thread Debugging Features...........183
14.11.3.3 Commands to view information on pthread primitives.........................187
14.11.4 Debugging Threads Interactively After Attaching to a Process......................187
14.11.5 Thread Debugging in Batch Mode..................................................................189
14.11.5.1 Pre-requisites for Batch mode of Thread Debugging..............................190
14.11.5.2 Limitations in Batch mode of thread debugging....................................193
14.11.6 Thread Debugging in +check Mode..............................................................193
14.11.7 Known issues with Thread Debugging for Interactive and Batch mode........194
14.12 Debugging MPI Programs.......................................................................................194
14.13 Debugging multiple processes ( programs with fork and vfork calls)...............195
14.13.1 Ask mode for set follow-fork-mode......................................................195
14.13.2 Serial mode for set follow-fork-mode....................................................195
14.13.3 Support for showing unwind info...................................................................195
14.13.4 Printing CFM and PFS registers......................................................................196
14.14 Command to Search for a Pattern in the Memory Address Space..........................196
14.15 Debugging Core Files..............................................................................................200
14.15.1 Generating core files with packcore /unpackcore/getcore....................200
Table of Contents 7
14.15.2 Support for the info target Command.....................................................201
14.15.3 Support for the dumpcore command.............................................................202
14.15.3.1 Enhancements to the dumpcore command............................................202
14.15.4 Support for display of run time type information..........................................203
14.16 Printing the Execution Path Entries for the Current Frame or Thread...................203
14.16.1 Compiler Dependencies for Printing the Execution Path Entries...................204
14.16.2 Example Illustrating Execution Path Recovery...............................................205
14.17 Command to Unwind Beyond 10000 Frames..........................................................206
14.18 Invoking GDB Before a Program Aborts.................................................................207
14.19 Aborting a Command Line Call..............................................................................207
14.20 Instruction Level Stepping.......................................................................................208
14.21 Enhanced support for watchpoints and breakpoints..............................................208
14.21.1 Deferred watchpoints......................................................................................208
14.21.2 Hardware watchpoints....................................................................................208
14.21.3 Hardware breakpoints.....................................................................................208
14.21.3.1 Setting breakpoints in unstripped shared library...................................209
14.21.4 Support for procedural breakpoints................................................................209
14.21.5 Support for template breakpoints...................................................................209
14.22 Debugging support for shared libraries..................................................................210
14.22.1 Using shared library as main program...........................................................210
14.22.2 Setting Deferred Breakpoints in Shared Library.............................................211
14.22.3 Using catch load..............................................................................................211
14.22.4 Privately mapping shared libraries.................................................................211
14.22.5 Selectively Mapping Shared Libraries As Private...........................................212
14.22.6 Setting breakpoints in shared library..............................................................213
14.22.7 Enhancement to the info shared Command..............................................213
14.23 Debugging support for Decimal Floating Point data type......................................213
14.23.1 Printing Decimal Floating point data types....................................................213
14.23.1.1 Printing Decimal floating point constant................................................214
14.23.1.2 Printing Decimal floating point variable.................................................214
14.23.2 Printing NaT Registers....................................................................................214
14.23.3 Handling Decimal Floating Point Data types..................................................214
14.23.4 Evaluating Decimal Floating Point data types................................................214
14.23.4.1 Printing type of Decimal Floating Point variable....................................215
14.24 Additional Support for binary floating point data type..........................................216
14.24.1 Support for Binary Floating Point constants f, l..............................................216
14.24.2 Support Binary Floating Point variables with format specifier.......................216
14.25 Language support....................................................................................................217
14.25.1 Enhanced Java Debugging Support................................................................217
14.25.1.1 Java Stack Unwind Features....................................................................217
14.25.1.2 gdb Subcommands for Java VM Debugging...........................................218
14.25.1.3 Java corefile debugging support.............................................................220
14.25.1.4 Java attach mode debugging support.....................................................220
14.25.2 Enhanced support for C++ templates..............................................................221
8 Table of Contents
14.25.3 Support for _ _fpreg data type on IPF.........................................................222
14.25.4 Support for _Complex variables in HP C........................................................222
14.25.5 Support for debugging namespaces................................................................222
14.25.6 Command for evaluating the address of an expression..................................223
14.26 Viewing Wide Character Strings.............................................................................223
14.27 Support for output logging......................................................................................224
14.27.1 Support for dumping array in an ASCII file...................................................224
14.27.2 Support for Fortran array slices.......................................................................225
14.27.3 Displaying enumerators..................................................................................225
14.27.4 Support for debugging typedefs.....................................................................225
14.27.5 Support for steplast command for C and C++.................................................225
14.28 Getting information from a non-debug executable.................................................226
14.29 Debugging optimized code.....................................................................................227
14.29.1 Debugging Optimized Code at Various Optimization Levels........................229
14.29.1.1 +O0 and +O1............................................................................................229
14.29.1.2 +O2/+O3/+O4/-ipo..............................................................................229
14.30 Debugging with ARIES...........................................................................................230
14.30.1 Debugging the application using GDB under ARIES.....................................231
14.30.1.1 Limitations of GDB Support under ARIES.............................................231
14.30.2 Attaching GDB to an already running emulated process...............................232
14.30.3 Detecting memory leaks using GDB under ARIES.........................................232
14.31 Visual Interface for WDB.........................................................................................233
14.31.1 Starting and stopping Visual Interface for WDB.............................................233
14.31.2 Navigating the Visual Interface for WDB display...........................................234
14.31.3 Specifying foreground and background colors...............................................235
14.31.4 Using the X-window graphical interface.........................................................235
14.31.5 Using the TUI mode........................................................................................236
14.31.6 Changing the size of the source or debugger pane.........................................236
14.31.7 Using commands to browse through source files...........................................237
14.31.8 Loading source files.........................................................................................237
14.31.9 Editing source files..........................................................................................237
14.31.10 Editing the command line and command-line history..................................237
14.31.11 Saving the contents of a debugging session to a file.....................................237
14.32 Support for ddd.......................................................................................................238
14.33 Support for XDB commands....................................................................................238
14.33.1 stop in/at dbx commands........................................................................238
14.34 GNU GDB Logging Commands..............................................................................238
14.35 Support for command line calls in a stripped executable.......................................238
14.35.1 Support for command line calls in a stripped executable on PA-RISC
systems........................................................................................................................239
14.35.2 Additional support for command line calls in a stripped executable.............239
14.35.2.1 For 32-bit applications:............................................................................239
14.35.2.2 For 64-bit applications.............................................................................240
14.35.3 Support for debugging stripped binaries........................................................240
Table of Contents 9
14.35.3.1 Printing of locals and globals in a stripped module...............................240
14.35.3.2 Backtrace on stripped frames..................................................................240
14.35.3.3 Command line calls to non-stripped library...........................................240
14.35.3.4 Setting breakpoints in unstripped shared library...................................240
14.36 Displaying the current block scope information.....................................................241
14.37 Linux support..........................................................................................................241
15 The HP-UX Terminal User Interface..........................................................................................243
15.1 Starting the TUI.........................................................................................................243
15.2 Automatically running a program at startup............................................................244
15.3 Screen Layouts...........................................................................................................244
15.3.1 Source pane.......................................................................................................245
15.3.2 Disassembly pane..............................................................................................245
15.3.3 Source/Disassembly pane..................................................................................246
15.3.4 Disassembly/Register pane...............................................................................246
15.3.5 Source/Register pane.........................................................................................247
15.4 Cycling through the panes........................................................................................248
15.5 Changing pane focus.................................................................................................248
15.6 Scrolling panes...........................................................................................................250
15.7 Changing the register display...................................................................................250
15.8 Changing the pane size.............................................................................................251
15.9 Refreshing and updating the window......................................................................252
16 XDB to WDB Transition Guide................................................................................................253
16.1 By-function lists of XDB commands and HP WDB equivalents...............................253
16.1.1 Invocation commands.......................................................................................254
16.1.2 Window mode commands................................................................................254
16.1.3 File viewing commands....................................................................................255
16.1.4 Source directory mapping commands..............................................................256
16.1.5 Data Viewing and modification commands.....................................................256
16.1.6 Stack viewing commands..................................................................................258
16.1.7 Status-viewing command..................................................................................259
16.1.8 Job control commands.......................................................................................259
16.2 Overall breakpoint commands..................................................................................260
16.2.1 Auxiliary breakpoint commands......................................................................260
16.2.2 Breakpoint creation commands.........................................................................261
16.2.3 Breakpoint status commands............................................................................262
16.2.4 All-procedures breakpoint commands.............................................................263
16.2.5 Global breakpoint commands...........................................................................263
16.2.6 Assertion control commands.............................................................................264
16.2.7 Record and playback commands......................................................................264
16.2.8 Macro facility commands..................................................................................264
16.2.9 Signal control commands..................................................................................265
10 Table of Contents
16.2.10 Miscellaneous commands................................................................................265
16.3 XDB data formats and HP WDB equivalents............................................................266
16.4 XDB location syntax and HP WDB equivalents........................................................268
16.5 XDB special language operators and HP WDB equivalents.....................................268
16.6 XDB special variables and HP WDB equivalents......................................................269
16.7 XDB variable identifiers and HP WDB equivalents..................................................270
16.8 Alphabetical lists of XDB commands and HP WDB equivalents..............................270
16.8.1 A........................................................................................................................270
16.8.2 B.........................................................................................................................271
16.8.3 C through D.......................................................................................................272
16.8.4 F through K........................................................................................................273
16.8.5 L.........................................................................................................................273
16.8.6 M through P.......................................................................................................274
16.8.7 Q through S.......................................................................................................275
16.8.8 T.........................................................................................................................275
16.8.9 U through Z.......................................................................................................276
16.8.10 Symbols............................................................................................................277
17 Controlling GDB...................................................................................................................281
17.1 Setting the GDB Prompt............................................................................................281
17.2 Setting Command Editing Options in GDB..............................................................281
17.3 Setting Command History Feature in GDB...............................................................281
17.4 Setting the GDB Screen Size......................................................................................283
17.5 Supported Number Formats.....................................................................................283
17.6 Optional warnings and messages..............................................................................284
17.7 Optional messages about internal happenings.........................................................285
18 Canned Sequences of Commands..........................................................................................287
18.1 User-defined commands...........................................................................................287
18.2 User-defined command hooks..................................................................................288
18.3 Command files..........................................................................................................289
18.4 Commands for controlled output..............................................................................290
19 Using GDB under gnu Emacs.................................................................................................293
20 GDB Annotations.................................................................................................................297
20.1 What is an annotation?..............................................................................................297
20.2 The server prefix........................................................................................................297
20.3 Values.........................................................................................................................298
20.4 Frames.......................................................................................................................299
20.5 Displays.....................................................................................................................301
20.6 Annotation for GDB input.........................................................................................301
Table of Contents 11
20.7 Errors.........................................................................................................................302
20.8 Information on breakpoints.......................................................................................302
20.9 Invalidation notices...................................................................................................303
20.10 Running the program..............................................................................................303
20.11 Displaying source....................................................................................................304
20.12 Annotations We Might Want in the Future.............................................................305
21 The GDB/MI Interface...........................................................................................................307
21.1 GDB/MI Command Syntax........................................................................................307
21.1.1 GDB/MI Input syntax.........................................................................................307
21.1.2 GDB/MI Output syntax......................................................................................308
21.1.3 Simple examples of GDB/MI interaction...........................................................310
21.2 GDB/MI compatibility with CLI................................................................................310
21.3 GDB/MI output records.............................................................................................311
21.3.1 GDB/MI result records.......................................................................................311
21.3.2 GDB/MI stream records.....................................................................................311
21.3.3 GDB/MI out-of-band records.............................................................................311
21.4 GDB/MI command description format......................................................................311
21.5 GDB/MI breakpoint table commands........................................................................312
21.6 GDB/MI Data manipulation.......................................................................................320
21.7 GDB/MI program control...........................................................................................330
21.8 Miscellaneous GDB commands in GDB/MI..............................................................339
21.9 GDB/MI Stack Manipulation Commands..................................................................341
21.10 GDB/MI Symbol query commands..........................................................................346
21.11 GDB/MI Target Manipulation Commands..............................................................349
21.12 GDB/MI thread commands......................................................................................353
21.13 GDB/MI tracepoint commands................................................................................355
21.14 GDB/MI variable objects..........................................................................................355
22 Reporting Bugs in GDB.........................................................................................................361
22.1 Have you found a bug?.............................................................................................361
22.2 How to report bugs...................................................................................................361
A Installing GDB.......................................................................................................................365
A.1 Compiling GDB in another directory.........................................................................366
A.2 Specifying names for hosts and targets......................................................................367
A.3 configure options...................................................................................................368
12 Table of Contents
List of Tables
14-1 Memory Debugging Commands in Interactive and Batch Mode.............................177
16-1 Invocation commands...............................................................................................254
16-2 Window mode commands........................................................................................254
16-3 File viewing commands............................................................................................255
16-4 Data viewing and modification commands..............................................................257
16-5 Stack viewing commands..........................................................................................258
16-6 Status viewing commands........................................................................................259
16-7 Job control commands...............................................................................................259
16-8 Overall breakpoint commands..................................................................................260
16-9 Auxillary breakpoint commands..............................................................................261
16-10 Breakpoint creation commands.................................................................................261
16-11 Overall breakpoint commands..................................................................................262
16-12 Global breakpoint commands...................................................................................263
16-13 Macro facility commands..........................................................................................265
16-14 Signal control commands..........................................................................................265
16-15 Miscellaneous commands.........................................................................................265
16-16 Data format commands.............................................................................................266
16-17 Macro facility commands..........................................................................................268
16-18 Special language operators........................................................................................269
16-19 Special variables........................................................................................................269
16-20 Variable Identifiers....................................................................................................270
16-21 A................................................................................................................................270
16-22 B.................................................................................................................................271
16-23 C through D...............................................................................................................272
16-24 F through K...............................................................................................................273
16-25 L.................................................................................................................................273
16-26 M through P..............................................................................................................274
16-27 Q through S...............................................................................................................275
16-28 T.................................................................................................................................275
16-29 U through Z...............................................................................................................276
16-30 Symbols.....................................................................................................................277
21-1 GDB/MI Operations...................................................................................................356
13
List of Examples
14-1 Sample Output for the find command....................................................................198
14-2 Sample Commands to Print NaT Registers...............................................................214
14 List of Examples

Summary of GDB

The purpose of a debugger such as GDB is to allow you to see whatis going on “inside” another programwhile it executes―or what another program was doing at the moment it crashed.
GDB allows you to do the following:
Load the executable along with any required arguments.
Stop your program on specified blocks of code.
Examine your program when it has stopped running due to an error.
Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
You can use GDB to debug programs written in C, C++, and Fortran. For more information, refer to the “Supported languages” (page 105). For more information on supported languages, refer to the “C and C++” (page 106).
GDB can be used to debug programs written in Fortran, although it may be necessary to refer to some variables with a trailing underscore. See “Fortran” (page 112).
This version of the manual documents WDB, implemented on HP 9000 or HP Integrity systems running Release 11.x of the HP-UX operating system. WDB can be used to debug code generated by the HP ANSI C, HP ANSI aC++ and HP Fortran compilers as well as the GNU C and C++ compilers. It does not support the debugging of Pascal, Modula-2 or Chill programs.

Free Software

GDB is free software, protected by the GNU General Public License (GPL). The GPL gives you the freedom to copy or adapt a licensed program―but every person getting a copy also gets with it the freedom to modify that copy (which means that they must get access to the source code), and the freedom to distribute further copies. Typical software companies use copyrights to limit your freedoms; the Free Software Foundation uses the GPL to preserve these freedoms.
Fundamentally, the General Public License is a license which says that you have these freedoms and that you cannot take these freedoms away from anyone else.

Contributors to GDB

Richard Stallman was the original author of GDB, and of many other GNU programs. Many others have contributed to its development. This section attempts to credit major contributors. One of the virtues of free software is that everyone is free to contribute to it; with regret, we cannot actually acknowledge everyone here. The file 'ChangeLog' in the GDB distribution approximates a blow-by-blow account.
Changes much prior to version 2.0 are lost in the mists of time.
Free Software 15
Plea: Additions to this section are particularly welcome. If you or your friends (or enemies, to be evenhanded) have been unfairly omitted from this list, we would like to add your names!
So that they may not regard their many labors as thankless, we particularly thank those who shepherdedGDB through major releases: Andrew Cagney (release 5.0); Jim Blandy (release 4.18); Jason Molenda (release 4.17); Stan Shebs (release 4.14); Fred Fish (releases
4.16, 4.15, 4.13, 4.12, 4.11, 4.10, and 4.9); Stu Grossman and John Gilmore (releases 4.8,
4.7, 4.6, 4.5, and 4.4); John Gilmore (releases 4.3, 4.2, 4.1, 4.0, and 3.9); Jim Kingdon (releases 3.5, 3.4, and 3.3); and Randy Smith (releases 3.2, 3.1, and 3.0).
Richard Stallman,assisted at various times by Peter TerMaat, Chris Hanson, and Richard Mlynarik, handled releases through 2.8.
Michael Tiemann is the author of most of the GNU C++ support in GDB, with significant additional contributions from Per Bothner. James Clark wrote the GNU C++ demangler. Early work on C++ was by Peter TerMaat (who also did much general update work leading to release 3.0).
GDB 4 uses the BFD subroutine library to examine multiple object-file formats; BFD was a joint project of David V. Henkel-Wallace, Rich Pixley, Steve Chamberlain, and John Gilmore.
David Johnson wrote the original COFF support; Pace Willison did the original support for encapsulated COFF.
Brent Benson of Harris Computer Systems contributed DWARF 2 support.
Adam de Boor andBradley Daviscontributed theISI OptimumV support.Per Bothner, Noboyuki Hikichi,and Alessandro Forin contributedMIPS support. Jean-Daniel Fekete contributed Sun 386i support. Chris Hanson improved the HP 9000 support. Noboyuki Hikichi and Tomoyuki Hasei contributed Sony/News OS 3 support. David Johnson contributed Encore Umaxsupport. Jyrki Kuoppala contributed Altos 3068 support. Jeff Law contributed HP PA and SOM support. Keith Packard contributed NS32K support. Doug Rabson contributed Acorn Risc Machine support. Bob Rusk contributed Harris Nighthawk CX-UX support. Chris Smith contributed Convex support (and Fortran debugging). JonathanStone contributed Pyramid support. Michael Tiemann contributed SPARC support. Tim Tucker contributed support for the Gould NP1 and Gould Powernode. Pace Willison contributed Intel 386 support. Jay Vosburgh contributed Symmetry support.
Andreas Schwab contributed M68K Linux support.
Rich Schaefer and Peter Schauer helped with support of SunOS shared libraries.
Jay Fenlason and Roland McGrath ensured that GDB and GAS agree about several machine instruction sets.
Patrick Duval, Ted Goldstein, Vikram Koka and Glenn Engel helped develop remote debugging. IntelCorporation, WindRiver Systems, AMD, and ARM contributed remote debugging modules for the i960, VxWorks, A29K UDI, and RDI targets, respectively.
16
Brian Fox is the author of the readline libraries providing command-line editing and command history.
Andrew Beers of SUNY Buffalo wrote the language-switching code, the Modula-2 support, and contributed the Languages chapter of this manual.
Fred Fish wrote most of the support for Unix System Vr4. He also enhanced the command-completion support to cover C++ overloaded symbols.
Hitachi America, Ltd. sponsored the support for H8/300, H8/500, and Super-H processors.
NEC sponsored the support for the v850, Vr4xxx, and Vr5xxx processors.
Mitsubishi sponsored the support for D10V, D30V, and M32R/D processors.
Toshiba sponsored the support for the TX39 Mips processor.
Matsushita sponsored the support for the MN10200 and MN10300 processors.
Fujitsu sponsored the support for SPARClite and FR30 processors.
Kung Hsu, Je Law, and Rick Sladkey added support for hardware watchpoints.
Michael Snyder added support for tracepoints.
Stu Grossman wrote gdbserver.
Jim Kingdon, Peter Schauer, Ian Taylor, and Stu Grossman made nearly innumerable bug fixes and cleanups throughout GDB.
The following people at the Hewlett-Packard Company contributed support for the PA-RISC 2.0 architecture, HP-UX 10.20, 10.30, and 11.x (narrow mode), HP's implementation of kernel threads, HP's aC++ compiler, and the terminal user interface: Ben Krepp, Richard Title, John Bishop, Susan Macchia, Kathy Mann, Satish Pai, India Paul, Steve Rehrauer, and Elena Zannoni. Kim Haase, Rosario de la Torre, Alex McKale, Michael Coulter, Carl Burch, Bharath Chndramohan, Diwakar Nag, Muthuswami, Dennis Handly, Subash Babu and Dipshikha Basu provided HP-specific information in this manual.
Cygnus Solutions has sponsored GDB maintenance and much of its development since
1991. Cygnus engineers who have worked on GDB full time include Mark Alexander,
Jim Blandy, Per Bothner, Kevin Buettner, Edith Epstein, Chris Faylor, Fred Fish, Martin Hunt, Jim Ingham, John Gilmore, Stu Grossman, Kung Hsu, Jim Kingdon, John Metzler, Fernando Nasser, Georey Noer, Dawn Perchik, Rich Pixley, Zdenek Radouch, Keith Seitz, Stan Shebs, David Taylor, and Elena Zannoni. In addition, Dave Brolley, Ian Carmichael, Steve Chamberlain, Nick Clifton, JT Conklin, Stan Cox, DJ Delorie, Ulrich Drepper, Frank Eigler, Doug Evans, Sean Fagan, David Henkel-Wallace, Richard Henderson, Jeff Holcomb, Jeff Law, Jim Lemke, Tom Lord, Bob Manson, Michael Meissner, Jason Merrill, Catherine Moore, Drew Moseley, Ken Raeburn, Gavin Romig-Koch, Rob Savoye, Jamie Smith, Mike Stump, Ian Taylor, Angela Thomas, Michael Tiemann, Tom Tromey, Ron Unrau, Jim Wilson, and David Zuhn have made contributions both large and small.
Contributors to GDB 17
18

1 A Sample GDB Session

This chapter describes the most common GDB commands with the help of an example.
The following topics are discussed:
Loading the Executable
Setting the Display Width
Setting Breakpoints
Running the Executable under GDB
Stepping to the next line
Stepping into a Subroutine
Examining the Stack
Printing Variable Values
Listing the Source Code
Setting Variable Values During a Debug Session
In this sample session, we emphasize user input like this: input, to make it easier to pick out from the surrounding output.
One of the preliminary versions of GNU m4 (a generic macro processor) exhibits the following bug: sometimes, when we change its quote strings from the default, the commands used to capture one macro definition within another stop working. In the following short m4 session, we define a macro foo which expands to 0000; we then use the m4 built-in defn to define bar as the same thing. However, when we change the open quote string to <QUOTE> and the close quote string to <UNQUOTE>, the same procedure fails to define a new synonym baz:
$ cd gnu/m4 //change your current directory to the location where the m4 executable is stored. $ ./m4 //run the m4 application define(foo,0000)
foo 0000 define (bar,defn('foo'))
bar 0000 changequote(<QUOTE>,<UNQUOTE>)
define(baz,defn(<QUOTE>foo<UNQUOTE>)) baz C-d
m4: End of input: 0: fatal error: EOF in string

1.1 Loading the Executable

Let us use GDB to try to see what is going on.
1.1 Loading the Executable 19
$ (gdb) m4
HP gdb 3.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00. Copyright 1986 - 2001 Free Software Foundation, Inc. Hewlett-Packard Wildebeest 3.0 (based on GDB ) is covered by the GNU General Public License. Type "show copying" to see the conditions to change it and/or distribute copies. Type "show warranty" for warranty/support.
GDB reads only enough symbol data to know where to find the rest when needed; as a result, the first prompt comes up very quickly.

1.2 Setting Display width

We now tell GDB to use a narrower display width than usual, so that examples fit in this manual.
((gdb)) set width 70
We need to see how the m4 built-in changequote works. Having looked at the source, we know the relevant subroutine is m4_changequote, so we set a breakpoint there with the GDB break command.

1.3 Setting Breakpoints

Here we describe how to set a breakpoint.
((gdb)) break m4 changequote Breakpoint 1 at 0x62f4: file builtin.c, line 879.

1.4 Running the executable under GDB

Using the run command, we start m4 under GDB control. As long as the control does not reach the m4_changequote subroutine, the program runs as usual.
((gdb)) run Starting program: /work/Editorial/gdb/gnu/m4/m4
define(foo,0000) foo
0000
To trigger the breakpoint, we call changequote. GDB suspends execution of m4, displaying information about the context where it stops.
changequote(<QUOTE>,<UNQUOTE>)
Breakpoint 1, m4_changequote (argc=3, argv=0x33c70) at builtin.c:879 879 if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))

1.5 Stepping to the next line in the source program

Now we use the command n (next) to advance execution to the next line of the current function.
20 A Sample GDB Session
((gdb)) n 882 set_quotes((argc >= 2) ? TOKEN_DATA_TEXT(argv[1])\
: nil,

1.6 Stepping into a subroutine

The set_quotes looks like a promising subroutine. We can go into it by using the command s (step) instead of next. step goes to the next line to be executed in any subroutine, so it steps into set_quotes.
((gdb)) s set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>") at input.c:530 530 if (lquote != def_lquote)

1.7 Examining the Stack

The display that shows the subroutine where m4 is now suspended (and its arguments) is called a stack frame display. It shows a summary of the stack. We can use the backtrace command (which can also be spelled bt), to see where we are in the stack as a whole: the backtrace command displays a stack frame for each active subroutine.
((gdb)) bt #0 set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>") at input.c:530 #1 0x6344 in m4_changequote (argc=3, argv=0x33c70) at builtin.c:882 #2 0x8174 in expand_macro (sym=0x33320) at macro.c:242 #3 0x7a88 in expand_token (obs=0x0, t=209696, td=0xf7fffa30) at macro.c:71 #4 0x79dc in expand_input () at macro.c:40 #5 0x2930 in main (argc=0, argv=0xf7fffb20) at m4.c:195
We step through a few more lines to see what happens. The first two times, we can use 's'; the next two times we use n to avoid falling into the xstrdup subroutine.
((gdb)) s 0x3b5c 532 if (rquote != def_rquote) ((gdb)) s 0x3b80 535 lquote = (lq == nil || *lq == '\0') ? \ def_lquote : xstrdup(lq); ((gdb)) n 536 rquote = (rq == nil || *rq == '\0') ? def_rquote\ : xstrdup(rq); ((gdb)) n 538 len_lquote = strlen(rquote);

1.8 Printing Variable Values

The last line displayed looks a little odd in the listing above; we can examine the variables lquote and rquote to see if they are in fact the new left and right quotes we specified. We use the command p (print) to view their values.
1.6 Stepping into a subroutine 21
((gdb)) p lquote $1 = 0x35d40 "<QUOTE>" ((gdb)) p rquote $2 = 0x35d50 "<UNQUOTE>"

1.9 Listing Source Code

lquote and rquote are indeed the new left and right quotes. To look at some context, we can display ten lines of source surrounding the current line with the l (list) command.
((gdb)) l 533 xfree(rquote); 534 535 lquote = (lq == nil || *lq == '\0') ? def_lquote\ : xstrdup (lq); 536 rquote = (rq == nil || *rq == '\0') ? def_rquote\ : xstrdup (rq); 537 538 len_lquote = strlen(rquote); 539 len_rquote = strlen(lquote); 540 } 541 542 void
Let usstep past the two lines that set len_lquote and len_rquote, and then examine the values of those variables.
((gdb)) n 539 len_rquote = strlen(lquote); ((gdb)) n 540 } ((gdb)) p len_lquote $3 = 9 ((gdb)) p len_rquote $4 = 7

1.10 Setting Variable Values During a Session

That certainly looks wrong, assuming len_lquote and len_rquote are meant to be the lengths of lquote and rquote respectively. We can set them to better values using the p command, since it can print the value of any expression―and that expression can include subroutine calls and assignments.
((gdb)) p len_lquote=strlen(lquote) $5 = 7 ((gdb)) p len_rquote=strlen(rquote) $6 = 9
Is that enough to fix the problem of using the new quotes with the m4 built-in defn? We can allow m4 to continue executing with the c (continue) command, and
then try the example that caused trouble initially:
22 A Sample GDB Session
((gdb)) c Continuing.
define(baz,defn(<QUOTE>foo<UNQUOTE>))
baz 0000
Success! The new quotes now work just as well as the default ones. The problem seems to have been just the two typos defining the wrong lengths. We allow m4 to exit by giving it an EOF as input:
C-d Program exited normally.
The message`Program exited normally.' is from GDB;it indicates m4 has finished executing. We can end our GDB session with the GDB quit command.
((gdb)) quit
1.10 Setting Variable Values During a Session 23
24

2 Getting In and Out of GDB

This chapter discusses how to start GDB, and exit out of it. The essentials are:
type '(gdb)' to start GDB.
type quit or C-d to exit.

2.1 Invoking GDB

Invoke GDB by running the program (gdb). Once started, GDB reads commands from the terminal until you tell it to exit.
You can also run (gdb) with a variety of arguments and options, to specify more of your debugging environment at the outset.
The command-line options described here are designed to cover a variety of situations; in some environments, some of these options may effectively be unavailable.
The most usual way to start GDB is with one argument, specifying an executable program:
(gdb) program
You can also start with both an executable program and a core file specified:
(gdb) program core
You can, instead, specify a process ID as a second argument, if you want to debug a running process:
(gdb) program 1234
would attach GDB to process 1234 (unless you also have a file named '1234'; GDB does check for a core file first).
Taking advantage of the second command-line argument requires a fairly complete operating system; when you use GDB as a remote debugger attached to a bare board, there may not be any notion of “process”, and there is often no way to get a core dump. GDB will warn you if it is unable to attach or to read core dumps.
You can run (gdb) without printing the front material, which describes GDB's non-warranty, by specifying -silent:
gdb -silent
You can further control how GDB starts up by using command-line options. GDB itself can remind you of the options available.
Type
(gdb) -help
to display all available options and briefly describe their use ('(gdb)-h' is a shorter equivalent).
2.1 Invoking GDB 25
All options and command-line arguments you give are processed in sequential order. The order makes a difference when the `-x' option is used.

2.1.1 Choosing files

When GDB starts, it reads any arguments other than options as specifying an executable file and core file (or process ID). This is the same as if the arguments were specified by the '-se' and '-c' options respectively. (GDB reads the first argument that does not have an associated option flag as equivalent to the '-se' option followed by that argument; and the second argument that does not have an associated option flag, if any, as equivalent to the '-c' option followed by that argument.)
If GDB has not been configured to included core file support, such as for most embedded targets, then it will complain about a second argument and ignore it.
Many options have both long and short forms; both are shown in the following list. GDB also recognizes the long forms if you truncate them, so long as enough of the option is present to be unambiguous. (If you prefer, you can flag option arguments with `--' rather than `-', though we illustrate the more usual convention.)
-symbols file
-s file
-exec file
-e file
-se file
Read symbol table from file file.
Use file file as the executable file to execute when appropriate, and for examining pure data in conjunction with a core dump.
Read symbol table from file file and use it as the executable file.
-core file
-c file
-c number Connect to process ID number, as with the attach
-command file
-x file
-directory directory
-d directory
26 Getting In and Out of GDB
Use file file as a core dump to examine.
command (unless there is a file in core-dump format named number, in which case `-c' specifies that file as a core dump to read).
Execute GDB commands from file file. See “Command
files” (page 289).
Add directory to the path to search for source files.
-m, -mapped
Warning: this option depends on operating system facilities that are not supported on all systems.
If memory-mapped files are available on your system through the mmap system call, you can use this option to have GDB write the symbols from your program into a reusable file in the current directory. If the program you are debugging is called '/tmp/fred', the mapped symbol file is '/tmp/fred.syms'. Future GDB debugging sessions notice the presence of this file, and can quickly map in symbol information from it, rather than reading the symbol table from the executable program.
The '.syms' file is specific to the host machine where GDB is run. It holds an exact image of the internal GDB symbol table. It cannot be shared across multiple host platforms.
-r, -readnow
You typically combine the -mapped and -readnow options in order to build a '.syms' file that contains complete symbol information. (See “Commands to specify files”
(page 125), for information on '.syms' files.) A simple GDB invocation to do nothing
but build a '.syms' file for future use is:
gdb -batch -nx -mapped -readnow programname

2.1.2 Choosing modes

You can run GDB in various alternative modes―for example, in batch mode or quiet mode.
-nx, -n
-quiet, -silent, -q
Read each symbol file's entire symbol table immediately, rather than the default, which is to read it incrementally as it is needed. This makes startup slower, but makes future operations faster.
Do not execute commands found in any initialization files (normally called '.gdbinit', or 'gdb.ini' on PCs). Normally, GDB executes the commands in these files after all thecommand optionsand argumentshave been processed. See “Command files” (page 289).
“Quiet”. Do not print the introductory and copyright messages. These messages are also suppressed in batch mode.
-batch Run in batch mode. Exit with status 0 after processing
all the command files specified with '-x' (and all commands from initialization files, if not inhibited with
2.1 Invoking GDB 27
'-n'). Exit with nonzero status if an error occurs in executing the GDB commands in the command files.
Batch mode may be useful for running GDB as a filter, for example to download and run a program on another computer; inorder to make this more useful, the message
Program exited normally.
(which isordinarily issued whenever a program running under GDB control terminates) is not issued when running in batch mode.
-nowindows, -nw
“No windows”. If GDB comes with a graphical user interface (GUI) built in, then this option tells GDB to only use the command-line interface. If no GUI is available, this option has no effect.
-windows, -w
If GDB includes a GUI, then this option requires it to be used if possible.
-cd directory
Run GDB using directory as its working directory, instead of the current directory.
-dbx Support additional dbx commands, including:
use
status (in dbx mode, status has a different
meaning than in default GDB mode.)
whereis
func
file
assign
call
stop
-fullname, -f GNU Emacs sets this option when it runs GDB as a
subprocess. It tells GDB to output the full file name and line number in a standard, recognizable fashion each time a stack frame is displayed (which includes each time your program stops). This recognizable formatlooks like two `\032' characters, followed by the file name, line number, and character position separated by colons, and a newline. The Emacs-to-GDB interface program uses the two '\032' characters as a signal to display the source code for the frame.
-epoch
The Epoch Emacs-GDB interface sets this option when it runs GDB as a subprocess. It tells GDB to modify its
28 Getting In and Out of GDB
-annotate level
-async
print routines so as to allow Epoch to display values of expressions in a separate window.
This option sets the annotation level inside GDB. Its effect is identicalto using `set annotate level' (see “GDB
Annotations” (page297)). Annotation level controls how
much information does GDB print together with its prompt, values of expressions, source lines, and other types of output. Level 0 is the normal, level 1 is for use when GDB is run as a subprocess of GNU Emacs, level 2 is the maximum annotation suitable for programs that control GDB.
Use the asynchronous event loop for the command-line interface. GDB processes all events, such as user
1
keyboard input, via a special event loop. This allows GDB to accept and process user commands in parallel with the debugged process being run1, so you do not need to wait for control to return to GDB before you type the next command.
NOTE: As of version 5.0, the target side of the asynchronous operation is not yet in place, so '-async' does not work fully yet.
When the standard input is connected to a terminal device, GDB uses the asynchronous event loop by default, unless disabled by the '-noasync' option.
-noasync
Disable the asynchronous event loop for the command-line interface.
-baud bps, -b bps
Set the line speed (baud rate or bits per second) of any serial interface used by GDB for remote debugging.
-tty device, -t device
Run using device for your program's standard input and output.
-tui
Use a Terminal User Interface. For information, use your Web browser to read the file 'tui.html', which is usually installed in the directory /opt/langtools/ wdb/doc on HP-UX systems. Do not use this option if you run GDB from Emacs (see “Using GDB under gnu
Emacs” (page 293)).
-xdb
Run in XDB compatibility mode, allowing the use of certain XDB commands. For information, see the file
1. GDB built with DJGPP tools for MS-DOS/MS-Windows supports this mode of operation, but the event
loop is suspended when the debug target runs.
2.1 Invoking GDB 29
-interpreter interp
-write
-statistics
-version
-pid
-inline
-src_no_g
'xdb_trans.html', which is usually installed in the directory /opt/langtools/wdb/doc on HP-UX systems.
Use the interpreter interp for interface with the controlling program or device. This option is meant to be set by programs which communicate with GDB using it as a back end. For example, '--interpreter=mi' causes GDB to use the gdbmi interface (see “The GDB/MI
Interface” (page 307)).
Open the executable and core files for both reading and writing. This is equivalent to the 'set write on' command inside GDB (see “Patching programs”
(page 122)).
This option causes GDB to print statistics about time and memory usage after it completes each command and returns to the prompt.
This option causes GDB to print its version number and no-warranty blurb, and exit.
This option causes GDB to attach to a running process.
This option causes the debugger to start with the inline debugging on.
This option is used to set the limited source level debugging without compiling.

2.1.3 Redirecting WDB input and output to a file

To redirect WDB input and output to a file, use either of these commands to start the debugger:
$ script log1 $ gdb
or
$ gdb | tee log1

2.2 Quitting GDB

quit [expression], q To exit GDB, use the quit command (abbreviated q), or
type an end-of-file character (usually C-d). If you do not supply expression, GDB will terminate normally; otherwise it will terminate using the result of expression as the error code.
An interrupt (often C-c) does not exit from GDB, but rather terminates the action of any GDB command that is in progress and returns to GDB command level. It is safe to
30 Getting In and Out of GDB
Loading...
+ 339 hidden pages