gdb 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

gdb 命令安装:


-bash/zsh: gdb command not found

#Debian
apt-get install gdb

#Ubuntu
apt-get install gdb

#Alpine
apk add gdb

#Arch Linux
pacman -S gdb

#Kali Linux
apt-get install gdb

#CentOS
yum install gdb

#Fedora
dnf install gdb

#OS X
brew install gdb

#Raspbian
apt-get install gdb

#Docker
docker run cmd.cat/gdb gdb

gdb 命令补充说明:


GNU 调试器 gdb 是一种宝贵的工具,可用于在开发程序时检查正在运行的进程并解决问题。设置断点是学习使用 GNU 调试器的第一步。程序在达到断点时停止,你可以运行 gdb 的命令对其进行检查或更改变量,然后再允许该程序继续运行。

gdb 命令语法:


gdb 
[-help] [-nx] [-q] [-batch] [-cd=dir] [-f] [-b bps] [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c core] [-x cmds] [-d dir] [prog[core|procID]]

gdb 命令选项:


-help
-h

List all options, with brief explanations.

-symbols=file
-s file
Read symbol table from file file.

-write
Enable writing into executable and core files.

-exec=file
-e file
Use file file as the executable file to execute when appropriate, and for examining pure data in conjunction with a core dump.

-se=file
Read symbol table from file file and use it as the executable file.

-core=file
-c file
Use file file as a core dump to examine.

-command=file

-x file
Execute GDB commands from file file.

-directory=directory
-d directory
Add directory to the path to search for source files.

-nx
-n

Do not execute commands from any '.gdbinit' initialization files. Normally, the commands in these files are executed after all the command options and arguments have been processed.

-quiet
-q

''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 '.gdbinit', if not inhibited). 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; in order to make this more useful, the message
Program exited normally.

(which is ordinarily issued whenever a program running under GDB control terminates) is not issued when running in batch mode.

-cd=directory
Run GDB using directory as its working directory, instead of the current directory.

-fullname
-f
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 the program stops). This recognizable format looks like two ' 32' 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 ' 32' characters as a signal to display the source code for the frame.

-b bps

Set the line speed (baud rate or bits per second) of any serial interface used by GDB for remote debugging.

-tty=device
Run using device for your program's standard input and output.

gdb 命令实例:


gdb 调试可执行文件:

gdb executable

将进程附加到 gdb:

gdb -p procID

启动时执行给定的 GDB 命令:

gdb -ex "commands" executable

启动 gdb 并传递参数:

gdb --args executable argument1 argument2

gdb 命令扩展阅读:




gdb 命令评论