[System] EndRead now throws WebException on abort.
[mono.git] / mcs / class / corlib / System.Diagnostics / StackTrace.cs
1 //
2 // System.Diagnostics.StackTrace.cs
3 //
4 // Author:
5 //      Alexander Klyubin (klyubin@aqris.com)
6 //      Dietmar Maurer (dietmar@ximian.com)
7 //
8 // (C) 2001
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections.Generic;
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Text;
39 using System.Threading;
40
41 namespace System.Diagnostics {
42
43         [Serializable]
44         [ComVisible (true)]
45         [MonoTODO ("Serialized objects are not compatible with .NET")]
46         public class StackTrace {
47
48         // TraceFormat is Used to specify options for how the 
49         // string-representation of a StackTrace should be generated.
50         internal enum TraceFormat 
51         {
52             Normal,
53             TrailingNewLine,        // include a trailing new line character
54             NoResourceLookup    // to prevent infinite resource recusion
55         }
56
57                 public const int METHODS_TO_SKIP = 0;
58
59                 private StackFrame[] frames;
60                 readonly StackTrace[] captured_traces;
61                 private bool debug_info;
62
63                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
64                 public StackTrace ()
65                 {
66                         init_frames (METHODS_TO_SKIP, false);
67                 }
68
69                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
70                 public StackTrace (bool fNeedFileInfo)
71                 {
72                         init_frames (METHODS_TO_SKIP, fNeedFileInfo);
73                 }
74
75                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
76                 public StackTrace (int skipFrames)
77                 {
78                         init_frames (skipFrames, false);
79                 }
80
81                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
82                 public StackTrace (int skipFrames, bool fNeedFileInfo)
83                 {
84                         init_frames (skipFrames, fNeedFileInfo);
85                 }
86
87                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
88                 void init_frames (int skipFrames, bool fNeedFileInfo)
89                 {
90                         if (skipFrames < 0)
91                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
92
93                         StackFrame sf;
94                         var l = new List<StackFrame> ();
95
96                         skipFrames += 2;
97                         
98                         while ((sf = new StackFrame (skipFrames, fNeedFileInfo)) != null &&
99                                sf.GetMethod () != null) {
100                                 
101                                 l.Add (sf);
102                                 skipFrames++;
103                         };
104
105                         debug_info = fNeedFileInfo;
106                         frames = l.ToArray ();
107                 }
108                 
109                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
110                 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool fNeedFileInfo);
111
112                 public StackTrace (Exception e)
113                         : this (e, METHODS_TO_SKIP, false)
114                 {
115                 }
116
117                 public StackTrace (Exception e, bool fNeedFileInfo)
118                         : this (e, METHODS_TO_SKIP, fNeedFileInfo)
119                 {
120                 }
121
122                 public StackTrace (Exception e, int skipFrames)
123                         : this (e, skipFrames, false)
124                 {
125                 }
126
127                 public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo)
128                 {
129                         if (e == null)
130                                 throw new ArgumentNullException ("e");
131                         if (skipFrames < 0)
132                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
133
134                         frames = get_trace (e, skipFrames, fNeedFileInfo);
135
136                         captured_traces = e.captured_traces;
137                 }
138
139                 public StackTrace (StackFrame frame)
140                 {
141                         this.frames = new StackFrame [1];
142                         this.frames [0] = frame;
143                 }
144
145                 [MonoLimitation ("Not possible to create StackTraces from other threads")]
146                 [Obsolete]
147                 public StackTrace (Thread targetThread, bool needFileInfo)
148                 {
149                         if (targetThread == Thread.CurrentThread){
150                                 init_frames (METHODS_TO_SKIP, needFileInfo);
151                                 return;
152                         }
153                         
154                         throw new NotImplementedException ();
155                 }
156
157                 internal StackTrace (StackFrame[] frames) {
158                         this.frames = frames;
159                 }
160
161                 public virtual int FrameCount {
162                         get {
163                                 return (frames == null) ? 0 : frames.Length;
164                         }
165                 }
166
167                 public virtual StackFrame GetFrame (int index)
168                 {
169                         if ((index < 0) || (index >= FrameCount)) {
170                                 return null;
171                         }
172
173                         return frames [index];
174                 }
175
176                 [ComVisibleAttribute (false)]
177                 public virtual StackFrame[] GetFrames ()
178                 {
179                         return frames;
180                 }
181
182                 bool AddFrames (StringBuilder sb)
183                 {
184                         bool printOffset;
185                         string debugInfo, indentation;
186                         string unknown = Locale.GetText ("<unknown method>");
187
188                         indentation = "  ";
189                         debugInfo = Locale.GetText (" in {0}:{1} ");
190
191                         var newline = String.Format ("{0}{1}{2} ", Environment.NewLine, indentation,
192                                         Locale.GetText ("at"));
193
194                         int i;
195                         for (i = 0; i < FrameCount; i++) {
196                                 StackFrame frame = GetFrame (i);
197                                 if (i == 0)
198                                         sb.AppendFormat ("{0}{1} ", indentation, Locale.GetText ("at"));
199                                 else
200                                         sb.Append (newline);
201
202                                 if (frame.GetMethod () == null) {
203                                         string internal_name = frame.GetInternalMethodName ();
204                                         if (internal_name != null)
205                                                 sb.Append (internal_name);
206                                         else
207                                                 sb.AppendFormat ("<0x{0:x5} + 0x{1:x5}> {2}", frame.GetMethodAddress (), frame.GetNativeOffset (), unknown);
208                                 } else {
209                                         GetFullNameForStackTrace (sb, frame.GetMethod ());
210
211                                         if (frame.GetILOffset () == -1) {
212                                                 sb.AppendFormat (" <0x{0:x5} + 0x{1:x5}>", frame.GetMethodAddress (), frame.GetNativeOffset ());
213                                                 if (frame.GetMethodIndex () != 0xffffff)
214                                                         sb.AppendFormat (" {0}", frame.GetMethodIndex ());
215                                         } else {
216                                                 sb.AppendFormat (" [0x{0:x5}]", frame.GetILOffset ());
217                                         }
218
219                                         sb.AppendFormat (debugInfo, frame.GetSecureFileName (),
220                                                          frame.GetFileLineNumber ());
221                                 }
222                         }
223
224                         return i != 0;
225                 }
226
227                 public static void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
228                 {
229                         var declaringType = mi.DeclaringType;
230                         if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
231                                 declaringType = declaringType.GetGenericTypeDefinition ();
232
233                         // Get generic definition
234                         const BindingFlags bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
235                         foreach (var m in declaringType.GetMethods (bindingflags)) {
236                                 if (m.MetadataToken == mi.MetadataToken) {
237                                         mi = m;
238                                         break;
239                                 }
240                         }
241
242                         sb.Append (declaringType.ToString ());
243
244                         sb.Append (".");
245                         sb.Append (mi.Name);
246
247                         if (mi.IsGenericMethod) {
248                                 Type[] gen_params = mi.GetGenericArguments ();
249                                 sb.Append ("[");
250                                 for (int j = 0; j < gen_params.Length; j++) {
251                                         if (j > 0)
252                                                 sb.Append (",");
253                                         sb.Append (gen_params [j].Name);
254                                 }
255                                 sb.Append ("]");
256                         }
257
258                         ParameterInfo[] p = mi.GetParameters ();
259
260                         sb.Append (" (");
261                         for (int i = 0; i < p.Length; ++i) {
262                                 if (i > 0)
263                                         sb.Append (", ");
264
265                                 Type pt = p[i].ParameterType;
266                                 if (pt.IsGenericType && ! pt.IsGenericTypeDefinition)
267                                         pt = pt.GetGenericTypeDefinition ();
268
269                                 sb.Append (pt.ToString());
270
271                                 if (p [i].Name != null) {
272                                         sb.Append (" ");
273                                         sb.Append (p [i].Name);
274                                 }
275                         }
276                         sb.Append (")");
277                 }               
278
279                 public override string ToString ()
280                 {
281                         StringBuilder sb = new StringBuilder ();
282
283                         //
284                         // Add traces captured using ExceptionDispatchInfo
285                         //
286                         if (captured_traces != null) {
287                                 foreach (var t in captured_traces) {
288                                         if (!t.AddFrames (sb))
289                                                 continue;
290
291                                         sb.Append (Environment.NewLine);
292                                         sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
293                                         sb.Append (Environment.NewLine);
294                                 }
295                         }
296
297                         AddFrames (sb);
298                         return sb.ToString ();
299                 }
300
301                 internal String ToString (TraceFormat traceFormat)
302                 {
303                         // TODO:
304                         return ToString ();
305                 }
306         }
307 }