This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ObjectFormatter.cs
410 lines (352 loc) · 9.72 KB
/
ObjectFormatter.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
using System;
using System.Text;
using System.Diagnostics;
using System.Collections;
using Mono.Debugger;
using Mono.Debugger.Languages;
namespace Mono.Debugger.Frontend
{
public class ObjectFormatter
{
public readonly DisplayFormat DisplayFormat;
public static int Columns = 75;
public static bool WrapLines = true;
StringBuilder sb = new StringBuilder ();
public ObjectFormatter (DisplayFormat format)
{
this.DisplayFormat = format;
}
public void Format (Thread target, object obj)
{
if ((obj is byte) || (obj is sbyte) || (obj is char) ||
(obj is short) || (obj is ushort) || (obj is int) || (obj is uint) ||
(obj is long) || (obj is ulong)) {
if (DisplayFormat == DisplayFormat.HexaDecimal)
Append (String.Format ("0x{0:x}", obj));
else
Append (obj.ToString ());
} else if (obj is bool) {
Append (((bool) obj) ? "true" : "false");
} else if (obj is string) {
Append ('"' + (string) obj + '"');
} else if (obj is TargetType) {
Append (((TargetType) obj).Name);
} else if (obj is TargetObject) {
Format (target, (TargetObject) obj);
} else if (obj is IntPtr) {
IntPtr ptr = (IntPtr) obj;
if (ptr == IntPtr.Zero)
Append ("null");
else
Append (TargetAddress.FormatAddress (ptr.ToInt64 ()));
} else {
Append (obj.ToString ());
}
}
public void Format (Thread target, TargetObject obj)
{
FormatObjectRecursed (target, obj, false);
}
public void FormatVariable (StackFrame frame, TargetVariable variable)
{
TargetObject obj = null;
Append ("{0} = ", variable.Name);
if (!variable.IsAlive (frame.TargetAddress)) {
Append ("<optimized out>");
return;
}
try {
obj = variable.GetObject (frame);
if (obj != null)
Format (frame.Thread, obj);
else
Append ("<cannot display object>");
} catch {
Append ("<cannot display object>");
}
}
new public string ToString ()
{
return sb.ToString ();
}
int pos = 0;
int last = -1;
int indent_level = 0;
protected void Append (string text)
{
sb.Append (text);
pos += text.Length;
}
protected void Append (string text, params object[] args)
{
Append (String.Format (text, args));
}
protected void CheckLineWrap ()
{
if (!WrapLines)
return;
if (pos < Columns) {
last = sb.Length;
return;
}
string wrap = "\n" + new String (' ', indent_level);
if (last < 0)
sb.Append (wrap);
else
sb.Insert (last, wrap);
last = -1;
pos = 0;
}
protected void FormatNullable (Thread target, TargetNullableObject nullable)
{
bool has_value = nullable.HasValue (target);
if (has_value) {
TargetObject value = nullable.GetValue (target);
FormatObjectRecursed (target, value, true);
} else {
Append ("null");
}
}
protected void FormatObjectRecursed (Thread target, TargetObject obj, bool recursed)
{
try {
if (DisplayFormat == DisplayFormat.Address) {
if (obj.HasAddress)
Append (obj.GetAddress (target).ToString ());
else
Append ("<cannot get address>");
return;
} else if (obj.HasAddress && obj.GetAddress (target).IsNull) {
Append ("null");
return;
} else if (!recursed) {
FormatObject (target, obj);
return;
}
switch (obj.Kind) {
case TargetObjectKind.Enum:
FormatObject (target, obj);
break;
case TargetObjectKind.Fundamental:
TargetFundamentalObject fobj = (TargetFundamentalObject) obj;
object value = fobj.GetObject (target);
Format (target, value);
break;
case TargetObjectKind.Nullable:
FormatNullable (target, (TargetNullableObject) obj);
break;
default:
PrintObject (target, obj);
break;
}
} catch {
Append ("<cannot display object>");
}
}
protected void FormatObject (Thread target, TargetObject obj)
{
switch (obj.Kind) {
case TargetObjectKind.Array:
FormatArray (target, (TargetArrayObject) obj);
break;
case TargetObjectKind.Pointer:
TargetPointerObject pobj = (TargetPointerObject) obj;
if (!pobj.Type.IsTypesafe) {
FormatObjectRecursed (target, pobj, true);
break;
}
try {
TargetObject deref = pobj.GetDereferencedObject (target);
Append ("&({0}) ", deref.TypeName);
FormatObjectRecursed (target, deref, true);
} catch (Exception ex) {
FormatObjectRecursed (target, pobj, true);
}
break;
case TargetObjectKind.Object:
TargetObjectObject oobj = (TargetObjectObject) obj;
try {
TargetObject deref = oobj.GetDereferencedObject (target);
Append ("&({0}) ", deref.TypeName);
FormatObjectRecursed (target, deref, false);
} catch {
FormatObjectRecursed (target, oobj, true);
}
break;
case TargetObjectKind.Class:
case TargetObjectKind.Struct:
FormatStructObject (target, (TargetClassObject) obj);
break;
case TargetObjectKind.Fundamental: {
object value = ((TargetFundamentalObject) obj).GetObject (target);
Format (target, value);
break;
}
case TargetObjectKind.Enum:
FormatEnum (target, (TargetEnumObject) obj);
break;
case TargetObjectKind.GenericInstance:
FormatStructObject (target, (TargetClassObject) obj);
break;
case TargetObjectKind.Nullable:
FormatNullable (target, (TargetNullableObject) obj);
break;
default:
PrintObject (target, obj);
break;
}
}
protected void FormatStructObject (Thread target, TargetClassObject obj)
{
bool first = true;
TargetClass class_info = obj.Type.GetClass (target);
if (class_info != null)
FormatClassObject (target, obj, class_info, ref first);
}
protected void FormatClassObject (Thread target, TargetClassObject obj,
TargetClass class_info, ref bool first)
{
Append ("{ ");
indent_level += 3;
if (obj.Type.HasParent) {
TargetClassObject parent = obj.GetParentObject (target);
if ((parent != null) && (parent.Type != parent.Type.Language.ObjectType)) {
Append ("<{0}> = ", parent.Type.Name);
CheckLineWrap ();
FormatStructObject (target, parent);
first = false;
}
}
TargetFieldInfo[] fields = class_info.GetFields (target);
for (int i = 0; i < fields.Length; i++) {
if (fields [i].IsStatic || fields [i].HasConstValue)
continue;
if (fields [i].IsCompilerGenerated)
continue;
if (fields [i].DebuggerBrowsableState == DebuggerBrowsableState.Never)
continue;
if (!first) {
Append (", ");
CheckLineWrap ();
}
first = false;
Append (fields [i].Name + " = ");
try {
TargetObject fobj = class_info.GetField (target, obj, fields [i]);
if (fobj == null)
Append ("null");
else
FormatObjectRecursed (target, fobj, true);
} catch {
Append ("<cannot display object>");
}
}
Append (first ? "}" : " }");
indent_level -= 3;
}
protected void FormatEnum (Thread target, TargetEnumObject eobj)
{
TargetObject evalue = eobj.GetValue (target);
TargetFundamentalObject fobj = evalue as TargetFundamentalObject;
if ((DisplayFormat == DisplayFormat.HexaDecimal) || (fobj == null)) {
FormatObjectRecursed (target, evalue, true);
return;
}
object value = fobj.GetObject (target);
SortedList members = new SortedList ();
foreach (TargetEnumInfo field in eobj.Type.Members)
members.Add (field.Name, field.ConstValue);
if (!eobj.Type.IsFlagsEnum) {
foreach (DictionaryEntry entry in members) {
if (entry.Value.Equals (value)) {
Append ((string) entry.Key);
return;
}
}
} else if (value is ulong) {
bool first = true;
ulong the_value = (ulong) value;
foreach (DictionaryEntry entry in members) {
ulong fvalue = System.Convert.ToUInt64 (entry.Value);
if ((the_value & fvalue) != fvalue)
continue;
if (!first) {
Append (" | ");
CheckLineWrap ();
}
first = false;
Append ((string) entry.Key);
}
if (!first)
return;
} else {
bool first = true;
long the_value = System.Convert.ToInt64 (value);
foreach (DictionaryEntry entry in members) {
long fvalue = System.Convert.ToInt64 (entry.Value);
if ((the_value & fvalue) != fvalue)
continue;
if (!first) {
Append (" | ");
CheckLineWrap ();
}
first = false;
Append ((string) entry.Key);
}
if (!first)
return;
}
FormatObjectRecursed (target, fobj, true);
}
protected void FormatArray (Thread target, TargetArrayObject aobj)
{
TargetArrayBounds bounds = aobj.GetArrayBounds (target);
if (bounds.IsUnbound)
Append ("[ ]");
else
FormatArray (target, aobj, bounds, 0, new int [0]);
}
protected void FormatArray (Thread target, TargetArrayObject aobj,
TargetArrayBounds bounds, int dimension,
int[] indices)
{
Append ("[ ");
indent_level += 3;
bool first = true;
int[] new_indices = new int [dimension + 1];
indices.CopyTo (new_indices, 0);
int lower, upper;
if (!bounds.IsMultiDimensional) {
lower = 0;
upper = bounds.Length - 1;
} else {
lower = bounds.LowerBounds [dimension];
upper = bounds.UpperBounds [dimension];
}
for (int i = lower; i <= upper; i++) {
if (!first) {
Append (", ");
CheckLineWrap ();
}
first = false;
new_indices [dimension] = i;
if (dimension + 1 < bounds.Rank)
FormatArray (target, aobj, bounds, dimension + 1, new_indices);
else {
TargetObject eobj = aobj.GetElement (target, new_indices);
FormatObjectRecursed (target, eobj, false);
}
}
Append (first ? "]" : " ]");
indent_level -= 3;
}
protected void PrintObject (Thread target, TargetObject obj)
{
try {
Append (obj.Print (target));
} catch {
Append ("<cannot display object>");
}
}
}
}