crash 7.3.2-4.el8 Copyright (C) 2002-2022 Red Hat, Inc. Copyright (C) 2004, 2005, 2006, 2010 IBM Corporation Copyright (C) 1999-2006 Hewlett-Packard Co Copyright (C) 2005, 2006, 2011, 2012 Fujitsu Limited Copyright (C) 2006, 2007 VA Linux Systems Japan K.K. Copyright (C) 2005, 2011, 2020-2022 NEC Corporation Copyright (C) 1999, 2002, 2007 Silicon Graphics, Inc. Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc. This program is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Enter "help copying" to see the conditions. This program has absolutely no warranty. Enter "help warranty"for details.
GNU gdb (GDB) 7.6 Copyright (C) 2013 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-unknown-linux-gnu"...
Breakpoint 2, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) s 19 data[i] = 0; // 将每个元素初始化为 0 (gdb) s
Breakpoint 2, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) s 19 data[i] = 0; // 将每个元素初始化为 0 (gdb) s
Breakpoint 2, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) s 19 data[i] = 0; // 将每个元素初始化为 0
n - next 执行下一步语句, 不进入函数, 每语句执行. ni - 汇编的下一个指令.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Breakpoint 13, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) n 19 data[i] = 0; // 将每个元素初始化为 0 (gdb) n
Breakpoint 13, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) n 19 data[i] = 0; // 将每个元素初始化为 0 (gdb) n
Breakpoint 13, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) {
p - print 打印变量的值. p *data
1 2 3 4 5
(gdb) p i $2 = 9 (gdb) p data[i] $3 = 0
watch - 查看变量的变化. watch i
1 2 3 4 5 6 7 8
(gdb) watch i Watchpoint 14: i (gdb) info b Num Type Disp Enb Address What 13 breakpoint keep y 0x000055555555517d in main at ./123.c:18 breakpoint already hit 4 times 14 watchpoint keep y i
Old value = 9 New value = 10 0x0000555555555180 in main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) c Continuing.
Breakpoint 13, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) c Continuing.
Watchpoint 14: i
Old value = 10 New value = 11 0x0000555555555180 in main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) {
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 2: i = 20 4: *data = 0 (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 2: i = 21 4: *data = 0 (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 2: i = 22 4: *data = 0
bt - backtrace 查看函数的堆栈.
1 2 3 4 5 6
(gdb) bt #0 main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 (gdb) s 19 data[i] = 0; // 将每个元素初始化为 0 1: i = 18
f - frame 查看栈帧 up - 查看上一个栈帧 down - 查看下一个帧栈
1 2 3 4 5 6 7 8
(gdb) f #0 main (argc=<optimized out>, argv=<optimized out>) at ./123.c:19 19 data[i] = 0; // 将每个元素初始化为 0 (gdb) up Initial frame selected; you cannot go up. (gdb) down Bottom (innermost) frame selected; you cannot go down.
layout - 查看布局. layout src 可以展示源代码以及断点在源代码文件中的位置. layout asm 显示源代码的汇编结构. layout regs 展示寄存器的结构. 退出 layout 的方法是: Ctrl + X , a.
x - 查看内存空间, 以及解析其中的内容.
1 2 3 4 5 6 7
(gdb) p data $36 = (int *) 0x5555555592a0
(gdb) x /4w 0x5555555592a0 0x5555555592a0: 0x000000000x000000000x000000000x00000000
whatis - 查看变量的类型.
1 2 3 4
(gdb) whatis data type = int *
search - 查找代码中的关键字.
1 2 3 4 5 6
(gdb) search for 18for (int i = 0; i < 100; ++i) { (gdb) search printf 24printf("%d\n", data[2]);
kill - 杀掉当前的进程.
1 2 3 4
(gdb) k Kill the program being debugged? (y or n) y [Inferior 1 (process 4121) killed]
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 1: i = 11
// 添加一个新checkpoint (gdb) ch checkpoint 1: fork returned pid 4596.
// 查看新添加的checkpoint (gdb) info ch * 0 Thread 0x7ffff7dad740 (LWP 4529) (main process) at 0x0 1 process 4596 at 0x55555555517d, file ./123.c, line 18
// 删除添加的checkpoint (gdb) del ch 1 Killed process 4596 (gdb) info ch No checkpoints.
// 继续运行. (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 1: i = 12
// 添加checkpoint (gdb) ch checkpoint 1: fork returned pid 4598. (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 1: i = 13 (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 1: i = 14 (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 1: i = 15
// 前进两步之后, 回退到之前的checkpoint. (gdb) restart 1 Switching to Thread 0x7ffff7dad740 (LWP 4598) #0 main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { (gdb) c Continuing.
Breakpoint 15, main (argc=<optimized out>, argv=<optimized out>) at ./123.c:18 18 for (int i = 0; i < 100; ++i) { 1: i = 13 // 可以看到 上面的 i 的数值恢复到了 13 , checkpoint 回滚了全部的代码, 或者说在 调试的过程中, 在特定的状态 fork 新的一个进程出来, 在需要的时候将状态切换到当前备份出来的新进程上.