Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux gdb Command

Linux gdb Command

Linux Command Reference Linux Command Reference


GDB (GNU Debugger) is the most commonly used program debugging tool on Linux systems. It helps developers:

GDB supports multiple programming languages, including C, C++, Objective-C, Fortran, Ada, and others, making it an indispensable debugging tool for Linux developers.


Installing gdb

On most Linux distributions, gdb can be easily installed via the package manager:

Example

Ubuntu/Debian

sudo

apt-get install

gdb

CentOS/RHEL

sudo

yum install

gdb

Fedora

sudo

dnf

install

gdb

Arch Linux

sudo

pacman

-S

gdb

After installation, you can verify it was successful with the following command:

gdb --version


Compiling a debuggable program

To debug a program with gdb, you need to add the -g option at compile time to generate debugging information:

gcc -g program.c -o program

-g The option embeds source code information in the executable, allowing gdb to map machine instructions to source code.


Basic gdb commands

Starting and exiting gdb

Example

Start gdb and load a program

gdb

.

/

program

Start gdb and attach to a running process

gdb

-p

PID

Exit gdb

(

gdb

)

quit

or the shorthand

(

gdb

)

q

Run the program

Example

Run the program

(

gdb

)

run

or the shorthand

(

gdb

)

r

Run with arguments

(

gdb

)

run arg1 arg2

Breakpoint management

Example

Set a breakpoint at a specified line

(

gdb

)

break

10

or the shorthand

(

gdb

)

b

10

Set a breakpoint at a function entry

(

gdb

)

break

main

(

gdb

)

break

function_name

List all breakpoints

(

gdb

)

info breakpoints

Delete a breakpoint

(

gdb

)

delete

1

 

Delete breakpoint number 1

(

gdb

)

delete    

Delete all breakpoints

Program execution control

Example

Continue execution until the next breakpoint

(

gdb

)

continue

or the shorthand

(

gdb

)

c

Step into (enter function)

(

gdb

)

step

or the shorthand

(

gdb

)

s

Step over (do not enter function)

(

gdb

)

next

or the shorthand

(

gdb

)

n

Finish the current function and return

(

gdb

)

finish

View code

Example

View code near the current line

(

gdb

)

list

or the shorthand

(

gdb

)

l

View code near a specified line

(

gdb

)

list

15

View the code of a specified function

(

gdb

)

list main

Inspect variables and memory

Example

Print a variable's value

(

gdb

)

print variable_name

or the shorthand

(

gdb

)

p variable_name

Modify a variable's value

(

gdb

)

print variable_name = new_value

Viewing the variable type

(

gdb

)

ptype variable_name

View memory contents

(

gdb

)

x

/

10xw

&

amp;variable  

View 10 words in hexadecimal

Call stack analysis

Example

View the call stack

(

gdb

)

backtrace

or the shorthand

(

gdb

)

bt

Switch to a specified stack frame

(

gdb

)

frame

2

or the shorthand

(

gdb

)

f

2


Advanced debugging techniques

Conditional breakpoint

Example

Trigger a breakpoint when i equals 5

(

gdb

)

break

10

if

i ==

5

Watchpoints

Example

Pause when a variable is modified

(

gdb

)

watch

variable_name

Pause when a variable is read

(

gdb

)

rwatch variable_name

Pause when a variable is read or modified

(

gdb

)

awatch variable_name

Multithreaded debugging

Example

List all threads

(

gdb

)

info threads

Switch to a specified thread

(

gdb

)

thread

2

Allow only the current thread to execute

(

gdb

)

set

scheduler-locking on

Debugging core dump files

Example

Load a core dump file

gdb

.

/

program core

View the call stack at the time of the crash

(

gdb

)

bt


gdb graphical interface

gdb also supports a graphical interface mode, which you can start with the -tui option:

gdb -tui ./program

or switch during use with:

Example

(

gdb

)

layout src  

Show the source code window

(

gdb

)

layout asm  

Show the assembly window

(

gdb

)

layout regs

Show the registers window

(

gdb

)

layout

split

Show source code and assembly together


Useful gdb configurations

You can add commonly used configurations to ~/.gdbinit file:

Example

Enable pretty printing

set

print pretty on

Set the history size

set

history

save on

set

history

size

1000

Custom commands

define printarray

   

set

$i

=

0

   

while

$i

&

lt;

$arg0

       

printf

&

quot;array

[

%

d

]

=

%

dn

&

quot;,

$i

,

$arg1

[

$i

]

       

set

$i

=

$i

1

    end

end


Common Problem Solutions

Cannot see source code when debugging

  1. Make sure the -g Option
  2. Check whether the source code is at the location used during compilation
  3. Use directory command to add the source code path:
(gdb) directory /path/to/source

Debugging optimized code

Optimization may change the execution order of code, making debugging difficult. It is recommended to disable optimization when debugging:

gcc -O0 -g program.c -o program


Summary

gdb is a powerful debugging tool on Linux, and mastering it can significantly improve debugging efficiency. This article introduces the basic usage and common commands of gdb, and readers are encouraged to practice with real debugging sessions to reinforce this knowledge. As you gain experience, you will find that gdb can help you solve a wide range of complex debugging problems.


Linux Command Reference Linux Command Reference