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