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
|
package generate
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
type Field struct {
Type string
Name string
Offset int
Size int
Signed bool
}
type Format struct {
Name string
ID int
InternalFields []Field
ExternalFields []Field
}
// FieldNumber returns the 0-based index of a named field in ExternalFields,
// minus 1 (to skip __syscall_nr), matching the Raku behavior where args[0]
// is the first field after __syscall_nr.
func (f *Format) FieldNumber(name string) int {
for i, field := range f.ExternalFields {
if field.Name == name {
return i - 1
}
}
return 0 - 1
}
// ParseFormats parses concatenated sysfs tracepoint format files from r.
// Each section has: name, ID, format fields, print fmt.
func ParseFormats(r io.Reader) ([]Format, error) {
scanner := bufio.NewScanner(r)
var formats []Format
var current *Format
isExternal := false
for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)
switch {
case strings.HasPrefix(trimmed, "name:"):
f := Format{}
f.Name = strings.TrimSpace(strings.TrimPrefix(trimmed, "name:"))
formats = append(formats, f)
current = &formats[len(formats)-1]
isExternal = false
case strings.HasPrefix(trimmed, "ID:"):
if current == nil {
return nil, fmt.Errorf("ID without name")
}
id, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(trimmed, "ID:")))
if err != nil {
return nil, fmt.Errorf("parsing ID: %w", err)
}
current.ID = id
case strings.HasPrefix(trimmed, "field:"):
if current == nil {
return nil, fmt.Errorf("field without name")
}
field, err := parseField(trimmed)
if err != nil {
return nil, fmt.Errorf("parsing field in %s: %w", current.Name, err)
}
if field.Name == "__syscall_nr" {
isExternal = true
}
if isExternal {
current.ExternalFields = append(current.ExternalFields, field)
} else {
current.InternalFields = append(current.InternalFields, field)
}
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("scanning input: %w", err)
}
return formats, nil
}
func parseField(line string) (Field, error) {
// Format: "field:TYPE NAME; offset:N; size:N; signed:N;"
line = strings.TrimPrefix(line, "field:")
parts := strings.Split(line, ";")
if len(parts) < 4 {
return Field{}, fmt.Errorf("not enough field parts: %q", line)
}
decl := strings.TrimSpace(parts[0])
fieldType, fieldName := splitDeclaration(decl)
offset, err := parseFieldInt(parts[1], "offset:")
if err != nil {
return Field{}, err
}
size, err := parseFieldInt(parts[2], "size:")
if err != nil {
return Field{}, err
}
signedVal, err := parseFieldInt(parts[3], "signed:")
if err != nil {
return Field{}, err
}
return Field{
Type: fieldType,
Name: fieldName,
Offset: offset,
Size: size,
Signed: signedVal != 0,
}, nil
}
// splitDeclaration splits "const char * filename" into ("const char *", "filename").
func splitDeclaration(decl string) (string, string) {
tokens := strings.Fields(decl)
if len(tokens) == 0 {
return "", ""
}
if len(tokens) == 1 {
return "", tokens[0]
}
name := tokens[len(tokens)-1]
typePart := strings.Join(tokens[:len(tokens)-1], " ")
return typePart, name
}
func parseFieldInt(s, prefix string) (int, error) {
s = strings.TrimSpace(s)
s = strings.TrimPrefix(s, prefix)
s = strings.TrimSpace(s)
return strconv.Atoi(s)
}
|