Debugging with gdb

Debugging with gdb

The gnu Source-Level Debugger

Tenth Edition, for gdb version 16.0.50.20240607-git

(GDB)

Richard Stallman, Roland Pesch, Stan Shebs, et al.

(Send bugs and comments on gdb to .) Debugging with gdb

TEXinfo 2017-08-23.19

Published by the Free Software Foundation 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA ISBN 978-0-9831592-3-0 Copyright c 1988-2024 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 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 FSF's Back-Cover Text is: "You are free to copy and modify this GNU Manual. Buying copies from GNU Press supports the FSF in developing GNU and promoting software freedom."

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 . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.1.4 Initialization Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.1.4.1 Home directory early initialization files . . . . . . . . . . . . . . . 18 2.1.4.2 System wide initialization files . . . . . . . . . . . . . . . . . . . . . . . 18 2.1.4.3 Home directory initialization file . . . . . . . . . . . . . . . . . . . . . 19 2.1.4.4 Local directory initialization file . . . . . . . . . . . . . . . . . . . . . . 19

2.2 Quitting gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.4 Logging Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3 gdb Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.1 Command Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 Command Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.3 Command Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.4 Filenames As Command Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.5 Command options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.6 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

4 Running Programs Under gdb . . . . . . . . . . . . . . . . 33

4.1 Compiling for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 4.2 Starting your Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.3 Your Program's Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.4 Your Program's Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.5 Your Program's Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.6 Your Program's Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.7 Debugging an Already-running Process . . . . . . . . . . . . . . . . . . . . . . . . 41 4.8 Killing the Child Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.9 Debugging Multiple Inferiors Connections and Programs . . . . . . . 42

4.9.1 Inferior-Specific Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 4.10 Debugging Programs with Multiple Threads . . . . . . . . . . . . . . . . . . 47

ii

Debugging with gdb

4.11 Debugging Forks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.12 Setting a Bookmark to Return to Later . . . . . . . . . . . . . . . . . . . . . . . 54

4.12.1 A Non-obvious Benefit of Using Checkpoints . . . . . . . . . . . . . 56

5 Stopping and Continuing . . . . . . . . . . . . . . . . . . . . . . 57

5.1 Breakpoints, Watchpoints, and Catchpoints . . . . . . . . . . . . . . . . . . . . 57 5.1.1 Setting Breakpoints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.1.2 Setting Watchpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 5.1.3 Setting Catchpoints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.1.4 Deleting Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5.1.5 Disabling Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1.6 Break Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.1.7 Breakpoint Command Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.1.8 Dynamic Printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.1.9 How to save breakpoints to a file . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.1.10 Static Probe Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.1.11 "Cannot insert breakpoints" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.1.12 "Breakpoint address adjusted..." . . . . . . . . . . . . . . . . . . . . . . . . . 83

5.2 Continuing and Stepping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 5.3 Skipping Over Functions and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.4 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.5 Stopping and Starting Multi-thread Programs. . . . . . . . . . . . . . . . . . 93

5.5.1 All-Stop Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5.5.2 Non-Stop Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5.5.3 Background Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.5.4 Thread-Specific Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 5.5.5 Interrupted System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.5.6 Observer Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

6 Running programs backward . . . . . . . . . . . . . . . . 101

7 Recording Inferior's Execution and Replaying It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

8 Examining the Stack . . . . . . . . . . . . . . . . . . . . . . . . . 111

8.1 Stack Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 8.2 Backtraces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 8.3 Selecting a Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 8.4 Information About a Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 8.5 Applying a Command to Several Frames. . . . . . . . . . . . . . . . . . . . . . 118 8.6 Management of Frame Filters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

iii

9 Examining Source Files . . . . . . . . . . . . . . . . . . . . . . 123

9.1 Printing Source Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 9.2 Location Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

9.2.1 Linespec Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 9.2.2 Explicit Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 9.2.3 Address Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 9.3 Editing Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 9.3.1 Choosing your Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 9.4 Searching Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 9.5 Specifying Source Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 9.6 Source and Machine Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 9.7 Disable Reading Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

10 Examining Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

10.1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 10.2 Ambiguous Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 10.3 Program Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 10.4 Artificial Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 10.5 Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 10.6 Examining Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 10.7 Memory Tagging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 10.8 Automatic Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 10.9 Print Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 10.10 Pretty Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

10.10.1 Pretty-Printer Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 10.10.2 Pretty-Printer Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 10.10.3 Pretty-Printer Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 10.11 Value History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 10.12 Convenience Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 10.13 Convenience Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 10.14 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 10.15 Floating Point Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 10.16 Vector Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 10.17 Operating System Auxiliary Information . . . . . . . . . . . . . . . . . . . 179 10.18 Memory Region Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 10.18.1 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

10.18.1.1 Memory Access Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 10.18.1.2 Memory Access Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 10.18.1.3 Data Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 10.18.2 Memory Access Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 10.19 Copy Between Memory and a File . . . . . . . . . . . . . . . . . . . . . . . . . . 183 10.20 How to Produce a Core File from Your Program . . . . . . . . . . . . 184 10.21 Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 10.22 Caching Data of Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 10.23 Search Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 10.24 Value Sizes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download