|
| 1 | +use casr::util; |
| 2 | +use libcasr::{ |
| 3 | + init_ignored_frames, |
| 4 | + lua::LuaException, |
| 5 | + report::CrashReport, |
| 6 | + severity::Severity, |
| 7 | + stacktrace::Filter, |
| 8 | + stacktrace::Stacktrace, |
| 9 | + stacktrace::{CrashLine, CrashLineExt}, |
| 10 | +}; |
| 11 | + |
| 12 | +use anyhow::{bail, Result}; |
| 13 | +use clap::{Arg, ArgAction, ArgGroup}; |
| 14 | +use std::path::{Path, PathBuf}; |
| 15 | +use std::process::Command; |
| 16 | + |
| 17 | +fn main() -> Result<()> { |
| 18 | + let matches = clap::Command::new("casr-lua") |
| 19 | + .version(clap::crate_version!()) |
| 20 | + .about("Create CASR reports (.casrep) from Lua reports") |
| 21 | + .term_width(90) |
| 22 | + .arg( |
| 23 | + Arg::new("output") |
| 24 | + .short('o') |
| 25 | + .long("output") |
| 26 | + .action(ArgAction::Set) |
| 27 | + .value_parser(clap::value_parser!(PathBuf)) |
| 28 | + .value_name("REPORT") |
| 29 | + .help( |
| 30 | + "Path to save report. Path can be a directory, then report name is generated", |
| 31 | + ), |
| 32 | + ) |
| 33 | + .arg( |
| 34 | + Arg::new("stdout") |
| 35 | + .action(ArgAction::SetTrue) |
| 36 | + .long("stdout") |
| 37 | + .help("Print CASR report to stdout"), |
| 38 | + ) |
| 39 | + .group( |
| 40 | + ArgGroup::new("out") |
| 41 | + .args(["stdout", "output"]) |
| 42 | + .required(true), |
| 43 | + ) |
| 44 | + .arg( |
| 45 | + Arg::new("stdin") |
| 46 | + .long("stdin") |
| 47 | + .action(ArgAction::Set) |
| 48 | + .value_parser(clap::value_parser!(PathBuf)) |
| 49 | + .value_name("FILE") |
| 50 | + .help("Stdin file for program"), |
| 51 | + ) |
| 52 | + .arg( |
| 53 | + Arg::new("timeout") |
| 54 | + .short('t') |
| 55 | + .long("timeout") |
| 56 | + .action(ArgAction::Set) |
| 57 | + .default_value("0") |
| 58 | + .value_name("SECONDS") |
| 59 | + .help("Timeout (in seconds) for target execution, 0 value means that timeout is disabled") |
| 60 | + .value_parser(clap::value_parser!(u64)) |
| 61 | + ) |
| 62 | + .arg( |
| 63 | + Arg::new("ignore") |
| 64 | + .long("ignore") |
| 65 | + .action(ArgAction::Set) |
| 66 | + .value_parser(clap::value_parser!(PathBuf)) |
| 67 | + .value_name("FILE") |
| 68 | + .help("File with regular expressions for functions and file paths that should be ignored"), |
| 69 | + ) |
| 70 | + .arg( |
| 71 | + Arg::new("strip-path") |
| 72 | + .long("strip-path") |
| 73 | + .env("CASR_STRIP_PATH") |
| 74 | + .action(ArgAction::Set) |
| 75 | + .value_name("PREFIX") |
| 76 | + .help("Path prefix to strip from stacktrace"), |
| 77 | + ) |
| 78 | + .arg( |
| 79 | + Arg::new("ARGS") |
| 80 | + .action(ArgAction::Set) |
| 81 | + .num_args(1..) |
| 82 | + .last(true) |
| 83 | + .required(true) |
| 84 | + .help("Add \"-- <path> <arguments>\" to run"), |
| 85 | + ) |
| 86 | + .get_matches(); |
| 87 | + |
| 88 | + init_ignored_frames!("lua"); |
| 89 | + if let Some(path) = matches.get_one::<PathBuf>("ignore") { |
| 90 | + util::add_custom_ignored_frames(path)?; |
| 91 | + } |
| 92 | + // Get program args. |
| 93 | + let argv: Vec<&str> = if let Some(argvs) = matches.get_many::<String>("ARGS") { |
| 94 | + argvs.map(|s| s.as_str()).collect() |
| 95 | + } else { |
| 96 | + bail!("Wrong arguments for starting program"); |
| 97 | + }; |
| 98 | + |
| 99 | + // Get stdin for target program. |
| 100 | + let stdin_file = util::stdin_from_matches(&matches)?; |
| 101 | + |
| 102 | + // Get timeout |
| 103 | + let timeout = *matches.get_one::<u64>("timeout").unwrap(); |
| 104 | + |
| 105 | + // Run program. |
| 106 | + let mut cmd = Command::new(argv[0]); |
| 107 | + if let Some(ref file) = stdin_file { |
| 108 | + cmd.stdin(std::fs::File::open(file)?); |
| 109 | + } |
| 110 | + if argv.len() > 1 { |
| 111 | + cmd.args(&argv[1..]); |
| 112 | + } |
| 113 | + let result = util::get_output(&mut cmd, timeout, true)?; |
| 114 | + let stderr = String::from_utf8_lossy(&result.stderr); |
| 115 | + |
| 116 | + // Create report. |
| 117 | + let mut report = CrashReport::new(); |
| 118 | + report.executable_path = argv[0].to_string(); |
| 119 | + if argv.len() > 1 { |
| 120 | + if let Some(fname) = Path::new(argv[0]).file_name() { |
| 121 | + let fname = fname.to_string_lossy(); |
| 122 | + if fname.starts_with("lua") && !fname.ends_with(".lua") && argv[1].ends_with(".lua") { |
| 123 | + report.executable_path = argv[1].to_string(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + report.proc_cmdline = argv.join(" "); |
| 128 | + let _ = report.add_os_info(); |
| 129 | + let _ = report.add_proc_environ(); |
| 130 | + |
| 131 | + // Extract lua exception |
| 132 | + let Some(exception) = LuaException::new(&stderr) else { |
| 133 | + bail!("Lua exception is not found!"); |
| 134 | + }; |
| 135 | + |
| 136 | + // Parse exception |
| 137 | + report.lua_report = exception.lua_report(); |
| 138 | + report.stacktrace = exception.extract_stacktrace()?; |
| 139 | + report.execution_class = exception.severity()?; |
| 140 | + if let Ok(crashline) = exception.crash_line() { |
| 141 | + report.crashline = crashline.to_string(); |
| 142 | + if let CrashLine::Source(debug) = crashline { |
| 143 | + if let Some(sources) = CrashReport::sources(&debug) { |
| 144 | + report.source = sources; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + let stacktrace = exception.parse_stacktrace()?; |
| 149 | + if let Some(path) = matches.get_one::<String>("strip-path") { |
| 150 | + util::strip_paths(&mut report, &stacktrace, path); |
| 151 | + } |
| 152 | + |
| 153 | + //Output report |
| 154 | + util::output_report(&report, &matches, &argv) |
| 155 | +} |
0 commit comments