-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathutils.cpp
1235 lines (1074 loc) · 32.8 KB
/
utils.cpp
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
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <glob.h>
#include <limits>
#include <link.h>
#include <map>
#include <memory>
#include <regex>
#include <sstream>
#include <string>
#include <sys/auxv.h>
#include <sys/stat.h>
#include <system_error>
#include <tuple>
#include <unistd.h>
#include "bpftrace.h"
#include "log.h"
#include "probe_matcher.h"
#include "tracefs.h"
#include "utils.h"
#include <bcc/bcc_elf.h>
#include <bcc/bcc_syms.h>
#include <bcc/bcc_usdt.h>
#include <elf.h>
#include <linux/version.h>
#if __has_include(<filesystem>)
#include <filesystem>
namespace std_filesystem = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace std_filesystem = std::experimental::filesystem;
#else
#error "neither <filesystem> nor <experimental/filesystem> are present"
#endif
namespace {
std::vector<int> read_cpu_range(std::string path)
{
std::ifstream cpus_range_stream { path };
std::vector<int> cpus;
std::string cpu_range;
while (std::getline(cpus_range_stream, cpu_range, ',')) {
std::size_t rangeop = cpu_range.find('-');
if (rangeop == std::string::npos) {
cpus.push_back(std::stoi(cpu_range));
}
else {
int start = std::stoi(cpu_range.substr(0, rangeop));
int end = std::stoi(cpu_range.substr(rangeop + 1));
for (int i = start; i <= end; i++)
cpus.push_back(i);
}
}
return cpus;
}
std::vector<std::string> expand_wildcard_path(const std::string& path)
{
glob_t glob_result;
memset(&glob_result, 0, sizeof(glob_result));
if (glob(path.c_str(), GLOB_NOCHECK, nullptr, &glob_result)) {
globfree(&glob_result);
throw std::runtime_error("glob() failed");
}
std::vector<std::string> matching_paths;
for (size_t i = 0; i < glob_result.gl_pathc; ++i) {
matching_paths.push_back(std::string(glob_result.gl_pathv[i]));
}
globfree(&glob_result);
return matching_paths;
}
std::vector<std::string> expand_wildcard_paths(const std::vector<std::string>& paths)
{
std::vector<std::string> expanded_paths;
for (const auto& p : paths)
{
auto ep = expand_wildcard_path(p);
expanded_paths.insert(expanded_paths.end(), ep.begin(), ep.end());
}
return expanded_paths;
}
} // namespace
namespace bpftrace {
//'borrowed' from libbpf's bpf_core_find_kernel_btf
// from Andrii Nakryiko
const struct vmlinux_location vmlinux_locs[] = {
{ "/sys/kernel/btf/vmlinux", true },
{ "/boot/vmlinux-%1$s", false },
{ "/lib/modules/%1$s/vmlinux-%1$s", false },
{ "/lib/modules/%1$s/build/vmlinux", false },
{ "/usr/lib/modules/%1$s/kernel/vmlinux", false },
{ "/usr/lib/debug/boot/vmlinux-%1$s", false },
{ "/usr/lib/debug/boot/vmlinux-%1$s.debug", false },
{ "/usr/lib/debug/lib/modules/%1$s/vmlinux", false },
{ nullptr, false },
};
static bool pid_in_different_mountns(int pid);
static std::vector<std::string>
resolve_binary_path(const std::string &cmd, const char *env_paths, int pid);
void StdioSilencer::silence()
{
auto syserr = [](std::string msg) {
return std::system_error(errno, std::generic_category(), msg);
};
try
{
int fd = fileno(ofile);
if (fd < 0)
throw syserr("fileno()");
fflush(ofile);
if ((old_stdio_ = dup(fd)) < 0)
throw syserr("dup(fd)");
int new_stdio = -1;
if ((new_stdio = open("/dev/null", O_WRONLY)) < 0)
throw syserr("open(\"/dev/null\")");
if (dup2(new_stdio, fd) < 0)
throw syserr("dup2(new_stdio_, fd)");
close(new_stdio);
}
catch (const std::system_error &e)
{
if (errno == EMFILE)
LOG(FATAL) << e.what() << ": please raise NOFILE";
else
LOG(BUG) << e.what();
}
}
StdioSilencer::~StdioSilencer()
{
if (old_stdio_ == -1)
return;
auto syserr = [](std::string msg) {
return std::system_error(errno, std::generic_category(), msg);
};
try
{
int fd = fileno(ofile);
if (fd < 0)
throw syserr("fileno()");
fflush(ofile);
if (dup2(old_stdio_, fd) < 0)
throw syserr("dup2(old_stdio_)");
close(old_stdio_);
old_stdio_ = -1;
}
catch (const std::system_error &e)
{
LOG(BUG) << e.what();
}
}
bool get_uint64_env_var(const std::string &str, uint64_t &dest)
{
if (const char* env_p = std::getenv(str.c_str()))
{
std::istringstream stringstream(env_p);
if (!(stringstream >> dest))
{
LOG(ERROR) << "Env var '" << str
<< "' did not contain a valid uint64_t, or was zero-valued.";
return false;
}
}
return true;
}
bool get_bool_env_var(const std::string &str, bool &dest, bool neg)
{
if (const char *env_p = std::getenv(str.c_str()))
{
std::string s(env_p);
if (s == "1")
dest = !neg;
else if (s == "0")
dest = neg;
else
{
LOG(ERROR) << "Env var '" << str
<< "' did not contain a "
"valid value (0 or 1).";
return false;
}
}
return true;
}
std::string get_pid_exe(const std::string &pid)
{
std::error_code ec;
std_filesystem::path proc_path{ "/proc" };
proc_path /= pid;
proc_path /= "exe";
if (!std_filesystem::exists(proc_path, ec) ||
!std_filesystem::is_symlink(proc_path, ec))
return "";
return std_filesystem::read_symlink(proc_path).string();
}
std::string get_pid_exe(pid_t pid)
{
return get_pid_exe(std::to_string(pid));
}
bool has_wildcard(const std::string &str)
{
return str.find("*") != std::string::npos ||
(str.find("[") != std::string::npos &&
str.find("]") != std::string::npos);
}
std::vector<std::string> split_string(const std::string &str,
char delimiter,
bool remove_empty)
{
std::vector<std::string> elems;
std::stringstream ss(str);
std::string value;
while(std::getline(ss, value, delimiter)) {
if (remove_empty && value.empty())
continue;
elems.push_back(value);
}
return elems;
}
/// Erase prefix up to the first colon (:) from str and return the prefix
std::string erase_prefix(std::string &str)
{
std::string prefix = str.substr(0, str.find(':'));
str.erase(0, prefix.length() + 1);
return prefix;
}
bool wildcard_match(const std::string &str, std::vector<std::string> &tokens, bool start_wildcard, bool end_wildcard) {
size_t next = 0;
if (!start_wildcard)
if (str.find(tokens[0], next) != next)
return false;
for (std::string token : tokens) {
size_t found = str.find(token, next);
if (found == std::string::npos)
return false;
next = found + token.length();
}
if (!end_wildcard)
if (str.length() != next)
return false;
return true;
}
/*
* Splits input string by '*' delimiter and return the individual parts.
* Sets start_wildcard and end_wildcard if input starts or ends with '*'.
*/
std::vector<std::string> get_wildcard_tokens(const std::string &input,
bool &start_wildcard,
bool &end_wildcard)
{
if (input.empty())
return {};
start_wildcard = input[0] == '*';
end_wildcard = input[input.length() - 1] == '*';
std::vector<std::string> tokens = split_string(input, '*');
tokens.erase(std::remove(tokens.begin(), tokens.end(), ""), tokens.end());
return tokens;
}
std::vector<int> get_online_cpus()
{
return read_cpu_range("/sys/devices/system/cpu/online");
}
std::vector<int> get_possible_cpus()
{
return read_cpu_range("/sys/devices/system/cpu/possible");
}
std::vector<std::string> get_kernel_cflags(
const char* uname_machine,
const std::string& ksrc,
const std::string& kobj)
{
std::vector<std::string> cflags;
std::string arch = uname_machine;
const char *archenv;
if (!strncmp(uname_machine, "x86_64", 6)) {
arch = "x86";
} else if (uname_machine[0] == 'i' && !strncmp(&uname_machine[2], "86", 2)) {
arch = "x86";
} else if (!strncmp(uname_machine, "arm", 3)) {
arch = "arm";
} else if (!strncmp(uname_machine, "sa110", 5)) {
arch = "arm";
} else if (!strncmp(uname_machine, "s390x", 5)) {
arch = "s390";
} else if (!strncmp(uname_machine, "parisc64", 8)) {
arch = "parisc";
} else if (!strncmp(uname_machine, "ppc", 3)) {
arch = "powerpc";
} else if (!strncmp(uname_machine, "mips", 4)) {
arch = "mips";
} else if (!strncmp(uname_machine, "sh", 2)) {
arch = "sh";
} else if (!strncmp(uname_machine, "aarch64", 7)) {
arch = "arm64";
}
// If ARCH env is defined, use it over uname
archenv = getenv("ARCH");
if (archenv)
arch = std::string(archenv);
cflags.push_back("-nostdinc");
cflags.push_back("-isystem");
cflags.push_back("/virtual/lib/clang/include");
// see linux/Makefile for $(LINUXINCLUDE) + $(USERINCLUDE)
cflags.push_back("-I" + ksrc + "/arch/"+arch+"/include");
cflags.push_back("-I" + kobj + "/arch/"+arch+"/include/generated");
cflags.push_back("-I" + ksrc + "/include");
cflags.push_back("-I" + kobj + "/include");
cflags.push_back("-I" + ksrc + "/arch/"+arch+"/include/uapi");
cflags.push_back("-I" + kobj + "/arch/"+arch+"/include/generated/uapi");
cflags.push_back("-I" + ksrc + "/include/uapi");
cflags.push_back("-I" + kobj + "/include/generated/uapi");
cflags.push_back("-include");
cflags.push_back(ksrc + "/include/linux/kconfig.h");
cflags.push_back("-D__KERNEL__");
cflags.push_back("-D__BPF_TRACING__");
cflags.push_back("-D__HAVE_BUILTIN_BSWAP16__");
cflags.push_back("-D__HAVE_BUILTIN_BSWAP32__");
cflags.push_back("-D__HAVE_BUILTIN_BSWAP64__");
cflags.push_back("-DKBUILD_MODNAME=\"bpftrace\"");
// If ARCH env variable is set, pass this along.
if (archenv)
cflags.push_back("-D__TARGET_ARCH_" + arch);
if (arch == "arm")
{
// Required by several header files in arch/arm/include
cflags.push_back("-D__LINUX_ARM_ARCH__=7");
}
return cflags;
}
std::string get_cgroup_path_in_hierarchy(uint64_t cgroupid,
std::string base_path)
{
static std::map<std::pair<uint64_t, std::string>, std::string> path_cache;
struct stat path_st;
auto cached_path = path_cache.find({ cgroupid, base_path });
if (cached_path != path_cache.end() &&
stat(cached_path->second.c_str(), &path_st) >= 0 &&
path_st.st_ino == cgroupid)
return cached_path->second;
// Check for root cgroup path separately, since recursive_directory_iterator
// does not iterate over base directory
if (stat(base_path.c_str(), &path_st) >= 0 && path_st.st_ino == cgroupid)
{
path_cache[{ cgroupid, base_path }] = "/";
return "/";
}
for (auto &path_iter :
std_filesystem::recursive_directory_iterator(base_path))
{
if (stat(path_iter.path().c_str(), &path_st) < 0)
return "";
if (path_st.st_ino == cgroupid)
{
// Base directory is not a part of cgroup path
path_cache[{ cgroupid, base_path }] = path_iter.path().string().substr(
base_path.length());
return path_cache[{ cgroupid, base_path }];
}
}
return "";
}
std::vector<std::pair<std::string, std::string>> get_cgroup_hierarchy_roots()
{
// Get all cgroup mounts and their type (cgroup/cgroup2) from /proc/mounts
std::ifstream mounts_file("/proc/mounts");
std::vector<std::pair<std::string, std::string>> result;
const std::regex cgroup_mount_regex("(cgroup[2]?) (\\S*)[ ]?.*");
for (std::string line; std::getline(mounts_file, line);)
{
std::smatch match;
if (std::regex_match(line, match, cgroup_mount_regex))
{
result.push_back({ match[1].str(), match[2].str() });
}
}
mounts_file.close();
return result;
}
std::vector<std::pair<std::string, std::string>> get_cgroup_paths(
uint64_t cgroupid,
std::string filter)
{
// TODO: Rewrite using std::views when C++20 support becomes common
auto roots = get_cgroup_hierarchy_roots();
// Replace cgroup version with cgroup mount point directory name for cgroupv1
// roots and "unified" for cgroupv2 roots
for (auto &root : roots)
{
if (root.first == "cgroup")
{
root = { std_filesystem::path(root.second).filename().string(),
root.second };
}
else if (root.first == "cgroup2")
{
root = { "unified", root.second };
}
}
// Filter roots
bool start_wildcard, end_wildcard;
auto tokens = get_wildcard_tokens(filter, start_wildcard, end_wildcard);
std::vector<std::pair<std::string, std::string>> filtered_roots;
std::copy_if(roots.begin(),
roots.end(),
std::back_inserter(filtered_roots),
[&tokens, &start_wildcard, &end_wildcard](auto &pair) {
return wildcard_match(
pair.first, tokens, start_wildcard, end_wildcard);
});
// Get cgroup path for each root
std::vector<std::pair<std::string, std::string>> result;
std::transform(filtered_roots.begin(),
filtered_roots.end(),
std::back_inserter(result),
[&cgroupid](auto &pair) {
return std::pair<std::string, std::string>{
pair.first,
get_cgroup_path_in_hierarchy(cgroupid, pair.second)
};
});
// Sort paths lexically by name (with the exception of unified, which always
// comes first)
std::sort(result.begin(), result.end(), [](auto &pair1, auto &pair2) {
if (pair1.first == "unified")
return true;
else if (pair2.first == "unified")
return false;
else
return pair1.first < pair2.first;
});
return result;
}
bool is_dir(const std::string& path)
{
std::error_code ec;
std_filesystem::path buf{ path };
return std_filesystem::is_directory(buf, ec);
}
namespace {
struct KernelHeaderTmpDir {
KernelHeaderTmpDir(const std::string& prefix) : path{prefix + "XXXXXX"}
{
if (::mkdtemp(&path[0]) == nullptr) {
throw std::runtime_error("creating temporary path for kheaders.tar.xz failed");
}
}
~KernelHeaderTmpDir()
{
if (path.size() > 0) {
// move_to either did not succeed or did not run, so clean up after ourselves
exec_system(("rm -rf " + path).c_str());
}
}
void move_to(const std::string& new_path)
{
int err = ::rename(path.c_str(), new_path.c_str());
if (err == 0) {
path = "";
}
}
std::string path;
};
std::string unpack_kheaders_tar_xz(const struct utsname& utsname)
{
std::error_code ec;
std_filesystem::path path_prefix{ "/tmp" };
std_filesystem::path path_kheaders{ "/sys/kernel/kheaders.tar.xz" };
if (const char* tmpdir = ::getenv("TMPDIR")) {
path_prefix = tmpdir;
}
path_prefix /= "kheaders-";
std_filesystem::path shared_path{ path_prefix.string() + utsname.release };
if (std_filesystem::exists(shared_path, ec))
{
// already unpacked
return shared_path.string();
}
if (!std_filesystem::exists(path_kheaders, ec))
{
StderrSilencer silencer;
silencer.silence();
FILE* modprobe = ::popen("modprobe kheaders", "w");
if (modprobe == nullptr || pclose(modprobe) != 0) {
return "";
}
if (!std_filesystem::exists(path_kheaders, ec))
{
return "";
}
}
KernelHeaderTmpDir tmpdir{path_prefix};
FILE* tar = ::popen(("tar xf /sys/kernel/kheaders.tar.xz -C " + tmpdir.path).c_str(), "w");
if (!tar) {
return "";
}
int rc = ::pclose(tar);
if (rc == 0) {
tmpdir.move_to(shared_path);
return shared_path;
}
return "";
}
} // namespace
// get_kernel_dirs returns {ksrc, kobj} - directories for pristine and
// generated kernel sources.
//
// When the kernel was built in its source tree ksrc == kobj, however when
// the kernel was build in a different directory than its source, ksrc != kobj.
//
// A notable example is Debian, which places pristine kernel headers in
//
// /lib/modules/`uname -r`/source/
//
// and generated kernel headers in
//
// /lib/modules/`uname -r`/build/
//
// {"", ""} is returned if no trace of kernel headers was found at all.
// Both ksrc and kobj are guaranteed to be != "", if at least some trace of kernel sources was found.
std::tuple<std::string, std::string> get_kernel_dirs(
const struct utsname &utsname,
bool unpack_kheaders)
{
#ifdef KERNEL_HEADERS_DIR
return {KERNEL_HEADERS_DIR, KERNEL_HEADERS_DIR};
#endif
const char *kpath_env = ::getenv("BPFTRACE_KERNEL_SOURCE");
if (kpath_env)
{
const char *kpath_build_env = ::getenv("BPFTRACE_KERNEL_BUILD");
if (!kpath_build_env)
{
kpath_build_env = kpath_env;
}
return std::make_tuple(kpath_env, kpath_build_env);
}
std::string kdir = std::string("/lib/modules/") + utsname.release;
auto ksrc = kdir + "/source";
auto kobj = kdir + "/build";
// if one of source/ or build/ is not present - try to use the other one for both.
if (!is_dir(ksrc)) {
ksrc = "";
}
if (!is_dir(kobj)) {
kobj = "";
}
if (ksrc.empty() && kobj.empty())
{
if (unpack_kheaders)
{
const auto kheaders_tar_xz_path = unpack_kheaders_tar_xz(utsname);
if (kheaders_tar_xz_path.size() > 0)
return std::make_tuple(kheaders_tar_xz_path, kheaders_tar_xz_path);
}
return std::make_tuple("", "");
}
if (ksrc.empty())
{
ksrc = kobj;
}
else if (kobj.empty())
{
kobj = ksrc;
}
return std::make_tuple(ksrc, kobj);
}
const std::string &is_deprecated(const std::string &str)
{
std::vector<DeprecatedName>::iterator item;
for (item = DEPRECATED_LIST.begin(); item != DEPRECATED_LIST.end(); item++)
{
if (str == item->old_name)
{
if (item->show_warning)
{
LOG(WARNING) << item->old_name
<< " is deprecated and will be removed in the future. Use "
<< item->new_name << " instead.";
item->show_warning = false;
}
return item->new_name;
}
}
return str;
}
bool is_unsafe_func(const std::string &func_name)
{
return std::any_of(
UNSAFE_BUILTIN_FUNCS.begin(),
UNSAFE_BUILTIN_FUNCS.end(),
[&](const auto& cand) {
return func_name == cand;
});
}
bool is_compile_time_func(const std::string &func_name)
{
return std::any_of(COMPILE_TIME_FUNCS.begin(),
COMPILE_TIME_FUNCS.end(),
[&](const auto &cand) { return func_name == cand; });
}
std::string exec_system(const char* cmd)
{
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
/*
Original resolve_binary_path API defaulting to bpftrace's mount namespace
*/
std::vector<std::string> resolve_binary_path(const std::string& cmd)
{
const char *env_paths = getenv("PATH");
return resolve_binary_path(cmd, env_paths, -1);
}
/*
If a pid is specified, the binary path is taken relative to its own PATH if
it is in a different mount namespace. Otherwise, the path is resolved relative
to the local PATH env var for bpftrace's own mount namespace if it is set
*/
std::vector<std::string> resolve_binary_path(const std::string &cmd, int pid)
{
std::string env_paths = "";
std::ostringstream pid_environ_path;
if (pid > 0 && pid_in_different_mountns(pid))
{
pid_environ_path << "/proc/" << pid << "/environ";
std::ifstream environ(pid_environ_path.str());
if (environ)
{
std::string env_var;
std::string pathstr = ("PATH=");
while (std::getline(environ, env_var, '\0'))
{
if (env_var.find(pathstr) != std::string::npos)
{
env_paths = env_var.substr(pathstr.length());
break;
}
}
}
return resolve_binary_path(cmd, env_paths.c_str(), pid);
}
else
{
return resolve_binary_path(cmd, getenv("PATH"), pid);
}
}
/*
Private interface to resolve_binary_path, used for the exposed variants above,
allowing for a PID whose mount namespace should be optionally considered.
*/
static std::vector<std::string>
resolve_binary_path(const std::string &cmd, const char *env_paths, int pid)
{
std::vector<std::string> candidate_paths = { cmd };
if (env_paths != nullptr && cmd.find("/") == std::string::npos)
for (const auto& path : split_string(env_paths, ':'))
candidate_paths.push_back(path + "/" + cmd);
if (cmd.find("*") != std::string::npos)
candidate_paths = ::expand_wildcard_paths(candidate_paths);
std::vector<std::string> valid_executable_paths;
for (const auto &path : candidate_paths)
{
std::string rel_path;
if (pid > 0 && pid_in_different_mountns(pid))
rel_path = path_for_pid_mountns(pid, path);
else
rel_path = path;
if (bcc_elf_is_exe(rel_path.c_str()) ||
bcc_elf_is_shared_obj(rel_path.c_str()))
valid_executable_paths.push_back(rel_path);
}
return valid_executable_paths;
}
std::string path_for_pid_mountns(int pid, const std::string &path)
{
std::ostringstream pid_relative_path;
char pid_root[64];
snprintf(pid_root, sizeof(pid_root), "/proc/%d/root", pid);
if (path.find(pid_root) != 0)
{
std::string sep = (path.length() >= 1 && path.at(0) == '/') ? "" : "/";
pid_relative_path << pid_root << sep << path;
}
else
{
// The path is already relative to the pid's root
pid_relative_path << path;
}
return pid_relative_path.str();
}
/*
Determines if the target process is in a different mount namespace from
bpftrace.
If a process is in a different mount namespace (eg, container) it is very
likely that any references to local paths will not be valid, and that paths
need to be made relative to the PID.
If an invalid PID is specified or doesn't exist, it returns false.
True is only returned if the namespace of the target process could be read and
it doesn't match that of bpftrace. If there was an error reading either mount
namespace, it will throw an exception
*/
static bool pid_in_different_mountns(int pid)
{
if (pid <= 0)
return false;
std::error_code ec;
std_filesystem::path self_path{ "/proc/self/ns/mnt" };
std_filesystem::path target_path{ "/proc" };
target_path /= std::to_string(pid);
target_path /= "ns/mnt";
if (!std_filesystem::exists(self_path, ec))
{
throw MountNSException(
"Failed to compare mount ns with PID " + std::to_string(pid) +
". The error was open (/proc/self/ns/mnt): " + ec.message());
}
if (!std_filesystem::exists(target_path, ec))
{
throw MountNSException(
"Failed to compare mount ns with PID " + std::to_string(pid) +
". The error was open (/proc/<pid>/ns/mnt): " + ec.message());
}
bool result = !std_filesystem::equivalent(self_path, target_path, ec);
if (ec)
{
throw MountNSException("Failed to compare mount ns with PID " +
std::to_string(pid) +
". The error was (fstat): " + ec.message());
}
return result;
}
void cat_file(const char *filename, size_t max_bytes, std::ostream &out)
{
std::ifstream file(filename);
const size_t BUFSIZE = 4096;
if (file.fail()){
LOG(ERROR) << "failed to open file '" << filename
<< "': " << strerror(errno);
return;
}
char buf[BUFSIZE];
size_t bytes_read = 0;
// Read the file batches to avoid allocating a potentially
// massive buffer.
while (bytes_read < max_bytes) {
size_t size = std::min(BUFSIZE, max_bytes - bytes_read);
file.read(buf, size);
out.write(buf, file.gcount());
if (file.eof()) {
return;
}
if (file.fail()) {
LOG(ERROR) << "failed to open file '" << filename
<< "': " << strerror(errno);
return;
}
bytes_read += file.gcount();
}
}
std::string str_join(const std::vector<std::string> &list, const std::string &delim)
{
std::string str;
bool first = true;
for (const auto &elem : list)
{
if (first)
first = false;
else
str += delim;
str += elem;
}
return str;
}
bool is_numeric(const std::string &s)
{
std::size_t idx;
try
{
std::stoll(s, &idx, 0);
}
catch (...)
{
return false;
}
return idx == s.size();
}
bool symbol_has_cpp_mangled_signature(const std::string &sym_name)
{
if (!sym_name.rfind("_Z", 0) || !sym_name.rfind("____Z", 0))
return true;
else
return false;
}
pid_t parse_pid(const std::string &str)
{
try
{
constexpr ssize_t pid_max = 4 * 1024 * 1024;
std::size_t idx = 0;
auto pid = std::stol(str, &idx, 10);
// Detect cases like `13ABC`
if (idx < str.size())
throw InvalidPIDException(str, "is not a valid decimal number");
if (pid < 1 || pid > pid_max)
throw InvalidPIDException(str,
"out of valid pid range [1," +
std::to_string(pid_max) + "]");
return pid;
}
catch (const std::out_of_range &e)
{
throw InvalidPIDException(str, "outside of integer range");
}
catch (const std::invalid_argument &e)
{
throw InvalidPIDException(str, "is not a valid decimal number");
}
}
std::string hex_format_buffer(const char *buf,
size_t size,
bool keep_ascii,
bool escape_hex)
{
// Allow enough space for every byte to be sanitized in the form "\x00"
char s[size * 4 + 1];
size_t offset = 0;
for (size_t i = 0; i < size; i++)
if (keep_ascii && buf[i] >= 32 && buf[i] <= 126)
offset += sprintf(s + offset, "%c", ((const uint8_t *)buf)[i]);
else if (escape_hex)
offset += sprintf(s + offset, "\\x%02x", ((const uint8_t *)buf)[i]);
else
offset += sprintf(s + offset,
i == size - 1 ? "%02x" : "%02x ",
((const uint8_t *)buf)[i]);
s[offset] = '\0';
return std::string(s);
}
FuncsModulesMap get_traceable_funcs()
{
#ifdef FUZZ
return {};
#else
// Try to get the list of functions from BPFTRACE_AVAILABLE_FUNCTIONS_TEST env
const char *path_env = std::getenv("BPFTRACE_AVAILABLE_FUNCTIONS_TEST");
const std::string kprobe_path = path_env
? path_env
: tracefs::available_filter_functions();
std::ifstream available_funs(kprobe_path);
if (available_funs.fail())
{
if (bt_debug != DebugLevel::kNone)
{
std::cerr << "Error while reading traceable functions from "
<< kprobe_path << ": " << strerror(errno);
}
return {};
}