forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerDebugging.cs
More file actions
411 lines (367 loc) · 13 KB
/
SerializerDebugging.cs
File metadata and controls
411 lines (367 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2013 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
#if UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WII || UNITY_IPHONE || UNITY_ANDROID || UNITY_PS3 || UNITY_XBOX360 || UNITY_FLASH || UNITY_BKACKBERRY || UNITY_WINRT
#define UNITY
#endif
using System;
#if !NETFX_35 && !UNITY && !WINDOWS_PHONE
using System.Collections.Concurrent;
#endif // !NETFX_35 && !UNITY && !WINDOWS_PHONE
using System.Collections.Generic;
#if !UNITY
using System.Diagnostics.Contracts;
#endif // !UNITY
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
namespace MsgPack.Serialization
{
/// <summary>
/// Holds debugging support information.
/// </summary>
internal static class SerializerDebugging
{
#if !XAMIOS && !XAMDROID && !UNITY
[ThreadStatic]
private static bool _traceEnabled;
/// <summary>
/// Gets or sets a value indicating whether instruction/expression tracing is enabled or not.
/// </summary>
/// <value>
/// <c>true</c> if instruction/expression tracing is enabled; otherwise, <c>false</c>.
/// </value>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static bool TraceEnabled
{
get { return _traceEnabled; }
set { _traceEnabled = value; }
}
[ThreadStatic]
private static bool _dumpEnabled;
/// <summary>
/// Gets or sets a value indicating whether IL dump is enabled or not.
/// </summary>
/// <value>
/// <c>true</c> if IL dump is enabled; otherwise, <c>false</c>.
/// </value>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static bool DumpEnabled
{
get { return _dumpEnabled; }
set { _dumpEnabled = value; }
}
#endif // !XAMIOS && !XAMDROID && !UNITY
[ThreadStatic]
private static bool _avoidsGenericSerializer;
/// <summary>
/// Gets or sets a value indicating whether generic serializer for array, <see cref="List{T}"/>, <see cref="Dictionary{TKey,TValue}"/>,
/// or <see cref="Nullable{T}"/> is not used.
/// </summary>
/// <value>
/// <c>true</c> if generic serializer is not used; otherwise, <c>false</c>.
/// </value>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static bool AvoidsGenericSerializer
{
get { return _avoidsGenericSerializer; }
set { _avoidsGenericSerializer = value; }
}
#if !XAMIOS && !XAMDROID && !UNITY
#if !NETFX_CORE
[ThreadStatic]
private static StringWriter _ilTraceWriter;
#endif // !NETFX_CORE
/// <summary>
/// Gets the <see cref="TextWriter"/> for IL tracing.
/// </summary>
/// <value>
/// The <see cref="TextWriter"/> for IL tracing.
/// This value will not be <c>null</c>.
/// </value>
public static TextWriter ILTraceWriter
{
get
{
#if !NETFX_CORE
if ( !_traceEnabled )
{
return TextWriter.Null;
}
if ( _ilTraceWriter == null )
{
_ilTraceWriter = new StringWriter( CultureInfo.InvariantCulture );
}
return _ilTraceWriter;
#else
return TextWriter.Null;
#endif // !NETFX_CORE
}
}
/// <summary>
/// Traces the specific event.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The args for formatting.</param>
#if NETFX_CORE || WINDOWS_PHONE
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "format", Justification = "Used in other platforms" )]
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args", Justification = "Used in other platforms" )]
#endif // NETFX_CORE || WINDOWS_PHONE
public static void TraceEvent( string format, params object[] args )
{
#if !NETFX_CORE && !WINDOWS_PHONE
if ( !_traceEnabled )
{
return;
}
Tracer.Emit.TraceEvent( Tracer.EventType.DefineType, Tracer.EventId.DefineType, format, args );
#endif // !NETFX_CORE && !WINDOWS_PHONE
}
/// <summary>
/// Flushes the trace data.
/// </summary>
public static void FlushTraceData()
{
#if !NETFX_CORE && !WINDOWS_PHONE
if ( !_traceEnabled )
{
return;
}
Tracer.Emit.TraceData( Tracer.EventType.DefineType, Tracer.EventId.DefineType, _ilTraceWriter.ToString() );
#endif // !NETFX_CORE && !WINDOWS_PHONE
}
#if !NETFX_CORE && !SILVERLIGHT && !NETFX_35
[ThreadStatic]
private static AssemblyBuilder _assemblyBuilder;
[ThreadStatic]
private static ModuleBuilder _moduleBuilder;
/// <summary>
/// Prepares instruction dump with specified <see cref="AssemblyBuilder"/>.
/// </summary>
/// <param name="assemblyBuilder">The assembly builder to hold instructions.</param>
public static void PrepareDump( AssemblyBuilder assemblyBuilder )
{
if ( _dumpEnabled )
{
#if DEBUG
Contract.Assert( assemblyBuilder != null );
#endif // DEBUG
_assemblyBuilder = assemblyBuilder;
}
}
/// <summary>
/// Prepares the dump with dedicated internal <see cref="AssemblyBuilder"/>.
/// </summary>
public static void PrepareDump()
{
_assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName( "ExpressionTreeSerializerLogics" ),
AssemblyBuilderAccess.Save,
default( IEnumerable<CustomAttributeBuilder> )
);
_moduleBuilder =
_assemblyBuilder.DefineDynamicModule( "ExpressionTreeSerializerLogics", "ExpressionTreeSerializerLogics.dll", true );
}
#endif // !NETFX_CORE && !SILVERLIGHT && !NETFX_35
// TODO: Cleanup %Temp% to delete temp assemblies generated for on the fly code DOM.
#if !NETFX_CORE && !SILVERLIGHT
[ThreadStatic]
private static IList<string> _runtimeAssemblies;
[ThreadStatic]
private static IList<string> _compiledCodeDomSerializerAssemblies;
public static IEnumerable<string> CodeDomSerializerDependentAssemblies
{
get
{
EnsureDependentAssembliesListsInitialized();
#if DEBUG
Contract.Assert( _compiledCodeDomSerializerAssemblies != null );
#endif // DEBUG
// FCL dependencies and msgpack core libs
foreach ( var runtimeAssembly in _runtimeAssemblies )
{
yield return runtimeAssembly;
}
// dependents
foreach ( var compiledAssembly in _compiledCodeDomSerializerAssemblies )
{
yield return compiledAssembly;
}
}
}
#endif // !NETFX_CORE && !SILVERLIGHT
#if NETFX_CORE || SILVERLIGHT
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "pathToAssembly", Justification = "For API compatibility" )]
#endif // NETFX_CORE || SILVERLIGHT
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static void AddRuntimeAssembly( string pathToAssembly )
{
#if !NETFX_CORE && !SILVERLIGHT
EnsureDependentAssembliesListsInitialized();
_runtimeAssemblies.Add( pathToAssembly );
#endif // !NETFX_CORE && !SILVERLIGHT
}
#if NETFX_CORE || SILVERLIGHT
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "pathToAssembly", Justification = "For API compatibility" )]
#endif // NETFX_CORE || SILVERLIGHT
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static void AddCompiledCodeDomAssembly( string pathToAssembly )
{
#if !NETFX_CORE && !SILVERLIGHT
EnsureDependentAssembliesListsInitialized();
_compiledCodeDomSerializerAssemblies.Add( pathToAssembly );
#endif // !NETFX_CORE && !SILVERLIGHT
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static void ResetDependentAssemblies()
{
#if !NETFX_CORE && !SILVERLIGHT
EnsureDependentAssembliesListsInitialized();
#if !NETFX_35
File.AppendAllLines( GetHistoryFilePath(), _compiledCodeDomSerializerAssemblies );
#else
File.AppendAllText( GetHistoryFilePath(), String.Join( Environment.NewLine, _compiledCodeDomSerializerAssemblies.ToArray() ) + Environment.NewLine );
#endif // !NETFX_35
_compiledCodeDomSerializerAssemblies.Clear();
ResetRuntimeAssemblies();
#endif // !NETFX_CORE && !SILVERLIGHT
}
#if !NETFX_CORE && !SILVERLIGHT
private static int _wasDeleted;
private const string HistoryFile = "MsgPack.Serialization.SerializationGenerationDebugging.CodeDOM.History.txt";
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode" , Justification = "For unit testing")]
public static void DeletePastTemporaries()
{
if ( Interlocked.CompareExchange( ref _wasDeleted, 1, 0 ) != 0 )
{
return;
}
try
{
var historyFilePath = GetHistoryFilePath();
if ( !File.Exists( historyFilePath ) )
{
return;
}
foreach ( var pastAssembly in File.ReadAllLines( historyFilePath ) )
{
if ( !String.IsNullOrEmpty( pastAssembly ) )
{
File.Delete( pastAssembly );
}
}
new FileStream( historyFilePath, FileMode.Truncate ).Close();
}
catch ( IOException ) { }
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode" , Justification = "For unit testing")]
private static string GetHistoryFilePath()
{
return Path.Combine( Path.GetTempPath(), HistoryFile );
}
private static void EnsureDependentAssembliesListsInitialized()
{
if ( _runtimeAssemblies == null )
{
_runtimeAssemblies = new List<string>();
ResetRuntimeAssemblies();
}
if ( _compiledCodeDomSerializerAssemblies == null )
{
_compiledCodeDomSerializerAssemblies = new List<string>();
}
}
private static void ResetRuntimeAssemblies()
{
_runtimeAssemblies.Add( "System.dll" );
#if NETFX_35
_runtimeAssemblies.Add( typeof( Enumerable ).Assembly.Location );
#else
_runtimeAssemblies.Add( "System.Core.dll" );
_runtimeAssemblies.Add( "System.Numerics.dll" );
#endif // NETFX_35
_runtimeAssemblies.Add( typeof( SerializerDebugging ).Assembly.Location );
}
#endif // !NETFX_CORE && !SILVERLIGHT
[ThreadStatic]
private static bool _onTheFlyCodeDomEnabled;
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static bool OnTheFlyCodeDomEnabled
{
get { return _onTheFlyCodeDomEnabled; }
set { _onTheFlyCodeDomEnabled = value; }
}
#if !NETFX_CORE && !SILVERLIGHT && !NETFX_35
/// <summary>
/// Creates the new type builder for the serializer.
/// </summary>
/// <param name="targetType">The serialization target type.</param>
/// <returns></returns>
/// <exception cref="System.InvalidOperationException">PrepareDump() was not called.</exception>
public static TypeBuilder NewTypeBuilder( Type targetType )
{
if ( _moduleBuilder == null )
{
throw new InvalidOperationException( "PrepareDump() was not called." );
}
return
_moduleBuilder.DefineType( IdentifierUtility.EscapeTypeName( targetType ) + "SerializerLogics" );
}
#endif // !NETFX_CORE && !SILVERLIGHT && !NETFX_35
#if !NETFX_CORE && !SILVERLIGHT
/// <summary>
/// Takes dump of instructions.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode" , Justification = "For unit testing")]
public static void Dump()
{
#if !NETFX_35
if ( _assemblyBuilder != null )
{
_assemblyBuilder.Save( _assemblyBuilder.GetName().Name + ".dll" );
}
#endif // !NETFX_35
}
#endif // !NETFX_CORE && !SILVERLIGHT
/// <summary>
/// Resets debugging states.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "For unit testing" )]
public static void Reset()
{
#if !NETFX_CORE && !SILVERLIGHT && !NETFX_35
_assemblyBuilder = null;
_moduleBuilder = null;
if ( _ilTraceWriter != null )
{
_ilTraceWriter.Dispose();
_ilTraceWriter = null;
}
#endif // !NETFX_CORE && !SILVERLIGHT && !NETFX_35
_dumpEnabled = false;
_traceEnabled = false;
ResetDependentAssemblies();
}
#endif // !XAMIOS && !XAMDROID && !UNITY
}
}