Skip to content

Commit 906faa0

Browse files
committed
Attempt to add bp to allow better symbol resolution for native frames.
1 parent 29ff6ad commit 906faa0

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

examples/cpp/pyperf/PyPerfBPFProgram.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ struct event {
168168
int32_t stack[STACK_MAX_LEN];
169169
uintptr_t user_ip;
170170
uintptr_t user_sp;
171+
uintptr_t user_bp;
171172
uint32_t user_stack_len;
172173
uint8_t raw_user_stack[__USER_STACKS_PAGES__ * PAGE_SIZE];
173174
#define FRAME_CODE_IS_NULL 0x80000001
@@ -345,6 +346,7 @@ on_event(struct pt_regs* ctx) {
345346
346347
event->user_sp = user_regs.sp;
347348
event->user_ip = user_regs.ip;
349+
event->user_bp = user_regs.bp;
348350
event->user_stack_len = 0;
349351
350352
// Subtract 128 from sp for x86-ABI red zone

examples/cpp/pyperf/PyPerfNativeStackTrace.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const uint8_t *NativeStackTrace::stack = NULL;
3131
size_t NativeStackTrace::stack_len = 0;
3232
uintptr_t NativeStackTrace::sp = 0;
3333
uintptr_t NativeStackTrace::ip = 0;
34+
uintptr_t NativeStackTrace::bp = 0;
3435
ProcSymbolsCache NativeStackTrace::procSymbolsCache;
3536
bool NativeStackTrace::insert_dso_name = false;
3637
const static double ProcSymbolsCacheTTL_S = 60;
@@ -43,11 +44,12 @@ static double steady_time_since_epoch() {
4344
}
4445

4546
NativeStackTrace::NativeStackTrace(uint32_t pid, const unsigned char *raw_stack,
46-
size_t stack_len, uintptr_t ip, uintptr_t sp) : error_occurred(false) {
47+
size_t stack_len, uintptr_t ip, uintptr_t sp, uintptr_t bp) : error_occurred(false) {
4748
NativeStackTrace::stack = raw_stack;
4849
NativeStackTrace::stack_len = stack_len;
4950
NativeStackTrace::ip = ip;
5051
NativeStackTrace::sp = sp;
52+
NativeStackTrace::bp = bp;
5153

5254
if (stack_len == 0) {
5355
return;
@@ -157,6 +159,15 @@ NativeStackTrace::NativeStackTrace(uint32_t pid, const unsigned char *raw_stack,
157159

158160
int NativeStackTrace::UPT_access_reg(unw_addr_space_t as, unw_regnum_t regnum,
159161
unw_word_t *valp, int write, void *arg) {
162+
if (regnum == UNW_X86_64_RBP) {
163+
if (write) {
164+
logInfo(2, "Libunwind attempts to write to BP\n");
165+
return -UNW_EINVAL;
166+
}
167+
168+
*valp = NativeStackTrace::bp;
169+
return 0;
170+
}
160171
if (regnum == UNW_REG_SP) {
161172
if (write) {
162173
logInfo(2, "Libunwind attempts to write to SP\n");
@@ -234,7 +245,7 @@ int NativeStackTrace::UPT_access_mem(unw_addr_space_t as, unw_word_t addr,
234245
int NativeStackTrace::UPT_access_fpreg(unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
235246
int write, void *arg) {
236247
logInfo(3, "Libunwind unexpected UPT_access_fpreg() attempt\n");
237-
return -UNW_EINVAL;
248+
return -UNW_EINVAL;
238249
}
239250

240251
int NativeStackTrace::UPT_resume(unw_addr_space_t as, unw_cursor_t *c, void *arg) {

examples/cpp/pyperf/PyPerfNativeStackTrace.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ typedef std::map<uint32_t, ProcSymbolsCacheEntry> ProcSymbolsCache;
2525
class NativeStackTrace {
2626
public:
2727
explicit NativeStackTrace(uint32_t pid, const uint8_t *raw_stack,
28-
size_t stack_len, uintptr_t ip, uintptr_t sp);
28+
size_t stack_len, uintptr_t ip, uintptr_t sp,
29+
uintptr_t bp);
2930

3031
std::vector<std::string> get_stack_symbol() const;
3132
bool error_occured() const;
@@ -41,6 +42,7 @@ class NativeStackTrace {
4142
static size_t stack_len;
4243
static uintptr_t ip;
4344
static uintptr_t sp;
45+
static uintptr_t bp;
4446
static ProcSymbolsCache procSymbolsCache;
4547

4648
static int UPT_access_reg(unw_addr_space_t as, unw_regnum_t regnum,

examples/cpp/pyperf/PyPerfType.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ typedef struct event {
225225
#define FRAME_CODE_IS_NULL ((int32_t)0x80000001)
226226
uintptr_t user_ip;
227227
uintptr_t user_sp;
228+
uintptr_t user_bp;
228229
uint32_t user_stack_len;
229230
uint8_t raw_user_stack[]; // NOTICE: Field with variadic length - must be last!
230231
} Event;
@@ -248,7 +249,7 @@ struct PyPerfSample {
248249
kernelStackId(raw->kernel_stack_id),
249250
pyStackIds(raw->stack, raw->stack + raw->stack_len),
250251
nativeStack(raw->pid, raw->raw_user_stack, raw->user_stack_len,
251-
raw->user_ip, raw->user_sp) {}
252+
raw->user_ip, raw->user_sp, raw->user_bp) {}
252253
};
253254

254255
} // namespace pyperf

0 commit comments

Comments
 (0)