Skip to contentSkip to conversation

rust_binder: add and wire Binder tracepoints

PATCH New Mohamad Alsadhan 1 participant 7 messages Mar 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover rust_binder: add and wire Binder tracepoints

This series adds several missing tracepoints for the Rust Binder implementation (equivalent to ones already present in C binder) and wires them into the relevant execution paths.

The first patch adjusts current naming to match the existing userspace expectationse (e.g. rust_binder_ioctl -> binder_ioctl).

The rest of the patches each add declarations for a separate tracepoint category and wire in their calls into the corresponding Binder execution paths.

Tracepoints added include:
- ioctl entry/exit (binder_ioctl, binder_ioctl_done)
- command parsing and return writes (binder_command, binder_return)
- read/write completion (binder_read_done, binder_write_done)
- wait (binder_wait_for_work)
- transaction (binder_transaction_received)
- fd translation (binder_transaction_fd_{send,recv})

---
Changes in v3:
- Reworked series to split by each tracepoint category instead of one
commit for all declarations and one for all calls
- Dropped unneeded buffer/page lifecycle tracepoints
- No functional changes from v2, except the removal of buffer/page
lifecycle tracepoints
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Fixed broken formatting artifacts (rustfmtcheck passes now)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Mohamad Alsadhan (6):
rust_binder: remove "rust_" prefix from tracepoints
rust_binder: add ioctl/read/write done tracepoints
rust_binder: add wait_for_work tracepoint
rust_binder: add transaction_received tracepoint
rust_binder: add fd translation tracepoints
rust_binder: add command/return tracepoints

 drivers/android/binder/allocation.rs        |   1 +
 drivers/android/binder/process.rs           |   7 +-
 drivers/android/binder/rust_binder.h        |   5 ++
 drivers/android/binder/rust_binder_events.h | 121 +++++++++++++++++++++++++++-
 drivers/android/binder/rust_binder_main.rs  |   1 +
 drivers/android/binder/thread.rs            |  15 +++-
 drivers/android/binder/trace.rs             |  78 ++++++++++++++++--
 drivers/android/binder/transaction.rs       |   2 +
 8 files changed, 219 insertions(+), 11 deletions(-)

---
base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
change-id: 20260307-rust-binder-trace-c3eb49918deb

Best regards,

Signature · 1 line
Mohamad Alsadhan <[email protected]>
#1 lore raw

PATCH v3 1/6 rust_binder: remove "rust_" prefix from tracepoints

Remove the "rust_" prefix as the name is part of the uapi, and userspace expects tracepoints to have the old names.

---
 drivers/android/binder/rust_binder_events.h | 4 ++--
 drivers/android/binder/trace.rs             | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/android/binder/rust_binder_events.h +2 -2
index 8ad785c6b..e3adfb931 100644
@@ -15,7 +15,7 @@
 
 #include <linux/tracepoint.h>
 
-TRACE_EVENT(rust_binder_ioctl,
+TRACE_EVENT(binder_ioctl,
 	TP_PROTO(unsigned int cmd, unsigned long arg),
 	TP_ARGS(cmd, arg),
 
@@ -30,7 +30,7 @@ TRACE_EVENT(rust_binder_ioctl,
 	TP_printk("cmd=0x%x arg=0x%lx", __entry->cmd, __entry->arg)
 );
 
-TRACE_EVENT(rust_binder_transaction,
+TRACE_EVENT(binder_transaction,
 	TP_PROTO(bool reply, rust_binder_transaction t, struct task_struct *thread),
 	TP_ARGS(reply, t, thread),
 	TP_STRUCT__entry(
drivers/android/binder/trace.rs +4 -4
index 9839901c7..d54b18ab7 100644
@@ -10,8 +10,8 @@
 use kernel::tracepoint::declare_trace;
 
 declare_trace! {
-    unsafe fn rust_binder_ioctl(cmd: c_uint, arg: c_ulong);
-    unsafe fn rust_binder_transaction(reply: bool, t: rust_binder_transaction, thread: *mut task_struct);
+    unsafe fn binder_ioctl(cmd: c_uint, arg: c_ulong);
+    unsafe fn binder_transaction(reply: bool, t: rust_binder_transaction, thread: *mut task_struct);
 }
 
 #[inline]
@@ -22,7 +22,7 @@ fn raw_transaction(t: &Transaction) -> rust_binder_transaction {
 #[inline]
 pub(crate) fn trace_ioctl(cmd: u32, arg: usize) {
     // SAFETY: Always safe to call.
-    unsafe { rust_binder_ioctl(cmd, arg as c_ulong) }
+    unsafe { binder_ioctl(cmd, arg as c_ulong) }
 }
 
 #[inline]
@@ -33,5 +33,5 @@ pub(crate) fn trace_transaction(reply: bool, t: &Transaction, thread: Option<&Ta
     };
     // SAFETY: The raw transaction is valid for the duration of this call. The thread pointer is
     // valid or null.
-    unsafe { rust_binder_transaction(reply, raw_transaction(t), thread) }
+    unsafe { binder_transaction(reply, raw_transaction(t), thread) }
 }
Signature · 1 line
2.52.0
#2 lore raw

PATCH v3 2/6 rust_binder: add ioctl/read/write done tracepoints

In reply to Mohamad Alsadhan

Add Rust Binder tracepoints declarations for ioctl_done, read_done and write_done.

Additionally, wire in the new tracepoints into the corresponding Binder call sites.

Note that the new tracepoints report final errno-style return values, matching the existing C model for operation completion.

---
 drivers/android/binder/process.rs           |  7 +++++--
 drivers/android/binder/rust_binder_events.h | 21 ++++++++++++++++++++
 drivers/android/binder/thread.rs            |  2 ++
 drivers/android/binder/trace.rs             | 30 ++++++++++++++++++++++++++++-
 4 files changed, 57 insertions(+), 3 deletions(-)
drivers/android/binder/process.rs +5 -2
index 41de55931..4d7b165e7 100644
@@ -1656,11 +1656,14 @@ pub(crate) fn ioctl(this: ArcBorrow<'_, Process>, file: &File, cmd: u32, arg: us
 
         const _IOC_READ_WRITE: u32 = _IOC_READ | _IOC_WRITE;
 
-        match _IOC_DIR(cmd) {
+        let res = match _IOC_DIR(cmd) {
             _IOC_WRITE => Self::ioctl_write_only(this, file, cmd, &mut user_slice.reader()),
             _IOC_READ_WRITE => Self::ioctl_write_read(this, file, cmd, user_slice),
             _ => Err(EINVAL),
-        }
+        };
+
+        crate::trace::trace_ioctl_done(res);
+        res
     }
 
     pub(crate) fn mmap(
drivers/android/binder/rust_binder_events.h +21 -0
index e3adfb931..4fda8576c 100644
@@ -30,6 +30,27 @@ TRACE_EVENT(binder_ioctl,
 	TP_printk("cmd=0x%x arg=0x%lx", __entry->cmd, __entry->arg)
 );
 
+DECLARE_EVENT_CLASS(binder_function_return_class,
+	TP_PROTO(int ret),
+	TP_ARGS(ret),
+	TP_STRUCT__entry(
+		__field(int, ret)
+	),
+	TP_fast_assign(
+		__entry->ret = ret;
+	),
+	TP_printk("ret=%d", __entry->ret)
+);
+
+#define DEFINE_RBINDER_FUNCTION_RETURN_EVENT(name)	\
+DEFINE_EVENT(binder_function_return_class, name,	\
+	TP_PROTO(int ret), \
+	TP_ARGS(ret))
+
+DEFINE_RBINDER_FUNCTION_RETURN_EVENT(binder_ioctl_done);
+DEFINE_RBINDER_FUNCTION_RETURN_EVENT(binder_read_done);
+DEFINE_RBINDER_FUNCTION_RETURN_EVENT(binder_write_done);
+
 TRACE_EVENT(binder_transaction,
 	TP_PROTO(bool reply, rust_binder_transaction t, struct task_struct *thread),
 	TP_ARGS(reply, t, thread),
drivers/android/binder/thread.rs +2 -0
index 0b62d24b2..bb36c5228 100644
@@ -1482,6 +1482,7 @@ pub(crate) fn write_read(self: &Arc<Self>, data: UserSlice, wait: bool) -> Resul
         let mut ret = Ok(());
         if req.write_size > 0 {
             ret = self.write(&mut req);
+            crate::trace::trace_write_done(ret);
             if let Err(err) = ret {
                 pr_warn!(
                     "Write failure {:?} in pid:{}",
@@ -1498,6 +1499,7 @@ pub(crate) fn write_read(self: &Arc<Self>, data: UserSlice, wait: bool) -> Resul
         // Go through the work queue.
         if req.read_size > 0 {
             ret = self.read(&mut req, wait);
+            crate::trace::trace_read_done(ret);
             if ret.is_err() && ret != Err(EINTR) {
                 pr_warn!(
                     "Read failure {:?} in pid:{}",
drivers/android/binder/trace.rs +29 -1
index d54b18ab7..3b0458e27 100644
@@ -5,12 +5,16 @@
 use crate::transaction::Transaction;
 
 use kernel::bindings::{rust_binder_transaction, task_struct};
-use kernel::ffi::{c_uint, c_ulong};
+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_transaction(reply: bool, t: rust_binder_transaction, thread: *mut task_struct);
 }
 
@@ -19,12 +23,36 @@ fn raw_transaction(t: &Transaction) -> rust_binder_transaction {
     t as *const Transaction as rust_binder_transaction
 }
 
+#[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_transaction(reply: bool, t: &Transaction, thread: Option<&Task>) {
     let thread = match thread {
Signature · 1 line
2.52.0
#3 lore raw

PATCH v3 3/6 rust_binder: add `wait_for_work` tracepoint

In reply to Mohamad Alsadhan

Add the Rust Binder wait_for_work tracepoint declaration and wire it into the thread read path before selecting the work source.

---
 drivers/android/binder/rust_binder_events.h | 18 ++++++++++++++++++
 drivers/android/binder/thread.rs            | 11 +++++++++--
 drivers/android/binder/trace.rs             |  7 +++++++
 3 files changed, 34 insertions(+), 2 deletions(-)
drivers/android/binder/rust_binder_events.h +18 -0
index 4fda8576c..62b587c7c 100644
@@ -51,6 +51,24 @@ DEFINE_RBINDER_FUNCTION_RETURN_EVENT(binder_ioctl_done);
 DEFINE_RBINDER_FUNCTION_RETURN_EVENT(binder_read_done);
 DEFINE_RBINDER_FUNCTION_RETURN_EVENT(binder_write_done);
 
+TRACE_EVENT(binder_wait_for_work,
+	TP_PROTO(bool proc_work, bool transaction_stack, bool thread_todo),
+	TP_ARGS(proc_work, transaction_stack, thread_todo),
+	TP_STRUCT__entry(
+		__field(bool, proc_work)
+		__field(bool, transaction_stack)
+		__field(bool, thread_todo)
+	),
+	TP_fast_assign(
+		__entry->proc_work = proc_work;
+		__entry->transaction_stack = transaction_stack;
+		__entry->thread_todo = thread_todo;
+	),
+	TP_printk("proc_work=%d transaction_stack=%d thread_todo=%d",
+		  __entry->proc_work, __entry->transaction_stack,
+		  __entry->thread_todo)
+);
+
 TRACE_EVENT(binder_transaction,
 	TP_PROTO(bool reply, rust_binder_transaction t, struct task_struct *thread),
 	TP_ARGS(reply, t, thread),
drivers/android/binder/thread.rs +9 -2
index bb36c5228..15657d6e9 100644
@@ -1412,11 +1412,18 @@ fn read(self: &Arc<Self>, req: &mut BinderWriteRead, wait: bool) -> Result {
             UserSlice::new(UserPtr::from_addr(read_start as _), read_len as _).writer(),
             self,
         );
-        let (in_pool, use_proc_queue) = {
+        let (in_pool, has_transaction, thread_todo, use_proc_queue) = {
             let inner = self.inner.lock();
-            (inner.is_looper(), inner.should_use_process_work_queue())
+            (
+                inner.is_looper(),
+                inner.current_transaction.is_some(),
+                !inner.work_list.is_empty(),
+                inner.should_use_process_work_queue(),
+            )
         };
 
+        crate::trace::trace_wait_for_work(use_proc_queue, has_transaction, thread_todo);
+
         let getter = if use_proc_queue {
             Self::get_work
         } else {
drivers/android/binder/trace.rs +7 -0
index 3b0458e27..1f62b2276 100644
@@ -15,6 +15,7 @@
     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);
 }
 
@@ -53,6 +54,12 @@ pub(crate) fn trace_write_done(ret: Result) {
     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 {
Signature · 1 line
2.52.0
#4 lore raw

PATCH v3 4/6 rust_binder: add `transaction_received` tracepoint

In reply to Mohamad Alsadhan

Add Rust Binder transaction_received tracepoint decalaration and wire in the corresponding trace call when a transaction work item is accepted for execution.

---
 drivers/android/binder/rust_binder_events.h | 12 ++++++++++++
 drivers/android/binder/trace.rs             |  7 +++++++
 drivers/android/binder/transaction.rs       |  2 ++
 3 files changed, 21 insertions(+)
drivers/android/binder/rust_binder_events.h +12 -0
index 62b587c7c..8a0b72bf0 100644
@@ -99,6 +99,18 @@ TRACE_EVENT(binder_transaction,
 		  __entry->reply, __entry->flags, __entry->code)
 );
 
+TRACE_EVENT(binder_transaction_received,
+	TP_PROTO(rust_binder_transaction t),
+	TP_ARGS(t),
+	TP_STRUCT__entry(
+		__field(int, debug_id)
+	),
+	TP_fast_assign(
+		__entry->debug_id = rust_binder_transaction_debug_id(t);
+	),
+	TP_printk("transaction=%d", __entry->debug_id)
+);
+
 #endif /* _RUST_BINDER_TRACE_H */
 
 /* This part must be outside protection */
drivers/android/binder/trace.rs +7 -0
index 1f62b2276..d96afdb79 100644
@@ -17,6 +17,7 @@
     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);
 }
 
 #[inline]
@@ -70,3 +71,9 @@ pub(crate) fn trace_transaction(reply: bool, t: &Transaction, thread: Option<&Ta
     // 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)) }
+}
drivers/android/binder/transaction.rs +2 -0
index 75e6f5fba..c43846bb7 100644
@@ -430,6 +430,8 @@ fn do_work(
 
         self.drop_outstanding_txn();
 
+        crate::trace::trace_transaction_received(&self);
+
         // When this is not a reply and not a oneway transaction, update `current_transaction`. If
         // it's a reply, `current_transaction` has already been updated appropriately.
         if self.target_node.is_some() && tr_sec.transaction_data.flags & TF_ONE_WAY == 0 {
Signature · 1 line
2.52.0
#5 lore raw

PATCH v3 5/6 rust_binder: add fd translation tracepoints

In reply to Mohamad Alsadhan

Add Rust Binder tracepoint declarations for both transaction_fd_send and transaction_fd_recv. Also, wire in the corresponding trace calls where fd objects are serialised/deserialised.

---
 drivers/android/binder/allocation.rs        |  1 +
 drivers/android/binder/rust_binder.h        |  5 +++++
 drivers/android/binder/rust_binder_events.h | 34 +++++++++++++++++++++++++++++
 drivers/android/binder/thread.rs            |  1 +
 drivers/android/binder/trace.rs             | 13 +++++++++++
 5 files changed, 54 insertions(+)
drivers/android/binder/allocation.rs +1 -0
index 7f65a9c3a..8c0f94463 100644
@@ -208,6 +208,7 @@ pub(crate) fn translate_fds(&mut self) -> Result<TranslatedFds> {
             let res = FileDescriptorReservation::get_unused_fd_flags(bindings::O_CLOEXEC)?;
             let fd = res.reserved_fd();
             self.write::<u32>(file_info.buffer_offset, &fd)?;
+            crate::trace::trace_transaction_fd_recv(self.debug_id, fd, file_info.buffer_offset);
 
             reservations.push(
                 Reservation {
drivers/android/binder/rust_binder.h +5 -0
index d2284726c..3936b9d0b 100644
@@ -99,4 +99,9 @@ static inline size_t rust_binder_node_debug_id(rust_binder_node t)
 	return *(size_t *) (t + RUST_BINDER_LAYOUT.n.debug_id);
 }
 
+static inline binder_uintptr_t rust_binder_node_ptr(rust_binder_node t)
+{
+	return *(binder_uintptr_t *) (t + RUST_BINDER_LAYOUT.n.ptr);
+}
+
 #endif
drivers/android/binder/rust_binder_events.h +34 -0
index 8a0b72bf0..572a4bf7d 100644
@@ -111,6 +111,40 @@ TRACE_EVENT(binder_transaction_received,
 	TP_printk("transaction=%d", __entry->debug_id)
 );
 
+TRACE_EVENT(binder_transaction_fd_send,
+	TP_PROTO(int t_debug_id, int fd, size_t offset),
+	TP_ARGS(t_debug_id, fd, offset),
+	TP_STRUCT__entry(
+		__field(int, debug_id)
+		__field(int, fd)
+		__field(size_t, offset)
+	),
+	TP_fast_assign(
+		__entry->debug_id = t_debug_id;
+		__entry->fd = fd;
+		__entry->offset = offset;
+	),
+	TP_printk("transaction=%d src_fd=%d offset=%zu",
+		  __entry->debug_id, __entry->fd, __entry->offset)
+);
+
+TRACE_EVENT(binder_transaction_fd_recv,
+	TP_PROTO(int t_debug_id, int fd, size_t offset),
+	TP_ARGS(t_debug_id, fd, offset),
+	TP_STRUCT__entry(
+		__field(int, debug_id)
+		__field(int, fd)
+		__field(size_t, offset)
+	),
+	TP_fast_assign(
+		__entry->debug_id = t_debug_id;
+		__entry->fd = fd;
+		__entry->offset = offset;
+	),
+	TP_printk("transaction=%d dest_fd=%d offset=%zu",
+		  __entry->debug_id, __entry->fd, __entry->offset)
+);
+
 #endif /* _RUST_BINDER_TRACE_H */
 
 /* This part must be outside protection */
drivers/android/binder/thread.rs +1 -0
index 15657d6e9..7301fc26d 100644
@@ -706,6 +706,7 @@ fn translate_object(
                     core::mem::offset_of!(uapi::binder_fd_object, __bindgen_anon_1.fd);
 
                 let field_offset = offset + FD_FIELD_OFFSET;
+                crate::trace::trace_transaction_fd_send(view.alloc.debug_id, fd, field_offset);
 
                 view.alloc.info_add_fd(file, field_offset, false)?;
             }
drivers/android/binder/trace.rs +13 -0
index d96afdb79..c6f39d833 100644
@@ -18,6 +18,8 @@
     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);
 }
 
 #[inline]
@@ -77,3 +79,14 @@ 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) }
+}
Signature · 1 line
2.52.0
#6 lore raw

PATCH v3 6/6 rust_binder: add `command`/`return` tracepoints

In reply to Mohamad Alsadhan

Add Rust Binder command and return tracepoint declarations and wire them in where BC commands are parsed and BR return codes are emitted to userspace.

---
 drivers/android/binder/rust_binder_events.h | 32 +++++++++++++++++++++++++++++
 drivers/android/binder/rust_binder_main.rs  |  1 +
 drivers/android/binder/thread.rs            |  1 +
 drivers/android/binder/trace.rs             | 13 ++++++++++++
 4 files changed, 47 insertions(+)
drivers/android/binder/rust_binder_events.h +32 -0
index 572a4bf7d..1a446787c 100644
@@ -145,6 +145,38 @@ TRACE_EVENT(binder_transaction_fd_recv,
 		  __entry->debug_id, __entry->fd, __entry->offset)
 );
 
+TRACE_EVENT(binder_command,
+	TP_PROTO(uint32_t cmd),
+	TP_ARGS(cmd),
+	TP_STRUCT__entry(
+		__field(uint32_t, cmd)
+	),
+	TP_fast_assign(
+		__entry->cmd = cmd;
+	),
+	TP_printk("cmd=0x%x %s",
+		  __entry->cmd,
+		  _IOC_NR(__entry->cmd) < ARRAY_SIZE(binder_command_strings) ?
+			  binder_command_strings[_IOC_NR(__entry->cmd)] :
+			  "unknown")
+);
+
+TRACE_EVENT(binder_return,
+	TP_PROTO(uint32_t cmd),
+	TP_ARGS(cmd),
+	TP_STRUCT__entry(
+		__field(uint32_t, cmd)
+	),
+	TP_fast_assign(
+		__entry->cmd = cmd;
+	),
+	TP_printk("cmd=0x%x %s",
+		  __entry->cmd,
+		  _IOC_NR(__entry->cmd) < ARRAY_SIZE(binder_return_strings) ?
+			  binder_return_strings[_IOC_NR(__entry->cmd)] :
+			  "unknown")
+);
+
 #endif /* _RUST_BINDER_TRACE_H */
 
 /* This part must be outside protection */
drivers/android/binder/rust_binder_main.rs +1 -0
index aa5f2a75a..1028e0a8a 100644
@@ -116,6 +116,7 @@ fn new(writer: UserSliceWriter, thread: &'a Thread) -> Self {
     /// Write a return code back to user space.
     /// Should be a `BR_` constant from [`defs`] e.g. [`defs::BR_TRANSACTION_COMPLETE`].
     fn write_code(&mut self, code: u32) -> Result {
+        crate::trace::trace_return(code);
         stats::GLOBAL_STATS.inc_br(code);
         self.thread.process.stats.inc_br(code);
         self.writer.write(&code)
drivers/android/binder/thread.rs +1 -0
index 7301fc26d..ef7fba700 100644
@@ -1324,6 +1324,7 @@ fn write(self: &Arc<Self>, req: &mut BinderWriteRead) -> Result {
         while reader.len() >= size_of::<u32>() && self.inner.lock().return_work.is_unused() {
             let before = reader.len();
             let cmd = reader.read::<u32>()?;
+            crate::trace::trace_command(cmd);
             GLOBAL_STATS.inc_bc(cmd);
             self.process.stats.inc_bc(cmd);
             match cmd {
drivers/android/binder/trace.rs +13 -0
index c6f39d833..5539672d7 100644
@@ -20,6 +20,8 @@
     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);
 }
 
 #[inline]
@@ -90,3 +92,14 @@ pub(crate) fn trace_transaction_fd_recv(t_debug_id: usize, fd: u32, offset: usiz
     // 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) }
+}
Signature · 1 line
2.52.0