Merge pull request #1156 from felfert/master
[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                 private bool debug_info;
61
62                 public StackTrace ()
63                 {
64                         init_frames (METHODS_TO_SKIP, false);
65                 }
66
67                 public StackTrace (bool fNeedFileInfo)
68                 {
69                         init_frames (METHODS_TO_SKIP, fNeedFileInfo);
70                 }
71
72                 public StackTrace (int skipFrames)
73                 {
74                         init_frames (skipFrames, false);
75                 }
76
77                 public StackTrace (int skipFrames, bool fNeedFileInfo)
78                 {
79                         init_frames (skipFrames, fNeedFileInfo);
80                 }
81
82                 void init_frames (int skipFrames, bool fNeedFileInfo)
83                 {
84                         if (skipFrames < 0)
85                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
86
87                         StackFrame sf;
88                         var l = new List<StackFrame> ();
89
90                         skipFrames += 2;
91                         
92                         while ((sf = new StackFrame (skipFrames, fNeedFileInfo)) != null &&
93                                sf.GetMethod () != null) {
94                                 
95                                 l.Add (sf);
96                                 skipFrames++;
97                         };
98
99                         debug_info = fNeedFileInfo;
100                         frames = l.ToArray ();
101                 }
102                 
103                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104                 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool fNeedFileInfo);
105
106                 public StackTrace (Exception e)
107                         : this (e, METHODS_TO_SKIP, false)
108                 {
109                 }
110
111                 public StackTrace (Exception e, bool fNeedFileInfo)
112                         : this (e, METHODS_TO_SKIP, fNeedFileInfo)
113                 {
114                 }
115
116                 public StackTrace (Exception e, int skipFrames)
117                         : this (e, skipFrames, false)
118                 {
119                 }
120
121                 public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo)
122                         : this (e, skipFrames, fNeedFileInfo, false)
123                 {
124                 }
125
126                 internal StackTrace (Exception e, int skipFrames, bool fNeedFileInfo, bool returnNativeFrames)
127                 {
128                         if (e == null)
129                                 throw new ArgumentNullException ("e");
130                         if (skipFrames < 0)
131                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
132
133                         frames = get_trace (e, skipFrames, fNeedFileInfo);
134
135                         if (!returnNativeFrames) {
136                                 bool resize = false;
137                                 for (int i = 0; i < frames.Length; ++i)
138                                         if (frames [i].GetMethod () == null)
139                                                 resize = true;
140
141                                 if (resize) {
142                                         var l = new List<StackFrame> ();
143
144                                         for (int i = 0; i < frames.Length; ++i)
145                                                 if (frames [i].GetMethod () != null)
146                                                         l.Add (frames [i]);
147
148                                         frames = l.ToArray ();
149                                 }
150                         }
151                 }
152
153                 public StackTrace (StackFrame frame)
154                 {
155                         this.frames = new StackFrame [1];
156                         this.frames [0] = frame;
157                 }
158
159                 [MonoLimitation ("Not possible to create StackTraces from other threads")]
160                 [Obsolete]
161                 public StackTrace (Thread targetThread, bool needFileInfo)
162                 {
163                         if (targetThread == Thread.CurrentThread){
164                                 init_frames (METHODS_TO_SKIP, needFileInfo);
165                                 return;
166                         }
167                         
168                         throw new NotImplementedException ();
169                 }
170
171                 public virtual int FrameCount {
172                         get {
173                                 return (frames == null) ? 0 : frames.Length;
174                         }
175                 }
176
177                 public virtual StackFrame GetFrame (int index)
178                 {
179                         if ((index < 0) || (index >= FrameCount)) {
180                                 return null;
181                         }
182
183                         return frames [index];
184                 }
185
186                 [ComVisibleAttribute (false)]
187                 public virtual StackFrame[] GetFrames ()
188                 {
189                         return frames;
190                 }
191
192                 public override string ToString ()
193                 {
194                         string newline = String.Format ("{0}   {1} ", Environment.NewLine, Locale.GetText ("at"));
195                         string unknown = Locale.GetText ("<unknown method>");
196                         string debuginfo = Locale.GetText (" in {0}:line {1}");
197                         StringBuilder sb = new StringBuilder ();
198                         for (int i = 0; i < FrameCount; i++) {
199                                 StackFrame frame = GetFrame (i);
200                                 if (i > 0)
201                                         sb.Append (newline);
202                                 else
203                                         sb.AppendFormat ("   {0} ", Locale.GetText ("at"));
204                                 MethodBase method = frame.GetMethod ();
205                                 if (method != null) {
206                                         // Method information available
207                                         sb.AppendFormat ("{0}.{1}", method.DeclaringType.FullName, method.Name);
208                                         /* Append parameter information */
209                                         sb.Append ("(");
210                                         ParameterInfo[] p = method.GetParametersInternal ();
211                                         for (int j = 0; j < p.Length; ++j) {
212                                                 if (j > 0)
213                                                         sb.Append (", ");
214                                                 Type pt = p[j].ParameterType;
215                                                 bool byref = pt.IsByRef;
216                                                 if (byref)
217                                                         pt = pt.GetElementType ();
218                                                 if (pt.IsClass && pt.Namespace != String.Empty) {
219                                                         sb.Append (pt.Namespace);
220                                                         sb.Append (".");
221                                                 }
222                                                 sb.Append (pt.Name);
223                                                 if (byref)
224                                                         sb.Append (" ByRef");
225                                                 sb.AppendFormat (" {0}", p [j].Name);
226                                         }
227                                         sb.Append (")");
228                                 }
229                                 else {
230                                         // Method information not available
231                                         sb.Append (unknown);
232                                 }
233
234                                 if (debug_info) {
235                                         // we were asked for debugging informations
236                                         // but that doesn't mean we have the debug information available
237                                         string fname = frame.GetSecureFileName ();
238                                         if (fname != "<filename unknown>")
239                                                 sb.AppendFormat (debuginfo, fname, frame.GetFileLineNumber ());
240                                 }
241                         }
242                         return sb.ToString ();
243                 }
244
245                 internal String ToString (TraceFormat traceFormat)
246                 {
247                         // TODO:
248                         return ToString ();
249                 }
250         }
251 }