如何跟踪在C函数调用? [英] how to trace function call in C?

查看:563
本文介绍了如何跟踪在C函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的修改源$ C ​​$ C,我怎么能跟踪一个函数的调用和参数,当调用一些什么功能(比如func100在下面的例子)。我想输出如下:

Without modifying the source code, how can i trace which functions are called and with what parameters, when some function(say func100 in the following example) is invoked. I would like the output to be as follows:

enter func100(p1001=xxx,p1002=xxx)
        enter func110(p1101=xxx,p1102=xxx)
        exit  func110(p1101=xxx,p1102=xxx)
        enter func120(p1201=xxx,p1202=xxx,p1203=xxx)
                enter func121(p1211=xxx)
                exit  func121(p1211=xxx)
        exit  func120(p1201=xxx,p1202=xxx,p1203=xxx)
exit  func100(p1001=xxx,p1002=xxx)

这是可行的?或者有什么用源$ C ​​$ C的最小修改解决?

is this doable? or what's the solution with minimum modification of source code?

推荐答案

如果您使用 GCC ,您可以使用 -finstrument函数编译标志。
它增加了code,它调用了两个函数, __ cyg_profile_func_enter __ cyg_profile_func_exit ,每当一个函数进入/退出。

If you use gcc, you can use the -finstrument-functions compilation flag. It adds code that calls two functions, __cyg_profile_func_enter and __cyg_profile_func_exit, whenever a function enters/exits.

您需要实现这些功能,做你想做的。确保编译他们要么没有标志,或属性((no_instrument_function)),所以他们不会试图自称。

You'll need to implement these functions, to do what you want. Make sure to compile them either without the flag, or with attribute((no_instrument_function)), so they won't try to call themselves.

的函数的第二个参数是一个指向呼叫站点(即,调用函数中的返回地址)。你可以只是%P 打印出来,但它会有些难以使用。您可以使用纳米找出其中包含该地址的真正功能。

The functions' second parameter would be a pointer to the call site (i.e. the return address within the calling function). You can just print it with %p, but it will be somewhat hard to use. You can use nm to figure out the real function which contains this address.

您不能得到函数参数这种方式。

You can't get the function parameters this way.

这篇关于如何跟踪在C函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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