0

The following code is used to get the IL offset.

StackTrack st = new StackTrace(exception, true);
st.GetFrame(0).GetILOffset();

At runtime, the following function makes a remote call to get the ret. Sometimes the data is null and the runtime throws an exception. When this exception is trapped by the above code, the IL offset is 0 after deployment but some valid number in dev.

void func()
{
    ....
    ret.data[0] = "a";
    ....
}

Any thoughts?

Antediluvian
  • 653
  • 1
  • 6
  • 18
  • 1
    From `GetILOffset` docs: This offset might be an approximation depending on whether or not the just-in-time (JIT) compiler is generating debugging code. https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe.getiloffset?view=netframework-4.7.2 May have something to do with JIT. – Andrey Oct 25 '18 at 00:11
  • You can try GetNativeOffset and see what it returns https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe.getnativeoffset?view=netframework-4.7.2 – Andrey Oct 25 '18 at 00:12
  • @Andrey But if I explicitly call `throw new Exception()` in `func()`, the stack trace code can give me a valid IL offset number after deployment. – Antediluvian Oct 25 '18 at 00:15
  • Those two lines of code can get compiled to quite different native binaries, not to mention that NRE is raised by the CLR itself, there is no `throw` for it. Honestly, I am guessing now, but the fact is that IL offset can't be precise after JIT. Why do you need it? What larger problem are you trying to solve? – Andrey Oct 25 '18 at 00:22
  • @Andrey GetNativeOffset() gave me some number. But how can I map it to the complied code to find the liine number in the original source file? – Antediluvian Oct 25 '18 at 00:24
  • @Andrey The error log for the deployment is not precise. With a large function, it does not contain the source file line number. So it is difficult to find the root cause with a sole function name in the exception output but hundreds of lines of code in that function. – Antediluvian Oct 25 '18 at 00:26
  • Ok, if you want line numbers in your stack trace on production you need debug symbols that are disabled by default for Production build, can try those methods https://stackoverflow.com/questions/628565/display-lines-number-in-stack-trace-for-net-assembly-in-release-mode – Andrey Oct 25 '18 at 00:46
  • @Andrey Thanks for your information but it is not an option for me to deploy the pbd files. – Antediluvian Oct 25 '18 at 02:05
  • Then you are mostly out of options. Either deploy pdbs, or you can look at third-party tools that provide app crash inspection like Raygun, they might be able to provide line numbers (I have not tried). Then there are some tricks how to map offsets to PDBs https://stackoverflow.com/questions/1328836/how-can-i-include-line-numbers-in-a-stack-trace-without-a-pdb but since your IL offset is 0 I am no sure if it will work. Try dumping offsets of all frames, not only the last one. – Andrey Oct 25 '18 at 10:01

0 Answers0