trace_btf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/btf.h>
  3. #include <linux/kernel.h>
  4. #include <linux/slab.h>
  5. #include "trace_btf.h"
  6. /*
  7. * Find a function proto type by name, and return the btf_type with its btf
  8. * in *@btf_p. Return NULL if not found.
  9. * Note that caller has to call btf_put(*@btf_p) after using the btf_type.
  10. */
  11. const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p)
  12. {
  13. const struct btf_type *t;
  14. s32 id;
  15. id = bpf_find_btf_id(func_name, BTF_KIND_FUNC, btf_p);
  16. if (id < 0)
  17. return NULL;
  18. /* Get BTF_KIND_FUNC type */
  19. t = btf_type_by_id(*btf_p, id);
  20. if (!t || !btf_type_is_func(t))
  21. goto err;
  22. /* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */
  23. t = btf_type_by_id(*btf_p, t->type);
  24. if (!t || !btf_type_is_func_proto(t))
  25. goto err;
  26. return t;
  27. err:
  28. btf_put(*btf_p);
  29. return NULL;
  30. }
  31. /*
  32. * Get function parameter with the number of parameters.
  33. * This can return NULL if the function has no parameters.
  34. * It can return -EINVAL if the @func_proto is not a function proto type.
  35. */
  36. const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr)
  37. {
  38. if (!btf_type_is_func_proto(func_proto))
  39. return ERR_PTR(-EINVAL);
  40. *nr = btf_type_vlen(func_proto);
  41. if (*nr > 0)
  42. return (const struct btf_param *)(func_proto + 1);
  43. else
  44. return NULL;
  45. }
  46. #define BTF_ANON_STACK_MAX 16
  47. struct btf_anon_stack {
  48. u32 tid;
  49. u32 offset;
  50. };
  51. /*
  52. * Find a member of data structure/union by name and return it.
  53. * Return NULL if not found, or -EINVAL if parameter is invalid.
  54. * If the member is an member of anonymous union/structure, the offset
  55. * of that anonymous union/structure is stored into @anon_offset. Caller
  56. * can calculate the correct offset from the root data structure by
  57. * adding anon_offset to the member's offset.
  58. */
  59. const struct btf_member *btf_find_struct_member(struct btf *btf,
  60. const struct btf_type *type,
  61. const char *member_name,
  62. u32 *anon_offset)
  63. {
  64. struct btf_anon_stack *anon_stack;
  65. const struct btf_member *member;
  66. u32 tid, cur_offset = 0;
  67. const char *name;
  68. int i, top = 0;
  69. anon_stack = kcalloc(BTF_ANON_STACK_MAX, sizeof(*anon_stack), GFP_KERNEL);
  70. if (!anon_stack)
  71. return ERR_PTR(-ENOMEM);
  72. retry:
  73. if (!btf_type_is_struct(type)) {
  74. member = ERR_PTR(-EINVAL);
  75. goto out;
  76. }
  77. for_each_member(i, type, member) {
  78. if (!member->name_off) {
  79. /* Anonymous union/struct: push it for later use */
  80. if (btf_type_skip_modifiers(btf, member->type, &tid) &&
  81. top < BTF_ANON_STACK_MAX) {
  82. anon_stack[top].tid = tid;
  83. anon_stack[top++].offset =
  84. cur_offset + member->offset;
  85. }
  86. } else {
  87. name = btf_name_by_offset(btf, member->name_off);
  88. if (name && !strcmp(member_name, name)) {
  89. if (anon_offset)
  90. *anon_offset = cur_offset;
  91. goto out;
  92. }
  93. }
  94. }
  95. if (top > 0) {
  96. /* Pop from the anonymous stack and retry */
  97. tid = anon_stack[--top].tid;
  98. cur_offset = anon_stack[top].offset;
  99. type = btf_type_by_id(btf, tid);
  100. goto retry;
  101. }
  102. member = NULL;
  103. out:
  104. kfree(anon_stack);
  105. return member;
  106. }