-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfFieldFormat.cs
1156 lines (1032 loc) · 45.9 KB
/
PerfFieldFormat.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma warning disable CA1720 // Identifier contains type name
namespace Microsoft.LinuxTracepoints.Decode
{
using System;
using Debug = System.Diagnostics.Debug;
/// <summary>
/// The type of the Array property of PerfFieldFormat.
/// Array-ness of a field.
/// </summary>
public enum PerfFieldArray : byte
{
/// <summary>
/// e.g. "char val; size:1;".
/// </summary>
None,
/// <summary>
/// e.g. "char val[12]; size:12;".
/// </summary>
Fixed,
/// <summary>
/// e.g. "char val[]; size:0;".
/// </summary>
RestOfEvent,
/// <summary>
/// e.g. "__rel_loc char val[]; size:2;".
/// Value relativeOffset. dataLen is determined via strlen.
/// </summary>
RelLoc2,
/// <summary>
/// e.g. "__data_loc char val[]; size:2;".
/// Value is offset. dataLen is determined via strlen.
/// </summary>
DataLoc2,
/// <summary>
/// e.g. "__rel_loc char val[]; size:4;".
/// Value is (dataLen LSH 16) | relativeOffset.
/// </summary>
RelLoc4,
/// <summary>
/// e.g. "__data_loc char val[]; size:4;".
/// Value is (dataLen LSH 16) | offset.
/// </summary>
DataLoc4,
};
/// <summary>
/// Stores decoding information about a field, parsed from a tracefs "format" file.
/// </summary>
public class PerfFieldFormat
{
/// <summary>
/// Initializes Field, Offset, Size, and Signed properties exactly as specified.
/// Parses and deduces the other properties. The signed parameter should be null
/// if the "signed:" property is not present in the format line.
/// </summary>
internal PerfFieldFormat(
bool longSize64, // true if sizeof(long) == 8, false if sizeof(long) == 4.
string field,
ushort offset,
ushort size,
bool? signed)
{
var foundLongLong = false;
var foundLong = false;
var foundShort = false;
var foundUnsigned = false;
var foundSigned = false;
var foundStruct = false;
var foundDataLoc = false;
var foundRelLoc = false;
var foundArray = false;
var foundPointer = false;
ReadOnlySpan<char> baseType = default;
// PARSE: Name, SpecifiedArrayCount
ReadOnlySpan<char> nameSpan = default;
ushort specifiedArrayCount = 0;
Tokenizer tokenizer = new Tokenizer(field);
while (true)
{
tokenizer.MoveNext();
switch (tokenizer.Kind)
{
case TokenKind.None:
goto TokensDone;
case TokenKind.Ident:
if (tokenizer.Value.SequenceEqual("long"))
{
if (foundLong)
{
foundLongLong = true;
}
else
{
foundLong = true;
}
}
else if (tokenizer.Value.SequenceEqual("short"))
{
foundShort = true;
}
else if (tokenizer.Value.SequenceEqual("unsigned"))
{
foundUnsigned = true;
}
else if (tokenizer.Value.SequenceEqual("signed"))
{
foundSigned = true;
}
else if (tokenizer.Value.SequenceEqual("struct"))
{
foundStruct = true;
}
else if (tokenizer.Value.SequenceEqual("__data_loc"))
{
foundDataLoc = true;
}
else if (tokenizer.Value.SequenceEqual("__rel_loc"))
{
foundRelLoc = true;
}
else if (
!tokenizer.Value.SequenceEqual("__attribute__") &&
!tokenizer.Value.SequenceEqual("const") &&
!tokenizer.Value.SequenceEqual("volatile"))
{
baseType = nameSpan;
nameSpan = tokenizer.Value;
}
break;
case TokenKind.Brackets:
// [] or [ElementCount]
foundArray = true;
var arrayCount = tokenizer.Value;
int iBegin = 1; // Skip '['.
while (iBegin < arrayCount.Length && Utility.IsSpaceOrTab(arrayCount[iBegin]))
{
iBegin += 1;
}
int iEnd;
if (arrayCount.Length - iBegin > 2 &&
arrayCount[iBegin] == '0' &&
(arrayCount[iBegin + 1] == 'x' || arrayCount[iBegin + 1] == 'X'))
{
iBegin += 2; // Skip "0x".
iEnd = iBegin;
while (iEnd < arrayCount.Length && Utility.IsHexDigit(arrayCount[iEnd]))
{
iEnd += 1;
}
}
else
{
iEnd = iBegin;
while (iEnd < arrayCount.Length && Utility.IsDecimalDigit(arrayCount[iEnd]))
{
iEnd += 1;
}
}
specifiedArrayCount = 0;
if (iEnd > iBegin)
{
Utility.ParseUInt(arrayCount.Slice(iBegin, iEnd - iBegin), out specifiedArrayCount);
}
tokenizer.MoveNext();
if (tokenizer.Kind == TokenKind.Ident)
{
baseType = nameSpan;
nameSpan = tokenizer.Value;
}
goto TokensDone;
case TokenKind.Parentheses:
case TokenKind.String:
// Ignored.
break;
case TokenKind.Punctuation:
// Most punctuation ignored.
if (tokenizer.Value.SequenceEqual("*"))
{
foundPointer = true;
}
break;
default:
Debug.Fail("Unexpected token kind");
goto TokensDone;
}
}
TokensDone:
this.Name = nameSpan.IsEmpty ? "noname" : nameSpan.ToString();
this.Field = field;
this.Offset = offset;
this.Size = size;
this.Signed = signed;
this.SpecifiedArrayCount = specifiedArrayCount;
// PARSE: SpecifiedEncoding, SpecifiedFormat
if (foundPointer)
{
this.SpecifiedFormat = EventHeaderFieldFormat.HexInt;
this.SpecifiedEncoding = longSize64 ? EventHeaderFieldEncoding.Value64 : EventHeaderFieldEncoding.Value32;
}
else if (foundStruct)
{
this.SpecifiedFormat = EventHeaderFieldFormat.HexBytes; // SPECIAL
this.SpecifiedEncoding = EventHeaderFieldEncoding.Struct; // SPECIAL
}
else if (baseType.IsEmpty || baseType.SequenceEqual("int"))
{
this.SpecifiedFormat = foundUnsigned
? EventHeaderFieldFormat.UnsignedInt
: EventHeaderFieldFormat.SignedInt;
if (foundLongLong)
{
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value64;
}
else if (foundLong)
{
this.SpecifiedEncoding = longSize64 ? EventHeaderFieldEncoding.Value64 : EventHeaderFieldEncoding.Value32;
if (foundUnsigned)
{
this.SpecifiedFormat = EventHeaderFieldFormat.HexInt; // Use hex for unsigned long.
}
}
else if (foundShort)
{
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value16;
}
else
{
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value32; // "unsigned" or "signed" means "int".
if (baseType.IsEmpty && !foundUnsigned && !foundSigned)
{
// Unexpected.
Debug.WriteLine("No baseType found for \"{}\"",
this.Field);
}
}
}
else if (baseType.SequenceEqual("char"))
{
this.SpecifiedFormat = foundUnsigned
? EventHeaderFieldFormat.UnsignedInt
: foundSigned
? EventHeaderFieldFormat.SignedInt
: EventHeaderFieldFormat.String8; // SPECIAL
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value8;
}
else if (baseType.SequenceEqual("u8") || baseType.SequenceEqual("__u8") || baseType.SequenceEqual("uint8_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.UnsignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value8;
}
else if (baseType.SequenceEqual("s8") || baseType.SequenceEqual("__s8") || baseType.SequenceEqual("int8_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.SignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value8;
}
else if (baseType.SequenceEqual("u16") || baseType.SequenceEqual("__u16") || baseType.SequenceEqual("uint16_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.UnsignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value16;
}
else if (baseType.SequenceEqual("s16") || baseType.SequenceEqual("__s16") || baseType.SequenceEqual("int16_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.SignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value16;
}
else if (baseType.SequenceEqual("u32") || baseType.SequenceEqual("__u32") || baseType.SequenceEqual("uint32_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.UnsignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value32;
}
else if (baseType.SequenceEqual("s32") || baseType.SequenceEqual("__s32") || baseType.SequenceEqual("int32_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.SignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value32;
}
else if (baseType.SequenceEqual("u64") || baseType.SequenceEqual("__u64") || baseType.SequenceEqual("uint64_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.UnsignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value64;
}
else if (baseType.SequenceEqual("s64") || baseType.SequenceEqual("__s64") || baseType.SequenceEqual("int64_t"))
{
this.SpecifiedFormat = EventHeaderFieldFormat.SignedInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Value64;
}
else
{
this.SpecifiedFormat = EventHeaderFieldFormat.HexInt;
this.SpecifiedEncoding = EventHeaderFieldEncoding.Invalid; // SPECIAL
}
// PARSE: Array
if (this.Size == 0)
{
this.Array = PerfFieldArray.RestOfEvent;
}
else if (this.Size == 2 && foundRelLoc)
{
this.Array = PerfFieldArray.RelLoc2;
}
else if (this.Size == 2 && foundDataLoc)
{
this.Array = PerfFieldArray.DataLoc2;
}
else if (this.Size == 4 && foundRelLoc)
{
this.Array = PerfFieldArray.RelLoc4;
}
else if (this.Size == 4 && foundDataLoc)
{
this.Array = PerfFieldArray.DataLoc4;
}
else if (foundArray)
{
this.Array = PerfFieldArray.Fixed;
}
else
{
this.Array = PerfFieldArray.None;
}
// DEDUCE: DeducedFormat.
// Apply the "signed:" property if specified.
if (this.SpecifiedFormat == EventHeaderFieldFormat.UnsignedInt ||
this.SpecifiedFormat == EventHeaderFieldFormat.SignedInt)
{
// If valid, signed overrides baseType.
switch (signed)
{
default: this.DeducedFormat = this.SpecifiedFormat; break; // signed == null
case false: this.DeducedFormat = EventHeaderFieldFormat.UnsignedInt; break;
case true: this.DeducedFormat = EventHeaderFieldFormat.SignedInt; break;
}
}
else
{
this.DeducedFormat = this.SpecifiedFormat;
}
// DEDUCE: DeducedEncoding, DeducedArrayCount, ElementSizeShift.
if (this.SpecifiedFormat == EventHeaderFieldFormat.String8)
{
Debug.Assert(this.SpecifiedEncoding == EventHeaderFieldEncoding.Value8);
this.DeducedEncoding = this.Size == 1 ? EventHeaderFieldEncoding.Value8 : EventHeaderFieldEncoding.ZStringChar8;
this.DeducedArrayCount = 1;
this.ElementSizeShift = this.Size == 1 ? (byte)0 : byte.MaxValue;
}
else if (this.SpecifiedFormat == EventHeaderFieldFormat.HexBytes)
{
Debug.Assert(this.SpecifiedEncoding == EventHeaderFieldEncoding.Struct);
this.DeducedEncoding = this.Size == 1 ? EventHeaderFieldEncoding.Value8 : EventHeaderFieldEncoding.StringLength16Char8;
this.DeducedArrayCount = 1;
this.ElementSizeShift = byte.MaxValue;
}
else
{
switch (this.Array)
{
case PerfFieldArray.None:
// Size overrides element size deduced from type name.
switch (this.Size)
{
case 1:
this.DeducedEncoding = EventHeaderFieldEncoding.Value8;
this.ElementSizeShift = 0;
break;
case 2:
this.DeducedEncoding = EventHeaderFieldEncoding.Value16;
this.ElementSizeShift = 1;
break;
case 4:
this.DeducedEncoding = EventHeaderFieldEncoding.Value32;
this.ElementSizeShift = 2;
break;
case 8:
this.DeducedEncoding = EventHeaderFieldEncoding.Value64;
this.ElementSizeShift = 3;
break;
default:
goto DoHexDump;
}
this.DeducedArrayCount = 1;
break;
case PerfFieldArray.Fixed:
if (this.SpecifiedArrayCount == 0)
{
this.DeducedEncoding = this.SpecifiedEncoding | EventHeaderFieldEncoding.CArrayFlag;
switch (this.SpecifiedEncoding)
{
case EventHeaderFieldEncoding.Value8:
this.DeducedArrayCount = this.Size;
this.ElementSizeShift = 0;
break;
case EventHeaderFieldEncoding.Value16:
if (this.Size % sizeof(UInt16) != 0)
{
goto DoHexDump;
}
this.DeducedArrayCount = (ushort)(this.Size / sizeof(UInt16));
this.ElementSizeShift = 1;
break;
case EventHeaderFieldEncoding.Value32:
if (this.Size % sizeof(UInt32) != 0)
{
goto DoHexDump;
}
this.DeducedArrayCount = (ushort)(this.Size / sizeof(UInt32));
this.ElementSizeShift = 2;
break;
case EventHeaderFieldEncoding.Value64:
if (this.Size % sizeof(UInt64) != 0)
{
goto DoHexDump;
}
this.DeducedArrayCount = (ushort)(this.Size / sizeof(UInt64));
this.ElementSizeShift = 3;
break;
default:
Debug.Assert(this.SpecifiedEncoding == EventHeaderFieldEncoding.Invalid);
goto DoHexDump;
}
}
else
{
if (this.Size % this.SpecifiedArrayCount != 0)
{
goto DoHexDump;
}
switch (this.Size / this.SpecifiedArrayCount)
{
case 1:
this.DeducedEncoding = EventHeaderFieldEncoding.Value8 | EventHeaderFieldEncoding.CArrayFlag;
this.ElementSizeShift = 0;
break;
case 2:
this.DeducedEncoding = EventHeaderFieldEncoding.Value16 | EventHeaderFieldEncoding.CArrayFlag;
this.ElementSizeShift = 1;
break;
case 4:
this.DeducedEncoding = EventHeaderFieldEncoding.Value32 | EventHeaderFieldEncoding.CArrayFlag;
this.ElementSizeShift = 2;
break;
case 8:
this.DeducedEncoding = EventHeaderFieldEncoding.Value64 | EventHeaderFieldEncoding.CArrayFlag;
this.ElementSizeShift = 3;
break;
default:
goto DoHexDump;
}
this.DeducedArrayCount = this.SpecifiedArrayCount;
}
break;
default:
// Variable-length data.
switch (this.SpecifiedEncoding)
{
case EventHeaderFieldEncoding.Value8:
this.ElementSizeShift = 0;
break;
case EventHeaderFieldEncoding.Value16:
this.ElementSizeShift = 1;
break;
case EventHeaderFieldEncoding.Value32:
this.ElementSizeShift = 2;
break;
case EventHeaderFieldEncoding.Value64:
this.ElementSizeShift = 3;
break;
default:
Debug.Assert(this.SpecifiedEncoding == EventHeaderFieldEncoding.Invalid);
goto DoHexDump;
}
this.DeducedEncoding = this.SpecifiedEncoding | EventHeaderFieldEncoding.VArrayFlag;
this.DeducedArrayCount = 0;
break;
DoHexDump:
this.DeducedEncoding = EventHeaderFieldEncoding.StringLength16Char8;
this.DeducedFormat = EventHeaderFieldFormat.HexBytes;
this.DeducedArrayCount = 1;
this.ElementSizeShift = byte.MaxValue;
break;
}
}
#if DEBUG
Debug.Assert(this.Name.Length > 0);
Debug.Assert(this.Field.Length > 0);
var encodingValue = this.DeducedEncoding.WithoutFlags();
switch (encodingValue)
{
case EventHeaderFieldEncoding.Value8:
if (this.DeducedArrayCount != 0)
{
Debug.Assert(this.Size == this.DeducedArrayCount * sizeof(byte));
}
Debug.Assert(this.ElementSizeShift == 0);
break;
case EventHeaderFieldEncoding.Value16:
if (this.DeducedArrayCount != 0)
{
Debug.Assert(this.Size == this.DeducedArrayCount * sizeof(UInt16));
}
Debug.Assert(this.ElementSizeShift == 1);
break;
case EventHeaderFieldEncoding.Value32:
if (this.DeducedArrayCount != 0)
{
Debug.Assert(this.Size == this.DeducedArrayCount * sizeof(UInt32));
}
Debug.Assert(this.ElementSizeShift == 2);
break;
case EventHeaderFieldEncoding.Value64:
if (this.DeducedArrayCount != 0)
{
Debug.Assert(this.Size == this.DeducedArrayCount * sizeof(UInt64));
}
Debug.Assert(this.ElementSizeShift == 3);
break;
case EventHeaderFieldEncoding.StringLength16Char8:
Debug.Assert(this.DeducedArrayCount == 1);
Debug.Assert((this.DeducedEncoding & EventHeaderFieldEncoding.FlagMask) == 0);
Debug.Assert(this.DeducedFormat == EventHeaderFieldFormat.HexBytes);
Debug.Assert(this.ElementSizeShift == byte.MaxValue);
break;
case EventHeaderFieldEncoding.ZStringChar8:
Debug.Assert(this.DeducedArrayCount == 1);
Debug.Assert((this.DeducedEncoding & EventHeaderFieldEncoding.FlagMask) == 0);
Debug.Assert(this.DeducedFormat == EventHeaderFieldFormat.String8);
Debug.Assert(this.ElementSizeShift == byte.MaxValue);
break;
default:
Debug.Fail("Unexpected DeducedEncoding type");
break;
}
var encodingFlags = this.DeducedEncoding & EventHeaderFieldEncoding.FlagMask;
switch (encodingFlags)
{
case 0:
Debug.Assert(this.DeducedArrayCount == 1);
break;
case EventHeaderFieldEncoding.VArrayFlag:
Debug.Assert(this.DeducedArrayCount == 0);
break;
case EventHeaderFieldEncoding.CArrayFlag:
Debug.Assert(this.DeducedArrayCount >= 1);
break;
default:
Debug.Fail("Unexpected DeducedEncoding flags");
break;
}
switch (this.DeducedFormat)
{
case EventHeaderFieldFormat.UnsignedInt:
case EventHeaderFieldFormat.SignedInt:
case EventHeaderFieldFormat.HexInt:
Debug.Assert(encodingValue >= EventHeaderFieldEncoding.Value8);
Debug.Assert(encodingValue <= EventHeaderFieldEncoding.Value64);
break;
case EventHeaderFieldFormat.HexBytes:
Debug.Assert(encodingValue == EventHeaderFieldEncoding.StringLength16Char8);
break;
case EventHeaderFieldFormat.String8:
Debug.Assert(
encodingValue == EventHeaderFieldEncoding.Value8 ||
encodingValue == EventHeaderFieldEncoding.ZStringChar8);
break;
default:
Debug.Fail("Unexpected DeducedFormat type");
break;
}
#endif
return;
}
/// <summary>
/// Name of the field, or "noname" if unable to determine the name.
/// (Parsed from Field, e.g. if Field = "char my_field[8]" then Name = "my_field".)
/// </summary>
public string Name { get; }
/// <summary>
/// Field declaration in pseudo-C syntax, e.g. "char my_field[8]".
/// (Value of the format's "field:" property.)
/// </summary>
public string Field { get; }
/// <summary>
/// The byte offset of the start of the field data from the start of
/// the event raw data. (Value of the format's "offset:" property.)
/// </summary>
public ushort Offset { get; }
/// <summary>
/// The byte size of the field data. May be 0 to indicate "rest of event".
/// (Value of the format's "size:" property.)
/// </summary>
public ushort Size { get; }
/// <summary>
/// Whether the field is signed; null if unspecified.
/// (Value of the format's "signed:" property.)
/// </summary>
public bool? Signed { get; }
/// <summary>
/// The number of elements in this field, as specified in the field property,
/// or 0 if no array count was found.
/// (Parsed from Field, e.g. if Field = "char my_field[8]" then SpecifiedArrayCount = 8.)
/// </summary>
public ushort SpecifiedArrayCount { get; }
/// <summary>
/// The number of elements in this field, as deduced from field and size.
/// If the field is not being treated as an array (i.e. a single item, or
/// an array that is being treated as a string or a blob), this will be 1.
/// If the field is a variable-length array, this will be 0.
/// </summary>
public ushort DeducedArrayCount { get; }
/// <summary>
/// The encoding of the field's base type, as specified in the field property.
/// This may be Value8, Value16, Value32, Value64, Struct, or Invalid if no
/// recognized encoding was found.
/// (Parsed from Field, e.g. if Field = "char my_field[8]" then base type is
/// "char" so Encoding = "Value8".)
/// </summary>
public EventHeaderFieldEncoding SpecifiedEncoding { get; }
/// <summary>
/// The encoding of the field's base type, as deduced from field and size.
/// This will be Value8, Value16, Value32, Value64, ZStringChar8 for a
/// nul-terminated string, or StringLength16Char8 for a binary blob.
/// The VArrayFlag flag or the CArrayFlag flag may be set for Value8,
/// Value16, Value32, and Value64.
/// </summary>
public EventHeaderFieldEncoding DeducedEncoding { get; }
/// <summary>
/// The format of the field's base type, as specified by the field and signed properties.
/// This will be UnsignedInt, SignedInt, HexInt, String8, or HexBytes.
/// (Parsed from Field, e.g. if Field = "char my_field[8]" then base type is
/// "char" so Format = "String8".)
/// </summary>
public EventHeaderFieldFormat SpecifiedFormat { get; }
/// <summary>
/// The format of the field's base type, as deduced from field, size, and signed.
/// </summary>
public EventHeaderFieldFormat DeducedFormat { get; }
/// <summary>
/// The kind of array this field is, as specified in the field property.
/// (Parsed from Field and Size, e.g. if Field = "char my_field[8]" then Array = Fixed.)
/// </summary>
public PerfFieldArray Array { get; }
/// <summary>
/// For string or blob, this is byte.MaxValue.
/// For other types, ElementSizeShift is the log2 of the size of each element
/// in the field. If the field is a single N-bit integer or an array of N-bit
/// integers, ElementSizeShift is: 0 for 8-bit integers, 1 for 16-bit integers,
/// 2 for 32-bit integers, and 3 for 64-bit integers.
/// </summary>
public byte ElementSizeShift { get; }
/// <summary>
/// <para>
/// Parses a line of the "format:" section of an event's "format" file. The
/// formatLine string will generally look like
/// "[whitespace?]field:[declaration]; offset:[number]; size:[number]; ...".
///</para><para>
/// If "field:" is non-empty, "offset:" is a valid unsigned integer, and
/// "size:" is a valid unsigned integer, returns
/// PerfFieldFormat(field, offset, size, signed). Otherwise, returns null.
/// </para><para>
/// Note that You'll usually use PerfEventFormat.Parse to parse the entire format
/// file rather than calling this method directly.
///</para>
/// </summary>
public static PerfFieldFormat? Parse(
bool longSize64, // true if sizeof(long) == 8, false if sizeof(long) == 4.
ReadOnlySpan<char> formatLine)
{
ReadOnlySpan<char> field = default;
ushort offset = 0;
bool foundOffset = false;
ushort size = 0;
bool foundSize = false;
bool? isSigned = null;
var str = formatLine;
var i = 0;
// FIND: field, offset, size
// Search for " NAME: VALUE;"
while (i < str.Length)
{
// Skip spaces and semicolons.
while (Utility.IsSpaceOrTab(str[i]) || str[i] == ';')
{
i += 1;
if (i >= str.Length)
{
goto Done;
}
}
// "NAME:"
var iPropName = i;
while (str[i] != ':')
{
i += 1;
if (i >= str.Length)
{
Debug.WriteLine("EOL before ':' in format");
goto Done; // Unexpected.
}
}
var propName = str.Slice(iPropName, i - iPropName);
i += 1; // Skip ':'
// Skip spaces.
while (i < str.Length && Utility.IsSpaceOrTab(str[i]))
{
Debug.WriteLine("Space before propval in format");
i += 1; // Unexpected.
}
// "VALUE;"
var iPropValue = i;
while (i < str.Length && str[i] != ';')
{
i += 1;
}
var propValue = str.Slice(iPropValue, i - iPropValue);
if (propName.SequenceEqual("field") || propName.SequenceEqual("field special"))
{
field = propValue;
}
else if (propName.SequenceEqual("offset") && i < str.Length)
{
foundOffset = Utility.ParseUInt(propValue, out offset);
}
else if (propName.SequenceEqual("size") && i < str.Length)
{
foundSize = Utility.ParseUInt(propValue, out size);
}
else if (propName.SequenceEqual("signed") && i < str.Length)
{
ushort signedVal;
isSigned = Utility.ParseUInt(propValue, out signedVal)
? signedVal != 0
: (bool?)null;
}
}
Done:
PerfFieldFormat? result;
if (field.Length == 0 || !foundOffset || !foundSize)
{
result = null;
}
else
{
result = new PerfFieldFormat(longSize64, field.ToString(), offset, size, isSigned);
}
return result;
}
/// <summary>
/// <para>
/// Given the event's raw data (e.g. PerfSampleEventInfo::RawData), return
/// this field's raw data. Returns null for out of bounds.
///</para><para>
/// Does not do any byte-swapping. This method uses byteReader to resolve
/// data_loc and rel_loc references, not to fix up the field data.
///</para><para>
/// Note that in some cases, the size returned by GetFieldBytes may be
/// different from the value returned by Size():
/// <list type="bullet"><item>
/// If eventRawDataSize < Offset() + Size(), returns {}.
/// </item><item>
/// If Size() == 0, returns all data from offset to the end of the event,
/// i.e. it returns eventRawDataSize - Offset() bytes.
/// </item><item>
/// If Array() is Dynamic or RelDyn, the returned size depends on the
/// event contents.
/// </item></list>
/// </para>
/// </summary>
public ReadOnlySpan<byte> GetFieldBytes(
ReadOnlySpan<byte> eventRawData,
PerfByteReader byteReader)
{
if (this.Offset + this.Size <= eventRawData.Length)
{
int dynOffset, dynSize;
switch (this.Array)
{
case PerfFieldArray.None:
case PerfFieldArray.Fixed:
return eventRawData.Slice(this.Offset, this.Size);
case PerfFieldArray.RestOfEvent:
return eventRawData.Slice(this.Offset);
case PerfFieldArray.DataLoc2:
case PerfFieldArray.RelLoc2:
// 2-byte value is an offset leading to the real data, size is strlen.
dynOffset = byteReader.ReadU16(eventRawData.Slice(this.Offset));
if (this.Array == PerfFieldArray.RelLoc2)
{
// offset is relative to end of field.
dynOffset += this.Offset + this.Size;
}
if (dynOffset < eventRawData.Length)
{
dynSize = eventRawData.Slice(dynOffset).IndexOf((byte)0);
if (dynSize >= 0)
{
return eventRawData.Slice(dynOffset, dynSize);
}
}
break;
case PerfFieldArray.DataLoc4:
case PerfFieldArray.RelLoc4:
// 4-byte value is an offset/length pair leading to the real data.
var dyn32 = byteReader.ReadU32(eventRawData.Slice(this.Offset));
dynSize = (int)(dyn32 >> 16);
dynOffset = (int)(dyn32 & 0xFFFF);
if (this.Array == PerfFieldArray.RelLoc4)
{
// offset is relative to end of field.
dynOffset += this.Offset + this.Size;
}
if (dynOffset + dynSize <= eventRawData.Length)
{
return eventRawData.Slice(dynOffset, dynSize);
}
break;
}
}
return default;
}
/// <summary>
/// Gets the value of this field from the event's raw data.
/// </summary>
/// <param name="sampleEventInfo">
/// Event information, used for sampleEventInfo.RawDataSpan and sampleEventInfo.ByteReader.
/// </param>
/// <returns>
/// A PerfItemValue with the field value, or an empty PerfItemValue (result.Encoding == Invalid)
/// if the event's expected offset exceeds eventRawData.Length.
/// </returns>
public PerfItemValue GetFieldValue(in PerfSampleEventInfo sampleEventInfo)
{
return this.GetFieldValue(sampleEventInfo.RawDataSpan, sampleEventInfo.ByteReader);
}
/// <summary>
/// Gets the value of this field from the event's raw data.
/// </summary>
/// <param name="eventRawData">Event's "raw" section, e.g. sampleEventInfo.RawDataSpan.</param>
/// <param name="byteReader">Event's byte order, e.g. sampleEventInfo.ByteReader.</param>
/// <returns>
/// A PerfItemValue with the field value, or an empty PerfItemValue (result.Encoding == Invalid)
/// if the event's expected offset exceeds eventRawData.Length.
/// </returns>
public PerfItemValue GetFieldValue(
ReadOnlySpan<byte> eventRawData,
PerfByteReader byteReader)
{
bool checkStrLen = this.DeducedEncoding == EventHeaderFieldEncoding.ZStringChar8;
ReadOnlySpan<byte> bytes;
ushort arrayCount;
if (this.Offset + this.Size <= eventRawData.Length)
{
int dynOffset, dynSize;
switch (this.Array)
{
case PerfFieldArray.None:
case PerfFieldArray.Fixed:
bytes = eventRawData.Slice(this.Offset, this.Size);
if (checkStrLen)
{
bytes = UntilFirstNul(bytes);
}
arrayCount = this.DeducedArrayCount;
goto FixedSize;
case PerfFieldArray.RestOfEvent:
bytes = eventRawData.Slice(this.Offset);
goto VariableSize;
case PerfFieldArray.DataLoc2:
case PerfFieldArray.RelLoc2:
// 2-byte value is an offset leading to the real data, size is strlen.
dynOffset = byteReader.ReadU16(eventRawData.Slice(this.Offset));
if (this.Array == PerfFieldArray.RelLoc2)
{
// offset is relative to end of field.
dynOffset += this.Offset + this.Size;
}
if (dynOffset < eventRawData.Length)
{
bytes = eventRawData.Slice(dynOffset);
checkStrLen = true;
goto VariableSize;
}
break;
case PerfFieldArray.DataLoc4:
case PerfFieldArray.RelLoc4:
// 4-byte value is an offset/length pair leading to the real data.
var dyn32 = byteReader.ReadU32(eventRawData.Slice(this.Offset));
dynSize = (int)(dyn32 >> 16);
dynOffset = (int)(dyn32 & 0xFFFF);
if (this.Array == PerfFieldArray.RelLoc4)
{
// offset is relative to end of field.
dynOffset += this.Offset + this.Size;
}
if (dynOffset + dynSize <= eventRawData.Length)
{
bytes = eventRawData.Slice(dynOffset, dynSize);
goto VariableSize;
}