forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#81469 - tweksteen:android_set_message, r=m-…
…ou-se android: set abort message Android has the ability to supply an abort message [1]. This message is automatically included in the debug trace, which helps debugging [2]. Modify panic_abort to populate this message before calling abort(). [1] https://android.googlesource.com/platform/bionic/+/master/libc/include/android/set_abort_message.h [2] https://source.android.com/devices/tech/debug/native-crash
- Loading branch information
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use alloc::string::String; | ||
use alloc::vec::Vec; | ||
use core::mem::transmute; | ||
use core::panic::BoxMeUp; | ||
|
||
const ANDROID_SET_ABORT_MESSAGE: &[u8] = b"android_set_abort_message\0"; | ||
type SetAbortMessageType = unsafe extern "C" fn(*const libc::c_char) -> (); | ||
|
||
// Forward the abort message to libc's android_set_abort_message. The fallible allocator is used | ||
// to avoid panicking, as this function may already be called as part of a failed allocation. | ||
// | ||
// Weakly resolve the symbol for android_set_abort_message. This function is only available | ||
// for API >= 21. | ||
pub(crate) unsafe fn android_set_abort_message(payload: *mut &mut dyn BoxMeUp) { | ||
let func_addr = | ||
libc::dlsym(libc::RTLD_DEFAULT, ANDROID_SET_ABORT_MESSAGE.as_ptr() as *const libc::c_char) | ||
as usize; | ||
if func_addr == 0 { | ||
return; | ||
} | ||
|
||
let payload = (*payload).get(); | ||
let msg = match payload.downcast_ref::<&'static str>() { | ||
Some(msg) => msg.as_bytes(), | ||
None => match payload.downcast_ref::<String>() { | ||
Some(msg) => msg.as_bytes(), | ||
None => &[], | ||
}, | ||
}; | ||
if msg.is_empty() { | ||
return; | ||
} | ||
|
||
let size = msg.len() + 1; | ||
let mut v = Vec::new(); | ||
if v.try_reserve(size).is_err() { | ||
return; | ||
} | ||
|
||
v.extend(msg); | ||
v.push(0); | ||
let func = transmute::<usize, SetAbortMessageType>(func_addr); | ||
func(v.as_ptr() as *const libc::c_char); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters