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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

//! This crate provides an implementation of Fuchsia Diagnostic Streams, often referred to as
//! "logs."

#![warn(missing_docs)]

use bitfield::bitfield;
use fidl_fuchsia_diagnostics_stream::RawSeverity;
use std::borrow::Cow;
use tracing::{Level, Metadata};

pub use fidl_fuchsia_diagnostics::Severity;
pub use fidl_fuchsia_diagnostics_stream::{Argument, Record, Value, ValueUnknown};

pub mod encode;
pub mod parse;

/// The tracing format supports many types of records, we're sneaking in as a log message.
const TRACING_FORMAT_LOG_RECORD_TYPE: u8 = 9;

bitfield! {
    /// A header in the tracing format. Expected to precede every Record and Argument.
    ///
    /// The tracing format specifies [Record headers] and [Argument headers] as distinct types, but
    /// their layouts are the same in practice, so we represent both bitfields using the same
    /// struct.
    ///
    /// [Record headers]: https://fuchsia.dev/fuchsia-src/development/tracing/trace-format#record_header
    /// [Argument headers]: https://fuchsia.dev/fuchsia-src/development/tracing/trace-format#argument_header
    pub struct Header(u64);
    impl Debug;

    /// Record type.
    u8, raw_type, set_type: 3, 0;

    /// Record size as a multiple of 8 bytes.
    u16, size_words, set_size_words: 15, 4;

    /// String ref for the associated name, if any.
    u16, name_ref, set_name_ref: 31, 16;

    /// Boolean value, if any.
    bool, bool_val, set_bool_val: 32;

    /// Reserved for record-type-specific data.
    u16, value_ref, set_value_ref: 47, 32;

    /// Severity of the record, if any.
    u8, severity, set_severity: 63, 56;
}

impl Header {
    /// Sets the length of the item the header refers to. Panics if not 8-byte aligned.
    fn set_len(&mut self, new_len: usize) {
        assert_eq!(new_len % 8, 0, "encoded message must be 8-byte aligned");
        #[allow(clippy::bool_to_int_with_if)]
        self.set_size_words((new_len / 8) as u16 + u16::from(new_len % 8 > 0))
    }
}

/// Tag derived from metadata.
///
/// Unlike tags, metatags are not represented as strings and instead must be resolved from event
/// metadata. This means that they may resolve to different text for different events.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Metatag {
    /// The location of a span or event.
    ///
    /// The target is typically a module path, but this can be configured by a particular span or
    /// event when it is constructed.
    Target,
}

/// These literal values are specified by the tracing format:
///
/// https://fuchsia.dev/fuchsia-src/development/tracing/trace-format#argument_header
#[repr(u8)]
enum ArgType {
    Null = 0,
    I32 = 1,
    U32 = 2,
    I64 = 3,
    U64 = 4,
    F64 = 5,
    String = 6,
    Pointer = 7,
    Koid = 8,
    Bool = 9,
}

impl TryFrom<u8> for ArgType {
    type Error = parse::ParseError;
    fn try_from(b: u8) -> Result<Self, Self::Error> {
        Ok(match b {
            0 => ArgType::Null,
            1 => ArgType::I32,
            2 => ArgType::U32,
            3 => ArgType::I64,
            4 => ArgType::U64,
            5 => ArgType::F64,
            6 => ArgType::String,
            7 => ArgType::Pointer,
            8 => ArgType::Koid,
            9 => ArgType::Bool,
            _ => return Err(parse::ParseError::ValueOutOfValidRange),
        })
    }
}

#[derive(Clone)]
enum StringRef<'a> {
    Empty,
    Inline(Cow<'a, str>),
}

impl<'a> StringRef<'a> {
    fn mask(&self) -> u16 {
        match self {
            StringRef::Empty => 0,
            StringRef::Inline(s) => (s.len() as u16) | (1 << 15),
        }
    }

    fn for_str(string: &'a str) -> Self {
        match string.len() {
            0 => StringRef::Empty,
            _ => StringRef::Inline(Cow::Borrowed(string)),
        }
    }
}

impl<'a> From<StringRef<'a>> for String {
    fn from(string: StringRef<'a>) -> String {
        match string {
            StringRef::Empty => String::new(),
            StringRef::Inline(s) => s.to_string(),
        }
    }
}

impl<'a> ToString for StringRef<'a> {
    fn to_string(&self) -> String {
        self.clone().into()
    }
}

/// A type which has a `Severity`.
pub trait SeverityExt {
    /// Return the severity of this value.
    fn severity(&self) -> Severity;

    /// Return the raw severity of this value.
    fn raw_severity(&self) -> RawSeverity;
}

impl SeverityExt for Metadata<'_> {
    fn severity(&self) -> Severity {
        match *self.level() {
            Level::ERROR => Severity::Error,
            Level::WARN => Severity::Warn,
            Level::INFO => Severity::Info,
            Level::DEBUG => Severity::Debug,
            Level::TRACE => Severity::Trace,
        }
    }

    fn raw_severity(&self) -> RawSeverity {
        match *self.level() {
            Level::ERROR => Severity::Error.into_primitive(),
            Level::WARN => Severity::Warn.into_primitive(),
            Level::INFO => Severity::Info.into_primitive(),
            Level::DEBUG => Severity::Debug.into_primitive(),
            Level::TRACE => Severity::Trace.into_primitive(),
        }
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::{
            encode::{Encoder, EncodingError, MutableBuffer},
            parse::{parse_argument, try_parse_record, ParseResult},
        },
        fuchsia_zircon as zx,
        std::{fmt::Debug, io::Cursor},
    };

    const BUF_LEN: usize = 1024;

    pub(crate) fn assert_roundtrips<T>(
        val: T,
        encoder_method: impl Fn(&mut Encoder<Cursor<Vec<u8>>>, &T) -> Result<(), EncodingError>,
        parser: impl Fn(&[u8]) -> ParseResult<'_, T>,
        canonical: Option<&[u8]>,
    ) where
        T: Debug + PartialEq,
    {
        let mut encoder = Encoder::new(Cursor::new(vec![0; BUF_LEN]));
        encoder_method(&mut encoder, &val).unwrap();

        // next we'll parse the record out of a buf with padding after the record
        let (_, decoded_from_full) =
            nom::dbg_dmp(&parser, "roundtrip")(encoder.buf.get_ref()).unwrap();
        assert_eq!(val, decoded_from_full, "decoded version with trailing padding must match");

        if let Some(canonical) = canonical {
            let recorded = encoder.buf.get_ref().split_at(canonical.len()).0;
            assert_eq!(canonical, recorded, "encoded repr must match the canonical value provided");

            let (zero_buf, decoded) = nom::dbg_dmp(&parser, "roundtrip")(recorded).unwrap();
            assert_eq!(val, decoded, "decoded version must match what we tried to encode");
            assert_eq!(zero_buf.len(), 0, "must parse record exactly out of provided buffer");
        }
    }

    /// Bit pattern for the log record type, severity info, and a record of two words: one header,
    /// one timestamp.
    const MINIMAL_LOG_HEADER: u64 = 0x3000000000000029;

    #[fuchsia::test]
    fn minimal_header() {
        let mut poked = Header(0);
        poked.set_type(TRACING_FORMAT_LOG_RECORD_TYPE);
        poked.set_size_words(2);
        poked.set_severity(Severity::Info.into_primitive());

        assert_eq!(
            poked.0, MINIMAL_LOG_HEADER,
            "minimal log header should only describe type, size, and severity"
        );
    }

    #[fuchsia::test]
    fn no_args_roundtrip() {
        let mut expected_record = MINIMAL_LOG_HEADER.to_le_bytes().to_vec();
        let timestamp = 5_000_000i64;
        expected_record.extend(timestamp.to_le_bytes());

        assert_roundtrips(
            Record { timestamp, severity: Severity::Info.into_primitive(), arguments: vec![] },
            Encoder::write_record,
            try_parse_record,
            Some(&expected_record),
        );
    }

    #[fuchsia::test]
    fn signed_arg_roundtrip() {
        assert_roundtrips(
            Argument { name: String::from("signed"), value: Value::SignedInt(-1999) },
            |encoder, val| encoder.write_argument(crate::encode::Argument::from(val)),
            parse_argument,
            None,
        );
    }

    #[fuchsia::test]
    fn unsigned_arg_roundtrip() {
        assert_roundtrips(
            Argument { name: String::from("unsigned"), value: Value::UnsignedInt(42) },
            |encoder, val| encoder.write_argument(crate::encode::Argument::from(val)),
            parse_argument,
            None,
        );
    }

    #[fuchsia::test]
    fn text_arg_roundtrip() {
        assert_roundtrips(
            Argument { name: String::from("stringarg"), value: Value::Text(String::from("owo")) },
            |encoder, val| encoder.write_argument(crate::encode::Argument::from(val)),
            parse_argument,
            None,
        );
    }

    #[fuchsia::test]
    fn float_arg_roundtrip() {
        assert_roundtrips(
            Argument { name: String::from("float"), value: Value::Floating(3.25) },
            |encoder, val| encoder.write_argument(crate::encode::Argument::from(val)),
            parse_argument,
            None,
        );
    }

    #[fuchsia::test]
    fn bool_arg_roundtrip() {
        assert_roundtrips(
            Argument { name: String::from("bool"), value: Value::Boolean(false) },
            |encoder, val| encoder.write_argument(crate::encode::Argument::from(val)),
            parse_argument,
            None,
        );
    }

    #[fuchsia::test]
    fn arg_of_each_type_roundtrips() {
        assert_roundtrips(
            Record {
                timestamp: zx::Time::get_monotonic().into_nanos(),
                severity: Severity::Warn.into_primitive(),
                arguments: vec![
                    Argument { name: String::from("signed"), value: Value::SignedInt(-10) },
                    Argument { name: String::from("unsigned"), value: Value::SignedInt(7) },
                    Argument { name: String::from("float"), value: Value::Floating(3.25) },
                    Argument { name: String::from("bool"), value: Value::Boolean(true) },
                    Argument {
                        name: String::from("msg"),
                        value: Value::Text(String::from("test message one")),
                    },
                ],
            },
            Encoder::write_record,
            try_parse_record,
            None,
        );
    }

    #[fuchsia::test]
    fn multiple_string_args() {
        assert_roundtrips(
            Record {
                timestamp: zx::Time::get_monotonic().into_nanos(),
                severity: Severity::Trace.into_primitive(),
                arguments: vec![
                    Argument {
                        name: String::from("msg"),
                        value: Value::Text(String::from("test message one")),
                    },
                    Argument {
                        name: String::from("msg2"),
                        value: Value::Text(String::from("test message two")),
                    },
                    Argument {
                        name: String::from("msg3"),
                        value: Value::Text(String::from("test message three")),
                    },
                ],
            },
            Encoder::write_record,
            try_parse_record,
            None,
        );
    }

    #[fuchsia::test]
    fn invalid_records() {
        // invalid word size
        let mut encoder = Encoder::new(Cursor::new(vec![0; BUF_LEN]));
        let mut header = Header(0);
        header.set_type(TRACING_FORMAT_LOG_RECORD_TYPE);
        header.set_size_words(0); // invalid, should be at least 2 as header and time are included
        encoder.buf.put_u64_le(header.0).unwrap();
        encoder.buf.put_i64_le(zx::Time::get_monotonic().into_nanos()).unwrap();
        encoder
            .write_argument(crate::encode::Argument {
                name: "msg",
                value: crate::encode::Value::Text("test message one"),
            })
            .unwrap();

        assert!(try_parse_record(encoder.buf.get_ref()).is_err());
    }
}