Commit d82e813e authored by Teng Qin's avatar Teng Qin

Add and unify helper for ELF type

parent b6b9c6db
...@@ -483,21 +483,31 @@ int bcc_elf_loadaddr(const char *path, uint64_t *address) { ...@@ -483,21 +483,31 @@ int bcc_elf_loadaddr(const char *path, uint64_t *address) {
return res; return res;
} }
int bcc_elf_is_shared_obj(const char *path) { int bcc_elf_get_type(const char *path) {
Elf *e; Elf *e;
GElf_Ehdr hdr; GElf_Ehdr hdr;
int fd, res = -1; int fd;
void* res = NULL;
if (openelf(path, &e, &fd) < 0) if (openelf(path, &e, &fd) < 0)
return -1; return -1;
if (gelf_getehdr(e, &hdr)) res = (void*)gelf_getehdr(e, &hdr);
res = (hdr.e_type == ET_DYN);
elf_end(e); elf_end(e);
close(fd); 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 #if 0
......
...@@ -41,7 +41,10 @@ int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback, ...@@ -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_loadaddr(const char *path, uint64_t *address);
int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback, int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback,
void *payload); void *payload);
int bcc_elf_get_type(const char *path);
int bcc_elf_is_shared_obj(const char *path); int bcc_elf_is_shared_obj(const char *path);
int bcc_elf_is_exe(const char *path);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -35,23 +35,12 @@ ...@@ -35,23 +35,12 @@
#include "bcc_proc.h" #include "bcc_proc.h"
#include "bcc_elf.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 *bcc_procutils_which(const char *binpath) {
char buffer[4096]; char buffer[4096];
const char *PATH; const char *PATH;
if (strchr(binpath, '/')) if (strchr(binpath, '/'))
return is_exe(binpath) ? strdup(binpath) : 0; return bcc_elf_is_exe(binpath) ? strdup(binpath) : 0;
if (!(PATH = getenv("PATH"))) if (!(PATH = getenv("PATH")))
return 0; return 0;
...@@ -65,7 +54,7 @@ char *bcc_procutils_which(const char *binpath) { ...@@ -65,7 +54,7 @@ char *bcc_procutils_which(const char *binpath) {
buffer[path_len] = '/'; buffer[path_len] = '/';
strcpy(buffer + path_len + 1, binpath); strcpy(buffer + path_len + 1, binpath);
if (is_exe(buffer)) if (bcc_elf_is_exe(buffer))
return strdup(buffer); return strdup(buffer);
} }
......
...@@ -48,7 +48,7 @@ Probe::Probe(const char *bin_path, const char *provider, const char *name, ...@@ -48,7 +48,7 @@ Probe::Probe(const char *bin_path, const char *provider, const char *name,
bool Probe::in_shared_object() { bool Probe::in_shared_object() {
if (!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(); return in_shared_object_.value();
} }
......
...@@ -39,7 +39,7 @@ bool Argument::get_global_address(uint64_t *address, const std::string &binpath, ...@@ -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); .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}; struct bcc_symbol sym = {deref_ident_->c_str(), binpath.c_str(), 0x0};
if (!bcc_find_symbol_addr(&sym) && sym.offset) { if (!bcc_find_symbol_addr(&sym) && sym.offset) {
*address = sym.offset; *address = sym.offset;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment