如何找到调用函数的名称? [英] How do I find the name of the calling function?

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

问题描述

我一直在使用PRETTY_FUNCTION来输出当前的函数名,但我重新实现了一些函数,并想知道哪些函数调用它们。

I have been using PRETTY_FUNCTION to output the current function name, however I have reimplemented some functions and would like to find out which functions are calling them.

在C ++

推荐答案

这里有两个选项:

您可以使用最新版本的glibc获取完整的stacktrace(包括调用函数的名称,模块和偏移量),其中 GNU backtrace函数。请参阅我的回答这里详细信息。这可能是最简单的事情。

  1. You can get a full stacktrace (including the name, module, and offset of the calling function) with recent versions of glibc with the GNU backtrace functions. See my answer here for the details. This is probably the easiest thing.

如果这不是你想要的,那么你可以尝试libunwind ,但会涉及更多的工作。

If that isn't exactly what you're looking for, then you might try libunwind, but it's going to involve more work.

请记住,这不是静态的(如PRETTY_FUNCTION);你实际上必须走堆栈找出什么函数调用你。所以这不是真正值得做的在普通调试printfs。如果你想做更严重的调试或分析,那么这可能对你有用。

Keep in mind that this isn't something you can know statically (as with PRETTY_FUNCTION); you actually have to walk the stack to figure out what function called you. So this isn't something that's really worth doing in ordinary debug printfs. If you want to do more serious debugging or analysis, though, then this might be useful for you.

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

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