summaryrefslogtreecommitdiff
path: root/src/instr/rt.zig
blob: 3719409990201ba6923efb0f206499d3c3e5afba (plain) (blame)
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
//! libztrace-rt.a: what compiler-instrumented code calls, linked into the
//! target; every call becomes an event in the ring the tracer handed over,
//! or returns at once when there is none. Two faces:
//!
//! Our pass (llvm/pass.cpp):
//!   __zt_line(file, line, column)          line      a0=file a1=line a2=column
//!   __zt_store(addr, size)                 mem_write a0=addr a1=len, the bytes (see `wrote`)
//!   __zt_load(addr, size)                  mem_read  a0=addr a1=len
//!   __zt_enter(name, type, sp, site)       call      a0=function name a1=call site a2=sp a3=function type
//!   __zt_exit(name, value, size)           ret       a0=function name a1=value, aux=bytes of value (0: none)
//!   __zt_var(addr, size, name, type, arg)  variable  a0=addr a1=size a2=name a3=type, aux=N for the Nth parameter
//! file, name and type arrive as pointers to text records and leave as
//! string ids. The records, and the program's globals, sit in two sections
//! the pass filled: before the first event the runtime numbers the texts
//! and emits each as a `string` (from thread 0: no thread), and each global
//! as a `variable` with one `mem_write` of what it holds, from the thread
//! that got there first, which is inside no call yet. A local is declared,
//! never dumped: what it holds arrives with the stores.
//!
//! No pc in `line`, `mem_write` and `mem_read`, though event.zig has a
//! place for one: the source line is the place, and seven bytes would be a
//! third of each of them in a recording.
//!
//! Stock clang, no plugin:
//!   -fsanitize-coverage=trace-pc-guard     edge      a0=pc
//!   -fsanitize-coverage=trace-cmp          cmp       aux=operand bytes a0=pc a1=lhs a2=rhs
//!   -finstrument-functions                 call      a0=function address a1=call site a2=frame
//!                                          ret       a0=function address
//!
//! Raw syscalls only, no allocation: a store may be anywhere.
// shortcut: one program, one runtime: the texts of a shared library loaded later are never numbered; walk its sections on dlopen when a target needs it.
const std = @import("std");
const linux = std.os.linux;
const zt = @import("ztrace");

/// 0 untried, 1 starting, 2 tracing, 3 not traced.
var state: std.atomic.Value(u8) = .init(0);
var ring: *zt.Ring = undefined;

fn start(tid: u32) bool {
    @branchHint(.cold);
    while (true) switch (state.load(.acquire)) {
        2 => return true,
        3 => return false,
        // Another thread is emitting the names: nothing may get ahead of them.
        1 => _ = linux.sched_yield(),
        else => if (state.cmpxchgStrong(0, 1, .acquire, .monotonic) == null) {
            const found = zt.client.get();
            if (found) |r| {
                ring = r;
                names();
                globals(tid);
            }
            state.store(if (found != null) 2 else 3, .release);
        },
    };
}

fn put(tid: u32, kind: zt.Kind, aux: u16, a: [6]u64, payload: []const u8) void {
    ring.emit(.{ .ts = zt.event.now(), .tid = tid, .kind = kind, .aux = aux, .a = a }, payload);
}

const Thread = struct {
    /// A gettid a thread, not an event: the syscall would be half the cost
    /// of an event.
    // shortcut: a forked child keeps its parent's tid; reset it in a fork hook once a target forks.
    tid: u32 = 0,
    inside: bool = false,
};
threadlocal var thread: Thread = .{};

/// The calling thread, for one event; `inside` is the caller's to clear.
/// Null when nobody listens, or when the thread is already in here: what the
/// runtime calls (memcpy and strlen, in a libc compiled with the pass) must
/// not trace itself.
fn enter() ?*Thread {
    const s = state.load(.monotonic);
    if (s == 3) return null;
    const t = &thread;
    if (t.inside) return null;
    t.inside = true;
    if (t.tid == 0) t.tid = @intCast(linux.gettid());
    if (s == 2 or start(t.tid)) return t;
    t.inside = false;
    return null;
}

/// An event with no text in it: a text's id is read after `enter`, which
/// may be what numbers it.
fn emit(kind: zt.Kind, aux: u16, a: [6]u64, payload: []const u8) void {
    const t = enter() orelse return;
    defer t.inside = false;
    put(t.tid, kind, aux, a, payload);
}

/// The `mem_write`s of one write (a store, a memset, a memcpy, a global's
/// first contents), in pieces that fit the smallest ring. a1 is how much was
/// written from a0 on, so the first piece says the whole length; a payload
/// shorter than a1 is all that is recorded of it. A write is recorded up to
/// `most`: a megabyte cleared is not a megabyte in the recording.
const most = 64 << 10;

fn wrote(tid: u32, addr: usize, len: usize) void {
    var at: usize = 0;
    while (at < @min(len, most)) : (at += 2048) {
        const bytes: [*]const u8 = @ptrFromInt(addr + at);
        put(tid, .mem_write, 0, .{ addr + at, len - at, 0, 0, 0, 0 }, bytes[0..@min(len - at, 2048)]);
    }
}

