1 // SPDX-License-Identifier: GPL-2.0 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 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, 0, 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_int_bits(__u32 int_type, __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 nr_bits = BTF_INT_BITS(int_type); 82 int total_bits_offset; 83 int bytes_to_copy; 84 int bits_to_copy; 85 __u64 print_num; 86 87 total_bits_offset = bit_offset + BTF_INT_OFFSET(int_type); 88 data += BITS_ROUNDDOWN_BYTES(total_bits_offset); 89 bit_offset = BITS_PER_BYTE_MASKED(total_bits_offset); 90 bits_to_copy = bit_offset + nr_bits; 91 bytes_to_copy = BITS_ROUNDUP_BYTES(bits_to_copy); 92 93 print_num = 0; 94 memcpy(&print_num, data, bytes_to_copy); 95 #if defined(__BIG_ENDIAN_BITFIELD) 96 left_shift_bits = bit_offset; 97 #elif defined(__LITTLE_ENDIAN_BITFIELD) 98 left_shift_bits = 64 - bits_to_copy; 99 #else 100 #error neither big nor little endian 101 #endif 102 right_shift_bits = 64 - nr_bits; 103 104 print_num <<= left_shift_bits; 105 print_num >>= right_shift_bits; 106 if (is_plain_text) 107 jsonw_printf(jw, "0x%llx", print_num); 108 else 109 jsonw_printf(jw, "%llu", print_num); 110 } 111 112 static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset, 113 const void *data, json_writer_t *jw, 114 bool is_plain_text) 115 { 116 __u32 *int_type; 117 __u32 nr_bits; 118 119 int_type = (__u32 *)(t + 1); 120 nr_bits = BTF_INT_BITS(*int_type); 121 /* if this is bit field */ 122 if (bit_offset || BTF_INT_OFFSET(*int_type) || 123 BITS_PER_BYTE_MASKED(nr_bits)) { 124 btf_dumper_int_bits(*int_type, bit_offset, data, jw, 125 is_plain_text); 126 return 0; 127 } 128 129 switch (BTF_INT_ENCODING(*int_type)) { 130 case 0: 131 if (BTF_INT_BITS(*int_type) == 64) 132 jsonw_printf(jw, "%lu", *(__u64 *)data); 133 else if (BTF_INT_BITS(*int_type) == 32) 134 jsonw_printf(jw, "%u", *(__u32 *)data); 135 else if (BTF_INT_BITS(*int_type) == 16) 136 jsonw_printf(jw, "%hu", *(__u16 *)data); 137 else if (BTF_INT_BITS(*int_type) == 8) 138 jsonw_printf(jw, "%hhu", *(__u8 *)data); 139 else 140 btf_dumper_int_bits(*int_type, bit_offset, data, jw, 141 is_plain_text); 142 break; 143 case BTF_INT_SIGNED: 144 if (BTF_INT_BITS(*int_type) == 64) 145 jsonw_printf(jw, "%ld", *(long long *)data); 146 else if (BTF_INT_BITS(*int_type) == 32) 147 jsonw_printf(jw, "%d", *(int *)data); 148 else if (BTF_INT_BITS(*int_type) == 16) 149 jsonw_printf(jw, "%hd", *(short *)data); 150 else if (BTF_INT_BITS(*int_type) == 8) 151 jsonw_printf(jw, "%hhd", *(char *)data); 152 else 153 btf_dumper_int_bits(*int_type, bit_offset, data, jw, 154 is_plain_text); 155 break; 156 case BTF_INT_CHAR: 157 if (isprint(*(char *)data)) 158 jsonw_printf(jw, "\"%c\"", *(char *)data); 159 else 160 if (is_plain_text) 161 jsonw_printf(jw, "0x%hhx", *(char *)data); 162 else 163 jsonw_printf(jw, "\"\\u00%02hhx\"", 164 *(char *)data); 165 break; 166 case BTF_INT_BOOL: 167 jsonw_bool(jw, *(int *)data); 168 break; 169 default: 170 /* shouldn't happen */ 171 return -EINVAL; 172 } 173 174 return 0; 175 } 176 177 static int btf_dumper_struct(const struct btf_dumper *d, __u32 type_id, 178 const void *data) 179 { 180 const struct btf_type *t; 181 struct btf_member *m; 182 const void *data_off; 183 int ret = 0; 184 int i, vlen; 185 186 t = btf__type_by_id(d->btf, type_id); 187 if (!t) 188 return -EINVAL; 189 190 vlen = BTF_INFO_VLEN(t->info); 191 jsonw_start_object(d->jw); 192 m = (struct btf_member *)(t + 1); 193 194 for (i = 0; i < vlen; i++) { 195 data_off = data + BITS_ROUNDDOWN_BYTES(m[i].offset); 196 jsonw_name(d->jw, btf__name_by_offset(d->btf, m[i].name_off)); 197 ret = btf_dumper_do_type(d, m[i].type, 198 BITS_PER_BYTE_MASKED(m[i].offset), 199 data_off); 200 if (ret) 201 break; 202 } 203 204 jsonw_end_object(d->jw); 205 206 return ret; 207 } 208 209 static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id, 210 __u8 bit_offset, const void *data) 211 { 212 const struct btf_type *t = btf__type_by_id(d->btf, type_id); 213 214 switch (BTF_INFO_KIND(t->info)) { 215 case BTF_KIND_INT: 216 return btf_dumper_int(t, bit_offset, data, d->jw, 217 d->is_plain_text); 218 case BTF_KIND_STRUCT: 219 case BTF_KIND_UNION: 220 return btf_dumper_struct(d, type_id, data); 221 case BTF_KIND_ARRAY: 222 return btf_dumper_array(d, type_id, data); 223 case BTF_KIND_ENUM: 224 btf_dumper_enum(data, d->jw); 225 return 0; 226 case BTF_KIND_PTR: 227 btf_dumper_ptr(data, d->jw, d->is_plain_text); 228 return 0; 229 case BTF_KIND_UNKN: 230 jsonw_printf(d->jw, "(unknown)"); 231 return 0; 232 case BTF_KIND_FWD: 233 /* map key or value can't be forward */ 234 jsonw_printf(d->jw, "(fwd-kind-invalid)"); 235 return -EINVAL; 236 case BTF_KIND_TYPEDEF: 237 case BTF_KIND_VOLATILE: 238 case BTF_KIND_CONST: 239 case BTF_KIND_RESTRICT: 240 return btf_dumper_modifier(d, type_id, data); 241 default: 242 jsonw_printf(d->jw, "(unsupported-kind"); 243 return -EINVAL; 244 } 245 } 246 247 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id, 248 const void *data) 249 { 250 return btf_dumper_do_type(d, type_id, 0, data); 251 } 252 253 #define BTF_PRINT_ARG(...) \ 254 do { \ 255 pos += snprintf(func_sig + pos, size - pos, \ 256 __VA_ARGS__); \ 257 if (pos >= size) \ 258 return -1; \ 259 } while (0) 260 #define BTF_PRINT_TYPE(type) \ 261 do { \ 262 pos = __btf_dumper_type_only(btf, type, func_sig, \ 263 pos, size); \ 264 if (pos == -1) \ 265 return -1; \ 266 } while (0) 267 268 static int btf_dump_func(const struct btf *btf, char *func_sig, 269 const struct btf_type *func_proto, 270 const struct btf_type *func, int pos, int size); 271 272 static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id, 273 char *func_sig, int pos, int size) 274 { 275 const struct btf_type *proto_type; 276 const struct btf_array *array; 277 const struct btf_type *t; 278 279 if (!type_id) { 280 BTF_PRINT_ARG("void "); 281 return pos; 282 } 283 284 t = btf__type_by_id(btf, type_id); 285 286 switch (BTF_INFO_KIND(t->info)) { 287 case BTF_KIND_INT: 288 BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off)); 289 break; 290 case BTF_KIND_STRUCT: 291 BTF_PRINT_ARG("struct %s ", 292 btf__name_by_offset(btf, t->name_off)); 293 break; 294 case BTF_KIND_UNION: 295 BTF_PRINT_ARG("union %s ", 296 btf__name_by_offset(btf, t->name_off)); 297 break; 298 case BTF_KIND_ENUM: 299 BTF_PRINT_ARG("enum %s ", 300 btf__name_by_offset(btf, t->name_off)); 301 break; 302 case BTF_KIND_ARRAY: 303 array = (struct btf_array *)(t + 1); 304 BTF_PRINT_TYPE(array->type); 305 BTF_PRINT_ARG("[%d]", array->nelems); 306 break; 307 case BTF_KIND_PTR: 308 BTF_PRINT_TYPE(t->type); 309 BTF_PRINT_ARG("* "); 310 break; 311 case BTF_KIND_UNKN: 312 case BTF_KIND_FWD: 313 case BTF_KIND_TYPEDEF: 314 return -1; 315 case BTF_KIND_VOLATILE: 316 BTF_PRINT_ARG("volatile "); 317 BTF_PRINT_TYPE(t->type); 318 break; 319 case BTF_KIND_CONST: 320 BTF_PRINT_ARG("const "); 321 BTF_PRINT_TYPE(t->type); 322 break; 323 case BTF_KIND_RESTRICT: 324 BTF_PRINT_ARG("restrict "); 325 BTF_PRINT_TYPE(t->type); 326 break; 327 case BTF_KIND_FUNC_PROTO: 328 pos = btf_dump_func(btf, func_sig, t, NULL, pos, size); 329 if (pos == -1) 330 return -1; 331 break; 332 case BTF_KIND_FUNC: 333 proto_type = btf__type_by_id(btf, t->type); 334 pos = btf_dump_func(btf, func_sig, proto_type, t, pos, size); 335 if (pos == -1) 336 return -1; 337 break; 338 default: 339 return -1; 340 } 341 342 return pos; 343 } 344 345 static int btf_dump_func(const struct btf *btf, char *func_sig, 346 const struct btf_type *func_proto, 347 const struct btf_type *func, int pos, int size) 348 { 349 int i, vlen; 350 351 BTF_PRINT_TYPE(func_proto->type); 352 if (func) 353 BTF_PRINT_ARG("%s(", btf__name_by_offset(btf, func->name_off)); 354 else 355 BTF_PRINT_ARG("("); 356 vlen = BTF_INFO_VLEN(func_proto->info); 357 for (i = 0; i < vlen; i++) { 358 struct btf_param *arg = &((struct btf_param *)(func_proto + 1))[i]; 359 360 if (i) 361 BTF_PRINT_ARG(", "); 362 if (arg->type) { 363 BTF_PRINT_TYPE(arg->type); 364 BTF_PRINT_ARG("%s", 365 btf__name_by_offset(btf, arg->name_off)); 366 } else { 367 BTF_PRINT_ARG("..."); 368 } 369 } 370 BTF_PRINT_ARG(")"); 371 372 return pos; 373 } 374 375 void btf_dumper_type_only(const struct btf *btf, __u32 type_id, char *func_sig, 376 int size) 377 { 378 int err; 379 380 func_sig[0] = '\0'; 381 if (!btf) 382 return; 383 384 err = __btf_dumper_type_only(btf, type_id, func_sig, 0, size); 385 if (err < 0) 386 func_sig[0] = '\0'; 387 } 388