summaryrefslogtreecommitdiff
path: root/drivers/android/binder/trace.rs
blob: de2894bcb368ca89a4073f4c58fd25624a6fe064 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-2.0

// Copyright (C) 2025 Google LLC.

use crate::transaction::Transaction;

use core::ptr;

use kernel::bindings::{rust_binder_transaction, task_struct};
use kernel::error::Result;
use kernel::ffi::{c_int, c_uint, c_ulong};
use kernel::task::Task;
use kernel::tracepoint::declare_trace;

declare_trace! {
    unsafe fn binder_ioctl(cmd: c_uint, arg: c_ulong);
    unsafe fn binder_ioctl_done(ret: c_int);
    unsafe fn binder_read_done(ret: c_int);
    unsafe fn binder_write_done(ret: c_int);
    unsafe fn binder_wait_for_work(proc_work: bool, transaction_stack: bool, thread_todo: bool);
    unsafe fn binder_transaction(reply: bool, t: rust_binder_transaction, thread: *mut task_struct);
    unsafe fn binder_transaction_received(t: rust_binder_transaction);
    unsafe fn binder_transaction_fd_send(t_debug_id: c_int, fd: c_int, offset: usize);
    unsafe fn binder_transaction_fd_recv(t_debug_id: c_int, fd: c_int, offset: usize);
    unsafe fn binder_command(cmd: u32);
    unsafe fn binder_return(ret: u32);
    unsafe fn binder_transaction_alloc_buf(
        debug_id: c_int,data_size: usize, offset_size: usize, extra_buffers_size: usize);
    unsafe fn binder_transaction_buffer_release(debug_id: c_int);
    unsafe fn binder_transaction_failed_buffer_release(debug_id: c_int);
    unsafe fn binder_transaction_update_buffer_release(debug_id: c_int);
}

#[inline]
fn raw_transaction(t: &Transaction) -> rust_binder_transaction {
    ptr::from_ref(t).cast_mut().cast()
}

#[inline]
fn to_errno(ret: Result) -> i32 {
    match ret {
        Ok(()) => 0,
        Err(err) => err.to_errno(),
    }
}

#[inline]
pub(crate) fn trace_ioctl(cmd: u32, arg: usize) {
    // SAFETY: Always safe to call.
    unsafe { binder_ioctl(cmd, arg as c_ulong) }
}

#[inline]
pub(crate) fn trace_ioctl_done(ret: Result) {
    // SAFETY: Always safe to call.
    unsafe { binder_ioctl_done(to_errno(ret)) }
}
#[inline]
pub(crate) fn trace_read_done(ret: Result) {
    // SAFETY: Always safe to call.
    unsafe { binder_read_done(to_errno(ret)) }
}
#[inline]
pub(crate) fn trace_write_done(ret: Result) {
    // SAFETY: Always safe to call.
    unsafe { binder_write_done(to_errno(ret)) }
}

#[inline]
pub(crate) fn trace_wait_for_work(proc_work: bool, transaction_stack: bool, thread_todo: bool) {
    // SAFETY: Always safe to call.
    unsafe { binder_wait_for_work(proc_work, transaction_stack, thread_todo) }
}

#[inline]
pub(crate) fn trace_transaction(reply: bool, t: &Transaction, thread: Option<&Task>) {
    let thread = match thread {
        Some(thread) => thread.as_ptr(),
        None => core::ptr::null_mut(),
    };
    // SAFETY: The raw transaction is valid for the duration of this call. The thread pointer is
    // valid or null.
    unsafe { binder_transaction(reply, raw_transaction(t), thread) }
}

#[inline]
pub(crate) fn trace_transaction_received(t: &Transaction) {
    // SAFETY: The raw transaction is valid for the duration of this call.
    unsafe { binder_transaction_received(raw_transaction(t)) }
}

#[inline]
pub(crate) fn trace_transaction_fd_send(t_debug_id: usize, fd: u32, offset: usize) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_transaction_fd_send(t_debug_id as c_int, fd as c_int, offset) }
}
#[inline]
pub(crate) fn trace_transaction_fd_recv(t_debug_id: usize, fd: u32, offset: usize) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_transaction_fd_recv(t_debug_id as c_int, fd as c_int, offset) }
}

#[inline]
pub(crate) fn trace_command(cmd: u32) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_command(cmd) }
}
#[inline]
pub(crate) fn trace_return(ret: u32) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_return(ret) }
}
#[inline]
pub(crate) fn trace_transaction_alloc_buf(
    debug_id: usize,
    data_size: usize,
    offset_size: usize,
    extra_buffers_size: usize,
) {
    // SAFETY: The `data` pointer is valid.
    unsafe {
        binder_transaction_alloc_buf(
            debug_id as c_int,
            data_size,
            offset_size,
            extra_buffers_size,
        )
    }
}
#[inline]
pub(crate) fn trace_transaction_buffer_release(debug_id: usize) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_transaction_buffer_release(debug_id as c_int) }
}
#[inline]
pub(crate) fn trace_transaction_failed_buffer_release(debug_id: usize) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_transaction_failed_buffer_release(debug_id as c_int) }
}
#[inline]
pub(crate) fn trace_transaction_update_buffer_release(debug_id: usize) {
    // SAFETY: This function is always safe to call.
    unsafe { binder_transaction_update_buffer_release(debug_id as c_int) }
}