Skip to content

fix: subtract 1 from call chain addresses other than the first one #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: hotspot
Choose a base branch
from

Conversation

pcc
Copy link
Contributor

@pcc pcc commented Jun 2, 2025

For stack frames other than the first one, ip is a return address, so it points to the instruction after the call instruction. By using the return address as is, we were incorrectly attributing these call chain samples to the instruction following the call. Therefore, subtract 1 from the return address so that it gets correctly attributed to the call instruction.

For stack frames other than the first one, ip is a return address, so it
points to the instruction after the call instruction. By using the return
address as is, we were incorrectly attributing these call chain samples to
the instruction following the call. Therefore, subtract 1 from the return
address so that it gets correctly attributed to the call instruction.
@pcc
Copy link
Contributor Author

pcc commented Jun 2, 2025

Reproducer:

$ cat 1.c
int x;

void f() {
  x++;
}

__attribute__((weak))
void force_frame_pointer() {}

__attribute__((noinline))
void infloop() {
#ifdef FP
  force_frame_pointer();
#endif
  // Use asm to prevent the compiler from optimizing away the rest of main.
#ifdef __aarch64__
  __asm__ __volatile__("b .");
#elif defined(__x86_64__)
  __asm__ __volatile__("jmp .");
#endif
}

int main() {
  infloop();
  f();
}
$ clang -fuse-ld=mold 1.c -gdwarf-4 -DFP -fno-omit-frame-pointer -O
$ perf record -g ./a.out
$ hotspot perf.data

Without this change: f appears in flame graph even though it is never executed.

With this change: f does not appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant