Add and unify helper for ELF type
authorTeng Qin <qinteng@fb.com>
Thu, 4 May 2017 19:09:14 +0000 (12:09 -0700)
committerTeng Qin <qinteng@fb.com>
Tue, 9 May 2017 08:18:46 +0000 (01:18 -0700)
src/cc/bcc_elf.c
src/cc/bcc_elf.h
src/cc/bcc_proc.c
src/cc/usdt.cc
src/cc/usdt_args.cc

index cb6c47ac18f99b82243d748acde35fc003f19de9..8540a605faf43515da5d3b5febd66efc31541417 100644 (file)
@@ -483,21 +483,31 @@ int bcc_elf_loadaddr(const char *path, uint64_t *address) {
   return res;
 }
 
-int bcc_elf_is_shared_obj(const char *path) {
+int bcc_elf_get_type(const char *path) {
   Elf *e;
   GElf_Ehdr hdr;
-  int fd, res = -1;
+  int fd;
+  void* res = NULL;
 
   if (openelf(path, &e, &fd) < 0)
     return -1;
 
-  if (gelf_getehdr(e, &hdr))
-    res = (hdr.e_type == ET_DYN);
-
+  res = (void*)gelf_getehdr(e, &hdr);
   elf_end(e);
   close(fd);
 
-  return res;
+  if (!res)
+    return -1;
+  else
+    return hdr.e_type;
+}
+
+int bcc_elf_is_exe(const char *path) {
+  return (bcc_elf_get_type(path) != -1) && (access(path, X_OK) == 0);
+}
+
+int bcc_elf_is_shared_obj(const char *path) {
+  return bcc_elf_get_type(path) == ET_DYN;
 }
 
 #if 0
index 7ee0fe76d093079703cea56ad78c0ae7d58c09a7..e87b037f81511a37ea5d592eba6e94119a1090ff 100644 (file)
@@ -41,7 +41,10 @@ int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback,
 int bcc_elf_loadaddr(const char *path, uint64_t *address);
 int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback,
                         void *payload);
+
+int bcc_elf_get_type(const char *path);
 int bcc_elf_is_shared_obj(const char *path);
+int bcc_elf_is_exe(const char *path);
 
 #ifdef __cplusplus
 }
index 60e58a2c6b430b948ce66a06e7e5f8e12074235b..12bbfc0066c030d779f7035d765aff6b6beb9fa6 100644 (file)
 #include "bcc_proc.h"
 #include "bcc_elf.h"
 
-static bool is_exe(const char *path) {
-  struct stat s;
-  if (access(path, X_OK) < 0)
-    return false;
-
-  if (stat(path, &s) < 0)
-    return false;
-
-  return S_ISREG(s.st_mode);
-}
-
 char *bcc_procutils_which(const char *binpath) {
   char buffer[4096];
   const char *PATH;
 
   if (strchr(binpath, '/'))
-    return is_exe(binpath) ? strdup(binpath) : 0;
+    return bcc_elf_is_exe(binpath) ? strdup(binpath) : 0;
 
   if (!(PATH = getenv("PATH")))
     return 0;
@@ -65,7 +54,7 @@ char *bcc_procutils_which(const char *binpath) {
       buffer[path_len] = '/';
       strcpy(buffer + path_len + 1, binpath);
 
-      if (is_exe(buffer))
+      if (bcc_elf_is_exe(buffer))
         return strdup(buffer);
     }
 
index ef929d7144283cb100b50219cdf363ff36a86909..a2427e75d0b1f09afea2eee03fc988c77af7a856 100644 (file)
@@ -48,7 +48,7 @@ Probe::Probe(const char *bin_path, const char *provider, const char *name,
 
 bool Probe::in_shared_object() {
   if (!in_shared_object_)
-    in_shared_object_ = (bcc_elf_is_shared_obj(bin_path_.c_str()) == 1);
+    in_shared_object_ = bcc_elf_is_shared_obj(bin_path_.c_str());
   return in_shared_object_.value();
 }
 
index 8d73403ddff27edb8e247e4ffe231598f6dea19f..e7d75c88b1a5708ba63a3199c254427bcbc89be5 100644 (file)
@@ -39,7 +39,7 @@ bool Argument::get_global_address(uint64_t *address, const std::string &binpath,
         .resolve_name(binpath.c_str(), deref_ident_->c_str(), address);
   }
 
-  if (bcc_elf_is_shared_obj(binpath.c_str()) == 0) {
+  if (!bcc_elf_is_shared_obj(binpath.c_str())) {
     struct bcc_symbol sym = {deref_ident_->c_str(), binpath.c_str(), 0x0};
     if (!bcc_find_symbol_addr(&sym) && sym.offset) {
       *address = sym.offset;