-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathstructmembers.go
257 lines (240 loc) · 7.06 KB
/
structmembers.go
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
package goexec
import (
"bytes"
"debug/dwarf"
"debug/elf"
_ "embed"
"fmt"
"log/slog"
"strings"
"github.com/grafana/go-offsets-tracker/pkg/offsets"
)
func log() *slog.Logger {
return slog.With("component", "goexec.structMemberOffsets")
}
//go:embed offsets.json
var prefetchedOffsets string
type structInfo struct {
// lib is the name of the library where the struct is defined.
// "go" for the standar library or e.g. "google.golang.org/grpc"
lib string
// fields of the struct as key, and the name of the constant defined in the eBPF code as value
fields map[string]string
}
// level-1 key = Struct type name and its containing library
// level-2 key = name of the field
// level-3 value = C constant name to override (e.g. path_ptr_pos)
var structMembers = map[string]structInfo{
"net/http.Request": {
lib: "go",
fields: map[string]string{
"URL": "url_ptr_pos",
"Method": "method_ptr_pos",
"RemoteAddr": "remoteaddr_ptr_pos",
"Host": "host_ptr_pos",
"ContentLength": "content_length_ptr_pos",
"Header": "req_header_ptr_pos",
},
},
"net/url.URL": {
lib: "go",
fields: map[string]string{
"Path": "path_ptr_pos",
},
},
"net/http.response": {
lib: "go",
fields: map[string]string{
"status": "status_ptr_pos",
"req": "resp_req_pos",
},
},
"net/http.Response": {
lib: "go",
fields: map[string]string{
"StatusCode": "status_code_ptr_pos",
},
},
"google.golang.org/grpc/internal/transport.Stream": {
lib: "google.golang.org/grpc",
fields: map[string]string{
"st": "grpc_stream_st_ptr_pos",
"method": "grpc_stream_method_ptr_pos",
"ctx": "grpc_stream_ctx_ptr_pos",
},
},
"google.golang.org/grpc/internal/status.Status": {
lib: "google.golang.org/grpc",
fields: map[string]string{
"s": "grpc_status_s_pos",
},
},
"google.golang.org/genproto/googleapis/rpc/status.Status": {
lib: "google.golang.org/genproto",
fields: map[string]string{
"Code": "grpc_status_code_ptr_pos",
},
},
"google.golang.org/grpc/internal/transport.http2Server": {
lib: "google.golang.org/grpc",
fields: map[string]string{
"remoteAddr": "grpc_st_remoteaddr_ptr_pos",
"localAddr": "grpc_st_localaddr_ptr_pos",
},
},
"net.TCPAddr": {
lib: "go",
fields: map[string]string{
"IP": "tcp_addr_ip_ptr_pos",
"Port": "tcp_addr_port_ptr_pos",
},
},
"google.golang.org/grpc.ClientConn": {
lib: "google.golang.org/grpc",
fields: map[string]string{
"target": "grpc_client_target_ptr_pos",
},
},
"context.valueCtx": {
lib: "go",
fields: map[string]string{
"val": "value_context_val_ptr_pos",
},
},
}
func structMemberOffsets(elfFile *elf.File) (FieldOffsets, error) {
// first, try to read offsets from DWARF debug info
var offs FieldOffsets
var expected map[string]struct{}
dwarfData, err := elfFile.DWARF()
if err == nil {
offs, expected = structMemberOffsetsFromDwarf(dwarfData)
if len(expected) > 0 {
log().Debug("Fields not found in the DWARF file", "fields", expected)
} else {
return offs, nil
}
} else {
// initialize empty offsets
offs = FieldOffsets{}
}
log().Debug("Can't read all offsets from DWARF info. Checking in prefetched database")
// if it is not possible, query from prefetched offsets
return structMemberPreFetchedOffsets(elfFile, offs)
}
func structMemberPreFetchedOffsets(elfFile *elf.File, fieldOffsets FieldOffsets) (FieldOffsets, error) {
log := log().With("function", "structMemberPreFetchedOffsets")
offs, err := offsets.Read(bytes.NewBufferString(prefetchedOffsets))
if err != nil {
return nil, fmt.Errorf("reading offsets file contents: %w", err)
}
libVersions, err := findLibraryVersions(elfFile)
if err != nil {
return nil, fmt.Errorf("searching for library versions: %w", err)
}
// after putting the offsets.json in a Go structure, we search all the
// structMembers elements on it, to get the annotated offsets
for strName, strInfo := range structMembers {
version, ok := libVersions[strInfo.lib]
if !ok {
log.Debug("can't find version for library. Assuming 0.0.0", "lib", strInfo.lib)
// unversioned libraries are accounted as "0.0.0" in offsets.json file
// https://github.com/grafana/go-offsets-tracker/blob/main/pkg/writer/writer.go#L108-L110
version = "0.0.0"
}
dash := strings.Index(version, "-")
if dash > 0 {
version = version[:dash]
}
for fieldName, constantName := range strInfo.fields {
// look the version of the required field in the offsets.json memory copy
offset, ok := offs.Find(strName, fieldName, version)
if !ok {
log.Debug("can't find offsets for field",
"lib", strInfo.lib, "name", strName, "field", fieldName, "version", version)
continue
}
log.Debug("found offset", "constantName", constantName, "offset", offset)
fieldOffsets[constantName] = offset
}
}
return fieldOffsets, nil
}
// structMemberOffsetsFromDwarf reads the executable dwarf information to get
// the offsets specified in the structMembers map
func structMemberOffsetsFromDwarf(data *dwarf.Data) (FieldOffsets, map[string]struct{}) {
log := log().With("function", "structMemberOffsetsFromDwarf")
expectedReturns := map[string]struct{}{}
for _, str := range structMembers {
for _, ctName := range str.fields {
expectedReturns[ctName] = struct{}{}
}
}
log.Debug("searching offests for field constants", "constants", expectedReturns)
fieldOffsets := FieldOffsets{}
reader := data.Reader()
for {
entry, err := reader.Next()
if err != nil {
log.Debug("error reading DRWARF info", "data", err)
return fieldOffsets, expectedReturns
}
if entry == nil { // END of dwarf data
return fieldOffsets, expectedReturns
}
if entry.Tag != dwarf.TagStructType {
continue
}
attrs := getAttrs(entry)
typeName, ok := attrs[dwarf.AttrName]
if !ok {
reader.SkipChildren()
continue
}
structMember, ok := structMembers[typeName.(string)]
if !ok {
reader.SkipChildren()
continue
}
log.Debug("inspecting fields for struct type", "type", typeName)
if err := readMembers(reader, structMember.fields, expectedReturns, fieldOffsets); err != nil {
log.Debug("error reading DRWARF info", "type", typeName, "members", err)
return nil, expectedReturns
}
}
}
func readMembers(
reader *dwarf.Reader,
fields map[string]string,
expectedReturns map[string]struct{},
offsets FieldOffsets,
) error {
for {
entry, err := reader.Next()
if err != nil {
return fmt.Errorf("can't read DWARF data: %w", err)
}
if entry == nil { // END of dwarf data
return nil
}
// Nil tag: end of the members list
if entry.Tag == 0 {
return nil
}
attrs := getAttrs(entry)
if constName, ok := fields[attrs[dwarf.AttrName].(string)]; ok {
delete(expectedReturns, constName)
value := attrs[dwarf.AttrDataMemberLoc]
log().Debug("found struct member offset",
"const", constName, "offset", attrs[dwarf.AttrDataMemberLoc])
offsets[constName] = uint64(value.(int64))
}
}
}
func getAttrs(entry *dwarf.Entry) map[dwarf.Attr]any {
attrs := map[dwarf.Attr]any{}
for f := range entry.Field {
attrs[entry.Field[f].Attr] = entry.Field[f].Val
}
return attrs
}