## 指定输出文件名t
lenovo@lenovo-virtual-machine:~$ gcc t.c -o t
lenovo@lenovo-virtual-machine:~$ ls
a.out itop4412 snap t t.c 公共的 模板 视频 图片 文档 下载 音乐 桌面
## 运行文件
lenovo@lenovo-virtual-machine:~$ ./t
helloworld
选项
功能
-c
只激活预处理、编译和汇编,生成扩展名.o的目标代码文件
-S
只激活预处理、编译,生成扩展名.s的汇编代码文件
-E
只激活预处理,并输出至标准输出
-g
为调试程序,生成相关信息
3、调试:gdb
3.1 启动gdb
## gcc 编译文件生成a.out文件,gdb调试
lenovo@lenovo-virtual-machine:~$ gcc -g t.c
lenovo@lenovo-virtual-machine:~$ gdb a.out
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 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-linux-gnu".Type"show configuration"for configuration details.For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.For help,type"help".Type"apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.(gdb)
3.2 获取帮助
## 进入gdb帮助文档(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data-- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type"help" followed by a class name for a list of commands in that class.Type"help all"for the list of all commands.Type"help" followed by command name for full documentation.Type"apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
## 查看breakpoint帮助(gdb) help breakpoint
Making program stop at certain points.
List of commands:
awatch --Set a watchpoint for an expression
break--Set breakpoint at specified location
break-range --Set a breakpoint for an address range
catch--Set catchpoints to catch events
catch assert --Catch failed Ada assertions
catchcatch--Catch an exception
catch exception --Catch Ada exceptions
catch exec --Catch calls to exec
catch fork --Catch calls to fork
catch handlers --Catch Ada exceptions
catch load --Catch loads of shared libraries
catch rethrow --Catch an exception
catch signal --Catch signals by their names and/or numbers
catch syscall --Catch system calls by their names
catchthrow--Catch an exception
catch unload --Catch unloads of shared libraries
catch vfork --Catch calls to vfork
clear -- Clear breakpoint at specified location
commands --Set commands to be executed when the given breakpoints are hit
---Type <return> to continue, or q <return> to quit---
3.3 查看源代码
## list查看程序源代码,第一次使用list显示出前10行,回车表示执行上一次命令,再显示10行(gdb) list
1 #include<stdio.h>
2
3 int summary( int n);
4
5 int main()
6 {
7 int i,result;
8 result = 0;
9 for(i=1;i<=100;i++)
10 {(gdb)
11 result +=i;
12 }
13 printf("Summary[1-100]=%d\n",result);
14 printf("Summary[1-450]=%d\n",summary(450));
15 return 0;
16
17 }
18
19 int summary (int n)
20 {
## 搜索源代码中int的行,显示第一个符合的,回车显示下一个(gdb) search int
21 int sum =0;
## search向文尾搜索,reverse-search向文头搜索(gdb) search int
21 int sum =0;(gdb)
22 int i;(gdb) reverse-search int
21 int sum =0;
3.4 设置断点
## 设置断点,第10行(gdb)break 10
Breakpoint 1 at 0x662: file t.c, line 10.
#3 查看break信息(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000000662 in main at t.c:10
3.5 执行和单步执行
## run表示执行操作(gdb) run
Starting program: /home/lenovo/a.out
Breakpoint 1, main () at t.c:11
11 result +=i;## 可以使用clear命令清楚当前行的断点
## next表示下一步(gdb) next
9 for(i=1;i<=100;i++)## n也表示下一步(gdb) n
Breakpoint 1, main () at t.c:11
11 result +=i;
##继续执行,直到遇到下一个断点,缩写c(gdb)continue
Continuing.
Breakpoint 1, main () at t.c:11
11 result +=i;
## 另一种单步执行step,step会遇到函数调用进入函数内部,而next只是调用这个函数(gdb) step
9 for(i=1;i<=100;i++)(gdb)
Breakpoint 1, main () at t.c:11
11 result +=i;(gdb)
9 for(i=1;i<=100;i++)
3.6 监视变量
## print监视参数,需要手动print查看(gdb) print i
$1 = 4
(gdb) n
Breakpoint 1, main () at t.c:11
11 result +=i;(gdb)
9 for(i=1;i<=100;i++)(gdb)
Breakpoint 1, main () at t.c:11
11 result +=i;(gdb) print i
$2 = 6
## 设置观察点,watch接受变量作参数,一旦参数发生变化,就停下(gdb) watch i
Hardware watchpoint 2: i
(gdb) c
Continuing.
Hardware watchpoint 2: i
Old value = 6
New value = 7
0x000055555555466c in main () at t.c:9
9 for(i=1;i<=100;i++)
3.7临时修改变量
(gdb) print i
$3 = 7
(gdb)setvar i=2
(gdb) print i
$4 = 2
3.8 查看堆栈
(gdb) bt
#0 0x000055555555466c in main () at t.c:9
3.9退出gdb
## 退出gdb,缩写q(gdb) quit
A debugging session is active.
Inferior 1 [process 3360] will be killed.
Quit anyway? (y or n) y