1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! This is an ffi wrapper around rftrace-frontend, enabling calling it from c code
//! You can find a usage example in the [repository](https://github.com/hermit-os/rftrace/examples/c)
//! A lot of documentation can be found in the parent workspaces [readme](https://github.com/hermit-os/rftrace).

use std::ffi::CStr;
use std::os::raw::c_char;

pub type Events = rftrace_frontend::Events;

#[no_mangle]
/// Wraps rftrace_frontend::enable()
pub unsafe extern "C" fn rftrace_enable() {
    rftrace_frontend::enable();
}

#[no_mangle]
/// Wraps rftrace_frontend::disable();
pub unsafe extern "C" fn rftrace_disable() {
    rftrace_frontend::disable();
}

#[no_mangle]
/// Wraps rftrace_frontend::init();
pub unsafe extern "C" fn rftrace_init(max_event_count: usize, overwriting: bool) -> *mut Events {
    rftrace_frontend::init(max_event_count, overwriting)
}

#[no_mangle]
/// Wraps rftrace_frontend::dump_full_uftrace
pub unsafe extern "C" fn rftrace_dump_full_uftrace(
    events: *mut Events,
    out_dir: *const c_char,
    binary_name: *const c_char,
) -> i64 {
    let out_dir = CStr::from_ptr(out_dir).to_string_lossy().into_owned();
    let binary_name = CStr::from_ptr(binary_name).to_string_lossy().into_owned();

    if rftrace_frontend::dump_full_uftrace(&mut *events, &out_dir, &binary_name).is_err() {
        return -1;
    }
    0
}

#[no_mangle]
/// Wraps rftrace_frontend::dump_trace
pub unsafe extern "C" fn rftrace_dump_trace(events: *mut Events, outfile: *const c_char) -> i64 {
    let outfile = CStr::from_ptr(outfile).to_string_lossy().into_owned();

    if rftrace_frontend::dump_trace(&mut *events, &outfile).is_err() {
        return -1;
    }
    0
}

#[no_mangle]
pub extern "C" fn marker() -> u64 {
    1337
}