Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VsCodeObjectSource.cs « MonoDevelop.Debugger.VsCodeDebugProtocol « MonoDevelop.Debugger.VSCodeDebugProtocol « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3da1def7efee55543c8093279a168ec38d643ab4 (plain)
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
using System;
using System.Linq;
using System.Text;
using System.Globalization;

using Microsoft.VisualStudio.Shared.VSCodeDebugProtocol.Messages;

using Mono.Debugging.Backend;
using Mono.Debugging.Client;
using Mono.Debugging.Evaluation;

namespace MonoDevelop.Debugger.VsCodeDebugProtocol
{
	class VSCodeObjectSource : IObjectValueSource
	{
		const VariablePresentationHint.AttributesValue ConstantReadOnlyStatic = VariablePresentationHint.AttributesValue.Constant | VariablePresentationHint.AttributesValue.ReadOnly | VariablePresentationHint.AttributesValue.Static;
		static readonly char[] CommaDotOrSquareEndBracket = { ',', '.', ']' };
		static readonly char[] CommaOrSquareEndBracket = { ',', ']' };
		static readonly char[] LessThanOrSquareBracket = { '<', '[' };

		ObjectValue[] objValChildren;

		readonly VSCodeDebuggerSession vsCodeDebuggerSession;
		readonly int parentVariablesReference;
		readonly ObjectValueFlags flags;
		readonly int variablesReference;
		readonly int frameId;
		readonly string evalName;
		readonly string display;
		readonly string name;
		readonly string type;
		readonly string val;

		static string GetActualTypeName (string type)
		{
			int startIndex;

			if (type == null)
				return string.Empty;

			if ((startIndex = type.IndexOf (" {", StringComparison.Ordinal)) != -1) {
				// The type is boxed. The string between the {}'s is the actual type name.
				int endIndex = type.LastIndexOf ('}');

				startIndex += 2;

				if (endIndex > startIndex)
					return type.Substring (startIndex, endIndex - startIndex);
			}

			return type;
		}

		static string GetFixedVariableName (string name)
		{
			// Check for a type attribute and strip it off.
			var index = name.LastIndexOf (" [", StringComparison.Ordinal);

			if (index != -1)
				return name.Remove (index);

			return name;
		}

		static bool IsMultiDimensionalArray (string type, out int arrayIndexer)
		{
			int index = type.IndexOfAny (LessThanOrSquareBracket);

			arrayIndexer = -1;

			if (index == -1)
				return false;

			if (type[index] == '<') {
				int depth = 1;

				index++;
				while (index < type.Length && depth > 0) {
					switch (type[index++]) {
					case '<': depth++; break;
					case '>': depth--; break;
					}
				}

				if (index >= type.Length || type[index] != '[')
					return false;
			}

			arrayIndexer = index++;

			return index < type.Length && type[index] == ',';
		}

		// Note: displayType will often have spaces after commas
		string GetFixedValue (string value, string canonType, string displayType)
		{
			int arrayIndex;

			if (IsMultiDimensionalArray (displayType, out arrayIndex)) {
				var arrayType = displayType.Substring (0, arrayIndex);
				var prefix = $"{{{arrayType}[";

				if (value.StartsWith (prefix, StringComparison.Ordinal)) {
					var compacted = new StringBuilder (prefix.Replace (", ", ","));
					int index = prefix.Length;

					while (index < value.Length) {
						int endIndex = value.IndexOfAny (CommaDotOrSquareEndBracket, index);
						string number;

						if (endIndex == -1)
							return value;

						if (endIndex + 1 < value.Length && value[endIndex] == '.' && value[endIndex + 1] == '.') {
							int min, max;

							number = value.Substring (index, endIndex - index);

							if (!int.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out min))
								return value;

							index = endIndex + 2;

							if ((endIndex = value.IndexOfAny (CommaOrSquareEndBracket, index)) == -1)
								return value;

							number = value.Substring (index, endIndex - index);

							if (!int.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out max))
								return value;

							compacted.Append (((max - min) + 1).ToString (CultureInfo.InvariantCulture));
						} else {
							compacted.Append (value, index, endIndex - index);
						}

						compacted.Append (value[endIndex]);
						index = endIndex + 1;

						if (value[endIndex] == ']')
							break;

						if (index < value.Length && value[index] == ' ')
							index++;
					}

					compacted.Append ('}');

					return compacted.ToString ();
				}
			} else if (canonType == "char") {
				int startIndex = value.IndexOf ('\'');

				if (startIndex != -1)
					return value.Substring (startIndex);
			} else {
				var request = new EvaluateRequest ($"typeof ({displayType}).IsEnum") { FrameId = frameId };
				var result = vsCodeDebuggerSession.protocolClient.SendRequestSync (request);

				if (result.Result.Equals ("true", StringComparison.OrdinalIgnoreCase)) {
					int endIndex = value.IndexOf (" | ", StringComparison.Ordinal);

					if (endIndex != -1) {
						// The value a bitwise-or'd set of enum values
						var expanded = new StringBuilder ();
						int index = 0;

						while (index < value.Length) {
							endIndex = value.IndexOf (" | ", index, StringComparison.Ordinal);
							string enumValue;

							if (endIndex != -1)
								enumValue = value.Substring (index, endIndex - index);
							else if (index > 0)
								enumValue = value.Substring (index);
							else
								enumValue = value;

							expanded.Append (canonType).Append ('.').Append (enumValue);

							if (endIndex == -1)
								break;

							expanded.Append (" | ");
							index = endIndex + 3;
						}

						return expanded.ToString ();
					}

					return canonType + "." + value;
				}
			}

