How do debuggers get line numbers of commands?

484 views Asked by At

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?

2

There are 2 answers

1
jdehaan On

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)?

1
Jerry Coffin On

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 (e.g., 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.