Commit 1585341b authored by Alastair Robertson's avatar Alastair Robertson

Fix bug when printing more than one symbol with printf

e.g. The first symbol would be blank/corrupted when running:
    printf("%s %s\n", func, func)

This was caused by a dodgy conversion from std::string
to C-string, where the result of ".c_str()" was being
saved, but the underlying std::string was modified by
the vector it was being stored in, invalidating the C-string
parent 97cc8f18
......@@ -164,7 +164,7 @@ void SemanticAnalyser::visit(Call &call)
err_ << "printf() should not be assigned to a map" << std::endl;
}
if (nargs == 0) {
err_ << "printf() should take at least 1 argument (";
err_ << "printf() requires at least 1 argument (";
err_ << nargs << " provided)" << std::endl;
}
if (nargs > 7) {
......@@ -188,7 +188,7 @@ void SemanticAnalyser::visit(Call &call)
bpftrace_.printf_args_.push_back(std::make_tuple(fmt.str, args));
}
}
call.type = SizedType(Type::string, STRING_SIZE);
call.type = SizedType(Type::none, 0);
}
else {
err_ << "Unknown function: '" << call.func << "'" << std::endl;
......
......@@ -132,7 +132,7 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
auto fmt = std::get<0>(bpftrace->printf_args_[printf_id]).c_str();
auto args = std::get<1>(bpftrace->printf_args_[printf_id]);
std::vector<uint64_t> arg_values;
std::vector<std::string> resolved_symbols;
std::vector<std::unique_ptr<char>> resolved_symbols;
for (auto arg : args)
{
switch (arg.type)
......@@ -144,12 +144,14 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
arg_values.push_back((uint64_t)arg_data);
break;
case Type::sym:
resolved_symbols.push_back(bpftrace->resolve_sym(*(uint64_t*)arg_data));
arg_values.push_back((uint64_t)resolved_symbols.back().c_str());
resolved_symbols.emplace_back(strdup(
bpftrace->resolve_sym(*(uint64_t*)arg_data).c_str()));
arg_values.push_back((uint64_t)resolved_symbols.back().get());
break;
case Type::usym:
resolved_symbols.push_back(bpftrace->resolve_usym(*(uint64_t*)arg_data));
arg_values.push_back((uint64_t)resolved_symbols.back().c_str());
resolved_symbols.emplace_back(strdup(
bpftrace->resolve_usym(*(uint64_t*)arg_data).c_str()));
arg_values.push_back((uint64_t)resolved_symbols.back().get());
break;
default:
abort();
......@@ -378,9 +380,9 @@ int BPFtrace::print_map(IMap &map)
else if (map.type_.type == Type::ustack)
std::cout << get_stack(*(uint32_t*)value.data(), true, 8);
else if (map.type_.type == Type::sym)
std::cout << resolve_sym(*(uint64_t*)value.data());
std::cout << resolve_sym(*(uintptr_t*)value.data());
else if (map.type_.type == Type::usym)
std::cout << resolve_usym(*(uint64_t*)value.data());
std::cout << resolve_usym(*(uintptr_t*)value.data());
else if (map.type_.type == Type::string)
std::cout << value.data() << std::endl;
else if (map.type_.type == Type::count)
......@@ -607,7 +609,7 @@ std::string BPFtrace::get_stack(uint32_t stackid, bool ustack, int indent)
return stack.str();
}
std::string BPFtrace::resolve_sym(uint64_t addr, bool show_offset)
std::string BPFtrace::resolve_sym(uintptr_t addr, bool show_offset)
{
struct bcc_symbol sym;
std::ostringstream symbol;
......@@ -626,7 +628,7 @@ std::string BPFtrace::resolve_sym(uint64_t addr, bool show_offset)
return symbol.str();
}
std::string BPFtrace::resolve_usym(uint64_t addr) const
std::string BPFtrace::resolve_usym(uintptr_t addr) const
{
// TODO
std::ostringstream symbol;
......
......@@ -25,8 +25,8 @@ public:
int run();
int print_maps();
std::string get_stack(uint32_t stackid, bool ustack, int indent=0);
std::string resolve_sym(uint64_t addr, bool show_offset=false);
std::string resolve_usym(uint64_t addr) const;
std::string resolve_sym(uintptr_t addr, bool show_offset=false);
std::string resolve_usym(uintptr_t addr) const;
std::map<std::string, std::unique_ptr<IMap>> maps_;
std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_;
......
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