栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

gdb调试之查看源程序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gdb调试之查看源程序

摘要 显示源代码 list

显示程序第linenum行的周围的源程序。

list

显示函数名为function的函数的源程序。

list

显示当前行后面的源程序。

list -

显示当前行前面的源程序。

set listsize

设置一次显示源代码的行数。

show listsize

查看当前listsize的设置。

list ,

显示从first行到last行之间的源代码。

list ,

显示从当前行到last行之间的源代码。

list +

往后显示源代码。

搜索源代码 forward-search/search

从当前行向后查找匹配某个字符串的程序行

reverse-search

从当前行向后查找匹配某个字符串的程序行

指定源文件的路径

某些时候,用-g编译过后的执行程序中只是包括了源文件的名字,没有路径名。GDB提供了可以让你指定源文件的路径的命令,以便GDB进行搜索。

directory ,dir

加一个源文件路径到当前路径的前面。如果你要指定多个路径,UNIX下你可以使用“:”,Windows下你可以使用“;”。

directory

清除所有的自定义的源文件搜索路径信息。

show directories

显示定义了的源文件搜索路径。

源代码的内存 info line

可以查看源代码在内存中的地址

disassemble

该指令可以查看源程序的当前执行时的机器码

实验
  1. 设置断点
(gdb) break func
Breakpoint 1 at 0x1149: file tst.c, line 4.
  1. 查看有func函数的行的代码内存地址
(gdb) info line tst.c:func
Line 4 of "tst.c" starts at address 0x1149  and ends at 0x1154 .
  1. 使程序运行
(gdb) r
Starting program: /home/ubuntu/learn_gdb/tst 

Breakpoint 1, func (n=0) at tst.c:4
4       {
  1. 继续运行程序
(gdb) c
Continuing.
result[1-100] = 5050 

Breakpoint 1, func (n=10) at tst.c:4
4       {
  1. 继续运行程序
(gdb) c
Continuing.
result[1-250] = 31125 
[Inferior 1 (process 3016500) exited normally]
  1. 查看此时有func函数的行的代码内存地址
(gdb) info line tst.c:func
Line 4 of "tst.c" starts at address 0x555555555149  and ends at 0x555555555154 .
  1. 显示func函数的汇编代码
(gdb) disassemble func
Dump of assembler code for function func:
   0x0000555555555149 <+0>:     endbr64 
   0x000055555555514d <+4>:     push   %rbp
   0x000055555555514e <+5>:     mov    %rsp,%rbp
   0x0000555555555151 <+8>:     mov    %edi,-0x14(%rbp)
   0x0000555555555154 <+11>:    movl   $0x0,-0x8(%rbp)
   0x000055555555515b <+18>:    movl   $0x0,-0x4(%rbp)
   0x0000555555555162 <+25>:    jmp    0x55555555516e 
   0x0000555555555164 <+27>:    mov    -0x4(%rbp),%eax
   0x0000555555555167 <+30>:    add    %eax,-0x8(%rbp)
   0x000055555555516a <+33>:    addl   $0x1,-0x4(%rbp)
   0x000055555555516e <+37>:    mov    -0x4(%rbp),%eax
--Type  for more, q to quit, c to continue without paging--c
   0x0000555555555171 <+40>:    cmp    -0x14(%rbp),%eax
   0x0000555555555174 <+43>:    jl     0x555555555164 
   0x0000555555555176 <+45>:    mov    -0x8(%rbp),%eax
   0x0000555555555179 <+48>:    pop    %rbp
   0x000055555555517a <+49>:    retq   
End of assembler dump.
参考

用GDB调试程序(四)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号