Commit a07be2c1 authored by Daniel Xu's avatar Daniel Xu

Search PATH for executables when running commands

This makes it easier for the user to run commands. Specifying abosolute
paths to a binary was kind of a pain in the rear.
parent 9e608df7
......@@ -65,7 +65,7 @@ Enable USDT probes on PID. Will terminate bpftrace on PID termination. Note this
.TP
\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
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
\fB\-v\fR
......
......@@ -448,6 +448,7 @@ int BPFtrace::run(std::unique_ptr<BpfOrc> bpforc)
if (cmd_.size())
{
auto args = split_string(cmd_, ' ');
args[0] = resolve_binary_path(args[0]); // does path lookup on executable
int pid = spawn_child(args);
if (pid < 0)
{
......@@ -1111,6 +1112,28 @@ int BPFtrace::print_lhist(const std::vector<uint64_t> &values, int min, int max,
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)
{
static const int maxargs = 256;
......
......@@ -120,6 +120,7 @@ private:
static std::string lhist_index_label(int number);
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;
static std::string resolve_binary_path(const std::string& cmd);
static int spawn_child(const std::vector<std::string>& args);
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