1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci//! Panic trigger for Rust.
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ciextern crate panic_handler;
19800b99b8Sopenharmony_ciextern crate stacktrace_rust;
20800b99b8Sopenharmony_ci
21800b99b8Sopenharmony_ciuse std::{panic, thread};
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_ci/// function main
24800b99b8Sopenharmony_cifn main() {
25800b99b8Sopenharmony_ci    let args: Vec<String> = std::env::args().collect();
26800b99b8Sopenharmony_ci    panic_handler::init();
27800b99b8Sopenharmony_ci    if args.len() > 1 {
28800b99b8Sopenharmony_ci        test_panic(&args[1]);
29800b99b8Sopenharmony_ci    } else {
30800b99b8Sopenharmony_ci        println!("Invalid arguments.");
31800b99b8Sopenharmony_ci    }
32800b99b8Sopenharmony_ci}
33800b99b8Sopenharmony_ci
34800b99b8Sopenharmony_cifn test_panic(pt: &String) {
35800b99b8Sopenharmony_ci    if pt == "main" {
36800b99b8Sopenharmony_ci        panic_main();
37800b99b8Sopenharmony_ci    } else if pt == "child" {
38800b99b8Sopenharmony_ci        panic_child();
39800b99b8Sopenharmony_ci    } else if pt == "multi" {
40800b99b8Sopenharmony_ci        get_trace_in_multi_thread();
41800b99b8Sopenharmony_ci    }else if pt == "print_trace" {
42800b99b8Sopenharmony_ci        stacktrace_rust::print_trace(1);
43800b99b8Sopenharmony_ci    } else if pt == "get_trace" {
44800b99b8Sopenharmony_ci        let ret = stacktrace_rust::get_trace(false);
45800b99b8Sopenharmony_ci        println!("{}", ret);
46800b99b8Sopenharmony_ci    }
47800b99b8Sopenharmony_ci}
48800b99b8Sopenharmony_ci
49800b99b8Sopenharmony_cifn panic_main() {
50800b99b8Sopenharmony_ci    panic!("panic in main thread");
51800b99b8Sopenharmony_ci}
52800b99b8Sopenharmony_ci
53800b99b8Sopenharmony_cifn panic_child() {
54800b99b8Sopenharmony_ci    let ret = thread::spawn(move || {
55800b99b8Sopenharmony_ci        panic!("panic in child thread");
56800b99b8Sopenharmony_ci    }).join();
57800b99b8Sopenharmony_ci    println!("{:?}", ret);
58800b99b8Sopenharmony_ci}
59800b99b8Sopenharmony_ci
60800b99b8Sopenharmony_cifn get_trace_in_multi_thread() {
61800b99b8Sopenharmony_ci    let mut handles = vec![];
62800b99b8Sopenharmony_ci    for _ in 0..50 {
63800b99b8Sopenharmony_ci        let handle = thread::spawn(move || {
64800b99b8Sopenharmony_ci            let trace = stacktrace_rust::get_trace(false);
65800b99b8Sopenharmony_ci            println!("{}", trace);
66800b99b8Sopenharmony_ci        });
67800b99b8Sopenharmony_ci        handles.push(handle);
68800b99b8Sopenharmony_ci    }
69800b99b8Sopenharmony_ci    for handle in handles {
70800b99b8Sopenharmony_ci        handle.join().unwrap();
71800b99b8Sopenharmony_ci    }
72800b99b8Sopenharmony_ci}
73