Repository navigation
Expand file tree
/
Copy pathattached_probe.cpp
More file actions
1409 lines (1208 loc) · 40.6 KB
/
Copy pathattached_probe.cpp
File metadata and controls
1409 lines (1208 loc) · 40.6 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <algorithm>
#include <cstring>
#include <elf.h>
#include <fcntl.h>
#include <filesystem>
#include <iostream>
#include <linux/hw_breakpoint.h>
#include <linux/limits.h>
#include <linux/perf_event.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <utility>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "attached_probe.h"
#include "bpftrace.h"
#include "disasm.h"
#include "log.h"
#include "symbols/kernel.h"
#include "util/bpf_names.h"
#include "util/cpus.h"
#include "util/exceptions.h"
#include "util/paths.h"
#include "util/symbols.h"
#include "util/system.h"
namespace bpftrace {
char AttachError::ID;
void AttachError::log(llvm::raw_ostream &OS) const
{
OS << msg_;
}
bool is_return_type(ProbeType t)
{
// clang-format off
switch (t)
{
case ProbeType::kprobe: return false; break;
case ProbeType::kretprobe: return true; break;
case ProbeType::special: return false; break;
case ProbeType::test: return false; break;
case ProbeType::benchmark: return false; break;
case ProbeType::uprobe: return false; break;
case ProbeType::uretprobe: return true; break;
case ProbeType::usdt: return false; break;
default:
LOG(BUG) << "invalid probe type \"" << t << "\"";
}
// clang-format on
}
bpf_prog_type progtype(ProbeType t)
{
switch (t) {
// clang-format off
case ProbeType::special: return BPF_PROG_TYPE_RAW_TRACEPOINT; break;
case ProbeType::test: return BPF_PROG_TYPE_XDP; break;
case ProbeType::benchmark: return BPF_PROG_TYPE_XDP; break;
case ProbeType::kprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::kretprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::uprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::uretprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::usdt: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::tracepoint: return BPF_PROG_TYPE_TRACEPOINT; break;
case ProbeType::profile: return BPF_PROG_TYPE_PERF_EVENT; break;
case ProbeType::interval: return BPF_PROG_TYPE_PERF_EVENT; break;
case ProbeType::software: return BPF_PROG_TYPE_PERF_EVENT; break;
case ProbeType::watchpoint: return BPF_PROG_TYPE_PERF_EVENT; break;
case ProbeType::hardware: return BPF_PROG_TYPE_PERF_EVENT; break;
case ProbeType::fentry: return BPF_PROG_TYPE_TRACING; break;
case ProbeType::fexit: return BPF_PROG_TYPE_TRACING; break;
case ProbeType::iter: return BPF_PROG_TYPE_TRACING; break;
case ProbeType::rawtracepoint: return BPF_PROG_TYPE_TRACING; break;
// clang-format on
case ProbeType::invalid:
LOG(BUG) << "program type invalid";
}
return {}; // unreached
}
// eventprefix is always used as std::string
// NOLINTNEXTLINE(modernize-use-string-view)
std::string eventprefix(ProbeType t)
{
return is_return_type(t) ? "r_" : "p_";
}
std::string eventname(const Probe &probe, uint64_t offset)
{
std::ostringstream offset_str;
std::string index_str = "_" + std::to_string(probe.index);
switch (probe.type) {
case ProbeType::kprobe:
case ProbeType::kretprobe:
case ProbeType::rawtracepoint:
offset_str << std::hex << offset;
return eventprefix(probe.type) +
util::sanitise_bpf_program_name(probe.attach_point) + "_" +
offset_str.str() + index_str;
case ProbeType::uprobe:
case ProbeType::uretprobe:
case ProbeType::usdt:
offset_str << std::hex << offset;
return eventprefix(probe.type) +
util::sanitise_bpf_program_name(probe.path) + "_" +
offset_str.str() + index_str;
case ProbeType::tracepoint:
return probe.attach_point;
default:
LOG(BUG) << "invalid eventname probe \"" << probe.type << "\"";
}
}
Result<uint64_t> resolve_offset_kprobe(Probe &probe)
{
uint64_t offset = probe.func_offset;
// If we are using only the symbol, we don't need to check the offset.
bool is_symbol_kprobe = !probe.attach_point.empty();
if (is_symbol_kprobe && probe.func_offset == 0)
return offset;
// To validate an arbitrary address, we'd need to check symtabs of vmlinux
// and all modules, as well as know their load addresses. Since we support
// address probes only in unsafe mode anyway, validation is deferred to the
// kernel. This function is only called for sym+offset kprobes.
assert(is_symbol_kprobe);
Symbol sym = {};
sym.name = probe.attach_point;
auto path = symbols::find_vmlinux(&sym);
if (!path.has_value()) {
LOG(V1) << "Could not resolve symbol " << probe.attach_point
<< ". Skipping usermode offset checking.";
LOG(V1) << "The kernel will verify the safety of the location but "
"will also allow the offset to be in a different symbol.";
return offset;
}
if (probe.func_offset >= sym.size) {
return make_error<AttachError>("Offset outside the function bounds ('" +
probe.attach_point + "' size is " +
std::to_string(sym.size) + ")");
}
return offset;
}
static constexpr std::string_view hint_unsafe =
"\nUse --unsafe to force attachment. WARNING: This option could lead to "
"data corruption in the target process.";
Result<> check_alignment(Probe &probe,
std::string &symbol,
uint64_t sym_offset,
uint64_t func_offset,
bool safe_mode)
{
Disasm dasm(probe.path);
AlignState aligned = dasm.is_aligned(sym_offset, func_offset);
std::string tmp = probe.path + ":" + symbol + "+" +
std::to_string(func_offset);
switch (aligned) {
case AlignState::Ok:
return OK();
case AlignState::NotAlign:
if (safe_mode) {
return make_error<AttachError>(
"Could not add " + probetypeName(probe.type) +
" into middle of instruction: " + tmp + std::string{ hint_unsafe });
} else {
std::string_view hint;
LOG(WARNING) << "Unsafe " << probe.type
<< " in the middle of the instruction: " << tmp << hint;
return OK();
}
case AlignState::Fail:
if (safe_mode) {
return make_error<AttachError>(
"Failed to check if " + probetypeName(probe.type) +
" is in proper place: " + tmp + std::string{ hint_unsafe });
} else {
LOG(WARNING) << "Unchecked " << probe.type << ": " << tmp;
return OK();
}
case AlignState::NotSupp:
if (safe_mode) {
return make_error<AttachError>("Can't check if " +
probetypeName(probe.type) +
" is in proper place (compiled without "
"(k|u)probe offset support): " +
tmp + std::string{ hint_unsafe });
} else {
LOG(WARNING) << "Unchecked " << probe.type << ": " << tmp;
return OK();
}
}
return OK();
}
Result<uint64_t> resolve_offset_uprobe(Probe &probe, bool safe_mode)
{
std::string &symbol = probe.attach_point;
uint64_t func_offset = probe.func_offset;
if (symbol.empty()) {
auto sym = util::resolve_symbol(probe.path, probe.address);
if (!sym) {
if (safe_mode) {
consumeError(sym.takeError());
return make_error<AttachError>(
"Could not determine instruction boundary for " + probe.name +
" (binary appears stripped)." + std::string{ hint_unsafe });
} else {
consumeError(sym.takeError());
LOG(WARNING) << "Could not determine instruction boundary for "
<< probe.name
<< " (binary appears stripped). Misaligned probes "
"can lead to tracee crashes!";
return probe.address;
}
}
symbol = sym->name;
func_offset = probe.address - sym->v_addr;
}
auto symbols = util::resolve_symbols(probe.path, { symbol });
if (!symbols) {
return make_error<AttachError>(llvm::toString(symbols.takeError()));
}
if (symbols->empty()) {
return make_error<AttachError>("Could not resolve symbol: " + probe.path +
":" + symbol);
}
auto &sym = symbols->front();
if (probe.type == ProbeType::uretprobe && func_offset != 0) {
return make_error<AttachError>("uretprobes cannot be attached at function "
"offset. (address resolved to: " +
symbol + "+" + std::to_string(func_offset) +
")");
}
if (sym.size == 0 && func_offset == 0) {
if (safe_mode) {
return make_error<AttachError>("Could not determine boundary for " +
sym.name + " (symbol has size 0)." +
std::string{ hint_unsafe });
}
} else if (func_offset >= sym.size) {
return make_error<AttachError>("Offset outside the function bounds ('" +
symbol + "' size is " +
std::to_string(sym.size) + ")");
}
uint64_t offset = sym.file_offset + func_offset;
if (func_offset == 0)
return offset;
auto align_ok = check_alignment(
probe, symbol, sym.file_offset, func_offset, safe_mode);
if (!align_ok) {
return align_ok.takeError();
}
return offset;
}
Result<std::vector<Symbol>> resolve_offsets_uprobe_multi(Probe &probe)
{
std::set<std::string> names;
for (const std::string &func : probe.funcs) {
auto pos = func.find(':');
if (pos == std::string::npos) {
return make_error<AttachError>("Error resolving probe: " + probe.name);
}
names.insert(func.substr(pos + 1));
}
auto symbols = util::resolve_symbols(probe.path, names);
if (!symbols) {
return make_error<AttachError>(llvm::toString(symbols.takeError()));
}
return symbols;
}
class AttachedKprobeProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedKprobeProbe>> make(
Probe &probe,
const BpfProgram &prog);
~AttachedKprobeProbe() override;
int link_fd() override;
private:
AttachedKprobeProbe(const Probe &probe, struct bpf_link *link);
struct bpf_link *link_;
};
AttachedKprobeProbe::AttachedKprobeProbe(const Probe &probe,
struct bpf_link *link)
: AttachedProbe(probe), link_(link)
{
}
AttachedKprobeProbe::~AttachedKprobeProbe()
{
if (bpf_link__destroy(link_)) {
LOG(WARNING) << "failed to destroy link for kprobe probe: "
<< strerror(errno);
}
}
int AttachedKprobeProbe::link_fd()
{
return bpf_link__fd(link_);
}
Result<std::unique_ptr<AttachedKprobeProbe>> AttachedKprobeProbe::make(
Probe &probe,
const BpfProgram &prog)
{
// Construct a string containing "module:function". We don't do any
// additional checking here because this has already been verified during
// attachpoint verification. We will automatically propagate any failures
// to attach form the libbpf layer in the future.
std::string funcname;
if (!probe.path.empty() && probe.path != "vmlinux") {
funcname = probe.path + ":" + probe.attach_point;
} else {
funcname = probe.attach_point;
}
const char *kprobe_func;
uint64_t offset;
// The kprobe can either be defined by a symbol+offset or an address:
// For symbol+offset kprobe, we need to check the validity of the offset.
// Address kprobes are only allowed in unsafe mode so no validation is
// performed -- the kernel will reject invalid probes.
bool is_symbol_kprobe = !probe.attach_point.empty();
if (is_symbol_kprobe) {
auto offset_res = resolve_offset_kprobe(probe);
if (!offset_res) {
return offset_res.takeError();
}
kprobe_func = funcname.c_str();
offset = *offset_res;
} else {
if (probe.address == 0) {
return make_error<AttachError>("Probe address not set");
}
// The kernel interprets the offset as the absolute address if the symbol
// name is null; see alloc_trace_kprobe() in kernel/trace/trace_kprobe.c
kprobe_func = nullptr;
offset = probe.address;
}
DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
opts.offset = offset;
opts.retprobe = probe.type == ProbeType::kretprobe;
auto *link = bpf_program__attach_kprobe_opts(prog.bpf_prog(),
kprobe_func,
&opts);
if (!link) {
if (errno == EILSEQ)
return make_error<AttachError>(
"Possible attachment attempt in the middle of an instruction, "
"try a different offset.");
return make_error<AttachError>();
}
return std::unique_ptr<AttachedKprobeProbe>(
new AttachedKprobeProbe(probe, link));
}
class AttachedMultiKprobeProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedMultiKprobeProbe>> make(
Probe &probe,
const BpfProgram &prog);
~AttachedMultiKprobeProbe() override;
int link_fd() override;
size_t probe_count() const override;
private:
AttachedMultiKprobeProbe(const Probe &probe, int link_fd);
int link_fd_;
};
AttachedMultiKprobeProbe::AttachedMultiKprobeProbe(const Probe &probe,
int link_fd)
: AttachedProbe(probe), link_fd_(link_fd)
{
}
AttachedMultiKprobeProbe::~AttachedMultiKprobeProbe()
{
close(link_fd_);
}
int AttachedMultiKprobeProbe::link_fd()
{
return link_fd_;
}
size_t AttachedMultiKprobeProbe::probe_count() const
{
return probe_.funcs.size();
}
Result<std::unique_ptr<AttachedMultiKprobeProbe>> AttachedMultiKprobeProbe::
make(Probe &probe, const BpfProgram &prog)
{
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
std::vector<const char *> syms;
unsigned int i = 0;
for (const auto &func : probe.funcs)
syms.push_back(func.c_str());
opts.kprobe_multi.syms = syms.data();
opts.kprobe_multi.cnt = syms.size();
opts.kprobe_multi.flags = probe.type == ProbeType::kretprobe
? BPF_F_KPROBE_MULTI_RETURN
: 0;
if (bt_verbose) {
LOG(V1) << "Attaching to " << probe.funcs.size() << " functions";
for (i = 0; i < opts.kprobe_multi.cnt; i++) {
LOG(V1) << " " << syms[i];
}
}
auto attach_type = probe.is_session ? BPF_TRACE_KPROBE_SESSION
: BPF_TRACE_KPROBE_MULTI;
int link_fd = bpf_link_create(prog.fd(), 0, attach_type, &opts);
if (link_fd < 0) {
return make_error<AttachError>();
}
return std::unique_ptr<AttachedMultiKprobeProbe>(
new AttachedMultiKprobeProbe(probe, link_fd));
}
class AttachedUprobeProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedUprobeProbe>> make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid,
bool safe_mode);
~AttachedUprobeProbe() override;
int link_fd() override;
private:
AttachedUprobeProbe(const Probe &probe, struct bpf_link *link);
struct bpf_link *link_;
};
AttachedUprobeProbe::AttachedUprobeProbe(const Probe &probe,
struct bpf_link *link)
: AttachedProbe(probe), link_(link)
{
}
AttachedUprobeProbe::~AttachedUprobeProbe()
{
if (bpf_link__destroy(link_))
LOG(WARNING) << "failed to destroy link for uprobe probe: "
<< strerror(errno);
}
int AttachedUprobeProbe::link_fd()
{
return bpf_link__fd(link_);
}
Result<std::unique_ptr<AttachedUprobeProbe>> AttachedUprobeProbe::make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid,
bool safe_mode)
{
auto offset_res = resolve_offset_uprobe(probe, safe_mode);
if (!offset_res) {
return offset_res.takeError();
}
uint64_t offset = *offset_res;
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
opts.retprobe = probe.type == ProbeType::uretprobe;
if (!probe.attach_point.empty() && util::is_archive_path(probe.path)) {
// When probing libraries mapped directly from zip archives, libbpf needs
// the function name to determine its offset within the archive. The offset
// argument in this case is relative to the start of the function. See
// https://github.com/libbpf/libbpf/blob/3b4f0ef5/src/libbpf.c#L12289
opts.func_name = probe.attach_point.c_str();
offset = probe.func_offset;
}
auto *link = bpf_program__attach_uprobe_opts(prog.bpf_prog(),
pid.has_value() ? *pid : -1,
probe.path.c_str(),
offset,
&opts);
if (!link) {
return make_error<AttachError>();
}
return std::unique_ptr<AttachedUprobeProbe>(
new AttachedUprobeProbe(probe, link));
}
class AttachedMultiUprobeProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedMultiUprobeProbe>> make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid);
~AttachedMultiUprobeProbe() override;
size_t probe_count() const override;
private:
AttachedMultiUprobeProbe(const Probe &probe, int link_fd);
int link_fd_;
};
AttachedMultiUprobeProbe::AttachedMultiUprobeProbe(const Probe &probe,
int link_fd)
: AttachedProbe(probe), link_fd_(link_fd)
{
}
AttachedMultiUprobeProbe::~AttachedMultiUprobeProbe()
{
close(link_fd_);
}
size_t AttachedMultiUprobeProbe::probe_count() const
{
return probe_.funcs.size();
}
Result<std::unique_ptr<AttachedMultiUprobeProbe>> AttachedMultiUprobeProbe::
make(Probe &probe, const BpfProgram &prog, std::optional<int> pid)
{
auto symbols = resolve_offsets_uprobe_multi(probe);
if (!symbols) {
return symbols.takeError();
}
std::vector<unsigned long> offsets;
for (const auto &sym : *symbols) {
offsets.push_back(sym.file_offset);
}
// Attach uprobe through uprobe_multi link
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
opts.uprobe_multi.path = probe.path.c_str();
opts.uprobe_multi.offsets = offsets.data();
opts.uprobe_multi.cnt = offsets.size();
opts.uprobe_multi.flags = probe.type == ProbeType::uretprobe
? BPF_F_UPROBE_MULTI_RETURN
: 0;
if (pid.has_value()) {
opts.uprobe_multi.pid = *pid;
}
if (bt_verbose) {
LOG(V1) << "Attaching to " << probe.funcs.size() << " functions";
for (const auto &sym : *symbols) {
LOG(V1) << probe.path << ":" << sym.name;
}
}
int link_fd = bpf_link_create(prog.fd(), 0, BPF_TRACE_UPROBE_MULTI, &opts);
if (link_fd < 0) {
return make_error<AttachError>();
}
return std::unique_ptr<AttachedMultiUprobeProbe>(
new AttachedMultiUprobeProbe(probe, link_fd));
}
class AttachedUSDTProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedUSDTProbe>> make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid);
~AttachedUSDTProbe() override;
int link_fd() override;
private:
AttachedUSDTProbe(const Probe &probe, struct bpf_link *link);
struct bpf_link *link_;
};
AttachedUSDTProbe::AttachedUSDTProbe(const Probe &probe, struct bpf_link *link)
: AttachedProbe(probe), link_(link)
{
}
int AttachedUSDTProbe::link_fd()
{
return bpf_link__fd(link_);
}
AttachedUSDTProbe::~AttachedUSDTProbe()
{
if (bpf_link__destroy(link_))
LOG(WARNING) << "failed to destroy link for usdt probe: "
<< strerror(errno);
}
Result<std::unique_ptr<AttachedUSDTProbe>> AttachedUSDTProbe::make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid)
{
auto *link = bpf_program__attach_usdt(prog.bpf_prog(),
pid.has_value() ? *pid : -1,
probe.path.c_str(),
probe.ns.c_str(),
probe.attach_point.c_str(),
nullptr);
if (!link) {
char bpf_error_msg[128];
int res = libbpf_strerror(errno, bpf_error_msg, sizeof(bpf_error_msg));
if (res) {
return make_error<AttachError>("Failed to attach usdt probe: " +
std::string(std::strerror(errno)));
} else {
return make_error<AttachError>("Failed to attach usdt probe: " +
std::string(bpf_error_msg));
}
}
return std::unique_ptr<AttachedUSDTProbe>(new AttachedUSDTProbe(probe, link));
}
class AttachedTracepointProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedTracepointProbe>> make(
Probe &probe,
const BpfProgram &prog);
~AttachedTracepointProbe() override;
private:
AttachedTracepointProbe(const Probe &probe, struct bpf_link *link);
struct bpf_link *link_;
};
AttachedTracepointProbe::AttachedTracepointProbe(const Probe &probe,
struct bpf_link *link)
: AttachedProbe(probe), link_(link)
{
}
AttachedTracepointProbe::~AttachedTracepointProbe()
{
if (bpf_link__destroy(link_)) {
LOG(WARNING) << "failed to destroy link for tracepiont probe: "
<< strerror(errno);
}
}
Result<std::unique_ptr<AttachedTracepointProbe>> AttachedTracepointProbe::make(
Probe &probe,
const BpfProgram &prog)
{
auto *link = bpf_program__attach_tracepoint(prog.bpf_prog(),
probe.path.c_str(),
eventname(probe, 0).c_str());
if (!link) {
return make_error<AttachError>();
}
return std::unique_ptr<AttachedTracepointProbe>(
new AttachedTracepointProbe(probe, link));
}
int open_perf_event(uint32_t ev_type,
uint32_t ev_config,
uint64_t sample_period,
uint64_t sample_freq,
pid_t pid,
int cpu,
int group_fd)
{
if (sample_period > 0 && sample_freq > 0) {
LOG(BUG) << "Exactly one of sample_period / sample_freq should be set";
return -1;
}
struct perf_event_attr attr = {};
attr.type = ev_type;
attr.size = sizeof(struct perf_event_attr);
attr.config = ev_config;
if (sample_freq > 0) {
attr.freq = 1;
attr.sample_freq = sample_freq;
} else {
attr.sample_period = sample_period;
}
if (pid > 0)
attr.inherit = 1;
return syscall(
__NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC);
}
class AttachedProfileProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedProfileProbe>> make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid);
~AttachedProfileProbe() override;
private:
AttachedProfileProbe(const Probe &probe,
std::vector<struct bpf_link *> links);
std::vector<struct bpf_link *> links_;
};
AttachedProfileProbe::AttachedProfileProbe(const Probe &probe,
std::vector<struct bpf_link *> links)
: AttachedProbe(probe), links_(std::move(links))
{
}
AttachedProfileProbe::~AttachedProfileProbe()
{
for (struct bpf_link *link : links_) {
if (bpf_link__destroy(link))
LOG(WARNING) << "failed to destroy link for profile probe: "
<< strerror(errno);
}
}
Result<std::unique_ptr<AttachedProfileProbe>> AttachedProfileProbe::make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid)
{
int group_fd = -1;
uint64_t period, freq;
if (probe.path == "hz") {
period = 0;
freq = probe.freq;
} else if (probe.path == "s") {
period = probe.freq * 1e9;
freq = 0;
} else if (probe.path == "ms") {
period = probe.freq * 1e6;
freq = 0;
} else if (probe.path == "us") {
period = probe.freq * 1e3;
freq = 0;
} else {
return make_error<AttachError>("invalid profile path \"" + probe.path +
"\"");
}
bool has_error = false;
std::vector<struct bpf_link *> links;
std::vector<int> cpus = util::get_online_cpus();
for (int cpu : cpus) {
int perf_event_fd = open_perf_event(PERF_TYPE_SOFTWARE,
PERF_COUNT_SW_CPU_CLOCK,
period,
freq,
pid.has_value() ? *pid : -1,
cpu,
group_fd);
if (perf_event_fd < 0) {
has_error = true;
break;
}
auto *link = bpf_program__attach_perf_event(prog.bpf_prog(), perf_event_fd);
if (!link) {
close(perf_event_fd);
has_error = true;
break;
}
links.push_back(link);
}
if (has_error) {
for (struct bpf_link *link : links) {
if (bpf_link__destroy(link))
LOG(WARNING) << "failed to destroy link for profile probe: "
<< strerror(errno);
}
return make_error<AttachError>();
}
return std::unique_ptr<AttachedProfileProbe>(
new AttachedProfileProbe(probe, links));
}
class AttachedIntervalProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedIntervalProbe>> make(
Probe &probe,
const BpfProgram &prog);
~AttachedIntervalProbe() override;
int link_fd() override;
private:
AttachedIntervalProbe(const Probe &probe, struct bpf_link *link);
struct bpf_link *link_;
};
AttachedIntervalProbe::AttachedIntervalProbe(const Probe &probe,
struct bpf_link *link)
: AttachedProbe(probe), link_(link)
{
}
AttachedIntervalProbe::~AttachedIntervalProbe()
{
if (bpf_link__destroy(link_))
LOG(WARNING) << "failed to destroy link for interval probe: "
<< strerror(errno);
}
int AttachedIntervalProbe::link_fd()
{
return bpf_link__fd(link_);
}
Result<std::unique_ptr<AttachedIntervalProbe>> AttachedIntervalProbe::make(
Probe &probe,
const BpfProgram &prog)
{
int group_fd = -1;
int cpu = 0;
std::vector<int> cpus = util::get_online_cpus();
if (!cpus.empty())
cpu = cpus[0];
uint64_t period = 0, freq = 0;
if (probe.path == "s") {
period = probe.freq * 1e9;
} else if (probe.path == "ms") {
period = probe.freq * 1e6;
} else if (probe.path == "us") {
period = probe.freq * 1e3;
} else if (probe.path == "hz") {
freq = probe.freq;
} else {
return make_error<AttachError>("invalid interval path \"" + probe.path +
"\"");
}
int perf_event_fd = open_perf_event(PERF_TYPE_SOFTWARE,
PERF_COUNT_SW_CPU_CLOCK,
period,
freq,
-1,
cpu,
group_fd);
if (perf_event_fd < 0) {
return make_error<AttachError>();
}
auto *link = bpf_program__attach_perf_event(prog.bpf_prog(), perf_event_fd);
if (!link) {
close(perf_event_fd);
return make_error<AttachError>();
}
return std::unique_ptr<AttachedIntervalProbe>(
new AttachedIntervalProbe(probe, link));
}
class AttachedSoftwareProbe : public AttachedProbe {
public:
static Result<std::unique_ptr<AttachedSoftwareProbe>> make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid);
~AttachedSoftwareProbe() override;
private:
AttachedSoftwareProbe(const Probe &probe,
std::vector<struct bpf_link *> links);
std::vector<struct bpf_link *> links_;
};
AttachedSoftwareProbe::AttachedSoftwareProbe(
const Probe &probe,
std::vector<struct bpf_link *> links)
: AttachedProbe(probe), links_(std::move(links))
{
}
AttachedSoftwareProbe::~AttachedSoftwareProbe()
{
for (struct bpf_link *link : links_) {
if (bpf_link__destroy(link))
LOG(WARNING) << "failed to destroy link for software probe: "
<< strerror(errno);
}
}
Result<std::unique_ptr<AttachedSoftwareProbe>> AttachedSoftwareProbe::make(
Probe &probe,
const BpfProgram &prog,
std::optional<int> pid)
{
int group_fd = -1;
uint64_t period = probe.freq;
uint64_t defaultp = 1;
uint32_t type = 0;
// from linux/perf_event.h, with aliases from perf:
for (const auto &probeListItem : SW_PROBE_LIST) {
if (probe.path == probeListItem.path || probe.path == probeListItem.alias) {
type = probeListItem.type;
defaultp = probeListItem.defaultp;
}
}
if (period == 0)
period = defaultp;
bool has_error = false;
std::vector<struct bpf_link *> links;
std::vector<int> cpus = util::get_online_cpus();
for (int cpu : cpus) {
int perf_event_fd = open_perf_event(PERF_TYPE_SOFTWARE,
type,
period,
0,
pid.has_value() ? *pid : -1,
cpu,
group_fd);
if (perf_event_fd < 0) {
has_error = true;
break;
}
auto *link = bpf_program__attach_perf_event(prog.bpf_prog(), perf_event_fd);
if (!link) {
close(perf_event_fd);
has_error = true;
break;
}
links.push_back(link);
}
if (has_error) {