Debugging with GDB (February 2008)
Table Of Contents
- Summary of GDB
- A Sample GDB Session
- Getting In and Out of GDB
- GDB Commands
- Running Programs Under GDB
- Stopping and Continuing
- Examining the Stack
- Examining Source Files
- Examining Data
- Using GDB with Different Languages
- Examining the Symbol Table
- Altering Execution
- GDB Files
- Specifying a Debugging Target
- HP-UX Configuration-Specific Information
- Summary of HP Enhancements to GDB
- HP-UX dependencies
- Supported Platforms and Modes
- HP-UX targets
- Support for Alternate root
- Specifying object file directories
- Fix and continue debugging
- Inline Support
- Debugging Macros
- Debugging Memory Problems
- When to suspect a memory leak
- Memory debugging restrictions
- Memory Debugging Methodologies
- Debugging Memory in Interactive Mode
- Debugging Memory in Batch Mode
- Debugging Memory Interactively After Attaching to a Running Process
- Configuring memory debugging settings
- Scenarios in memory debugging
- Stop when freeing unallocated or deallocated blocks
- Stop when freeing a block if bad writes occurred outside block boundary
- Stop when a specified block address is allocated or deallocated
- Scramble previous memory contents at malloc/free calls
- Detect dangling pointers and dangling blocks
- Detect in-block corruption of freed blocks
- Specify the amount of guard bytes for every block of allocated memory
- Comparison of Memory Debugging Commands in Interactive Mode and Batch Mode
- Heap Profiling
- Memory Checking Analysis for User Defined Memory Management Routines
- Commands to track the change in data segment value
- Thread Debugging Support
- Debugging MPI Programs
- Debugging multiple processes ( programs with fork and vfork calls)
- Debugging Core Files
- Printing the Execution Path Entries for the Current Frame or Thread
- Invoking GDB Before a Program Aborts
- Aborting a Command Line Call
- Instruction Level Stepping
- Enhanced support for watchpoints and breakpoints
- Debugging support for shared libraries
- Language support
- Enhanced Java Debugging Support
- Commands for Examining Java Virtual Machine(JVM) internals
- Support for stack traces in Java, C, and C++ programs
- Support for 64-bit Java, C, aC++ stack unwinding
- Enhanced support for C++ templates
- Support for __fpreg data type on IPF
- Support for _Complex variables in HP C
- Support for debugging namespaces
- Command for evaluating the address of an expression
- Viewing Wide Character Strings
- Support for output logging
- Getting information from a non-debug executable
- Debugging optimized code
- Visual Interface for WDB
- Starting and stopping Visual Interface for WDB
- Navigating the Visual Interface for WDB display
- Specifying foreground and background colors
- Using the X-window graphical interface
- Using the TUI mode
- Changing the size of the source or debugger pane
- Using commands to browse through source files
- Loading source files
- Editing source files
- Editing the command line and command-line history
- Saving the contents of a debugging session to a file
- Support for ddd
- Support for XDB commands
- GNU GDB Logging Commands
- Support for command line calls in a stripped executable
- Displaying the current block scope information
- Linux support
- The HP-UX Terminal User Interface
- XDB to WDB Transition Guide
- By-function lists of XDB commands and HP WDB equivalents
- Overall breakpoint commands
- XDB data formats and HP WDB equivalents
- XDB location syntax and HP WDB equivalents
- XDB special language operators and HP WDB equivalents
- XDB special variables and HP WDB equivalents
- XDB variable identifiers and HP WDB equivalents
- Alphabetical lists of XDB commands and HP WDB equivalents
- Controlling GDB
- Canned Sequences of Commands
- Using GDB under gnu Emacs
- GDB Annotations
- The gdb/mi Interface
- Function and purpose
- Notation and terminology
- gdb/mi Command Syntax
- gdb/mi compatibility with CLI
- gdb/mi output records
- gdb/mi command description format
- gdb/mi breakpoint table commands
- gdb/mi Data manipulation
- gdb/mi program control
- Miscellaneous GDB commands in gdb/mi
- gdb/mi Stack Manipulation Commands
- gdb/mi Symbol query commands
- gdb/mi Target Manipulation Commands
- gdb/mi thread commands
- gdb/mi tracepoint commands
- gdb/mi variable objects
- Reporting Bugs in GDB
- Installing GDB
- Index

Chapter 7: Examining Source Files 57
7 Examining Source Files
GDB can print parts of the source code of your program, since the debugging information
recorded in the program tells GDB what source files were used to build it. When your
program stops, GDB spontaneously prints the line where it stopped. Likewise, when you
select a stack frame (see Section 6.5 [Selecting a frame], page 53), GDB prints the line where
execution in that frame has stopped. You can print other portions of source files by explicit
command.
You can invoke GDB from its gnu Emacs interface to view the source code see Chapter 19
[Using GDB under gnu Emacs], page 259.
7.1 Printing source lines
To print lines from a source file, use the list command (abbreviated l). By default, ten
lines are printed. There are several ways to specify what part of the file you want to print.
The following forms of the list command are used:
list linenum
Prints lines centered around line number linenum in the current source file.
list function
Prints lines centered around the beginning of function function.
list Prints more lines. The list command prints lines following the lines printed
by a previously executed list command. If the command prior to executing
a list just printed the stack frame, then the list command only prints the
lines around that line.
list - Prints lines just before the lines last printed.
By default, GDB prints ten source lines with any of these forms of the list command.
The number of lines printed by GDB can be set by the set listsize command. The
following two forms are supported:
set listsize count
Makes the list command display count source lines (unless the list argument
explicitly specifies some other number).
show listsize
Displays the number of lines that list prints.
Repeating a list command with
h
RET
i
discards the argument, so it is equivalent to
typing just list. This is more useful than listing the same lines again. An exception is
made for an argument of ‘-’; that argument is preserved in repetition so that each repetition
moves up in the source file.
In general, the list command expects you to supply zero, one or two linespecs.
Linespecs specify source lines. There are several ways of writing them, but the most
common way is to specify some source line.
The following arguments can be given to the list command: