-
Notifications
You must be signed in to change notification settings - Fork 720
/
Copy pathApp.cs
executable file
·1408 lines (1285 loc) · 59.4 KB
/
App.cs
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// #define PUBLIC_BUILD
using Microsoft.Diagnostics.Symbols;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Session;
using Microsoft.Diagnostics.Utilities;
using PerfView.Properties;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Utilities;
namespace PerfView
{
/// <summary>
/// The App Class is the main program
///
/// We don't do the normal WPF style main (where WPF is responsible for the main program because
/// on ARM devices we don't have WPF, however we still want to enable some things (like data collection).
/// Thus we need to defer touching WPF until we know we need it. To do this we take explicit control over
/// the Entry point and do command line processing first, before doing any GUI stuff.
/// </summary>
public class App
{
/// <summary>
/// At the top most level, Main simply calls 'DoMain' insuring that all error messages get to the user.
/// Main is also responsible for doing the 'install On First launch' logic that unpacks the EXE if needed.
/// </summary>
[System.STAThreadAttribute()]
// [System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static int Main(string[] args)
{
CommandProcessor = new CommandProcessor();
StreamWriter writerToCleanup = null; // If we create a log file, we need to clean it up.
int retCode = -1;
bool newConsoleCreated = false; // If we create a new console, we need to wait before existing
try
{
#if !PERFVIEW_COLLECT
// Can't display on ARM because the SplashScreen is WPF
var noGui = SupportFiles.ProcessArch == ProcessorArchitecture.Arm ||
(args.Length > 0 &&
(string.Compare(args[0], "/noGui", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(args[0], 0, "/logFile", 0, 8, StringComparison.OrdinalIgnoreCase) == 0));
// If we need to install, display the splash screen early, otherwise wait
if (!Directory.Exists(SupportFiles.SupportFileDir) && !noGui)
{
DisplaySplashScreen();
}
#endif
App.Unpack(); // Install the program if it is not done already
App.RelaunchIfNeeded(args); // If we are running from a a network share, relaunch locally.
// This does the real work
retCode = DoMain(args, ref newConsoleCreated, ref writerToCleanup);
}
catch (ThreadInterruptedException)
{
if (App.CommandProcessor.LogFile != null)
{
App.CommandProcessor.LogFile.WriteLine("Thread Aborted by user.");
}
}
catch (Exception e)
{
if (App.CommandProcessor.LogFile == null)
{
// This really can only happen when program is buggy, but we still want to display an error message.
newConsoleCreated = CreateConsole();
App.CommandProcessor.LogFile = Console.Out;
}
bool userLevel;
string message = ExceptionMessage.GetUserMessage(e, out userLevel);
App.CommandProcessor.LogFile.WriteLine(message);
}
finally
{
if (App.CommandProcessor.LogFile != null)
{
App.CommandProcessor.LogFile.Flush();
}
if (writerToCleanup != null)
{
writerToCleanup.Dispose();
}
}
// If we created a new console (collect command), prompt before closing it so the user has a chance to look at it.
if (newConsoleCreated)
{
Console.WriteLine("Press enter to close window.");
Console.ReadLine();
}
return retCode;
}
/// <summary>
/// DoMain's job is to parse the args, and determine if we should be using the GUI or the command line, then execute
/// the command in the appropriate environment.
/// </summary>
/// <param name="args">The arguments to the program</param>
/// <param name="newConsoleCreated">If we created a new console window (and thus we should wait before closing it on exit, set this)</param>
/// <param name="textWriterToCleanUp">If you create a StreamWriter, set this so we clean it up on exit.</param>
/// <returns></returns>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static int DoMain(string[] args, ref bool newConsoleCreated, ref StreamWriter textWriterToCleanUp)
{
Triggers.ETWEventTrigger.SessionNamePrefix = CommandProcessor.s_UserModeSessionName + "ETWTrigger";
CommandLineArgs = new CommandLineArgs();
if (args.Length > 0)
{
CommandLineArgs.ParseArgs(args); // This routine catches command line parsing exceptions. (sets CommandLineFailure)
}
// Figure out where output goes and set CommandProcessor.LogFile
#if !PERFVIEW_COLLECT
// On ARM we don't have a GUI
if (SupportFiles.ProcessArch == ProcessorArchitecture.Arm)
{
CommandLineArgs.NoGui = true;
}
#else
// We never support GUI in PerfViewCollect
CommandLineArgs.NoGui = true;
#endif
// If the operation is to collect, we also need to create a new console
// even if we already have one (because that console has already 'moved on' and is reading the next command)
// To get data from that console would be very confusing.
var needNewConsole = (CommandLineArgs.DoCommand == CommandProcessor.Collect && CommandLineArgs.MaxCollectSec == 0);
if (CommandLineArgs.LogFile != null)
{
textWriterToCleanUp = new StreamWriter(CommandLineArgs.LogFile, true);
textWriterToCleanUp.AutoFlush = true;
CommandProcessor.LogFile = textWriterToCleanUp;
CommandProcessor.LogFile.WriteLine("PerfView logging started at " + DateTime.Now);
CommandProcessor.LogFile.Flush();
CommandLineArgs.NoGui = true;
}
else
{
if (!CommandLineArgs.NoGui)
{
// Collect uses the GUI as well as View.
if (!(CommandLineArgs.DoCommand == null ||
CommandLineArgs.DoCommand == CommandProcessor.Collect ||
CommandLineArgs.DoCommand == CommandProcessor.Run ||
CommandLineArgs.DoCommand == CommandProcessor.View ||
CommandLineArgs.DoCommand == CommandProcessor.UserCommand ||
CommandLineArgs.DoCommand == CommandProcessor.UserCommandHelp ||
CommandLineArgs.DoCommand == CommandProcessor.GuiRun ||
CommandLineArgs.DoCommand == CommandProcessor.GuiCollect ||
CommandLineArgs.DoCommand == CommandProcessor.GuiHeapSnapshot))
{
CommandLineArgs.NoGui = true;
}
}
if (CommandLineArgs.NoGui)
{
newConsoleCreated = CreateConsole();
CommandProcessor.LogFile = Console.Out;
}
}
// Check for a common mistake (misspelling a command name)
if (CommandLineArgs.DoCommand == App.CommandProcessor.View && CommandLineArgs.DataFile != null &&
CommandLineArgs.DataFile.IndexOf('.') < 0 && CommandLineArgs.DataFile.IndexOf('\\') < 0)
{
throw new ApplicationException("Error " + CommandLineArgs.DataFile + " not a perfView command.");
}
#if !PERFVIEW_COLLECT
// Check for error where you have a TraceEvent dll in the wrong place.
var traceEventDllPath = typeof(TraceEvent).Assembly.ManifestModule.FullyQualifiedName;
if (!traceEventDllPath.StartsWith(SupportFiles.SupportFileDir, StringComparison.OrdinalIgnoreCase))
{
var correctTraceEventDll = Path.Combine(SupportFiles.SupportFileDir, "Microsoft.Diagnostics.Tracing.TraceEvent.dll");
using (var fileInUse = new PEFile.PEFile(traceEventDllPath))
using (var correctFile = new PEFile.PEFile(correctTraceEventDll))
{
if (fileInUse.Header.TimeDateStampSec != correctFile.Header.TimeDateStampSec)
{
throw new ApplicationException("Error using the wrong Microsoft.Diagnostics.Tracing.TraceEvent.dll.\r\n" +
" You cannot place a version of Microsoft.Diagnostics.Tracing.TraceEvent.dll next to PerfView.exe.");
}
}
}
#endif
// The 64 bit version of some of the native DLLs we use are built against the new Win10 libraries and will
// fail to bind if they are loaded on an older OS (it would probably work for Win8 but do we care?)
// It also can work if we only do viewing operations (msdia* uses old libraries), but again do we care?
// If we do we can move this to before the DLLs are loaded.
// Give the user a clean error.
if (Environment.Is64BitProcess && Environment.OSVersion.Version.Major * 10 + Environment.OSVersion.Version.Minor < 62)
{
throw new ApplicationException("The PerfView64 does not work properly Windows version < 10\r\n" +
" Please use the 32 bit version (PerfView.exe).");
}
// For reasons I have not dug into SetFileName does not work if you attach to a session. Warn the user.
if (CommandLineArgs.InMemoryCircularBuffer && CommandLineArgs.DoCommand == CommandProcessor.Start)
{
throw new ApplicationException("Error: InMemoryCircularBuffer currently can't be used with separate Start and Stop processes.");
}
if (CommandLineArgs.NoGui)
{
if (SupportFiles.ProcessArch != ProcessorArchitecture.Arm)
{
CloseSplashScreen();
}
if (needNewConsole && !newConsoleCreated)
{
newConsoleCreated = CreateConsole();
}
if (CommandLineArgs.CommandLineFailure != null)
{
CommandProcessor.LogFile.WriteLine("Error: " +
CommandLineArgs.CommandLineFailure.Message + "\r\n" + "Use -? for help.");
return -2;
}
if (CommandLineArgs.HelpRequested)
{
CommandProcessor.LogFile.Write(CommandLineArgs.GetHelpString(80));
return 0;
}
if (CommandLineArgs.DoCommand == null || CommandLineArgs.DoCommand == CommandProcessor.View)
{
#if !PERFVIEW_COLLECT // THese messages are confusing for PerfViewCollect given that it does not support View.
if (CommandLineArgs.DataFile != null)
{
CommandProcessor.LogFile.WriteLine("Trying to view {0}", CommandLineArgs.DataFile);
}
else
{
CommandProcessor.LogFile.WriteLine("No command given, Trying to open viewer.");
}
#endif
CommandProcessor.LogFile.WriteLine("Use 'PerfView collect' or 'PerfView HeapSnapshot' to collect data.");
return -4;
}
if (NeedsEulaConfirmation(CommandLineArgs))
{
// Send it both the the log file and the console window.
Console.WriteLine("The PerfView EULA has not been accepted. Use /AcceptEULA to accept.");
CommandProcessor.LogFile.WriteLine("The PerfView EULA has not been accepted. Use /AcceptEULA to accept.");
return -3;
}
// For these commands, redirect most of the log to a file.
if (CommandLineArgs.LogFile == null && (
CommandLineArgs.DoCommand == CommandProcessor.Start ||
CommandLineArgs.DoCommand == CommandProcessor.Stop ||
CommandLineArgs.DoCommand == CommandProcessor.Run ||
CommandLineArgs.DoCommand == CommandProcessor.Merge ||
CommandLineArgs.DoCommand == CommandProcessor.UserCommand ||
CommandLineArgs.DoCommand == CommandProcessor.Collect))
{
string verboseLogName;
if (CommandLineArgs.DataFile == null)
{
verboseLogName = "PerfViewData.log.txt";
}
else
{
verboseLogName = Path.ChangeExtension(CommandLineArgs.DataFile, "log.txt");
}
App.LogFileName = verboseLogName;
CommandProcessor.LogFile.WriteLine("VERBOSE LOG IN: {0}", verboseLogName);
if (CommandLineArgs.DoCommand != CommandProcessor.Collect)
{
CommandProcessor.LogFile.WriteLine("Use /LogFile:FILE to redirect output entirely.");
}
TextWriter verboseLog;
if (CommandLineArgs.DoCommand == CommandProcessor.Stop)
{
verboseLog = File.AppendText(verboseLogName);
}
else
{
verboseLog = File.CreateText(verboseLogName);
}
CommandProcessor.LogFile = new VerboseLogWriter(verboseLog, CommandProcessor.LogFile);
}
else
{
App.LogFileName = CommandLineArgs.LogFile;
}
var allArgs = string.Join(" ", args);
CommandProcessor.LogFile.WriteLine("[EXECUTING: PerfView {0}]", allArgs);
// Since we are not doing the GUI, just do the command directly
var retCode = CommandProcessor.ExecuteCommand(CommandLineArgs);
CommandProcessor.LogFile.WriteLine("[DONE {0} {1}: PerfView {2}]", DateTime.Now.ToString("HH:mm:ss"),
retCode == 0 ? "SUCCESS" : "FAIL", allArgs);
return retCode;
}
else
{
// Ask the gui to do the command. This is in its own method so that on ARM we never try to load WPF.
DoMainForGui();
return 0; // Does not actually return but
}
}
/// <summary>
/// Logic in DoMainForGui was segregated into its own method so that we don't load WPF until we need to (for ARM)
/// </summary>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void DoMainForGui()
{
#if !PERFVIEW_COLLECT
DisplaySplashScreen(); // If we have not already displayed the splash screen do it now.
s_splashScreen = null; // this serves no purpose any more.
var app = new PerfView.GuiApp();
app.Run();
#endif
}
// ConfigData
public static string ConfigDataFileName
{
get
{
if (s_ConfigDataName == null)
{
s_ConfigDataName = Path.Combine(SupportFiles.SupportFileDirBase, "UserConfig.xml");
}
return s_ConfigDataName;
}
}
public static ConfigData ConfigData
{
get
{
if (s_ConfigData == null)
{
s_ConfigData = new ConfigData(ConfigDataFileName, autoWrite: true);
}
return s_ConfigData;
}
}
public static void WriteConfig() { ConfigData.Write(ConfigDataFileName); }
// Logfile
/// <summary>
/// Returns the name of the log file that is saved for any perfView instance. It is the /LogFile parameter if given,
/// otherwise it is a generate name in the Temp\PerfView directory.
/// </summary>
public static string LogFileName
{
get
{
if (s_LogFileName == null)
{
if (CommandLineArgs.LogFile != null)
{
s_LogFileName = CommandLineArgs.LogFile;
}
else
{
var uniq = "";
for (int i = 1; ; i++)
{
s_LogFileName = Path.Combine(CacheFiles.CacheDir, "PerfViewLogFile" + uniq + ".txt");
if (FileUtilities.TryDelete(s_LogFileName))
{
break;
}
uniq = "." + i.ToString();
}
}
}
return s_LogFileName;
}
set
{
Debug.Assert(s_LogFileName == null);
s_LogFileName = value;
}
}
private static string s_LogFileName;
// CommandLine processing
public static CommandLineArgs CommandLineArgs;
public static CommandProcessor CommandProcessor;
/// <summary>
/// Unpacks all the support files associated with this program.
/// </summary>
public static bool Unpack()
{
var unpacked = SupportFiles.UnpackResourcesIfNeeded();
if (unpacked)
{
// We store the tutorial.cs source as a .cs.txt file so that the browser will load it properly.
// But we also need is original non .txt suffix so that source code fetching will find it.
var tutorial = Path.Combine(SupportFiles.SupportFileDir, "tutorial.cs");
var tutorialTxt = Path.Combine(SupportFiles.SupportFileDir, "tutorial.cs.txt");
if (File.Exists(tutorialTxt))
{
File.Copy(tutorialTxt, tutorial);
}
if (Environment.OSVersion.Platform != PlatformID.Unix)
{
// You don't need amd64 on ARM (TODO remove it on X86 machines too).
if (SupportFiles.ProcessArch == ProcessorArchitecture.Arm)
{
DirectoryUtilities.Clean(Path.Combine(SupportFiles.SupportFileDir, "amd64"));
}
// We have two versions of HeapDump.exe, and they each need their own copy of Microsoft.Diagnostics.Runtime.dll
// so copy this dll to the other architectures.
var fromDir = Path.Combine(SupportFiles.SupportFileDir, "x86");
foreach (var arch in new string[] { "amd64", "arm" })
{
var toDir = Path.Combine(SupportFiles.SupportFileDir, arch);
var fromFile = Path.Combine(fromDir, "Microsoft.Diagnostics.Runtime.dll");
if (Directory.Exists(toDir) && File.Exists(fromFile))
{
File.Copy(fromFile, Path.Combine(toDir, "Microsoft.Diagnostics.Runtime.dll"));
// ARM can use the X86 version of the heap dumper.
if (arch == "arm")
{
File.Copy(Path.Combine(fromDir, "HeapDump.exe"), Path.Combine(toDir, "HeapDump.exe"));
}
}
}
// To support intellisense for extensions, we need the PerfView.exe to be next to the .XML file that describes it
var targetExe = Path.Combine(SupportFiles.SupportFileDir, Path.GetFileName(SupportFiles.MainAssemblyPath));
if (!File.Exists(targetExe))
{
File.Copy(SupportFiles.MainAssemblyPath, targetExe);
// This file indicates that we need to copy the extensions if we use this EXE to run from
File.WriteAllText(Path.Combine(SupportFiles.SupportFileDir, "ExtensionsNotCopied"), "");
}
// The KernelTraceControl that works for Win10 and above does not work properly form older OSes
// The symptom is that when collecting data, it does not properly merge files and you don't get
// the KernelTraceControl events for PDBs and thus symbol lookup does not work.
var version = Environment.OSVersion.Version.Major * 10 + Environment.OSVersion.Version.Minor;
if (version < 62)
{
var kernelTraceControlDir = Path.Combine(SupportFiles.SupportFileDir, "x86");
var src = Path.Combine(kernelTraceControlDir, "KernelTraceControl.Win61.dll");
var dest = Path.Combine(kernelTraceControlDir, "KernelTraceControl.dll");
FileUtilities.ForceCopy(src, dest);
}
SetPermissionsForWin8Apps();
}
}
return unpacked;
}
/// <summary>
/// This code is separated into its own method because it uses Command, which is in TraceEvent.dll, which was
/// unpacked in the previous step.
/// </summary>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void SetPermissionsForWin8Apps()
{
// Are we on Win8 or above
var version = Environment.OSVersion.Version.Major * 10 + Environment.OSVersion.Version.Minor;
if (version >= 62)
{
// Make sure that Win8 packages can get at the EtwClrProfiler dll.
// *S-1-15-2-1 == "ALL APPLICATION PACKAGES" we don't use the text because it does not work in other locales
var cmdLine = "icacls.exe \"" + SupportFiles.SupportFileDir + "\" /grant *S-1-15-2-1:(OI)(CI)(RX) /T";
var cmd = Command.Run(cmdLine, new CommandOptions().AddNoThrow());
Debug.Assert(cmd.ExitCode == 0);
// Also grant *S-1-1-0 = everyone read access (so that ASP.NET users can get at the ETW profiler DLL.
cmdLine = "icacls.exe \"" + SupportFiles.SupportFileDir + "\" /grant *S-1-1-0:(OI)(CI)(RX) /T";
cmd = Command.Run(cmdLine, new CommandOptions().AddNoThrow());
Debug.Assert(cmd.ExitCode == 0);
}
}
/// <summary>
/// This code is only needed because we let people run PerfView.exe from a network file
/// share and we want to update the file share easily. Currently windows makes this
/// problematic (you can't update without forcibly closing the file). Our solution is
/// to copy the EXE locally and run it there, so there is only a very short time where
/// perfView is actually being run from the network.
///
/// TODO this can be removed when we don't update PerfView commonly and are willing
/// to 'kick people off' when we need to do an update.
/// </summary>
public static void RelaunchIfNeeded(string[] args)
{
// TODO FIX NOW. should I use 'args' rather than CommandLine
var cmdLine = Environment.CommandLine;
// Give up if we are running as a script, as it is likely waiting on the result
if (cmdLine.IndexOf("/logFile=", StringComparison.OrdinalIgnoreCase) >= 0)
{
return;
}
// Is the EXE on a network share
var exe = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName;
if (!exe.StartsWith(@"\\"))
{
return;
}
// This is redundant, but insures that we don't get into infinite loops during testing.
if (exe.StartsWith(SupportFiles.SupportFileDir, StringComparison.OrdinalIgnoreCase))
{
return;
}
// We have unpacked.
Debug.Assert(Directory.Exists(SupportFiles.SupportFileDir));
try
{
bool updatedExe = false;
var targetExe = Path.Combine(SupportFiles.SupportFileDir, Path.GetFileName(exe));
if (!File.Exists(targetExe) || File.GetLastWriteTimeUtc(targetExe) != File.GetLastWriteTimeUtc(exe))
{
Directory.CreateDirectory(SupportFiles.SupportFileDir);
File.Copy(exe, targetExe);
updatedExe = true;
}
// Unpacking can create the EXE file but it does not do extensions (because they are not
// needed if you don't launch PerfView from a file share). It will however create a
// ExtentionNotCopied file to mark the fact that it is just the EXE so we can do this quick check.
var extensionsCopiedFile = Path.Combine(SupportFiles.SupportFileDir, "ExtensionsNotCopied");
if (updatedExe || File.Exists(extensionsCopiedFile))
{
var pdbFile = Path.ChangeExtension(exe, ".pdb");
if (File.Exists(pdbFile))
{
File.Copy(pdbFile, Path.ChangeExtension(targetExe, ".pdb"));
}
// Copy TraceEvent.pdb if you can.
var srcTraceEventPdb = Path.Combine(Path.GetDirectoryName(exe), "TraceEvent.pdb");
if (File.Exists(srcTraceEventPdb))
{
var dstTraceEventPdb = Path.Combine(SupportFiles.SupportFileDir, SupportFiles.ProcessArchitectureDirectory, "TraceEvent.pdb");
File.Copy(srcTraceEventPdb, dstTraceEventPdb);
}
//Copy any PerfViewExtensions stuff
var extensionsDir = Path.Combine(Path.GetDirectoryName(exe), "perfViewExtensions");
if (Directory.Exists(extensionsDir))
{
var dstExtensionsDir = Path.Combine(SupportFiles.SupportFileDir, "perfViewExtensions");
DirectoryUtilities.Clean(dstExtensionsDir);
DirectoryUtilities.Copy(extensionsDir, dstExtensionsDir, SearchOption.TopDirectoryOnly);
}
// Indicate that we have the extensions directory copied.
FileUtilities.ForceDelete(extensionsCopiedFile);
}
var m = System.Text.RegularExpressions.Regex.Match(cmdLine, "^\\s*\"(.*?)\"\\s*(.*)");
if (!m.Success)
{
m = System.Text.RegularExpressions.Regex.Match(cmdLine, @"\s*(\S*)\s*(.*)");
}
// Can't use Command class because that lives in TraceEvent and have not set up our assembly resolve event.
var perfView = new Process();
perfView.StartInfo.FileName = targetExe;
perfView.StartInfo.Arguments = m.Groups[2].Value;
perfView.Start();
Environment.Exit(0);
}
catch (Exception) { }
}
// Legal stuff (EULA)
public static bool NeedsEulaConfirmation(CommandLineArgs commandLineArgs)
{
var acceptedVersion = App.ConfigData["EULA_Accepted"];
if (acceptedVersion != null && acceptedVersion == EulaVersion)
{
return false;
}
// Did the accept the EULA by command line argument?
if (App.CommandLineArgs.AcceptEULA)
{
App.ConfigData["EULA_Accepted"] = EulaVersion;
return false;
}
// Internal users implicitly accept the EULA
if (AppLog.InternalUser)
{
return false;
}
return true;
}
public static void AcceptEula()
{
App.ConfigData["EULA_Accepted"] = EulaVersion;
// It will auto-update the user state.
}
/// <summary>
/// True if the process is elevated.
/// </summary>
public static bool IsElevated
{
get
{
if (!s_IsElevatedInited)
{
s_IsElevated = TraceEventSession.IsElevated() ?? false;
s_IsElevatedInited = true;
}
return s_IsElevated;
}
}
// Global symbol and source paths
public static string SymbolPath
{
get
{
if (m_SymbolPath == null)
{
// Start with _NT_SYMBOL_PATH
var symPath = new SymbolPath(Microsoft.Diagnostics.Symbols.SymbolPath.SymbolPathFromEnvironment);
// Add any path that we had on previous runs.
var savedPath = App.ConfigData["_NT_SYMBOL_PATH"];
if (savedPath != null)
{
symPath.Add(savedPath);
}
bool persistSymPath = true;
// If we still don't have anything, add a default one
// Since the default goes off machine, if we are outside of Microsoft, we have to ask
// the user for permission.
if (AppLog.InternalUser)
{
symPath.Add("SRV*http://symweb.corp.microsoft.com");
symPath.Add(Microsoft.Diagnostics.Symbols.SymbolPath.MicrosoftSymbolServerPath);
}
else if (symPath.Elements.Count == 0)
{
if (SupportFiles.ProcessArch == ProcessorArchitecture.Arm || App.CommandLineArgs.NoGui)
{
App.CommandProcessor.LogFile.WriteLine("WARNING NO _NT_SYMBOL_PATH set ...");
persistSymPath = false; // If we could not interact with the user, don't persist the answer.
}
else
{
if (UserOKWithSymbolServerGui())
{
symPath.Add(Microsoft.Diagnostics.Symbols.SymbolPath.MicrosoftSymbolServerPath);
}
}
}
// TODO FIX NOW we will end up with both internal and external symbol servers on the symbol path.
// Should we clean that up?
// Remember it.
if (persistSymPath)
{
SymbolPath = symPath.InsureHasCache(symPath.DefaultSymbolCache()).CacheFirst().ToString();
}
}
return m_SymbolPath;
}
set
{
m_SymbolPath = value;
if (App.ConfigData["_NT_SYMBOL_PATH"] != m_SymbolPath)
{
App.ConfigData["_NT_SYMBOL_PATH"] = m_SymbolPath;
}
}
}
public static string SourcePath
{
get
{
if (m_SourcePath == null)
{
var symPath = new SymbolPath(Environment.GetEnvironmentVariable("_NT_SOURCE_PATH"));
var savedPath = App.ConfigData["_NT_SOURCE_PATH"];
if (savedPath != null)
{
symPath.Add(savedPath);
}
// Remember it.
SourcePath = symPath.ToString();
}
return m_SourcePath;
}
set
{
m_SourcePath = value;
App.ConfigData["_NT_SOURCE_PATH"] = value;
}
}
/// <summary>
/// A SymbolReader contains all the context (symbol path, symbol lookup preferences ...) needed
/// to look up PDB files needed to give events in the TraceLog symbolic names. Note that by
/// default this symbol path includes directories relative to the TraceLog (the directory and
/// a 'Symbols' directory next to the file).
/// </summary>
public static SymbolReader GetSymbolReader(string etlFilePath = null, SymbolReaderOptions symbolFlags = SymbolReaderOptions.None)
{
var log = App.CommandProcessor.LogFile;
SymbolPath symPath = new SymbolPath(App.SymbolPath);
if ((symbolFlags & SymbolReaderOptions.CacheOnly) != 0)
{
symPath = new SymbolPath("SRV*" + symPath.DefaultSymbolCache());
}
var sourcePath = App.SourcePath;
string localSymDir = symPath.DefaultSymbolCache();
if (etlFilePath != null)
{
// Add the directory where the file resides and a 'symbols' subdirectory
var filePathDir = Path.GetDirectoryName(etlFilePath);
if (filePathDir.Length != 0)
{
// Then the directory where the .ETL file lives.
symPath.Insert(filePathDir);
// If there is a 'symbols' directory next to the data file, look for symbols there
// as well. Note that we also put copies of any symbols here as well (see below)
string potentiallocalSymDir = Path.Combine(filePathDir, "symbols");
if (Directory.Exists(potentiallocalSymDir))
{
symPath.Insert(potentiallocalSymDir);
symPath.Insert("SRV*" + potentiallocalSymDir);
localSymDir = potentiallocalSymDir;
}
// WPR conventions add any .etl.ngenPDB directory to the path too. has higher priority still.
var wprSymDir = etlFilePath + ".NGENPDB";
if (Directory.Exists(wprSymDir))
{
symPath.Insert("SRV*" + wprSymDir);
}
else
{
// I have now seen both conventions .etl.ngenpdb and .ngenpdb, so look for both.
wprSymDir = Path.ChangeExtension(etlFilePath, ".NGENPDB");
if (Directory.Exists(wprSymDir))
{
symPath.Insert("SRV*" + wprSymDir);
}
}
// VS uses .NGENPDBS as a convention.
wprSymDir = etlFilePath + ".NGENPDBS";
if (Directory.Exists(wprSymDir))
{
symPath.Insert("SRV*" + wprSymDir);
}
if (!string.IsNullOrWhiteSpace(sourcePath))
{
sourcePath += ";";
}
sourcePath += filePathDir;
var srcDir = Path.Combine(filePathDir, "src");
if (Directory.Exists(srcDir))
{
sourcePath += ";" + srcDir;
}
}
}
// Add the Support Files directory so that you get the tutorial example
if (!string.IsNullOrWhiteSpace(sourcePath))
{
sourcePath += ";";
}
sourcePath += SupportFiles.SupportFileDir;
// Can we use the cached symbol reader?
if (s_symbolReader != null)
{
s_symbolReader.SourcePath = sourcePath;
if (symbolFlags == SymbolReaderOptions.None && s_symbolReader.SymbolPath == symPath.ToString())
{
return s_symbolReader;
}
s_symbolReader.Dispose();
s_symbolReader = null;
}
log.WriteLine("Symbol reader _NT_SYMBOL_PATH= {");
foreach (var element in symPath.Elements)
{
log.WriteLine(" {0};", element.ToString());
}
log.WriteLine(" }");
log.WriteLine("This can be set using the File -> Set Symbol Path dialog on the Stack Viewer.");
SymbolReader ret = new SymbolReader(log, symPath.ToString());
ret.SourcePath = sourcePath;
ret.Options = symbolFlags;
#if !PERFVIEW_COLLECT
if (!AppLog.InternalUser && !App.CommandLineArgs.TrustPdbs)
{
ret.SecurityCheck = delegate (string pdbFile)
{
var result = System.Windows.MessageBox.Show("Found " + pdbFile + " in a location that may not be trustworthy, do you trust this file?",
"Security Check", System.Windows.MessageBoxButton.YesNo);
return result == System.Windows.MessageBoxResult.Yes;
};
}
else
#endif
{
ret.SecurityCheck = (pdbFile => true);
}
ret.SourceCacheDirectory = Path.Combine(CacheFiles.CacheDir, "src");
if (localSymDir != null)
{
ret.OnSymbolFileFound += (pdbPath, pdbGuid, pdbAge) => CacheInLocalSymDir(localSymDir, pdbPath, pdbGuid, pdbAge, log);
}
if (symbolFlags == SymbolReaderOptions.None)
{
s_symbolReader = ret;
}
return ret;
}
#region private
/// <summary>
/// This routine gets called every time we find a PDB. We copy any PDBs to 'localPdbDir' if it is not
/// already there. That way every PDB that is needed is locally available, which is a nice feature.
/// We log any action we take to 'log'.
/// </summary>
private static void CacheInLocalSymDir(string localPdbDir, string pdbPath, Guid pdbGuid, int pdbAge, TextWriter log)
{
// We do this all in a fire-and-forget task so that it does not block the User. It is
// optional after all.
Task.Factory.StartNew(delegate ()
{
try
{
var fileName = Path.GetFileName(pdbPath);
if (pdbGuid != Guid.Empty)
{
var pdbPathPrefix = Path.Combine(localPdbDir, fileName);
// There is a non-trivial possibility that someone puts a FILE that is named what we want the dir to be.
if (File.Exists(pdbPathPrefix))
{
// If the pdb path happens to be the SymbolCacheDir (a definite possibility) then we would
// clobber the source file in our attempt to set up the target. In this case just give up
// and leave the file as it was.
if (string.Compare(pdbPath, pdbPathPrefix, StringComparison.OrdinalIgnoreCase) == 0)
{
return;
}
log.WriteLine("Removing file {0} from symbol cache to make way for symsrv files.", pdbPathPrefix);
File.Delete(pdbPathPrefix);
}
localPdbDir = Path.Combine(pdbPathPrefix, pdbGuid.ToString("N") + pdbAge.ToString());
}
if (!Directory.Exists(localPdbDir))
{
Directory.CreateDirectory(localPdbDir);
}
var localPdbPath = Path.Combine(localPdbDir, fileName);
var fileExists = File.Exists(localPdbPath);
if (!fileExists || File.GetLastWriteTimeUtc(localPdbPath) != File.GetLastWriteTimeUtc(pdbPath))
{
if (fileExists)
{
log.WriteLine("WARNING: overwriting existing file {0}.", localPdbPath);
}
log.WriteLine("Copying {0} to local cache {1}", pdbPath, localPdbPath);
// Do it as a copy and a move so that the update is atomic.
var newLocalPdbPath = localPdbPath + ".new";
FileUtilities.ForceCopy(pdbPath, newLocalPdbPath);
FileUtilities.ForceMove(newLocalPdbPath, localPdbPath);
}
}
catch (Exception e)
{
log.WriteLine("Error trying to update local PDB cache {0}", e.Message);
}
});
}
// Display the splash screen (if it is not already displayed).
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void DisplaySplashScreen()
{
#if !PERFVIEW_COLLECT
try
{
if (s_splashScreen == null)
{
var splashScreen = new System.Windows.SplashScreen("splashscreen.png");
s_splashScreen = new WeakReference(splashScreen);
splashScreen.Show(true);
}
}
catch (Exception) { }
#endif
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void CloseSplashScreen()
{
#if !PERFVIEW_COLLECT
if (s_splashScreen != null)
{
var splashScreen = (System.Windows.SplashScreen)s_splashScreen.Target;
s_splashScreen = null;
if (splashScreen != null)
{
splashScreen.Close(new TimeSpan(0));
}
}
#endif
}
#if !PERFVIEW_COLLECT
private static WeakReference s_splashScreen;
#endif
private const string EulaVersion = "1";
private static ConfigData s_ConfigData;
private static string s_ConfigDataName;
private static bool s_IsElevated;
private static bool s_IsElevatedInited;
private static SymbolReader s_symbolReader;
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static bool UserOKWithSymbolServerGui()
{
#if !PERFVIEW_COLLECT
// Ask the user if it is OK to use the Microsoft symbol server.
var done = false;
var ret = false;
if (GuiApp.MainWindow == null || GuiApp.MainWindow.Dispatcher == null)
{
return ret;
}
// We are on the GUI thread, we can just open the dialog
if (GuiApp.MainWindow.Dispatcher.CheckAccess())
{
var emptyPathDialog = new PerfView.Dialogs.EmptySymbolPathDialog(GuiApp.MainWindow);
emptyPathDialog.Owner = GuiApp.MainWindow;
emptyPathDialog.ShowDialog();
ret = emptyPathDialog.UseMSSymbols;
}
else
{
// We are not on the GUI thread, we have to do BeginInvoke to get there.
// TODO this is a bit of a hack. I really want the 'current' StackWindow
GuiApp.MainWindow.Dispatcher.BeginInvoke((Action)delegate ()
{
try