diff options
| author | Alasdair | 2020-08-25 17:20:00 +0100 |
|---|---|---|
| committer | Alasdair | 2020-08-25 17:20:00 +0100 |
| commit | 14e5c79b1c6943c88ab36ccc46f073674a76e16c (patch) | |
| tree | e0dbddb46ddbbba49d60081cb613737f26983b6e | |
| parent | 758a5b23ff0426ab6cf880d181fce1552b7f3ca7 (diff) | |
Add function sail_set_coverage_file to sail_coverage header
Can be set by C emulator to control where coverage information is
written
| -rw-r--r-- | lib/coverage/src/lib.rs | 28 | ||||
| -rw-r--r-- | lib/sail_coverage.h | 8 | ||||
| -rw-r--r-- | src/jib/c_backend.mli | 2 | ||||
| -rw-r--r-- | src/sail.ml | 2 |
4 files changed, 25 insertions, 15 deletions
diff --git a/lib/coverage/src/lib.rs b/lib/coverage/src/lib.rs index 003f8b88..34261eae 100644 --- a/lib/coverage/src/lib.rs +++ b/lib/coverage/src/lib.rs @@ -3,7 +3,7 @@ use lazy_static::lazy_static; use std::collections::HashSet; use std::ffi::{CStr, CString}; -use std::fs::{OpenOptions, File}; +use std::fs::{File, OpenOptions}; use std::io::Write; use std::os::raw::{c_char, c_int}; use std::sync::Mutex; @@ -20,6 +20,7 @@ struct Span { lazy_static! { static ref BRANCHES: Mutex<HashSet<Span>> = Mutex::new(HashSet::new()); static ref FUNCTIONS: Mutex<HashSet<Span>> = Mutex::new(HashSet::new()); + static ref OUTPUT_FILE: Mutex<String> = Mutex::new("sail_coverage".to_string()); } fn function_entry(_function_name: &CStr, span: Span) { @@ -30,15 +31,13 @@ fn branch_taken(_branch_id: i32, span: Span) { BRANCHES.lock().unwrap().insert(span); } -fn branch_reached(_branch_id: i32, _span: Span) { - () -} +fn branch_reached(_branch_id: i32, _span: Span) {} fn write_locations(file: &mut File, kind: char, spans: &Mutex<HashSet<Span>>) -> bool { for span in spans.lock().unwrap().iter() { - let res = write!( + let res = writeln!( file, - "{} \"{}\", {}, {}, {}, {}\n", + "{} \"{}\", {}, {}, {}, {}", kind, span.sail_file.to_string_lossy(), span.line1, @@ -49,18 +48,22 @@ fn write_locations(file: &mut File, kind: char, spans: &Mutex<HashSet<Span>>) -> if res.is_err() { return false; } - }; + } true } #[no_mangle] pub extern "C" fn sail_coverage_exit() -> c_int { - if let Ok(mut file) = OpenOptions::new().create(true).append(true).open("sail_coverage") { + if let Ok(mut file) = OpenOptions::new() + .create(true) + .append(true) + .open(&*OUTPUT_FILE.lock().unwrap()) + { if !write_locations(&mut file, 'B', &BRANCHES) { - return 1 + return 1; } if !write_locations(&mut file, 'F', &FUNCTIONS) { - return 1 + return 1; } 0 } else { @@ -69,6 +72,11 @@ pub extern "C" fn sail_coverage_exit() -> c_int { } #[no_mangle] +pub unsafe extern "C" fn sail_set_coverage_file(output_file: *const c_char) { + *OUTPUT_FILE.lock().unwrap() = CStr::from_ptr(output_file).to_string_lossy().to_string() +} + +#[no_mangle] pub unsafe extern "C" fn sail_function_entry( function_name: *const c_char, sail_file: *const c_char, diff --git a/lib/sail_coverage.h b/lib/sail_coverage.h index 92f78f9c..487b9dce 100644 --- a/lib/sail_coverage.h +++ b/lib/sail_coverage.h @@ -3,10 +3,12 @@ int sail_coverage_exit(void); -void sail_function_entry(char *function_name, char *sail_file, int l1, int c1, int l2, int c2); +void sail_set_coverage_file(const char *output_file); -void sail_branch_taken(int branch_id, char *sail_file, int l1, int c1, int l2, int c2); +void sail_function_entry(const char *function_name, const char *sail_file, int l1, int c1, int l2, int c2); -void sail_branch_reached(int branch_id, char *sail_file, int l1, int c1, int l2, int c2); +void sail_branch_taken(int branch_id, const char *sail_file, int l1, int c1, int l2, int c2); + +void sail_branch_reached(int branch_id, const char *sail_file, int l1, int c1, int l2, int c2); #endif diff --git a/src/jib/c_backend.mli b/src/jib/c_backend.mli index 5f2d5211..2d3d3c2b 100644 --- a/src/jib/c_backend.mli +++ b/src/jib/c_backend.mli @@ -89,7 +89,7 @@ val opt_extra_params : string option ref val opt_extra_arguments : string option ref val opt_branch_coverage : out_channel option ref - + (** Optimization flags *) val optimize_primops : bool ref diff --git a/src/sail.ml b/src/sail.ml index 31fc7717..d07ea216 100644 --- a/src/sail.ml +++ b/src/sail.ml @@ -234,7 +234,7 @@ let options = Arg.align ([ " remove comma separated list of functions from C output, replacing them with unit"); ( "-c_coverage", Arg.String (fun str -> C_backend.opt_branch_coverage := Some (open_out str)), - " output file for C code instrumention to track branch coverage"); + "<file> Turn on coverage tracking and output information about all branches and functions to a file"); ( "-elf", Arg.String (fun elf -> opt_process_elf := Some elf), " process an ELF file so that it can be executed by compiled C code"); |
