-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathDebugSupport.cs
160 lines (138 loc) · 4.47 KB
/
DebugSupport.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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See the LICENSE file in the project root for full license information.
*/
using System;
using System.IO;
using System.Diagnostics;
using Tpm2Lib;
namespace Tpm2Tester
{
internal class Dbg
{
internal static bool Enabled = false;
internal bool ThisEnabled = false;
private string CurIndent = "";
internal Dbg(bool enabled = false)
{
ThisEnabled = enabled;
}
internal void Trace(string format, params object[] args)
{
if (Enabled && ThisEnabled)
{
Debug.WriteLine(CurIndent + format, args);
}
}
internal void Indent()
{
if (Enabled && ThisEnabled)
{
CurIndent += " ";
}
}
internal void Unindent()
{
if (Enabled && ThisEnabled && CurIndent.Length > 3)
{
CurIndent = CurIndent.Substring(0, CurIndent.Length - 4);
}
}
} // class Dbg
#if !TSS_NO_TCP
// TransportLogger creates log files of TPM commands executed.
// Currently only works with the TCP TPM device.
internal class TransportLogger
{
string dir = null;
StreamWriter log;
bool Sending = true;
string CurrentTest = null;
string tempName = null;
bool logging = false;
bool PhaseToLog = false;
internal bool IsLogging()
{
return logging;
}
internal TransportLogger() { }
internal TransportLogger(string logDirectory, Tpm2Device device)
{
dir = logDirectory;
if (!Directory.CreateDirectory(logDirectory).Exists)
return;
logging = true;
var d = device as TcpTpmDevice;
if (d == null)
{
throw new Exception("Logging only supports TPM over TCP");
}
d.SetTransportCallback(this.NotifyData);
InitTempLogFile();
}
void InitTempLogFile()
{
tempName = Path.GetFullPath(dir) + Path.DirectorySeparatorChar + "temp_log.txt";
log = new StreamWriter(new FileStream(tempName, FileMode.Create));
}
internal void NotifyTestStart(string testName)
{
if (!logging)
return;
lock (notifyLock)
{
CurrentTest = testName;
Sending = true;
PhaseToLog = true;
}
}
internal void NotifyTestComplete()
{
if (!logging)
return;
lock (notifyLock)
{
log.Flush();
#if !TSS_MIN_API
log.Close();
#endif
log.Dispose();
string fileName = CurrentTest + "_log.txt";
string logName = Path.GetFullPath(dir) +
Path.DirectorySeparatorChar + fileName;
if (File.Exists(logName)) File.Delete(logName);
File.Move(tempName, logName);
log = null;
InitTempLogFile();
Debug.Assert(Sending);
PhaseToLog = false;
}
}
Object notifyLock = new Object();
TcpTpmDevice.Channel lastChannel = TcpTpmDevice.Channel.Undefined;
TcpTpmDevice.CommsSort lastSort = TcpTpmDevice.CommsSort.Undefined;
internal void NotifyData(TcpTpmDevice.CommsSort sort,
TcpTpmDevice.Channel channel, byte[] inOrOutData)
{
// Note - a complexity is that there can be two NotifyData per TCP read,
// because we need to do a Read(len) followed by a Read(data)
if (!logging || !PhaseToLog)
return;
lock (notifyLock)
{
if ((sort != lastSort) || (channel != lastChannel))
{
log.WriteLine("");
string t = channel.ToString()[0] + " ";
t += (sort == TcpTpmDevice.CommsSort.ByteSent) ? "S" : "R";
t += " ";
log.Write(t);
}
lastSort = sort;
lastChannel = channel;
log.Write(Globs.HexFromByteArray(inOrOutData));
}
}
} // class TransportLogger
#endif //!TSS_NO_TCP
}