updating to the latest module.
[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;
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.Threading;
39
40 namespace System.Diagnostics {
41
42         [Serializable]
43         [MonoTODO ("Fix serialization compatibility with MS.NET")]
44         public class StackTrace {
45
46                 public const int METHODS_TO_SKIP = 0;
47
48                 private StackFrame[] frames;
49
50                 public StackTrace ()
51                 {
52                         init_frames (METHODS_TO_SKIP, false);
53                 }
54
55                 public StackTrace (bool needFileInfo)
56                 {
57                         init_frames (METHODS_TO_SKIP, needFileInfo);
58                 }
59
60                 public StackTrace (int skipFrames)
61                 {
62                         init_frames (skipFrames, false);
63                 }
64
65                 public StackTrace (int skipFrames, bool needFileInfo)
66                 {
67                         init_frames (skipFrames, needFileInfo);
68                 }
69
70                 void init_frames (int skipFrames, bool needFileInfo)
71                 {
72                         if (skipFrames < 0)
73                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
74
75                         StackFrame sf;
76                         ArrayList al = new ArrayList ();
77
78                         skipFrames += 2;
79                         
80                         while ((sf = new StackFrame (skipFrames, needFileInfo)) != null &&
81                                sf.GetMethod () != null) {
82                                 
83                                 al.Add (sf);
84                                 skipFrames++;
85                         };
86
87                         frames = (StackFrame [])al.ToArray (typeof (StackFrame));       
88                 }
89                 
90                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
91                 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool needFileInfo);
92
93                 public StackTrace (Exception e)
94                         : this (e, METHODS_TO_SKIP, false)
95                 {
96                 }
97
98                 public StackTrace (Exception e, bool needFileInfo)
99                         : this (e, METHODS_TO_SKIP, needFileInfo)
100                 {
101                 }
102
103                 public StackTrace (Exception e, int skipFrames)
104                         : this (e, skipFrames, false)
105                 {
106                 }
107
108                 public StackTrace (Exception e, int skipFrames, bool needFileInfo)
109                 {
110                         if (e == null)
111                                 throw new ArgumentNullException ("e");
112                         if (skipFrames < 0)
113                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
114
115                         frames = get_trace (e, skipFrames, needFileInfo);
116                 }
117
118 #if ONLY_1_1
119                 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
120 #endif
121                 public StackTrace (StackFrame frame)
122                 {
123                         this.frames = new StackFrame [1];
124                         this.frames [0] = frame;
125                 }
126
127 #if ONLY_1_1
128                 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
129 #endif
130                 [MonoTODO]
131                 public StackTrace (Thread targetThread, bool needFileInfo)
132                 {
133                         throw new NotImplementedException ();
134                 }
135
136                 public virtual int FrameCount {
137                         get {
138                                 return (frames == null) ? 0 : frames.Length;
139                         }
140                 }
141
142                 public virtual StackFrame GetFrame (int index)
143                 {
144                         if ((index < 0) || (index >= FrameCount)) {
145                                 return null;
146                         }
147
148                         return frames [index];
149                 }
150
151 #if NET_2_0
152                 [ComVisibleAttribute (false)]
153                 public virtual
154 #else
155                 // used for CAS implementation (before Fx 2.0)
156                 internal
157 #endif
158                 StackFrame[] GetFrames ()
159                 {
160                         return frames;
161                 }
162
163                 public override string ToString ()
164                 {
165                         string newline = String.Format ("{0}\t {1} ", Environment.NewLine, Locale.GetText ("at"));
166                         string unknown = Locale.GetText ("<unknown method>");
167                         StringBuilder sb = new StringBuilder ();
168                         for (int i = 0; i < FrameCount; i++) {
169                                 StackFrame frame = GetFrame (i);
170                                 sb.Append (newline);
171                                 MethodBase method = frame.GetMethod ();
172                                 if (method != null) {
173                                         // Method information available
174                                         sb.AppendFormat ("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
175                                 }
176                                 else {
177                                         // Method information not available
178                                         sb.Append (unknown);
179                                 }
180                         }
181
182                         return sb.ToString ();
183                 }
184         }
185 }