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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Low level API to the Tracy Client
//!
//! This crate builds the C++ tracy client and has no external dependencies.
//!
//! For a higher-level API consider `tracy-client`.
//!
//! # Important note
//!
//! Simply depending on this crate is sufficient for tracy to be enabled at program startup, even
//! if none of the APIs provided by this crate are invoked. Tracy will broadcast discovery packets
//! to the local network and expose the data it collects in the background to that same network.
//! Traces collected by Tracy may include source and assembly code as well.
//!
//! As thus, you may want make sure to only enable the `tracy-client-sys` crate conditionally, via
//! the `enable` feature flag provided by this crate.

use std::os::raw::{c_char, c_int};
use std::ffi::c_void;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ___tracy_source_location_data {
    pub name: *const c_char,
    pub function: *const c_char,
    pub file: *const c_char,
    pub line: u32,
    pub color: u32,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct TracyCZoneCtx {
    pub id: u32,
    pub active: c_int,
}

#[cfg(feature="enable")]
macro_rules! enabled_fn {
    (pub $($tokens: tt)*) => {
        extern "C" { pub $($tokens)*; }
    };
}

#[cfg(not(feature="enable"))]
macro_rules! enabled_fn {
    (pub $($tokens: tt)*) => {
        #[allow(non_snake_case, unused_variables)]
        #[inline(always)]
        pub unsafe extern "C" $($tokens)* { std::mem::zeroed() }
    };
}

enabled_fn! { pub fn ___tracy_init_thread() }
enabled_fn! { pub fn ___tracy_alloc_srcloc(
    line: u32,
    source: *const c_char,
    source_len: usize,
    function: *const c_char,
    function_len: usize
) -> u64 }
enabled_fn! { pub fn ___tracy_alloc_srcloc_name(
    line: u32,
    source: *const c_char,
    source_len: usize,
    function: *const c_char,
    function_len: usize,
    name: *const c_char,
    name_len: usize
) -> u64 }
enabled_fn! { pub fn ___tracy_emit_zone_begin(
    srcloc: *const ___tracy_source_location_data,
    active: c_int
) -> TracyCZoneCtx }
enabled_fn! { pub fn ___tracy_emit_zone_begin_callstack(
    srcloc: *const ___tracy_source_location_data,
    depth: c_int,
    active: c_int
) -> TracyCZoneCtx }
enabled_fn! { pub fn ___tracy_emit_zone_begin_alloc(
    srcloc: u64,
    active: c_int
) -> TracyCZoneCtx }
enabled_fn! { pub fn ___tracy_emit_zone_begin_alloc_callstack(
    srcloc: u64,
    depth: c_int,
    active: c_int
) -> TracyCZoneCtx }
enabled_fn! { pub fn ___tracy_emit_zone_end(
    ctx: TracyCZoneCtx
) }
enabled_fn! { pub fn ___tracy_emit_zone_text(
    ctx: TracyCZoneCtx,
    txt: *const c_char,
    size: usize
) }
enabled_fn! { pub fn ___tracy_emit_zone_color(
    ctx: TracyCZoneCtx,
    color: u32,
) }
enabled_fn! { pub fn ___tracy_emit_zone_name(
    ctx: TracyCZoneCtx,
    txt: *const c_char,
    size: usize
) }
enabled_fn! { pub fn ___tracy_emit_zone_value(
    ctx: TracyCZoneCtx,
    value: u64
) }
enabled_fn! { pub fn ___tracy_emit_memory_alloc(
    ptr: *const c_void,
    size: usize,
    secure: c_int
) }
enabled_fn! { pub fn ___tracy_emit_memory_alloc_callstack(
    ptr: *const c_void,
    size: usize,
    depth: c_int,
    secure: c_int
) }
enabled_fn! { pub fn ___tracy_emit_memory_free(
    ptr: *const c_void,
    secure: c_int
) }
enabled_fn! { pub fn ___tracy_emit_memory_free_callstack(
    ptr: *const c_void,
    depth: c_int,
    secure: c_int
) }
enabled_fn! { pub fn ___tracy_emit_message(
    txt: *const c_char,
    size: usize,
    callstack: c_int
) }
enabled_fn! { pub fn ___tracy_emit_messageL(
    txt: *const c_char,
    callstack: c_int
) }
enabled_fn! { pub fn ___tracy_emit_messageC(
    txt: *const c_char,
    size: usize, color: u32,
    callstack: c_int
) }
enabled_fn! { pub fn ___tracy_emit_messageLC(
    txt: *const c_char,
    color: u32,
    callstack: c_int) }
enabled_fn! { pub fn ___tracy_emit_frame_mark(
    name: *const c_char
) }
enabled_fn! { pub fn ___tracy_emit_frame_mark_start(
    name: *const c_char
) }
enabled_fn! { pub fn ___tracy_emit_frame_mark_end(
    name: *const c_char
) }
enabled_fn! { pub fn ___tracy_emit_frame_image(
    image: *const c_void,
    w: u16,
    h: u16,
    offset: u8,
    flip: c_int
) }
enabled_fn! { pub fn ___tracy_emit_plot(
    name: *const c_char,
    val: f64
) }
enabled_fn! { pub fn ___tracy_emit_message_appinfo(
    txt: *const c_char,
    size: usize
) }

enabled_fn! { pub fn ___tracy_set_thread_name(
    name: *const c_char,
) }

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn emit_zone() {
        unsafe {
            ___tracy_init_thread();
            let srcloc = ___tracy_source_location_data {
                name: b"name\0".as_ptr() as _,
                function: b"function\0".as_ptr() as _,
                file: b"file\0".as_ptr() as _,
                line: 42,
                color: 0,
            };
            let zone_ctx = ___tracy_emit_zone_begin(&srcloc, 1);
            ___tracy_emit_zone_end(zone_ctx);
        }
    }

    #[test]
    fn emit_message_no_null() {
        unsafe {
            ___tracy_init_thread();
            ___tracy_emit_message(b"hello world".as_ptr() as _, 11, 1);
        }
    }
}