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
use serde::Deserialize;
use std::borrow::Cow;
use std::time::Duration;

/// A representation of the various trace messages a client thread can send us during normal
/// tracing operation.
///
/// We use [`Cow`] instead of [`String`] because most strings should be zero-copy deserializable.
#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
pub enum FunctionTrace<'event> {
    /// A request to register this as a new thread.
    ///
    /// **NOTE**: This must be sent as the first message from a new thread, and may not be sent
    /// twice by any thread.
    RegisterThread(ThreadRegistration),
    Call {
        time: Duration,
        func_name: Cow<'event, str>,
        filename: Cow<'event, str>,
        linenumber: u32,
    },
    Return {
        time: Duration,
        func_name: Cow<'event, str>,
    },
    NativeCall {
        time: Duration,
        func_name: Cow<'event, str>,
        module_name: Cow<'event, str>,
    },
    NativeReturn {
        time: Duration,
        func_name: Cow<'event, str>,
    },
    Exception {
        time: Duration,
        exception_type: Cow<'event, str>,
        exception_value: Cow<'event, str>,
        filename: String,
        linenumber: u32,
    },
    Log {
        time: Duration,
        log_type: Cow<'event, str>,
        log_value: Cow<'event, str>,
    },
    Import {
        time: Duration,
        module_name: Cow<'event, str>,
    },
    Allocation {
        time: Duration,
        details: AllocationDetails,
    },
}

/// Information about allocations.
#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
pub enum AllocationDetails {
    /// The amount and location of a new allocation
    Alloc { bytes: usize, addr: usize },
    /// The new size of a reallocation from `old_addr` to `new_addr`.
    Realloc {
        bytes: usize,
        old_addr: usize,
        new_addr: usize,
    },
    /// The address that was `free()`ed.
    Free { old_addr: usize },
}

/// Information relevant for initializing a trace.
#[derive(Deserialize, Debug)]
pub struct TraceInitialization {
    /// The name (typically based off of argv) of the program initializing the trace (ex:
    /// `hello.py world`).
    pub program_name: String,
    /// The version information of the `functiontrace` client talking to this server (ex:
    /// `py-functiontrace 0.3.0`).
    pub program_version: String,
    /// The version for the underlying language the program is running on (ex: `Python 3.7.1`).
    pub lang_version: String,
    /// The general operating system platform the program is running on (ex: `darwin`).
    pub platform: String,
    /// An opaque system time that all other client-sent times will be relative to.
    pub time: Duration,
}

/// This message contains information for registering threads (including a program's main thread).
#[derive(Deserialize, Debug)]
pub struct ThreadRegistration {
    /// A system time for when this thread registered itself as being traced with FunctionTrace.
    pub time: Duration,
    /// The program name this thread corresponds to.
    pub program_name: String,
    /// The process this thread belongs to.
    pub pid: usize,
}