forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_usdt_probes.cc
433 lines (346 loc) · 12.7 KB
/
test_usdt_probes.cc
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2016 GitHub, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <linux/version.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "catch.hpp"
#include "usdt.h"
#include "api/BPF.h"
/* required to insert USDT probes on this very executable --
* we're gonna be testing them live! */
#include "folly/tracing/StaticTracepoint.h"
static int a_probed_function() {
int an_int = 23 + getpid();
void *a_pointer = malloc(4);
FOLLY_SDT(libbcc_test, sample_probe_1, an_int, a_pointer);
free(a_pointer);
return an_int;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
FOLLY_SDT_DEFINE_SEMAPHORE(libbcc_test, sample_probe_2)
static int a_probed_function_with_sem() {
int an_int = 23 + getpid();
void *a_pointer = malloc(4);
FOLLY_SDT_WITH_SEMAPHORE(libbcc_test, sample_probe_2, an_int, a_pointer);
free(a_pointer);
return an_int;
}
#endif // linux version >= 4.20
extern "C" int lib_probed_function();
int call_shared_lib_func() {
return lib_probed_function();
}
TEST_CASE("test finding a probe in our own process", "[usdt]") {
USDT::Context ctx(getpid());
REQUIRE(ctx.num_probes() >= 1);
SECTION("our test probe") {
auto probe = ctx.get("sample_probe_1");
REQUIRE(probe);
if(probe->in_shared_object(probe->bin_path()))
return;
REQUIRE(probe->name() == "sample_probe_1");
REQUIRE(probe->provider() == "libbcc_test");
REQUIRE(probe->bin_path().find("/test_libbcc") != std::string::npos);
REQUIRE(probe->num_locations() == 1);
REQUIRE(probe->num_arguments() == 2);
REQUIRE(probe->need_enable() == false);
REQUIRE(a_probed_function() != 0);
}
}
TEST_CASE("test probe's attributes with C++ API", "[usdt]") {
const ebpf::USDT u("/proc/self/exe", "libbcc_test", "sample_probe_1", "on_event");
REQUIRE(u.binary_path() == "/proc/self/exe");
REQUIRE(u.pid() == -1);
REQUIRE(u.provider() == "libbcc_test");
REQUIRE(u.name() == "sample_probe_1");
REQUIRE(u.probe_func() == "on_event");
}
TEST_CASE("test fine a probe in our own binary with C++ API", "[usdt]") {
ebpf::BPF bpf;
ebpf::USDT u("/proc/self/exe", "libbcc_test", "sample_probe_1", "on_event");
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.ok());
res = bpf.attach_usdt(u);
REQUIRE(res.ok());
res = bpf.detach_usdt(u);
REQUIRE(res.ok());
}
TEST_CASE("test fine probes in our own binary with C++ API", "[usdt]") {
ebpf::BPF bpf;
ebpf::USDT u("/proc/self/exe", "libbcc_test", "sample_probe_1", "on_event");
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.ok());
res = bpf.attach_usdt_all();
REQUIRE(res.ok());
res = bpf.detach_usdt_all();
REQUIRE(res.ok());
}
TEST_CASE("test fine a probe in our Process with C++ API", "[usdt]") {
ebpf::BPF bpf;
ebpf::USDT u(::getpid(), "libbcc_test", "sample_probe_1", "on_event");
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.ok());
res = bpf.attach_usdt(u);
REQUIRE(res.ok());
res = bpf.detach_usdt(u);
REQUIRE(res.ok());
}
TEST_CASE("test find a probe in our process' shared libs with c++ API", "[usdt]") {
ebpf::BPF bpf;
ebpf::USDT u(::getpid(), "libbcc_test", "sample_lib_probe_1", "on_event");
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.msg() == "");
REQUIRE(res.ok());
}
TEST_CASE("test usdt partial init w/ fail init_usdt", "[usdt]") {
ebpf::BPF bpf;
ebpf::USDT u(::getpid(), "libbcc_test", "sample_lib_probe_nonexistent", "on_event");
ebpf::USDT p(::getpid(), "libbcc_test", "sample_lib_probe_1", "on_event");
// We should be able to fail initialization and subsequently do bpf.init w/o USDT
// successfully
auto res = bpf.init_usdt(u);
REQUIRE(res.msg() != "");
REQUIRE(!res.ok());
// Shouldn't be necessary to re-init bpf object either after failure to init w/
// bad USDT
res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.msg() != "");
REQUIRE(!res.ok());
res = bpf.init_usdt(p);
REQUIRE(res.msg() == "");
REQUIRE(res.ok());
res = bpf.init("int on_event() { return 0; }", {}, {});
REQUIRE(res.msg() == "");
REQUIRE(res.ok());
}
class ChildProcess {
pid_t pid_;
public:
ChildProcess(const char *name, char *const argv[]) {
pid_ = fork();
if (pid_ == 0) {
execvp(name, argv);
exit(0);
}
if (spawned()) {
usleep(250000);
if (kill(pid_, 0) < 0)
pid_ = -1;
}
}
~ChildProcess() {
if (spawned()) {
int status;
kill(pid_, SIGKILL);
if (waitpid(pid_, &status, 0) != pid_)
abort();
}
}
bool spawned() const { return pid_ > 0; }
pid_t pid() const { return pid_; }
};
extern int cmd_scanf(const char *cmd, const char *fmt, ...);
static int probe_num_locations(const char *bin_path, const char *func_name) {
int num_locations;
char cmd[512];
const char *cmdfmt = "readelf -n %s | grep -c \"Name: %s$\"";
sprintf(cmd, cmdfmt, bin_path, func_name);
if (cmd_scanf(cmd, "%d", &num_locations) != 0) {
return -1;
}
return num_locations;
}
static int probe_num_arguments(const char *bin_path, const char *func_name) {
int num_arguments;
char cmd[512];
const char *cmdfmt = "readelf -n %s | grep -m 1 -A 2 \" %s$\" | " \
"tail -1 | cut -d \" \" -f 6- | wc -w";
sprintf(cmd, cmdfmt, bin_path, func_name);
if (cmd_scanf(cmd, "%d", &num_arguments) != 0) {
return -1;
}
return num_arguments;
}
// Unsharing pid namespace requires forking
// this uses pgrep to find the child process, by searching for a process
// that has the unshare as its parent
static int unshared_child_pid(const int ppid) {
int child_pid;
char cmd[512];
const char *cmdfmt = "pgrep -P %d";
sprintf(cmd, cmdfmt, ppid);
if (cmd_scanf(cmd, "%d", &child_pid) != 0) {
return -1;
}
return child_pid;
}
// FIXME This seems like a legitimate bug with probing ruby where the
// ruby symbols are in libruby.so?
TEST_CASE("test listing all USDT probes in Ruby/MRI", "[usdt][!mayfail]") {
size_t mri_probe_count = 0;
SECTION("without a running Ruby process") {
USDT::Context ctx("ruby");
if (!ctx.loaded())
return;
REQUIRE(ctx.num_probes() > 10);
mri_probe_count = ctx.num_probes();
SECTION("GC static probe") {
auto name = "gc__mark__begin";
auto probe = ctx.get(name);
REQUIRE(probe);
REQUIRE(probe->in_shared_object(probe->bin_path()) == true);
REQUIRE(probe->name() == name);
REQUIRE(probe->provider() == "ruby");
auto bin_path = probe->bin_path();
bool bin_path_match =
(bin_path.find("/ruby") != std::string::npos) ||
(bin_path.find("/libruby") != std::string::npos);
REQUIRE(bin_path_match);
int exp_locations, exp_arguments;
exp_locations = probe_num_locations(bin_path.c_str(), name);
exp_arguments = probe_num_arguments(bin_path.c_str(), name);
REQUIRE(probe->num_locations() == exp_locations);
REQUIRE(probe->num_arguments() == exp_arguments);
REQUIRE(probe->need_enable() == true);
}
SECTION("object creation probe") {
auto name = "object__create";
auto probe = ctx.get(name);
REQUIRE(probe);
REQUIRE(probe->in_shared_object(probe->bin_path()) == true);
REQUIRE(probe->name() == name);
REQUIRE(probe->provider() == "ruby");
auto bin_path = probe->bin_path();
bool bin_path_match =
(bin_path.find("/ruby") != std::string::npos) ||
(bin_path.find("/libruby") != std::string::npos);
REQUIRE(bin_path_match);
int exp_locations, exp_arguments;
exp_locations = probe_num_locations(bin_path.c_str(), name);
exp_arguments = probe_num_arguments(bin_path.c_str(), name);
REQUIRE(probe->num_locations() == exp_locations);
REQUIRE(probe->num_arguments() == exp_arguments);
REQUIRE(probe->need_enable() == true);
}
SECTION("array creation probe") {
auto name = "array__create";
auto probe = ctx.get(name);
REQUIRE(probe);
REQUIRE(probe->name() == name);
auto bin_path = probe->bin_path().c_str();
int exp_locations, exp_arguments;
exp_locations = probe_num_locations(bin_path, name);
exp_arguments = probe_num_arguments(bin_path, name);
REQUIRE(probe->num_locations() == exp_locations);
REQUIRE(probe->num_arguments() == exp_arguments);
REQUIRE(probe->need_enable() == true);
}
}
SECTION("with a running Ruby process") {
static char _ruby[] = "ruby";
char *const argv[2] = {_ruby, NULL};
ChildProcess ruby(argv[0], argv);
if (!ruby.spawned())
return;
USDT::Context ctx(ruby.pid());
REQUIRE(ctx.num_probes() >= mri_probe_count);
SECTION("get probe in running process") {
auto name = "gc__mark__begin";
auto probe = ctx.get(name);
REQUIRE(probe);
REQUIRE(probe->in_shared_object(probe->bin_path()) == true);
REQUIRE(probe->name() == name);
REQUIRE(probe->provider() == "ruby");
auto bin_path = probe->bin_path();
bool bin_path_match =
(bin_path.find("/ruby") != std::string::npos) ||
(bin_path.find("/libruby") != std::string::npos);
REQUIRE(bin_path_match);
int exp_locations, exp_arguments;
exp_locations = probe_num_locations(bin_path.c_str(), name);
exp_arguments = probe_num_arguments(bin_path.c_str(), name);
REQUIRE(probe->num_locations() == exp_locations);
REQUIRE(probe->num_arguments() == exp_arguments);
REQUIRE(probe->need_enable() == true);
}
}
}
// These tests are expected to fail if there is no Ruby with dtrace probes
TEST_CASE("test probing running Ruby process in namespaces",
"[usdt][!mayfail]") {
SECTION("in separate mount namespace") {
static char _unshare[] = "unshare";
const char *const argv[4] = {_unshare, "--mount", "ruby", NULL};
ChildProcess unshare(argv[0], (char **const)argv);
if (!unshare.spawned())
return;
int ruby_pid = unshare.pid();
ebpf::BPF bpf;
ebpf::USDT u(ruby_pid, "ruby", "gc__mark__begin", "on_event");
u.set_probe_matching_kludge(1); // Also required for overlayfs...
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.msg() == "");
REQUIRE(res.ok());
res = bpf.attach_usdt(u, ruby_pid);
REQUIRE(res.ok());
res = bpf.detach_usdt(u, ruby_pid);
REQUIRE(res.ok());
}
SECTION("in separate mount namespace and separate PID namespace") {
static char _unshare[] = "unshare";
const char *const argv[8] = {_unshare, "--fork",
"--mount", "--pid", "--mount-proc",
"ruby", NULL};
ChildProcess unshare(argv[0], (char **const)argv);
if (!unshare.spawned())
return;
int ruby_pid = unshared_child_pid(unshare.pid());
ebpf::BPF bpf;
ebpf::USDT u(ruby_pid, "ruby", "gc__mark__begin", "on_event");
u.set_probe_matching_kludge(1); // Also required for overlayfs...
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.msg() == "");
REQUIRE(res.ok());
res = bpf.attach_usdt(u, ruby_pid);
REQUIRE(res.ok());
res = bpf.detach_usdt(u, ruby_pid);
REQUIRE(res.ok());
struct bcc_symbol sym;
std::string pid_root= "/proc/" + std::to_string(ruby_pid) + "/root/";
std::string module = pid_root + "usr/local/bin/ruby";
REQUIRE(bcc_resolve_symname(module.c_str(), "rb_gc_mark", 0x0, ruby_pid, nullptr, &sym) == 0);
REQUIRE(std::string(sym.module).find(pid_root, 1) == std::string::npos);
kill(ruby_pid, SIGKILL);
bcc_procutils_free(sym.module);
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
TEST_CASE("Test uprobe refcnt semaphore activation", "[usdt]") {
ebpf::BPF bpf;
REQUIRE(!FOLLY_SDT_IS_ENABLED(libbcc_test, sample_probe_2));
ebpf::USDT u("/proc/self/exe", "libbcc_test", "sample_probe_2", "on_event");
auto res = bpf.init("int on_event() { return 0; }", {}, {u});
REQUIRE(res.ok());
res = bpf.attach_usdt(u);
REQUIRE(res.ok());
REQUIRE(FOLLY_SDT_IS_ENABLED(libbcc_test, sample_probe_2));
res = bpf.detach_usdt(u);
REQUIRE(res.ok());
REQUIRE(a_probed_function_with_sem() != 0);
}
#endif // linux version >= 4.20