标签:10 教程 argc int 简明 gdb GDB main root
GDB调试简明教程
目录创建GDB调试程序
一般关闭编译优化 -o
显示所有warnings -Wall
gcc -g -Wall program.c -o program
g++ -g -Wall program1.c program2.c -o program
可以看到加入了调试的test更大,但事实上只是加入行号等信息,没有加入源代码。因此源代码要在同一目录下
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# gcc test.c -o test -g
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# gcc test.c -o test1
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# ll
total 68
drwxr-xr-x 2 root root 4096 Dec 14 10:07 ./
drwxr-xr-x 9 root root 4096 Dec 14 09:17 ../
-rw-r--r-- 1 root root 310 Dec 14 09:17 bubble.cpp
-rw-r--r-- 1 root root 691 Dec 14 09:17 main.cpp
-rw-r--r-- 1 root root 294 Dec 14 09:17 select.cpp
-rw-r--r-- 1 root root 117 Dec 14 09:17 sort.h
-rwxr-xr-x 1 root root 19736 Dec 14 10:06 test*
-rwxr-xr-x 1 root root 16808 Dec 14 10:07 test1*
-rw-r--r-- 1 root root 657 Dec 14 09:17 test.c
启动GDB调试
命令
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# gdb test
运行结果
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
(gdb)
退出GDB调试
quit/q
设置获取参数
(gdb) set args 10 20
(gdb) show args
结果
Argument list to give program being debugged when it is started is "10 20".
查看当前文件代码
从当前位置往下显示
(gdb) l
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
(gdb) l
11 a = 10;
12 b = 30;
13 } else {
14 a = atoi(argv[1]);
15 b = atoi(argv[2]);
16 }
17 printf("a = %d, b = %d\n", a, b);
18 printf("a + b = %d\n", a + b);
19
20 for(int i = 0; i < a; ++i) {
指定行
gdb) l 11
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
11 a = 10;
12 b = 30;
13 } else {
14 a = atoi(argv[1]);
15 b = atoi(argv[2]);
指定函数
(gdb) l main
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
查看其他文件代码
(gdb) list test.c:10
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
11 a = 10;
12 b = 30;
13 } else {
14 a = atoi(argv[1]);
(gdb) list test.c:main
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
设置显示行数
(gdb) show listsize
Number of source lines gdb will list by default is 10.
(gdb) set listsize 30
(gdb) show listsize
Number of source lines gdb will list by default is 30.
查看断点 设置断点
(gdb) break 10
Breakpoint 1 at 0x14c6: file main.cpp, line 11.
(gdb) break main
Breakpoint 2 at 0x1481: file main.cpp, line 6.
(gdb) break bubble.cpp:10
Breakpoint 3 at 0x1227: file bubble.cpp, line 10.
(gdb) break bubble.cpp:bubbleSort
Breakpoint 4 at 0x11e9: file bubble.cpp, line 6.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000000014c6 in main() at main.cpp:11
2 breakpoint keep y 0x0000000000001481 in main() at main.cpp:6
3 breakpoint keep y 0x0000000000001227 in bubbleSort(int*, int) at bubble.cpp:10
4 breakpoint keep y 0x00000000000011e9 in bubbleSort(int*, int) at bubble.cpp:6
断点意味着 执行到该行之前
删除断点
info/i break/b
设置无效/有效断点
dis/disable 断点编号
ena/enable 断点编号
条件断点
b/break 10 if i==5
运行GDB调试的程序
start 停在第一行
(gdb) start
Temporary breakpoint 1 at 0x1481: file main.cpp, line 6.
Starting program: /root/Linux/lession08/main
Temporary breakpoint 1, main () at main.cpp:6
6 int main() {
run 遇到断点为止
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/Linux/lession08/main
冒泡排序之后的数组: 12 22 27 55 67
===================================
Breakpoint 2, main () at main.cpp:21
21 int array1[] = {25, 47, 36, 80, 11};
向下执行
next/n 向下执行 不进入函数体
(gdb) n
22 len = sizeof(array1) / sizeof(int);
(gdb) n
24 selectSort(array1, len);
(gdb) n
27 cout << "选择排序之后的数组: ";
(gdb) n
28 for(int i = 0; i < len; i++) {
(gdb) n
29 cout << array1[i] << " ";
step/s 进入函数体 运行结束跳出函数体 finish
Breakpoint 4, main () at main.cpp:24
24 selectSort(array1, len);
(gdb) s
selectSort (array=0x555555555690 <__libc_csu_init>, len=32767) at select.cpp:6
6 void selectSort(int *array, int len) {
(gdb) s
8 for (int j = 0; j < len - 1; j++) {
(gdb) s
9 for (int i = j + 1; i < len; i++) {
(gdb) s
10 if (array[j] > array[i]) {
(gdb) s
9 for (int i = j + 1; i < len; i++) {
继续运行
遇到下一断点 continue/c
变量操作和自动变量操作
print/p
(gdb) n
8 int array[] = {12, 27, 55, 22, 67};
(gdb) n
9 int len = sizeof(array) / sizeof(int);
(gdb) n
11 bubbleSort(array, len);
(gdb) print array[0]
$1 = 12
display undisplay info/i display
(gdb) display len
2: len = 5
(gdb) s
10 if (array[j] > array[j + 1]) {
1: a = {i = {0, 1045149306}, d = 1.2904777690891933e-08}
2: len = 5
(gdb)
其它操作
循环时 设置值
set var 变量名 = 变量值
until 跳出循环
标签:10,教程,argc,int,简明,gdb,GDB,main,root 来源: https://www.cnblogs.com/most-silence/p/15692158.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。