Skip to content

Commit

Permalink
Add --strip-path option to strip paths for stacktrace and crash line (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anfedotoff authored Feb 8, 2024
1 parent c63adfc commit 4db4706
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 48 deletions.
12 changes: 12 additions & 0 deletions casr/src/bin/casr-gdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ fn main() -> Result<()> {
.value_parser(clap::value_parser!(PathBuf))
.help("File with regular expressions for functions and file paths that should be ignored"),
)
.arg(
Arg::new("strip-path")
.long("strip-path")
.env("CASR_STRIP_PATH")
.action(ArgAction::Set)
.value_name("PREFIX")
.help("Path prefix to strip from stacktrace and crash line"),
)
.arg(
Arg::new("ARGS")
.action(ArgAction::Set)
Expand Down Expand Up @@ -257,6 +265,10 @@ fn main() -> Result<()> {
}
}

if let Some(path) = matches.get_one::<String>("strip-path") {
util::strip_paths(&mut report, &parsed_stacktrace, path);
}

//Output report
util::output_report(&report, &matches, &argv)
}
16 changes: 14 additions & 2 deletions casr/src/bin/casr-java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ fn main() -> Result<()> {
.value_name("FILE")
.help("File with regular expressions for functions and file paths that should be ignored"),
)
.arg(
Arg::new("strip-path")
.long("strip-path")
.env("CASR_STRIP_PATH")
.action(ArgAction::Set)
.value_name("PREFIX")
.help("Path prefix to strip from stacktrace and crash line"),
)
.arg(
Arg::new("ARGS")
.action(ArgAction::Set)
Expand Down Expand Up @@ -157,8 +165,8 @@ fn main() -> Result<()> {
// Call casr-san
return util::call_casr_san(&matches, &argv, "casr-java");
}

if let Ok(crash_line) = JavaStacktrace::parse_stacktrace(&report.stacktrace)?.crash_line() {
let stacktrace = JavaStacktrace::parse_stacktrace(&report.stacktrace)?;
if let Ok(crash_line) = stacktrace.crash_line() {
report.crashline = crash_line.to_string();
if let CrashLine::Source(mut debug) = crash_line {
// Modify DebugInfo to find sources
Expand Down Expand Up @@ -190,6 +198,10 @@ fn main() -> Result<()> {
}
}

if let Some(path) = matches.get_one::<String>("strip-path") {
util::strip_paths(&mut report, &stacktrace, path);
}

//Output report
util::output_report(&report, &matches, &argv)
}
16 changes: 14 additions & 2 deletions casr/src/bin/casr-js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ fn main() -> Result<()> {
.value_name("FILE")
.help("File with regular expressions for functions and file paths that should be ignored"),
)
.arg(
Arg::new("strip-path")
.long("strip-path")
.env("CASR_STRIP_PATH")
.action(ArgAction::Set)
.value_name("PREFIX")
.help("Path prefix to strip from stacktrace and crash line"),
)
.arg(
Arg::new("ARGS")
.action(ArgAction::Set)
Expand Down Expand Up @@ -155,8 +163,8 @@ fn main() -> Result<()> {
modified_argv[0] = path_to_tool.to_str().unwrap_or(argv[0]);
return util::call_casr_san(&matches, &modified_argv, "casr-js");
}

if let Ok(crash_line) = JsStacktrace::parse_stacktrace(&report.stacktrace)?.crash_line() {
let stacktrace = JsStacktrace::parse_stacktrace(&report.stacktrace)?;
if let Ok(crash_line) = stacktrace.crash_line() {
report.crashline = crash_line.to_string();
if let CrashLine::Source(debug) = crash_line {
if let Some(sources) = CrashReport::sources(&debug) {
Expand All @@ -165,6 +173,10 @@ fn main() -> Result<()> {
}
}

if let Some(path) = matches.get_one::<String>("strip-path") {
util::strip_paths(&mut report, &stacktrace, path);
}

//Output report
util::output_report(&report, &matches, &argv)
}
16 changes: 14 additions & 2 deletions casr/src/bin/casr-python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ fn main() -> Result<()> {
.value_name("FILE")
.help("File with regular expressions for functions and file paths that should be ignored"),
)
.arg(
Arg::new("strip-path")
.long("strip-path")
.env("CASR_STRIP_PATH")
.action(ArgAction::Set)
.value_name("PREFIX")
.help("Path prefix to strip from stacktrace"),
)
.arg(
Arg::new("ARGS")
.action(ArgAction::Set)
Expand Down Expand Up @@ -175,8 +183,8 @@ fn main() -> Result<()> {
// Call casr-san
return util::call_casr_san(&matches, &argv, "casr-python");
}

if let Ok(crash_line) = PythonStacktrace::parse_stacktrace(&report.stacktrace)?.crash_line() {
let stacktrace = PythonStacktrace::parse_stacktrace(&report.stacktrace)?;
if let Ok(crash_line) = stacktrace.crash_line() {
report.crashline = crash_line.to_string();
if let CrashLine::Source(debug) = crash_line {
if let Some(sources) = CrashReport::sources(&debug) {
Expand All @@ -185,6 +193,10 @@ fn main() -> Result<()> {
}
}

if let Some(path) = matches.get_one::<String>("strip-path") {
util::strip_paths(&mut report, &stacktrace, path);
}

//Output report
util::output_report(&report, &matches, &argv)
}
12 changes: 12 additions & 0 deletions casr/src/bin/casr-san.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ fn main() -> Result<()> {
.value_parser(clap::value_parser!(PathBuf))
.help("File with regular expressions for functions and file paths that should be ignored"),
)
.arg(
Arg::new("strip-path")
.long("strip-path")
.env("CASR_STRIP_PATH")
.action(ArgAction::Set)
.value_name("PREFIX")
.help("Path prefix to strip from stacktrace and crash line"),
)
.arg(
Arg::new("ARGS")
.action(ArgAction::Set)
Expand Down Expand Up @@ -286,5 +294,9 @@ fn main() -> Result<()> {
}
}

if let Some(path) = matches.get_one::<String>("strip-path") {
util::strip_paths(&mut report, &stacktrace, path);
}

util::output_report(&report, &matches, &argv)
}
32 changes: 31 additions & 1 deletion casr/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ extern crate libcasr;
use libcasr::cluster::{Cluster, ReportInfo};
use libcasr::report::CrashReport;
use libcasr::stacktrace::{
STACK_FRAME_FILEPATH_IGNORE_REGEXES, STACK_FRAME_FUNCTION_IGNORE_REGEXES,
Stacktrace, STACK_FRAME_FILEPATH_IGNORE_REGEXES, STACK_FRAME_FUNCTION_IGNORE_REGEXES,
};

use anyhow::{bail, Context, Result};
use clap::ArgMatches;
use gdb_command::stacktrace::StacktraceExt;
use is_executable::IsExecutable;
use log::{info, warn};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
Expand Down Expand Up @@ -558,3 +559,32 @@ pub fn save_reports(reports: &Vec<PathBuf>, dir: &str) -> Result<()> {
}
Ok(())
}

/// Strip paths for stacktrace and crash line in CrashReport
///
/// # Arguments
///
/// * `report` - CASR crash report struct
///
/// * 'stacktrace' - Stacktrace struct
///
/// * `prefix` - path prefix
pub fn strip_paths(report: &mut CrashReport, stacktrace: &Stacktrace, prefix: &str) {
let mut stripped_stacktrace = stacktrace.clone();
stripped_stacktrace.strip_prefix(prefix);
for (idx, (entry, stripped)) in stacktrace
.iter()
.zip(stripped_stacktrace.iter())
.enumerate()
{
if !stripped.debug.file.is_empty() {
report.stacktrace[idx] =
report.stacktrace[idx].replace(&entry.debug.file, &stripped.debug.file);
}
}
if !report.crashline.is_empty() {
if let Ok(stripped) = Path::new(&report.crashline).strip_prefix(prefix) {
report.crashline = stripped.display().to_string();
}
}
}
3 changes: 2 additions & 1 deletion casr/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2852,6 +2852,7 @@ fn test_casr_san() {

let output = Command::new(*EXE_CASR_SAN.read().unwrap())
.args(["--stdout", "--", &paths[1]])
.env("CASR_STRIP_PATH", env::current_dir().unwrap())
.output()
.expect("failed to start casr-san");

Expand Down Expand Up @@ -2884,7 +2885,7 @@ fn test_casr_san() {
report["CrashLine"]
.as_str()
.unwrap()
.contains("test_asan_df.cpp:8:5")
.eq("tests/casr_tests/test_asan_df.cpp:8:5")
// We build a test on ubuntu18 and run it on ubuntu20.
// Debug information is broken.
|| report["CrashLine"]
Expand Down
89 changes: 49 additions & 40 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ Create CASR reports (.casrep) from gdb execution
[ARGS]... Add "-- ./binary <arguments>" to run executable

Options:
-o, --output <REPORT> Path to save report. Path can be a directory, then report name
is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths that
should be ignored
-h, --help Print help
-V, --version Print version
-o, --output <REPORT> Path to save report. Path can be a directory, then report
name is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths
that should be ignored
--strip-path <PREFIX> Path prefix to strip from stacktrace and crash line [env:
CASR_STRIP_PATH=]
-h, --help Print help
-V, --version Print version

Example:

Expand All @@ -53,16 +55,18 @@ Create CASR reports (.casrep) from AddressSanitizer reports
[ARGS]... Add "-- ./binary <arguments>" to run executable

Options:
-o, --output <REPORT> Path to save report. Path can be a directory, then report name
is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths that
should be ignored
-h, --help Print help
-V, --version Print version
-o, --output <REPORT> Path to save report. Path can be a directory, then report
name is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths
that should be ignored
--strip-path <PREFIX> Path prefix to strip from stacktrace and crash line [env:
CASR_STRIP_PATH=]
-h, --help Print help
-V, --version Print version

Compile binary with ASAN:

Expand Down Expand Up @@ -126,16 +130,17 @@ Create CASR reports (.casrep) from python reports
[ARGS]... Add "-- <path> <arguments>" to run

Options:
-o, --output <REPORT> Path to save report. Path can be a directory, then report name
is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths that
should be ignored
-h, --help Print help
-V, --version Print version
-o, --output <REPORT> Path to save report. Path can be a directory, then report
name is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths
that should be ignored
--strip-path <PREFIX> Path prefix to strip from stacktrace [env: CASR_STRIP_PATH=]
-h, --help Print help
-V, --version Print version

Example:

Expand All @@ -161,6 +166,8 @@ Create CASR reports (.casrep) from java reports
that timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths
that should be ignored
--strip-path <PREFIX> Path prefix to strip from stacktrace and crash line [env:
CASR_STRIP_PATH=]
-h, --help Print help
-V, --version Print version

Expand All @@ -178,16 +185,18 @@ Create CASR reports (.casrep) from JavaScript crash reports
[ARGS]... Add "-- <path> <arguments>" to run

Options:
-o, --output <REPORT> Path to save report. Path can be a directory, then report name
is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths that
should be ignored
-h, --help Print help
-V, --version Print version
-o, --output <REPORT> Path to save report. Path can be a directory, then report
name is generated
--stdout Print CASR report to stdout
--stdin <FILE> Stdin file for program
-t, --timeout <SECONDS> Timeout (in seconds) for target execution, 0 value means that
timeout is disabled [default: 0]
--ignore <FILE> File with regular expressions for functions and file paths
that should be ignored
--strip-path <PREFIX> Path prefix to strip from stacktrace and crash line [env:
CASR_STRIP_PATH=]
-h, --help Print help
-V, --version Print version

Run casr-js:

Expand Down

0 comments on commit 4db4706

Please sign in to comment.