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//! Panic trigger for Rust. 17 18extern crate panic_handler; 19extern crate stacktrace_rust; 20 21use std::{panic, thread}; 22 23/// function main 24fn main() { 25 let args: Vec<String> = std::env::args().collect(); 26 panic_handler::init(); 27 if args.len() > 1 { 28 test_panic(&args[1]); 29 } else { 30 println!("Invalid arguments."); 31 } 32} 33 34fn test_panic(pt: &String) { 35 if pt == "main" { 36 panic_main(); 37 } else if pt == "child" { 38 panic_child(); 39 } else if pt == "multi" { 40 get_trace_in_multi_thread(); 41 }else if pt == "print_trace" { 42 stacktrace_rust::print_trace(1); 43 } else if pt == "get_trace" { 44 let ret = stacktrace_rust::get_trace(false); 45 println!("{}", ret); 46 } 47} 48 49fn panic_main() { 50 panic!("panic in main thread"); 51} 52 53fn panic_child() { 54 let ret = thread::spawn(move || { 55 panic!("panic in child thread"); 56 }).join(); 57 println!("{:?}", ret); 58} 59 60fn get_trace_in_multi_thread() { 61 let mut handles = vec![]; 62 for _ in 0..50 { 63 let handle = thread::spawn(move || { 64 let trace = stacktrace_rust::get_trace(false); 65 println!("{}", trace); 66 }); 67 handles.push(handle); 68 } 69 for handle in handles { 70 handle.join().unwrap(); 71 } 72} 73