gpt4 book ai didi

c - 如何计算全局变量被使用(读取和写入)的次数?

转载 作者:行者123 更新时间:2023-11-30 16:16:16 25 4
gpt4 key购买 nike

我正在尝试优化 C 代码项目。

我想计算全局变量被使用(读取或写入)的次数,以便将其放置在最合适的内存类型中。
例如,将常用的变量存储在快速访问内存类型中。

出于确定性原因禁用数据缓存。

有没有一种方法可以在不插入计数器或添加额外代码的情况下计算变量的使用次数?例如,使用汇编代码?

代码是用 C 语言编写的。

我拥有:

A) (.map) 文件,由 GCC 编译器生成,我从中提取全局变量名称、地址和大小。

B) 使用 GCC 编译器 -S 标志生成的项目的汇编代码。

非常感谢,

最佳答案

GDB 有一个名为观察点的东西:https://sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html

Set a watchpoint for an expression. GDB will break when the expression expr is written into by the program and its value changes. The simplest (and the most popular) use of this command is to watch the value of a single variable:

(gdb) watch foo

awatch [-l|-location] expr [thread thread-id] [mask maskvalue]

Set a watchpoint that will break when expr is either read from or written into by the program.

命令可以附加到观察点:https://sourceware.org/gdb/onlinedocs/gdb/Break-Commands.html#Break-Commands

You can give any breakpoint (or watchpoint or catchpoint) a series of commands to execute when your program stops due to that breakpoint… For example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.

break foo if x>0 
commands
silent
printf "x is %d\n",x
cont
end

该命令通常应该增加一个变量或将“读/写”打印到文件中,但您实际上也可以添加其他内容,例如回溯。不确定使用 gdb 进行外部通信的最佳方式。也许您以交互模式运行它就足够了。

关于c - 如何计算全局变量被使用(读取和写入)的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56691491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com