BPF.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright (c) 2016 Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <cctype>
  18. #include <cstdint>
  19. #include <memory>
  20. #include <ostream>
  21. #include <string>
  22. #include "BPFTable.h"
  23. #include "bcc_exception.h"
  24. #include "bcc_syms.h"
  25. #include "bpf_module.h"
  26. #include "linux/bpf.h"
  27. #include "libbpf.h"
  28. #include "table_storage.h"
  29. static const int DEFAULT_PERF_BUFFER_PAGE_CNT = 8;
  30. namespace ebpf {
  31. struct open_probe_t {
  32. int perf_event_fd;
  33. std::string func;
  34. std::vector<std::pair<int, int>>* per_cpu_fd;
  35. };
  36. class BPF;
  37. class USDT {
  38. public:
  39. USDT(const std::string& binary_path, const std::string& provider,
  40. const std::string& name, const std::string& probe_func);
  41. USDT(pid_t pid, const std::string& provider, const std::string& name,
  42. const std::string& probe_func);
  43. USDT(const std::string& binary_path, pid_t pid, const std::string& provider,
  44. const std::string& name, const std::string& probe_func);
  45. USDT(const USDT& usdt);
  46. USDT(USDT&& usdt) noexcept;
  47. const std::string &binary_path() const { return binary_path_; }
  48. pid_t pid() const { return pid_; }
  49. const std::string &provider() const { return provider_; }
  50. const std::string &name() const { return name_; }
  51. const std::string &probe_func() const { return probe_func_; }
  52. StatusTuple init();
  53. bool operator==(const USDT& other) const;
  54. std::string print_name() const {
  55. return provider_ + ":" + name_ + " from binary " + binary_path_ + " PID " +
  56. std::to_string(pid_) + " for probe " + probe_func_;
  57. }
  58. friend std::ostream& operator<<(std::ostream& out, const USDT& usdt) {
  59. return out << usdt.provider_ << ":" << usdt.name_ << " from binary "
  60. << usdt.binary_path_ << " PID " << usdt.pid_ << " for probe "
  61. << usdt.probe_func_;
  62. }
  63. // When the kludge flag is set to 1 (default), we will only match on inode
  64. // when searching for modules in /proc/PID/maps that might contain the
  65. // tracepoint we're looking for.
  66. // By setting this to 0, we will match on both inode and
  67. // (dev_major, dev_minor), which is a more accurate way to uniquely
  68. // identify a file, but may fail depending on the filesystem backing the
  69. // target file (see bcc#2715)
  70. //
  71. // This hack exists because btrfs and overlayfs report different device
  72. // numbers for files in /proc/PID/maps vs stat syscall. Don't use it unless
  73. // you've had issues with inode collisions. Both btrfs and overlayfs are
  74. // known to require inode-only resolution to accurately match a file.
  75. //
  76. // set_probe_matching_kludge(0) must be called before USDTs are submitted to
  77. // BPF::init()
  78. int set_probe_matching_kludge(uint8_t kludge);
  79. private:
  80. bool initialized_;
  81. std::string binary_path_;
  82. pid_t pid_;
  83. std::string provider_;
  84. std::string name_;
  85. std::string probe_func_;
  86. std::unique_ptr<void, std::function<void(void*)>> probe_;
  87. std::string program_text_;
  88. uint8_t mod_match_inode_only_;
  89. friend class BPF;
  90. };
  91. class BPF {
  92. public:
  93. static const int BPF_MAX_STACK_DEPTH = 127;
  94. explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr,
  95. bool rw_engine_enabled = bpf_module_rw_engine_enabled(),
  96. const std::string &maps_ns = "",
  97. bool allow_rlimit = true)
  98. : flag_(flag),
  99. bsymcache_(NULL),
  100. bpf_module_(new BPFModule(flag, ts, rw_engine_enabled, maps_ns,
  101. allow_rlimit)) {}
  102. StatusTuple init(const std::string& bpf_program,
  103. const std::vector<std::string>& cflags = {},
  104. const std::vector<USDT>& usdt = {});
  105. StatusTuple init_usdt(const USDT& usdt);
  106. ~BPF();
  107. StatusTuple detach_all();
  108. StatusTuple attach_kprobe(const std::string& kernel_func,
  109. const std::string& probe_func,
  110. uint64_t kernel_func_offset = 0,
  111. bpf_probe_attach_type = BPF_PROBE_ENTRY,
  112. int maxactive = 0);
  113. StatusTuple detach_kprobe(
  114. const std::string& kernel_func,
  115. bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY);
  116. StatusTuple attach_uprobe(const std::string& binary_path,
  117. const std::string& symbol,
  118. const std::string& probe_func,
  119. uint64_t symbol_addr = 0,
  120. bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY,
  121. pid_t pid = -1,
  122. uint64_t symbol_offset = 0,
  123. uint32_t ref_ctr_offset = 0);
  124. StatusTuple detach_uprobe(const std::string& binary_path,
  125. const std::string& symbol, uint64_t symbol_addr = 0,
  126. bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY,
  127. pid_t pid = -1,
  128. uint64_t symbol_offset = 0);
  129. StatusTuple attach_usdt(const USDT& usdt, pid_t pid = -1);
  130. StatusTuple attach_usdt_all();
  131. StatusTuple detach_usdt(const USDT& usdt, pid_t pid = -1);
  132. StatusTuple detach_usdt_all();
  133. StatusTuple attach_tracepoint(const std::string& tracepoint,
  134. const std::string& probe_func);
  135. StatusTuple detach_tracepoint(const std::string& tracepoint);
  136. StatusTuple attach_raw_tracepoint(const std::string& tracepoint,
  137. const std::string& probe_func);
  138. StatusTuple detach_raw_tracepoint(const std::string& tracepoint);
  139. StatusTuple attach_perf_event(uint32_t ev_type, uint32_t ev_config,
  140. const std::string& probe_func,
  141. uint64_t sample_period, uint64_t sample_freq,
  142. pid_t pid = -1, int cpu = -1,
  143. int group_fd = -1);
  144. StatusTuple attach_perf_event_raw(void* perf_event_attr,
  145. const std::string& probe_func,
  146. pid_t pid = -1, int cpu = -1,
  147. int group_fd = -1,
  148. unsigned long extra_flags = 0);
  149. StatusTuple detach_perf_event(uint32_t ev_type, uint32_t ev_config);
  150. StatusTuple detach_perf_event_raw(void* perf_event_attr);
  151. std::string get_syscall_fnname(const std::string& name);
  152. const BPFModule *get_mod() {
  153. return bpf_module_.get();
  154. }
  155. /*php add*/
  156. size_t get_num_functions();
  157. /*php add*/
  158. const char *get_function_name(size_t id);
  159. BPFTable get_table(const std::string& name) {
  160. TableStorage::iterator it;
  161. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  162. return BPFTable(it->second);
  163. return BPFTable({});
  164. }
  165. template <class ValueType>
  166. BPFArrayTable<ValueType> get_array_table(const std::string& name) {
  167. TableStorage::iterator it;
  168. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  169. return BPFArrayTable<ValueType>(it->second);
  170. return BPFArrayTable<ValueType>({});
  171. }
  172. template <class ValueType>
  173. BPFPercpuArrayTable<ValueType> get_percpu_array_table(
  174. const std::string& name) {
  175. TableStorage::iterator it;
  176. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  177. return BPFPercpuArrayTable<ValueType>(it->second);
  178. return BPFPercpuArrayTable<ValueType>({});
  179. }
  180. template <class KeyType, class ValueType>
  181. BPFHashTable<KeyType, ValueType> get_hash_table(const std::string& name) {
  182. TableStorage::iterator it;
  183. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  184. return BPFHashTable<KeyType, ValueType>(it->second);
  185. return BPFHashTable<KeyType, ValueType>({});
  186. }
  187. template <class KeyType, class ValueType>
  188. BPFPercpuHashTable<KeyType, ValueType> get_percpu_hash_table(
  189. const std::string& name) {
  190. TableStorage::iterator it;
  191. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  192. return BPFPercpuHashTable<KeyType, ValueType>(it->second);
  193. return BPFPercpuHashTable<KeyType, ValueType>({});
  194. }
  195. template <class ValueType>
  196. BPFSkStorageTable<ValueType> get_sk_storage_table(const std::string& name) {
  197. TableStorage::iterator it;
  198. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  199. return BPFSkStorageTable<ValueType>(it->second);
  200. return BPFSkStorageTable<ValueType>({});
  201. }
  202. template <class ValueType>
  203. BPFInodeStorageTable<ValueType> get_inode_storage_table(const std::string& name) {
  204. TableStorage::iterator it;
  205. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  206. return BPFInodeStorageTable<ValueType>(it->second);
  207. return BPFInodeStorageTable<ValueType>({});
  208. }
  209. template <class ValueType>
  210. BPFTaskStorageTable<ValueType> get_task_storage_table(const std::string& name) {
  211. TableStorage::iterator it;
  212. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  213. return BPFTaskStorageTable<ValueType>(it->second);
  214. return BPFTaskStorageTable<ValueType>({});
  215. }
  216. template <class ValueType>
  217. BPFCgStorageTable<ValueType> get_cg_storage_table(const std::string& name) {
  218. TableStorage::iterator it;
  219. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  220. return BPFCgStorageTable<ValueType>(it->second);
  221. return BPFCgStorageTable<ValueType>({});
  222. }
  223. template <class ValueType>
  224. BPFPercpuCgStorageTable<ValueType> get_percpu_cg_storage_table(const std::string& name) {
  225. TableStorage::iterator it;
  226. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  227. return BPFPercpuCgStorageTable<ValueType>(it->second);
  228. return BPFPercpuCgStorageTable<ValueType>({});
  229. }
  230. template <class ValueType>
  231. BPFQueueStackTable<ValueType> get_queuestack_table(const std::string& name) {
  232. TableStorage::iterator it;
  233. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  234. return BPFQueueStackTable<ValueType>(it->second);
  235. return BPFQueueStackTable<ValueType>({});
  236. }
  237. void* get_bsymcache(void) {
  238. if (bsymcache_ == NULL) {
  239. bsymcache_ = bcc_buildsymcache_new();
  240. }
  241. return bsymcache_;
  242. }
  243. BPFProgTable get_prog_table(const std::string& name);
  244. BPFCgroupArray get_cgroup_array(const std::string& name);
  245. BPFDevmapTable get_devmap_table(const std::string& name);
  246. BPFXskmapTable get_xskmap_table(const std::string& name);
  247. BPFSockmapTable get_sockmap_table(const std::string& name);
  248. BPFSockhashTable get_sockhash_table(const std::string& name);
  249. BPFStackTable get_stack_table(const std::string& name,
  250. bool use_debug_file = true,
  251. bool check_debug_file_crc = true);
  252. BPFStackBuildIdTable get_stackbuildid_table(const std::string &name,
  253. bool use_debug_file = true,
  254. bool check_debug_file_crc = true);
  255. template <class KeyType>
  256. BPFMapInMapTable<KeyType> get_map_in_map_table(const std::string& name){
  257. TableStorage::iterator it;
  258. if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
  259. return BPFMapInMapTable<KeyType>(it->second);
  260. return BPFMapInMapTable<KeyType>({});
  261. }
  262. bool add_module(std::string module);
  263. StatusTuple open_perf_event(const std::string& name, uint32_t type,
  264. uint64_t config, int pid = -1);
  265. StatusTuple close_perf_event(const std::string& name);
  266. // Open a Perf Buffer of given name, providing callback and callback cookie
  267. // to use when polling. BPF class owns the opened Perf Buffer and will free
  268. // it on-demand or on destruction.
  269. StatusTuple open_perf_buffer(const std::string& name, perf_reader_raw_cb cb,
  270. perf_reader_lost_cb lost_cb = nullptr,
  271. void* cb_cookie = nullptr,
  272. int page_cnt = DEFAULT_PERF_BUFFER_PAGE_CNT);
  273. // Close and free the Perf Buffer of given name.
  274. StatusTuple close_perf_buffer(const std::string& name);
  275. // Obtain an pointer to the opened BPFPerfBuffer instance of given name.
  276. // Will return nullptr if such open Perf Buffer doesn't exist.
  277. BPFPerfBuffer* get_perf_buffer(const std::string& name);
  278. // Poll an opened Perf Buffer of given name with given timeout, using callback
  279. // provided when opening. Do nothing if such open Perf Buffer doesn't exist.
  280. // Returns:
  281. // -1 on error or if perf buffer with such name doesn't exist;
  282. // 0, if no data was available before timeout;
  283. // number of CPUs that have new data, otherwise.
  284. int poll_perf_buffer(const std::string& name, int timeout_ms = -1);
  285. StatusTuple load_func(const std::string& func_name, enum bpf_prog_type type,
  286. int& fd, unsigned flags = 0, enum bpf_attach_type = (bpf_attach_type) -1);
  287. StatusTuple unload_func(const std::string& func_name);
  288. StatusTuple attach_func(int prog_fd, int attachable_fd,
  289. enum bpf_attach_type attach_type,
  290. uint64_t flags);
  291. StatusTuple detach_func(int prog_fd, int attachable_fd,
  292. enum bpf_attach_type attach_type);
  293. int free_bcc_memory();
  294. private:
  295. std::string get_kprobe_event(const std::string& kernel_func,
  296. bpf_probe_attach_type type);
  297. std::string get_uprobe_event(const std::string& binary_path, uint64_t offset,
  298. bpf_probe_attach_type type, pid_t pid);
  299. StatusTuple attach_usdt_without_validation(const USDT& usdt, pid_t pid);
  300. StatusTuple detach_usdt_without_validation(const USDT& usdt, pid_t pid);
  301. StatusTuple detach_kprobe_event(const std::string& event, open_probe_t& attr);
  302. StatusTuple detach_uprobe_event(const std::string& event, open_probe_t& attr);
  303. StatusTuple detach_tracepoint_event(const std::string& tracepoint,
  304. open_probe_t& attr);
  305. StatusTuple detach_raw_tracepoint_event(const std::string& tracepoint,
  306. open_probe_t& attr);
  307. StatusTuple detach_perf_event_all_cpu(open_probe_t& attr);
  308. std::string attach_type_debug(bpf_probe_attach_type type) {
  309. switch (type) {
  310. case BPF_PROBE_ENTRY:
  311. return "";
  312. case BPF_PROBE_RETURN:
  313. return "return ";
  314. }
  315. return "ERROR";
  316. }
  317. std::string attach_type_prefix(bpf_probe_attach_type type) {
  318. switch (type) {
  319. case BPF_PROBE_ENTRY:
  320. return "p";
  321. case BPF_PROBE_RETURN:
  322. return "r";
  323. }
  324. return "ERROR";
  325. }
  326. static bool kprobe_event_validator(char c) {
  327. return (c != '+') && (c != '.');
  328. }
  329. static bool uprobe_path_validator(char c) {
  330. return std::isalpha(c) || std::isdigit(c) || (c == '_');
  331. }
  332. StatusTuple check_binary_symbol(const std::string& binary_path,
  333. const std::string& symbol,
  334. uint64_t symbol_addr, std::string& module_res,
  335. uint64_t& offset_res,pid_t pid,
  336. uint64_t symbol_offset = 0);
  337. void init_fail_reset();
  338. int flag_;
  339. void *bsymcache_;
  340. std::unique_ptr<std::string> syscall_prefix_;
  341. std::unique_ptr<BPFModule> bpf_module_;
  342. std::map<std::string, int> funcs_;
  343. std::vector<USDT> usdt_;
  344. std::string all_bpf_program_;
  345. std::map<std::string, open_probe_t> kprobes_;
  346. std::map<std::string, open_probe_t> uprobes_;
  347. std::map<std::string, open_probe_t> tracepoints_;
  348. std::map<std::string, open_probe_t> raw_tracepoints_;
  349. std::map<std::string, BPFPerfBuffer*> perf_buffers_;
  350. std::map<std::string, BPFPerfEventArray*> perf_event_arrays_;
  351. std::map<std::pair<uint32_t, uint32_t>, open_probe_t> perf_events_;
  352. };
  353. } // namespace ebpf