			return value;
		}

		static bool IsCSError (int code, string message, string value, out string newValue)
		{
			var prefix = string.Format (CultureInfo.InvariantCulture, "error CS{0:D4}: '", code);

			newValue = null;

			if (value == null || !value.StartsWith (prefix, StringComparison.Ordinal))
				return false;

			int startIndex = prefix.Length;
			int index = startIndex;

			while (index < value.Length && value[index] != '\'')
				index++;

			newValue = value.Substring (startIndex, index - startIndex);
			index++;

			if (index >= value.Length || value[index] != ' ')
				return false;

			index++;

			if (index + message.Length != value.Length)
				return false;

			return string.CompareOrdinal (value, index, message, 0, message.Length) == 0;
		}

		public VSCodeObjectSource (VSCodeDebuggerSession vsCodeDebuggerSession, int variablesReference, int parentVariablesReference, string name, string type, string evalName, int frameId, string val)
		{
			this.vsCodeDebuggerSession = vsCodeDebuggerSession;
			this.parentVariablesReference = parentVariablesReference;
			this.variablesReference = variablesReference;
			this.evalName = evalName;
			this.frameId = frameId;

			if (type == null) {
				if (IsCSError (118, "is a namespace but is used like a variable", val, out string ns)) {
					this.display = this.name = this.val = ns;
					this.flags = ObjectValueFlags.Namespace;
					this.type = "<namespace>";
					return;
				}

				if (IsCSError (119, "is a type, which is not valid in the given context", val, out string vtype)) {
					if (name.StartsWith ("global::", StringComparison.Ordinal))
						vtype = name.Substring ("global::".Length);

					this.display = this.name = this.val = ObjectValueAdaptor.GetCSharpTypeName (vtype);
					this.flags = ObjectValueFlags.Type;
					this.type = "<type>";
					return;
				}
			}

			var actualType = GetActualTypeName (type);

			this.flags = parentVariablesReference > 0 ? ObjectValueFlags.None : ObjectValueFlags.ReadOnly;
			this.type = actualType.Replace (", ", ",");
			this.name = GetFixedVariableName (name);

			if (actualType != "void")
				this.val = GetFixedValue (val, this.type, actualType);
			else
				this.val = "No return value.";
			this.display = val;

			if (this.name[0] == '[')
				flags |= ObjectValueFlags.ArrayElement;

			if (type == null || val == $"'{this.name}' threw an exception of type '{this.type}'")
				flags |= ObjectValueFlags.Error;
		}

		public ObjectValue[] GetChildren (ObjectPath path, int index, int count, EvaluationOptions options)
		{
			if (objValChildren == null) {
				if (variablesReference <= 0) {
					objValChildren = new ObjectValue[0];
				} else {
					using (var timer = vsCodeDebuggerSession.EvaluationStats.StartTimer ()) {
						var children = vsCodeDebuggerSession.protocolClient.SendRequestSync (new VariablesRequest (
							variablesReference
						)).Variables;
						objValChildren = children.Select (c => VSCodeDebuggerBacktrace.VsCodeVariableToObjectValue (vsCodeDebuggerSession, c, variablesReference, frameId)).ToArray ();
						timer.Success = true;
					}
				}
			}
			return objValChildren;
		}

		class RawString : IRawValueString
		{
			string val;

			public RawString (string val)
			{
				this.val = val.Remove (val.Length - 1).Remove (0, 1);
			}

			public int Length {
				get {
					return val.Length;
				}
			}

			public string Value {
				get {
					return val;
				}
			}

			public string Substring (int index, int length)
			{
				return val.Substring (index, length);
			}
		}

		public object GetRawValue (ObjectPath path, EvaluationOptions options)
		{
			string rawValue = null;

			using (var timer = vsCodeDebuggerSession.EvaluationStats.StartTimer ()) {
				rawValue = vsCodeDebuggerSession.protocolClient.SendRequestSync (new EvaluateRequest (evalName) { FrameId = frameId }).Result;
				timer.Success = true;
			}

			if (rawValue.StartsWith ("\"", StringComparison.Ordinal)) {
				if (options.ChunkRawStrings)
					return new RawValueString (new RawString (rawValue));

				return rawValue.Substring (1, rawValue.Length - 2);
			}

			throw new NotImplementedException ();
		}

		public ObjectValue GetValue (ObjectPath path, EvaluationOptions options)
		{
			if (val == "null")
				return ObjectValue.CreateNullObject (this, name, type, flags);
			if (variablesReference == 0)//This is some kind of primitive...
				return ObjectValue.CreatePrimitive (this, new ObjectPath (name), type, new EvaluationResult (val, display), flags);
			return ObjectValue.CreateObject (this, new ObjectPath (name), type, new EvaluationResult (val, display), flags, null);
		}

		public void SetRawValue (ObjectPath path, object value, EvaluationOptions options)
		{
			var v = value.ToString ();
			if (type == "string")
				v = $"\"{v}\"";
			vsCodeDebuggerSession.protocolClient.SendRequestSync (new SetVariableRequest (parentVariablesReference, name, v));
		}

		public EvaluationResult SetValue (ObjectPath path, string value, EvaluationOptions options)
		{
			return new EvaluationResult (vsCodeDebuggerSession.protocolClient.SendRequestSync (new SetVariableRequest (parentVariablesReference, name, value)).Value);
		}
	}
}