caller function name, its filename and line number
Muthukumar Ratty
muthu@iqmail.net
Fri Jun 27 19:58:00 GMT 2003
> /*
> * It should look for the caller function name and the file name
> * and line number where the call was done.
> *
> * It could be done through the frame pointer, the stack and
> * the symbol table or whatever other means.
> *
> * It should be possible to do it, because gdb is able to do it under
> * the backtrace command (or up command)
You got the name correct. you can use the backtrace function from glibc to
get the address and backtrace_symbols to get the symbol names for those
addresses. btw its a run time thing.... not a compile time.
> const void* return_addr = __builtin_return_address(0);
> const void* frame_addr = __builtin_frame_address(0);
you dont have to do this yourself. backtrace does it for you. its
xnot available for all glibc ports. since you already use
__builtin_*_address
it shouldnt be a problem for you.
Muthu.
>
> look_for_caller_file_and_line(return_addr, frame_addr,
> caller_name, filename, line);
>
> cout << "Error: value out of range " << x
> << " in " << caller_name
> << " in " << filename
> << " at line " << line << endl;
> #else
> cout << "Error: value out of range " << x << endl;
> #endif
> abort();
> }
> }
>
> int
> main()
> {
> check(5); // this is file main.cpp at line 65
> check(20); // this is file main.cpp at line 66
> }
> //- main.cpp ------------------------------------------------------------------
> //-----------------------------------------------------------------------------
> // Desired output:
> //
> // Error: value out of range 20 in main in main.cpp at line 66
> //-----------------------------------------------------------------------------
>
More information about the Gcc-help
mailing list