1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (c) 2018 Facebook */ 3 4 #include <ctype.h> 5 #include <stdio.h> /* for (FILE *) used by json_writer */ 6 #include <string.h> 7 #include <asm/byteorder.h> 8 #include <linux/bitops.h> 9 #include <linux/btf.h> 10 #include <linux/err.h> 11 12 #include "btf.h" 13 #include "json_writer.h" 14 #include "main.h" 15 16 #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1) 17 #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK) 18 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) 19 #define BITS_ROUNDUP_BYTES(bits) \ 20 (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) 21 22 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id, 23 __u8 bit_offset, const void *data); 24 25 static void btf_dumper_ptr(const void *data, json_writer_t *jw, 26 bool is_plain_text) 27 { 28 if (is_plain_text) 29 jsonw_printf(jw, "%p", *(unsigned long *)data); 30 else 31 jsonw_printf(jw, "%u", *(unsigned long *)data); 32 } 33 34 static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id, 35 __u8 bit_offset, const void *data) 36 { 37 int actual_type_id; 38 39 actual_type_id = btf__resolve_type(d->btf, type_id); 40 if (actual_type_id < 0) 41 return actual_type_id; 42 43 return btf_dumper_do_type(d, actual_type_id, bit_offset, data); 44 } 45 46 static void btf_dumper_enum(const void *data, json_writer_t *jw) 47 { 48 jsonw_printf(jw, "%d", *(int *)data); 49 } 50 51 static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id, 52 const void *data) 53 { 54 const struct btf_type *t = btf__type_by_id(d->btf, type_id); 55 struct btf_array *arr = (struct btf_array *)(t + 1); 56 long long elem_size; 57 int ret = 0; 58 __u32 i; 59 60 elem_size = btf__resolve_size(d->btf, arr->type); 61 if (elem_size < 0) 62 return elem_size; 63 64 jsonw_start_array(d->jw); 65 for (i = 0; i < arr->nelems; i++) { 66 ret = btf_dumper_do_type(d, arr->type, 0, 67 data + i * elem_size); 68 if (ret) 69 break; 70 } 71 72 jsonw_end_array(d->jw); 73 return ret; 74 } 75 76 static void btf_dumper_bitfield(__u32 nr_bits, __u8 bit_offset, 77 const void *data, json_writer_t *jw, 78 bool is_plain_text) 79 { 80 int left_shift_bits, right_shift_bits; 81 int bytes_to_copy; 82 int bits_to_copy; 83 __u64 print_num; 84 85 data += BITS_ROUNDDOWN_BYTES(bit_offset); 86 bit_offset = BITS_PER_BYTE_MASKED(bit_offset); 87 bits_to_copy = bit_offset + nr_bits; 88 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy); 89 90 print_num = 0; 91 memcpy(&print_num, data, bytes_to_copy); 92 #if defined(__BIG_ENDIAN_BITFIELD) 93 left_shift_bits = bit_offset; 94 #elif defined(__LITTLE_ENDIAN_BITFIELD) 95 left_shift_bits = 64 - bits_to_copy; 96 #else 97 #error neither big nor little endian 98 #endif 99 right_shift_bits = 64 - nr_bits; 100 101 print_num <<= left_shift_bits; 102 print_num >>= right_shift_bits; 103 if (is_plain_text) 104 jsonw_printf(jw, "0x%llx", print_num); 105 else 106 jsonw_printf(jw, "%llu", print_num); 107 } 108 109 110 static void btf_dumper_int_bits(__u32 int_type, __u8 bit_offset, 111 const void *data, json_writer_t *jw, 112 bool is_plain_text) 113 { 114 int nr_bits = BTF_INT_BITS(int_type); 115 int total_bits_offset; 116 117 /* bits_offset is at most 7. 118 * BTF_INT_OFFSET() cannot exceed 64 bits. 119 */ 120 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type); 121 btf_dumper_bitfield(nr_bits, total_bits_offset, data, jw, 122 is_plain_text); 123 } 124 125 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset, 126 const void *data, json_writer_t *jw, 127 bool is_plain_text) 128 { 129 __u32 *int_type; 130 __u32 nr_bits; 131 132 int_type = (__u32 *)(t + 1); 133 nr_bits = BTF_INT_BITS(*int_type); 134 /* if this is bit field */ 135 if (bit_offset || BTF_INT_OFFSET(*int_type) || 136 BITS_PER_BYTE_MASKED(nr_bits)) { 137 btf_dumper_int_bits(*int_type, bit_offset, data, jw, 138 is_plain_text); 139 return 0; 140 } 141 142 switch (BTF_INT_ENCODING(*int_type)) { 143 case 0: 144 if (BTF_INT_BITS(*int_type) == 64) 145 jsonw_printf(jw, "%lu", *(__u64 *)data); 146 else if (BTF_INT_BITS(*int_type) == 32) 147 jsonw_printf(jw, "%u", *(__u32 *)data); 148 else if (BTF_INT_BITS(*int_type) == 16) 149 jsonw_printf(jw, "%hu", *(__u16 *)data); 150 else if (BTF_INT_BITS(*int_type) == 8) 151 jsonw_printf(jw, "%hhu", *(__u8 *)data); 152 else 153 btf_dumper_int_bits(*int_type, bit_offset, data, jw, 154 is_plain_text); 155 break; 156 case BTF_INT_SIGNED: 157 if (BTF_INT_BITS(*int_type) == 64) 158 jsonw_printf(jw, "%ld", *(long long *)data); 159 else if (BTF_INT_BITS(*int_type) == 32) 160 jsonw_printf(jw, "%d", *(int *)data); 161 else if (BTF_INT_BITS(*int_type) == 16) 162 jsonw_printf(jw, "%hd", *(short *)data); 163 else if (BTF_INT_BITS(*int_type) == 8) 164 jsonw_printf(jw, "%hhd", *(char *)data); 165 else 166 btf_dumper_int_bits(*int_type, bit_offset, data, jw, 167 is_plain_text); 168 break; 169 case BTF_INT_CHAR: 170 if (isprint(*(char *)data)) 171 jsonw_printf(jw, "\"%c\"", *(char *)data); 172 else 173 if (is_plain_text) 174 jsonw_printf(jw, "0x%hhx", *(char *)data); 175 else 176 jsonw_printf(jw, "\"\\u00%02hhx\"", 177 *(char *)data); 178 break; 179 case BTF_INT_BOOL: 180 jsonw_bool(jw, *(int *)data); 181 break; 182 default: 183 /* shouldn't happen */ 184 return -EINVAL; 185 } 186 187 return 0; 188 } 189 190 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id, 191 const void *data) 192 { 193 const struct btf_type *t; 194 struct btf_member *m; 195 const void *data_off; 196 int kind_flag; 197 int ret = 0; 198 int i, vlen; 199 200 t = btf__type_by_id(d->btf, type_id); 201 if (!t) 202 return -EINVAL; 203 204 kind_flag = BTF_INFO_KFLAG(t->info); 205 vlen = BTF_INFO_VLEN(t->info); 206 jsonw_start_object(d->jw); 207 m = (struct btf_member *)(t + 1); 208 209 for (i = 0; i < vlen; i++) { 210 __u32 bit_offset = m[i].offset; 211 __u32 bitfield_size = 0; 212 213 if (kind_flag) { 214 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(bit_offset); 215 bit_offset = BTF_MEMBER_BIT_OFFSET(bit_offset); 216 } 217 218 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off)); 219 if (bitfield_size) { 220 btf_dumper_bitfield(bitfield_size, bit_offset, 221 data, d->jw, d->is_plain_text); 222 } else { 223 data_off = data + BITS_ROUNDDOWN_BYTES(bit_offset); 224 ret = btf_dumper_do_type(d, m[i].type, 225 BITS_PER_BYTE_MASKED(bit_offset), 226 data_off); 227 if (ret) 228 break; 229 } 230 } 231 232 jsonw_end_object(d->jw); 233 234 return ret; 235 } 236 237 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id, 238 __u8 bit_offset, const void *data) 239 { 240 const struct btf_type *t = btf__type_by_id(d->btf, type_id); 241 242 switch (BTF_INFO_KIND(t->info)) { 243 case BTF_KIND_INT: 244 return btf_dumper_int(t, bit_offset, data, d->jw, 245 d->is_plain_text); 246 case BTF_KIND_STRUCT: 247 case BTF_KIND_UNION: 248 return btf_dumper_struct(d, type_id, data); 249 case BTF_KIND_ARRAY: 250 return btf_dumper_array(d, type_id, data); 251 case BTF_KIND_ENUM: 252 btf_dumper_enum(data, d->jw); 253 return 0; 254 case BTF_KIND_PTR: 255 btf_dumper_ptr(data, d->jw, d->is_plain_text); 256 return 0; 257 case BTF_KIND_UNKN: 258 jsonw_printf(d->jw, "(unknown)"); 259 return 0; 260 case BTF_KIND_FWD: 261 /* map key or value can't be forward */ 262 jsonw_printf(d->jw, "(fwd-kind-invalid)"); 263 return -EINVAL; 264 case BTF_KIND_TYPEDEF: 265 case BTF_KIND_VOLATILE: 266 case BTF_KIND_CONST: 267 case BTF_KIND_RESTRICT: 268 return btf_dumper_modifier(d, type_id, bit_offset, data); 269 default: 270 jsonw_printf(d->jw, "(unsupported-kind"); 271 return -EINVAL; 272 } 273 } 274 275 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id, 276 const void *data) 277 { 278 return btf_dumper_do_type(d, type_id, 0, data); 279 } 280 281 #define BTF_PRINT_ARG(...) \ 282 do { \ 283 pos += snprintf(func_sig + pos, size - pos, \ 284 __VA_ARGS__); \ 285 if (pos >= size) \ 286 return -1; \ 287 } while (0) 288 #define BTF_PRINT_TYPE(type) \ 289 do { \ 290 pos = __btf_dumper_type_only(btf, type, func_sig, \ 291 pos, size); \ 292 if (pos == -1) \ 293 return -1; \ 294 } while (0) 295 296 static int btf_dump_func(const struct btf *btf, char *func_sig, 297 const struct btf_type *func_proto, 298 const struct btf_type *func, int pos, int size); 299 300 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id, 301 char *func_sig, int pos, int size) 302 { 303 const struct btf_type *proto_type; 304 const struct btf_array *array; 305 const struct btf_type *t; 306 307 if (!type_id) { 308 BTF_PRINT_ARG("void "); 309 return pos; 310 } 311 312 t = btf__type_by_id(btf, type_id); 313 314 switch (BTF_INFO_KIND(t->info)) { 315 case BTF_KIND_INT: 316 case BTF_KIND_TYPEDEF: 317 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off)); 318 break; 319 case BTF_KIND_STRUCT: 320 BTF_PRINT_ARG("struct %s ", 321 btf__name_by_offset(btf, t->name_off)); 322 break; 323 case BTF_KIND_UNION: 324 BTF_PRINT_ARG("union %s ", 325 btf__name_by_offset(btf, t->name_off)); 326 break; 327 case BTF_KIND_ENUM: 328 BTF_PRINT_ARG("enum %s ", 329 btf__name_by_offset(btf, t->name_off)); 330 break; 331 case BTF_KIND_ARRAY: 332 array = (struct btf_array *)(t + 1); 333 BTF_PRINT_TYPE(array->type); 334 BTF_PRINT_ARG("[%d]", array->nelems); 335 break; 336 case BTF_KIND_PTR: 337 BTF_PRINT_TYPE(t->type); 338 BTF_PRINT_ARG("* "); 339 break; 340 case BTF_KIND_FWD: 341 BTF_PRINT_ARG("%s %s ", 342 BTF_INFO_KFLAG(t->info) ? "union" : "struct", 343 btf__name_by_offset(btf, t->name_off)); 344 break; 345 case BTF_KIND_VOLATILE: 346 BTF_PRINT_ARG("volatile "); 347 BTF_PRINT_TYPE(t->type); 348 break; 349 case BTF_KIND_CONST: 350 BTF_PRINT_ARG("const "); 351 BTF_PRINT_TYPE(t->type); 352 break; 353 case BTF_KIND_RESTRICT: 354 BTF_PRINT_ARG("restrict "); 355 BTF_PRINT_TYPE(t->type); 356 break; 357 case BTF_KIND_FUNC_PROTO: 358 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size); 359 if (pos == -1) 360 return -1; 361 break; 362 case BTF_KIND_FUNC: 363 proto_type = btf__type_by_id(btf, t->type); 364 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size); 365 if (pos == -1) 366 return -1; 367 break; 368 case BTF_KIND_UNKN: 369 default: 370 return -1; 371 } 372 373 return pos; 374 } 375 376 static int btf_dump_func(const struct btf *btf, char *func_sig, 377 const struct btf_type *func_proto, 378 const struct btf_type *func, int pos, int size) 379 { 380 int i, vlen; 381 382 BTF_PRINT_TYPE(func_proto->type); 383 if (func) 384 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off)); 385 else 386 BTF_PRINT_ARG("("); 387 vlen = BTF_INFO_VLEN(func_proto->info); 388 for (i = 0; i < vlen; i++) { 389 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i]; 390 391 if (i) 392 BTF_PRINT_ARG(", "); 393 if (arg->type) { 394 BTF_PRINT_TYPE(arg->type); 395 BTF_PRINT_ARG("%s", 396 btf__name_by_offset(btf, arg->name_off)); 397 } else { 398 BTF_PRINT_ARG("..."); 399 } 400 } 401 BTF_PRINT_ARG(")"); 402 403 return pos; 404 } 405 406 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig, 407 int size) 408 { 409 int err; 410 411 func_sig[0] = '\0'; 412 if (!btf) 413 return; 414 415 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size); 416 if (err < 0) 417 func_sig[0] = '\0'; 418 } 419 420 static const char *ltrim(const char *s) 421 { 422 while (isspace(*s)) 423 s++; 424 425 return s; 426 } 427 428 void btf_dump_linfo_plain(const struct btf *btf, 429 const struct bpf_line_info *linfo, 430 const char *prefix, bool linum) 431 { 432 const char *line = btf__name_by_offset(btf, linfo->line_off); 433 434 if (!line) 435 return; 436 line = ltrim(line); 437 438 if (!prefix) 439 prefix = ""; 440 441 if (linum) { 442 const char *file = btf__name_by_offset(btf, linfo->file_name_off); 443 444 /* More forgiving on file because linum option is 445 * expected to provide more info than the already 446 * available src line. 447 */ 448 if (!file) 449 file = ""; 450 451 printf("%s%s [file:%s line_num:%u line_col:%u]\n", 452 prefix, line, file, 453 BPF_LINE_INFO_LINE_NUM(linfo->line_col), 454 BPF_LINE_INFO_LINE_COL(linfo->line_col)); 455 } else { 456 printf("%s%s\n", prefix, line); 457 } 458 } 459 460 void btf_dump_linfo_json(const struct btf *btf, 461 const struct bpf_line_info *linfo, bool linum) 462 { 463 const char *line = btf__name_by_offset(btf, linfo->line_off); 464 465 if (line) 466 jsonw_string_field(json_wtr, "src", ltrim(line)); 467 468 if (linum) { 469 const char *file = btf__name_by_offset(btf, linfo->file_name_off); 470 471 if (file) 472 jsonw_string_field(json_wtr, "file", file); 473 474 if (BPF_LINE_INFO_LINE_NUM(linfo->line_col)) 475 jsonw_int_field(json_wtr, "line_num", 476 BPF_LINE_INFO_LINE_NUM(linfo->line_col)); 477 478 if (BPF_LINE_INFO_LINE_COL(linfo->line_col)) 479 jsonw_int_field(json_wtr, "line_col", 480 BPF_LINE_INFO_LINE_COL(linfo->line_col)); 481 } 482 } 483