keep 'enum bpf_attach_type' inside libbpf.h, renamed it to bpf_probe_attach_type
authorDerek <“derek0883@gmail.com”>
Tue, 31 Jan 2017 05:03:02 +0000 (21:03 -0800)
committerDerek <“derek0883@gmail.com”>
Tue, 31 Jan 2017 05:03:02 +0000 (21:03 -0800)
using static buf size in libbpf.c. for uprobe, set buf size to PATH_MAX

src/cc/BPF.cc
src/cc/BPF.h
src/cc/libbpf.c
src/cc/libbpf.h

index 75e8093..9e1c23a 100644 (file)
@@ -147,7 +147,7 @@ StatusTuple BPF::detach_all() {
 
 StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
                                const std::string& probe_func,
-                               int attach_type,
+                               bpf_probe_attach_type attach_type,
                                pid_t pid, int cpu, int group_fd,
                                perf_reader_cb cb, void* cb_cookie) {
   std::string probe_event = get_kprobe_event(kernel_func, attach_type);
@@ -179,7 +179,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
                                const std::string& symbol,
                                const std::string& probe_func,
                                uint64_t symbol_addr,
-                               int attach_type,
+                               bpf_probe_attach_type attach_type,
                                pid_t pid, int cpu, int group_fd,
                                perf_reader_cb cb, void* cb_cookie) {
   bcc_symbol sym = bcc_symbol();
@@ -318,7 +318,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
 }
 
 StatusTuple BPF::detach_kprobe(const std::string& kernel_func,
-                               int attach_type) {
+                               bpf_probe_attach_type attach_type) {
   std::string event = get_kprobe_event(kernel_func, attach_type);
 
   auto it = kprobes_.find(event);
@@ -334,7 +334,7 @@ StatusTuple BPF::detach_kprobe(const std::string& kernel_func,
 
 StatusTuple BPF::detach_uprobe(const std::string& binary_path,
                                const std::string& symbol, uint64_t symbol_addr,
-                               int attach_type) {
+                               bpf_probe_attach_type attach_type) {
   bcc_symbol sym = bcc_symbol();
   TRY2(check_binary_symbol(binary_path, symbol, symbol_addr, &sym));
 
@@ -416,7 +416,7 @@ void BPF::poll_perf_buffer(const std::string& name, int timeout) {
 }
 
 StatusTuple BPF::load_func(const std::string& func_name,
-                           enum bpf_prog_type type, int& fd) {
+                           bpf_prog_type type, int& fd) {
   if (funcs_.find(func_name) != funcs_.end()) {
     fd = funcs_[func_name];
     return StatusTuple(0);
@@ -466,14 +466,14 @@ StatusTuple BPF::check_binary_symbol(const std::string& binary_path,
 }
 
 std::string BPF::get_kprobe_event(const std::string& kernel_func,
-                                  int type) {
+                                  bpf_probe_attach_type type) {
   std::string res = attach_type_prefix(type) + "_";
   res += sanitize_str(kernel_func, &BPF::kprobe_event_validator);
   return res;
 }
 
 std::string BPF::get_uprobe_event(const std::string& binary_path,
-                                  uint64_t offset, int type) {
+                                  uint64_t offset, bpf_probe_attach_type type) {
   std::string res = attach_type_prefix(type) + "_";
   res += sanitize_str(binary_path, &BPF::uprobe_path_validator);
   res += "_0x" + uint_to_hex(offset);
index b1b8937..d45ef51 100644 (file)
@@ -51,23 +51,23 @@ public:
 
   StatusTuple attach_kprobe(
       const std::string& kernel_func, const std::string& probe_func,
-      int attach_type = BPF_PROBE_ENTRY,
+      bpf_probe_attach_type = probe_entry,
       pid_t pid = -1, int cpu = 0, int group_fd = -1,
       perf_reader_cb cb = nullptr, void* cb_cookie = nullptr);
   StatusTuple detach_kprobe(
       const std::string& kernel_func,
-      int attach_type = BPF_PROBE_ENTRY);
+      bpf_probe_attach_type attach_type = probe_entry);
 
   StatusTuple attach_uprobe(
       const std::string& binary_path, const std::string& symbol,
       const std::string& probe_func, uint64_t symbol_addr = 0,
-      int attach_type = BPF_PROBE_ENTRY,
+      bpf_probe_attach_type attach_type = probe_entry,
       pid_t pid = -1, int cpu = 0, int group_fd = -1,
       perf_reader_cb cb = nullptr, void* cb_cookie = nullptr);
   StatusTuple detach_uprobe(
       const std::string& binary_path, const std::string& symbol,
       uint64_t symbol_addr = 0,
-      int attach_type = BPF_PROBE_ENTRY);
+      bpf_probe_attach_type attach_type = probe_entry);
   StatusTuple attach_usdt(const USDT& usdt, pid_t pid = -1, int cpu = 0,
                           int group_fd = -1);
   StatusTuple detach_usdt(const USDT& usdt);
@@ -106,9 +106,9 @@ private:
   StatusTuple unload_func(const std::string& func_name);
 
   std::string get_kprobe_event(const std::string& kernel_func,
-                               int attach_type);
+                               bpf_probe_attach_type type);
   std::string get_uprobe_event(const std::string& binary_path, uint64_t offset,
-                               int attach_type);
+                               bpf_probe_attach_type type);
 
   StatusTuple detach_kprobe_event(const std::string& event, open_probe_t& attr);
   StatusTuple detach_uprobe_event(const std::string& event, open_probe_t& attr);
@@ -116,21 +116,21 @@ private:
                                       open_probe_t& attr);
   StatusTuple detach_perf_event_all_cpu(open_probe_t& attr);
 
-  std::string attach_type_debug(int attach_type) {
-    switch (attach_type) {
-    case BPF_PROBE_ENTRY:
+  std::string attach_type_debug(bpf_probe_attach_type type) {
+    switch (type) {
+    case probe_entry:
       return "";
-    case BPF_PROBE_RETURN:
+    case probe_return:
       return "return ";
     }
     return "ERROR";
   }
 
-  std::string attach_type_prefix(int attach_type) {
-    switch (attach_type) {
-    case BPF_PROBE_ENTRY:
+  std::string attach_type_prefix(bpf_probe_attach_type type) {
+    switch (type) {
+    case probe_entry:
       return "p";
-    case BPF_PROBE_RETURN:
+    case probe_return:
       return "r";
     }
     return "ERROR";
index d7ca6d6..d8caf69 100644 (file)
@@ -338,16 +338,17 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path,
   return 0;
 }
 
-void * bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
                         const char *fn_name,
                         pid_t pid, int cpu, int group_fd,
                         perf_reader_cb cb, void *cb_cookie) 
 {
   int kfd;
-  char buf[strlen(ev_name)+256];
-  char new_name[strlen(ev_name)+32];
+  char buf[256];
+  char new_name[128];
   struct perf_reader *reader = NULL;
   static char *event_type = "kprobe";
+  int n;
 
   snprintf(new_name, sizeof(new_name), "%s_bcc_%d", ev_name, getpid());
   reader = perf_reader_new(cb, NULL, cb_cookie);
@@ -375,11 +376,10 @@ void * bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
     snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s", new_name);
     if (mkdir(buf, 0755) == -1) 
       goto retry;
-    snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s/events/%ss/%s", 
+    n = snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s/events/%ss/%s", 
              new_name, event_type, new_name);
-    if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) == 0) {
+    if (n < sizeof(buf) && bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) == 0)
          goto out;
-    }
     snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s", new_name);
     rmdir(buf);
   }
