1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Collections
;
6 using System
.Reflection
;
7 using System
.Diagnostics
;
8 using System
.Runtime
.InteropServices
;
12 [StructLayout (LayoutKind
.Sequential
)]
13 partial class Exception
15 internal readonly struct DispatchState
17 public readonly MonoStackFrame
[] StackFrames
;
19 public DispatchState (MonoStackFrame
[] stackFrames
)
21 StackFrames
= stackFrames
;
25 # region Keep in sync with MonoException in object-internals.h
27 internal string _message
;
29 Exception _innerException
;
32 string? _stackTraceString
;
35 object _dynamicMethods
; // Dynamic methods referenced by the stack trace
39 internal MonoStackFrame
[] foreignExceptionsFrames
;
40 IntPtr
[] native_trace_ips
;
41 int caught_in_unmanaged
;
44 public MethodBase
? TargetSite
{
46 StackTrace st
= new StackTrace (this, true);
47 if (st
.FrameCount
> 0)
48 return st
.GetFrame (0)?.GetMethod ();
54 public virtual string? StackTrace
=> GetStackTrace (true);
56 string? GetStackTrace (bool needFileInfo
)
58 if (_stackTraceString
!= null)
59 return _stackTraceString
;
60 if (_traceIPs
== null)
63 return new StackTrace (this, needFileInfo
).ToString (System
.Diagnostics
.StackTrace
.TraceFormat
.Normal
);
66 internal DispatchState
CaptureDispatchState ()
68 MonoStackFrame
[] stackFrames
;
70 if (_traceIPs
!= null) {
71 stackFrames
= System
.Diagnostics
.StackTrace
.get_trace (this, 0, true);
72 stackFrames
[stackFrames
.Length
- 1].isLastFrameFromForeignException
= true;
74 if (foreignExceptionsFrames
!= null) {
75 var combinedStackFrames
= new MonoStackFrame
[stackFrames
.Length
+ foreignExceptionsFrames
.Length
];
76 Array
.Copy (foreignExceptionsFrames
, 0, combinedStackFrames
, 0, foreignExceptionsFrames
.Length
);
77 Array
.Copy (stackFrames
, 0, combinedStackFrames
, foreignExceptionsFrames
.Length
, stackFrames
.Length
);
79 stackFrames
= combinedStackFrames
;
82 stackFrames
= foreignExceptionsFrames
;
85 return new DispatchState (stackFrames
);
88 internal void RestoreDispatchState (in DispatchState state
)
90 foreignExceptionsFrames
= state
.StackFrames
;
92 _stackTraceString
= null;
96 internal void SetCurrentStackTrace ()
98 // Check to see if the exception already has a stack set in it.
99 if (_stackTraceString
!= null)
100 ThrowHelper
.ThrowInvalidOperationException();
102 // TODO: Store the current stack trace into this exception
105 string? CreateSourceName ()
107 var st
= new StackTrace (this, fNeedFileInfo
: false);
108 if (st
.FrameCount
> 0) {
109 StackFrame sf
= st
.GetFrame (0)!;
110 MethodBase method
= sf
.GetMethod ();
112 Module module
= method
.Module
;
113 RuntimeModule rtModule
= module
as RuntimeModule
;
115 if (rtModule
== null) {
116 var moduleBuilder
= module
as System
.Reflection
.Emit
.ModuleBuilder
;
117 if (moduleBuilder
!= null)
118 throw new NotImplementedException (); // TODO: rtModule = moduleBuilder.InternalModule;
120 throw new ArgumentException (SR
.Argument_MustBeRuntimeReflectionObject
);
123 return rtModule
.GetRuntimeAssembly ().GetName ().Name
; // TODO: GetSimpleName ();
129 static IDictionary
CreateDataContainer () => new ListDictionaryInternal ();
131 static string? SerializationWatsonBuckets
=> null;
132 static string? SerializationRemoteStackTraceString
=> null;
133 string? SerializationStackTraceString
=> GetStackTrace (true);