Commit 46f2cb22 authored by Brendan Gregg's avatar Brendan Gregg

allow uaddr() to work on non-bash

parent 217d6017
......@@ -314,7 +314,7 @@ void CodegenLLVM::visit(Call &call)
{
uint64_t addr;
auto &name = static_cast<String&>(*call.vargs->at(0)).str;
addr = bpftrace_.resolve_uname(name.c_str());
addr = bpftrace_.resolve_uname(name.c_str(), path_.c_str());
expr_ = b_.getInt64(addr);
}
else if (call.func == "join")
......@@ -864,6 +864,13 @@ void CodegenLLVM::visit(Probe &probe)
{b_.getInt8PtrTy()}, // struct pt_regs *ctx
false);
// needed for uaddr() call:
for (auto &attach_point : *probe.attach_points) {
path_ = attach_point->target;
// TODO: semantic analyser should ensure targets are equal when uaddr() is used
break;
}
/*
* Most of the time, we can take a probe like kprobe:do_f* and build a
* single BPF program for that, called "s_kprobe:do_f*", and attach it to
......
......@@ -66,6 +66,7 @@ private:
Value *ctx_;
BPFtrace &bpftrace_;
std::string probefull_;
std::string path_;
std::map<std::string, Value *> variables_;
int printf_id_ = 0;
......
......@@ -1154,13 +1154,12 @@ uint64_t BPFtrace::resolve_kname(const char *name)
return addr;
}
uint64_t BPFtrace::resolve_uname(const char *name)
uint64_t BPFtrace::resolve_uname(const char *name, const char *path)
{
uint64_t addr = 0;
// TODO: switch from objdump to library call
std::string call_str = "objdump -tT /bin/bash | grep ";
call_str += name;
// TODO: switch from objdump to library call, perhaps bcc_resolve_symname()
std::string call_str = std::string("objdump -tT ") + path + " | grep -w " + name;
const char *call = call_str.c_str();
auto result = exec_system(call);
addr = read_address_from_output(result);
......@@ -1170,14 +1169,14 @@ uint64_t BPFtrace::resolve_uname(const char *name)
std::string BPFtrace::exec_system(const char* cmd)
{
std::array<char, 128> buffer;
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
......
......@@ -38,7 +38,7 @@ public:
std::string resolve_sym(uintptr_t addr, bool show_offset=false);
std::string resolve_usym(uintptr_t addr, int pid, bool show_offset=false);
uint64_t resolve_kname(const char *name);
uint64_t resolve_uname(const char *name);
uint64_t resolve_uname(const char *name, const char *path);
std::string resolve_name(uint64_t name_id);
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