mit6.828提供的gdb使用方法复习

GDB使用技巧

本文是关于Mit 6.828 gdb的一份幻灯片的内容提炼,
虽然gdb用的蛮多的,但是才知道自己用的太蠢了

Step

‘step’ runs one line of code at a time. When there is a
function call, it steps into the called function.

‘next’ does the same thing, except that it steps over function calls.
也就是step和next都是执行一行代码,后者跳过函数调用

‘stepi’ and ‘nexti’ do the same thing for assembly instructions rather than lines of code. i意味着instruction

All take a numerical argument to specify repetition. 例如,si 4,带参数

Pressing the ‘enter’ key repeats the previous command.对的,直接enter就是重复上一条命令,步进很方便

Running

‘continue’ runs code until a breakpoint is encountered or you interrupt it with Control-C.继续到下一个断点

‘finish’ runs code until the current function returns. 继续到下一个ret?不小心步入了函数里,可以考虑用

'advance ’ runs code until the instruction pointer gets to the specified location. 没用过,指定位置

Breakpoints

‘break’ sets a breakpoint at the specified location.

Locations can be memory addresses (*0x7c00) or names (“mon backtrace”, “monitor.c:71”).

Modify breakpoints using delete, disable, enable.清除和关闭断点,要记清楚了,蛮有用的

Conditional breakpoints

'break if ’ sets a breakpoint at the specified location, but only breaks if the condition is satisfied.

设置条件断点,这个功能可以多了解下
后面的语句是条件语句,例如x>0,或者是复杂的表达式strlen(mystring)==0,注意返回的值之能是int(待考证)
假如是浮点数什么的变化,需要转化

'cond ’ adds a condition on an existing breakpoint.

Watchpoints

Like breakpoints, but with more complicated conditions.

'watch 'will stop execution whenever the expression’s value changes.

'watch -l

’ will stop execution whenever the contents of the specified memory address change.

What’s the difference between wa var and wa -l &var?

前者还可以看仅存在于寄存器里值,后者没法查看寄存器的变量的值的变化

'rwatch [-l] ’ will stop execution whenever the value of the expression is read.

Examining

‘x’ prints the raw contents of memory in whatever format you specify (x/x for hexadecimal, x/i for assembly, etc).

这个针对的是内存的原始内容,可以从寄存器取地址去看内存,例如x/6xg $rsp,看栈寄存器指向的栈顶

‘print’ evaluates a C expression and prints the result as its proper type. It is often more useful than x.

可以查看C表达式!

The output from p *((struct elfhdr *) 0x10000) is much nicer than the output from x/13x 0x10000.

这个例子能看出print的优点了,指明了文件头类型来读取,包括了大小,和结构成员的类型。

More examining

‘info registers’ prints the value of every register.显示寄存器

‘info frame’ prints the current stack frame. 当前函数的帧栈

list prints the source code of the function at the specified location.

例如list main,当然需要编译的时候带上-g选项

backtrace might be useful as you work on lab 1!

Layouts

GDB has a text user interface that shows useful information like code listing, disassembly, and register contents in a curses UI.

layout switches to the given layout.

Other tricks 奇技淫巧

You can use the ‘set’ command to change the value of avariable during execution.

You have to switch symbol files to get function andvariable names for environments other than the kernel.
For example, when debugging JOS:

symbol-file obj/user/<name>

symbol-file obj/kern/kernel

上面也只是些皮毛,毕竟,写程序,于我而言大部分时间都在调试
后续还会添加一些实用适用有趣的情况的一些例子的

Reference:
Mit 6.828 gdb slides

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值