learning gcc&gdb
初学GCC&GDB记录一下基本的操作指令,以后遇到新的将不定时更新。
GCC 的基本用法
-Wall:
This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning),even in conjunction with macros. This also enables some language-specific warnings.
-o
指定输出的文件名,若无,则默认为a.out
-l -L
-l 用于链接库,gcc默认选项是链接libc.so。但是无法指定特定文件中的库文件仅仅在以下三个目录下进行检索
/lib;/usr/lib;/usr/local/lib 如果未找到则返回错误指令
-L 用于链接库,但是可以指定特定的目录进行检索
GDB的基本用法
基本命令1
命令 | 描述 |
backtrace(bt) | 查看各级函数调用及参数 |
finish | 运行至本函数结束,然后等待命令 |
frame(f) | 选择栈帧,有时与i local连用,选择栈帧后则会显示本栈的数据 |
info(i) locals | 查看当前栈帧的数据 |
list(l) | 列出源代码,每次列十行,更为详细用法见下方 |
list 行号 | 列出从某行开始的源代码 |
list 函数名 | 列出某函数的源代码 |
next(n) | 执行下一条语句 |
print(p) | 打印出某变量的值,通过表达式可以修改变量的值或者调用函数 (调用时会用$来记录中值,且$会自动增长,此时可以用$1,$2查看以前调用的值) |
quit(q) | 退出gdb调试 |
set var | 在运行时将某变量设置为某一值,观察程序是否错误 |
start | 开始执行程序 |
step(s) | 进入函数中进行执行 |
list 更为详细的用法
Here are the forms of the list
command most commonly used:
list linenum
- Print lines centered around line number linenum in the current source file.
list function
- Print lines centered around the beginning of function function.
list
- Print more lines. If the last lines printed were printed with a
list
command, this prints lines following the last lines printed; however, if the last line printed was a solitary line printed as part of displaying a stack frame (see section Examining the Stack), this prints lines centered around that line. list -
- Print lines just before the lines last printed.
By default, GDB prints ten source lines with any of these forms of the list
command. You can change this using set listsize
:
set listsize count
- Make the
list
command display count source lines (unless thelist
argument explicitly specifies some other number). show listsize
- Display the number of lines that
list
prints.
Repeating a list
command with RET discards the argument, so it is equivalent to typing just list
. This is more useful than listing the same lines again. An exception is made for an argument of `-'; that argument is preserved in repetition so that each repetition moves up in the source file.
In general, the list
command expects you to supply zero, one or two linespecs. Linespecs specify source lines; there are several ways of writing them but the effect is always to specify some source line. Here is a complete description of the possible arguments for list
:
list linespec
- Print lines centered around the line specified by linespec.
list first,last
- Print lines from first to last. Both arguments are linespecs.
list ,last
- Print lines ending with last.
list first,
- Print lines starting with first.
list +
- Print lines just after the lines last printed.
list -
- Print lines just before the lines last printed.
list
- As described in the preceding table.
Here are the ways of specifying a single source line--all the kinds of linespec.
number
- Specifies line number of the current source file. When a
list
command has two linespecs, this refers to the same source file as the first linespec. +offset
- Specifies the line offset lines after the last line printed. When used as the second linespec in a
list
command that has two, this specifies the line offset lines down from the first linespec. -offset
- Specifies the line offset lines before the last line printed.
filename:number
- Specifies line number in the source file filename.
function
- Specifies the line that begins the body of the function function. For example: in C, this is the line with the open brace.
filename:function
- Specifies the line of the open-brace that begins the body of the function function in the file filename. You only need the file name with a function name to avoid ambiguity when there are identically named functions in different source files.
*address
- Specifies the line containing the program address address. address may be any expression.
基本命令2(有关断点)
命令 | 描述 |
break(b) | 在某一行设置断点 |
break function | 在某一个函数设置断点 |
break..if | if后的条件判断为真时触摸断点 |
continue(c) | 执行到断点出 |
delete breakpoints 断点号 | 删除断点 |
display variable | 跟踪查看某一变量,每次停下都显示其值(相比p要方便的多) |
disable breakpoints 断点号 | 禁用断点(用于一些暂时不想启用但是又不想删除的断点) |
enable 断点号 | 启用断点 |
info(i) breakpoints | 查看当前设置了那些的断点 |
run(r) | 从头开始执行程序 |
undisplay 跟踪显示号 | 取消跟踪显示 |
基本命令3(有关观察点)
命令 | 描述 |
watch | 设置观察点(注意与display与p的区别,这是在改变观察点其值时才显示,有点类似与break..if) |
info(i) watchpoints | 查看当前设置了那些观察点 |
x | 从某个位置打印存储单元的内容,全部当作字节来看,而不区分哪个字节属于哪个变量 (for example:x/7bx input 代表从input地址开始打印7组每组以字节的形式显示,x意思是16进制) |