有没有办法在运行时访问调试符号? [英] Is there a way to access debug symbols at run time?
问题描述
这是一些示例代码,可以让您了解我想要什么.
Here's some example code to give an idea of what I want.
int regular_function(void)
{
int x,y,z;
/** do some stuff **/
my_api_call();
return x;
}
...
void my_api_call(void)
{
char* caller = get_caller_file();
int line = get_caller_line();
printf("I was called from %s:%d\n", caller, line);
}
有没有办法实现get_caller_file()
和get_caller_line()
?我已经看到/使用了像 #define
ing my_api_call
这样的技巧作为传入 __FILE__
和 __LINE__
的函数调用宏.我想知道是否有办法在运行时而不是编译时访问该信息(假设它存在)?难道像 Valgrind 这样的东西不是必须做这样的事情才能得到它返回的信息吗?
Is there a way to implement the get_caller_file()
and get_caller_line()
? I've seen/used tricks like #define
ing my_api_call
as a function call passing in the __FILE__
and __LINE__
macros. I was wondering if there was a way to access that information (assuming it's present) at run time instead of compile time? Wouldn't something like Valgrind have to do something like this in order to get the information it returns?
推荐答案
如果您使用调试符号编译了二进制文件,您可以使用特殊库访问它,例如 libdwarf 用于 DWARF 调试格式.
If you have compiled your binary with debug symbols, you may access it using special libraries, like libdwarf for DWARF debug format.
这篇关于有没有办法在运行时访问调试符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!