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

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

问题描述

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



在C ++中如何获取调用例程的函数名?

解决方案

这里有两个选项:


  1. 您可以使用最近版本的glibc与 GNU回溯功能。请参阅我在这里的回答的详细信息。这可能是最简单的事情。


  2. 如果这不是你正在寻找的,那么你可以试试 libunwind ,但它将涉及更多的工作。



  3. 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.

    In C++ how can I get the function name of the calling routine?

    解决方案

    Here are two options:

    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.

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

    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天全站免登陆