自动化 gdb:每 10 毫秒显示一次回溯

发布于 2024-08-26 20:38:02 字数 187 浏览 10 评论 0原文

我想为 gdb 编写一个脚本,它将每 10 毫秒保存一次进程的回溯(堆栈)。我该怎么做?

它可以类似于“身无分文”的调用图分析(对于无法使用任何类型的高级分析器的人)。

是的,有很多高级分析器。适用于流行的 CPU 和流行的操作系统。 Shark 非常令人印象深刻且易于使用,但我想通过使用 gdb 来使用此类脚本获得基本功能。

I want to write a script for gdb, which will save backtrace (stack) of process every 10 ms. How can I do this?

It can be smth like call graph profiling for 'penniless' (for people, who can't use any sort of advanced profiler).

Yes, there are a lot of advanced profilers. For popular CPUs and for popular OSes. Shark is very impressive and easy to use, but I want to get a basic functionality with such script, working with gdb.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱了心跳 2024-09-02 20:38:02

你能得到lsstack吗?也许您可以从应用程序外部的脚本运行它。为什么是10毫秒? 100 毫秒或更长时,百分比大致相同。如果应用程序太快,您可以使用外部循环人为地减慢它的速度,这也不会改变百分比。就此而言,如果应用程序运行的时间足够长并且您的目标是找出性能问题所在,则可以使用 Ctrl-C 在 gdb 下手动获取示例。

Can you get lsstack? Perhaps you could run that from a script outside your app. Why 10ms? Percentages will be about the same at 100ms or more. If the app is too fast, you could artificially slow it down with an outer loop, and that wouldn't change the percentages either. For that matter, you could just use Ctrl-C to get the samples manually under gdb, if the app runs long enough and if your goal is to find out where the performance problems are.

悟红尘 2024-09-02 20:38:02

(1) 手册。在 shell 中执行以下命令。在 shell 提示符下重复按 Ctrl+C。

gdb -x print_callstack.gdb -p pid

或者,(2) 在另一个 shell 上向 pid 重复发送相同次数的信号,如下循环中

let count=0; \
while [ $count -le 100 ]; do \
  kill -INT pid ; sleep 0.10; \
  let $count=$count+1; \
done

(1) 中的 print_callstack.gdb 源代码如下所示:

set pagination 0
set $count = 0
while $count < 100
    backtrace
    continue
    set $count = $count + 1
end
detach
quit

pstack 的手册页 https://linux.die.net/man/1/pstack

(1) Manual. Execute the following in a shell. Keep pressing Ctrl+C repeatedly on shell prompt.

gdb -x print_callstack.gdb -p pid

or, (2) send signals to pid repeatedly same number of times on another shell as in below loop

let count=0; \
while [ $count -le 100 ]; do \
  kill -INT pid ; sleep 0.10; \
  let $count=$count+1; \
done

The source of print_callstack.gdb from (1) is as below:

set pagination 0
set $count = 0
while $count < 100
    backtrace
    continue
    set $count = $count + 1
end
detach
quit

man page of pstack https://linux.die.net/man/1/pstack

Spring初心 2024-09-02 20:38:02
cat > gdb.run
set pagination 0 
backtrace 
continue 
backtrace 
continue 
... as many more backtrace + continue's as needed
backtrace 
continue 
detach 
quit

当然,省略重复的换行符,在这个论坛软件中如何做单个换行符呢? :(

gdb -x gdb.run -p $pid

使用 do 即可

kill -INT $pid ; sleep 0.01

然后只需在另一个脚本的循环中

kill -INT 是当您点击 ctrl-C 时操作系统执行的操作。读者练习:制作 gdb脚本使用具有 $n 迭代的循环。

cat > gdb.run
set pagination 0 
backtrace 
continue 
backtrace 
continue 
... as many more backtrace + continue's as needed
backtrace 
continue 
detach 
quit

Of course, omit the duplicate newlines, how do you do single newlines in this forum software? :(

gdb -x gdb.run -p $pid

Then just use do

kill -INT $pid ; sleep 0.01

in a loop in another script.

kill -INT is what the OS does when you hit ctrl-C. Exercise for the reader: make the gdb script use a loop with $n iterations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文