gdb调试的基本使用

小说:适合宝妈做的兼职作者:石通辛杜更新时间:2018-11-17字数:64459

可以说无尽轮回不但是攻击现在,更是攻击人的过去和未来,人是如此,天地也是如此,一旦吃下这一招除非瞬间以莫大法力破开,否则只能迷失在无尽轮回当中彻底的消散,就算是圣人来了颠倒时间长河也无法复活。

创富网赚论坛

在她看来自己战败了的话不过是她自己实力不如人罢了,没什么好失落的,别人战败了不是死就是生不如死,她战败了却屁事都没用,而且她也不算是完全战败。
“只是半成品的大千之器根本抵挡不住两大大罗金仙的先天灵宝的。”红衣摇了摇头:“不过刘皓可不是纯粹的内宇宙圣人,他还是一个修道者,还有各种神通。”

这并不是开玩笑的,像刘皓这样随便一滴血,只要那个人能承受得住,那么立刻就能脱胎换骨变得极为厉害,只可惜刘皓体内的不灭之血极为霸道,如果没有刘皓帮忙给化解,就算刘皓给别人一滴血最后也只会爆体而亡。

gdb调试的基本使用


GDB调试
启动程序准备调试
GDB yourpram
或者
先输入GDB
然后输入 file yourpram

然后使用run或者r命令开始程序的执行,也可以使用 run parameter将参数传递给该程序

参数列表

命令

命令缩写

命令说明

list

l

显示多行源代码

break

b

设置断点,程序运行到断点的位置会停下来

info

i

描述程序的状态

run

r

开始运行程序

display

disp

跟踪查看某个变量,每次停下来都显示它的值

step

s

执行下一条语句,如果该语句为函数调用,则进入函数执行其中的第一条语句

next

n

执行下一条语句,如果该语句为函数调用,不会进入函数内部执行(即不会一步步地调试函数内部语句)

print

p

打印内部变量值

continue

c

继续程序的运行,直到遇到下一个断点

set var name=v

 

设置变量的值

start

st

开始执行程序,main函数的第一条语句前面停下来

file

 

装入需要调试的程序

kill

k

终止正在调试的程序

watch

 

监视变量值的变化

backtrace

bt

产看函数调用信息(堆栈)

frame

f

查看栈帧

quit

q

退出GDB环境

 

  

//e.c
 #include <stdio.h>
