views:

255

answers:

5

Other than reading the GetFileName() file and reading to line GetFileLineNumber() of a stackframe from

new StackTrace(exception).GetFrame(x)

is there a way to get the actual line of code (as a string) which caused an exception?

Thanks

+1  A: 

I don't think so. The code is compiled, therefore it is no longer available. However, if you also have the source code available, there may be a few workarounds.

luiscubal
what about with the .pdb debug symbols?
Andrew Bullock
I don't think pdb has that kind of information. It'd be too detailed.
luiscubal
A: 

No, that's your only option as far as I know. In order to get the original line of code that caused the exception, then it's necessary to have the source available. That the StackFrame already enables you to get the line number (via the debug symbols - the PDB file in most cases), makes it straightforward enough, I'd say.

Is there any particular problem with the method you suggested?

Noldorin
just wondered if i could extract if from the .pdbs through some internal mechanism or something :s, wasnt sure what the alternatives where, hence the question :)
Andrew Bullock
The PDBs don't actually include the source code. (This would bloat them hugely, and would be redundant at the very least.) It's the PDB file that's telling you the file name line number (via the calls to the methods of StackFrame that you mentioned).
Noldorin
With regards to the alternatives, there really aren't any I'm afraid. Of course, you could decode the PDB file manually (a painful task that would be), but I'm not sure it would tell you *anything* more than the StackTrace/StackFrame objects would.
Noldorin
A: 

There is no reliable way to do this because line information is not stored in DLL files. The information which maps blocks of IL into source code lines is stored within the PDB file. You'd need to access the PDB in order to get the line information for an exception.

There is sufficient information on the StackFrame class to get the appropriate ISymUnmanagedMethod class if the PDB is available. Mainly you just need the method token and the current offset into the method. This does require you to understand the internal structure of the PDB and I'm not sure if it's documented anywhere.

PDB API: http://msdn.microsoft.com/en-us/library/ms233503.aspx

JaredPar
how would i do that? what info is available in the pdbs?
Andrew Bullock
@Andrew there is a lot of information available in the PDB's. Each language tends to store a bit of the same and a bit of language specific information. I'm not sure where the line column information is stored or where the documentation is.
JaredPar
A: 

It would be compiled, so at run time you would only have the IL. At best, you could get the IL and decompile it back to C#, much like reflector does.

epotter
any tips on doing that?
Andrew Bullock
+1  A: 

Post moretem debugging is difficult, but not impossible. There are tools you can use (here and here for example) as well as techniques.

JP Alioto