调试利器GDB(上)

什么是GDB:

GDB应用:

静态分析工具与动态分析工具:

GDB启动方式:

GDB启动之后会有一个交互式的命令行,可以输入GDB特定的命令让GDB去工作。

gdb test.out意思是这一次gdb启动关注的是test.out这个进程。

gdb test.out core意思是程序崩溃时产生core文件。

动态连接:

gdb test.out pid意思是gdb去跟踪test.out这个程序文件对应的进程号为pid的进程。

应用示例一:

应用示例二:

实验:

test.c程序如下:

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 extern int* g_pointer;
 5 extern void func();
 6 
 7 void test_1()
 8 {
 9     printf("test_1() : %p\n", test_1);
10 }
11 
12 void test_2()
13 {
14     printf("test_2() : %p\n", test_2);
15 }
16 
17 void test_3()
18 {
19     printf("test_3() : %p\n", test_3);
20 }
21 
22 int main(int argc, char *argv[])
23 {
24     typedef void(TFunc)();
25     TFunc* fa[] = {test_1, test_2, test_3};
26     int i = 0;
27     
28     printf("main() : begin...\n");
29     
30     for(i=0; i<argc; i++)
31     {
32         printf("argv[%d] = %s\n", i, argv[i]);
33     }
34     
35     for(i=0; i<100; i++)
36     {
37         fa[i%3]();
38         sleep(argc > 1);
39     }
40 
41     printf("g_pointer = %p\n", g_pointer);
42 
43     func();
44     
45     printf("main() : end...\n");
46 
47     return 0;
48 }

func.c如下:

 1 #include <stdio.h>
 2 
 3 int* g_pointer;
 4 
 5 void func()
 6 {
 7     *g_pointer = (int)"D.T.Software";
 8 
 9     return;
10 }

猜你喜欢

转载自www.cnblogs.com/wanmeishenghuo/p/9821098.html