Is there a way to access debugging symbols at runtime?

Here is a sample 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);
}

Is there a way to implement get_caller_file()and get_caller_line()? I saw / used #defineing type tricks my_api_callas a function call passing in macros __FILE__and __LINE__. I was wondering if there is a way to access this information (provided it is present) at runtime instead of compile time? Shouldn't something like Valgrind do something like this to get the information it returns?

+3
source share
3 answers

, , libdwarf DWARF.

+5

. Windows Linux, , . .

+1

, , -, . .

(argv[0] - - . ).

Even if you can find debugging symbols, you will have to decode them to try to figure out where you came from.

And your code can be optimized until the point where the information is useless.

This is a long answer. The short answer is that you probably should rely on the transfer in __FILE__and __LINE__as you were. It is much more portable, reliable.

0
source

Source: https://habr.com/ru/post/1766068/


All Articles