Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled exception line number issues #1890

Closed
wants to merge 1 commit into from

Conversation

mikem8361
Copy link
Member

@mikem8361 mikem8361 commented Jan 17, 2020

There are a few paths to get the place (DebugStackTrace::DebugStackTraceElement::InitPass2) where
the offset/ip needs an adjustment:

  1. The System.Diagnostics.StackTrace constructors that take an exception object. The stack trace in
    the exception object is used (from the _stackTrace field).
  2. Processing an unhandled exception for display (ExceptionTracker::ProcessOSExceptionNotification).
  3. The System.Diagnostics.StackTrace constructors that don't take an exception object that get the
    stack trace of the current thread.

The changes for cases #1 and 2 start with StackTraceInfo/StackTraceElement structs (adding the
"fAdjustOffset" field) when the stack trace for an exception is generated and is put in the private
_stackTrace Exception object field.

When the stack trace for an exception is rendered to a string (via the GetStackFramesInternal FCALL)
the internal GetStackFramesData/DebugStackTraceElement structs are initialized and the new fAdjustOffset
field there is set from the one in StackTraceElement. That is where the IL offset is calculated and
this flag is used to decrement the native offset enough to point to actual instruction that caused the
exception.

For case #3 all this happens in the GetStackFramesInternal FCALL called from the managed constructor
building the GetStackFramesData/DebugStackTraceElement structs directly.

Fixes dotnet/coreclr#27765 and fixes dotnet/coreclr#25740.

@mikem8361 mikem8361 added this to the 5.0 milestone Jan 17, 2020
@mikem8361 mikem8361 self-assigned this Jan 17, 2020
// JIT_OverFlow, etc.) that caused a software exception.
// 3) The instruction after the call to a managed function (non-leaf node).
//
// #2 and #3 are the cases that need to adjust dwOffset because they point after the call instructionand
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #2 and #3 are the cases that need to adjust dwOffset because they point after the call instructionand
// #2 and #3 are the cases that need to adjust dwOffset because they point after the call instruction and

There are a few paths to get the place (DebugStackTrace::DebugStackTraceElement::InitPass2) where
the offset/ip needs an adjustment:

1) The System.Diagnostics.StackTrace constructors that take an exception object. The stack trace in
   the exception object is used (from the _stackTrace field).
2) Processing an unhandled exception for display (ExceptionTracker::ProcessOSExceptionNotification).
3) The System.Diagnostics.StackTrace constructors that don't take an exception object that get the
   stack trace of the current thread.

The changes for cases dotnet#1 and 2 start with StackTraceInfo/StackTraceElement structs (adding the
"fAdjustOffset" field) when the stack trace for an exception is generated and is put in the private
_stackTrace Exception object field.

When the stack trace for an exception is rendered to a string (via the GetStackFramesInternal FCALL)
the internal GetStackFramesData/DebugStackTraceElement structs are initialized and the new fAdjustOffset
field there is set from the one in StackTraceElement. That is where the IL offset is calculated and
this flag is used to decrement the native offset enough to point to actual instruction that caused the
exception.

For case dotnet#3 all this happens in the GetStackFramesInternal FCALL called from the managed constructor
building the GetStackFramesData/DebugStackTraceElement structs directly.

Fixes issues dotnet#27765 and dotnet#25740.
Copy link
Member

@noahfalk noahfalk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is going to break PrintException in SOS : (

@@ -30,6 +30,9 @@ struct StackTraceElement
// TRUE if this element represents the last frame of the foreign
// exception stack trace.
BOOL fIsLastFrameFromForeignStackTrace;
// TRUE if the ip needs to be adjusted back to the calling or
// throwing instruction.
BOOL fAdjustOffset;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is separately defined in SOS here. Data is extracted as a binary format here and then parsed using this type definition here. I'd expect this breaks !PrintException in SOS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see this SOS dependency and I was looking at this code for the SOS line number fix. Thanks for catching this.

// This is a workaround to fix the generation of stack traces from exception objects so that
// they point to the line that actually generated the exception instead of the line
// following.
if (!(pCf->HasFaulted() || pCf->IsIPadjusted()) && pStackTraceElem->ip != 0)
if (!pStackTraceElem->fAdjustOffset && pStackTraceElem->ip != 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic appears to have been negated?

!fAdjustOffset == 
!(!pCf->HasFaulted() && !pCf->IsIPadjusted()) ==
 ( pCf->HasFaulted() ||  pCf->IsIPadjusted())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what I was thinking. It is simple Boolean logic.

@mikem8361 mikem8361 closed this Jan 24, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants