使 GDB 在调用函数时打印控制流 [英] Make GDB print control flow of functions as they are called

查看:16
本文介绍了使 GDB 在调用函数时打印控制流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在调用 gdb 时打印感兴趣的函数,并根据它们在堆栈中的深度进行缩进?

How do I make gdb print functions of interest as they are called, indented according to how deep in the stack they are?

我希望能够说出类似(编造)的话:

I want to be able to say something like (made up):

(gdb) trace Foo* Bar* printf

并让 gdb 打印所有以 Foo 或 Bar 开头的函数,因为它们被调用.有点像 gnu cflow,除了使用调试符号并且只打印实际调用的函数,而不是所有可能的调用流.

And have gdb print all functions which begin with Foo or Bar, as they are called. Kind of like gnu cflow, except using the debugging symbols and only printing functions which actually get called, not all possible call flows.

无法提供帮助的工具包括 cachegrind、callgrind 和 oprofile,它们对最常调用函数的结果进行排序.我需要保留调用顺序.

Tools which won't help include cachegrind, callgrind and oprofile, which order the results by which functions were called most often. I need the order of calling preserved.

通配符(或等效的)是必不可少的,因为有很多 Foo 和 Bar funcs.虽然我会满足于记录每个功能.或者,也许告诉 gdb 记录特定库中的所有函数.

The wildcarding (or equivalent) is essential, as there are a lot of Foo and Bar funcs. Although I would settle for recording absolutely every function. Or, perhaps telling gdb to record all functions in a particular library.

一些 GDB 向导必须有一个脚本来完成这个常见的工作!

Some GDB wizard must have a script for this common job!

推荐答案

在你的情况下,我会求助于 gdb 中的 define 命令,它允许你定义一个函数,它最多可以占用10 个参数.

In your case I would turn to the define command in gdb, which allows you to define a function, which can take up to 10 arguments.

您可以将函数名称作为参数传递给跟踪"您定义的函数,或者将它们全部记录在函数本身中.我会做类似以下的事情

You can pass in the names of functions to "trace" as arguments to the function you define, or record them all in the function itself. I'd do something like the following

define functiontrace
if $arg0
    break $arg0
    commands
        where
        continue
        end
    end

if $arg1
...

gdb 中用户定义函数的参数引用为 $arg0-$arg9.或者,您可以只在函数中记录您想要跟踪的每个函数,而不是使用 $arg0-9.

Arguments to a user-defined function in gdb are referenced as $arg0-$arg9. Alternatively, you could just record every function you wanted to trace in the function, instead of using $arg0-9.

注意:这不会缩进堆栈跟踪中的深度,但会在每次调用函数时打印堆栈跟踪.我发现这种方法比 strace 等更有用...因为它会记录您想要的 任何 函数、系统、库、本地或其他.

Note: this will not indent as to depth in the stack trace, but will print the stack trace every time the function is called. I find this approach more useful than strace etc... because it will log any function you want, system, library, local, or otherwise.

这篇关于使 GDB 在调用函数时打印控制流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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