一般来说,GDB主要帮忙你完成下面四个方面的功能:
[ych@localhost ~]$ gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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-redhat-linux-gnu".
For bug reporting instructions, please see:
.
gdb在启动时会显示如上类似的提示信息。
如果不想显示这个信息,则可以使用-q选项把提示信息关掉:
[ych@localhost ~]$ gdb -q
(gdb) q
也可以在
~/.bashrc中,为gdb设置一个别名:
alias gdb="gdb -q"
使用gdb调试时,使用“info functions”命令可以列出可执行文件的所有函数名称。以上面代码为例:
(gdb) info functions tips*
All functions matching regular expression "tips*":
Non-debugging symbols:
0x00000000004005dd tips2_func1
0x00000000004005fc tips2_func2
0x0000000000400619 tips2_func3
0x0000000000400636 tips2_func4
这个命令也支持正则表达式:“info functions tips*”
[ych@localhost ~]$ gdb tips2
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/ych/tips2...(no debugging symbols found)...done.
(gdb)
(gdb)
[ych@localhost ~]$ gdb -p 3390
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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-redhat-linux-gnu".
For bug reporting instructions, please see:
.
Attaching to process 3390
以下命令是进入gdb调试界面后所使用的调试命令
(gdb) break 28
Breakpoint 1 at 0x40065d: file tips2.c, line 28.
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040065d in main at tips2.c:28
2 breakpoint keep y 0x000000000040065d in main at tips2.c:29
3 breakpoint keep y 0x000000000040065d in main at tips2.c:30
(gdb) run
Starting program: /home/ych/tips2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Breakpoint 1, main () at tips2.c:30
30 tips2_func1();
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
(gdb) run
Starting program: /home/ych/tips2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Breakpoint 1, main () at tips2.c:30
30 tips2_func1();
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
(gdb) n
tips2_func1(6)
31 pthread_create(&t1, NULL, tips2_func2, "Thread 1");
(gdb) n
[New Thread 0x7ffff77f1700 (LWP 14310)]
32 pthread_create(&t2, NULL, tips2_func3, "Thread 2");
(gdb) n
[New Thread 0x7ffff6ff0700 (LWP 14311)]
33 tips2_func4();
(gdb) n
tips2_func4(23)
36 sleep(1000);
Breakpoint 3, main () at tips2.c:32
32 pthread_create(&t2, NULL, tips2_func3, "Thread 2");
(gdb) n
[New Thread 0x7ffff6ff0700 (LWP 14383)]
Breakpoint 4, main () at tips2.c:33
33 tips2_func4();
(gdb) s
tips2_func4 () at tips2.c:23
23 printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
(gdb)
Breakpoint 2, main () at tips2.c:31
31 pthread_create(&t1, NULL, tips2_func2, "Thread 1");
Value returned is $1 = (void *) 0x10
(gdb) p t1
$2 = 0
(gdb) n
[New Thread 0x7ffff77f1700 (LWP 25004)]
Breakpoint 3, main () at tips2.c:32
32 pthread_create(&t2, NULL, tips2_func3, "Thread 2");
(gdb) p t1
$3 = 140737345689344
(gdb) p t2
$4 = 140737488348560
(gdb) c
Continuing.
[New Thread 0x7ffff6ff0700 (LWP 25086)]
Breakpoint 4, main () at tips2.c:33
33 tips2_func4();
Breakpoint 1, main () at tips2.c:30
30 tips2_func1();
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
(gdb) s
tips2_func1 () at tips2.c:6
6 printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
(gdb) finish
Run till exit from #0 tips2_func1 () at tips2.c:6
tips2_func1(6)
(gdb) list main
22 {
23 printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
24 }
25
26 int main(void)
27 {
28 pthread_t t1, t2;
29
30 tips2_func1();
31 pthread_create(&t1, NULL, tips2_func2, "Thread 1");
(gdb)
32 pthread_create(&t2, NULL, tips2_func3, "Thread 2");
33 tips2_func4();
34
35 while (1)
36 sleep(1000);
37 return;
38 }
通过 set listsize 命令可以设置设置一次显示源代码的行数
通过 show listsize 命令可以查看当前listsize的设置