┌──(root㉿Executor)-[/home/kali] └─# gdb GNU gdb (Debian 10.1-2+b1) 10.1.90.20210103-git Copyright (C) 2021 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty"for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration"for configuration details. For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>.
For help, type"help". Type "apropos word" to search for commands related to "word". (gdb)
┌──(root㉿Executor)-[/home/kali/mydir] └─# gdb --silent main.out Reading symbols from main.out... (No debugging symbols found in main.out) #报告没有调试信息 (gdb)
(gdb) file /home/kali/mydir/main.out Load new symbol table from "/home/kali/mydir/main.out"? (y or n) y Reading symbols from /home/kali/mydir/main.out...
(gdb) show language The current source language is "auto; currently c".
查看源文件信息info source
1 2 3 4 5 6 7 8 9
(gdb) info source Current source file is main.c Compilation directory is /home/kali/mydir Located in /home/kali/mydir/main.c Contains 10 lines. Source language is c. Producer is GNU C17 11.2.0 -mtune=generic -march=x86-64 -g -Og -fasynchronous-unwind-tables. Compiled with DWARF 2 debugging format. Does not include preprocessor macro info.
查看可以设置的程序语言set language
1 2
(gdb) set language Requires an argument. Valid arguments are auto, local, unknown, ada, asm, c, c++, d, fortran, go, minimal, modula-2, objective-c, opencl, pascal, rust.
查看程序运行状态info program
1 2
(gdb) info prog The program being debugged is not being run.
设置信息
设置命令行参数set args <参数1> <参数2>...
1 2 3
(gdb) set args 1 2 3 (gdb) show args Argument list to give program being debugged when it is started is "1 2 3".
如果在启动时有指定参数,此时再用set指定参数则会覆盖启动时设置的参数
设置语言'set language <语言>'
1
(gdb) set language c
运行
运行程序run
命令行参数使用启动时指定的参数或者set
args设置的参数,如果都没有给定则无参数执行
如果有断点则程序在第一个断点处停止,否则直接运行完.
带参数运行run <参数1> <参数2>...
此参数将会直接作为运行参数,覆盖前面设置的参数
main停止运行start
start相当于在main函数处下了断点然后run,自动在main开始前停下
运行时
断点
设置断点b <行号>
断点可以运行前设置也可以运行时设置
1 2
(gdb) b 6 Breakpoint 6 at 0x555555555142: file main.c, line 6.
如果以-tui分屏打开,则设置好的断点会显示在行号左侧,大写的B+>意味当前程序暂停的断点
image-20220421213142314
b <函数名>直接给函数下断点
1 2
(gdb) b main Breakpoint 10 at 0x555555555139: file main.c, line 5.
删除断点
delete <断点编号>
注意端点编号不是行号
删除全部断点则不指定编号,直接delete
删除指定行上的断点clear <行号>
条件断点b if <条件>
比如如果没有输入命令行参数时才给main函数下断点
1 2
(gdb) b main if argc==1 #用户没有输入时argc=1,第一个参数是当前程序位置 Breakpoint 11 at 0x555555555139: file main.c, line 5.
查看断点信息info b <断点号>
1 2 3 4 5
Breakpoint 11 at 0x555555555139: file main.c, line 5. (gdb) info b 11 Num Type Disp Enb Address What 11 breakpoint keep y 0x0000555555555139 in main at main.c:5 stop only if argc==1
info b查看所有断点信息
1 2 3 4 5 6
(gdb) info b Num Type Disp Enb Address What 11 breakpoint keep y 0x0000555555555139 in main at main.c:5 stop only if argc==1 12 breakpoint keep y 0x0000555555555142 in main at main.c:6 13 breakpoint keep y 0x0000555555555149 in main at main.c:7
查看信息
print命令
查看函数信息p <函数名>
函数信息也可以在运行前查看
1 2
(gdb) p main $6 = {int (int, char **)} 0x555555555139 <main>
1
{返回值类型(参数1类型,参数2类型)} 函数地址 <函数名>
1 2 3 4
(gdb) whatis main type = int (int, char **) (gdb) ptype main type = int (int, char **)
查看变量信息p <变量名>
查看变量信息必须是程序在该变量下文的断点处停下
即当前程序的运行位置必须已经经过变量,并且变量没有消亡
比如函数中的局部变量在函数返回之后就会消亡,只能在函数中断点然后查看断点之前的变量
如图调试一个用循环计算阶乘的函数,将断点下在第10行result*=n处
image-20220422175357420
当程序第一次执行到次时会停在result*=n==执行前==的状态
image-20220422175511240
如图第一次在第10行停下,打印result=1
查看寄存器信息p $<寄存器名>
对于刚才的fact循环求阶乘函数,最后返回值是result,可想而知,该值是存放在rax寄存器中的
1 2 3 4 5 6 7 8 9 10 11
intfact(int n){ if(n<0)return n; if(n==0)return1; int result=1; while(n>0){ result*=n; --n;