Quantcast

袁伟时:中国的奴性和戾气从哪里来?

“芯片大学”虚晃一枪,人才断层问题不能跑步解决

两大中国首富双双被重挫-释放信号强烈

民间帝王赖小民和性感女星舒淇与许晴

女性高潮时为什么会“喷水”?

Facebook Twitter

分享到微信朋友圈

点击图标下载本文截图到手机
即可分享到朋友圈。如何使用?

自由微信安卓APP发布,立即下载!
查看原文

GDB 命令使用总结

2017-03-23 程序源 程序源

GDB 静态跟踪和学习的时候非常有用,但是命令比较多,这里按照自己的分类总结输出

按照 Brendan 的做法,有示例很重要

未完待续 …

开始调试

gdb test

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

gcc -g -O0 -o ctest1 test.c

gdb test

root@trusty64-135:/go/go.lesson/src/function-call-stack# gdb ctest1

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1

Copyright (C) 2014 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 ctest1...done.

(gdb) r

Starting program: /go/go.lesson/src/function-call-stack/ctest1

Breakpoint 1, main (argc=1, argv=0x7fffffffe628) at test.c:19

19 int x = 0x100;

查看源代码

1

2

3

4

5

6

7

8

9

10

11

(gdb) l main

13

14 return z;

15 }

16

17 int main(int argc, char **argv)

