linux - How to map a function name and line number by a memory address in C language? -


how can map function name , line number memory address in gcc ?

i.e assuming prototype in c language:

void func() {   // address of caller , maybe avoided   memoryaddress = get_call_address();    // line source code executing , calls func()   linenumber = get_lineno_from_symbol ( &memoryaddress );    // grab name calls func()   functionname = get_func_from_symbol ( &memoryaddress ); } 

so there existing apis provided gcc or whatever , can meet requirements ?

many of response ;-p

if include header

#include <execinfo.h> 

then can use backtrace() function determine address of calling line, , backtrace_symbols() retrieve names of functions. however, not give line numbers (though may give enough information debugging, if require).

if absolutely need line numbers, you'll need to:

  • ensure program (and libraries) compiled debugging enabled (-g flag gcc)
  • use addr2line program translate addresses (retrieved backtrace()) file/line number references. can call program using system(), example. send output stdout, can use redirection or pipe capture output if required.

Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -