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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
#include "ast/passes/probe_expansion.h"
#include "ast/attachpoint_parser.h"
#include "ast/passes/printer.h"
#include "btf_common.h"
#include "driver.h"
#include "mocks.h"
#include "gtest/gtest.h"
namespace bpftrace::test::probe_expansion {
static void test(const std::string &prog,
const std::vector<std::string> &expected_aps,
const std::vector<std::set<std::string>> &expected_funcs,
std::string_view expected_ast,
bool features)
{
auto mock_bpftrace = get_mock_bpftrace();
mock_bpftrace->feature_ = std::make_unique<MockBPFfeature>(features);
BPFtrace &bpftrace = *mock_bpftrace;
ast::ASTContext ast("stdin", prog);
ast::PassManager pm;
pm.put(ast)
.put(bpftrace)
.add(CreateParsePass())
.add(ast::CreateParseAttachpointsPass())
.add(ast::CreateProbeExpansionPass());
auto result = pm.run();
ASSERT_TRUE(result && ast.diagnostics().ok());
if (!expected_aps.empty()) {
auto &probe = ast.root->probes.at(0);
ASSERT_EQ(probe->attach_points.size(), expected_aps.size());
for (size_t i = 0; i < expected_aps.size(); i++) {
ASSERT_EQ(probe->attach_points.at(i)->name(), expected_aps.at(i));
}
}
if (!expected_funcs.empty()) {
auto &probe = ast.root->probes.at(0);
auto &expansions = result->get<ast::ExpansionResult>();
ASSERT_EQ(probe->attach_points.size(), expected_funcs.size());
for (size_t i = 0; i < expected_funcs.size(); i++) {
auto *ap = probe->attach_points.at(i);
ASSERT_EQ(expansions.get_expanded_funcs(*ap), expected_funcs.at(i));
}
}
if (!expected_ast.empty()) {
if (expected_ast[0] == '\n')
expected_ast.remove_prefix(1);
std::stringstream out;
ast::Printer printer(out);
printer.visit(ast.root);
EXPECT_EQ(out.str(), expected_ast);
}
}
static void test_attach_points(const std::string &input,
const std::vector<std::string> &expected_aps,
bool features = true)
{
test(input, expected_aps, {}, "", features);
}
static void test_multi_attach_points(
const std::string &input,
const std::vector<std::string> &expected_aps,
const std::vector<std::set<std::string>> &expected_funcs)
{
test(input, expected_aps, expected_funcs, "", true);
}
static void test_ast(const std::string &input, std::string_view expected_ast)
{
test(input, {}, {}, expected_ast, true);
}
TEST(probe_expansion, session_ast)
{
test_ast("kprobe:sys_* { @entry = 1 } kretprobe:sys_* { @exit = 1 }", R"(
Program
kprobe:sys_*
if
builtin: __builtin_session_is_return
then
=
map: @exit
int: 1 :: [int64]
else
=
map: @entry
int: 1 :: [int64]
)");
}
TEST(probe_expansion, kprobe_wildcard)
{
// Disabled kprobe_multi - should get full expansion
test_attach_points("kprobe:sys_read,kprobe:my_*,kprobe:sys_write {}",
{ "kprobe:sys_read",
"kprobe:my_one",
"kprobe:my_two",
"kprobe:sys_write" },
false);
}
TEST(probe_expansion, kprobe_multi_wildcard)
{
test_multi_attach_points(
"kprobe:sys_read,kprobe:my_*,kprobe:sys_write {}",
{ "kprobe:sys_read", "kprobe:my_*", "kprobe:sys_write" },
{ {}, { "my_one", "my_two" }, {} });
}
TEST(probe_expansion, probe_builtin)
{
// Even though kprobe_multi is enabled (by default), we should get full
// expansion due to using the "probe" builtin.
test_attach_points(
"kprobe:sys_read,kprobe:my_*,kprobe:sys_write { __builtin_probe }",
{ "kprobe:sys_read",
"kprobe:my_one",
"kprobe:my_two",
"kprobe:sys_write" });
}
TEST(probe_expansion, kprobe_wildcard_no_matches)
{
test_attach_points("kprobe:sys_read,kprobe:not_here_*,kprobe:sys_write {}",
{ "kprobe:sys_read", "kprobe:sys_write" },
false);
}
TEST(probe_expansion, krpobe_multi_wildcard_no_matches)
{
test_attach_points("kprobe:sys_read,kprobe:not_here_*,kprobe:sys_write {}",
{ "kprobe:sys_read", "kprobe:sys_write" });
}
TEST(probe_expansion, kprobe_module_wildcard)
{
// We leave kprobe_multi enabled here but it doesn't support the
// module:function syntax so full expansion should be done anyways.
test_attach_points("kprobe:*kernel_mod:* {}",
{ "kprobe:kernel_mod:func_in_mod",
"kprobe:kernel_mod:other_func_in_mod",
"kprobe:other_kernel_mod:func_in_mod" });
}
TEST(probe_expansion, kprobe_module_function_wildcard)
{
// We leave kprobe_multi enabled here but it doesn't support the
// module:function syntax so full expansion should be done anyways.
test_attach_points("kprobe:kernel_mod:*func_in_mod {}",
{ "kprobe:kernel_mod:func_in_mod",
"kprobe:kernel_mod:other_func_in_mod" });
}
TEST(probe_expansion, uprobe_wildcard)
{
// Disabled uprobe_multi - should get full expansion
test_attach_points("uprobe:/bin/sh:*open {}",
{ "uprobe:/bin/sh:first_open",
"uprobe:/bin/sh:second_open" },
false);
}
TEST(probe_expansion, uprobe_multi_wildcard)
{
test_multi_attach_points("uprobe:/bin/sh:*open {}",
{ "uprobe:/bin/sh:*open" },
{ { "/bin/sh:first_open", "/bin/sh:second_open" } });
}
TEST(probe_expansion, uprobe_wildcard_file)
{
// Disabled uprobe_multi - should get full expansion
test_attach_points("uprobe:/bin/*sh:*open {}",
{ "uprobe:/bin/bash:first_open",
"uprobe:/bin/sh:first_open",
"uprobe:/bin/sh:second_open" },
false);
}
TEST(probe_expansion, uprobe_multi_wildcard_file)
{
// Enabled uprobe_multi - targets should be expanded, functions shouldn't
test_multi_attach_points("uprobe:/bin/*sh:*open {}",
{ "uprobe:/bin/sh:*open", "uprobe:/bin/bash:*open" },
{ { "/bin/sh:first_open", "/bin/sh:second_open" },
{ "/bin/bash:first_open" } });
}
TEST(probe_expansion, uprobe_wildcard_no_matches)
{
test_attach_points("uprobe:/bin/sh:foo*,uprobe:/bin/sh:first_open {}",
{ "uprobe:/bin/sh:first_open" },
false);
}
TEST(probe_expansion, uprobe_wildcard_multi_no_matches)
{
test_attach_points("uprobe:/bin/sh:foo*,uprobe:/bin/sh:first_open {}",
{ "uprobe:/bin/sh:first_open" });
}
TEST(probe_expansion, uprobe_cpp_symbol)
{
test_attach_points("uprobe:/bin/sh:cpp:cpp_mangled {}",
{ "uprobe:/bin/sh:cpp:_Z11cpp_mangledi",
"uprobe:/bin/sh:cpp:_Z11cpp_mangledv",
"uprobe:/bin/sh:cpp:cpp_mangled" },
false);
}
TEST(probe_expansion, uprobe_cpp_symbol_full)
{
test_attach_points("uprobe:/bin/sh:cpp:\"cpp_mangled(int)\" {}",
{ "uprobe:/bin/sh:cpp:_Z11cpp_mangledi" },
false);
}
TEST(probe_expansion, uprobe_cpp_symbol_wildcard)
{
test_attach_points("uprobe:/bin/sh:cpp:cpp_mangled* {}",
{ "uprobe:/bin/sh:cpp:_Z11cpp_mangledi",
"uprobe:/bin/sh:cpp:_Z11cpp_mangledv",
"uprobe:/bin/sh:cpp:_Z18cpp_mangled_suffixv",
"uprobe:/bin/sh:cpp:cpp_mangled" },
false);
}
TEST(probe_expansion, uprobe_no_demangling)
{
// Without the :cpp prefix, only look for non-mangled "cpp_mangled" symbol
test_attach_points("uprobe:/bin/sh:cpp_mangled* {}",
{ "uprobe:/bin/sh:cpp_mangled" },
false);
}
TEST(probe_expansion, usdt_wildcard)
{
test_attach_points("usdt:/bin/*sh:prov*:tp* {}",
{ "usdt:/bin/bash:prov1:tp3",
"usdt:/bin/sh:prov1:tp1",
"usdt:/bin/sh:prov1:tp2",
"usdt:/bin/sh:prov2:tp" });
}
TEST(probe_expansion, usdt_empty_namespace)
{
test_attach_points("usdt:/bin/sh:tp1 {}", { "usdt:/bin/sh:prov1:tp1" });
}
TEST(probe_expansion, tracepoint_wildcard)
{
test_attach_points("tracepoint:sched:sched_* {}",
{ "tracepoint:sched:sched_one",
"tracepoint:sched:sched_two" });
}
TEST(probe_expansion, tracepoint_category_wildcard)
{
test_attach_points("tracepoint:sched*:sched_* {}",
{ "tracepoint:sched:sched_one",
"tracepoint:sched:sched_two",
"tracepoint:sched_extra:sched_extra" });
}
TEST(probe_expansion, tracepoint_wildcard_no_matches)
{
test_attach_points("tracepoint:type:typo_*,tracepoint:sched:sched_one {}",
{ "tracepoint:sched:sched_one" });
}
class probe_expansion_btf : public test_btf {};
TEST(probe_expansion_btf, fentry_wildcard)
{
test_attach_points("fentry:func_* {}",
{ "fentry:vmlinux:func_1",
"fentry:vmlinux:func_2",
"fentry:vmlinux:func_3" });
}
TEST(probe_expansion_btf, fentry_wildcard_no_matches)
{
test_attach_points("fentry:foo*,fentry:vmlinux:func_1 {}",
{ "fentry:vmlinux:func_1" });
}
TEST(probe_expansion_btf, fentry_module_wildcard)
{
test_attach_points("fentry:*:func_1 {}", { "fentry:vmlinux:func_1" });
}
TEST(probe_expansion_btf, fentry_bpf_id_wildcard)
{
test_attach_points("fentry:bpf:123:func_* {}",
{ "fentry:bpf:123:func_1", "fentry:bpf:123:func_2" });
}
TEST(probe_expansion_btf, rawtracepoint_wildcard)
{
test_attach_points("rawtracepoint:event* {}",
{ "rawtracepoint:vmlinux:event_rt" });
}
TEST(probe_expansion_btf, rawtracepoint_wildcard_no_matches)
{
test_attach_points("rawtracepoint:foo*,rawtracepoint:event_rt {}",
{ "rawtracepoint:vmlinux:event_rt" });
}
TEST(probe_expansion_btf, kprobe_session)
{
test_multi_attach_points("kprobe:my_* {} kretprobe:my_* {}",
{ "kprobe:my_*" },
{ { "my_one", "my_two" } });
}
} // namespace bpftrace::test::probe_expansion
|