黑马《linux基础编程》学习笔记(从56到60)

五十六. makefile练习

题目:

 解答:

五十七. 启动gdb和命令行传参

 接下来为了演示gdb方便,新建一个test.c如图

  1 #include <stdio.h>
  2
  3 int main(int argc, const char* argv[])
  4 {
  5     printf("args num = %d\n",argc);
  6     for(int i=0;i<argc;i++)
  7     {
  8         printf("arg%d:%s\n",i,argv[i]);
  9     }
 10     return 0;
 11 }

 接下来进行命令

[root@VM_0_15_centos TestGdb]# gcc test.c -g -o app -std=c99
[root@VM_0_15_centos TestGdb]# ls
app  test.c
[root@VM_0_15_centos TestGdb]# gdb app
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/TestGdb/app...done.


(gdb) set args ssss xxxx 4444 5555
(gdb) r
Starting program: /root/TestGdb/app ssss xxxx 4444 5555
args num = 5
arg0:/root/TestGdb/app
arg1:ssss
arg2:xxxx
arg3:4444
arg4:5555
[Inferior 1 (process 12152) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.el7.x86_64
(gdb)

五十八. gdb查看源代码

[root@VM_0_15_centos 3Day]# cd GDB/
[root@VM_0_15_centos GDB]# ls
args  insert_sort.c  main.c  makefile  select_sort.c  sort.h
[root@VM_0_15_centos GDB]# cat sort.h
#ifndef _SORT_H_
#define _SORT_H_

void insertionSort(int *array, int len);
void selectionSort(int *array, int len);

#endif // _SORT_H_
[root@VM_0_15_centos GDB]# vim main.c


[root@VM_0_15_centos GDB]# gcc *.c -g -o app -std=gnu99
[root@VM_0_15_centos GDB]# ls
app  args  insert_sort.c  main.c  makefile  select_sort.c  sort.h
[root@VM_0_15_centos GDB]# gdb app
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/3Day/GDB/app...done.
(gdb) l
1       #include <stdio.h>
2       #include "sort.h"
3
4       void main()
5       {
6               int i;
7               //▒▒▒▒▒▒▒▒▒▒▒▒
8               int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9               int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb)
11              //▒▒▒▒▒▒▒鳤▒▒
12              int len = sizeof(array) / sizeof(int);
13              //▒▒▒▒▒▒▒▒
14              printf("Sort Array:\n");
15              for (i = 0; i < len; ++i)
16              {
17                      printf("%d\t", array[i]);
18              }
19              printf("\n");
20
(gdb)
21              // selectionSort
22              selectionSort(array, len);
23              // printf
24              printf("Selection Sort:\n");
25              for (i = 0; i < len; ++i)
26              {
27                      printf("%d  ", array[i]);
28              }
29
30              // insertionSort
(gdb)
31              insertionSort(array2, len);
32              // printf
33              printf("\n==========Gorgeous Split Line==========\n");
34              printf("Insertion Sort: \n");
35              for (i = 0; i < len; ++i)
36              {
37                      printf("%d  ", array2[i]);
38              }
39              printf("\n");
40      }
(gdb)
Line number 41 out of range; main.c has 40 lines.
(gdb) show listsize
Number of source lines gdb will list by default is 10.
(gdb) set listsize 20
(gdb) l
Line number 41 out of range; main.c has 40 lines.
(gdb)
Line number 41 out of range; main.c has 40 lines.
(gdb) set listsize 10
(gdb) l 20
15              for (i = 0; i < len; ++i)
16              {
17                      printf("%d\t", array[i]);
18              }
19              printf("\n");
20
21              // selectionSort
22              selectionSort(array, len);
23              // printf
24              printf("Selection Sort:\n");
(gdb) l 5
1       #include <stdio.h>
2       #include "sort.h"
3
4       void main()
5       {
6               int i;
7               //▒▒▒▒▒▒▒▒▒▒▒▒
8               int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9               int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) l main
1       #include <stdio.h>
2       #include "sort.h"
3
4       void main()
5       {
6               int i;
7               //▒▒▒▒▒▒▒▒▒▒▒▒
8               int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9               int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb)
11              //▒▒▒▒▒▒▒鳤▒▒
12              int len = sizeof(array) / sizeof(int);
13              //▒▒▒▒▒▒▒▒
14              printf("Sort Array:\n");
15              for (i = 0; i < len; ++i)
16              {
17                      printf("%d\t", array[i]);
18              }
19              printf("\n");
20
(gdb) l insert_sort.c:15
10              ▒ȶ▒▒▒:▒▒▒▒▒▒▒▒▒▒▒ȶ▒▒▒
11
12      ***********************************************/
13
14      //▒▒▒▒▒▒▒▒▒㷨(▒▒▒▒▒▒▒▒)
15      void insertionSort(int *array, int len)
16      {
17              int tmp = 0;    // ▒洢▒▒׼▒▒
18              int index = 0;  // ▒ӵ▒λ▒▒
19              // ▒▒▒▒▒▒▒▒▒▒▒▒
(gdb) l insert_sort.c:insertSort
Function "insertSort" not defined in "insert_sort.c".
(gdb) l insert_sort.c:insertionSort
11
12      ***********************************************/
13
14      //▒▒▒▒▒▒▒▒▒㷨(▒▒▒▒▒▒▒▒)
15      void insertionSort(int *array, int len)
16      {
17              int tmp = 0;    // ▒洢▒▒׼▒▒
18              int index = 0;  // ▒ӵ▒λ▒▒
19              // ▒▒▒▒▒▒▒▒▒▒▒▒
20              for (int i = 1; i < len; ++i)
(gdb) l main.c:main
1       #include <stdio.h>
2       #include "sort.h"
3
4       void main()
5       {
6               int i;
7               //▒▒▒▒▒▒▒▒▒▒▒▒
8               int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9               int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10

 

五十九. gdb断点相关操作

[root@VM_0_15_centos GDB]# ls
app  args  insert_sort.c  main.c  makefile  select_sort.c  sort.h
[root@VM_0_15_centos GDB]# gdb app
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/3Day/GDB/app...done.
(gdb) l
1       #include <stdio.h>
2       #include "sort.h"
3
4       void main()
5       {
6               int i;
7               //▒▒▒▒▒▒▒▒▒▒▒▒
8               int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9               int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb)
11              //▒▒▒▒▒▒▒鳤▒▒
12              int len = sizeof(array) / sizeof(int);
13              //▒▒▒▒▒▒▒▒
14              printf("Sort Array:\n");
15              for (i = 0; i < len; ++i)
16              {
17                      printf("%d\t", array[i]);
18              }
19              printf("\n");
20
(gdb) b 12
Breakpoint 1 at 0x4008bc: file main.c, line 12.
(gdb) b 14
Breakpoint 2 at 0x4008c3: file main.c, line 14.
(gdb) b 15
Breakpoint 3 at 0x4008cd: file main.c, line 15.
(gdb) b 17
Breakpoint 4 at 0x4008d6: file main.c, line 17.
(gdb) b 19
Breakpoint 5 at 0x4008ff: file main.c, line 19.
(gdb) b24
Undefined command: "b24".  Try "help".
(gdb) b 24
Breakpoint 6 at 0x40091d: file main.c, line 24.
(gdb) b 27
Breakpoint 7 at 0x400930: file main.c, line 27.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004008bc in main at main.c:12
2       breakpoint     keep y   0x00000000004008c3 in main at main.c:14
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15
4       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
5       breakpoint     keep y   0x00000000004008ff in main at main.c:19
6       breakpoint     keep y   0x000000000040091d in main at main.c:24
7       breakpoint     keep y   0x0000000000400930 in main at main.c:27
(gdb) d 1
(gdb) i b
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x00000000004008c3 in main at main.c:14
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15
4       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
5       breakpoint     keep y   0x00000000004008ff in main at main.c:19
6       breakpoint     keep y   0x000000000040091d in main at main.c:24
7       breakpoint     keep y   0x0000000000400930 in main at main.c:27
(gdb) d 2 5
(gdb) i b
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15
4       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
6       breakpoint     keep y   0x000000000040091d in main at main.c:24
7       breakpoint     keep y   0x0000000000400930 in main at main.c:27
(gdb) d 4-7
No breakpoint number 5.
(gdb) i b
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15

接下来,是设置断点有效or无效

(gdb) l main
1       #include <stdio.h>
2       #include "sort.h"
3
4       void main()
5       {
6               int i;
7               //▒▒▒▒▒▒▒▒▒▒▒▒
8               int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9               int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) b 17
Breakpoint 8 at 0x4008d6: file main.c, line 17.
(gdb) i b
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15
8       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
(gdb) dis 8
(gdb) i b
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15
8       breakpoint     keep n   0x00000000004008d6 in main at main.c:17
(gdb) ena 8
(gdb) i b
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   0x00000000004008cd in main at main.c:15
8       breakpoint     keep y   0x00000000004008d6 in main at main.c:17

接下来是条件断点的设置

(gdb) b 17 if  i==10
Breakpoint 2 at 0x4008d6: file main.c, line 17.
(gdb) i b
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
        stop only if  i==10
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/3Day/GDB/app
Sort Array:

Breakpoint 2, main () at main.c:17
17                      printf("%d\t", array[i]);
(gdb) p i
$1 = 10
(gdb) d 2
(gdb) p i
$2 = 10
(gdb) i b
No breakpoints or watchpoints.
(gdb) b 17
Breakpoint 3 at 0x4008d6: file main.c, line 17.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/3Day/GDB/app
Sort Array:

Breakpoint 3, main () at main.c:17
17                      printf("%d\t", array[i]);
(gdb) p i
$3 = 0

 

 六十. 代码调试相关的命令

[root@VM_0_15_centos GDB]# gdb app
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/3Day/GDB/app...done.

//首先在17,22,27行各自打一个断点。
(gdb) b 17
Breakpoint 1 at 0x4008d6: file main.c, line 17.
(gdb)  b 22
Breakpoint 2 at 0x400909: file main.c, line 22.
(gdb) b 27
Breakpoint 3 at 0x400930: file main.c, line 27.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
2       breakpoint     keep y   0x0000000000400909 in main at main.c:22
3       breakpoint     keep y   0x0000000000400930 in main at main.c:27
(gdb) r
Starting program: /root/3Day/GDB/app
Sort Array:

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.el7.x86_64


//p表示print,可以出打印出各个变量的值
(gdb) p i
$1 = 0
(gdb) p array[i]
$2 = 12
(gdb) p len
$3 = 31

//ptype可以打印出各个变量的类型
(gdb) ptype i
type = int
(gdb) ptype array
type = int [31]

//n表示next
(gdb) n
15              for (i = 0; i < len; ++i)
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
(gdb)
15              for (i = 0; i < len; ++i)
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
(gdb)
15              for (i = 0; i < len; ++i)
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
(gdb)
15              for (i = 0; i < len; ++i)
(gdb) p i
$4 = 3

//加了display命令后,以后每次都会自动额外显示变量i的值
(gdb) display i
1: i = 3
(gdb)
(gdb)
(gdb) n

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
1: i = 4
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 4
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
1: i = 5
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 5
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
1: i = 6
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 6

//每次都会额外显示变量array[i]的值
(gdb) display array[i]
2: array[i] = 67
(gdb)
(gdb)
(gdb) n

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
2: array[i] = 89
1: i = 7
(gdb)
15              for (i = 0; i < len; ++i)
2: array[i] = 89
1: i = 7
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
2: array[i] = 87
1: i = 8
(gdb)
15              for (i = 0; i < len; ++i)
2: array[i] = 87
1: i = 8
(gdb) undisplay 2
(gdb)
(gdb) n

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
1: i = 9
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 9
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
1: i = 10
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 10
(gdb)

Breakpoint 1, main () at main.c:17
17                      printf("%d\t", array[i]);
1: i = 11
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 11
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004008d6 in main at main.c:17
        breakpoint already hit 12 times
2       breakpoint     keep y   0x0000000000400909 in main at main.c:22
3       breakpoint     keep y   0x0000000000400930 in main at main.c:27


//这里我们禁用掉第一个断点
(gdb) dis 1
(gdb) n
17                      printf("%d\t", array[i]);
1: i = 12
(gdb)
15              for (i = 0; i < len; ++i)
1: i = 12
(gdb)
17                      printf("%d\t", array[i]);
1: i = 13
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x00000000004008d6 in main at main.c:17
        breakpoint already hit 12 times
2       breakpoint     keep y   0x0000000000400909 in main at main.c:22
3       breakpoint     keep y   0x0000000000400930 in main at main.c:27
(gdb)
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x00000000004008d6 in main at main.c:17
        breakpoint already hit 12 times
2       breakpoint     keep y   0x0000000000400909 in main at main.c:22
3       breakpoint     keep y   0x0000000000400930 in main at main.c:27
(gdb) n
15              for (i = 0; i < len; ++i)
1: i = 13
(gdb)
17                      printf("%d\t", array[i]);
1: i = 14

//continue后,就直接跳到了22行的这个断点。
(gdb) c
Continuing.
12      5       33      6       10      35      67      89      87      65     54       24      58      92      100     24      46      78      99      200    55       44      33      22      11      71      2       4       86      8      9

Breakpoint 2, main () at main.c:22
22              selectionSort(array, len);
1: i = 31
(gdb) b 22
Breakpoint 1 at 0x400909: file main.c, line 22.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400909 in main at main.c:22
(gdb) r
Starting program: /root/3Day/GDB/app
Sort Array:
12      5       33      6       10      35      67      89      87      65     54       24      58      92      100     24      46      78      99      200    55       44      33      22      11      71      2       4       86      8      9

Breakpoint 1, main () at main.c:22
22              selectionSort(array, len);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.el7.x86_64
(gdb) step
selectionSort (array=0x7fffffffe410, len=31) at select_sort.c:17
17              int min = 0;    // ָ▒▒▒▒С▒▒Ԫ▒ص▒λ▒▒
(gdb) finish
Run till exit from #0  selectionSort (array=0x7fffffffe410, len=31)
    at select_sort.c:17
main () at main.c:24
24              printf("Selection Sort:\n");

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85011310