使用gdb计算机器指令 [英] Counting machine instructions using gdb

查看:227
本文介绍了使用gdb计算机器指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据x86机器指令计数来估计程序中某个热点的确切起始位置(以便稍后可以在某个仿真器/模拟器中运行)。有没有一种方法可以使用gdb来计算执行到断点的机器指令的数量?



当然还有其他选择,我可以使用仿真/二进制仪表工具(如Pin),并在计算指令时跟踪运行情况,但这需要在我工作的每个平台上安装此工具 - 并非总是可行。我需要一些在几乎任何Linux机器上都可用的工具。



使用gdb,我猜也可以运行 stepi X 作为某种粗粒度搜索,直到我们达到断点,然后重复以降低分辨率,但这会非常缓慢。有没有另一种方法来做到这一点?

解决方案

试试这个:

 <$ c $ ($ pc!= 0xyourstoppingaddress)
stepi
set $ count = 1 + $ count
end $ b设置分页关闭
设置$ count = 0
$ b print $ count

然后去拿一杯咖啡。或者是一顿长时间的午餐。

I need to estimate the exact starting location of some hotspot in a program, in terms of x86 machine instruction count (so that it can later be run in some emulator/simulator). Is there a way to use gdb to count the number of machine instructions being executed up to a breakpoint?

There are other alternatives of course, I could use a emulation / binary instrumentation tool (like Pin), and track the run while counting instructions, but that would require installing this tool on every platform I work on - not always possible. I need some tool that's available on pretty much any linux machine.

With gdb, I guess it's also possible to run stepi X over large strides as some sort of coarse grained search until we hit the breakpoint, then repeat with reduced the resolution, but that would be excruciatingly slow. Is there another way to do this?

解决方案

Try this:

set pagination off
set $count=0
while ($pc != 0xyourstoppingaddress)
stepi
set $count=1+$count
end
print $count

Then go get a cup of coffee. Or a long lunch.

这篇关于使用gdb计算机器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