Commit a4aa9518 authored by williangaspar's avatar williangaspar
parent 0b79d156
......@@ -54,10 +54,10 @@ void CodegenLLVM::visit(Builtin &builtin)
expr_ = b_.CreateAnd(pidtgid, 0xffffffff);
}
}
else if (builtin.ident == "uid" || builtin.ident == "gid")
else if (builtin.ident == "uid" || builtin.ident == "gid" || builtin.ident == "username")
{
Value *uidgid = b_.CreateGetUidGid();
if (builtin.ident == "uid")
if (builtin.ident == "uid" || builtin.ident == "username")
{
expr_ = b_.CreateAnd(uidgid, 0xffffffff);
}
......
......@@ -76,6 +76,9 @@ void SemanticAnalyser::visit(Builtin &builtin)
builtin.type = SizedType(Type::name, 8);
probe_->need_expansion = true;
}
else if (builtin.ident == "username") {
builtin.type = SizedType(Type::username, 8);
}
else {
builtin.type = SizedType(Type::none, 0);
err_ << "Unknown builtin variable: '" << builtin.ident << "'" << std::endl;
......
......@@ -206,6 +206,8 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
auto args = std::get<1>(bpftrace->printf_args_[printf_id]);
std::vector<uint64_t> arg_values;
std::vector<std::unique_ptr<char>> resolved_symbols;
std::vector<std::unique_ptr<char>> resolved_usernames;
char *name;
for (auto arg : args)
{
......@@ -243,6 +245,11 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
bpftrace->resolve_usym(*(uint64_t*)(arg_data+arg.offset), *(uint64_t*)(arg_data+arg.offset + 8)).c_str()));
arg_values.push_back((uint64_t)resolved_symbols.back().get());
break;
case Type::username:
resolved_usernames.emplace_back(strdup(
bpftrace->resolve_uid(*(uint64_t*)(arg_data+arg.offset)).c_str()));
arg_values.push_back((uint64_t)resolved_usernames.back().get());
break;
case Type::name:
name = strdup(bpftrace->resolve_name(*(uint64_t*)(arg_data+arg.offset)).c_str());
arg_values.push_back((uint64_t)name);
......@@ -654,6 +661,8 @@ int BPFtrace::print_map(IMap &map, uint32_t top, uint32_t div)
std::cout << resolve_sym(*(uintptr_t*)value.data());
else if (map.type_.type == Type::usym)
std::cout << resolve_usym(*(uintptr_t*)value.data(), *(uint64_t*)(value.data() + 8));
else if (map.type_.type == Type::username)
std::cout << resolve_uid(*(uint64_t*)(value.data())) << std::endl;
else if (map.type_.type == Type::string)
std::cout << value.data() << std::endl;
else if (map.type_.type == Type::count || map.type_.type == Type::sum)
......@@ -1101,6 +1110,48 @@ std::string BPFtrace::get_stack(uint64_t stackidpid, bool ustack, int indent)
return stack.str();
}
std::string BPFtrace::resolve_uid(uintptr_t addr)
{
std::string file_name = "/etc/passwd";
std::string uid = std::to_string(addr);
std::string username = "";
std::ifstream file(file_name);
if (file.fail())
{
std::cerr << strerror(errno) << ": " << file_name << std::endl;
return username;
}
std::string line;
bool found = false;
while (std::getline(file, line) && !found)
{
auto fields = split_string(line, ':');
if (fields[2] == uid) {
found = true;
username = fields[0];
}
}
file.close();
return username;
}
std::vector<std::string> BPFtrace::split_string(std::string &str, char split_by)
{
std::vector<std::string> elems;
std::stringstream ss(str);
std::string value;
while(std::getline(ss, value, split_by)) {
elems.push_back(value);
}
return elems;
}
std::string BPFtrace::resolve_sym(uintptr_t addr, bool show_offset)
{
struct bcc_symbol sym;
......
......@@ -37,6 +37,7 @@ public:
std::string get_stack(uint64_t stackidpid, bool ustack, int indent=0);
std::string resolve_sym(uintptr_t addr, bool show_offset=false);
std::string resolve_usym(uintptr_t addr, int pid, bool show_offset=false);
std::string resolve_uid(uintptr_t addr);
uint64_t resolve_kname(const char *name);
uint64_t resolve_uname(const char *name, const char *path);
std::string resolve_name(uint64_t name_id);
......@@ -87,6 +88,7 @@ private:
static std::string exec_system(const char* cmd);
static std::string hist_index_label(int power);
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;
};
......
......@@ -46,7 +46,7 @@ path :(\\.|[_\-\./a-zA-Z0-9])*:
<COMMENT>"EOF" driver.error(loc, std::string("end of file during comment"));
<COMMENT>.|"\n" ;
pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func|name|curtask|rand|ctx {
pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func|name|curtask|rand|ctx|username {
return Parser::make_BUILTIN(yytext, loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
......
......@@ -33,7 +33,7 @@ std::string verify_format_string(const std::string &fmt, std::vector<Field> args
for (int i=0; i<num_args; i++, token_iter++)
{
Type arg_type = args.at(i).type.type;
if (arg_type == Type::sym || arg_type == Type::usym || arg_type == Type::name)
if (arg_type == Type::sym || arg_type == Type::usym || arg_type == Type::name || arg_type == Type::username)
arg_type = Type::string; // Symbols should be printed as strings
int offset = 1;
......
......@@ -32,6 +32,7 @@ enum class Type
cast,
join,
name,
username,
};
std::ostream &operator<<(std::ostream &os, Type type);
......
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