summaryrefslogtreecommitdiffstats
path: root/dynamic-layers/openembedded-layer/recipes-devtools/bpftrace/bpftrace/0001-Detect-new-BTF-api-btf_dump__new-btf_dump__new_v0_6_.patch
blob: 76517643f2f875606cf1946ed64557d052bbd340 (plain)
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
From c5092eee7dc5f3d28a1de2c33bda6611e9ed9d34 Mon Sep 17 00:00:00 2001
From: Jiri Olsa <jolsa@kernel.org>
Date: Tue, 22 Feb 2022 16:36:44 +0100
Subject: [PATCH] Detect new BTF api btf_dump__new/btf_dump__new_v0_6_0

Some of the libbpf functions we use  got deprecated and
replaced with new versions.

   btf__get_nr_types to btf__type_cnt
   btf_dump__new changed arguments

Adding detection of this and making bpftrace to compile
against latest libbpf.

Upstream-Status: Backport [https://github.com/iovisor/bpftrace/commit/3d451feeddf725d11bb52dfbc49b616724d24fd0]

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 CMakeLists.txt         |  8 +++++++
 cmake/FindLibBpf.cmake | 25 ++++++++++++++++++++
 src/btf.cpp            | 52 +++++++++++++++++++++++++++---------------
 3 files changed, 66 insertions(+), 19 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c5959732..8a9b0082 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -274,6 +274,14 @@ if (LIBBPF_BTF_DUMP_FOUND)
   endif()
 endif(LIBBPF_BTF_DUMP_FOUND)
 
+if (HAVE_LIBBPF_BTF_TYPE_CNT)
+	set(BPFTRACE_FLAGS "${BPFTRACE_FLAGS}" HAVE_LIBBPF_BTF_TYPE_CNT)
+endif(HAVE_LIBBPF_BTF_TYPE_CNT)
+
+if (HAVE_LIBBPF_BTF_DUMP_NEW_V0_6_0)
+	set(BPFTRACE_FLAGS "${BPFTRACE_FLAGS}" HAVE_LIBBPF_BTF_DUMP_NEW_V0_6_0)
+endif(HAVE_LIBBPF_BTF_DUMP_NEW_V0_6_0)
+
 if (LIBDW_FOUND)
   set(BPFTRACE_FLAGS "${BPFTRACE_FLAGS}" HAVE_LIBDW)
 endif ()
diff --git a/cmake/FindLibBpf.cmake b/cmake/FindLibBpf.cmake
index 86eb8050..b088415f 100644
--- a/cmake/FindLibBpf.cmake
+++ b/cmake/FindLibBpf.cmake
@@ -55,4 +55,29 @@ if (LIBBPF_FOUND)
   check_symbol_exists(bpf_link_create "${LIBBPF_INCLUDE_DIRS}/bpf/bpf.h" HAVE_LIBBPF_LINK_CREATE)
   SET(CMAKE_REQUIRED_DEFINITIONS)
   SET(CMAKE_REQUIRED_LIBRARIES)
+
+  INCLUDE(CheckCXXSourceCompiles)
+  SET(CMAKE_REQUIRED_INCLUDES ${LIBBPF_INCLUDE_DIRS})
+  SET(CMAKE_REQUIRED_LIBRARIES ${LIBBPF_LIBRARIES} elf z)
+  CHECK_CXX_SOURCE_COMPILES("
+#include <bpf/btf.h>
+
+int main(void) {
+  btf__type_cnt(NULL);
+  return 0;
+}
+" HAVE_LIBBPF_BTF_TYPE_CNT)
+
+  CHECK_CXX_SOURCE_COMPILES("
+#include <bpf/btf.h>
+
+int main(void) {
+  const struct btf_dump_opts *opts = (const struct btf_dump_opts*) 1;
+
+  btf_dump__new(NULL, NULL, NULL, opts);
+  return 0;
+}
+" HAVE_LIBBPF_BTF_DUMP_NEW_V0_6_0)
+  SET(CMAKE_REQUIRED_INCLUDES)
+  SET(CMAKE_REQUIRED_LIBRARIES)
 endif()
diff --git a/src/btf.cpp b/src/btf.cpp
index 7d83cf68..c08ef17b 100644
--- a/src/btf.cpp
+++ b/src/btf.cpp
@@ -28,6 +28,15 @@
 
 namespace bpftrace {
 
+static __u32 type_cnt(const struct btf *btf)
+{
+#ifdef HAVE_LIBBPF_BTF_TYPE_CNT
+  return btf__type_cnt(btf);
+#else
+  return btf__get_nr_types(btf);
+#endif
+}
+
 static unsigned char *get_data(const char *file, ssize_t *sizep)
 {
   struct stat st;
@@ -185,6 +194,21 @@ static void dump_printf(void *ctx, const char *fmt, va_list args)
   free(str);
 }
 
+static struct btf_dump *dump_new(const struct btf *btf,
+                                 btf_dump_printf_fn_t dump_printf,
+                                 void *ctx)
+{
+#ifdef HAVE_LIBBPF_BTF_DUMP_NEW_V0_6_0
+  return btf_dump__new(btf, dump_printf, ctx, nullptr);
+#else
+  struct btf_dump_opts opts = {
+    .ctx = ctx,
+  };
+
+  return btf_dump__new(btf, nullptr, &opts, dump_printf);
+#endif
+}
+
 static const char *btf_str(const struct btf *btf, __u32 off)
 {
   if (!off)
@@ -220,12 +244,11 @@ std::string BTF::c_def(const std::unordered_set<std::string> &set) const
     return std::string("");
 
   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);
+  dump = dump_new(btf, dump_printf, &ret);
   err = libbpf_get_error(dump);
   if (err)
   {
@@ -235,7 +258,7 @@ std::string BTF::c_def(const std::unordered_set<std::string> &set) const
   }
 
   std::unordered_set<std::string> myset(set);
-  __s32 id, max = (__s32) btf__get_nr_types(btf);
+  __s32 id, max = (__s32)type_cnt(btf);
 
   for (id = 1; id <= max && myset.size(); id++)
   {
@@ -415,7 +438,7 @@ int BTF::resolve_args(const std::string &func,
   if (!has_data())
     throw std::runtime_error("BTF data not available");
 
-  __s32 id, max = (__s32)btf__get_nr_types(btf);
+  __s32 id, max = (__s32)type_cnt(btf);
   std::string name = func;
 
   for (id = 1; id <= max; id++)
@@ -486,17 +509,14 @@ int BTF::resolve_args(const std::string &func,
 
 std::unique_ptr<std::istream> BTF::get_all_funcs() const
 {
-  __s32 id, max = (__s32)btf__get_nr_types(btf);
+  __s32 id, max = (__s32)type_cnt(btf);
   std::string type = std::string("");
-  struct btf_dump_opts opts = {
-    .ctx = &type,
-  };
   struct btf_dump *dump;
   std::string funcs;
   char err_buf[256];
   int err;
 
-  dump = btf_dump__new(btf, nullptr, &opts, dump_printf);
+  dump = dump_new(btf, dump_printf, &type);
   err = libbpf_get_error(dump);
   if (err)
   {
@@ -545,16 +565,13 @@ std::map<std::string, std::vector<std::string>> BTF::get_params(
     const std::set<std::string> &funcs) const
 {
 #ifdef HAVE_LIBBPF_BTF_DUMP_EMIT_TYPE_DECL
-  __s32 id, max = (__s32)btf__get_nr_types(btf);
+  __s32 id, max = (__s32)type_cnt(btf);
   std::string type = std::string("");
-  struct btf_dump_opts opts = {
-    .ctx = &type,
-  };
   struct btf_dump *dump;
   char err_buf[256];
   int err;
 
-  dump = btf_dump__new(btf, nullptr, &opts, dump_printf);
+  dump = dump_new(btf, dump_printf, &type);
   err = libbpf_get_error(dump);
   if (err)
   {
@@ -639,16 +656,13 @@ std::map<std::string, std::vector<std::string>> BTF::get_params(
 std::set<std::string> BTF::get_all_structs() const
 {
   std::set<std::string> struct_set;
-  __s32 id, max = (__s32)btf__get_nr_types(btf);
+  __s32 id, max = (__s32)type_cnt(btf);
   std::string types = std::string("");
-  struct btf_dump_opts opts = {
-    .ctx = &types,
-  };
   struct btf_dump *dump;
   char err_buf[256];
   int err;
 
-  dump = btf_dump__new(btf, nullptr, &opts, dump_printf);
+  dump = dump_new(btf, dump_printf, &types);
   err = libbpf_get_error(dump);
   if (err)
   {
-- 
2.36.0