Merge pull request #5194 from xmcclure/orbis-freeswept
[mono.git] / mcs / class / Mono.Profiler.Log / Mono.Profiler.Log / LogProfiler.cs
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.
4
5 using System;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
8
9 namespace Mono.Profiler.Log {
10
11         public static class LogProfiler {
12
13                 static bool? _attached;
14
15                 public static bool IsAttached {
16                         get {
17                                 if (_attached != null)
18                                         return (bool) _attached;
19
20                                 try {
21                                         GetMaxStackTraceFrames ();
22                                         return (bool) (_attached = true);
23                                 } catch (MissingMethodException) {
24                                         return (bool) (_attached = false);
25                                 }
26                         }
27                 }
28
29                 [MethodImpl (MethodImplOptions.InternalCall)]
30                 extern static int GetMaxStackTraceFrames ();
31
32                 public static int MaxStackTraceFrames {
33                         get { return GetMaxStackTraceFrames (); }
34                 }
35
36                 [MethodImpl (MethodImplOptions.InternalCall)]
37                 extern static int GetStackTraceFrames ();
38
39                 [MethodImpl (MethodImplOptions.InternalCall)]
40                 extern static void SetStackTraceFrames (int value);
41
42                 public static int StackTraceFrames {
43                         get { return GetStackTraceFrames (); }
44                         set {
45                                 var max = MaxStackTraceFrames;
46
47                                 if (value < 0 || value > max)
48                                         throw new ArgumentOutOfRangeException (nameof (value), value, $"Value must be between 0 and {max}.");
49
50                                 SetStackTraceFrames (value);
51                         }
52                 }
53
54                 [MethodImpl (MethodImplOptions.InternalCall)]
55                 extern static LogHeapshotMode GetHeapshotMode ();
56
57                 [MethodImpl (MethodImplOptions.InternalCall)]
58                 extern static void SetHeapshotMode (LogHeapshotMode value);
59
60                 public static LogHeapshotMode HeapshotMode {
61                         get { return GetHeapshotMode (); }
62                         set {
63                                 if (!Enum.IsDefined (typeof (LogHeapshotMode), value))
64                                         throw new ArgumentException ("Invalid heapshot mode.", nameof (value));
65
66                                 SetHeapshotMode (value);
67                         }
68                 }
69
70                 [MethodImpl (MethodImplOptions.InternalCall)]
71                 extern static int GetHeapshotMillisecondsFrequency ();
72
73                 [MethodImpl (MethodImplOptions.InternalCall)]
74                 extern static void SetHeapshotMillisecondsFrequency (int value);
75
76                 public static int HeapshotMillisecondsFrequency {
77                         get { return GetHeapshotMillisecondsFrequency (); }
78                         set {
79                                 if (value < 0)
80                                         throw new ArgumentOutOfRangeException (nameof (value), value, "Value must be non-negative.");
81
82                                 SetHeapshotMillisecondsFrequency (value);
83                         }
84                 }
85
86                 [MethodImpl (MethodImplOptions.InternalCall)]
87                 extern static int GetHeapshotCollectionsFrequency ();
88
89                 [MethodImpl (MethodImplOptions.InternalCall)]
90                 extern static void SetHeapshotCollectionsFrequency (int value);
91
92                 public static int HeapshotCollectionsFrequency {
93                         get { return GetHeapshotCollectionsFrequency (); }
94                         set {
95                                 if (value < 0)
96                                         throw new ArgumentOutOfRangeException (nameof (value), value, "Value must be non-negative.");
97
98                                 SetHeapshotCollectionsFrequency (value);
99                         }
100                 }
101
102                 [MethodImpl (MethodImplOptions.InternalCall)]
103                 extern static int GetCallDepth ();
104
105                 [MethodImpl (MethodImplOptions.InternalCall)]
106                 extern static void SetCallDepth (int value);
107
108                 public static int CallDepth {
109                         get { return GetCallDepth (); }
110                         set {
111                                 if (value < 0)
112                                         throw new ArgumentOutOfRangeException (nameof (value), value, "Value must be non-negative.");
113
114                                 SetCallDepth (value);
115                         }
116                 }
117
118                 [MethodImpl (MethodImplOptions.InternalCall)]
119                 extern static void GetSampleMode (out LogSampleMode mode, out int frequency);
120
121                 [MethodImpl (MethodImplOptions.InternalCall)]
122                 extern static bool SetSampleMode (LogSampleMode value, int frequency);
123
124                 public static LogSampleMode SampleMode {
125                         get {
126                                 GetSampleMode (out var mode, out var _);
127
128                                 return mode;
129                         }
130                 }
131
132                 public static int SampleFrequency {
133                         get {
134                                 GetSampleMode (out var _, out var frequency);
135
136                                 return frequency;
137                         }
138                 }
139
140                 public static bool SetSampleParameters (LogSampleMode mode, int frequency)
141                 {
142                         if (!Enum.IsDefined (typeof (LogSampleMode), mode))
143                                 throw new ArgumentException ("Invalid sample mode.", nameof (mode));
144
145                         if (frequency < 1)
146                                 throw new ArgumentOutOfRangeException (nameof (frequency), frequency, "Frequency must be positive.");
147
148                         return SetSampleMode (mode, frequency);
149                 }
150
151                 [MethodImpl (MethodImplOptions.InternalCall)]
152                 extern static bool GetExceptionEvents ();
153
154                 [MethodImpl (MethodImplOptions.InternalCall)]
155                 extern static void SetExceptionEvents (bool value);
156
157                 public static bool ExceptionEventsEnabled {
158                         get { return GetExceptionEvents (); }
159                         set { SetExceptionEvents (value); }
160                 }
161
162                 [MethodImpl (MethodImplOptions.InternalCall)]
163                 extern static bool GetMonitorEvents ();
164
165                 [MethodImpl (MethodImplOptions.InternalCall)]
166                 extern static void SetMonitorEvents (bool value);
167
168                 public static bool MonitorEventsEnabled {
169                         get { return GetMonitorEvents (); }
170                         set { SetMonitorEvents (value); }
171                 }
172
173                 [MethodImpl (MethodImplOptions.InternalCall)]
174                 extern static bool GetGCEvents ();
175
176                 [MethodImpl (MethodImplOptions.InternalCall)]
177                 extern static void SetGCEvents (bool value);
178
179                 public static bool GCEventsEnabled {
180                         get { return GetGCEvents (); }
181                         set { SetGCEvents (value); }
182                 }
183
184                 [MethodImpl (MethodImplOptions.InternalCall)]
185                 extern static bool GetGCAllocationEvents ();
186
187                 [MethodImpl (MethodImplOptions.InternalCall)]
188                 extern static void SetGCAllocationEvents (bool value);
189
190                 public static bool GCAllocationEventsEnabled {
191                         get { return GetGCAllocationEvents (); }
192                         set { SetGCAllocationEvents (value); }
193                 }
194
195                 [MethodImpl (MethodImplOptions.InternalCall)]
196                 extern static bool GetGCMoveEvents ();
197
198                 [MethodImpl (MethodImplOptions.InternalCall)]
199                 extern static void SetGCMoveEvents (bool value);
200
201                 public static bool GCMoveEventsEnabled {
202                         get { return GetGCMoveEvents (); }
203                         set { SetGCMoveEvents (value); }
204                 }
205
206                 [MethodImpl (MethodImplOptions.InternalCall)]
207                 extern static bool GetGCRootEvents ();
208
209                 [MethodImpl (MethodImplOptions.InternalCall)]
210                 extern static void SetGCRootEvents (bool value);
211
212                 public static bool GCRootEventsEnabled {
213                         get { return GetGCRootEvents (); }
214                         set { SetGCRootEvents (value); }
215                 }
216
217                 [MethodImpl (MethodImplOptions.InternalCall)]
218                 extern static bool GetGCHandleEvents ();
219
220                 [MethodImpl (MethodImplOptions.InternalCall)]
221                 extern static void SetGCHandleEvents (bool value);
222
223                 public static bool GCHandleEventsEnabled {
224                         get { return GetGCHandleEvents (); }
225                         set { SetGCHandleEvents (value); }
226                 }
227
228                 [MethodImpl (MethodImplOptions.InternalCall)]
229                 extern static bool GetGCFinalizationEvents ();
230
231                 [MethodImpl (MethodImplOptions.InternalCall)]
232                 extern static void SetGCFinalizationEvents (bool value);
233
234                 public static bool GCFinalizationEventsEnabled {
235                         get { return GetGCFinalizationEvents (); }
236                         set { SetGCFinalizationEvents (value); }
237                 }
238
239                 [MethodImpl (MethodImplOptions.InternalCall)]
240                 extern static bool GetCounterEvents ();
241
242                 [MethodImpl (MethodImplOptions.InternalCall)]
243                 extern static void SetCounterEvents (bool value);
244
245                 public static bool CounterEventsEnabled {
246                         get { return GetCounterEvents (); }
247                         set { SetCounterEvents (value); }
248                 }
249
250                 [MethodImpl (MethodImplOptions.InternalCall)]
251                 extern static bool GetJitEvents ();
252
253                 [MethodImpl (MethodImplOptions.InternalCall)]
254                 extern static void SetJitEvents (bool value);
255
256                 public static bool JitEventsEnabled {
257                         get { return GetJitEvents (); }
258                         set { SetJitEvents (value); }
259                 }
260         }
261 }