@@ -396,16 +396,17 @@ error:
 
 }
 
-void * bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
                         const char *binary_path, uint64_t offset,
                         pid_t pid, int cpu, int group_fd,
                         perf_reader_cb cb, void *cb_cookie) 
 {
   int kfd;
-  char buf[strlen(binary_path)+strlen(ev_name)+256];
-  char new_name[strlen(ev_name)+32];
+  char buf[PATH_MAX];
+  char new_name[128];
   struct perf_reader *reader = NULL;
   static char *event_type = "uprobe";
+  int n;
 
   snprintf(new_name, sizeof(new_name), "%s_bcc_%d", ev_name, getpid());
   reader = perf_reader_new(cb, NULL, cb_cookie);
@@ -419,8 +420,12 @@ void * bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name,
     goto error;
   }
 
-  snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r', 
+  n = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r', 
                        event_type, new_name, binary_path, offset);
+  if (n >= sizeof(buf)) {
+    close(kfd);
+    goto error;
+  }
   if (write(kfd, buf, strlen(buf)) < 0) {
     if (errno == EINVAL)
       fprintf(stderr, "check dmesg output for possible cause\n");
@@ -443,7 +448,7 @@ error:
 static int bpf_detach_probe(const char *ev_name, const char *event_type)
 {
   int kfd;
-  char buf[strlen(ev_name)+256];
+  char buf[256];
   snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type);
   kfd = open(buf, O_WRONLY | O_APPEND, 0);
   if (kfd < 0) {
@@ -464,7 +469,7 @@ static int bpf_detach_probe(const char *ev_name, const char *event_type)
 
 int bpf_detach_kprobe(const char *ev_name)
 {
-  char buf[strlen(ev_name)+256];
+  char buf[256];
   int ret = bpf_detach_probe(ev_name, "kprobe");
   snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s_bcc_%d", ev_name, getpid());
   if (access(buf, F_OK) != -1) {
index fdc5fcf..34787b6 100644 (file)
 extern "C" {
 #endif
 
+enum bpf_probe_attach_type {
+       probe_entry,
+       probe_return
+};
 #define BPF_PROBE_ENTRY  0
 #define BPF_PROBE_RETURN 1
 
@@ -47,14 +51,14 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num,
                                void *callchain);
 typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size);
 
-void * bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
                         const char *fn_name,
                         pid_t pid, int cpu, int group_fd,
                         perf_reader_cb cb, void *cb_cookie);
 
 int bpf_detach_kprobe(const char *ev_name);
 
-void * bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
                         const char *binary_path, uint64_t offset,
                         pid_t pid, int cpu, int group_fd,
                         perf_reader_cb cb, void *cb_cookie);