functiontrace_server/function_trace.rs
1use serde::Deserialize;
2use std::borrow::Cow;
3use std::num::NonZeroU32;
4use std::time::Duration;
5
6/// A representation of the various trace messages a client thread can send us during normal
7/// tracing operation.
8///
9/// We use [`Cow`] instead of [`String`] because most strings should be zero-copy deserializable.
10#[derive(Deserialize, Debug)]
11#[serde(tag = "type")]
12pub enum FunctionTrace<'event> {
13 /// A request to register this as a new thread.
14 ///
15 /// **NOTE**: This must be sent as the first message from a new thread, and may not be sent
16 /// twice by any thread.
17 RegisterThread(ThreadRegistration),
18 Call {
19 time: Duration,
20 #[serde(borrow)]
21 func_name: Cow<'event, str>,
22 #[serde(borrow)]
23 filename: Cow<'event, str>,
24 linenumber: NonZeroU32,
25 },
26 Return {
27 time: Duration,
28 #[serde(borrow)]
29 func_name: Cow<'event, str>,
30 },
31 NativeCall {
32 time: Duration,
33 #[serde(borrow)]
34 func_name: Cow<'event, str>,
35 #[serde(borrow)]
36 module_name: Cow<'event, str>,
37 },
38 NativeReturn {
39 time: Duration,
40 #[serde(borrow)]
41 func_name: Cow<'event, str>,
42 },
43 Exception {
44 time: Duration,
45 #[serde(borrow)]
46 exception_type: Cow<'event, str>,
47 #[serde(borrow)]
48 exception_value: Cow<'event, str>,
49 filename: String,
50 linenumber: NonZeroU32,
51 },
52 Log {
53 time: Duration,
54 #[serde(borrow)]
55 log_type: Cow<'event, str>,
56 #[serde(borrow)]
57 log_value: Cow<'event, str>,
58 },
59 Import {
60 time: Duration,
61 #[serde(borrow)]
62 module_name: Cow<'event, str>,
63 },
64 Allocation {
65 time: Duration,
66 details: AllocationDetails,
67 },
68}
69
70/// Information about allocations.
71#[derive(Deserialize, Debug)]
72#[serde(tag = "type")]
73pub enum AllocationDetails {
74 /// The amount and location of a new allocation
75 Alloc { bytes: usize, addr: usize },
76 /// The new size of a reallocation from `old_addr` to `new_addr`.
77 Realloc {
78 bytes: usize,
79 old_addr: usize,
80 new_addr: usize,
81 },
82 /// The address that was `free()`ed.
83 Free { old_addr: usize },
84}
85
86/// Information relevant for initializing a trace.
87#[derive(Deserialize, Debug)]
88pub struct TraceInitialization {
89 /// The name (typically based off of argv) of the program initializing the trace (ex:
90 /// `hello.py world`).
91 pub program_name: String,
92 /// The version information of the `functiontrace` client talking to this server (ex:
93 /// `py-functiontrace 0.3.0`).
94 pub program_version: String,
95 /// The version for the underlying language the program is running on (ex: `Python 3.7.1`).
96 pub lang_version: String,
97 /// The general operating system platform the program is running on (ex: `darwin`).
98 pub platform: String,
99 /// An opaque system time that all other client-sent times will be relative to.
100 pub time: Duration,
101}
102
103/// This message contains information for registering threads (including a program's main thread).
104#[derive(Deserialize, Debug)]
105pub struct ThreadRegistration {
106 /// A system time for when this thread registered itself as being traced with FunctionTrace.
107 pub time: Duration,
108 /// The program name this thread corresponds to.
109 pub program_name: String,
110 /// The process this thread belongs to.
111 pub pid: usize,
112}