Commit bd253e51 authored by Brendan Gregg's avatar Brendan Gregg Committed by GitHub

Merge pull request #303 from danobi/path_lookup

Search PATH for executables when running commands
parents 9e608df7 a07be2c1
...@@ -65,7 +65,7 @@ Enable USDT probes on PID. Will terminate bpftrace on PID termination. Note this ...@@ -65,7 +65,7 @@ Enable USDT probes on PID. Will terminate bpftrace on PID termination. Note this
.TP .TP
\fB\-c CMD\fR \fB\-c CMD\fR
Helper to run CMD. Equivalent to manually running CMD and then giving passing the PID to -p. This is useful to ensure Helper to run CMD. Equivalent to manually running CMD and then giving passing the PID to -p. This is useful to ensure
you've traced at least the duration CMD's execution. You must provide an absolute path for the executable. you've traced at least the duration CMD's execution.
. .
.TP .TP
\fB\-v\fR \fB\-v\fR
......
...@@ -448,6 +448,7 @@ int BPFtrace::run(std::unique_ptr<BpfOrc> bpforc) ...@@ -448,6 +448,7 @@ int BPFtrace::run(std::unique_ptr<BpfOrc> bpforc)
if (cmd_.size()) if (cmd_.size())
{ {
auto args = split_string(cmd_, ' '); auto args = split_string(cmd_, ' ');
args[0] = resolve_binary_path(args[0]); // does path lookup on executable
int pid = spawn_child(args); int pid = spawn_child(args);
if (pid < 0) if (pid < 0)
{ {
...@@ -1111,6 +1112,28 @@ int BPFtrace::print_lhist(const std::vector<uint64_t> &values, int min, int max, ...@@ -1111,6 +1112,28 @@ int BPFtrace::print_lhist(const std::vector<uint64_t> &values, int min, int max,
return 0; return 0;
} }
std::string BPFtrace::resolve_binary_path(const std::string& cmd)
{
std::string query;
query += "command -pv ";
query += cmd;
std::string result = exec_system(query.c_str());
if (result.size())
{
// Remove newline at the end
auto it = result.rfind('\n');
if (it != std::string::npos)
result.erase(it);
return result;
}
else
{
return cmd;
}
}
int BPFtrace::spawn_child(const std::vector<std::string>& args) int BPFtrace::spawn_child(const std::vector<std::string>& args)
{ {
static const int maxargs = 256; static const int maxargs = 256;
......
...@@ -120,6 +120,7 @@ private: ...@@ -120,6 +120,7 @@ private:
static std::string lhist_index_label(int number); static std::string lhist_index_label(int number);
static std::vector<std::string> split_string(std::string &str, char split_by); static std::vector<std::string> split_string(std::string &str, char split_by);
std::vector<uint8_t> find_empty_key(IMap &map, size_t size) const; std::vector<uint8_t> find_empty_key(IMap &map, size_t size) const;
static std::string resolve_binary_path(const std::string& cmd);
static int spawn_child(const std::vector<std::string>& args); static int spawn_child(const std::vector<std::string>& args);
static bool is_pid_alive(int pid); static bool is_pid_alive(int pid);
}; };
......
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