Skip to main content

libdd_libunwind_sys/
libunwind_aarch64.rs

1// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4pub type UnwContext = libc::ucontext_t;
5
6pub type UnwWord = u64;
7
8// Opaque cursor structure
9#[repr(C)]
10#[derive(Debug, Copy, Clone)]
11pub struct UnwCursor {
12    pub opaque: [UnwWord; 250],
13}
14
15extern "C" {
16    #[link_name = "_ULaarch64_init_local2"]
17    pub fn unw_init_local2(cursor: *mut UnwCursor, context: *mut UnwContext, flag: i32) -> i32;
18    #[link_name = "_ULaarch64_step"]
19    pub fn unw_step(cursor: *mut UnwCursor) -> i32;
20    #[link_name = "_ULaarch64_get_reg"]
21    pub fn unw_get_reg(cursor: *mut UnwCursor, reg: i32, valp: *mut UnwWord) -> i32;
22    #[link_name = "_ULaarch64_get_proc_name"]
23    pub fn unw_get_proc_name(
24        cursor: *mut UnwCursor,
25        name: *mut libc::c_char,
26        len: usize,
27        offset: *mut u64,
28    ) -> i32;
29    #[link_name = "unw_backtrace2"]
30    pub fn unw_backtrace2(
31        buffer: *mut *mut ::std::os::raw::c_void,
32        size: i32,
33        context: *mut UnwContext,
34        flag: i32,
35    ) -> i32;
36}
37
38pub const UNW_REG_IP: i32 = 30; // Instruction Pointer
39pub const UNW_REG_SP: i32 = 31; // Stack Pointer
40pub const UNW_REG_FP: i32 = 29; // Frame Pointer
41pub const UNW_INIT_LOCAL_ONLY_IP: i32 = 1;
42
43/// Saves the current CPU context into `uc_mcontext.regs`.
44/// On aarch64 libunwind does not emit a callable symbol for getcontext —
45/// it uses a C preprocessor macro with inline assembly. This is the Rust
46/// equivalent: save all GPRs, SP, LR, and PC into `uc_mcontext`.
47///
48/// # Safety
49/// `context` must be a valid, non-null pointer to a zeroed or initialized `UnwContext`.
50// This is only for testing purposes and allow the tests to work with libc and musl-libc
51#[cfg(test)]
52#[inline(always)]
53pub unsafe fn getcontext(context: *mut UnwContext) -> i32 {
54    let base = core::ptr::addr_of_mut!((*context).uc_mcontext.regs) as u64;
55    let ret: u64;
56    core::arch::asm!(
57        "stp x0, x1, [x0, #0]",
58        "stp x2, x3, [x0, #16]",
59        "stp x4, x5, [x0, #32]",
60        "stp x6, x7, [x0, #48]",
61        "stp x8, x9, [x0, #64]",
62        "stp x10, x11, [x0, #80]",
63        "stp x12, x13, [x0, #96]",
64        "stp x14, x15, [x0, #112]",
65        "stp x16, x17, [x0, #128]",
66        "stp x18, x19, [x0, #144]",
67        "stp x20, x21, [x0, #160]",
68        "stp x22, x23, [x0, #176]",
69        "stp x24, x25, [x0, #192]",
70        "stp x26, x27, [x0, #208]",
71        "stp x28, x29, [x0, #224]",
72        "mov x1, sp",
73        "stp x30, x1, [x0, #240]",
74        "adr x1, 2f",
75        "str x1, [x0, #256]",
76        "mov x0, #0",
77        "2:",
78        inout("x0") base => ret,
79        out("x1") _,
80        options(nostack, preserves_flags),
81    );
82    ret as i32
83}