18 {

19 int x = 0x100;

20 int y = 0x200;

21 int z = add(x, y);

22

断点操作类

符号名设置断点 b main

1

2

(gdb) b main

Breakpoint 1 at 0x400585: file test.c, line 19.

查看所有断点

1

2

3

4

5

6

info b

Num     Type           Disp Enb Address            What

1       breakpoint     keep y   0x0000000000400585 in main at test.c:19

2       breakpoint     keep y   0x000000000040055c in add at test.c:11

3       breakpoint     keep y   0x0000000000400538 in info at test.c:6

根据编号删除断点

1

(gdb) d 1

删除所有断点

1

2

3

4

(gdb) d

Delete all breakpoints? (y or n) y

(gdb) info b

No breakpoints or watchpoints.

查看寄存器类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

(gdb) info r

rax            0x300 768

rbx            0x0 0

rcx            0x8 8

rdx            0x7ffff7dd59e0 140737351866848

rsi            0x7ffffff7 2147483639

rdi            0x1 1

rbp            0x7fffffffe540 0x7fffffffe540

rsp            0x7fffffffe520 0x7fffffffe520

r8             0x7ffff7b8b9c0 140737349466560

r9             0x0 0

r10            0x7ffff7dd26a0 140737351853728

r11            0x246 582

r12            0x400440 4195392

r13            0x7fffffffe620 140737488348704

r14            0x0 0

r15            0x0 0

rip            0x4005a2 0x4005a2 <main+44> // rip 也恢复到 main 函数值

eflags         0x202 [ IF ]

cs             0x33 51

ss             0x2b 43

ds             0x0 0

es             0x0 0

fs             0x0 0

gs             0x0 0

查看值类

查看函数的反汇编代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

(gdb) disass

Dump of assembler code for function add:

0x000000000040054e <+0>: push   rbp

0x000000000040054f <+1>: mov    rbp,rsp

0x0000000000400552 <+4>: sub    rsp,0x20

0x0000000000400556 <+8>: mov    DWORD PTR [rbp-0x14],edi

0x0000000000400559 <+11>: mov    DWORD PTR [rbp-0x18],esi

0x000000000040055c <+14>: mov    eax,DWORD PTR [rbp-0x18]

0x000000000040055f <+17>: mov    edx,DWORD PTR [rbp-0x14]

0x0000000000400562 <+20>: add    eax,edx

0x0000000000400564 <+22>: mov    DWORD PTR [rbp-0x4],eax

0x0000000000400567 <+25>: mov    eax,DWORD PTR [rbp-0x4]

0x000000000040056a <+28>: mov    edi,eax

0x000000000040056c <+30>: call   0x40052d <info>

0x0000000000400571 <+35>: mov    eax,DWORD PTR [rbp-0x4]

=> 0x0000000000400574 <+38>: leave

0x0000000000400575 <+39>: ret

End of assembler dump.

列出所有的局部变量

1

2

3

4

(gdb) info locals

x = 256

y = 512

z = 0

列出所有参数

1

2

3

(gdb) info args

argc = 1

argv = 0x7fffffffe5f8

运行期进程的内存映射

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

(gdb) info proc mappings

process 1545

Mapped address spaces:

Start Addr           End Addr       Size     Offset objfile

0x400000           0x401000     0x1000        0x0 /root/macshare/example/src/08/test

0x600000           0x601000     0x1000        0x0 /root/macshare/example/src/08/test

0x601000           0x602000     0x1000     0x1000 /root/macshare/example/src/08/test

0x7ffff7a15000     0x7ffff7bcf000   0x1ba000        0x0 /lib/x86_64-linux-gnu/libc-2.19.so

0x7ffff7bcf000     0x7ffff7dcf000   0x200000   0x1ba000 /lib/x86_64-linux-gnu/libc-2.19.so

0x7ffff7dcf000     0x7ffff7dd3000     0x4000   0x1ba000 /lib/x86_64-linux-gnu/libc-2.19.so

0x7ffff7dd3000     0x7ffff7dd5000     0x2000   0x1be000 /lib/x86_64-linux-gnu/libc-2.19.so

0x7ffff7dd5000     0x7ffff7dda000     0x5000        0x0

0x7ffff7dda000     0x7ffff7dfd000    0x23000        0x0 /lib/x86_64-linux-gnu/ld-2.19.so

0x7ffff7fed000     0x7ffff7ff0000     0x3000        0x0

0x7ffff7ff6000     0x7ffff7ff8000     0x2000        0x0

0x7ffff7ff8000     0x7ffff7ffa000     0x2000        0x0 [vvar]

0x7ffff7ffa000     0x7ffff7ffc000     0x2000        0x0 [vdso]

0x7ffff7ffc000     0x7ffff7ffd000     0x1000    0x22000 /lib/x86_64-linux-gnu/ld-2.19.so

0x7ffff7ffd000     0x7ffff7ffe000     0x1000    0x23000 /lib/x86_64-linux-gnu/ld-2.19.so

0x7ffff7ffe000     0x7ffff7fff000     0x1000        0x0

0x7ffffffde000     0x7ffffffff000    0x21000        0x0 [stack]

0xffffffffff600000 0xffffffffff601000     0x1000        0x0 [vsyscall]

查看地址

1

2

(gdb) p/x $rbp 查看一个特定的寄存器

$2 = 0x7fffffffe510

查看地址的值

1

2

3

4

5

(gdb) x/1xg $rsp // 查看地址的值是否符合预期

0x7fffffffe510: 0x00007fffffffe540

(gdb) x/1xg $rsp+0x18 // 根据 寄存器+偏移量寻址

0xc820037f18: 0x0000000000000100

查看调用栈帧

1

2

3

(gdb) bt

#0  0x0000000000401149 in main.add (x=256, y=512, ~r2=0) at /go/go.lesson/src/function-call-stack/test.go:10

#1  0x00000000004011bd in main.main () at /go/go.lesson/src/function-call-stack/test.go:18

查看符号地址

1

2

(gdb) info address main.main

Symbol "main.main" is a function at address 0x401180.

根据符号地址反向查看符号详情

1

2

(gdb) info symbol 0x401180

main.main in section .text of /go/go.lesson/src/function-call-stack/gtest1

单步机器指令

1

2

3

si 单步进入

ni 单步执行 next

ret 单步返回

丨往期精选丨



请添加小编微信2518988391(备注岗位)

Read more
Views
Loading

文章有问题?点此查看未经处理的缓存