Skip to content

Commit

Permalink
Skip function prologue for uprobes
Browse files Browse the repository at this point in the history
  • Loading branch information
ttreyer committed Mar 30, 2024
1 parent 81ea7bd commit b82fd29
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/arch/x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <algorithm>
#include <array>

// SP + 8 points to the first argument that is passed on the stack
#define ARG0_STACK 8
// SP + 16 points to the first argument that is passed on the stack
#define ARG0_STACK 16

namespace bpftrace {
namespace arch {
Expand Down
6 changes: 6 additions & 0 deletions src/bpftrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ int BPFtrace::add_probe(ast::Probe &p)
generateWatchpointSetupProbe(func_id, *attach_point, p));

resources.watchpoint_probes.emplace_back(std::move(probe));
} else if (probetype(attach_point->provider) == ProbeType::uprobe) {
if (auto *dwarf = get_dwarf(target))
probe.func_offset = std::max(probe.func_offset,
dwarf->get_prologue_size(func_id));

resources.probes.push_back(probe);
} else {
resources.probes.push_back(probe);
}
Expand Down
11 changes: 11 additions & 0 deletions src/dwarf_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ std::unique_ptr<Dwarf> Dwarf::GetFromBinary(BPFtrace *bpftrace,
}
}

uint64_t Dwarf::get_prologue_size(const std::string &function)
{
auto functions = target_.FindFunctions(function.c_str());
// The given function name MUST identify a unique function!
if (functions.GetSize() != 1)
return 0;

auto fn = functions.GetContextAtIndex(0).GetFunction();
return fn.GetPrologueByteSize();
}

std::string Dwarf::get_type_name(lldb::SBType type)
{
std::string type_name = type.GetDisplayTypeName() ?: "<anonymous type>";
Expand Down
6 changes: 6 additions & 0 deletions src/dwarf_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Dwarf {
static std::unique_ptr<Dwarf> GetFromBinary(BPFtrace *bpftrace,
std::string file_path);

uint64_t get_prologue_size(const std::string &function);
std::vector<std::string> get_function_params(const std::string &function);
Struct resolve_args(const std::string &function);

Expand Down Expand Up @@ -70,6 +71,11 @@ class Dwarf {
return nullptr;
}

uint64_t get_prologue_size(const std::string &function __attribute((unused)))
{
return 0;
}

std::vector<std::string> get_function_params(const std::string &function
__attribute__((unused)))
{
Expand Down
6 changes: 3 additions & 3 deletions tests/runtime/call
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ AFTER ./testprogs/syscall nanosleep 1e8
NAME ustack_stack_mode_env_bpftrace
PROG u:./testprogs/uprobe_loop:uprobeFunction1 { printf("%s\n", ustack(1)); exit(); }
ENV BPFTRACE_STACK_MODE=bpftrace
EXPECT uprobeFunction1+0
EXPECT_REGEX uprobeFunction1\+[1-9][0-9]*
TIMEOUT 5
AFTER ./testprogs/uprobe_loop
# This was debugged as far down as std::cout having badbit [0] set after a
Expand All @@ -249,7 +249,7 @@ SKIP_IF_ENV_HAS CI=true
NAME ustack_stack_mode_env_perf
PROG u:./testprogs/uprobe_loop:uprobeFunction1 { printf("%s\n", ustack(1)); exit(); }
ENV BPFTRACE_STACK_MODE=perf
EXPECT_REGEX \d+ uprobeFunction1\+0 \(.*/uprobe_loop\)
EXPECT_REGEX [0-9a-f]+ uprobeFunction1\+[1-9][0-9]* \(.*/uprobe_loop\)
TIMEOUT 5
AFTER ./testprogs/uprobe_loop
# See https://github.com/bpftrace/bpftrace/issues/3080
Expand All @@ -276,7 +276,7 @@ SKIP_IF_ENV_HAS CI=true
NAME ustack_elf_symtable
ENV BPFTRACE_CACHE_USER_SYMBOLS=PER_PROGRAM
PROG uprobe:./testprogs/uprobe_symres_exited_process:test { print(ustack); exit(); }
EXPECT_REGEX ^\s+test\+0\s+main\+[1-9][0-9]*
EXPECT_REGEX ^\s+test\+[1-9][0-9]*\s+test2\+[1-9][0-9]*\s+main\+[1-9][0-9]*
AFTER ./testprogs/disable_aslr ./testprogs/uprobe_symres_exited_process
ARCH x86_64
TIMEOUT 5
Expand Down

0 comments on commit b82fd29

Please sign in to comment.