自带的help命令,用到什么命令就查看什么命令,不要没事学不用的命令,可以没事试着玩(养生)。
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands.
breakpoints -- Making program stop at certain points.
data -- Examining data.
files -- Specifying and examining files.
internals -- Maintenance commands.
obscure -- Obscure features.
running -- Running the program.
stack -- Examining the stack.
status -- Status inquiries.
support -- Support facilities.
tracepoints -- Tracing of program execution without stopping the program.
user-defined -- User-defined commands.
...
Type "apropos -v word" for full documentation of commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) help stack
Examining the stack.
The stack is made up of stack frames. Gdb assigns numbers to stack frames
counting from zero for the innermost (currently executing) frame.
...
Type "apropos -v word" for full documentation of commands related to "word".
Command name abbreviations are allowed if unambiguous.
man gdb
gdb a.out
layout:用于分割窗口,可以一边查看代码,一边测试。主要有以下几种用法:
layout src:显示源代码窗口
layout asm:显示汇编窗口
layout regs:显示源代码/汇编和寄存器窗口
layout split:显示源代码和汇编窗口
layout next:显示下一个layout
layout prev:显示上一个layout
Ctrl + L:刷新窗口
Ctrl + x,再按1:单窗口模式,显示一个窗口
Ctrl + x,再按2:双窗口模式,显示两个窗口
Ctrl + x,再按a:回到传统模式,即退出layout,回到执行layout之前的调试窗口。
查看变量
(gdb) p a
$5 = 3
(gdb) print a
$6 = 3 // 查看变量,int char 等类型
(gdb) print str // 打印字符串
$4 = 0x555555556004 "str"
(gdb) p (char*) 0x555555556004
$7 = 0x555555556004 "wangkekun"
// 设置打印字符串的长度--打印地址0x555555556004三个字符
(gdb) p *(char*)0x555555556004@3
$10 = "wan"
// 查看-修改寄存器操作 -- info 命令
info registers -- 查看寄存器
set var $rip=0x0000555555555184 -- 修改rip寄存器改变程序运行的流程
查看调用栈
(gdb) bt
#0 sum (a=1, b=2) at linux.c:6
#1 0x000055555555519c in main () at linux.c:17
反汇编
disassemble dumpCrash // 反汇编dumpCrash函数
disassemble main // 反汇编main函数
// 生成coredump
wang@ubuntu:~/Desktop/c$ ulimit -c // 查看coredump的大小
0
wang@ubuntu:~/Desktop/c$ ulimit -c unlimited
wang@ubuntu:~/Desktop/c$ ulimit -c
unlimited // 设置无线大,无论多大都会产生coredump
wang@ubuntu:~/Desktop/c$ ./linux
free(): invalid pointer
Aborted (core dumped) // 产生coredump
/proc/sys/kernel/core_pattern
sudo bash -c echo '/usr/core_log/core_%e_%t_%p > /proc/sys/kernel/core_pattern'
// 发现失败,因为没有/usr/core_log这个文件夹。还是以root权限登录比较好,要不然很多时候因为权限问题很浪费时间。
wang@ubuntu:~/Desktop/c$ gdb linux core-linux-14223-1666359088
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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_-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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"...
Reading symbols from linux...
[New LWP 14223]
Core was generated by `./linux'.
Program terminated with signal SIGABRT, Aborted.
--Type <RET> for more, q to quit, c to continue without paging--c
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x00007f632a309859 in __GI_abort () at abort.c:79
#2 0x00007f632a37426e in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7f632a49e298 "%s\n")
at ../sysdeps/posix/libc_fatal.c:155
#3 0x00007f632a37c2fc in malloc_printerr (str=str@entry=0x7f632a49c4c1 "free(): invalid pointer") at malloc.c:5347
#4 0x00007f632a37db2c in _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:4173
#5 0x000055a7dc1e816c in dumpCrash () at linux.c:7
#6 0x000055a7dc1e8181 in main () at linux.c:11
(gdb) q
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务