Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the line number from callsite?

Status
Not open for further replies.

hunghsun

Programmer
Aug 25, 2002
31
0
0
US
Actually, this is a 2 part question. Someone told me this has something to do with stackwalking but I am not entirely sure. Any help would be appreciated.

1. How do I get the line number for the operation that is currently executing.
Ex: presume getLine() returns the line number in the code where it sits, how exactly do I implement getLine()? (i.e. getLine in this case returns 22)

#20 main() {
#22 getLine()}

2. How do I get the lin number for the call site? (i.e. I want the getCallsite to return 14 not 10)

#10 A() { getCallsite(); }
#12 main() {
#14 A(); }

Hung-Hsun
 
> (i.e. getLine in this case returns 22)
Use the pre-processor symbol __LINE__

> 2. How do I get the lin number for the call site?
Unless you start passing __LINE__ around as a parameter, with great difficulty.

Debuggers can do this because they refer to the program symbol table, map file and program database (depending on your debugger).

Whilst walking the stack in your code is possible, it is very specific to your implementation. It will only give you program counter addresses (which you would need a good map file to turn back into line numbers).

What are you trying to do anyway - generate a track-back type stack trace for bug reports?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Specifically, I am working on implementing a PMPI library (profiling interface) for a performance analysis tool that our lab is developing. Basically, I would like to report each instance of let's say mpi_send() separately if they originate from different part of the program. Since PMPI is done using wrapper functions, I need a way to discover the callsite line within the wrapper function.
 
I am now looking at binutils for this. Anyone expert user of binutils out there to give me an example on how this would work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top