void debug(char *str)
{
    printf("debug info :%s
",str );
}
main(int argc,char *argv[]){
    int i,j;
    j=0;
    for(i=0;i<10;i++){
        j+=5;
        printf("now a=%d
", j);
    }
}

gcc -g -o e e.c
调试gdb e
或者输入gdb
然后 file e

list 命令用法

list命令显示多行源代码,从上次的位置开始显示,默认情况下,一次显示10行,第一次使用时,从代码其实位置显示

gdb) list
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    main(int argc,char *argv[]){
7        int i,j;
8        j=0;
9        for(i=0;i<10;i++){
10            j+=5;
(gdb) 

list n显示已第n行未中心的10行代码

(gdb) list 8
3    {
4        printf("debug info :%s
",str );
5    }
6    main(int argc,char *argv[]){
7        int i,j;
8        j=0;
9        for(i=0;i<10;i++){
10            j+=5;
11            printf("now a=%d
", j);
12        }
(gdb) 

list functionname显示以functionname的函数为中心的10行代码

(gdb) list main
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    main(int argc,char *argv[]){
7        int i,j;
8        j=0;
9        for(i=0;i<10;i++){
10            j+=5;
(gdb) 

list - 显示刚才打印过的源代码之前的代码

(gdb) list 10
5    }
6    main(int argc,char *argv[]){
7        int i,j;
8        j=0;
9        for(i=0;i<10;i++){
10            j+=5;
11            printf("now a=%d
", j);
12        }
13    }(gdb) list -
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
(gdb) 

断点命令break
break location:在location位置设置断点,改位置可以为某一行,某函数名或者其它结构的地址
GDB会在执行该位置的代码之前停下来

gdb) list
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    main(int argc,char *argv[]){
7        int i,j;
8        j=0;
9        for(i=0;i<10;i++){
10            j+=5;
(gdb) 
11            printf("now a=%d
", j);
12        }
13    }(gdb) break 10
Breakpoint 1 at 0x40050a: file e.c, line 10.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e 

Breakpoint 1, main (argc=1, argv=0x7fffffffe548) at e.c:10
10            j+=5;
(gdb) c
Continuing.
now a=5

Breakpoint 1, main (argc=1, argv=0x7fffffffe548) at e.c:10
10            j+=5;
(gdb) c
Continuing.
now a=10

Breakpoint 1, main (argc=1, argv=0x7fffffffe548) at e.c:10
10            j+=5;
(gdb) 
View Code

使用delete breakpoints 断点号 删除断点
这里的断点号表示的是第几个断点,刚才执行break 10返回 reakpoint 1 at 0x40050a: file e.c, line 10.
中的1表示该断点的标号,因此使用 delete breakpoints 1表示删除第10行所定义的断点
clear n表示清除第n行的断点,因此clear 10等同于delete breakpoints 1
disable/enable n表示使得编号为n的断点暂时失效或有效
可使用info查看断点相关的信息
info breakpoints

gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) break 10
Breakpoint 2 at 0x40050a: file e.c, line 10.
(gdb) break 9
Breakpoint 3 at 0x400501: file e.c, line 9.
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x000000000040050a in main at e.c:10
3       breakpoint     keep y   0x0000000000400501 in main at e.c:9

display命令
查看参数的值

(gdb) break 10
Breakpoint 1 at 0x40050a: file e.c, line 10.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e 

Breakpoint 1, main (argc=1, argv=0x7fffffffe548) at e.c:10
10            j+=5;
(gdb) display j
1: j = 0
(gdb) c
Continuing.
now a=5

Breakpoint 1, main (argc=1, argv=0x7fffffffe548) at e.c:10
10            j+=5;
1: j = 5
(gdb) display
1: j = 5
(gdb) display i
2: i = 1
(gdb) display j
3: j = 5
(gdb) display j*2
4: j*2 = 10
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
4:   y  j*2
3:   y  j
2:   y  i
1:   y  j
View Code

也可以使用disable,enable,delete,info命令修改及查看其状态,用法与对断点的一样

step及next命令
step可使得程序逐条执行,即执行完一条语句然后在吓一跳语句前停下来,等待用户的命令
一般使用step命令是,可使用display或者watch命令查看变量的变化,从而判断程序行为是否符合要求
当下一条指令为函数时,s进入函数内部,在其第一条语句前停下来
step n,next n 表示连续但不执行n条指令,如果期间遇到断点,则停下来

(gdb) list
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    
7    main(int argc,char *argv[]){
8        int i,j;
9        j=0;
10        for(i=0;i<10;i++){
(gdb) 
11            j+=5;
12            printf("now j=%d
", j);
13            debug("x=======x");
14        }
15    }(gdb) 
Line number 16 out of range; e.c has 15 lines.
(gdb) break 11
Breakpoint 1 at 0x40050a: file e.c, line 11.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e1 

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:11
11            j+=5;
(gdb) s
12            printf("now j=%d
", j);
(gdb) s
__printf (format=0x400648 "now j=%d
") at printf.c:30
30    {
(gdb) bt
#0  __printf (format=0x400648 "now j=%d
") at printf.c:30
#1  0x0000000000400525 in main (argc=1, argv=0x7fffffffe538) at e.c:12
(gdb) n
34      va_start (arg, format);
(gdb) n
35      done = vfprintf (stdout, format, arg);
(gdb) n
now j=5
39    }
(gdb) bt
#0  __printf (format=<value optimized out>) at printf.c:39
#1  0x0000000000400525 in main (argc=1, argv=0x7fffffffe538) at e.c:12
(gdb) n
main (argc=1, argv=0x7fffffffe538) at e.c:13
13            debug("x=======x");
(gdb) n
debug info :x=======x
10        for(i=0;i<10;i++){
(gdb) s

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:11
11            j+=5;
(gdb) s
12            printf("now j=%d
", j);
(gdb) n
now j=10
13            debug("x=======x");
(gdb) n
debug info :x=======x
10        for(i=0;i<10;i++){
(gdb) 
View Code

watch
watch可设置观察点(watchpoint)。使用观察点可以使得当某表达式的值发生变化时,程序暂停执行。
执行该命令前,必须保证程序已经运行

(gdb) list 
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    
7    main(int argc,char *argv[]){
8        int i,j;
9        j=0;
10        for(i=0;i<10;i++){
(gdb) 
11            j+=5;
12            printf("now j=%d
", j);
13            debug("x=======x");
14        }
15    }(gdb) 
Line number 16 out of range; e.c has 15 lines.
(gdb) b main
Breakpoint 1 at 0x4004fa: file e.c, line 9.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e1 

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:9
9        j=0;
(gdb) watch j
Hardware watchpoint 2: j
(gdb) c
Continuing.
Hardware watchpoint 2: j

Old value = 0
New value = 5
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=5
debug info :x=======x
Hardware watchpoint 2: j

Old value = 5
New value = 10
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
View Code

print命令

(gdb) list
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    
7    main(int argc,char *argv[]){
8        int i,j;
9        j=0;
10        for(i=0;i<10;i++){
(gdb) 
11            j+=5;
12            printf("now j=%d
", j);
13            debug("x=======x");
14        }
15    }(gdb) 
Line number 16 out of range; e.c has 15 lines.
(gdb) break 12
Breakpoint 1 at 0x40050e: file e.c, line 12.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e1 

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) p j
$1 = 5
(gdb) c
Continuing.
now j=5
debug info :x=======x

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) p i,j
$2 = 10
(gdb) p j
$3 = 10
(gdb) 
View Code

set var name=value
在程序运行中动态改变变量的值

(gdb) list
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    
7    main(int argc,char *argv[]){
8        int i,j;
9        j=0;
10        for(i=0;i<10;i++){
(gdb) 
11            j+=5;
12            printf("now j=%d
", j);
13            debug("x=======x");
14        }
15    }(gdb) 
Line number 16 out of range; e.c has 15 lines.
(gdb) break main
Breakpoint 1 at 0x4004fa: file e.c, line 9.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e1 

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:9
9        j=0;
(gdb) watch i
Hardware watchpoint 2: i
(gdb) watch j
Hardware watchpoint 3: j
(gdb) c
Continuing.
Hardware watchpoint 3: j

Old value = 0
New value = 5
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=5
debug info :x=======x
Hardware watchpoint 2: i

Old value = 0
New value = 1
0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at e.c:10
10        for(i=0;i<10;i++){
(gdb) c
Continuing.
Hardware watchpoint 3: j

Old value = 5
New value = 10
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=10
debug info :x=======x
Hardware watchpoint 2: i

Old value = 1
New value = 2
0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at e.c:10
10        for(i=0;i<10;i++){
(gdb) c
Continuing.
Hardware watchpoint 3: j

Old value = 10
New value = 15
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=15
debug info :x=======x
Hardware watchpoint 2: i

Old value = 2
New value = 3
0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at e.c:10
10        for(i=0;i<10;i++){
(gdb) c
Continuing.
Hardware watchpoint 3: j

Old value = 15
New value = 20
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=20
debug info :x=======x
Hardware watchpoint 2: i

Old value = 3
New value = 4
0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at e.c:10
10        for(i=0;i<10;i++){
(gdb) set var i=8
(gdb) c
Continuing.
Hardware watchpoint 3: j

Old value = 20
New value = 25
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=25
debug info :x=======x
Hardware watchpoint 2: i

Old value = 8
New value = 9
0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at e.c:10
10        for(i=0;i<10;i++){
(gdb) c
Continuing.
Hardware watchpoint 3: j

Old value = 25
New value = 30
main (argc=1, argv=0x7fffffffe538) at e.c:12
12            printf("now j=%d
", j);
(gdb) c
Continuing.
now j=30
debug info :x=======x
Hardware watchpoint 2: i

Old value = 9
New value = 10
0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at e.c:10
10        for(i=0;i<10;i++){
(gdb) c
Continuing.

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.

Watchpoint 3 deleted because the program has left the block in
which its expression is valid.
__libc_start_main (main=0x4004eb <main>, argc=1, ubp_av=0x7fffffffe538, init=<value optimized out>, fini=<value optimized out>, rtld_fini=<value optimized out>, 
    stack_end=0x7fffffffe528) at libc-start.c:258
258      exit (result);
(gdb) c
Continuing.

Program exited with code 026.
View Code

函数调用相关的
backtrace
可使用frame 查看堆栈中某一帧的信息

(gdb) list
1    #include <stdio.h>
2    void debug(char *str)
3    {
4        printf("debug info :%s
",str );
5    }
6    
7    main(int argc,char *argv[]){
8        int i,j;
9        j=0;
10        for(i=0;i<10;i++){
(gdb) 
11            j+=5;
12            printf("now j=%d
", j);
13            debug("x=======x");
14        }
15    }(gdb) 
Line number 16 out of range; e.c has 15 lines.
(gdb) b 13
Breakpoint 1 at 0x400525: file e.c, line 13.
(gdb) r
Starting program: /mnt/hgfs/www/c/gcc/e1 
now j=5

Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at e.c:13
13            debug("x=======x");
(gdb) s
debug (str=0x400652 "x=======x") at e.c:4
4        printf("debug info :%s
",str );
(gdb) bt
#0  debug (str=0x400652 "x=======x") at e.c:4
#1  0x000000000040052f in main (argc=1, argv=0x7fffffffe538) at e.c:13
(gdb) s
__printf (format=0x400638 "debug info :%s
") at printf.c:30
30    {
(gdb) bt
#0  __printf (format=0x400638 "debug info :%s
") at printf.c:30
#1  0x00000000004004e9 in debug (str=0x400652 "x=======x") at e.c:4
#2  0x000000000040052f in main (argc=1, argv=0x7fffffffe538) at e.c:13
(gdb) s
34      va_start (arg, format);
(gdb) bt
#0  __printf (format=0x400638 "debug info :%s
") at prin

当前文章:http://0477auto.com/k0cut/91810.html

发布时间:2018-11-17 02:04:17

犯法的事来钱快的路子 投资有风险理财需谨慎 招聘兼职学生模板 适合周末的兼职 全部上海招聘兼职日结 2018网赚团队 网赚论坛视频 类似赚客吧的网站

编辑:北杜公王

我要说两句: (0人参与)

发布