/// The bounds the linker gives a section named like a C identifier; both 0
/// when no object has the section.
fn section(comptime name: []const u8) [2]usize {
    return .{
        @intFromPtr(@extern(?*const u8, .{ .name = "__start_" ++ name, .linkage = .weak })),
        @intFromPtr(@extern(?*const u8, .{ .name = "__stop_" ++ name, .linkage = .weak })),
    };
}

/// A text of the program, in section ztrace_strs: this, then the text,
/// padded to 8 bytes.
const Text = extern struct {
    /// The string id. The pass leaves 0, to be numbered here from 1: ids
    /// small enough to cost a recording a byte or two each. It fixes only
    /// the ids nothing but a viewer looks up (`types.defId`).
    id: u64,
    len: u64,
};

fn names() void {
    var at, const end = section("ztrace_strs");
    var next: u64 = 0;
    while (at < end) {
        const text: *Text = @ptrFromInt(at);
        if (text.id == 0) {
            next += 1;
            text.id = next;
        }
        const bytes: [*]const u8 = @ptrFromInt(at + @sizeOf(Text));
        // A text the ring cannot hold is cut: better than none of it.
        put(0, .string, 0, .{ text.id, 0, 0, 0, 0, 0 }, bytes[0..@min(text.len, ring.hdr.capacity / 2)]);
        at += @sizeOf(Text) + std.mem.alignForward(usize, text.len, 8);
    }
}

/// ztrace_vars: (address, size, name, type)*.
fn globals(tid: u32) void {
    var at, const end = section("ztrace_vars");
    while (at < end) : (at += 32) {
        const g: *const extern struct { addr: u64, size: u64, name: *const Text, type: *const Text } = @ptrFromInt(at);
        put(tid, .variable, 0, .{ g.addr, g.size, g.name.id, g.type.id, 0, 0 }, "");
        wrote(tid, g.addr, g.size);
    }
}

// The pass.

export fn __zt_line(file: *const Text, line: u32, column: u32) void {
    const t = enter() orelse return;
    defer t.inside = false;
    put(t.tid, .line, 0, .{ file.id, line, column, 0, 0, 0 }, "");
}

export fn __zt_store(addr: usize, size: u64) void {
    const t = enter() orelse return;
    defer t.inside = false;
    wrote(t.tid, addr, size);
}

export fn __zt_load(addr: usize, size: u64) void {
    emit(.mem_read, 0, .{ addr, size, 0, 0, 0, 0 }, "");
}

export fn __zt_enter(name: *const Text, type_text: *const Text, sp: usize, site: usize) void {
    const t = enter() orelse return;
    defer t.inside = false;
    put(t.tid, .call, 0, .{ name.id, site, sp, type_text.id, 0, 0 }, "");
}

export fn __zt_exit(name: *const Text, value: u64, size: u32) void {
    const t = enter() orelse return;
    defer t.inside = false;
    put(t.tid, .ret, @intCast(size), .{ name.id, value, 0, 0, 0, 0 }, "");
}

export fn __zt_var(addr: usize, size: u64, name: *const Text, type_text: *const Text, arg: u32) void {
    const t = enter() orelse return;
    defer t.inside = false;
    put(t.tid, .variable, @truncate(arg), .{ addr, size, name.id, type_text.id, 0, 0 }, "");
}

// Stock clang.

export fn __sanitizer_cov_trace_pc_guard_init(_: *u32, _: *u32) void {}

export fn __sanitizer_cov_trace_pc_guard(_: *u32) void {
    emit(.edge, 0, .{ @returnAddress(), 0, 0, 0, 0, 0 }, "");
}

comptime {
    for (.{ u8, u16, u32, u64 }) |T| {
        const f = struct {
            fn cmp(lhs: T, rhs: T) callconv(.c) void {
                emit(.cmp, @sizeOf(T), .{ @returnAddress(), lhs, rhs, 0, 0, 0 }, "");
            }
        }.cmp;
        for (.{ "", "const_" }) |c| @export(&f, .{ .name = std.fmt.comptimePrint("__sanitizer_cov_trace_{s}cmp{d}", .{ c, @sizeOf(T) }) });
    }
}

/// `cases` is (count, bits, values...). One event a switch, not one a case:
/// rhs is the case taken, or the first when it is the default.
export fn __sanitizer_cov_trace_switch(value: u64, cases: [*]const u64) void {
    var rhs = cases[2];
    for (cases[2..][0..cases[0]]) |c| {
        if (c == value) rhs = c;
    }
    emit(.cmp, @intCast(cases[1] / 8), .{ @returnAddress(), value, rhs, 0, 0, 0 }, "");
}

export fn __cyg_profile_func_enter(function: usize, site: usize) void {
    emit(.call, 0, .{ function, site, @frameAddress(), 0, 0, 0 }, "");
}

export fn __cyg_profile_func_exit(function: usize, _: usize) void {
    emit(.ret, 0, .{ function, 0, 0, 0, 0, 0 }, "");
}