【问题标题】:How can I know which function or file and which line called a function? [duplicate]我如何知道哪个函数或文件以及哪一行调用了函数? [复制]
【发布时间】:2016-01-20 16:00:01
【问题描述】:

例如

void MyFunction()
{
    printf( "This function is called by %s in file %s on line $i.\n", func, file, line ); // Where func, file and lines should be replaced
}

int main()
{
    MyFunction();
    return 0;
}

【问题讨论】:

  • @ResidentBiscuit 您粘贴的链接不是这个问题的答案
  • 选择一种语言!这些不是 C 中推荐的声明符。

标签: c++ c function


【解决方案1】:

在 Linux 上,您可以使用 backtrace 转储堆栈:

   backtrace() returns a backtrace for the calling program, in the array
   pointed to by buffer.  A backtrace is the series of currently active
   function calls for the program.  Each item in the array pointed to by
   buffer is of type void *, and is the return address from the
   corresponding stack frame.  The size argument specifies the maximum
   number of addresses that can be stored in buffer.  If the backtrace
   is larger than size, then the addresses corresponding to the size
   most recent function calls are returned; to obtain the complete
   backtrace, make sure that buffer and size are large enough.

【讨论】:

    猜你喜欢
    • 2011-03-21
    • 2021-05-19
    • 2018-07-23
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多