1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16//! stacktrace tools for Rust. 17 18use std::ffi::CStr; 19use std::os::raw::{c_char, c_int, c_uint}; 20use std::string::String; 21use std::sync::Mutex; 22 23static TRACE_MUTEX : Mutex<i32> = Mutex::new(0); 24 25#[link(name = "backtrace_local")] 26extern "C" { 27 fn PrintTrace(fd : c_int) -> bool; 28 fn GetTrace(skip_frame_num : c_uint) -> *const c_char; 29} 30 31/// Print Rust trace into File 32pub fn print_trace(fd : i32) -> bool { 33 unsafe { 34 PrintTrace(fd) 35 } 36} 37 38/// Get Rust trace by returned parameter 39#[allow(unused_variables)] 40pub fn get_trace(is_crash : bool) -> String { 41 unsafe { 42 let mutex = TRACE_MUTEX.lock().unwrap(); 43 let mut skip_frame_num = 0; 44 if is_crash { 45 skip_frame_num = 8; 46 } 47 let trace = GetTrace(skip_frame_num); 48 CStr::from_ptr(trace).to_str().unwrap().to_owned() 49 } 50}