diff options
author | 2025-02-17 17:29:21 +1100 | |
---|---|---|
committer | 2025-02-19 14:00:21 +1100 | |
commit | 7e6c6299e56e79b71ce11eb9d3b600e9f6cedfdd (patch) | |
tree | 955599dd4241fdc07b2d3e5f374a0998c1b1ea48 | |
parent | dc385d03753daf0cb46709b154e2128617258e47 (diff) |
system/rust: Add some debug code to try and track down crash
This has no functional change; it just contains logs that might help us
diagnose the issue.
Bug: 356462170
Test: m com.android.btservices
Flag: EXEMPT refactor only
Change-Id: Iad9cdb0d61bc815a7a2bc97c24fae7cbe18d48e7
-rw-r--r-- | android/app/jni/com_android_bluetooth_gatt.cpp | 6 | ||||
-rw-r--r-- | system/btcore/include/module.h | 3 | ||||
-rw-r--r-- | system/btcore/src/module.cc | 6 | ||||
-rw-r--r-- | system/rust/src/core/ffi.rs | 4 | ||||
-rw-r--r-- | system/rust/src/core/ffi/module.cc | 1 | ||||
-rw-r--r-- | system/rust/src/core/mod.rs | 4 | ||||
-rw-r--r-- | system/rust/src/lib.rs | 17 |
7 files changed, 38 insertions, 3 deletions
diff --git a/android/app/jni/com_android_bluetooth_gatt.cpp b/android/app/jni/com_android_bluetooth_gatt.cpp index 9f6c5c45ed..357e23f5d0 100644 --- a/android/app/jni/com_android_bluetooth_gatt.cpp +++ b/android/app/jni/com_android_bluetooth_gatt.cpp @@ -49,6 +49,7 @@ #include "hardware/distance_measurement_interface.h" #include "main/shim/le_scanning_manager.h" #include "rust/cxx.h" +#include "src/core/ffi/module.h" #include "src/gatt/ffi.rs.h" #include "types/bluetooth/uuid.h" #include "types/raw_address.h" @@ -586,6 +587,11 @@ static const btgatt_client_callbacks_t sGattClientCallbacks = { */ void btgatts_register_app_cb(int status, int server_if, const Uuid& uuid) { + // TODO(b/356462170): Remove this when we have fixed the bug + if (!is_module_started(&rust_module)) { + log::error("Rust module isn't started! scan_manager_refactor={}", + com::android::bluetooth::flags::scan_manager_refactor()); + } bluetooth::gatt::open_server(server_if); std::shared_lock<std::shared_mutex> lock(callbacks_mutex); CallbackEnv sCallbackEnv(__func__); diff --git a/system/btcore/include/module.h b/system/btcore/include/module.h index 6ea11c163b..7402ca161c 100644 --- a/system/btcore/include/module.h +++ b/system/btcore/include/module.h @@ -55,3 +55,6 @@ void module_shut_down(const module_t* module); // Clean up the provided module. |module| may not be NULL. // If not initialized, does nothing. void module_clean_up(const module_t* module); + +// Returns true if the module is started. +bool is_module_started(const module_t* module); diff --git a/system/btcore/src/module.cc b/system/btcore/src/module.cc index ec34b32636..62ab21ab34 100644 --- a/system/btcore/src/module.cc +++ b/system/btcore/src/module.cc @@ -160,3 +160,9 @@ static void set_module_state(const module_t* module, module_state_t state) { std::lock_guard<std::mutex> lock(metadata_mutex); metadata[module] = state; } + +bool is_module_started(const module_t* module) { + std::lock_guard<std::mutex> lock(metadata_mutex); + auto map_ptr = metadata.find(module); + return map_ptr != metadata.end() && map_ptr->second == MODULE_STATE_STARTED; +} diff --git a/system/rust/src/core/ffi.rs b/system/rust/src/core/ffi.rs index 506bfd0bad..c67e8b5514 100644 --- a/system/rust/src/core/ffi.rs +++ b/system/rust/src/core/ffi.rs @@ -15,7 +15,7 @@ // TODO(b/290018030): Remove this and add proper safety comments. #![allow(clippy::undocumented_unsafe_blocks)] -use crate::core::{start, stop}; +use crate::core::{set_disabled_in_test, start, stop}; use cxx::{type_id, ExternType}; pub use inner::*; @@ -83,5 +83,7 @@ mod inner { ); fn stop(); + + fn set_disabled_in_test(); } } diff --git a/system/rust/src/core/ffi/module.cc b/system/rust/src/core/ffi/module.cc index edd2b13b58..deaeb16cb6 100644 --- a/system/rust/src/core/ffi/module.cc +++ b/system/rust/src/core/ffi/module.cc @@ -60,6 +60,7 @@ future_t* Start() { bluetooth::log::error( "GATT profile not started, so we cannot start the Rust loop - this " "happens only in tests."); + bluetooth::rust_shim::set_disabled_in_test(); bluetooth::rust_shim::FutureReady(*fut); return fut; } diff --git a/system/rust/src/core/mod.rs b/system/rust/src/core/mod.rs index ebcd65c353..51b913ec2b 100644 --- a/system/rust/src/core/mod.rs +++ b/system/rust/src/core/mod.rs @@ -27,3 +27,7 @@ fn start( fn stop() { RustModuleRunner::stop(); } + +fn set_disabled_in_test() { + RustModuleRunner::set_disabled_in_test(); +} diff --git a/system/rust/src/lib.rs b/system/rust/src/lib.rs index f88e635911..d68e3624d4 100644 --- a/system/rust/src/lib.rs +++ b/system/rust/src/lib.rs @@ -40,7 +40,11 @@ enum RustModuleRunner { NotRunning, /// Main event loop is running and messages can be processed. Use [`RustModuleRunner::send`] to /// queue a callback to be sent. - Running { thread: JoinHandle<()>, tx: mpsc::UnboundedSender<BoxedMainThreadCallback> }, + Running { + thread: JoinHandle<()>, + tx: mpsc::UnboundedSender<BoxedMainThreadCallback>, + }, + DisabledInTest, } /// The ModuleViews lets us access all publicly accessible Rust modules from @@ -96,6 +100,15 @@ impl RustModuleRunner { // Wait for the thread to terminate. let _ = thread.join(); } + Self::DisabledInTest => {} + } + } + + pub fn set_disabled_in_test() { + let mut runner = GLOBAL_MODULE_RUNNER.lock().unwrap(); + match &*runner { + RustModuleRunner::NotRunning => *runner = Self::DisabledInTest, + _ => warn!("Unexpected state {:?}", &*runner), } } @@ -151,8 +164,8 @@ impl RustModuleRunner { #[allow(dead_code)] fn send(&self, f: BoxedMainThreadCallback) -> Result<(), (String, BoxedMainThreadCallback)> { match self { - Self::NotRunning => Err(("Not running".to_string(), f)), Self::Running { tx, .. } => tx.send(f).map_err(|e| ("Failed to send".to_string(), e.0)), + _ => Err((format!("Bad state {self:?}"), f)), } } } |