简体   繁体   中英

How do debuggers get line numbers of commands?

I'm trying to get line numbers of address I collected in a stackwalk by using symgetlinefromaddr64, but I can't seem to get addresses of simple commands or their lines.
for example, if i'm looking at the method:

void Test(int g)
{
  g++;
  DoSomething(g);
  g--;
}

I'll get only the line number of "DoSomething", but I want the line numbers of "g++" etc. I suppose it is doable because debuggers do it. how can I do it myself in c++ on windows?

A stack walk will only retrieve addresses that are stored on the stack, which pretty much means function calls. If you want the address of your g++ or g-- , you'll need to use something other than a stack walk to get them (eg, SymGetFileLineOffsets64 ). If you're starting from a stackwalk and have info from SymGetLineFromAddr64 , you can use SymGetLineNext64 and SymGetLinePrev64 to get information about the surrounding lines.

The only way to do it is to use compiler generated symbol files like the *.pdb files for microsoft visual studio compilers (pdb stands for program database). These files contain all symbols used during the compilation step. Even for a release compilation you'll get information about the symbols in use (some may have be optimized away).

The main disadvantage is that this is highly compiler dependent/specific. gcc for example may include symbol information in the executable so-file or executable. Other compilers have other formats...

What compiler do you use (name/version)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM