diff options
Diffstat (limited to 'tools/lib/bpf/btf.c')
-rw-r--r-- | tools/lib/bpf/btf.c | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 03161be094b4..1622a309f169 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c | |||
@@ -2,7 +2,6 @@ | |||
2 | /* Copyright (c) 2018 Facebook */ | 2 | /* Copyright (c) 2018 Facebook */ |
3 | 3 | ||
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <stdint.h> | ||
6 | #include <string.h> | 5 | #include <string.h> |
7 | #include <unistd.h> | 6 | #include <unistd.h> |
8 | #include <errno.h> | 7 | #include <errno.h> |
@@ -32,17 +31,25 @@ struct btf { | |||
32 | struct btf_type **types; | 31 | struct btf_type **types; |
33 | const char *strings; | 32 | const char *strings; |
34 | void *nohdr_data; | 33 | void *nohdr_data; |
35 | uint32_t nr_types; | 34 | __u32 nr_types; |
36 | uint32_t types_size; | 35 | __u32 types_size; |
37 | uint32_t data_size; | 36 | __u32 data_size; |
38 | int fd; | 37 | int fd; |
39 | }; | 38 | }; |
40 | 39 | ||
40 | static const char *btf_name_by_offset(const struct btf *btf, __u32 offset) | ||
41 | { | ||
42 | if (offset < btf->hdr->str_len) | ||
43 | return &btf->strings[offset]; | ||
44 | else | ||
45 | return NULL; | ||
46 | } | ||
47 | |||
41 | static int btf_add_type(struct btf *btf, struct btf_type *t) | 48 | static int btf_add_type(struct btf *btf, struct btf_type *t) |
42 | { | 49 | { |
43 | if (btf->types_size - btf->nr_types < 2) { | 50 | if (btf->types_size - btf->nr_types < 2) { |
44 | struct btf_type **new_types; | 51 | struct btf_type **new_types; |
45 | u32 expand_by, new_size; | 52 | __u32 expand_by, new_size; |
46 | 53 | ||
47 | if (btf->types_size == BTF_MAX_NR_TYPES) | 54 | if (btf->types_size == BTF_MAX_NR_TYPES) |
48 | return -E2BIG; | 55 | return -E2BIG; |
@@ -69,7 +76,7 @@ static int btf_add_type(struct btf *btf, struct btf_type *t) | |||
69 | static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log) | 76 | static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log) |
70 | { | 77 | { |
71 | const struct btf_header *hdr = btf->hdr; | 78 | const struct btf_header *hdr = btf->hdr; |
72 | u32 meta_left; | 79 | __u32 meta_left; |
73 | 80 | ||
74 | if (btf->data_size < sizeof(struct btf_header)) { | 81 | if (btf->data_size < sizeof(struct btf_header)) { |
75 | elog("BTF header not found\n"); | 82 | elog("BTF header not found\n"); |
@@ -148,7 +155,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log) | |||
148 | 155 | ||
149 | while (next_type < end_type) { | 156 | while (next_type < end_type) { |
150 | struct btf_type *t = next_type; | 157 | struct btf_type *t = next_type; |
151 | uint16_t vlen = BTF_INFO_VLEN(t->info); | 158 | __u16 vlen = BTF_INFO_VLEN(t->info); |
152 | int err; | 159 | int err; |
153 | 160 | ||
154 | next_type += sizeof(*t); | 161 | next_type += sizeof(*t); |
@@ -187,6 +194,14 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log) | |||
187 | return 0; | 194 | return 0; |
188 | } | 195 | } |
189 | 196 | ||
197 | const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id) | ||
198 | { | ||
199 | if (type_id > btf->nr_types) | ||
200 | return NULL; | ||
201 | |||
202 | return btf->types[type_id]; | ||
203 | } | ||
204 | |||
190 | static bool btf_type_is_void(const struct btf_type *t) | 205 | static bool btf_type_is_void(const struct btf_type *t) |
191 | { | 206 | { |
192 | return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD; | 207 | return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD; |
@@ -197,7 +212,7 @@ static bool btf_type_is_void_or_null(const struct btf_type *t) | |||
197 | return !t || btf_type_is_void(t); | 212 | return !t || btf_type_is_void(t); |
198 | } | 213 | } |
199 | 214 | ||
200 | static int64_t btf_type_size(const struct btf_type *t) | 215 | static __s64 btf_type_size(const struct btf_type *t) |
201 | { | 216 | { |
202 | switch (BTF_INFO_KIND(t->info)) { | 217 | switch (BTF_INFO_KIND(t->info)) { |
203 | case BTF_KIND_INT: | 218 | case BTF_KIND_INT: |
@@ -214,12 +229,12 @@ static int64_t btf_type_size(const struct btf_type *t) | |||
214 | 229 | ||
215 | #define MAX_RESOLVE_DEPTH 32 | 230 | #define MAX_RESOLVE_DEPTH 32 |
216 | 231 | ||
217 | int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id) | 232 | __s64 btf__resolve_size(const struct btf *btf, __u32 type_id) |
218 | { | 233 | { |
219 | const struct btf_array *array; | 234 | const struct btf_array *array; |
220 | const struct btf_type *t; | 235 | const struct btf_type *t; |
221 | uint32_t nelems = 1; | 236 | __u32 nelems = 1; |
222 | int64_t size = -1; | 237 | __s64 size = -1; |
223 | int i; | 238 | int i; |
224 | 239 | ||
225 | t = btf__type_by_id(btf, type_id); | 240 | t = btf__type_by_id(btf, type_id); |
@@ -279,9 +294,9 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id) | |||
279 | return type_id; | 294 | return type_id; |
280 | } | 295 | } |
281 | 296 | ||
282 | int32_t btf__find_by_name(const struct btf *btf, const char *type_name) | 297 | __s32 btf__find_by_name(const struct btf *btf, const char *type_name) |
283 | { | 298 | { |
284 | uint32_t i; | 299 | __u32 i; |
285 | 300 | ||
286 | if (!strcmp(type_name, "void")) | 301 | if (!strcmp(type_name, "void")) |
287 | return 0; | 302 | return 0; |
@@ -310,10 +325,9 @@ void btf__free(struct btf *btf) | |||
310 | free(btf); | 325 | free(btf); |
311 | } | 326 | } |
312 | 327 | ||
313 | struct btf *btf__new(uint8_t *data, uint32_t size, | 328 | struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log) |
314 | btf_print_fn_t err_log) | ||
315 | { | 329 | { |
316 | uint32_t log_buf_size = 0; | 330 | __u32 log_buf_size = 0; |
317 | char *log_buf = NULL; | 331 | char *log_buf = NULL; |
318 | struct btf *btf; | 332 | struct btf *btf; |
319 | int err; | 333 | int err; |