Repository navigation
Expand file tree
/
Copy pathbtf.cpp
More file actions
321 lines (249 loc) · 6.65 KB
/
Copy pathbtf.cpp
File metadata and controls
321 lines (249 loc) · 6.65 KB
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
#include "btf.h"
#include "bpftrace.h"
#include "types.h"
#include "utils.h"
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <linux/limits.h>
#include <regex>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#ifdef HAVE_LIBBPF_BTF_DUMP
#include <linux/bpf.h>
#include <linux/btf.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#include <bpf/btf.h>
#pragma GCC diagnostic pop
#include <bpf/libbpf.h>
namespace bpftrace {
static unsigned char *get_data(const char *file, ssize_t *sizep)
{
struct stat st;
if (stat(file, &st))
return nullptr;
FILE *f;
f = fopen(file, "rb");
if (!f)
return nullptr;
unsigned char *data;
unsigned int size;
size = st.st_size;
data = (unsigned char *) malloc(size);
if (!data)
{
fclose(f);
return nullptr;
}
ssize_t ret = fread(data, 1, st.st_size, f);
if (ret != st.st_size)
{
free(data);
fclose(f);
return nullptr;
}
fclose(f);
*sizep = size;
return data;
}
static struct btf* btf_raw(char *file)
{
unsigned char *data;
ssize_t size;
struct btf *btf;
data = get_data(file, &size);
if (!data)
{
std::cerr << "BTF: failed to read data from: " << file << std::endl;
return nullptr;
}
btf = btf__new(data, (__u32) size);
free(data);
return btf;
}
static int libbpf_print(enum libbpf_print_level level, const char *msg, va_list ap)
{
fprintf(stderr, "BTF: (%d) ", level);
return vfprintf(stderr, msg, ap);
}
static struct btf *btf_open(const struct vmlinux_location *locs)
{
struct utsname buf;
uname(&buf);
for (int i = 0; locs[i].path; i++)
{
char path[PATH_MAX + 1];
snprintf(path, PATH_MAX, locs[i].path, buf.release);
if (access(path, R_OK))
continue;
struct btf *btf;
if (locs[i].raw)
btf = btf_raw(path);
else
btf = btf__parse_elf(path, nullptr);
int err = libbpf_get_error(btf);
if (err)
{
if (bt_verbose)
{
char err_buf[256];
libbpf_strerror(libbpf_get_error(btf), err_buf, sizeof(err_buf));
std::cerr << "BTF: failed to read data (" << err_buf << ") from: " << path << std::endl;
}
continue;
}
if (bt_verbose)
{
std::cerr << "BTF: using data from " << path << std::endl;
}
return btf;
}
return nullptr;
}
BTF::BTF(void) : btf(nullptr), state(NODATA)
{
struct vmlinux_location locs_env[] = {
{ nullptr, true },
{ nullptr, false },
};
const struct vmlinux_location *locs = vmlinux_locs;
// Try to get BTF file from BPFTRACE_BTF env
char *path = std::getenv("BPFTRACE_BTF");
if (path)
{
locs_env[0].path = path;
locs = locs_env;
}
btf = btf_open(locs);
if (btf)
{
libbpf_set_print(libbpf_print);
state = OK;
}
else if (bt_debug != DebugLevel::kNone)
{
std::cerr << "BTF: failed to find BTF data " << std::endl;
}
}
BTF::~BTF()
{
btf__free(btf);
}
static void dump_printf(void *ctx, const char *fmt, va_list args)
{
std::string *ret = static_cast<std::string*>(ctx);
char *str;
if (vasprintf(&str, fmt, args) < 0)
return;
*ret += str;
free(str);
}
static const char *btf_str(const struct btf *btf, __u32 off)
{
if (!off)
return "(anon)";
return btf__name_by_offset(btf, off) ? : "(invalid)";
}
static std::string full_type_str(const struct btf *btf, const struct btf_type *type)
{
const char *str = btf_str(btf, type->name_off);
if (BTF_INFO_KIND(type->info) == BTF_KIND_STRUCT)
return std::string("struct ") + str;
if (BTF_INFO_KIND(type->info) == BTF_KIND_UNION)
return std::string("union ") + str;
return str;
}
static std::string btf_type_str(const std::string& type)
{
return std::regex_replace(type, std::regex("^(struct )|(union )"), "");
}
std::string BTF::c_def(std::unordered_set<std::string>& set)
{
std::string ret = std::string("");
struct btf_dump_opts opts = { .ctx = &ret, };
struct btf_dump *dump;
char err_buf[256];
int err;
dump = btf_dump__new(btf, nullptr, &opts, dump_printf);
err = libbpf_get_error(dump);
if (err)
{
libbpf_strerror(err, err_buf, sizeof(err_buf));
std::cerr << "BTF: failed to initialize dump (" << err_buf << ")" << std::endl;
return std::string("");
}
std::unordered_set<std::string> myset(set);
__s32 id, max = (__s32) btf__get_nr_types(btf);
for (id = 1; id <= max && myset.size(); id++)
{
const struct btf_type *t = btf__type_by_id(btf, id);
std::string str = full_type_str(btf, t);
auto it = myset.find(str);
if (it != myset.end())
{
btf_dump__dump_type(dump, id);
myset.erase(it);
}
}
btf_dump__free(dump);
return ret;
}
std::string BTF::type_of(const std::string& name, const std::string& field)
{
__s32 type_id = btf__find_by_name(btf, btf_type_str(name).c_str());
if (type_id < 0)
return std::string("");
const struct btf_type *type = btf__type_by_id(btf, type_id);
return type_of(type, field);
}
std::string BTF::type_of(const btf_type *type, const std::string &field)
{
if (!type ||
(BTF_INFO_KIND(type->info) != BTF_KIND_STRUCT &&
BTF_INFO_KIND(type->info) != BTF_KIND_UNION))
return std::string("");
// We need to walk through oaa the struct/union members
// and try to find the requested field name.
//
// More info on struct/union members:
// https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-union
const struct btf_member *m = reinterpret_cast<const struct btf_member*>(type + 1);
for (unsigned int i = 0; i < BTF_INFO_VLEN(type->info); i++)
{
std::string m_name = btf__name_by_offset(btf, m[i].name_off);
// anonymous struct/union
if (m_name == "")
{
const struct btf_type *type = btf__type_by_id(btf, m[i].type);
std::string type_name = type_of(type, field);
if (!type_name.empty())
return type_name;
}
if (m_name != field)
continue;
const struct btf_type *f = btf__type_by_id(btf, m[i].type);
if (!f)
break;
// Get rid of all the pointers on the way to the actual type.
while (BTF_INFO_KIND(f->info) == BTF_KIND_PTR) {
f = btf__type_by_id(btf, f->type);
}
return full_type_str(btf, f);
}
return std::string("");
}
} // namespace bpftrace
#else // HAVE_LIBBPF_BTF_DUMP
namespace bpftrace {
BTF::BTF() { }
BTF::~BTF() { }
std::string BTF::c_def(std::unordered_set<std::string>& set __attribute__((__unused__))) { return std::string(""); }
std::string BTF::type_of(const std::string& name __attribute__((__unused__)),
const std::string& field __attribute__((__unused__))) {
return std::string("");
}
} // namespace bpftrace
#endif // HAVE_LIBBPF_BTF_DUMP