0. gdb的gui用法
简单来说就是在以往的gdb开始的时候添加一个-tui选项.有的版本已经有gdbtui这个程序了
在linux自带的终端里是正常显示的,但是在securecrt里面,可能由于编码的问题,边缘会有些乱码,不过不影响使用(如果你的程序有错误输出,会扰乱整个界面,所以在调试的时候,建议添加2>/dev/null,这样的话基本可用)
启动gdb之后,上面是src窗口,下面是cmd窗口,默认focus在src窗口的,这样的话上下键以及pagedown,pageup都是在移动显示代码,并不显示上下的调试命令.这个时候要切换focus,具体可简单参见
(gdb) info win 查看当前focus SRC (36 lines) <has focus> CMD (18 lines) (gdb) fs next 切换focus Focus set to CMD window. (gdb) info win SRC (36 lines) CMD (18 lines) <has focus> (gdb) fs SRC 切换指定focus Focus set to SRC window. (gdb)
(Window names are case in-sensitive.)
1..调试复杂的宏定义
C语言中的宏定义,有着各种各样的好处和坏处,可谓让人有爱有恨。在大型的工程项目中,为了简洁,为了封装,宏的应用必不可少。但是在调试问题时,因为宏定义是被预定义处理的,所以不会有任何的编译符号和调试信息。这样给调试宏定义时,带来了很大的困难。对于开发人员来说,除了直接肉眼去看宏定义,自己来展开宏定义去确定问题,是否还有其它手段来调试宏定义吗?
- #include <stdlib.h>
- #include <stdio.h>
- #define MACRO1(x)(++(x))
- #define MACRO2(x)(MACRO1(x)+100)
- #define MACRO3(x)(MACRO2(x)+200)
- int main(void)
- {
- int a = 0;
- int b = 0;
- b = MACRO3(a);
- printf("%d\n", b);
- return 0;
- }
- /*
- 前面是1800+行的头文件代码,此处省略
- */
- int main(void)
- {
- int a = 0;
- int b = 0;
- b = (((++(a))+100)+200);
- printf("%d\n", b);
- return 0;
- }
- Breakpoint 1, main () at test.c:11
- 11 int a = 0;
- Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.i686
- (gdb) n
- 12 int b = 0;
- (gdb)
- 14 b = MACRO3(a);
- (gdb)
- 16 printf("%d\n", b);
- (gdb) macro expand MACRO3(a)
- expands to:(((++(a))+100)+200)
- (gdb) macro expand MACRO3(0)
- expands to:(((++(0))+100)+200)
- (gdb) macroexp MACRO3(0)
- expands to:(((++(0))+100)+200)
- (gdb)
- (gdb) macroexp MACRO3(3)
- expands to:(((++(3))+100)+200)
- (gdb)
2.多线程调试
先介绍一下GDB多线程调试的基本命令。
info threads 显示当前可调试的所有线程,每个线程会有一个GDB为其分配的ID,后面操作线程的时候会用到这个ID。 前面有*的是当前调试的线程。
thread ID 切换当前调试的线程为指定ID的线程。
break thread_test.c:123 thread all 在所有线程中相应的行上设置断点
thread apply ID1 ID2 command 让一个或者多个线程执行GDB命令command。
thread apply all command 让所有被调试线程执行GDB命令command。
set scheduler-locking off|on|step 估计是实际使用过多线程调试的人都可以发现,在使用step或者continue命令调试当前被调试线程的时候,其他线程也是同时执行的,怎么只让被调试程序执行呢?通过这个命令就可以实现这个需求。off 不锁定任何线程,也就是所有线程都执行,这是默认值。 on 只有当前被调试程序会执行。 step 在单步的时候,除了next过一个函数的情况(熟悉情况的人可能知道,这其实是一个设置断点然后continue的行为)以外,只有当前线程会执行。
gdb对于多线程程序的调试有如下的支持:
- 线程产生通知:在产生新的线程时, gdb会给出提示信息
(gdb) r
Starting program: /root/thread
[New Thread 1073951360 (LWP 12900)]
[New Thread 1082342592 (LWP 12907)]---以下三个为新产生的线程
[New Thread 1090731072 (LWP 12908)]
[New Thread 1099119552 (LWP 12909)]
- 查看线程:使用info threads可以查看运行的线程。
(gdb) info threads
4 Thread 1099119552 (LWP 12940) 0xffffe002 in ?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in ?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in ?? ()
* 1 Thread 1073951360 (LWP 12931) main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb)
注意,行首的蓝色文字为gdb分配的线程号,对线程进行切换时,使用该该号码,而不是上文标出的绿色数字。
另外,行首的红色星号标识了当前活动的线程
- 切换线程:使用 thread THREADNUMBER 进行切换,THREADNUMBER为上文提到的线程号。下例显示将活动线程从 1 切换至 4。
(gdb) info threads
4 Thread 1099119552 (LWP 12940) 0xffffe002 in ?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in ?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in ?? ()
* 1 Thread 1073951360 (LWP 12931) main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb) thread 4
[Switching to thread 4 (Thread 1099119552 (LWP 12940))]#0 0xffffe002 in ?? ()
(gdb) info threads
* 4 Thread 1099119552 (LWP 12940) 0xffffe002 in ?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in ?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in ?? ()
1 Thread 1073951360 (LWP 12931) main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb)
后面就是直接在你的线程函数里面设置断点,然后continue到那个断点,一般情况下多线程的时候,由于是同时运行的,最好设置 set scheduler-locking on
这样的话,只调试当前线程
- #include <stdlib.h>
- #include <stdio.h>
- static int func()
- {
- static int i = 0;
- ++i;
- if (i== 2){
- return 1;
- }
- return 0;
- }
- static int func3()
- {
- return func();
- }
- static int func2()
- {
- return func();
- }
- static int func1()
- {
- return func();
- }
- int main()
- {
- int ret = 0;
- ret += func1();
- ret += func2();
- ret += func3();
- return ret;
- }
- (gdb) b main
- Breakpoint 1 at 0x80483e0: file test.c, line 31.
- (gdb) r
- Starting program: /home/fgao/works/test/a.out
- Breakpoint 1, main () at test.c:31
- 31 int ret = 0;
- Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.i686
- (gdb) n
- 33 ret += func1();
- (gdb) checkpoint
- checkpoint: fork returned pid 2060.
- (gdb)
- Breakpoint 1, main() at test.c:31
- 31 int ret = 0;
- (gdb) n
- 33 ret += func1();
- (gdb) checkpoint
- checkpoint: fork returned pid 2060.
- (gdb) n
- 34 ret += func2();
- (gdb) p ret
- $4 = 0
- (gdb) n
- 35 ret += func3();
- (gdb) p ret
- $5 = 1
- (gdb) restart 1
- Switching to process 2060
- #0 main () at test.c:33
- 33 ret += func1();
- (gdb)
- (gdb) info checkpoints
- * 1 process 2060 at 0x80483e7, file test.c, line 33
- 0 process 2059 (main process) at 0x80483f7, file test.c, line 35
- #include <stdlib.h>
- #include <stdio.h>
- static void test(void)
- {
- pause();
- }
- int main(void)
- {
- test();
- return 0;
- }
- [xxx@xxx-vm-fc13 test]$ gcc-g test.c
- [xxx@xxx-vm-fc13 test]$./a.out
- [xxx@xxx-vm-fc13 test]$ ps auwx|grep a.out
- xxx 2412 0.0 0.0 1816 288 pts/4 S+ 11:19 0:00./a.out
- xxx 2415 0.0 0.0 4308 732 pts/5 S+ 11:20 0:00 grep a.out
- [xxx@xxx-vm-fc13 test]$ gdb at 2412
- GNU gdb (GDB) Fedora(7.1-18.fc13)
- Copyright (C) 2010 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3or later <http://gnu.org/licenses/gpl.html>
- This is free software: you are freeto 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 "i686-redhat-linux-gnu".
- For bug reporting instructions, please see:
- <http://www.gnu.org/software/gdb/bugs/>...
- Reading symbols from /usr/bin/at...(no debugging symbols found)...done.
- Attaching to program:/usr/bin/at, process 2412
- Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
- Loaded symbols for /lib/ld-linux.so.2
- 0x009f4424 in __kernel_vsyscall ()
- Missing separate debuginfos, use: debuginfo-install at-3.1.12-5.fc13.i686
- (gdb) bt
- #0 0x009f4424 in __kernel_vsyscall ()
- #1 0x0065dac6 in ?? ()
- #2 0x080483dc in ?? ()
- #3 0x005d6cc6 in ?? ()
- #4 0x08048331 in ?? ()
- (gdb)
- [xxx@xxx-vm-fc13 test]$ gdb
- GNU gdb (GDB) Fedora(7.1-18.fc13)
- Copyright (C) 2010 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3or later <http://gnu.org/licenses/gpl.html>
- This is free software: you are freeto 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 "i686-redhat-linux-gnu".
- For bug reporting instructions, please see:
- <http://www.gnu.org/software/gdb/bugs/>.
- (gdb) at 2412
- Attaching to process 2412
- Reading symbols from /home/fgao/works/test/a.out...done.
- Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
- Loaded symbols for /lib/libc.so.6
- Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
- Loaded symbols for /lib/ld-linux.so.2
- 0x009f4424 in __kernel_vsyscall ()
- Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.i686
- (gdb) bt
- #0 0x009f4424 in __kernel_vsyscall ()
- #1 0x0065dac6 in __pause_nocancel () from /lib/libc.so.6
- #2 0x080483cf in test () at test.c:6
- #3 0x080483dc in main () at test.c:12
- (gdb)
- (gdb) q
- A debugging session is active.
- Inferior 1 [process 2412] will be detached.
- Quit anyway? (y or n) y
- Detaching from program: /usr/bin/at, process 2412